test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import os
  2. import sys
  3. import glob
  4. import subprocess
  5. # List of variable to save for output
  6. PLASIM_VAR = ['tas','hfls','hfss']
  7. OCEAN_VAR = ['heata','fldoa','sst']
  8. LSG_VAR = ['fluxhea', 'tbound']
  9. PLASIM_VAR = ['time','lat','lon'] + PLASIM_VAR
  10. OCEAN_VAR = ['time','lat','lon'] + OCEAN_VAR
  11. LSG_VAR = ['time','lat','lon','lev'] + LSG_VAR
  12. # check the python version
  13. if '3.6' not in sys.version:
  14. print("This script require Python 3.6 !")
  15. print("Try:")
  16. print("\n\tmodule load python3\n\n")
  17. print("and restart this script again.")
  18. print("Aborting...")
  19. sys.exit(1)
  20. #check if ecfs utils are loaded
  21. user = os.getenv('USER')
  22. try:
  23. dummy = subprocess.run(['els', 'ectmp:/'+user+'/'], check=True, stdout=subprocess.PIPE)
  24. except:
  25. print("This script require the ECFS toolchain to be loaded !")
  26. print("Try:")
  27. print("\n\tmodule load ecfs\n\n")
  28. print("and restart this script again.")
  29. print("Aborting...")
  30. sys.exit(1)
  31. #check if netcdf utils are loaded
  32. try:
  33. dummy = subprocess.run(['ncdump'], check=True, stderr=subprocess.PIPE)
  34. except:
  35. print("This script require NetCDF4 toolchain to be loaded !")
  36. print("Try:")
  37. print("\n\tmodule load netcdf4\n\n")
  38. print("and restart this script again.")
  39. print("Aborting...")
  40. sys.exit(1)
  41. home_dir = os.getenv("HOME")
  42. scratch_dir = os.getenv("SCRATCH")
  43. perm_dir = os.getenv("PERM")
  44. plasim_dir = home_dir + "/models/PLASIM/"
  45. if len(sys.argv) < 5:
  46. print('Bad arguments:', sys.argv)
  47. print('Usage:')
  48. print('\n\t python3 restart_ensemble_experiment.py where experiment ensemble_size number_of_years where_to_save\n')
  49. print('Arguments:\n')
  50. print('\twhere :\t\t\tWhere the experiment ensemble folders are located.')
  51. print('\texperiment :\t\tName of the experiment.')
  52. print('\tensemble_size :\t\tSize of the ensemble.')
  53. print('\tnumber_of_years :\tNumber of years simulated by one run of the experiment.')
  54. print('\twhere_to_save :\t\tOptional. Where to backup the previous run. If not provide, uses $PERM.')
  55. sys.exit(0)
  56. where = sys.argv[1]
  57. basedir = where.split('/')[-1]
  58. experiment = sys.argv[2]
  59. ensemble_size = int(sys.argv[3])
  60. restart_year_to_save = sys.argv[4]
  61. try:
  62. save = sys.argv[5]
  63. except:
  64. save = perm_dir
  65. save_light = save + "/"+experiment+"_light/"
  66. save += "/"+experiment+"/"
  67. # Check if the experiment folder exists
  68. experiment_folder = where+"/"+experiment+"/"
  69. if not os.path.isdir(experiment_folder):
  70. print("Experiment folder not found!")
  71. print("Create and start the experiment "+experiment+" first.")
  72. print("Aborting...")
  73. sys.exit(1)
  74. last_experiment_index = 1
  75. print('Backup of the previous run done !')
  76. if last_experiment_index > 0:
  77. print('Creating the tar archive of the experiment run number '+str(last_experiment_index))
  78. print('and saving it in ECFS temporary storage...')
  79. j = last_experiment_index
  80. yts = int(restart_year_to_save)
  81. os.system('tar -c -f '+scratch_dir+'/tmp/plasim_'+experiment+'_years_'+str(yts*(j-1)+1)+'to'+str(yts*j)+'.tar '+scratch_dir+'/tmp/'+experiment)
  82. os.system('rm -rf '+scratch_dir+'/tmp/'+experiment+'/*')
  83. queue = subprocess.run(['emkdir', '-p', 'ectmp:'+'/'+user+'/'+basedir+'/'+experiment], check=True)
  84. os.system('ecp -t '+scratch_dir+'/tmp/plasim_'+experiment+'_years_'+str(yts*(j-1)+1)+'to'+str(yts*j)+'.tar '+ 'ectmp:'+'/'+user+'/'+basedir+'/'+experiment+'/')
  85. print('Backup and move to ecfs done !')
  86. print('Starting the ensemble runs...')
  87. for i in range(1, ensemble_size+1):
  88. member_number = str(i).rjust(2, '0')
  89. job_name = 'plasim_'+experiment+'_'+member_number
  90. ensemble_member_folder = experiment_folder+'run_'+experiment+'_'+member_number
  91. os.system('qsub '+ensemble_member_folder+'/PBS_'+job_name)
  92. print("Experiment '"+experiment+"' ensemble restarted.")
  93. print('Check the status with: qstat -u '+user)