skeleton.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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/nemo/$(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. n_oce_rs=`find ${dir_rs} -name "${base_rs}oce_????.nc" | wc -l`
  191. if(( n_oce_rs != nem_numproc )); then
  192. echo "in special_rs_nemo, not the right amount of ocean restart files."
  193. echo "${n_oce_rs} instead of ${nem_numproc}. exiting."
  194. exit 2
  195. fi
  196. n_ice_rs=`find ${dir_rs} -name "${base_rs}ice_????.nc" | wc -l`
  197. if(( n_ice_rs != nem_numproc )); then
  198. echo "in special_rs_nemo, not the right amount of ice restart files."
  199. echo "${n_ice_rs} instead of ${nem_numproc}. exiting."
  200. exit 2
  201. fi
  202. for (( n=0 ; n<nem_numproc ; n++ ))
  203. do
  204. np=$(printf %04d ${n})
  205. ln -fs ${special_rs_nemo_stencil}oce_${np}.nc restart_oce_${np}.nc
  206. ln -fs ${special_rs_nemo_stencil}ice_${np}.nc restart_ice_${np}.nc
  207. done
  208. fi # if $special_restart
  209. else # if ! $leg_is_restart
  210. # ---------------------------------------------------------------------
  211. # *** Creates some directories
  212. # ---------------------------------------------------------------------
  213. mkdir -p ${out_dir}
  214. # ---------------------------------------------------------------------
  215. # *** Copy restart files in run directory
  216. # ---------------------------------------------------------------------
  217. cd ${run_dir}
  218. ### NEMO
  219. outdir="${archive_dir}/restart/nemo/$(printf %03d $((leg_number-1)))"
  220. ns=$(printf %08d $(( leg_start_sec / nem_time_step_sec )))
  221. for f in oce ice
  222. do
  223. echo "${outdir}/${exp_name}_${ns}_restart_${f}_????.nc"
  224. ln -sf ${outdir}/${exp_name}_${ns}_restart_${f}_????.nc ${run_dir}
  225. done
  226. for (( n=0 ; n<nem_numproc ; n++ ))
  227. do
  228. np=$(printf %04d ${n})
  229. ln -fs ${exp_name}_${ns}_restart_oce_${np}.nc restart_oce_${np}.nc
  230. ln -fs ${exp_name}_${ns}_restart_ice_${np}.nc restart_ice_${np}.nc
  231. done
  232. fi # ! $leg_is_restart
  233. # -------------------------------------------------------------------------
  234. # *** Update ocean namelist
  235. # -------------------------------------------------------------------------
  236. source build_namelist_cfg.sh > namelist_cfg
  237. # -------------------------------------------------------------------------
  238. # *** Launch experiment
  239. # -------------------------------------------------------------------------
  240. echo "START of mpirun"
  241. time_begin=$(date +%s)
  242. mpirun -np "${xio_numproc:?}" "${xio_exe_file:?}" : -np "${nem_numproc:?}" "${nem_exe_file:?}"
  243. time_end=$(date +%s)
  244. echo "END of: mpirun"
  245. # -------------------------------------------------------------------------
  246. # *** Move output files to archive directory
  247. # -------------------------------------------------------------------------
  248. ### NEMO
  249. outdir="${archive_dir}/output/nemo/$(printf %03d $((leg_number)))"
  250. mkdir -p ${outdir}
  251. for v in grid_U grid_V grid_W grid_T icemod SBC scalar SBC_scalar diad_T \
  252. grid_T_2D grid_U_2D grid_V_2D grid_W_2D grid_T_3D grid_U_3D grid_V_3D grid_W_3D \
  253. grid_1point grid_T_3D_ncatice vert_sum \
  254. grid_ptr_W_3basin_3D grid_ptr_T_3basin_2D grid_ptr_T_2D \
  255. zoom_700_sum zoom_300_sum zoom_2000_sum
  256. do
  257. for f in ${exp_name}_*_????????_????????_*${v}*.nc
  258. do
  259. test -f $f && mv $f $outdir/
  260. done
  261. done
  262. # -------------------------------------------------------------------------
  263. # *** Move restart files to archive directory
  264. # -------------------------------------------------------------------------
  265. ### NEMO
  266. outdir="${archive_dir}/restart/nemo/$(printf %03d $((leg_number)))"
  267. mkdir -p ${outdir}
  268. ns=$(printf %08d $(( leg_end_sec / nem_time_step_sec )))
  269. for f in oce ice
  270. do
  271. mv ${exp_name}_${ns}_restart_${f}_????.nc ${outdir}
  272. done
  273. # -------------------------------------------------------------------------
  274. # *** Move log files to archive directory
  275. # -------------------------------------------------------------------------
  276. outdir="${archive_dir}/log/$(printf %03d $((leg_number)))"
  277. mkdir -p ${outdir}
  278. for f in \
  279. ocean.output time.step solver.stat
  280. do
  281. test -f ${f} && mv ${f} ${outdir}
  282. done
  283. # ---------------------------------------------------------------------
  284. # *** Remove all leftover output files from previous legs
  285. # ---------------------------------------------------------------------
  286. ### NEMO files
  287. rm -f ${exp_name}_??_????????_????????_{grid_U,grid_V,grid_W,grid_T,icemod,SBC,scalar,SBC_scalar}.nc
  288. rm -f ${exp_name}_*restart_*nc
  289. ns=$(printf %08d $(( leg_start_sec / nem_time_step_sec )))
  290. for (( n=0 ; n<nem_numproc ; n++ ))
  291. do
  292. np=$(printf %04d ${n})
  293. rm -f restart_oce_${np}.nc
  294. rm -f restart_ice_${np}.nc
  295. done
  296. # -------------------------------------------------------------------------
  297. # *** Write the restart control file
  298. # -------------------------------------------------------------------------
  299. shopt -u nullglob
  300. tr=$(date -d "0 -$time_begin sec + $time_end sec" +%T)
  301. current_date=$(date +'%F %T')
  302. {
  303. echo "#"
  304. echo "# Finished leg at ${current_date} after ${tr} (hh:mm:ss)"
  305. echo "leg_number=${leg_number}"
  306. echo "leg_start_date=\"${leg_start_date}\""
  307. echo "leg_end_date=\"${leg_end_date}\""
  308. } | tee -a "${info_file}"
  309. # Need to reset force_run_from_scratch in order to avoid destroying the next leg
  310. force_run_from_scratch=false
  311. special_restart=false
  312. done # Loop over legs
  313. cd ${start_dir}
  314. cd - >/dev/null
  315. [[ $@ == *noresubmit* ]] && exit 0
  316. if (( leg_end_epoch < run_end_epoch )) ; then
  317. echo "Leg end earlier than end of simulation."
  318. echo "Submitting another job."
  319. if [[ "$@" == *"local"* ]] ; then
  320. exec "$0" "$@"
  321. elif [ "qsub" == *sbatch* ] ; then
  322. sbatch $0 $@ | awk '{print $4}' >> ${run_dir}/.coral_jobs
  323. elif [[ $USER == "vsc"* ]]; then
  324. if [ -f ${PBS_O_WORKDIR-$PWD}/tmp/credits_file ]; then
  325. credits=`cat ${PBS_O_WORKDIR-$PWD}/tmp/credits_file`
  326. else
  327. credits_tmp=`grep "account string:" ${PBS_O_WORKDIR-$PWD}/*.o* -R | sed 's/^.*: //'`
  328. credits=`echo $credits_tmp | awk '{ print $1 }'`
  329. cd ${start_dir}
  330. mkdir -p tmp
  331. echo ${credits} > tmp/credits_file
  332. fi
  333. if [ -f ${PBS_O_WORKDIR-$PWD}/tmp/script_name ]; then
  334. script_name=`cat ${PBS_O_WORKDIR-$PWD}/tmp/script_name`
  335. else
  336. script_name="run.sh"
  337. fi
  338. ssh login2 "cd $start_dir; qsub -A $credits $script_name | tee -a coral_jobs"
  339. sleep 2
  340. jobid=`cat coral_jobs`
  341. rm -f coral_jobs
  342. jobid=${jobid%%.*}
  343. echo "${jobid}" >> "${run_dir}"/.coral_jobs ;
  344. else
  345. "qsub" -v PBS_OPTIONS="$@" "$0" | tee -a coral_jobs
  346. sleep 2
  347. jobid=`cat coral_jobs`
  348. rm -f coral_jobs
  349. jobid=${jobid%.*}
  350. echo "${jobid}" >> "${run_dir}"/.coral_jobs
  351. fi
  352. else
  353. if [[ $USER == "vsc"* ]]; then
  354. rm -rf ${start_dir}/tmp
  355. fi
  356. rm -rf ${run_dir}/../cclm_out_tmp
  357. echo "Nothing left to do. Exiting."
  358. cat << "EOF"
  359. _____ _ _ _ _ _ _ _
  360. / ____(_) | | | | (_) | | | | | |
  361. | (___ _ _ __ ___ _ _| | __ _| |_ _ ___ _ __ ___ ___ _ __ ___ _ __ | | ___| |_ ___ __| |
  362. \___ \| | '_ ` _ \| | | | |/ _` | __| |/ _ \| '_ \ / __/ _ \| '_ ` _ \| '_ \| |/ _ \ __/ _ \/ _` |
  363. ____) | | | | | | | |_| | | (_| | |_| | (_) | | | | | (_| (_) | | | | | | |_) | | __/ || __/ (_| |
  364. |_____/|_|_| |_| |_|\__,_|_|\__,_|\__|_|\___/|_| |_| \___\___/|_| |_| |_| .__/|_|\___|\__\___|\__,_|
  365. | |
  366. |_|
  367. EOF
  368. fi
  369. exit 0