1010import pandas as pd
1111from datetime import datetime
1212from argparse import ArgumentParser
13- from glob import glob
13+ from glob import glob as gg
1414
1515# Import medslik modules
1616from 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
440411if __name__ == "__main__" :
0 commit comments