skeleton.sh 22 KB

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