skeleton_isfcpl.sh 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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 "date_beg_isfcpl=${date_beg_isfcpl}\n" >> ${start_dir}/isfcpl.track
  290. printf "date_end_isfcpl=YYYYMMDD\n" >> ${start_dir}/isfcpl.track
  291. printf "ice_atm_cpl=${ice_atm_cpl}\n\n" >> ${start_dir}/isfcpl.track
  292. printf "# Information for NEMO to keep up\n" >> ${start_dir}/isfcpl.track
  293. printf "init_bathy=1\n" >> ${start_dir}/isfcpl.track
  294. printf "curr_bathy_file=${init_bathy_file}\n" >> ${start_dir}/isfcpl.track
  295. init_bathy=1
  296. curr_bathy_file=${init_bathy_file}
  297. fi
  298. ln -sf ${curr_bathy_file} ${run_dir}/bathy_meter.nc
  299. ln -sf ${curr_bathy_file} ${run_dir}/isf_draft_meter.nc
  300. fi # if [ "${iscpl}" == 1 ]
  301. # ---------------------------------------------------------------------
  302. # *** Copy potential special restarts
  303. # ---------------------------------------------------------------------
  304. if $special_restart
  305. then
  306. ### Getting the right output folders
  307. leg_start_date_tmp=$leg_start_date
  308. leg_end_date_tmp=$leg_end_date
  309. leg_number_tmp=$leg_number
  310. rsync -avz ${run_dir}/../../${special_restart_from}/rundir/${info_file} ${run_dir}
  311. special_date=$(date -uR -d "${special_restart_date}")
  312. sed -i "/$special_date/q" ${run_dir}/${info_file}
  313. . ${run_dir}/${info_file}
  314. special_restart_leg=$(printf %03d $((leg_number)))
  315. echo "special_date: $special_date"
  316. echo "special_restart_leg: $special_restart_leg"
  317. echo "special_restart_from: $special_restart_from"
  318. ### NEMO
  319. indir="${run_dir}/../../${special_restart_from}/archive/restart/nemo/$(printf %03d $((special_restart_leg)))"
  320. cd ${indir}
  321. for f in *.nc; do
  322. nf=`echo "${f/$special_restart_from/$exp_name}"`
  323. echo "copy $f to ${run_dir}/$nf"
  324. cp $f ${run_dir}/$nf
  325. done
  326. cd -
  327. cd ${run_dir}
  328. echo "pwd: $PWD"
  329. for f in ${exp_name}_????????_restart_???_????.nc; do
  330. nf=`echo ${f: -19}`
  331. echo "linking $f to ${run_dir}/$nf"
  332. ln -s $f $nf
  333. done
  334. cd ${run_dir}
  335. rm -rf ${info_file}
  336. leg_start_date=$leg_start_date_tmp
  337. leg_end_date=$leg_end_date_tmp
  338. leg_number=$leg_number_tmp
  339. elif ${special_rs_nemo}
  340. then
  341. dir_rs=`dirname ${special_rs_nemo_stencil}`
  342. base_rs=`basename ${special_rs_nemo_stencil}`
  343. n_oce_rs=`find ${dir_rs} -name "${base_rs}oce_????.nc" | wc -l`
  344. if(( n_oce_rs != nem_numproc )); then
  345. echo "in special_rs_nemo, not the right amount of ocean restart files."
  346. echo "${n_oce_rs} instead of ${nem_numproc}. exiting."
  347. exit 2
  348. fi
  349. n_ice_rs=`find ${dir_rs} -name "${base_rs}ice_????.nc" | wc -l`
  350. if(( n_ice_rs != nem_numproc )); then
  351. echo "in special_rs_nemo, not the right amount of ice restart files."
  352. echo "${n_ice_rs} instead of ${nem_numproc}. exiting."
  353. exit 2
  354. fi
  355. for (( n=0 ; n<nem_numproc ; n++ ))
  356. do
  357. np=$(printf %04d ${n})
  358. ln -fs ${special_rs_nemo_stencil}oce_${np}.nc restart_oce_${np}.nc
  359. ln -fs ${special_rs_nemo_stencil}ice_${np}.nc restart_ice_${np}.nc
  360. done
  361. fi # if $special_restart
  362. else # if ! $leg_is_restart
  363. # ---------------------------------------------------------------------
  364. # *** Creates some directories
  365. # ---------------------------------------------------------------------
  366. mkdir -p ${out_dir}
  367. if [ "${isfcpl}" == "1" ]; then
  368. ln -sf ${curr_bathy_file} ${run_dir}/bathy_meter.nc
  369. ln -sf ${curr_bathy_file} ${run_dir}/isf_draft_meter.nc
  370. fi
  371. # ---------------------------------------------------------------------
  372. # *** Copy restart files in run directory
  373. # ---------------------------------------------------------------------
  374. cd ${run_dir}
  375. ### NEMO
  376. outdir="${archive_dir}/restart/nemo/$(printf %03d $((leg_number-1)))"
  377. ns=$(printf %08d $(( leg_start_sec / nem_time_step_sec )))
  378. for f in oce ice
  379. do
  380. echo "${outdir}/${exp_name}_${ns}_restart_${f}_????.nc"
  381. ln -sf ${outdir}/${exp_name}_${ns}_restart_${f}_????.nc ${run_dir}
  382. done
  383. for (( n=0 ; n<nem_numproc ; n++ ))
  384. do
  385. np=$(printf %04d ${n})
  386. ln -fs ${exp_name}_${ns}_restart_oce_${np}.nc restart_oce_${np}.nc
  387. ln -fs ${exp_name}_${ns}_restart_ice_${np}.nc restart_ice_${np}.nc
  388. done
  389. fi # ! $leg_is_restart
  390. # -------------------------------------------------------------------------
  391. # *** Update ocean namelist
  392. # -------------------------------------------------------------------------
  393. source build_namelist_cfg.sh > namelist_cfg
  394. # -------------------------------------------------------------------------
  395. # *** Build ice sheet launch
  396. # -------------------------------------------------------------------------
  397. if (( isfcpl == 1)); then
  398. source ${icesheet_launch_template} > launch_icesheet.sh
  399. chmod +x launch_icesheet.sh
  400. fi
  401. # -------------------------------------------------------------------------
  402. # *** Launch experiment
  403. # -------------------------------------------------------------------------
  404. echo "START of mpirun"
  405. time_begin=$(date +%s)
  406. mpirun -np "${xio_numproc:?}" "${xio_exe_file:?}" : -np "${nem_numproc:?}" "${nem_exe_file:?}"
  407. time_end=$(date +%s)
  408. echo "END of: mpirun"
  409. # -------------------------------------------------------------------------
  410. # *** Ice sheet coupling
  411. # -------------------------------------------------------------------------
  412. if (( isfcpl == 1 )); then
  413. cnt_res_isfcpl=$(( $(( ${leg_number} - 1 )) % ${n_res_perisfcpl} ))
  414. fmt_res_isfcpl=$(printf "%02d" ${cnt_res_isfcpl})
  415. in_fwfisf_stencil="${exp_name}_${frequency_fwfisf}_${leg_start_date_yyyymmdd}_${leg_almostend_date_yyyymmdd}_${suffix_fwfisf}"
  416. out_fwfisf_stencil="${exp_name}_fwfisf_res"
  417. out_fwfisf_curr="${out_fwfisf_stencil}${fmt_res_isfcpl}"
  418. if (( is_xios2 == 0 )); then
  419. module purge
  420. module load ${module_list:?}
  421. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  422. /bin/bash ${rebuild_nemo_exe} ${in_fwfisf_stencil} ${xio_numproc}
  423. module purge
  424. load_modules_isfcpl
  425. cdo selname,${name_var_fwfisf} ${in_fwfisf_stencil}.nc tmp_fwfisf.nc
  426. cdo monmean tmp_fwfisf.nc ${out_fwfisf_curr}.nc
  427. rm -f ${in_fwfisf_stencil}.nc tmp_fwfisf.nc
  428. else
  429. cdo selname,${name_var_fwfisf} ${in_fwfisf_stencil}.nc tmp_fwfisf.nc
  430. cdo monmean tmp_fwfisf.nc ${out_fwfisf_curr}.nc
  431. rm -f tmp_fwfisf.nc
  432. fi
  433. mv -f ${out_fwfisf_curr}.nc ${out_dir}/isfcpl_tmp/
  434. if (( ${leg_number} % ${n_res_perisfcpl} == 0 )); then
  435. date_end_isfcpl=${leg_almostend_date_yyyymmdd}
  436. sed -i "s/.*date_end_isfcpl=.*/date_end_isfcpl=${date_end_isfcpl}/" ${start_dir}/isfcpl.track
  437. module purge
  438. module load ${module_list:?}
  439. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  440. /bin/bash ${rebuild_nemo_exe} mesh_mask ${nem_numproc}
  441. module purge
  442. load_modules_isfcpl
  443. infile_ocean2ice="${exp_name}_ocean2ice_raw_${date_beg_isfcpl}-${date_end_isfcpl}.nc"
  444. outfile_ocean2ice="${exp_name}_ocean2ice_${date_beg_isfcpl}-${date_end_isfcpl}.nc"
  445. fwfisf_records=""
  446. for((rs2=0; rs2<n_res_perisfcpl; rs2++)); do
  447. fmt_rs2=$(printf "%02d" ${rs2})
  448. curr_file="${out_dir}/isfcpl_tmp/${out_fwfisf_stencil}${fmt_rs2}.nc"
  449. if [ -f ${curr_file} ] ; then
  450. fwfisf_records="${fwfisf_records} ${curr_file}"
  451. else
  452. echo "${curr_file} is not in ${out_dir}/isfcpl_tmp. It is needed for NEMO - ice sheet coupling. Exiting"
  453. exit 2
  454. fi
  455. done
  456. cdo -O copy ${fwfisf_records} ${infile_ocean2ice}
  457. rm -f ${fwfisf_records}
  458. source build_pp_nemo_to_icesheet.sh > ./pp_nemo_to_${name_icesheet_model}.py
  459. python pp_nemo_to_${name_icesheet_model}.py
  460. mv -f ${infile_ocean2ice} ${archive_dir}/isfcpl/
  461. mv -f mesh_mask.nc ${archive_dir}/isfcpl/${exp_name}_mesh_mask_${date_beg_isfcpl}-${date_end_isfcpl}.nc
  462. module purge
  463. module load ${icesheet_modules}
  464. if [ "${icesheet_library_path}" != "" ]; then
  465. export LD_LIBRARY_PATH="${icesheet_library_path}"
  466. fi
  467. /bin/bash ./launch_icesheet.sh
  468. mv -f ${outfile_ocean2ice} ${archive_dir}/isfcpl
  469. if (( ice_atm_cpl == 1 )); then
  470. mv -f ${outfile_atm2ice} ${archive_dir}/isfcpl
  471. fi
  472. if [[ "${name_icesheet_model}" == "fETISh" ]]; then
  473. find . -type l -name "${exp_name}_${name_icesheet_model}Run_??????_toto.mat" -exec rm -f {} \;
  474. mv -f ${exp_name}_${name_icesheet_model}Run_??????_toto.mat ${archive_dir}/${name_icesheet_model}
  475. ln -sf ${archive_dir}/${name_icesheet_model}/${exp_name}_${name_icesheet_model}Run_${date_end_isfcpl::-2}_toto.mat
  476. else
  477. # GVAN EDIT HERE; DO THE SAME FOR BISICLES OUTPUT NOMENCLATURE
  478. echo "Ice-sheet models other than fETISh not implemented yet."
  479. exit 2
  480. fi
  481. curr_geom_file="${exp_name}_ice2ocean_${date_end_isfcpl}.nc"
  482. new_bathy_file="${exp_name}_bathymetry_isfdraft_${date_end_isfcpl}.nc"
  483. source build_pp_icesheet_to_nemo.sh > pp_${name_icesheet_model}_to_nemo.py
  484. if [ "${init_bathy}" == 0 ]; then
  485. mv -f ${curr_bathy_file} ${archive_dir}/isfcpl
  486. else
  487. init_bathy=0
  488. cp -f ${curr_bathy_file} ${archive_dir}/isfcpl
  489. fi
  490. module purge
  491. load_modules_isfcpl
  492. python pp_${name_icesheet_model}_to_nemo.py
  493. mv -f ${curr_geom_file} ${archive_dir}/isfcpl
  494. curr_bathy_file=${new_bathy_file}
  495. ln -sf ${curr_bathy_file} ${run_dir}/bathy_meter.nc
  496. ln -sf ${curr_bathy_file} ${run_dir}/isf_draft_meter.nc
  497. printf "# Information for the ice sheet model\n" > ${start_dir}/isfcpl.track
  498. printf "run_name=${exp_name}\n" >> ${start_dir}/isfcpl.track
  499. printf "initial_date=${run_start_date_yyyymmdd}\n" >> ${start_dir}/isfcpl.track
  500. printf "n_month_isfcpl=${n_month_isfcpl}\n" >> ${start_dir}/isfcpl.track
  501. printf "n_isfcpl_window=${n_isfcpl_window}\n" >> ${start_dir}/isfcpl.track
  502. printf "date_beg_isfcpl=${nextleg_start_date_yyyymmdd}\n" >> ${start_dir}/isfcpl.track
  503. printf "date_end_isfcpl=YYYYMMDD\n" >> ${start_dir}/isfcpl.track
  504. printf "ice_atm_cpl=${ice_atm_cpl}\n\n" >> ${start_dir}/isfcpl.track
  505. printf "# Information for NEMO to keep up\n" >> ${start_dir}/isfcpl.track
  506. printf "init_bathy=${init_bathy}\n" >> ${start_dir}/isfcpl.track
  507. printf "curr_bathy_file=${curr_bathy_file}\n" >> ${start_dir}/isfcpl.track
  508. module purge
  509. module load ${module_list:?}
  510. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  511. fi
  512. fi
  513. # -------------------------------------------------------------------------
  514. # *** Move output files to archive directory
  515. # -------------------------------------------------------------------------
  516. ### NEMO
  517. outdir="${archive_dir}/output/nemo/$(printf %03d $((leg_number)))"
  518. mkdir -p ${outdir}
  519. for v in grid_U grid_V grid_W grid_T icemod SBC scalar SBC_scalar diad_T \
  520. grid_T_2D grid_U_2D grid_V_2D grid_W_2D grid_T_3D grid_U_3D grid_V_3D grid_W_3D \
  521. grid_1point grid_T_3D_ncatice vert_sum \
  522. grid_ptr_W_3basin_3D grid_ptr_T_3basin_2D grid_ptr_T_2D \
  523. zoom_700_sum zoom_300_sum zoom_2000_sum
  524. do
  525. for f in ${exp_name}_*_????????_????????_*${v}*.nc
  526. do
  527. test -f $f && mv $f $outdir/
  528. done
  529. done
  530. # -------------------------------------------------------------------------
  531. # *** Move restart files to archive directory
  532. # -------------------------------------------------------------------------
  533. ### NEMO
  534. outdir="${archive_dir}/restart/nemo/$(printf %03d $((leg_number)))"
  535. mkdir -p ${outdir}
  536. ns=$(printf %08d $(( leg_end_sec / nem_time_step_sec )))
  537. for f in oce ice
  538. do
  539. mv ${exp_name}_${ns}_restart_${f}_????.nc ${outdir}
  540. done
  541. # -------------------------------------------------------------------------
  542. # *** Move log files to archive directory
  543. # -------------------------------------------------------------------------
  544. outdir="${archive_dir}/log/$(printf %03d $((leg_number)))"
  545. mkdir -p ${outdir}
  546. for f in \
  547. ocean.output time.step solver.stat
  548. do
  549. test -f ${f} && mv ${f} ${outdir}
  550. done
  551. # ---------------------------------------------------------------------
  552. # *** Remove all leftover output files from previous legs
  553. # ---------------------------------------------------------------------
  554. ### NEMO files
  555. rm -f ${exp_name}_??_????????_????????_{grid_U,grid_V,grid_W,grid_T,icemod,SBC,scalar,SBC_scalar}.nc
  556. rm -f ${exp_name}_*restart_*nc
  557. ns=$(printf %08d $(( leg_start_sec / nem_time_step_sec )))
  558. for (( n=0 ; n<nem_numproc ; n++ ))
  559. do
  560. np=$(printf %04d ${n})
  561. rm -f restart_oce_${np}.nc
  562. rm -f restart_ice_${np}.nc
  563. done
  564. # -------------------------------------------------------------------------
  565. # *** Write the restart control file
  566. # -------------------------------------------------------------------------
  567. shopt -u nullglob
  568. tr=$(date -d "0 -$time_begin sec + $time_end sec" +%T)
  569. current_date=$(date +'%F %T')
  570. {
  571. echo "#"
  572. echo "# Finished leg at ${current_date} after ${tr} (hh:mm:ss)"
  573. echo "leg_number=${leg_number}"
  574. echo "leg_start_date=\"${leg_start_date}\""
  575. echo "leg_end_date=\"${leg_end_date}\""
  576. } | tee -a "${info_file}"
  577. # Need to reset force_run_from_scratch in order to avoid destroying the next leg
  578. force_run_from_scratch=false
  579. special_restart=false
  580. done # Loop over legs
  581. cd ${start_dir}
  582. cd - >/dev/null
  583. [[ $@ == *noresubmit* ]] && exit 0
  584. if (( leg_end_epoch < run_end_epoch )) ; then
  585. echo "Leg end earlier than end of simulation."
  586. echo "Submitting another job."
  587. if [[ "$@" == *"local"* ]] ; then
  588. exec "$0" "$@"
  589. elif [ "qsub" == *sbatch* ] ; then
  590. sbatch $0 $@ | awk '{print $4}' >> ${run_dir}/.coral_jobs
  591. elif [[ $USER == "vsc"* ]]; then
  592. if [ -f ${PBS_O_WORKDIR-$PWD}/tmp/credits_file ]; then
  593. credits=`cat ${PBS_O_WORKDIR-$PWD}/tmp/credits_file`
  594. else
  595. credits_tmp=`grep "account string:" ${PBS_O_WORKDIR-$PWD}/*.o* -R | sed 's/^.*: //'`
  596. credits=`echo $credits_tmp | awk '{ print $1 }'`
  597. cd ${start_dir}
  598. mkdir -p tmp
  599. echo ${credits} > tmp/credits_file
  600. fi
  601. if [ -f ${PBS_O_WORKDIR-$PWD}/tmp/script_name ]; then
  602. script_name=`cat ${PBS_O_WORKDIR-$PWD}/tmp/script_name`
  603. else
  604. script_name="run.sh"
  605. fi
  606. ssh login2 "cd $start_dir; qsub -A $credits $script_name | tee -a coral_jobs"
  607. sleep 2
  608. jobid=`cat coral_jobs`
  609. rm -f coral_jobs
  610. jobid=${jobid%%.*}
  611. echo "${jobid}" >> "${run_dir}"/.coral_jobs ;
  612. else
  613. "qsub" -v PBS_OPTIONS="$@" "$0" | tee -a coral_jobs
  614. sleep 2
  615. jobid=`cat coral_jobs`
  616. rm -f coral_jobs
  617. jobid=${jobid%.*}
  618. echo "${jobid}" >> "${run_dir}"/.coral_jobs
  619. fi
  620. else
  621. if [[ $USER == "vsc"* ]]; then
  622. rm -rf ${start_dir}/tmp
  623. fi
  624. rm -rf ${run_dir}/../cclm_out_tmp
  625. echo "Nothing left to do. Exiting."
  626. cat << "EOF"
  627. _____ _ _ _ _ _ _ _
  628. / ____(_) | | | | (_) | | | | | |
  629. | (___ _ _ __ ___ _ _| | __ _| |_ _ ___ _ __ ___ ___ _ __ ___ _ __ | | ___| |_ ___ __| |
  630. \___ \| | '_ ` _ \| | | | |/ _` | __| |/ _ \| '_ \ / __/ _ \| '_ ` _ \| '_ \| |/ _ \ __/ _ \/ _` |
  631. ____) | | | | | | | |_| | | (_| | |_| | (_) | | | | | (_| (_) | | | | | | |_) | | __/ || __/ (_| |
  632. |_____/|_|_| |_| |_|\__,_|_|\__,_|\__|_|\___/|_| |_| \___\___/|_| |_| |_| .__/|_|\___|\__\___|\__,_|
  633. | |
  634. |_|
  635. EOF
  636. fi
  637. exit 0