skeleton.sh 25 KB

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