skeleton.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # Function leap days calculates the number of leap days (29th of Februrary) in
  2. # a time intervall between two dates.
  3. #
  4. # Usage leap_days START_DATE END_DATE
  5. #TODO: simplify this
  6. function leap_days()
  7. {
  8. local ld=0
  9. local frstYYYY=$(date -ud "$1" +%Y)
  10. local lastYYYY=$(date -ud "$2" +%Y)
  11. set +e
  12. # Check first year for leap day between start and end date
  13. $(date -ud "${frstYYYY}-02-29" > /dev/null 2>&1) \
  14. && (( $(date -ud "$1" +%s) < $(date -ud "${frstYYYY}-03-01" +%s) )) \
  15. && (( $(date -ud "$2" +%s) > $(date -ud "${lastYYYY}-02-28" +%s) )) \
  16. && (( ld++ ))
  17. # Check intermediate years for leap day
  18. for (( y=(( ${frstYYYY}+1 )); y<=(( ${lastYYYY}-1 )); y++ ))
  19. do
  20. $(date -ud "$y-02-29" > /dev/null 2>&1) && (( ld++ ))
  21. done
  22. # Check last year (if different from first year) for leap day between start
  23. # and end date
  24. (( $lastYYYY > $frstYYYY )) \
  25. && $(date -ud "${lastYYYY}-02-29" > /dev/null 2>&1) \
  26. && (( $(date -ud "$1" +%s) < $(date -ud "${frstYYYY}-03-01" +%s) )) \
  27. && (( $(date -ud "$2" +%s) > $(date -ud "${lastYYYY}-02-28" +%s) )) \
  28. && (( ld++ ))
  29. set -e
  30. echo "$ld"
  31. }
  32. [[ $@ == *verbose* ]] && set -x
  33. #module load "${module_list:?}"
  34. module load ${module_list}
  35. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  36. #
  37. # Create run directory
  38. # Copy executables
  39. # Copy XIOS XML files into run directory
  40. # Copy namelist files into run directory
  41. #
  42. mkdir -p "${run_dir:?}"
  43. cp -u xios_config/*xml "${run_dir}"
  44. cp -u namelists/* "${run_dir}"
  45. cd "${run_dir}"
  46. cp -u "${nem_exe_file:?}" .
  47. cp -u "${xio_exe_file:?}" .
  48. # Write fake file for previous fresh water budget adjustment (nn_fwb==2 in namelist)
  49. [[ ! -f EMPave_old.dat ]] && echo " 0 0.0000000000000000E+00 0.0000000000000000E+00" > EMPave_old.dat
  50. #TODO: Enquiry about this
  51. #
  52. # Link data files into run directory
  53. # Attention: no space in filenames allowed!
  54. # (No protection against word splitting by design)
  55. #
  56. for file in "${ic_files[@]}"; do
  57. [[ ! -e ${file#*> } ]] && ln -sf $(sed 's/ *=> */ /' <<< "${ini_data_dir}/${ic_subdir}/${nem_grid}/$file")
  58. done
  59. for file in "${forcing_files[@]}"; do
  60. [[ ! -e ${file#*> } || "$file" == \** ]] && ln -sf $(sed 's/ *=> */ /' <<< "${ini_data_dir}/${forcing_subdir}/${nem_forcing_set}/$file")
  61. done
  62. for file in "${shared_files[@]}"; do
  63. [[ ! -e ${file#*> } ]] && ln -sf $(sed 's/ *=> */ /' <<< "${shared_dir}/$file")
  64. done
  65. #
  66. # Normalize date formats
  67. #
  68. run_start_date=$(date -uR -d "${run_start_date}")
  69. run_end_date="${run_start_date} + ${run_duration:?}"
  70. run_end_date=$(date -uR -d "${run_end_date}")
  71. run_start_epoch=$(date -u -d"${run_start_date}" +%s)
  72. run_end_epoch=$(date -u -d"${run_end_date}" +%s)
  73. for (( ; run_num_legs>0 ; run_num_legs-- ))
  74. do
  75. #
  76. # Initialize variables (using restart file if it exists)
  77. # Variables ending in '_date' are in RFC2822
  78. # Variables ending in '_epoch' are in nb of seconds since 1970-01-01
  79. #
  80. [[ -r "${info_file:?}" ]] && source "${info_file:?}"
  81. leg_start_date=${leg_end_date:-$run_start_date}
  82. leg_number=$((${leg_number:=0}+1))
  83. leg_start_epoch=$(date -u -d "${leg_start_date}" +%s)
  84. leg_end_epoch=$(date -u -d "${leg_start_date:?} + ${rst_freq:=$run_duration}" +%s)
  85. leg_end_date=$(date -uR -d@"${leg_end_epoch}")
  86. leg_length_sec=$(( leg_end_epoch - leg_start_epoch ))
  87. leg_start_sec=$(( leg_start_epoch - run_start_epoch ))
  88. leg_end_sec=$(( leg_end_epoch - run_start_epoch ))
  89. leg_start_date_yyyymmdd=$(date -u -d "${leg_start_date}" +%Y%m%d) # FIXME appears unused
  90. # Correct for leap days because NEMO standalone uses no-leap calendar
  91. leg_length_sec=$(( leg_length_sec - $(leap_days "${leg_start_date}" "${leg_end_date}")*24*3600 ))
  92. leg_start_sec=$(( leg_start_sec - $(leap_days "${run_start_date}" "${leg_start_date}")*24*3600 ))
  93. leg_end_sec=$(( leg_end_sec - $(leap_days "${run_start_date}" "${leg_end_date}")*24*3600 ))
  94. (( leg_number > 1 )) && leg_is_restart=true || leg_is_restart=false
  95. #
  96. # Compute leg end-date and trim if necessary
  97. #
  98. (( leg_end_epoch > run_end_epoch )) && leg_end_date=${run_end_epoch}
  99. #
  100. # Check whether there is some work left to do
  101. #
  102. if (( leg_start_epoch >= run_end_epoch ))
  103. then
  104. echo "Leg start date equal to or after end of simulation."
  105. echo "Nothing left to do. Cleaning and exiting."
  106. for (( n=0 ; n<nem_numproc ; n++ ))
  107. do
  108. np=$(printf %04d ${n})
  109. rm -f "restart_oce_${np}.nc"
  110. rm -f "restart_ice_${np}.nc"
  111. done
  112. exit 0
  113. fi
  114. #
  115. # Update namelist
  116. #
  117. source build_namelist_cfg.sh > namelist_cfg
  118. #
  119. # Link the restart files
  120. #
  121. ns=$(printf %08d $(( leg_start_sec / nem_time_step_sec - nem_restart_offset )))
  122. if ((leg_start_sec > 0 )); then
  123. for (( n=0 ; n<nem_numproc ; n++ ))
  124. do
  125. np=$(printf %04d ${n})
  126. [[ -f "${exp_name:?}_${ns}_restart_oce_${np}.nc" ]] || { echo "Error: restart file not found." ; exit 2 ; }
  127. ln -fs "${exp_name:?}_${ns}_restart_oce_${np}.nc" "restart_oce_${np}.nc"
  128. [[ -f "${exp_name:?}_${ns}_restart_ice_${np}.nc" ]] || { echo "Error: restart file not found." ; exit 2 ; }
  129. ln -fs "${exp_name:?}_${ns}_restart_ice_${np}.nc" "restart_ice_${np}.nc"
  130. done
  131. fi
  132. # Stop here is preponly was specified
  133. [[ $@ == *preponly* ]] && exit 0
  134. #
  135. # Run nemo
  136. #
  137. time_begin=$(date +%s)
  138. mpirun -np "${xio_numproc:?}" "${xio_exe_file:?}" : -np "${nem_numproc:?}" "${nem_exe_file:?}"
  139. time_end=$(date +%s)
  140. #############################
  141. formatted_leg_number=$(printf %03d $((leg_number)))
  142. #
  143. # Move NEMO output files to archive directory
  144. #
  145. outdir="${archive_dir:?}/output/${formatted_leg_number}"
  146. mkdir -p "${outdir}"
  147. shopt -s nullglob
  148. for v in grid_U grid_V grid_W grid_T icemod icemoa SBC SBC_scalar scalar
  149. do
  150. for f in ${exp_name}_??_????????_????????_${v}_????.nc
  151. do
  152. mv "$f" "$outdir/"
  153. done
  154. for f in ${exp_name}_??_????????_????????_${v}.nc
  155. do
  156. mv "$f" "$outdir/"
  157. done
  158. for f in ${exp_name}_??_${v}.nc
  159. do
  160. mv "$f" "$outdir/"
  161. done
  162. done
  163. #
  164. # Move NEMO restart files from previous run to archive directory
  165. #
  166. outdir="$archive_dir/restart/${formatted_leg_number}"
  167. mkdir -p "${outdir}"
  168. for f in ${exp_name}_${ns}_restart_???_????.nc
  169. do
  170. [ -f "$f" ] && mv "$f" "${outdir}"
  171. done
  172. #
  173. # Move log files to archive directory
  174. #
  175. outdir="$archive_dir/log/${formatted_leg_number}"
  176. mkdir -p "${outdir}"
  177. for f in ocean.output time.step solver.stat ; do mv "${f}" "${outdir}"; done
  178. #
  179. #
  180. ##############################
  181. shopt -u nullglob
  182. #
  183. # Write checkpoint control file
  184. # TODO: enquiry why 0 -1 +2 rather than 2- 1
  185. #
  186. tr=$(date -d "0 -$time_begin sec + $time_end sec" +%T)
  187. current_date=$(date +'%F %T')
  188. {
  189. echo "#"
  190. echo "# Finished leg at ${current_date} after ${tr} (hh:mm:ss)"
  191. echo "leg_number=${leg_number}"
  192. echo "leg_start_date=\"${leg_start_date}\""
  193. echo "leg_end_date=\"${leg_end_date}\""
  194. } | tee -a "${info_file}"
  195. done
  196. #
  197. # Move back to submission directory
  198. #
  199. cd - >/dev/null
  200. #
  201. # Check whether there is some work left to do in a further job
  202. #
  203. [[ $@ == *noresubmit* ]] && exit 0
  204. if (( leg_end_epoch < run_end_epoch )) ;
  205. then
  206. echo "Leg end earlier than end of simulation."
  207. echo "Submitting another job."
  208. #[[ $@ == *local* ]] && exec $0 $@ || scontrol requeue $SLURM_JOB_ID #TODO: factorize this
  209. if [[ "$@" == *local* ]]
  210. then
  211. exec "$0" "$@"
  212. elif [[ "${submit_command}" == *sbatch* ]]
  213. then
  214. "${submit_command}" "$0" "$@" | awk '{print $4}' >> "${run_dir}"/.coral_jobs
  215. else
  216. "${submit_command}" -v PBS_OPTIONS="$@" "$0" | tee -a coral_jobs
  217. sleep 2
  218. jobid=`cat coral_jobs`
  219. rm -f coral_jobs
  220. jobid=${jobid%.*}
  221. echo "${jobid}" >> "${run_dir}"/.coral_jobs
  222. fi
  223. else
  224. echo "Nothing left to do. Cleaning and exiting." # FIXME Factorize this (we have two exit points)
  225. for (( n=0 ; n<nem_numproc ; n++ ))
  226. do
  227. np=$(printf %04d ${n})
  228. rm -f "restart_oce_${np}.nc"
  229. rm -f "restart_ice_${np}.nc"
  230. done
  231. fi
  232. exit 0