skeleton.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. function leap_days()
  6. {
  7. local ld=0
  8. local frstYYYY=$(date -ud "$1" +%Y)
  9. local lastYYYY=$(date -ud "$2" +%Y)
  10. set +e
  11. # Check first year for leap day between start and end date
  12. $(date -ud "${frstYYYY}-02-29" > /dev/null 2>&1) \
  13. && (( $(date -ud "$1" +%s) < $(date -ud "${frstYYYY}-03-01" +%s) )) \
  14. && (( $(date -ud "$2" +%s) > $(date -ud "${lastYYYY}-02-28" +%s) )) \
  15. && (( ld++ ))
  16. # Check intermediate years for leap day
  17. for (( y=(( ${frstYYYY}+1 )); y<=(( ${lastYYYY}-1 )); y++ ))
  18. do
  19. $(date -ud "$y-02-29" > /dev/null 2>&1) && (( ld++ ))
  20. done
  21. # Check last year (if different from first year) for leap day between start
  22. # and end date
  23. (( $lastYYYY > $frstYYYY )) \
  24. && $(date -ud "${lastYYYY}-02-29" > /dev/null 2>&1) \
  25. && (( $(date -ud "$1" +%s) < $(date -ud "${frstYYYY}-03-01" +%s) )) \
  26. && (( $(date -ud "$2" +%s) > $(date -ud "${lastYYYY}-02-28" +%s) )) \
  27. && (( ld++ ))
  28. set -e
  29. echo "$ld"
  30. }
  31. [[ $@ == *verbose* ]] && set -x
  32. module load ${module_list:?}
  33. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  34. mkdir -p ${run_dir}
  35. cd ${run_dir}
  36. run_start_epoch=$(date -u -d"${run_start_date}" +%s)
  37. run_start_date_cos="${run_start_date//-}00"
  38. run_start_date=$(date -uR -d "${run_start_date}")
  39. run_end_date="${run_start_date} + ${run_duration:?}"
  40. run_end_date=$(date -uR -d "${run_end_date}")
  41. run_end_epoch=$(date -u -d"${run_end_date}" +%s)
  42. run_end_date_cos="$(date -u -d"${run_end_date}" +%Y%m%d%H)"
  43. if [[ ${special_rs_nemo} == "true" && ${special_restart} == "true" ]]; then
  44. echo "ERROR: both special_rs_nemo and special_restart are true. only can be true (at most). exit".
  45. exit 2
  46. fi
  47. for (( ; run_num_legs>0 ; run_num_legs-- ))
  48. do
  49. # Check for restart information file and set the current leg start date
  50. # Ignore restart information file if force_run_from_scratch is true
  51. if ! [ -r ${info_file} ]
  52. then
  53. leg_is_restart=false
  54. leg_start_date=${run_start_date}
  55. leg_start_date_cos=${run_start_date_cos}
  56. leg_number=1
  57. echo "leg_is_restart=false"
  58. else
  59. leg_is_restart=true
  60. . ./${info_file}
  61. leg_start_date=${leg_end_date}
  62. leg_start_date_cos="$(date -u -d"${leg_start_date}" +%Y%m%d%H)"
  63. leg_number=$((leg_number+1))
  64. echo "leg_is_restart=true"
  65. fi
  66. # Compute the end date of the current leg
  67. if [ -n "${rst_freq}" ]
  68. then
  69. if (($(date -u -d "${leg_start_date:?} + ${rst_freq}" +%s) > $(date -u -d "${leg_start_date:?} + ${run_duration}" +%s) )); then
  70. rst_freq=$run_duration
  71. fi
  72. leg_end_date=$(date -uR -d "${leg_start_date} + ${rst_freq}")
  73. leg_end_date_cos="$(date -u -d"${leg_end_date}" +%Y%m%d%H)"
  74. else
  75. leg_end_date=${run_end_date}
  76. leg_end_date_cos=${run_end_date_cos}
  77. fi
  78. if [ $(date -u -d "${leg_end_date}" +%s) -gt $(date -u -d "${run_end_date}" +%s) ]
  79. then
  80. leg_end_date=${run_end_date}
  81. leg_end_date_cos=${run_end_date_cos}
  82. fi
  83. # Some time variables needed later
  84. leg_start_epoch=$(date -u -d "${leg_start_date}" +%s)
  85. leg_end_epoch=$(date -u -d "${leg_start_date:?} + ${rst_freq:=$run_duration}" +%s)
  86. leg_length_sec=$(( $(date -u -d "${leg_end_date}" +%s) - $(date -u -d "${leg_start_date}" +%s) ))
  87. leg_start_sec=$(( $(date -u -d "${leg_start_date}" +%s) - $(date -u -d "${run_start_date}" +%s) ))
  88. leg_end_sec=$(( $(date -u -d "${leg_end_date}" +%s) - $(date -u -d "${run_start_date}" +%s) ))
  89. leg_start_date_yyyymmdd=$(date -u -d "${leg_start_date}" +%Y%m%d)
  90. leg_start_date_yyyy=$(date -u -d "${leg_start_date}" +%Y)
  91. leg_end_date_yyyy=$(date -u -d "${leg_end_date}" +%Y)
  92. echo "leg_number: $leg_number"
  93. echo "leg_start_date: $leg_start_date"
  94. echo "leg_end_date: $leg_end_date"
  95. # Check whether there's actually time left to simulate - exit otherwise
  96. if [ ${leg_length_sec} -le 0 ]
  97. then
  98. info "Leg start date equal to or after end of simulation."
  99. info "Nothing left to do. Exiting."
  100. exit 0
  101. fi
  102. # -------------------------------------------------------------------------
  103. # *** Prepare the run directory for a run from scratch
  104. # -------------------------------------------------------------------------
  105. if ! $leg_is_restart
  106. then
  107. # ---------------------------------------------------------------------
  108. # *** Check if run dir is empty. If not, and if we are allowed to do so
  109. # by ${force_run_from_scratch}, remove everything
  110. # ---------------------------------------------------------------------
  111. echo "pwd: $PWD"
  112. if $(ls * >& /dev/null)
  113. then
  114. if ${force_run_from_scratch}
  115. then
  116. rm -fr ${run_dir}/*
  117. else
  118. echo "error: run directory not empty and \$force_run_from_scratch not set."
  119. exit 0
  120. fi
  121. fi
  122. # ---------------------------------------------------------------------
  123. # *** Creates some directories
  124. # ---------------------------------------------------------------------
  125. mkdir -p ${out_dir}
  126. # ---------------------------------------------------------------------
  127. # *** Copy executables and coupling-related files
  128. # ---------------------------------------------------------------------
  129. cd ${start_dir}
  130. cp -u xios_config/*xml "${run_dir}"
  131. cp -u namelists/* "${run_dir}"
  132. cd ${run_dir}
  133. cp -u "${nem_exe_file:?}" .
  134. cp -u "${xio_exe_file:?}" .
  135. # ---------------------------------------------------------------------
  136. # *** Link ic, forcing and shared files for NEMO
  137. # ---------------------------------------------------------------------
  138. [[ ! -f EMPave_old.dat ]] && echo " 0 0.0000000000000000E+00 0.0000000000000000E+00" > EMPave_old.dat
  139. for file in "${ic_files[@]}"; do
  140. [[ ! -e ${file#*> } ]] && ln -sf $(sed 's/ *=> */ /' <<< "${ini_data_dir}/${ic_subdir}/${nem_grid}/$file")
  141. done
  142. for file in "${forcing_files[@]}"; do
  143. [[ ! -e ${file#*> } || "$file" == \** ]] && ln -sf $(sed 's/ *=> */ /' <<< "${ini_data_dir}/${forcing_subdir}/${nem_forcing_set}/$file")
  144. done
  145. for file in "${shared_files[@]}"; do
  146. [[ ! -e ${file#*> } ]] && ln -sf $(sed 's/ *=> */ /' <<< "${shared_dir}/$file")
  147. done
  148. # ---------------------------------------------------------------------
  149. # *** Copy potential special restarts
  150. # ---------------------------------------------------------------------
  151. if $special_restart
  152. then
  153. ### Getting the right output folders
  154. leg_start_date_tmp=$leg_start_date
  155. leg_end_date_tmp=$leg_end_date
  156. leg_number_tmp=$leg_number
  157. rsync -avz ${run_dir}/../../${special_restart_from}/rundir/${info_file} ${run_dir}
  158. special_date=$(date -uR -d "${special_restart_date}")
  159. sed -i "/$special_date/q" ${run_dir}/${info_file}
  160. . ${run_dir}/${info_file}
  161. special_restart_leg=$(printf %03d $((leg_number)))
  162. echo "special_date: $special_date"
  163. echo "special_restart_leg: $special_restart_leg"
  164. echo "special_restart_from: $special_restart_from"
  165. ### NEMO
  166. indir="${run_dir}/../../${special_restart_from}/archive/restart/$(printf %03d $((special_restart_leg)))"
  167. cd ${indir}
  168. for f in *.nc; do
  169. nf=`echo "${f/$special_restart_from/$exp_name}"`
  170. echo "copy $f to ${run_dir}/$nf"
  171. cp $f ${run_dir}/$nf
  172. done
  173. cd -
  174. cd ${run_dir}
  175. echo "pwd: $PWD"
  176. for f in ${exp_name}_????????_restart_???_????.nc; do
  177. nf=`echo ${f: -19}`
  178. echo "linking $f to ${run_dir}/$nf"
  179. ln -s $f $nf
  180. done
  181. cd ${run_dir}
  182. rm -rf ${info_file}
  183. leg_start_date=$leg_start_date_tmp
  184. leg_end_date=$leg_end_date_tmp
  185. leg_number=$leg_number_tmp
  186. elif ${special_rs_nemo}
  187. then
  188. dir_rs=`dirname ${special_rs_nemo_stencil}`
  189. base_rs=`basename ${special_rs_nemo_stencil}`
  190. if ${special_rs_nemo_rebuilt}
  191. then
  192. if [[ -f ${special_rs_nemo_stencil}oce.nc && -f ${special_rs_nemo_stencil}ice.nc ]]
  193. then
  194. ln -fs ${special_rs_nemo_stencil}oce.nc restart_oce.nc
  195. ln -fs ${special_rs_nemo_stencil}ice.nc restart_ice.nc
  196. else
  197. echo "EXITING. ${special_rs_nemo_stencil}oce.nc or ${special_rs_nemo_stencil}ice.nc not found"
  198. exit 2
  199. fi
  200. else
  201. for (( n=0 ; n<nem_numproc ; n++ ))
  202. do
  203. np=$(printf %04d ${n})
  204. if [[ -f ${special_rs_nemo_stencil}oce_${np}.nc && -f ${special_rs_nemo_stencil}ice_${np}.nc ]]
  205. then
  206. ln -fs ${special_rs_nemo_stencil}oce_${np}.nc restart_oce_${np}.nc
  207. ln -fs ${special_rs_nemo_stencil}ice_${np}.nc restart_ice_${np}.nc
  208. else
  209. echo "EXITING. ${special_rs_nemo_stencil}oce_${np}.nc or ${special_rs_nemo_stencil}ice_${np}.nc not found"
  210. exit 2
  211. fi
  212. done
  213. fi
  214. fi # if $special_restart
  215. else # if ! $leg_is_restart
  216. # ---------------------------------------------------------------------
  217. # *** Creates some directories
  218. # ---------------------------------------------------------------------
  219. mkdir -p ${out_dir}
  220. # ---------------------------------------------------------------------
  221. # *** Copy restart files in run directory
  222. # ---------------------------------------------------------------------
  223. cd ${run_dir}
  224. ### NEMO
  225. outdir="${archive_dir}/restart/$(printf %03d $((leg_number-1)))"
  226. ns=$(printf %08d $(( leg_start_sec / nem_time_step_sec )))
  227. for f in oce ice
  228. do
  229. echo "${outdir}/${exp_name}_${ns}_restart_${f}_????.nc"
  230. ln -sf ${outdir}/${exp_name}_${ns}_restart_${f}_????.nc ${run_dir}
  231. done
  232. for (( n=0 ; n<nem_numproc ; n++ ))
  233. do
  234. np=$(printf %04d ${n})
  235. ln -fs ${exp_name}_${ns}_restart_oce_${np}.nc restart_oce_${np}.nc
  236. ln -fs ${exp_name}_${ns}_restart_ice_${np}.nc restart_ice_${np}.nc
  237. done
  238. fi # ! $leg_is_restart
  239. # -------------------------------------------------------------------------
  240. # *** Update ocean namelist
  241. # -------------------------------------------------------------------------
  242. source build_namelist_cfg.sh > namelist_cfg
  243. # -------------------------------------------------------------------------
  244. # *** Launch experiment
  245. # -------------------------------------------------------------------------
  246. echo "START of mpirun"
  247. time_begin=$(date +%s)
  248. mpirun -np "${xio_numproc:?}" "${xio_exe_file:?}" : -np "${nem_numproc:?}" "${nem_exe_file:?}"
  249. time_end=$(date +%s)
  250. echo "END of: mpirun"
  251. # -------------------------------------------------------------------------
  252. # *** Move output files to archive directory
  253. # -------------------------------------------------------------------------
  254. ### NEMO
  255. outdir="${archive_dir}/output/$(printf %03d $((leg_number)))"
  256. mkdir -p ${outdir}
  257. for v in grid_U grid_V grid_W grid_T icemod SBC scalar SBC_scalar diad_T \
  258. grid_T_2D grid_U_2D grid_V_2D grid_W_2D grid_T_3D grid_U_3D grid_V_3D grid_W_3D \
  259. grid_1point grid_T_3D_ncatice vert_sum \
  260. grid_ptr_W_3basin_3D grid_ptr_T_3basin_2D grid_ptr_T_2D \
  261. zoom_700_sum zoom_300_sum zoom_2000_sum
  262. do
  263. for f in ${exp_name}_*_????????_????????_*${v}*.nc
  264. do
  265. test -f $f && mv $f $outdir/
  266. done
  267. done
  268. # -------------------------------------------------------------------------
  269. # *** Move restart files to archive directory
  270. # -------------------------------------------------------------------------
  271. ### NEMO
  272. outdir="${archive_dir}/restart/$(printf %03d $((leg_number)))"
  273. mkdir -p ${outdir}
  274. ns=$(printf %08d $(( leg_end_sec / nem_time_step_sec )))
  275. for f in oce ice
  276. do
  277. mv ${exp_name}_${ns}_restart_${f}_????.nc ${outdir}
  278. done
  279. # -------------------------------------------------------------------------
  280. # *** Move log files to archive directory
  281. # -------------------------------------------------------------------------
  282. outdir="${archive_dir}/log/$(printf %03d $((leg_number)))"
  283. mkdir -p ${outdir}
  284. for f in \
  285. ocean.output time.step solver.stat
  286. do
  287. test -f ${f} && mv ${f} ${outdir}
  288. done
  289. # ---------------------------------------------------------------------
  290. # *** Remove all leftover output files from previous legs
  291. # ---------------------------------------------------------------------
  292. ### NEMO files
  293. rm -f ${exp_name}_??_????????_????????_{grid_U,grid_V,grid_W,grid_T,icemod,SBC,scalar,SBC_scalar}.nc
  294. rm -f ${exp_name}_*restart_*nc
  295. ns=$(printf %08d $(( leg_start_sec / nem_time_step_sec )))
  296. for (( n=0 ; n<nem_numproc ; n++ ))
  297. do
  298. np=$(printf %04d ${n})
  299. rm -f restart_oce_${np}.nc
  300. rm -f restart_ice_${np}.nc
  301. done
  302. # -------------------------------------------------------------------------
  303. # *** Write the restart control file
  304. # -------------------------------------------------------------------------
  305. shopt -u nullglob
  306. tr=$(date -d "0 -$time_begin sec + $time_end sec" +%T)
  307. current_date=$(date +'%F %T')
  308. {
  309. echo "#"
  310. echo "# Finished leg at ${current_date} after ${tr} (hh:mm:ss)"
  311. echo "leg_number=${leg_number}"
  312. echo "leg_start_date=\"${leg_start_date}\""
  313. echo "leg_end_date=\"${leg_end_date}\""
  314. } | tee -a "${info_file}"
  315. # Need to reset force_run_from_scratch in order to avoid destroying the next leg
  316. force_run_from_scratch=false
  317. special_restart=false
  318. done # Loop over legs
  319. cd ${start_dir}
  320. cd - >/dev/null
  321. [[ $@ == *noresubmit* ]] && exit 0
  322. if (( leg_end_epoch < run_end_epoch )) ; then
  323. echo "Leg end earlier than end of simulation."
  324. echo "Submitting another job."
  325. if [[ "$@" == *"local"* ]] ; then
  326. exec "$0" "$@"
  327. elif [ "qsub" == *sbatch* ] ; then
  328. sbatch $0 $@ | awk '{print $4}' >> ${run_dir}/.coral_jobs
  329. elif [[ $USER == "vsc"* ]]; then
  330. if [ -f ${PBS_O_WORKDIR-$PWD}/tmp/credits_file ]; then
  331. credits=`cat ${PBS_O_WORKDIR-$PWD}/tmp/credits_file`
  332. else
  333. credits_tmp=`grep "account string:" ${PBS_O_WORKDIR-$PWD}/*.o* -R | sed 's/^.*: //'`
  334. credits=`echo $credits_tmp | awk '{ print $1 }'`
  335. cd ${start_dir}
  336. mkdir -p tmp
  337. echo ${credits} > tmp/credits_file
  338. fi
  339. if [ -f ${PBS_O_WORKDIR-$PWD}/tmp/script_name ]; then
  340. script_name=`cat ${PBS_O_WORKDIR-$PWD}/tmp/script_name`
  341. else
  342. script_name="run.sh"
  343. fi
  344. ssh login2 "cd $start_dir; qsub -A $credits $script_name | tee -a coral_jobs"
  345. sleep 2
  346. jobid=`cat coral_jobs`
  347. rm -f coral_jobs
  348. jobid=${jobid%%.*}
  349. echo "${jobid}" >> "${run_dir}"/.coral_jobs ;
  350. else
  351. "qsub" -v PBS_OPTIONS="$@" "$0" | tee -a coral_jobs
  352. sleep 2
  353. jobid=`cat coral_jobs`
  354. rm -f coral_jobs
  355. jobid=${jobid%.*}
  356. echo "${jobid}" >> "${run_dir}"/.coral_jobs
  357. fi
  358. else
  359. if [[ $USER == "vsc"* ]]; then
  360. rm -rf ${start_dir}/tmp
  361. fi
  362. rm -rf ${run_dir}/../cclm_out_tmp
  363. echo "Nothing left to do. Exiting."
  364. cat << "EOF"
  365. _____ _ _ _ _ _ _ _
  366. / ____(_) | | | | (_) | | | | | |
  367. | (___ _ _ __ ___ _ _| | __ _| |_ _ ___ _ __ ___ ___ _ __ ___ _ __ | | ___| |_ ___ __| |
  368. \___ \| | '_ ` _ \| | | | |/ _` | __| |/ _ \| '_ \ / __/ _ \| '_ ` _ \| '_ \| |/ _ \ __/ _ \/ _` |
  369. ____) | | | | | | | |_| | | (_| | |_| | (_) | | | | | (_| (_) | | | | | | |_) | | __/ || __/ (_| |
  370. |_____/|_|_| |_| |_|\__,_|_|\__,_|\__|_|\___/|_| |_| \___\___/|_| |_| |_| .__/|_|\___|\__\___|\__,_|
  371. | |
  372. |_|
  373. EOF
  374. fi
  375. exit 0