skeleton_isfcpl.sh 30 KB

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