Skip to content

Commit 4d0780e

Browse files
authored
Merge pull request #5 from CMCC-Foundation/no-shell
Removed shell equals true from the reported files
2 parents cf42471 + f698f81 commit 4d0780e

File tree

5 files changed

+108
-126
lines changed

5 files changed

+108
-126
lines changed

main.py

Lines changed: 81 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import pandas as pd
1111
from datetime import datetime
1212
from argparse import ArgumentParser
13-
from glob import glob
13+
from glob import glob as gg
1414

1515
# Import medslik modules
1616
from src.utils import Utils, Config, read_oilbase
@@ -183,16 +183,13 @@ def data_download_medslik(
183183
password=copernicus_pass,
184184
)
185185

186-
subprocess.run(
187-
[
188-
f'cp {output_path}*{identifier}*{config["simulation"]["name"]}*.nc {root_directory}/oce_files/'
189-
],
190-
shell=True,
191-
)
192-
subprocess.run(
193-
[f'rm {output_path}*{identifier}*{config["simulation"]["name"]}*.nc'],
194-
shell=True,
195-
)
186+
source_files = gg(f"{output_path}*{identifier}*{config['simulation']['name']}*.nc")
187+
destination = os.path.join(root_directory, "oce_files")
188+
for file in source_files:
189+
shutil.copy(file, destination)
190+
191+
for file in gg(f"{output_path}*{identifier}*{config['simulation']['name']}*.nc"):
192+
os.remove(file)
196193

197194
if config["download"]["download_wind"]:
198195
# ensuring .cdsapirc is created in the home directory
@@ -216,16 +213,13 @@ def data_download_medslik(
216213
)
217214
process_era5(output_path=output_path, output_name=output_name)
218215

219-
subprocess.run(
220-
[
221-
f'cp {output_path}*{identifier}*{config["simulation"]["name"]}*.nc {root_directory}/met_files/'
222-
],
223-
shell=True,
224-
)
225-
subprocess.run(
226-
[f'rm {output_path}*{identifier}*{config["simulation"]["name"]}*.nc'],
227-
shell=True,
228-
)
216+
source_files = gg(f"{output_path}*{identifier}*{config['simulation']['name']}*.nc")
217+
destination = os.path.join(root_directory, "met_files")
218+
for file in source_files:
219+
shutil.copy(file, destination)
220+
221+
for file in gg(f"{output_path}*{identifier}*{config['simulation']['name']}*.nc"):
222+
os.remove(file)
229223

230224
def run_preproc(config: dict, exp_folder: str, lon_min, lon_max, lat_min, lat_max):
231225
"""
@@ -343,98 +337,75 @@ def run_medslik_sim(self, simdir, simname, separate_slicks=False):
343337

344338
output_dir = f"{model_dir}OUT/MDK_SIM_{year}_{month:02d}_{day:02d}_{hour:02d}{minute:02d}_{simname}/."
345339

346-
# removing old outputes just to be sure
347-
subprocess.run([f"rm -rf {output_dir}"], shell=True)
348-
349-
if separate_slicks == False:
350-
# copy METOCEAN files to MEDSLIK-II installation
351-
subprocess.run(
352-
[f"cp {simdir}{simname}/oce_files/*.mrc {model_dir}RUN/TEMP/OCE/"],
353-
shell=True,
354-
check=True,
355-
)
356-
subprocess.run(
357-
[f"cp {simdir}{simname}/met_files/*.eri {model_dir}RUN/TEMP/MET/"],
358-
shell=True,
359-
check=True,
360-
)
361-
# copy bnc files
362-
subprocess.run(
363-
[f"cp {simdir}{simname}/bnc_files/* {model_dir}DTM_INP/"],
364-
shell=True,
365-
check=True,
366-
)
367-
# copy Extract and config files
368-
subprocess.run(
369-
[
370-
f"cp {simdir}{simname}/xp_files/medslik_II.for {model_dir}RUN/MODEL_SRC/"
371-
],
372-
shell=True,
373-
check=True,
374-
)
375-
subprocess.run(
376-
[f"cp {simdir}{simname}/xp_files/config2.txt {model_dir}RUN/"],
377-
shell=True,
378-
check=True,
379-
)
380-
subprocess.run(
381-
[f"cp {simdir}{simname}/xp_files/config1.txt {model_dir}RUN/"],
382-
shell=True,
383-
check=True,
384-
)
385-
# Compile and start running
386-
subprocess.run(
387-
[f"cd {model_dir}RUN/; sh MODEL_SRC/compile.sh; ./RUN.sh"],
388-
shell=True,
389-
check=True,
390-
)
340+
# Remove old outputs (equivalent to `rm -rf`)
341+
if os.path.exists(output_dir):
342+
shutil.rmtree(output_dir)
343+
344+
if not separate_slicks:
345+
# Copy METOCEAN files
346+
oce_files = gg(f"{simdir}{simname}/oce_files/*.mrc")
347+
met_files = gg(f"{simdir}{simname}/met_files/*.eri")
348+
bnc_files = gg(f"{simdir}{simname}/bnc_files/*")
349+
xp_files = {
350+
"medslik_II.for": os.path.join(simdir, simname, "xp_files", "medslik_II.for"),
351+
"config2.txt": os.path.join(simdir, simname, "xp_files", "config2.txt"),
352+
"config1.txt": os.path.join(simdir, simname, "xp_files", "config1.txt"),
353+
}
354+
355+
# Copy METOCEAN, MET, and BNC files
356+
for file in oce_files:
357+
shutil.copy(file, os.path.join(model_dir, "RUN", "TEMP", "OCE"))
358+
for file in met_files:
359+
shutil.copy(file, os.path.join(model_dir, "RUN", "TEMP", "MET"))
360+
for file in bnc_files:
361+
shutil.copy(file, os.path.join(model_dir, "DTM_INP"))
362+
363+
# Copy other required files
364+
for dest, src in xp_files.items():
365+
shutil.copy(src, os.path.join(model_dir, "RUN", dest if "config" in dest else "MODEL_SRC"))
366+
367+
# Compile and start running (replacing `cd` with `cwd`)
368+
compile_script = "MODEL_SRC/compile.sh"
369+
run_script = "RUN.sh"
370+
subprocess.run(["sh", compile_script], check=True, cwd=os.path.join(model_dir, "RUN"))
371+
subprocess.run(["./" + run_script], check=True, cwd=os.path.join(model_dir, "RUN"))
391372

392373
else:
393-
slicks = glob(f"{simdir}{simname}/xp_files/*/")
394-
for i in range(0, len(slicks)):
395-
subprocess.run(
396-
[f"cp {simdir}{simname}/oce_files/*.mrc {model_dir}RUN/TEMP/OCE/"],
397-
shell=True,
398-
)
399-
subprocess.run(
400-
[f"cp {simdir}{simname}/met_files/*.eri {model_dir}RUN/TEMP/MET/"],
401-
shell=True,
402-
)
403-
# copy bnc files
404-
subprocess.run(
405-
[f"cp {simdir}{simname}/bnc_files/* {model_dir}DTM_INP/"],
406-
shell=True,
407-
)
408-
# copy Extract and config files
409-
subprocess.run(
410-
[
411-
f"cp {simdir}{simname}/xp_files/medslik_II.for {model_dir}RUN/MODEL_SRC/"
412-
],
413-
shell=True,
414-
)
415-
subprocess.run(
416-
[f"cp {simdir}{simname}/xp_files/config2.txt {model_dir}RUN/"],
417-
shell=True,
418-
)
419-
subprocess.run(
420-
[
421-
f"cp {simdir}{simname}/xp_files/slick{i+1}/config1.txt {model_dir}RUN/"
422-
],
423-
shell=True,
424-
)
425-
# Compile and start running
426-
subprocess.run(
427-
[f"cd {model_dir}RUN/; sh MODEL_SRC/compile.sh; ./RUN.sh"],
428-
shell=True,
429-
check=True,
430-
)
374+
# Handle separate slicks
375+
slicks = gg(f"{simdir}{simname}/xp_files/*/")
376+
for i, slick_dir in enumerate(slicks):
377+
# Copy METOCEAN, MET, and BNC files
378+
oce_files = gg(f"{simdir}{simname}/oce_files/*.mrc")
379+
met_files = gg(f"{simdir}{simname}/met_files/*.eri")
380+
bnc_files = gg(f"{simdir}{simname}/bnc_files/*")
381+
config1_path = os.path.join(simdir, simname, f"xp_files/slick{i + 1}/config1.txt")
382+
383+
for file in oce_files:
384+
shutil.copy(file, os.path.join(model_dir, "RUN", "TEMP", "OCE"))
385+
for file in met_files:
386+
shutil.copy(file, os.path.join(model_dir, "RUN", "TEMP", "MET"))
387+
for file in bnc_files:
388+
shutil.copy(file, os.path.join(model_dir, "DTM_INP"))
389+
shutil.copy(config1_path, os.path.join(model_dir, "RUN", "config1.txt"))
431390

432-
# Send files to case dir and remove temp files
433-
subprocess.run([f"cp -r {output_dir} {simdir}{simname}/out_files/"], shell=True)
434-
subprocess.run(
435-
[f"rm -rf {simdir}{simname}/out_files/MET {simdir}{simname}/out_files/OCE"],
436-
shell=True,
437-
)
391+
# Compile and start running
392+
compile_script = "MODEL_SRC/compile.sh"
393+
run_script = "RUN.sh"
394+
subprocess.run(["sh", compile_script], check=True, cwd=os.path.join(model_dir, "RUN"))
395+
subprocess.run(["./" + run_script], check=True, cwd=os.path.join(model_dir, "RUN"))
396+
397+
# Copy output files (replacing `cp -r`)
398+
output_dest = os.path.join(simdir, simname, "out_files")
399+
if os.path.exists(output_dir):
400+
shutil.copytree(output_dir, output_dest, dirs_exist_ok=True)
401+
402+
# Remove temporary MET and OCE files (equivalent to `rm -rf`)
403+
temp_met = os.path.join(output_dest, "MET")
404+
temp_oce = os.path.join(output_dest, "OCE")
405+
if os.path.exists(temp_met):
406+
shutil.rmtree(temp_met)
407+
if os.path.exists(temp_oce):
408+
shutil.rmtree(temp_oce)
438409

439410

440411
if __name__ == "__main__":

src/download/download_copernicus_parser.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ def download_copernicus(
7474
ds.to_netcdf(output_name)
7575

7676
# remove the temporary files
77-
subprocess.run([f"rm -rf {output_path}temp.nc"], shell=True)
77+
temp_file = os.path.join(output_path, "temp.nc")
78+
if os.path.exists(temp_file):
79+
os.remove(temp_file)
7880

7981
else:
8082

@@ -150,6 +152,7 @@ def download_copernicus(
150152
ds.to_netcdf(output_name)
151153

152154
# remove the temporary files
153-
subprocess.run(
154-
[f"rm -rf {output_path}/curr.nc {output_path}/temp.nc"], shell=True
155-
)
155+
temp_files = [os.path.join(output_path, "curr.nc"), os.path.join(output_path, "temp.nc")]
156+
for temp_file in temp_files:
157+
if os.path.exists(temp_file):
158+
os.remove(temp_file)

src/download/download_era5_parser.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import argparse
77
import subprocess
88
import time
9+
from glob import glob as gg
910

1011
# Functions outside this script
1112
from WITOIL_iMagine.src.utils.utils import *
@@ -79,7 +80,10 @@ def process_era5(output_path,output_name):
7980
met.to_netcdf(output_name)
8081

8182
#remove the temporary files
82-
subprocess.run([f'rm -rf {output_path}/temp*.nc'],shell=True)
83+
temp_files = gg(os.path.join(output_path, "temp*.nc"))
84+
for temp_file in temp_files:
85+
if os.path.exists(temp_file):
86+
os.remove(temp_file)
8387

8488

8589
if __name__ == '__main__':
@@ -125,5 +129,4 @@ def process_era5(output_path,output_name):
125129

126130
# os.system('rm ' + out_folder + '/output.nc')
127131

128-
# os.system('rm ' + out_folder + '/pre_*.nc')
129-
132+
# os.system('rm ' + out_folder + '/pre_*.nc')

src/plot/plot_mdk3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def create_gif(self):
180180
f"magick -delay 20 -loop 0 {path}/*surf_oil_*.png \
181181
{self.out_figures}/oil_concentration_{self.config['simulation']['name']}.gif"
182182
],
183-
shell=True,
183+
# shell=True,
184184
)
185185

186186
def plot_pyngl(
@@ -208,7 +208,7 @@ def plot_pyngl(
208208
{sim_length} {plot_lon[0]} {plot_lon[1]} \
209209
{plot_lat[0]} {plot_lat[1]}"
210210
],
211-
shell=True,
211+
# shell=True,
212212
check=True,
213213
)
214214

src/preprocessing/preprocessing_mdk3.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,20 @@ def process_medslik_memmory_array(self):
219219

220220
med_for = f'{self.exp_folder}/xp_files/medslik_II.for'
221221

222-
subprocess.run([f'cp WITOIL_iMagine/src/templates/medslik_II_template.for {med_for}'],shell=True)
222+
# Refactored: Copying the template file
223+
template_path = 'WITOIL_iMagine/src/templates/medslik_II_template.for'
224+
shutil.copy(template_path, med_for)
223225

224-
# Replacing NMAX in medslik fortran with a python function
226+
# Replacing NMAX in medslik Fortran with a Python function
225227
Utils.search_and_replace(med_for, 'NMAX', str(nmax))
226228

227229
def configuration_parameters(self):
228230

229-
subprocess.run([f'cp WITOIL_iMagine/src/templates/config2.txt {self.exp_folder}/xp_files/config2.txt'],shell=True)
231+
source_file = 'WITOIL_iMagine/src/templates/config2.txt'
232+
destination_file = f'{self.exp_folder}/xp_files/config2.txt'
233+
234+
# Refactored: Copy the file
235+
shutil.copy(source_file, destination_file)
230236

231237
def common_grid(self):
232238

@@ -271,9 +277,9 @@ def write_config_files(self,
271277
config_file = f"WITOIL_iMagine/cases/{simname}/xp_files/config1.txt"
272278
else:
273279
config_file = f"WITOIL_iMagine/cases/{simname}/xp_files/slick{s_num+1}/config1.txt"
274-
subprocess.run(
275-
[f"cp WITOIL_iMagine/src/templates/config1_template_0.txt {config_file}"], shell=True
276-
)
280+
# Refactored: Copy the template file
281+
source_file = "WITOIL_iMagine/src/templates/config1_template_0.txt"
282+
shutil.copy(source_file, config_file)
277283
# adding spill Name - Add slick number if separate slicks
278284
if separate_slicks == False:
279285
Utils.search_and_replace(config_file, "RUNNAME", simname)
@@ -331,4 +337,3 @@ def write_config_files(self,
331337
if __name__ == "__main__":
332338

333339
pass
334-

0 commit comments

Comments
 (0)