skeleton_isfcpl.sh 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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. # Function for loading modules
  32. function load_modules_isfcpl () {
  33. if [[ ${USER} == "vsc"????? ]]; then
  34. # quick and dirty patch - only BRENIAC will work while waiting for something to distinguish between both machines (include on computing nodes)
  35. # if [[ ${HOSTNAME} == "tier2"* ]]; then
  36. # if [[ "${name_icesheet_model}" == "fETISh" ]]; then
  37. # module purge
  38. # module load Python
  39. # PYTHONPATH="${HOME}/.local/bin:${HOME}/.local/lib/python3.7/site-packages:\${PYTHONPATH}"
  40. # module load CDO
  41. # echo ${PYTHONPATH} > ${start_dir}/ppath
  42. # echo "genius and fetish detected" > ${start_dir}/tmpout
  43. # else
  44. # echo "On Genius, ice sheet models other than fETISh not implemented yet" > ${start_dir}/tmpout
  45. # exit 2
  46. # fi
  47. # elif [[ ${HOSTNAME} == "login"? ]]; then
  48. if [[ "${name_icesheet_model}" == "fETISh" ]]; then
  49. module purge
  50. module load Python/3.7.2-intel-2018a CDO/1.9.5-intel-2018a
  51. [ ! -d "${VSC_HOME}/.local/lib/python3.7/site-packages/netCDF4" ] && pip install --user netCDF4
  52. [ ! -d "${VSC_HOME}/.local/lib/python3.7/site-packages/numpy" ] && pip install --target=${VSC_HOME}/.local/lib/python3.7/site-packages numpy
  53. [ ! -d "${VSC_HOME}/.local/lib/python3.7/site-packages/gsw" ] && pip install --user gsw
  54. PYTHONPATH="${HOME}/.local/bin:${HOME}/.local/lib/python3.7/site-packages:\${PYTHONPATH}"
  55. echo ${PYTHONPATH} > ${start_dir}/ppath
  56. echo "breniac and fetish detected" > ${start_dir}/tmpout
  57. else
  58. echo "On Breniac, ice sheet models other than fETISh not implemented yet" > ${start_dir}/tmpout
  59. exit 2
  60. fi
  61. # else
  62. # echo "You seem to be on a VSC machine that is neither Genius nor BRENIAC. This has not been implemented yet. Exiting"
  63. # exit 2
  64. # fi
  65. elif [[ ${HOSTNAME} == "lm3"* ]]; then
  66. if [[ "${name_icesheet_model}" == "fETISh" ]]; then
  67. echo "lemaitre3 and fetish detected" > ${start_dir}/tmpout
  68. module purge
  69. module load releases/2018a CDO/1.8.2-intel-2018a Python/3.6.4-intel-2018a
  70. else
  71. echo "On lemaitre3, ice sheet models other than fETISh not implemented yet" > ${start_dir}/tmpout
  72. exit 2
  73. fi
  74. else
  75. echo "hostname undetected. not implemented yet." > ${start_dir}/tmpout
  76. exit 2
  77. fi
  78. }
  79. [[ $@ == *verbose* ]] && set -x
  80. module load ${module_list:?}
  81. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  82. mkdir -p ${run_dir}
  83. cd ${run_dir}
  84. # Prepare iscpl
  85. if [ "${isfcpl}" == "1" ]; then
  86. if [[ ${USER} == "vsc"????? ]]; then
  87. if [[ ${VSC_INSTITUTE_CLUSTER} == "breniac" ]]; then
  88. echo "BRENIAC and iscpl detected. OK."
  89. else
  90. echo "VSC clusters other than BRENIAC not supported for isfcpl."
  91. exit 2
  92. fi
  93. elif [[ ${HOSTNAME} == "lm3"* ]]; then
  94. echo "Lemaitre3 and iscpl detected. OK."
  95. else
  96. echo "You have asked for iscpl, but I have not recognized genius nor lemaitre3. Exiting."
  97. exit 2
  98. fi
  99. if [ "${name_icesheet_model}" != "fETISh" ]; then
  100. echo "only \"fETISh\" is known as an ice sheet model. exiting."
  101. exit 2
  102. fi
  103. length_cut_pref=$(( ${#exp_name} + 4 ))
  104. rebuild_nemo_exe=${nemo_src_dir}/TOOLS/REBUILD_NEMO/rebuild_nemo
  105. if [ -f ${start_dir}/isfcpl.track ]; then
  106. source ${start_dir}/isfcpl.track
  107. fi
  108. iscpl_nm=.true.
  109. if [[ ${xio_exe_file} == *"xios-2"* ]]; then
  110. is_xios2=1
  111. else
  112. is_xios2=0
  113. fi
  114. else
  115. iscpl_nm=.false.
  116. fi
  117. run_start_epoch=$(date -u -d"${run_start_date}" +%s)
  118. run_start_date_cos="${run_start_date//-}00"
  119. run_start_date=$(date -uR -d "${run_start_date}")
  120. run_end_date="${run_start_date} + ${run_duration:?}"
  121. run_end_date=$(date -uR -d "${run_end_date}")
  122. run_end_epoch=$(date -u -d"${run_end_date}" +%s)
  123. run_end_date_cos="$(date -u -d"${run_end_date}" +%Y%m%d%H)"
  124. if [[ ${special_rs_nemo} == "true" && ${special_restart} == "true" ]]; then
  125. echo "ERROR: both special_rs_nemo and special_restart are true. only can be true (at most). exit".
  126. exit 2
  127. fi
  128. for (( ; run_num_legs>0 ; run_num_legs-- ))
  129. do
  130. # Check for restart information file and set the current leg start date
  131. # Ignore restart information file if force_run_from_scratch is true
  132. if ! [ -r ${info_file} ]
  133. then
  134. leg_is_restart=false
  135. leg_start_date=${run_start_date}
  136. leg_start_date_cos=${run_start_date_cos}
  137. leg_number=1
  138. echo "leg_is_restart=false"
  139. else
  140. leg_is_restart=true
  141. . ./${info_file}
  142. leg_start_date=${leg_end_date}
  143. leg_start_date_cos="$(date -u -d"${leg_start_date}" +%Y%m%d%H)"
  144. leg_number=$((leg_number+1))
  145. echo "leg_is_restart=true"
  146. fi
  147. # Compute the end date of the current leg
  148. if [ -n "${rst_freq}" ]
  149. then
  150. if (($(date -u -d "${leg_start_date:?} + ${rst_freq}" +%s) > $(date -u -d "${leg_start_date:?} + ${run_duration}" +%s) )); then
  151. rst_freq=$run_duration
  152. fi
  153. leg_end_date=$(date -uR -d "${leg_start_date} + ${rst_freq}")
  154. leg_end_date_cos="$(date -u -d"${leg_end_date}" +%Y%m%d%H)"
  155. else
  156. leg_end_date=${run_end_date}
  157. leg_end_date_cos=${run_end_date_cos}
  158. fi
  159. if [ $(date -u -d "${leg_end_date}" +%s) -gt $(date -u -d "${run_end_date}" +%s) ]
  160. then
  161. leg_end_date=${run_end_date}
  162. leg_end_date_cos=${run_end_date_cos}
  163. fi
  164. # Some time variables needed later
  165. leg_start_epoch=$(date -u -d "${leg_start_date}" +%s)
  166. leg_end_epoch=$(date -u -d "${leg_start_date:?} + ${rst_freq:=$run_duration}" +%s)
  167. leg_length_sec=$(( $(date -u -d "${leg_end_date}" +%s) - $(date -u -d "${leg_start_date}" +%s) ))
  168. leg_start_sec=$(( $(date -u -d "${leg_start_date}" +%s) - $(date -u -d "${run_start_date}" +%s) ))
  169. leg_end_sec=$(( $(date -u -d "${leg_end_date}" +%s) - $(date -u -d "${run_start_date}" +%s) ))
  170. leg_start_date_yyyymmdd=$(date -u -d "${leg_start_date}" +%Y%m%d)
  171. leg_start_date_yyyy=$(date -u -d "${leg_start_date}" +%Y)
  172. leg_end_date_yyyy=$(date -u -d "${leg_end_date}" +%Y)
  173. echo "leg_number: $leg_number"
  174. echo "leg_start_date: $leg_start_date"
  175. echo "leg_end_date: $leg_end_date"
  176. # Additional time variables for iscpl
  177. if (( isfcpl == 1 )); then
  178. leg_beforestart_epoch=$(( ${leg_start_epoch} - 43200 ))
  179. leg_beforestart_date=$(date -uR -d@"${leg_beforestart_epoch}")
  180. leg_almostend_epoch=$(( ${leg_end_epoch} - 43200 ))
  181. leg_almostend_date=$(date -uR -d@"${leg_almostend_epoch}")
  182. nextleg_start_epoch=$(( ${leg_end_epoch} + 43200 ))
  183. nextleg_start_date=$(date -uR -d@"${nextleg_start_epoch}")
  184. run_start_date_yyyymmdd=$(date -u -d "${run_start_date}" +%Y%m%d) # FIXME appears unused
  185. run_end_date_yyyymmdd=$(date -u -d "${run_end_date}" +%Y%m%d) # FIXME appears unused
  186. leg_start_date_yyyymmdd=$(date -u -d "${leg_start_date}" +%Y%m%d) # FIXME appears unused
  187. leg_almostend_date_yyyymmdd=$(date -u -d "${leg_almostend_date}" +%Y%m%d) # FIXME appears unused
  188. nextleg_start_date_yyyymmdd=$(date -u -d "${nextleg_start_date}" +%Y%m%d) # FIXME appears unused
  189. leg_beforestart_date_yyyymmdd=$(date -u -d "${leg_beforestart_date}" +%Y%m%d) # FIXME appears unused
  190. fi
  191. # Check whether there's actually time left to simulate - exit otherwise
  192. if [ ${leg_length_sec} -le 0 ]
  193. then
  194. info "Leg start date equal to or after end of simulation."
  195. info "Nothing left to do. Exiting."
  196. exit 0
  197. fi
  198. # -------------------------------------------------------------------------
  199. # *** Prepare the run directory for a run from scratch
  200. # -------------------------------------------------------------------------
  201. if ! $leg_is_restart
  202. then
  203. # ---------------------------------------------------------------------
  204. # *** Check if run dir is empty. If not, and if we are allowed to do so
  205. # by ${force_run_from_scratch}, remove everything
  206. # ---------------------------------------------------------------------
  207. echo "pwd: $PWD"
  208. if $(ls * >& /dev/null)
  209. then
  210. if ${force_run_from_scratch}
  211. then
  212. rm -fr ${run_dir}/*
  213. else
  214. echo "error: run directory not empty and \$force_run_from_scratch not set."
  215. exit 0
  216. fi
  217. fi
  218. # ---------------------------------------------------------------------
  219. # *** Creates some directories
  220. # ---------------------------------------------------------------------
  221. mkdir -p ${out_dir}
  222. # ---------------------------------------------------------------------
  223. # *** Copy executables and coupling-related files
  224. # ---------------------------------------------------------------------
  225. cd ${start_dir}
  226. cp -u xios_config/*xml "${run_dir}"
  227. cp -u namelists/* "${run_dir}"
  228. cd ${run_dir}
  229. cp -u "${nem_exe_file:?}" .
  230. cp -u "${xio_exe_file:?}" .
  231. if (( isfcpl == 1 )) ; then
  232. cp -u ${icesheet_exe_file} .
  233. cp -u ${isfcpl_dir}/* .
  234. ln -sf ${start_dir}/isfcpl.track ${run_dir}
  235. mkdir -p ${out_dir}/isfcpl_tmp
  236. fi
  237. # ---------------------------------------------------------------------
  238. # *** Link ic, forcing and shared files for NEMO
  239. # ---------------------------------------------------------------------
  240. [[ ! -f EMPave_old.dat ]] && echo " 0 0.0000000000000000E+00 0.0000000000000000E+00" > EMPave_old.dat
  241. for file in "${ic_files[@]}"; do
  242. [[ ! -e ${file#*> } ]] && ln -sf $(sed 's/ *=> */ /' <<< "${ini_data_dir}/${ic_subdir}/${nem_grid}/$file")
  243. done
  244. for file in "${forcing_files[@]}"; do
  245. [[ ! -e ${file#*> } || "$file" == \** ]] && ln -sf $(sed 's/ *=> */ /' <<< "${ini_data_dir}/${forcing_subdir}/${nem_forcing_set}/$file")
  246. done
  247. for file in "${shared_files[@]}"; do
  248. [[ ! -e ${file#*> } ]] && ln -sf $(sed 's/ *=> */ /' <<< "${shared_dir}/$file")
  249. done
  250. # ---------------------------------------------------------------------
  251. # *** Prepare NEMO - ice shelf coupling
  252. # ---------------------------------------------------------------------
  253. if [ "${isfcpl}" == "1" ]; then
  254. if (( leg_number == 1 )); then
  255. mkdir -p ${archive_dir}/isfcpl
  256. mkdir -p ${archive_dir}/${name_icesheet_model}
  257. rm -f ${start_dir}/isfcpl.track
  258. isfcpl_window_days=$(echo "(${n_res_perisfcpl} * ${leg_length_sec}) / 86400" | bc)
  259. isfcpl_d30=$(echo "${isfcpl_window_days} / 30" | bc -l)
  260. n_month_isfcpl=$(echo ${isfcpl_d30} | awk '{print int($1+0.5)}')
  261. 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} )) ))
  262. if (( n_month_isfcpl == 0 )); then
  263. echo "Error. With isfcpl == 1 (ice-shelf coupling activated), the restart length must be an integer number of months. Here rst_freq=\"${rst_freq}\" "
  264. exit 2
  265. fi
  266. if (( $(( ${n_month_full_run} % ${n_month_isfcpl} )) != 0 )); then
  267. echo "n_month_full_run=${n_month_full_run} is not a multiple of n_month_isfcpl=${n_month_isfcpl}."
  268. exit 2
  269. else
  270. n_isfcpl_window=$(echo "${n_month_full_run} / ${n_month_isfcpl}" | bc)
  271. fi
  272. tmp=`readlink -f isf_draft_meter.nc`
  273. tmp2=`readlink -f bathy_meter.nc`
  274. if [ "${tmp}" != "${tmp2}" ]; then
  275. echo "Error. At initialization, isf_draft_meter.nc and bathy_meter.nc do not point to the same file."
  276. exit 1
  277. fi
  278. init_bathy_file=`basename ${tmp}`
  279. cp -f ${tmp} ${archive_dir}/isfcpl
  280. cp -f ${tmp} ${run_dir}/init_NEMO_bathy.nc
  281. ln -sf ${icesheet_files_dir}/* ./
  282. ln -sf ${isfcpl_files_dir}/* ./
  283. date_beg_isfcpl="${run_start_date_yyyymmdd}"
  284. printf "# Information for the ice sheet model\n" > ${start_dir}/isfcpl.track
  285. printf "run_name=${exp_name}\n" >> ${start_dir}/isfcpl.track
  286. printf "initial_date=${run_start_date_yyyymmdd}\n" >> ${start_dir}/isfcpl.track
  287. printf "n_month_isfcpl=${n_month_isfcpl}\n" >> ${start_dir}/isfcpl.track
  288. printf "n_isfcpl_window=${n_isfcpl_window}\n" >> ${start_dir}/isfcpl.track
  289. printf "n_done_isfcpl_window_by_nemo=0\n" >> ${start_dir}/isfcpl.track
  290. printf "date_beg_isfcpl=${date_beg_isfcpl}\n" >> ${start_dir}/isfcpl.track
  291. printf "date_end_isfcpl=YYYYMMDD\n" >> ${start_dir}/isfcpl.track
  292. printf "ice_atm_cpl=${ice_atm_cpl}\n\n" > ${start_dir}/isfcpl.track
  293. printf "# Information for NEMO to keep up\n" >> ${start_dir}/isfcpl.track
  294. printf "init_bathy=1\n" >> ${start_dir}/isfcpl.track
  295. printf "curr_bathy_file=${init_bathy_file}\n" >> ${start_dir}/isfcpl.track
  296. init_bathy=1
  297. curr_bathy_file=${init_bathy_file}
  298. fi
  299. ln -sf ${curr_bathy_file} ${run_dir}/bathy_meter.nc
  300. ln -sf ${curr_bathy_file} ${run_dir}/isf_draft_meter.nc
  301. fi # if [ "${iscpl}" == 1 ]
  302. # ---------------------------------------------------------------------
  303. # *** Copy potential special restarts
  304. # ---------------------------------------------------------------------
  305. if $special_restart
  306. then
  307. ### Getting the right output folders
  308. leg_start_date_tmp=$leg_start_date
  309. leg_end_date_tmp=$leg_end_date
  310. leg_number_tmp=$leg_number
  311. rsync -avz ${run_dir}/../../${special_restart_from}/rundir/${info_file} ${run_dir}
  312. special_date=$(date -uR -d "${special_restart_date}")
  313. sed -i "/$special_date/q" ${run_dir}/${info_file}
  314. . ${run_dir}/${info_file}
  315. special_restart_leg=$(printf %03d $((leg_number)))
  316. echo "special_date: $special_date"
  317. echo "special_restart_leg: $special_restart_leg"
  318. echo "special_restart_from: $special_restart_from"
  319. ### NEMO
  320. indir="${run_dir}/../../${special_restart_from}/archive/restart/nemo/$(printf %03d $((special_restart_leg)))"
  321. cd ${indir}
  322. for f in *.nc; do
  323. nf=`echo "${f/$special_restart_from/$exp_name}"`
  324. echo "copy $f to ${run_dir}/$nf"
  325. cp $f ${run_dir}/$nf
  326. done
  327. cd -
  328. cd ${run_dir}
  329. echo "pwd: $PWD"
  330. for f in ${exp_name}_????????_restart_???_????.nc; do
  331. nf=`echo ${f: -19}`
  332. echo "linking $f to ${run_dir}/$nf"
  333. ln -s $f $nf
  334. done
  335. cd ${run_dir}
  336. rm -rf ${info_file}
  337. leg_start_date=$leg_start_date_tmp
  338. leg_end_date=$leg_end_date_tmp
  339. leg_number=$leg_number_tmp
  340. elif ${special_rs_nemo}
  341. then
  342. dir_rs=`dirname ${special_rs_nemo_stencil}`
  343. base_rs=`basename ${special_rs_nemo_stencil}`
  344. n_oce_rs=`find ${dir_rs} -name "${base_rs}oce_????.nc" | wc -l`
  345. if(( n_oce_rs != nem_numproc )); then
  346. echo "in special_rs_nemo, not the right amount of ocean restart files."
  347. echo "${n_oce_rs} instead of ${nem_numproc}. exiting."
  348. exit 2
  349. fi
  350. n_ice_rs=`find ${dir_rs} -name "${base_rs}ice_????.nc" | wc -l`
  351. if(( n_ice_rs != nem_numproc )); then
  352. echo "in special_rs_nemo, not the right amount of ice restart files."
  353. echo "${n_ice_rs} instead of ${nem_numproc}. exiting."
  354. exit 2
  355. fi
  356. for (( n=0 ; n<nem_numproc ; n++ ))
  357. do
  358. np=$(printf %04d ${n})
  359. ln -fs ${special_rs_nemo_stencil}oce_${np}.nc restart_oce_${np}.nc
  360. ln -fs ${special_rs_nemo_stencil}ice_${np}.nc restart_ice_${np}.nc
  361. done
  362. fi # if $special_restart
  363. else # if ! $leg_is_restart
  364. # ---------------------------------------------------------------------
  365. # *** Creates some directories
  366. # ---------------------------------------------------------------------
  367. mkdir -p ${out_dir}
  368. if [ "${isfcpl}" == "1" ]; then
  369. ln -sf ${curr_bathy_file} ${run_dir}/bathy_meter.nc
  370. ln -sf ${curr_bathy_file} ${run_dir}/isf_draft_meter.nc
  371. fi
  372. # ---------------------------------------------------------------------
  373. # *** Copy restart files in run directory
  374. # ---------------------------------------------------------------------
  375. cd ${run_dir}
  376. ### NEMO
  377. outdir="${archive_dir}/restart/nemo/$(printf %03d $((leg_number-1)))"
  378. ns=$(printf %08d $(( leg_start_sec / nem_time_step_sec )))
  379. for f in oce ice
  380. do
  381. echo "${outdir}/${exp_name}_${ns}_restart_${f}_????.nc"
  382. ln -sf ${outdir}/${exp_name}_${ns}_restart_${f}_????.nc ${run_dir}
  383. done
  384. for (( n=0 ; n<nem_numproc ; n++ ))
  385. do
  386. np=$(printf %04d ${n})
  387. ln -fs ${exp_name}_${ns}_restart_oce_${np}.nc restart_oce_${np}.nc
  388. ln -fs ${exp_name}_${ns}_restart_ice_${np}.nc restart_ice_${np}.nc
  389. done
  390. fi # ! $leg_is_restart
  391. # -------------------------------------------------------------------------
  392. # *** Update ocean namelist
  393. # -------------------------------------------------------------------------
  394. source build_namelist_cfg.sh > namelist_cfg
  395. # -------------------------------------------------------------------------
  396. # *** Build ice sheet launch
  397. # -------------------------------------------------------------------------
  398. if (( isfcpl == 1)); then
  399. source ${icesheet_launch_template} > launch_icesheet.sh
  400. chmod +x launch_icesheet.sh
  401. fi
  402. # -------------------------------------------------------------------------
  403. # *** Launch experiment
  404. # -------------------------------------------------------------------------
  405. echo "START of mpirun"
  406. time_begin=$(date +%s)
  407. mpirun -np "${xio_numproc:?}" "${xio_exe_file:?}" : -np "${nem_numproc:?}" "${nem_exe_file:?}"
  408. time_end=$(date +%s)
  409. echo "END of: mpirun"
  410. # -------------------------------------------------------------------------
  411. # *** Ice sheet coupling
  412. # -------------------------------------------------------------------------
  413. if (( isfcpl == 1 )); then
  414. cnt_res_isfcpl=$(( $(( ${leg_number} - 1 )) % ${n_res_perisfcpl} ))
  415. fmt_res_isfcpl=$(printf "%02d" ${cnt_res_isfcpl})
  416. in_fwfisf_stencil="${exp_name}_${frequency_fwfisf}_${leg_start_date_yyyymmdd}_${leg_almostend_date_yyyymmdd}_${suffix_fwfisf}"
  417. out_fwfisf_stencil="${exp_name}_fwfisf_res"
  418. out_fwfisf_curr="${out_fwfisf_stencil}${fmt_res_isfcpl}"
  419. if (( is_xios2 == 0 )); then
  420. module purge
  421. module load ${module_list:?}
  422. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  423. /bin/bash ${rebuild_nemo_exe} ${in_fwfisf_stencil} ${xio_numproc}
  424. module purge
  425. load_modules_isfcpl
  426. cdo selname,${name_var_fwfisf} ${in_fwfisf_stencil}.nc tmp_fwfisf.nc
  427. cdo monmean tmp_fwfisf.nc ${out_fwfisf_curr}.nc
  428. rm -f ${in_fwfisf_stencil}.nc tmp_fwfisf.nc
  429. else
  430. cdo selname,${name_var_fwfisf} ${in_fwfisf_stencil}.nc tmp_fwfisf.nc
  431. cdo monmean tmp_fwfisf.nc ${out_fwfisf_curr}.nc
  432. rm -f tmp_fwfisf.nc
  433. fi
  434. mv -f ${out_fwfisf_curr}.nc ${out_dir}/isfcpl_tmp/
  435. if (( ${leg_number} % ${n_res_perisfcpl} == 0 )); then
  436. date_end_isfcpl=${leg_almostend_date_yyyymmdd}
  437. sed -i "s/.*date_end_isfcpl=.*/date_end_isfcpl=${date_end_isfcpl}/" ${start_dir}/isfcpl.track
  438. module purge
  439. module load ${module_list:?}
  440. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  441. /bin/bash ${rebuild_nemo_exe} mesh_mask ${nem_numproc}
  442. module purge
  443. load_modules_isfcpl
  444. infile_ocean2ice="${exp_name}_ocean2ice_raw_${date_beg_isfcpl}-${date_end_isfcpl}.nc"
  445. outfile_ocean2ice="${exp_name}_ocean2ice_${date_beg_isfcpl}-${date_end_isfcpl}.nc"
  446. fwfisf_records=""
  447. for((rs2=0; rs2<n_res_perisfcpl; rs2++)); do
  448. fmt_rs2=$(printf "%02d" ${rs2})
  449. curr_file="${out_dir}/isfcpl_tmp/${out_fwfisf_stencil}${fmt_rs2}.nc"
  450. if [ -f ${curr_file} ] ; then
  451. fwfisf_records="${fwfisf_records} ${curr_file}"
  452. else
  453. echo "${curr_file} is not in ${out_dir}/isfcpl_tmp. It is needed for NEMO - ice sheet coupling. Exiting"
  454. exit 2
  455. fi
  456. done
  457. cdo -O copy ${fwfisf_records} ${infile_ocean2ice}
  458. rm -f ${fwfisf_records}
  459. source build_pp_nemo_to_icesheet.sh > ./pp_nemo_to_${name_icesheet_model}.py
  460. python pp_nemo_to_${name_icesheet_model}.py
  461. mv -f ${infile_ocean2ice} ${archive_dir}/isfcpl/
  462. mv -f mesh_mask.nc ${archive_dir}/isfcpl/${exp_name}_mesh_mask_${date_beg_isfcpl}-${date_end_isfcpl}.nc
  463. module purge
  464. module load ${icesheet_modules}
  465. if [ "${icesheet_library_path}" != "" ]; then
  466. export LD_LIBRARY_PATH="${icesheet_library_path}"
  467. fi
  468. /bin/bash ./launch_icesheet.sh
  469. mv -f ${outfile_ocean2ice} ${archive_dir}/isfcpl
  470. if (( ice_atm_cpl == 1 )); then
  471. mv -f ${outfile_atm2ice} ${archive_dir}/isfcpl
  472. fi
  473. if [[ "${name_icesheet_model}" == "fETISh" ]]; then
  474. find . -type l -name "${exp_name}_${name_icesheet_model}Run_??????_toto.mat" -exec rm -f {} \;
  475. mv -f ${exp_name}_${name_icesheet_model}Run_??????_toto.mat ${archive_dir}/${name_icesheet_model}
  476. ln -sf ${archive_dir}/${name_icesheet_model}/${exp_name}_${name_icesheet_model}Run_${date_end_isfcpl::-2}_toto.mat
  477. else
  478. # GVAN EDIT HERE; DO THE SAME FOR BISICLES OUTPUT NOMENCLATURE
  479. echo "Ice-sheet models other than fETISh not implemented yet."
  480. exit 2
  481. fi
  482. curr_geom_file="${exp_name}_ice2ocean_${date_end_isfcpl}.nc"
  483. new_bathy_file="${exp_name}_bathymetry_isfdraft_${date_end_isfcpl}.nc"
  484. source build_pp_icesheet_to_nemo.sh > pp_${name_icesheet_model}_to_nemo.py
  485. if [ "${init_bathy}" == 0 ]; then
  486. mv -f ${curr_bathy_file} ${archive_dir}/isfcpl
  487. else
  488. init_bathy=0
  489. cp -f ${curr_bathy_file} ${archive_dir}/isfcpl
  490. fi
  491. module purge
  492. load_modules_isfcpl
  493. python pp_${name_icesheet_model}_to_nemo.py
  494. mv -f ${curr_geom_file} ${archive_dir}/isfcpl
  495. curr_bathy_file=${new_bathy_file}
  496. ln -sf ${curr_bathy_file} ${run_dir}/bathy_meter.nc
  497. ln -sf ${curr_bathy_file} ${run_dir}/isf_draft_meter.nc
  498. n_done_isfcpl_window_by_nemo=$(( leg_number / n_res_perisfcpl ))
  499. printf "# Information for the ice sheet model\n" > ${start_dir}/isfcpl.track
  500. printf "run_name=${exp_name}\n" >> ${start_dir}/isfcpl.track
  501. printf "initial_date=${run_start_date_yyyymmdd}\n" >> ${start_dir}/isfcpl.track
  502. printf "n_month_isfcpl=${n_month_isfcpl}\n" >> ${start_dir}/isfcpl.track
  503. printf "n_isfcpl_window=${n_isfcpl_window}\n" >> ${start_dir}/isfcpl.track
  504. printf "n_done_isfcpl_window_by_nemo=${n_done_isfcpl_window_by_nemo}\n" >> ${start_dir}/isfcpl.track
  505. printf "date_beg_isfcpl=${nextleg_start_date_yyyymmdd}\n" >> ${start_dir}/isfcpl.track
  506. printf "date_end_isfcpl=YYYYMMDD\n" >> ${start_dir}/isfcpl.track
  507. printf "ice_atm_cpl=${ice_atm_cpl}\n\n" >> ${start_dir}/isfcpl.track
  508. printf "# Information for NEMO to keep up\n" >> ${start_dir}/isfcpl.track
  509. printf "init_bathy=${init_bathy}\n" >> ${start_dir}/isfcpl.track
  510. printf "curr_bathy_file=${curr_bathy_file}\n" >> ${start_dir}/isfcpl.track
  511. fi
  512. module purge
  513. module load ${module_list:?}
  514. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  515. fi
  516. # -------------------------------------------------------------------------
  517. # *** Move output files to archive directory
  518. # -------------------------------------------------------------------------
  519. ### NEMO
  520. outdir="${archive_dir}/output/nemo/$(printf %03d $((leg_number)))"
  521. mkdir -p ${outdir}
  522. for v in grid_U grid_V grid_W grid_T icemod SBC scalar SBC_scalar diad_T \
  523. grid_T_2D grid_U_2D grid_V_2D grid_W_2D grid_T_3D grid_U_3D grid_V_3D grid_W_3D \
  524. grid_1point grid_T_3D_ncatice vert_sum \
  525. grid_ptr_W_3basin_3D grid_ptr_T_3basin_2D grid_ptr_T_2D \
  526. zoom_700_sum zoom_300_sum zoom_2000_sum
  527. do
  528. for f in ${exp_name}_*_????????_????????_*${v}*.nc
  529. do
  530. test -f $f && mv $f $outdir/
  531. done
  532. done
  533. # -------------------------------------------------------------------------
  534. # *** Move restart files to archive directory
  535. # -------------------------------------------------------------------------
  536. ### NEMO
  537. outdir="${archive_dir}/restart/nemo/$(printf %03d $((leg_number)))"
  538. mkdir -p ${outdir}
  539. ns=$(printf %08d $(( leg_end_sec / nem_time_step_sec )))
  540. for f in oce ice
  541. do
  542. mv ${exp_name}_${ns}_restart_${f}_????.nc ${outdir}
  543. done
  544. # -------------------------------------------------------------------------
  545. # *** Move log files to archive directory
  546. # -------------------------------------------------------------------------
  547. outdir="${archive_dir}/log/$(printf %03d $((leg_number)))"
  548. mkdir -p ${outdir}
  549. for f in \
  550. ocean.output time.step solver.stat
  551. do
  552. test -f ${f} && mv ${f} ${outdir}
  553. done
  554. # ---------------------------------------------------------------------
  555. # *** Remove all leftover output files from previous legs
  556. # ---------------------------------------------------------------------
  557. ### NEMO files
  558. rm -f ${exp_name}_??_????????_????????_{grid_U,grid_V,grid_W,grid_T,icemod,SBC,scalar,SBC_scalar}.nc
  559. rm -f ${exp_name}_*restart_*nc
  560. ns=$(printf %08d $(( leg_start_sec / nem_time_step_sec )))
  561. for (( n=0 ; n<nem_numproc ; n++ ))
  562. do
  563. np=$(printf %04d ${n})
  564. rm -f restart_oce_${np}.nc
  565. rm -f restart_ice_${np}.nc
  566. done
  567. # -------------------------------------------------------------------------
  568. # *** Write the restart control file
  569. # -------------------------------------------------------------------------
  570. shopt -u nullglob
  571. tr=$(date -d "0 -$time_begin sec + $time_end sec" +%T)
  572. current_date=$(date +'%F %T')
  573. {
  574. echo "#"
  575. echo "# Finished leg at ${current_date} after ${tr} (hh:mm:ss)"
  576. echo "leg_number=${leg_number}"
  577. echo "leg_start_date=\"${leg_start_date}\""
  578. echo "leg_end_date=\"${leg_end_date}\""
  579. } | tee -a "${info_file}"
  580. # Need to reset force_run_from_scratch in order to avoid destroying the next leg
  581. force_run_from_scratch=false
  582. special_restart=false
  583. done # Loop over legs
  584. cd ${start_dir}
  585. cd - >/dev/null
  586. [[ $@ == *noresubmit* ]] && exit 0
  587. if (( leg_end_epoch < run_end_epoch )) ; then
  588. echo "Leg end earlier than end of simulation."
  589. echo "Submitting another job."
  590. if [[ "$@" == *"local"* ]] ; then
  591. exec "$0" "$@"
  592. elif [ "qsub" == *sbatch* ] ; then
  593. sbatch $0 $@ | awk '{print $4}' >> ${run_dir}/.coral_jobs
  594. elif [[ $USER == "vsc"* ]]; then
  595. if [ -f ${PBS_O_WORKDIR-$PWD}/tmp/credits_file ]; then
  596. credits=`cat ${PBS_O_WORKDIR-$PWD}/tmp/credits_file`
  597. else
  598. credits_tmp=`grep "account string:" ${PBS_O_WORKDIR-$PWD}/*.o* -R | sed 's/^.*: //'`
  599. credits=`echo $credits_tmp | awk '{ print $1 }'`
  600. cd ${start_dir}
  601. mkdir -p tmp
  602. echo ${credits} > tmp/credits_file
  603. fi
  604. if [ -f ${PBS_O_WORKDIR-$PWD}/tmp/script_name ]; then
  605. script_name=`cat ${PBS_O_WORKDIR-$PWD}/tmp/script_name`
  606. else
  607. script_name="run.sh"
  608. fi
  609. ssh login2 "cd $start_dir; qsub -A $credits $script_name | tee -a coral_jobs"
  610. sleep 2
  611. jobid=`cat coral_jobs`
  612. rm -f coral_jobs
  613. jobid=${jobid%%.*}
  614. echo "${jobid}" >> "${run_dir}"/.coral_jobs ;
  615. else
  616. "qsub" -v PBS_OPTIONS="$@" "$0" | tee -a coral_jobs
  617. sleep 2
  618. jobid=`cat coral_jobs`
  619. rm -f coral_jobs
  620. jobid=${jobid%.*}
  621. echo "${jobid}" >> "${run_dir}"/.coral_jobs
  622. fi
  623. else
  624. if [[ $USER == "vsc"* ]]; then
  625. rm -rf ${start_dir}/tmp
  626. fi
  627. rm -rf ${run_dir}/../cclm_out_tmp
  628. echo "Nothing left to do. Exiting."
  629. cat << "EOF"
  630. _____ _ _ _ _ _ _ _
  631. / ____(_) | | | | (_) | | | | | |
  632. | (___ _ _ __ ___ _ _| | __ _| |_ _ ___ _ __ ___ ___ _ __ ___ _ __ | | ___| |_ ___ __| |
  633. \___ \| | '_ ` _ \| | | | |/ _` | __| |/ _ \| '_ \ / __/ _ \| '_ ` _ \| '_ \| |/ _ \ __/ _ \/ _` |
  634. ____) | | | | | | | |_| | | (_| | |_| | (_) | | | | | (_| (_) | | | | | | |_) | | __/ || __/ (_| |
  635. |_____/|_|_| |_| |_|\__,_|_|\__,_|\__|_|\___/|_| |_| \___\___/|_| |_| |_| .__/|_|\___|\__\___|\__,_|
  636. | |
  637. |_|
  638. EOF
  639. fi
  640. exit 0