skeleton.sh 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. function mlpy () {
  33. if [[ `hostname` == "ozone" ]]; then
  34. module purge
  35. module load Python
  36. PYTHONPATH="/elic/home/pelletie/.local/lib/python3.6/site-packages:${PYTHONPATH}"
  37. elif [[ ${USER} == "vsc"????? ]]; then
  38. module purge
  39. module load Python
  40. PYTHONPATH="/user/leuven/327/vsc32749/.local/bin:${PYTHONPATH}"
  41. else
  42. echo "Nothing detected ${USER}" > ${start_dir}/tmpout
  43. exit 0
  44. fi
  45. }
  46. [[ $@ == *verbose* ]] && set -x
  47. module purge
  48. module load ${module_list:?}
  49. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  50. #
  51. # Create run directory
  52. # Copy executables
  53. # Copy XIOS XML files into run directory
  54. # Copy namelist files into run directory
  55. #
  56. if [ ! -d ${run_dir:?} ]
  57. then
  58. mkdir -p ${run_dir}
  59. if $special_restart
  60. then
  61. rsync -av --delete ${run_dir}/../${special_restart_from}/ --exclude log --exclude output --exclude restart --exclude="${special_restart_from}_*" --exclude="ocean*" --exclude="restart_*" --exclude="debug.*" --exclude="output.*" ${run_dir}
  62. cp -f ${nem_exe_file} ${run_dir}
  63. cp -f ${xio_exe_file} ${run_dir}
  64. special_year=${special_restart_date:0:4}
  65. sed -i "/$special_year/q" ${run_dir}/${info_file}
  66. . ${run_dir}/${info_file}
  67. special_restart_leg=$(printf %03d $((leg_number+1)))
  68. # PUT HERE THE INSTRUCTIONS TO COPY THE restart files
  69. cd ${run_dir}/../../archive/${special_restart_from}/restart/${special_restart_leg}
  70. for f in *.nc; do
  71. nf=${exp_name}${f:4}
  72. cp $f ${run_dir}/$nf
  73. done
  74. cd -
  75. cd ${run_dir}
  76. for f in ${exp_name}_????????_restart_???_????.nc; do
  77. nf=${f:14}
  78. ln -s $f $nf
  79. done
  80. cd -
  81. fi
  82. fi
  83. cd ${start_dir}
  84. cp -u xios_config/*xml "${run_dir}"
  85. cp -u namelists/* "${run_dir}"
  86. cd "${run_dir}"
  87. # cp -u "${nem_exe_file:?}" .
  88. # cp -u "${xio_exe_file:?}" .
  89. # Write fake file for previous fresh water budget adjustment (nn_fwb==2 in namelist)
  90. [[ ! -f EMPave_old.dat ]] && echo " 0 0.0000000000000000E+00 0.0000000000000000E+00" > EMPave_old.dat
  91. #TODO: Enquiry about this
  92. #
  93. # Link data files into run directory
  94. # Attention: no space in filenames allowed!
  95. # (No protection against word splitting by design)
  96. #
  97. for file in "${ic_files[@]}"; do
  98. [[ ! -e ${file#*> } ]] && ln -sf $(sed 's/ *=> */ /' <<< "${ini_data_dir}/${ic_subdir}/${nem_grid}/$file")
  99. done
  100. for file in "${forcing_files[@]}"; do
  101. [[ ! -e ${file#*> } || "$file" == \** ]] && ln -sf $(sed 's/ *=> */ /' <<< "${ini_data_dir}/${forcing_subdir}/${nem_forcing_set}/$file")
  102. done
  103. for file in "${shared_files[@]}"; do
  104. [[ ! -e ${file#*> } ]] && ln -sf $(sed 's/ *=> */ /' <<< "${shared_dir}/$file")
  105. done
  106. if [ "${isfcpl}" == "1" ]; then
  107. length_cut_pref=$(( ${#exp_name} + 4 ))
  108. rebuild_nemo_exe=${nemo_src_dir}/TOOLS/REBUILD_NEMO/rebuild_nemo
  109. if [ -f ${start_dir}/tmp/isfcpl.track ]; then
  110. source ${start_dir}/tmp/isfcpl.track
  111. # printf "\n" >> isfcpl.track
  112. # rm -f isfcpl.track
  113. fi
  114. cp -f ${fetish_src_dir}/* ${run_dir}
  115. iscpl_nm=.true.
  116. else
  117. iscpl_nm=.false.
  118. fi
  119. #
  120. # Normalize date formats
  121. #
  122. run_start_date=$(date -uR -d "${run_start_date}")
  123. run_end_date="${run_start_date} + ${run_duration:?}"
  124. run_end_date=$(date -uR -d "${run_end_date}")
  125. run_start_epoch=$(date -u -d"${run_start_date}" +%s)
  126. run_end_epoch=$(date -u -d"${run_end_date}" +%s)
  127. for (( ; run_num_legs>0 ; run_num_legs-- ))
  128. do
  129. #
  130. # Initialize variables (using restart file if it exists)
  131. # Variables ending in '_date' are in RFC2822
  132. # Variables ending in '_epoch' are in nb of seconds since 1970-01-01
  133. #
  134. [[ -r "${info_file:?}" ]] && source "${info_file:?}"
  135. leg_start_date=${leg_end_date:-$run_start_date}
  136. leg_number=$((${leg_number:=0}+1))
  137. leg_start_epoch=$(date -u -d "${leg_start_date}" +%s)
  138. leg_end_epoch=$(date -u -d "${leg_start_date:?} + ${rst_freq:=$run_duration}" +%s)
  139. leg_end_date=$(date -uR -d@"${leg_end_epoch}")
  140. leg_length_sec=$(( leg_end_epoch - leg_start_epoch ))
  141. leg_start_sec=$(( leg_start_epoch - run_start_epoch ))
  142. leg_end_sec=$(( leg_end_epoch - run_start_epoch ))
  143. leg_beforestart_epoch=$(( ${leg_start_epoch} - 43200 ))
  144. leg_beforestart_date=$(date -uR -d@"${leg_beforestart_epoch}")
  145. leg_almostend_epoch=$(( ${leg_end_epoch} - 43200 ))
  146. leg_almostend_date=$(date -uR -d@"${leg_almostend_epoch}")
  147. run_start_date_yyyymmdd=$(date -u -d "${run_start_date}" +%Y%m%d) # FIXME appears unused
  148. run_end_date_yyyymmdd=$(date -u -d "${run_end_date}" +%Y%m%d) # FIXME appears unused
  149. leg_start_date_yyyymmdd=$(date -u -d "${leg_start_date}" +%Y%m%d) # FIXME appears unused
  150. leg_almostend_date_yyyymmdd=$(date -u -d "${leg_almostend_date}" +%Y%m%d) # FIXME appears unused
  151. leg_beforestart_date_yyyymmdd=$(date -u -d "${leg_beforestart_date}" +%Y%m%d) # FIXME appears unused
  152. # run_start_date_yyyymm="${run_start_date_yyyymmdd::-2}"
  153. # run_end_date_yyyymm="${run_end_date_yyyymmdd::-2}"
  154. # leg_start_date_yyyymm="${leg_start_date_yyyymmdd::-2}"
  155. # leg_almostend_date_yyyymm="${leg_almostend_date_yyyymmdd::-2}"
  156. # leg_beforestart_date_yyyymm="${leg_beforestart_date_yyyymmdd::-2}"
  157. if [ "${isfcpl}" == "1" ]; then
  158. # Check if a monthly "icesheet" output file has indeed been asked.
  159. tmp_check=$(grep -nri "file id=\"file.*\".*name_suffix=\"_icesheet\"" ${run_dir}/file_def.xml | wc -l)
  160. if (( tmp_check == 0 )); then
  161. echo "file_def.xml does not look like you have asked for an icesheet output file! Exiting."
  162. exit 2
  163. elif (( tmp_check > 1 )); then
  164. echo "file_def.xml contains at least 2 icesheet output files! This is ambiguous. Exiting."
  165. exit 2
  166. fi
  167. if (( leg_number == 1 )); then
  168. mkdir -p ${archive_dir}/isfcpl
  169. rm -f ${start_dir}/tmp/isfcpl.track
  170. # Counter for the number of restart since last file exchange with fETISh.
  171. cnt_res_isfcpl=0
  172. isfcpl_window_days=$(echo "(${n_res_perisfcpl} * ${leg_length_sec}) / 86400" | bc)
  173. if (( isfcpl_window_days >= 28 && isfcpl_window_days <= 31 )); then
  174. n_month_isfcpl=1
  175. elif (( isfcpl_window_days >= 336 && isfcpl_window_days <= 372 )); then
  176. n_month_isfcpl=12
  177. else
  178. echo "Can't figure out how many month long is the coupling window."
  179. exit 2
  180. fi
  181. n_month_full_run=$(( $(( 12 * $(( ${run_end_date_yyyymmdd::4} - ${run_start_date_yyyymmdd::4} )) )) + $(( ${run_end_date_yyyymmdd:4:2} - ${run_start_date_yyyymmdd:4:2} )) ))
  182. if (( $(( ${n_month_full_run} % ${n_month_isfcpl} )) != 0 )); then
  183. echo "n_month_full_run=${n_month_full_run} is not a multiple of n_month_isfcpl=${n_month_isfcpl}."
  184. exit 2
  185. else
  186. n_isfcpl_window=$(echo "${n_month_full_run} / ${n_month_isfcpl}" | bc)
  187. fi
  188. tmp=`readlink -f isf_draft_meter.nc`
  189. tmp2=`readlink -f bathy_meter.nc`
  190. if [ "${tmp}" != "${tmp2}" ]; then
  191. echo "Error. At initialization, isf_draft_meter.nc and bathy_meter.nc do not point to the same file."
  192. exit 1
  193. fi
  194. init_bathy_file=`basename ${tmp}`
  195. cp -f ${tmp} ${archive_dir}/isfcpl
  196. cp -f ${tmp} ${run_dir}/init_NEMO_bathy.nc
  197. printf "# Information for the ice sheet model\n" > ${start_dir}/tmp/isfcpl.track
  198. printf "run_name=${exp_name}\n" >> ${start_dir}/tmp/isfcpl.track
  199. printf "initial_date=${run_start_date_yyyymmdd}\n" >> ${start_dir}/tmp/isfcpl.track
  200. printf "n_month_isfcpl=${n_month_isfcpl}\n" >> ${start_dir}/tmp/isfcpl.track
  201. printf "n_isfcpl_window=${n_isfcpl_window}\n" >> ${start_dir}/tmp/isfcpl.track
  202. printf "date_beg_isfcpl=YYYYMMDD\n" >> ${start_dir}/tmp/isfcpl.track
  203. printf "date_end_isfcpl=YYYYMMDD\n\n" >> ${start_dir}/tmp/isfcpl.track
  204. printf "# Information for NEMO to keep up\n" >> ${start_dir}/tmp/isfcpl.track
  205. printf "cnt_res_isfcpl=0\n" >> ${start_dir}/tmp/isfcpl.track
  206. printf "init_geom=1\n" >> ${start_dir}/tmp/isfcpl.track
  207. printf "init_bathy=1\n" >> ${start_dir}/tmp/isfcpl.track
  208. printf "curr_bathy_file=${init_bathy_file}\n" >> ${start_dir}/tmp/isfcpl.track
  209. init_geom=1
  210. init_bathy=1
  211. curr_bathy_file=${init_bathy_file}
  212. ln -sf ${run_dir}/fETISh_8km_geometry_init.nc ${run_dir}/fETISh_8km_geometry_curr.nc
  213. fi
  214. ln -sf ${curr_bathy_file} ${run_dir}/bathy_meter.nc
  215. ln -sf ${curr_bathy_file} ${run_dir}/isf_draft_meter.nc
  216. if (( $(( ${leg_number} - 1 )) % ${n_res_perisfcpl} == 0 )); then
  217. date_beg_isfcpl=${leg_start_date_yyyymmdd}
  218. sed -i "s/.*date_beg_isfcpl=.*/date_beg_isfcpl=${date_beg_isfcpl}/" ${start_dir}/tmp/isfcpl.track
  219. if (( leg_number > 1 )); then
  220. # leg number at which NEMO is supposed to receive updated geometry from fETISh.
  221. # We need to pre-treat fETISh outputs in order to make them readable by NEMO.
  222. # new fETISh output should be ${run_dir}/fETISh_8km_geometry_curr.nc
  223. module purge
  224. mlpy
  225. module load CDO
  226. ${pp_fetish_call}
  227. # Copy (for initial) or move (non initial) previous bathymetry file to archive and link new one to what NEMO will read.
  228. new_bathy_file="fETISh_eORCA025-SO_bathymetry_${leg_beforestart_date_yyyymmdd}.nc"
  229. mv -f fETISh_new_8km_eORCA025-bathymetry_curr.nc ${new_bathy_file}
  230. old_bathy=`readlink -f ${run_dir}/bathy_meter.nc`
  231. if [ "${init_bathy}" == 0 ]; then
  232. mv -f ${old_bathy} ${archive_dir}/isfcpl
  233. else
  234. init_bathy=0
  235. sed -i "s/^init_bathy=.*$/init_bathy=0/g" ${start_dir}/tmp/isfcpl.track
  236. cp -f ${old_bathy} ${archive_dir}/isfcpl
  237. fi
  238. curr_bathy_file=${new_bathy_file}
  239. sed -i "s/.*curr_bathy_file=.*/curr_bathy_file=${curr_bathy_file}/" ${start_dir}/tmp/isfcpl.track
  240. ln -sf ${new_bathy_file} ${run_dir}/bathy_meter.nc
  241. ln -sf ${new_bathy_file} ${run_dir}/isf_draft_meter.nc
  242. module purge
  243. module load ${module_list:?}
  244. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  245. fi
  246. fi
  247. fi
  248. # Correct for leap days because NEMO standalone uses no-leap calendar
  249. if (( leap_years == 0 )); then
  250. leg_length_sec=$(( leg_length_sec - $(leap_days "${leg_start_date}" "${leg_end_date}")*24*3600 ))
  251. leg_start_sec=$(( leg_start_sec - $(leap_days "${run_start_date}" "${leg_start_date}")*24*3600 ))
  252. leg_end_sec=$(( leg_end_sec - $(leap_days "${run_start_date}" "${leg_end_date}")*24*3600 ))
  253. fi
  254. (( leg_number > 1 )) && leg_is_restart=true || leg_is_restart=false
  255. #
  256. # Compute leg end-date and trim if necessary
  257. #
  258. (( leg_end_epoch > run_end_epoch )) && leg_end_date=${run_end_epoch}
  259. #
  260. # Check whether there is some work left to do
  261. #
  262. if (( leg_start_epoch >= run_end_epoch ))
  263. then
  264. echo "Leg start date equal to or after end of simulation."
  265. echo "Nothing left to do. Cleaning and exiting."
  266. for (( n=0 ; n<nem_numproc ; n++ ))
  267. do
  268. np=$(printf %04d ${n})
  269. rm -f "${run_dir}/restart_oce_${np}.nc"
  270. rm -f "${run_dir}/restart_ice_${np}.nc"
  271. done
  272. exit 0
  273. fi
  274. #
  275. # Update namelist
  276. #
  277. source build_namelist_cfg.sh > namelist_cfg
  278. # Link the restart files
  279. ns=$(printf %08d $(( leg_start_sec / nem_time_step_sec - nem_restart_offset )))
  280. if ((leg_start_sec > 0 )); then
  281. for (( n=0 ; n<nem_numproc ; n++ ))
  282. do
  283. np=$(printf %04d ${n})
  284. [[ -f "${exp_name:?}_${ns}_restart_oce_${np}.nc" ]] || { echo "Error: restart file not found." ; exit 2 ; }
  285. ln -fs "${exp_name:?}_${ns}_restart_oce_${np}.nc" "restart_oce_${np}.nc"
  286. [[ -f "${exp_name:?}_${ns}_restart_ice_${np}.nc" ]] || { echo "Error: restart file not found." ; exit 2 ; }
  287. ln -fs "${exp_name:?}_${ns}_restart_ice_${np}.nc" "restart_ice_${np}.nc"
  288. done
  289. fi
  290. # Stop here is preponly was specified
  291. [[ $@ == *preponly* ]] && exit 0
  292. #
  293. # Run nemo
  294. #
  295. if [[ "$@" == *"dbg"* ]]; then
  296. echo $HOSTNAME
  297. sleep 10000000000000
  298. fi
  299. time_begin=$(date +%s)
  300. mpirun -np "${xio_numproc:?}" "${xio_exe_file:?}" : -np "${nem_numproc:?}" "${nem_exe_file:?}"
  301. time_end=$(date +%s)
  302. if (( isfcpl == 1 )); then
  303. module purge
  304. module load CDO
  305. fmt_res_isfcpl=$(printf "%02d" ${cnt_res_isfcpl})
  306. in_stencil="${exp_name}_1m_${leg_start_date_yyyymmdd}_${leg_almostend_date_yyyymmdd}_icesheet"
  307. out_stencil="${exp_name}_icesheet_res${fmt_res_isfcpl}"
  308. # Renaming.
  309. for((prc=0;prc<xio_numproc;prc++)); do
  310. fmt_xio=$(printf "%04d" ${prc})
  311. infile="${in_stencil}_${fmt_xio}.nc"
  312. # echo "mv -f ${infile} ./${out_stencil}_${fmt_xio}.nc"
  313. mv -f ${infile} ${out_stencil}_${fmt_xio}.nc
  314. done
  315. # Rebuilding it.
  316. module purge
  317. module load ${module_list:?}
  318. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  319. ${rebuild_nemo_exe} ${out_stencil} ${xio_numproc}
  320. rm -f ${out_stencil}_????.nc
  321. # Counter increase (NEMO restart index within ice sheet / ocean coupling window).
  322. cnt_res_isfcpl=$(( ${cnt_res_isfcpl} + 1 ))
  323. sed -i "s/.*cnt_res_isfcpl=.*/cnt_res_isfcpl=${cnt_res_isfcpl}/" ${start_dir}/tmp/isfcpl.track
  324. if (( ${leg_number} % ${n_res_perisfcpl} == 0 )); then
  325. # We have just ended one NEMO leg which closes an ice sheet / ocean coupling window.
  326. date_end_isfcpl=${leg_almostend_date_yyyymmdd}
  327. sed -i "s/.*date_end_isfcpl=.*/date_end_isfcpl=${date_end_isfcpl}/" ${start_dir}/tmp/isfcpl.track
  328. cp -f ${start_dir}/tmp/isfcpl.track ${start_dir}/tmp/isfcpl.old
  329. ${rebuild_nemo_exe} mesh_mask ${nem_numproc}
  330. mv -f mesh_mask.nc ${archive_dir}/isfcpl/mesh_mask_${date_beg_isfcpl}-${date_end_isfcpl}.nc
  331. module purge; module load CDO
  332. # Concatenate all icesheet files from all legs contained within this coupling window
  333. outfile="${exp_name}_ocean2ice_${date_beg_isfcpl}-${date_end_isfcpl}.nc"
  334. cdo -O copy "${out_stencil::-2}??.nc" ${outfile}
  335. # CALL FETISH
  336. module purge
  337. cp -f ${start_dir}/tmp/isfcpl.track ${run_dir}
  338. module load ${fetish_modules}
  339. ${fetish_call}
  340. mv -f ${exp_name}_fETISh*.mat ${archive_dir}/isfcpl
  341. module purge
  342. # mv ocean2ice file to arch dir
  343. mv -f ${outfile} ${archive_dir}/isfcpl
  344. new_geom_file="${exp_name}_ice2ocean_${date_end_isfcpl}.nc"
  345. # module load NCO
  346. # ncks -C -v B ${init_fetish_file} -A ${new_geom_file}
  347. # module purge
  348. old_geom=`readlink -f fETISh_8km_geometry_curr.nc`
  349. if [ "${init_geom}" == 0 ]; then
  350. mv -f ${old_geom} ${archive_dir}/isfcpl
  351. else
  352. init_geom=0
  353. sed -i "s/.*init_geom=.*/init_geom=0/" ${start_dir}/tmp/isfcpl.track
  354. cp -f ${old_geom} ${archive_dir}/isfcpl
  355. fi
  356. ln -sf ${new_geom_file} fETISh_8km_geometry_curr.nc
  357. cnt_res_isfcpl=0
  358. printf "# Information for the ice sheet model\n" > ${start_dir}/tmp/isfcpl.track
  359. printf "run_name=${exp_name}\n" >> ${start_dir}/tmp/isfcpl.track
  360. printf "initial_date=${run_start_date_yyyymmdd}\n" >> ${start_dir}/tmp/isfcpl.track
  361. printf "n_month_isfcpl=${n_month_isfcpl}\n" >> ${start_dir}/tmp/isfcpl.track
  362. printf "n_isfcpl_window=${n_isfcpl_window}\n" >> ${start_dir}/tmp/isfcpl.track
  363. printf "date_beg_isfcpl=YYYYMMDD\n" >> ${start_dir}/tmp/isfcpl.track
  364. printf "date_end_isfcpl=YYYYMMDD\n\n" >> ${start_dir}/tmp/isfcpl.track
  365. printf "# Information for NEMO to keep up\n" >> ${start_dir}/tmp/isfcpl.track
  366. printf "cnt_res_isfcpl=0\n" >> ${start_dir}/tmp/isfcpl.track
  367. printf "init_geom=${init_geom}\n" >> ${start_dir}/tmp/isfcpl.track
  368. printf "init_bathy=${init_bathy}\n" >> ${start_dir}/tmp/isfcpl.track
  369. printf "curr_bathy_file=${curr_bathy_file}\n" >> ${start_dir}/tmp/isfcpl.track
  370. module purge
  371. module load ${module_list:?}
  372. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  373. fi
  374. fi
  375. #############################
  376. formatted_leg_number=$(printf %03d $((leg_number)))
  377. #
  378. # Move NEMO output files to archive directory
  379. #
  380. outdir="${archive_dir:?}/output/${formatted_leg_number}"
  381. mkdir -p "${outdir}"
  382. shopt -s nullglob
  383. for v in grid_U grid_V grid_W grid_T icemod icemoa SBC SBC_scalar scalar fwfisf
  384. do
  385. for f in ${exp_name}_??_????????_????????_${v}_????.nc
  386. do
  387. mv "$f" "$outdir/"
  388. done
  389. for f in ${exp_name}_??_????????_????????_${v}.nc
  390. do
  391. mv "$f" "$outdir/"
  392. done
  393. for f in ${exp_name}_??_${v}.nc
  394. do
  395. mv "$f" "$outdir/"
  396. done
  397. done
  398. #
  399. # Move NEMO restart files from previous run to archive directory
  400. #
  401. outdir="$archive_dir/restart/${formatted_leg_number}"
  402. mkdir -p "${outdir}"
  403. for f in ${exp_name}_${ns}_restart_???_????.nc
  404. do
  405. [ -f "$f" ] && mv "$f" "${outdir}"
  406. done
  407. #
  408. # Move log files to archive directory
  409. #
  410. outdir="$archive_dir/log/${formatted_leg_number}"
  411. mkdir -p "${outdir}"
  412. for f in ocean.output time.step solver.stat ; do mv "${f}" "${outdir}"; done
  413. shopt -u nullglob
  414. #
  415. # Write checkpoint control file
  416. # TODO: enquiry why 0 -1 +2 rather than 2- 1
  417. #
  418. tr=$(date -d "0 -$time_begin sec + $time_end sec" +%T)
  419. current_date=$(date +'%F %T')
  420. {
  421. echo "#"
  422. echo "# Finished leg at ${current_date} after ${tr} (hh:mm:ss)"
  423. echo "leg_number=${leg_number}"
  424. echo "leg_start_date=\"${leg_start_date}\""
  425. echo "leg_end_date=\"${leg_end_date}\""
  426. } | tee -a "${info_file}"
  427. special_restart=false
  428. done
  429. #
  430. # Move back to submission directory
  431. #
  432. cd - >/dev/null
  433. #
  434. # Check whether there is some work left to do in a further job
  435. #
  436. [[ $@ == *noresubmit* ]] && exit 0
  437. if (( leg_end_epoch < run_end_epoch )) ; then
  438. echo "Leg end earlier than end of simulation."
  439. echo "Submitting another job."
  440. #[[ $@ == *local* ]] && exec $0 $@ || scontrol requeue $SLURM_JOB_ID #TODO: factorize this
  441. if [[ "$@" == *"local"* ]] ; then
  442. cd ${start_dir}
  443. sleep 2
  444. ./run.sh local && exit 0
  445. #exec "$0" "$@"
  446. elif [ "${submit_command}" == *sbatch* ] ; then
  447. sbatch $0 $@ | awk '{print $4}' >> ${run_dir}/.coral_jobs
  448. elif [[ $USER == "vsc"* ]]; then
  449. if [ -f ${PBS_O_WORKDIR-$PWD}/tmp/credits_file ]; then
  450. credits=`cat ${PBS_O_WORKDIR-$PWD}/tmp/credits_file`
  451. else
  452. #logfiletmp=`echo ${PBS_O_WORKDIR-$PWD} | rev | cut -d'/' -f1 | rev`
  453. #logfiletmp2=`echo $logfiletmp | cut -d'_' -f1`
  454. #logfile=`ls ${PBS_O_WORKDIR-$PWD}"/"$logfiletmp2*.o*`
  455. #credits=`grep "account string:" $logfile | sed 's/^.*: //'`
  456. credits_tmp=`grep "account string:" ${PBS_O_WORKDIR-$PWD}/*.o* -R | sed 's/^.*: //'`
  457. credits=`echo $credits_tmp | awk '{ print $1 }'`
  458. mkdir -p tmp
  459. echo ${credits} > tmp/credits_file
  460. fi
  461. script_name=`cat ${PBS_O_WORKDIR-$PWD}/tmp/script_name`
  462. run_path=`cat ${PBS_O_WORKDIR-$PWD}/tmp/address`
  463. ssh login2 "cd $run_path; qsub -A $credits $script_name | tee -a coral_jobs"
  464. sleep 2
  465. jobid=`cat coral_jobs`
  466. rm -f coral_jobs
  467. jobid=${jobid%%.*}
  468. echo "${jobid}" >> "${run_dir}"/.coral_jobs ;
  469. else
  470. "qsub" -v PBS_OPTIONS="$@" "$0" | tee -a coral_jobs
  471. sleep 2
  472. jobid=`cat coral_jobs`
  473. rm -f coral_jobs
  474. jobid=${jobid%.*}
  475. echo "${jobid}" >> "${run_dir}"/.coral_jobs
  476. fi
  477. else
  478. if [[ $USER == "vsc"* ]]; then
  479. run_path=`cat ${PBS_O_WORKDIR-$PWD}/tmp/address`
  480. rm -rf $run_path/tmp
  481. fi
  482. echo "Nothing left to do. Cleaning and exiting." # FIXME Factorize this (we have two exit points)
  483. for (( n=0 ; n<nem_numproc ; n++ ))
  484. do
  485. np=$(printf %04d ${n})
  486. rm -f "restart_oce_${np}.nc"
  487. rm -f "restart_ice_${np}.nc"
  488. done
  489. fi
  490. exit 0