skeleton.sh 8.2 KB

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