bash_functions.bash 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. #!/usr/bin/env bash
  2. function barakuda_usage()
  3. {
  4. echo
  5. echo "USAGE: ${0} -C <config> -R <experiment> (options)"
  6. echo
  7. echo " Available configs are:"
  8. for cc in ${list_conf}; do
  9. echo " * ${cc}"
  10. done
  11. echo
  12. echo
  13. echo " OPTIONS:"
  14. echo
  15. echo " -f <years> => how many years per NEMO file? default = 1"
  16. echo
  17. echo " -y <YYYY> => force initial year to YYYY"
  18. echo
  19. echo " -F => forces creation of diagnostic page even if treatment of output files is not finished"
  20. echo
  21. echo " -e => create the HTML diagnostics page on local or remote server"
  22. echo
  23. echo " -E => same as '-e' but also create the 2D plots / observations"
  24. echo " you need to have built a climatology of your experiment with 'build_clim.sh' first!"
  25. echo
  26. echo " -c <exp> => when '-E' specified, 2D comparison diagnostics are performed "
  27. echo " against the climatology of another experiment <exp> rather than observations"
  28. echo
  29. echo " -h => print this message"
  30. echo
  31. }
  32. function barakuda_init()
  33. {
  34. # Some defaults (don't modify, they should be modified according to what's in your config_<CONFIG>.sh file...)
  35. export NCHNKS_Y=1
  36. export Y_INI_EC="1990" ; # initial year
  37. export M_INI_EC="01" ; # initial month
  38. export cmmdd1="0101"
  39. export cmmdd2="1231"
  40. export l_y2_j=false ; # if 1-year long NEMO files end sometime in year+1 instead of year!
  41. # Supported ORCA grids:
  42. export ORCA_LIST="ORCA025.L75 ORCA1.L75 ORCA1.L46 ORCA1.L42 ORCA2.L31 NANUK025"
  43. # Some defaults:
  44. export LFORCE_YINI=false
  45. export LFORCE_YEND=false
  46. export EXPREF=""
  47. export ISTAGE=1 ; # 1 => generation of data diagnostic files
  48. # # 2 => creation of figures and diagnostic HTML page
  49. export LFORCEDIAG=false
  50. export l_clim_diag=false
  51. export IFREQ_SAV_YEARS=1
  52. #
  53. if [ ! "`which ncdump 2>/dev/null`" = "" ]; then
  54. export NCDUMP="ncdump"
  55. elif [ -f ${NCDF_DIR}/bin/ncdump ]; then
  56. export NCDUMP="${NCDF_DIR}/bin/ncdump"
  57. else
  58. echo "ERROR: cannot find command 'ncdump' on your system..."; exit
  59. fi
  60. #
  61. }
  62. function barakuda_check()
  63. {
  64. #script=`basename $0 | sed -e s/'.sh'/''/g` ; # Job managers screw this!!!
  65. if [ -z ${CONFIG} ] || [ -z ${EXP} ]; then ${script}_usage ; exit ; fi
  66. if [ "${EXPREF}" != "" ] && [ ${ISTAGE} -eq 1 ]; then
  67. echo; echo " WARNING: option '-c' only makes sense when '-e' or '-E' are specified !"
  68. sleep 2; echo
  69. fi
  70. for og in ${ORCA_LIST}; do
  71. ORCA_CONF="`echo ${CONFIG} | cut -d '_' -f1`.`echo ${CONFIG} | cut -d '_' -f2`"
  72. #echo " ${og} / ${ORCA_CONF}"
  73. ca=""; ca=`echo ${ORCA_CONF} | grep ${og}`
  74. if [ "${ca}" != "" ]; then export ORCA=${og}; fi
  75. done
  76. if [ -z ${ORCA} ]; then echo "ORCA grid of config ${CONFIG} not supported yet"; exit; fi
  77. echo
  78. if [ "${script}" = "build_clim" ]; then
  79. echo Boo
  80. if [ -z ${Y1} ] || [ -z ${Y2} ]; then
  81. ${script}_usage
  82. echo; echo " ==> Please specify both first and last year for climatology (-i and -e ) !!!"; echo
  83. exit
  84. fi
  85. fi
  86. }
  87. function barakuda_setup()
  88. {
  89. #script=`basename $0 | sed -e s/'.sh'/''/g` ; # Job managers screw this!!!
  90. echo
  91. if [ ! "${ORCA}" = "${CONF}" ]; then echo "ERROR: ORCA and CONF disagree! => ${ORCA} ${CONF}"; exit; fi
  92. export ORCA=${CONF}
  93. echo
  94. if [ -z ${PYTHON_HOME} ]; then echo "ERROR: PYTHON_HOME is not set! => add it to config file"; exit; fi
  95. PYTH_tmp=`which python`
  96. export PYTH="${PYTH_tmp} -W ignore" ; # which Python installation to use
  97. export PYTHONPATH=${PYTHONPATH}:${BARAKUDA_ROOT}/python/modules ; # PATH to python barakuda modules
  98. export PYBRKD_EXEC_PATH=${BARAKUDA_ROOT}/python/exec ; # PATH to python barakuda executable
  99. echo " PYTHON_HOME => "${PYTHON_HOME} ; echo
  100. echo " TRANSPORT_SECTION_FILE => ${TRANSPORT_SECTION_FILE} !" ; echo
  101. if ${l_clim_diag} ; then
  102. echo
  103. echo " Files containing climatologies to be used:"
  104. echo " T 3D => ${F_T_OBS_3D_12} => ${NN_T_OBS}"
  105. echo " S 3D => ${F_S_OBS_3D_12} => ${NN_S_OBS}"
  106. echo " SST => ${F_SST_OBS_12} => ${NN_SST_OBS}"
  107. echo
  108. for ff in ${F_T_OBS_3D_12} ${F_S_OBS_3D_12} ${F_SST_OBS_12}; do
  109. if [ ! -f ${F_T_OBS_3D_12} ]; then echo "ERROR: ${ff} is missing!"; exit; fi
  110. done
  111. fi
  112. # Names for temperature, salinity, u- and v-current...
  113. if [ -z ${NN_T} ] || [ -z ${NN_S} ] || [ -z ${NN_U} ] || [ -z ${NN_V} ]; then
  114. echo "NN_T, NN_S, NN_U and NN_V are NOT given a value into"
  115. echo " in ${fconfig} "
  116. echo " => using default names: thetao, so, uo, vo" ; echo
  117. NN_T="thetao"; NN_S="so"; NN_U="uo"; NN_V="vo"
  118. fi
  119. echo ; echo " *** NN_T=${NN_T}, NN_S=${NN_S}, NN_U=${NN_U} and NN_V=${NN_V} "; echo
  120. # Checking what files we have / plan to use:
  121. if [ -z "${NEMO_SAVED_FILES}" ]; then
  122. echo "Please specify which NEMO files are saved (file suffixes, grid_T, ..., icemod) ?"
  123. echo " => set the variable NEMO_SAVED_FILES in your config_${CONFIG}.sh file!"; exit
  124. fi
  125. echo; echo "File types to import (NEMO_SAVED_FILES) : ${NEMO_SAVED_FILES}"; echo; echo
  126. # Need to be consistent with the netcdf installation upon which cdftools_light was compiled:
  127. ff="${BARAKUDA_ROOT}/cdftools_light/make.macro"
  128. if [ ! -f ${ff} ]; then echo "PROBLEM: cannot find ${ff} (needed to get NCDF_DIR)!"; exit; fi
  129. #export NCDF_DIR=`cat ${ff} | grep ^NCDF_DIR | cut -d = -f2 | sed -e s/' '//g`
  130. #NCDF_DIR="$`cat ${ff} | grep ^NCDF_DIR | cut -d = -f2 | cut -d '(' -f2 | cut -d ')' -f1`"
  131. export NCDF_DIR=${EBROOTNETCDFMINFORTRAN}
  132. echo ; echo "NCDF_DIR = ${NCDF_DIR}"; echo
  133. export LD_LIBRARY_PATH=${NCDF_DIR}/lib:${LD_LIBRARY_PATH}
  134. # Exporting some variables needed by the python scripts:
  135. export EXP=${EXP}
  136. export CONFEXP=${ORCA}-${EXP}
  137. export DIAG_D=${DIAG_DIR}/${CONFEXP}
  138. export CLIM_DIR=${DIAG_D}/clim
  139. if [ ${ISTAGE} -eq 1 ] || [ ! -z "${SLURM_JOBID}" ]; then
  140. # We need a scratch/temporary directory to copy these files to and gunzip them:
  141. # Normal case, inside a batch job scheduler:
  142. if [ ! -z "${SLURM_JOBID}" ]; then
  143. # NSC / Sweden ; Gustafson / BSC
  144. SCRATCH=`echo ${SCRATCH} | sed -e "s|<JOB_ID>|${SLURM_JOBID}|g"`
  145. export TMP_DIR=${SCRATCH}/tmp_${EXP}_${script}
  146. #
  147. elif [ ! -z "${LSB_JOBID}" ]; then
  148. # MARENOSTRUM / BSC
  149. export TMP_DIR=${TMPDIR}
  150. else
  151. # Default:
  152. export TMP_DIR=${SCRATCH}/tmp_${EXP}_${script}
  153. fi
  154. else
  155. export TMP_DIR=${SCRATCH}/html_${EXP}_tmp
  156. fi
  157. echo " IMPORTANT the TMP_DIR work directory is set to:" ; echo " ${TMP_DIR}"; echo ; sleep 2
  158. if [ -d ${TMP_DIR} ] && $(ls ${TMP_DIR}/* >& /dev/null); then
  159. echo "ERROR! TMP_DIR already exists and is not empty!"
  160. echo " => ${TMP_DIR}"
  161. exit
  162. fi
  163. mkdir -p ${DIAG_D}/flux_int_basins ${TMP_DIR}/movies
  164. ls ${TMP_DIR}
  165. export NEMO_OUT_D=`echo ${NEMO_OUT_STRCT} | sed -e "s|<ORCA>|${ORCA}|g" -e "s|<EXP>|${EXP}|g" -e "s|<Y_INI_EC>|${Y_INI_EC}|g" -e "s|<M_INI_EC>|${M_INI_EC}|g"`
  166. if [ ! -d ${NEMO_OUT_D} ]; then echo "Unfortunately we could not find ${NEMO_OUT_D}"; exit; fi
  167. # Where to look for NEMO namelists:
  168. #if [ ${ece_exp} -eq 0 ]; then
  169. # NEMO standalone:
  170. export NAMELIST_DIR=${NEMO_OUT_D}/..
  171. #elif [ ${ece_exp} -gt 0 ] && [ ${ece_exp} -lt 10 ]; then
  172. # # EC-Earth classic mode:
  173. # export NAMELIST_DIR=`echo ${NEMO_OUT_D} | sed -e "s|/output/nemo||g"`
  174. #else
  175. # # EC-Earth autosubmit mode TO FIX! :
  176. # echo; echo " WARNING ('barakuda_setup' of bash_functions.bash): don't know where to look for NEMO namelists!"
  177. #fi
  178. echo; echo " *** NAMELIST_DIR = ${NAMELIST_DIR} "; echo
  179. echo; echo " * Config to be used: ${CONFIG} => ORCA grid is ${ORCA}"
  180. echo " * Experiment is ${EXP} "; echo " * Files are stored into ${NEMO_OUT_D}"; echo; sleep 2
  181. export CPREF=`echo ${NEMO_FILE_PREFIX} | sed -e "s|<ORCA>|${ORCA}|g" -e "s|<EXP>|${EXP}|g" -e "s|<TSTAMP>|${TSTAMP}|g"`
  182. echo " NEMO files prefix = ${CPREF} "
  183. # only neede for barakuda.sh :
  184. if [ "${script}" = "barakuda" ]; then
  185. # Testing if NCO is installed:
  186. which ncks 1>out 2>/dev/null; ca=`cat out`; rm -f out
  187. if [ "${ca}" = "" ]; then echo "Install NCO!!!"; echo; exit; fi
  188. if [ ${ISTAGE} -eq 1 ]; then
  189. for ex in ${L_EXEC}; do check_if_file ${BARAKUDA_ROOT}/cdftools_light/bin/${ex} "Compile CDFTOOLS executables!"; done
  190. fi
  191. fi
  192. echo "${NEMO_OUT_D}" > ${DIAG_D}/NEMO_output_location.info
  193. if [ -z ${NCDF_DIR} ]; then
  194. if [ ! -z ${NETCDF_DIR} ]; then
  195. export NCDF_DIR=${NETCDF_DIR}
  196. elif [ ! -z ${NETCDF_HOME} ]; then
  197. export NCDF_DIR=${NETCDF_HOME}
  198. else
  199. echo "ERROR: NCDF_DIR could not be determined, please specify it in your config file!"
  200. exit
  201. fi
  202. fi
  203. echo
  204. }
  205. function barakuda_first_last_years()
  206. {
  207. cd ${NEMO_OUT_D}/
  208. #if [ ${ece_exp} -gt 0 ]; then
  209. if [ ! -d 001 ]; then
  210. echo " *** Inside: `pwd` !"; \ls -l ; echo
  211. echo "ERROR: since ece_exp=${ece_exp}, there should be a directory 001 in:"; echo " ${NEMO_OUT_D}"; echo; exit
  212. fi
  213. nby_ece=`ls -d ???/ | grep "[^0-9]" | wc -l`
  214. nby_ece_true=$((${nby_ece}*${IFREQ_SAV_YEARS}))
  215. echo " ${nby_ece_true} years have been completed..."
  216. cd 001/
  217. #fi
  218. # Try to guess the first year from stored "grid_T" files:
  219. YEAR_INI=`\ls ${CPREF}*${ctest}* | sed -e s/"${CPREF}"/""/g | head -1 | cut -c1-4`
  220. echo ${YEAR_INI} | grep "[^0-9]" >/dev/null ; # Checking if it's an integer:
  221. if [ ! "$?" -eq 1 ]; then
  222. echo "ERROR: it was imposible to guess initial year from your input files"
  223. echo " maybe the directory contains non-related files..."
  224. exit
  225. fi
  226. export YEAR_INI=$((${YEAR_INI}+0)) ; # example: 1 instead of 0001...
  227. export YEAR_INI_F=${YEAR_INI} ; # saving the year deduced from first file
  228. if ${LFORCE_YINI}; then
  229. if [ ${YEAR0} -lt ${YEAR_INI_F} ]; then echo "ERROR: forced initial year is before first year!"; exit; fi
  230. export YEAR_INI=${YEAR0}
  231. echo " Initial year forced to ${YEAR_INI} !"
  232. fi
  233. cd ${NEMO_OUT_D}/
  234. #if [ ${ece_exp} -gt 0 ]; then
  235. dir_end=`printf "%03d" ${nby_ece}`
  236. if [ ! -d ${dir_end} ]; then echo "ERROR: since ece_exp=${ece_exp}, there should be a directory ${dir_end} in:"; echo " ${NEMO_OUT_D}"; exit ; fi
  237. #export YEAR_END=$((${YEAR_INI}+${nby_ece}))
  238. #else
  239. #export YEAR_END=`\ls ${CPREF}*${ctest}* | sed -e s/"${CPREF}"/''/g | tail -1 | cut -c1-4`
  240. #echo ${YEAR_END} | grep "[^0-9]" >/dev/null; # Checking if it's an integer
  241. #if [ ! "$?" -eq 1 ]; then
  242. # echo "ERROR: it was imposible to guess the year coresponding to the last saved year!"
  243. # echo " => check your NEMO output directory and file naming..."; exit
  244. #fi
  245. export YEAR_END=$((${nby_ece}*${IFREQ_SAV_YEARS}))
  246. export YEAR_END=$((${YEAR_INI}+${YEAR_END}-1))
  247. #fi
  248. echo
  249. echo " *** Initial year set to ${YEAR_INI}"
  250. echo " *** Last year set to ${YEAR_END}"
  251. echo
  252. }
  253. function barakuda_init_plot()
  254. {
  255. if [ ! -z ${YEAR0} ]; then
  256. echo
  257. echo "WARNING: using the -y switch for the creation of the plots (switch -e)"
  258. echo " will have no impact. For the time slice to use, the plots are based"
  259. echo " on what is found in ${DIAG_D}/ !"
  260. echo " => the diagnostic netdcf files"
  261. echo " => which should also be equivalent to what's found in files"
  262. echo " first_year.info and last_year_done.info"
  263. echo ""
  264. sleep 5
  265. fi
  266. for fc in "first_year" "numb_year_per_file" "last_year_done"; do
  267. ff=${DIAG_D}/${fc}.info
  268. if [ ! -f ${ff} ]; then echo "ERROR: file ${ff} is missing!"; exit; fi
  269. done
  270. # Only doing plots...
  271. export YEAR_INI=`cat ${DIAG_D}/first_year.info`
  272. export YEAR_END=`cat ${DIAG_D}/last_year_done.info`
  273. export IFREQ_SAV_YEARS=`cat ${DIAG_D}/numb_year_per_file.info`
  274. echo
  275. echo " For time-series to be ploted:"
  276. echo " * initial year = ${YEAR_INI}"
  277. echo " * last year = ${YEAR_END}"
  278. echo
  279. }
  280. function barakuda_import_mesh_mask()
  281. {
  282. #script=`basename $0 | sed -e s/'.sh'/''/g` ; # Job managers screw this!!!
  283. cd ${TMP_DIR}/
  284. # Importing mesh_mask files:
  285. check_if_file ${MM_FILE}
  286. if [ ! -f ./mesh_mask.nc ]; then
  287. if [ "`cext ${MM_FILE}`" = ".gz" ]; then
  288. echo "Gunzipping mesh_mask.nc.gz..."
  289. cp -L ${MM_FILE} mesh_mask.nc.gz ; gunzip -f mesh_mask.nc.gz
  290. else
  291. cp -L ${MM_FILE} mesh_mask.nc
  292. fi
  293. fi
  294. #Fix, in case old nemo (prior version 3.6) must rename some metrics param:
  295. ca=""; ca=`${NCDUMP} -h mesh_mask.nc | grep 'e3t('`
  296. if [ ! "${ca}" = "" ]; then
  297. echo "Renaming some metrics into mesh_mask.nc !!!"
  298. ncrename -v e3t_0,e3t_1d -v e3w_0,e3w_1d -v gdept_0,gdept_1d -v gdepw_0,gdepw_1d mesh_mask.nc
  299. ncrename -v e3t,e3t_0 -v e3u,e3u_0 -v e3v,e3v_0 -v e3w,e3w_0 mesh_mask.nc
  300. echo
  301. fi
  302. check_if_file ${BM_FILE}
  303. if [ "`cext ${BM_FILE}`" = ".gz" ]; then
  304. cp -L ${BM_FILE} new_maskglo.nc.gz ; gunzip -f new_maskglo.nc.gz
  305. else
  306. cp -L ${BM_FILE} new_maskglo.nc
  307. fi
  308. # Setting MM BM files to the imported and unzipped version:
  309. export MM_FILE=${TMP_DIR}/mesh_mask.nc
  310. export BM_FILE=${TMP_DIR}/new_maskglo.nc
  311. echo " *** mesh_mask to be used: ${MM_FILE} "
  312. echo " *** basin mask to be used: ${BM_FILE} "
  313. echo
  314. if [ "${script}" = "build_clim" ]; then
  315. ln -sf mesh_mask.nc mesh_hgr.nc ; ln -sf mesh_mask.nc mesh_zgr.nc ; ln -sf mesh_mask.nc mask.nc
  316. fi
  317. }
  318. function barakuda_check_year_is_complete()
  319. {
  320. jy1=${jyear} ; jy2=$((${jyear}+${IFREQ_SAV_YEARS}-1))
  321. cy1=`printf "%04d" ${jy1}` ; cy2=`printf "%04d" ${jy2}`
  322. cy1m=`printf "%04d" $((${jy1}-${IFREQ_SAV_YEARS}))` ; cy2m=`printf "%04d" $((${jy2}-${IFREQ_SAV_YEARS}))`
  323. export i_get_file=1
  324. echo " *** (${cyear} => from ${cy1} to ${cy2})"
  325. export TTAG=${cy1}${cmmdd1}_${cy2}${cmmdd2} # calendar-related part of the file name
  326. # Testing if the current year-group has been done
  327. for ft in ${NEMO_SAVED_FILES}; do
  328. if ${lcontinue}; then
  329. ftst=${NEMO_OUT_D}/${cpf}${CPREF}${TTAG}_${ft} ; cfxt="0"
  330. for ca in "nc" "nc.gz" "nc4"; do
  331. if [ -f ${ftst}.${ca} ]; then cfxt="${ca}"; fi
  332. done
  333. if [ ${cfxt} = "0" ]; then
  334. echo "Year(s) ${cy1}-${cy2} is not completed yet:"; echo " => ${ftst}(?) is missing"; echo
  335. export lcontinue=false
  336. fi
  337. fi
  338. done
  339. if ${lcontinue}; then echo " *** All files for ${TTAG} are there!"; fi
  340. echo
  341. }
  342. function barakuda_import_files()
  343. {
  344. # Import command:
  345. CIMP="rsync -L"
  346. if [ "${CONF}" = "ORCA025.L75" ]; then CIMP="ln -sf" ; fi
  347. CROUT=${CPREF}${TTAG}
  348. # On what file type to test file presence:
  349. cgrid_test=`echo ${NEMO_SAVED_FILES} | cut -d ' ' -f2`
  350. echo " *** testing on files \"${cgrid_test}\" !"; echo
  351. l_happy=false
  352. while ! ${l_happy} ; do
  353. if [ ${IFREQ_SAV_YEARS} -eq 1 ]; then l_happy=true; fi
  354. rm -f *.tmp
  355. if [ ${i_get_file} -eq 1 ]; then
  356. echo " => gonna get ${CROUT}_* files..."
  357. # Importing required files to tmp dir and unzipping:
  358. for gt in ${NEMO_SAVED_FILES}; do
  359. f2i=${CROUT}_${gt}.nc ; sgz=""
  360. for ca in ".gz" "4"; do
  361. if [ -f ${NEMO_OUT_D}/${cpf}${f2i}${ca} ]; then sgz="${ca}"; fi
  362. done
  363. check_if_file ${NEMO_OUT_D}/${cpf}${f2i}${sgz}
  364. if [ ! -f ./${f2i} ]; then
  365. echo "Importing ${f2i}${sgz} ..."
  366. echo "${CIMP} ${NEMO_OUT_D}/${cpf}${f2i}${sgz} `pwd`/"
  367. ${CIMP} ${NEMO_OUT_D}/${cpf}${f2i}${sgz} ./
  368. if [ "${sgz}" = ".gz" ]; then gunzip -f ./${f2i}.gz ; fi
  369. if [ "${sgz}" = "4" ]; then
  370. echo "mv -f ./${f2i}4 ./${f2i}"
  371. mv -f ./${f2i}4 ./${f2i}
  372. fi
  373. check_if_file ${f2i}
  374. echo " ... done!"; echo
  375. else
  376. echo " ${f2i}${sgz} was already in `pwd`"
  377. fi
  378. done
  379. # Need to create annual files if more than 1 year in 1 one NEMO file
  380. if [ ${IFREQ_SAV_YEARS} -gt 1 ]; then
  381. for gt in ${NEMO_SAVED_FILES}; do
  382. ftd=./${CROUT}_${gt}.nc ; # file to divide!
  383. if [ -f ${ftd} ]; then
  384. jy=0
  385. while [ ${jy} -lt ${IFREQ_SAV_YEARS} ]; do
  386. im1=$((${jy}*12+1)) ; im2=$((${im1}+11))
  387. jytc=$((${jy}+${jy1})) ; cjytc=`printf "%04d" ${jytc}`
  388. ftc="./${CPREF}${cjytc}${cmmdd1}_${cjytc}${cmmdd2}_${gt}.nc" ; # file to create!
  389. if [ ! -f ${ftc} ]; then
  390. echo "Extracting file ${ftc} from ${ftd}, month records: ${im1}=>${im2}"
  391. echo "=> ncks -a -O -F -d time_counter,${im1},${im2} ${ftd} -o ${ftc}"
  392. ncks -a -O -F -d time_counter,${im1},${im2} ${ftd} -o ${ftc}
  393. echo
  394. fi
  395. jy=$((${jy}+1))
  396. done
  397. rm -f ${ftd}
  398. else
  399. echo " ${ftd} is not here!!!"
  400. fi
  401. done
  402. fi
  403. fi
  404. # In case the job crashed testing only on ${cgrid_test} file:
  405. if [ ${IFREQ_SAV_YEARS} -gt 1 ]; then
  406. if [ ! -f ./${CRT1M}_${cgrid_test}.nc ]; then
  407. echo "${CRT1M}_${cgrid_test}.nc is missing !!!"
  408. jy1=$((${jyear}-${jyear}%${IFREQ_SAV_YEARS})) ; jy2=$((${jy1}+${IFREQ_SAV_YEARS}-1))
  409. cy1=`printf "%04d" ${jy1}` ; cy2=`printf "%04d" ${jy2}`; TTAG=${cy1}${cmmdd1}_${cy2}${cmmdd2}
  410. CROUT=${CPREF}${TTAG}
  411. echo "Should re-import files ${CROUT}* cause something went wrong...."; echo
  412. i_get_file=1
  413. else
  414. l_happy=true
  415. fi
  416. fi
  417. done
  418. #
  419. # Testing if ALL required files are present now:
  420. for gt in ${NEMO_SAVED_FILES}; do
  421. ftt="./${CRT1M}_${gt}.nc" ; check_if_file ${ftt}
  422. done
  423. echo; echo "All required files are in `pwd` for year ${cyear} !"; echo
  424. }
  425. function build_clim_usage()
  426. {
  427. echo
  428. echo "USAGE: ${0} -C <config> -R <experiment> -i <first_year> -e <last_year> (options)"
  429. echo
  430. echo " Available configs are:"
  431. for cc in ${list_conf}; do
  432. echo " * ${cc}"
  433. done
  434. echo
  435. echo " OPTIONS:"
  436. echo " -f <years> => how many years per NEMO file? default = 1"
  437. echo " -h => print this message"
  438. echo
  439. }
  440. # ================ Misc. Functions ===============
  441. # Now a file extension:
  442. function cext()
  443. {
  444. nbc=`echo ${1} | wc -c`; nbb=`expr ${nbc} - 3`; echo "`echo ${1} | cut -c${nbb}-${nbc}`"
  445. # More elegant way would be following but there can be some "." into the ORCA grid...
  446. #echo ".`echo $1 | cut -d'.' -f2`
  447. }
  448. lb_is_leap()
  449. {
  450. if [ "$1" = "" ]; then echo "USAGE: lb_is_leap <YEAR>"; exit; fi
  451. #
  452. i_mod_400=`expr ${1} % 400`
  453. i_mod_100=`expr ${1} % 100`
  454. i_mod_4=`expr ${1} % 4`
  455. #
  456. if [ ${i_mod_400} -eq 0 -o ${i_mod_4} -eq 0 -a ! ${i_mod_100} -eq 0 ]; then
  457. echo "1"
  458. else
  459. echo "0"
  460. fi
  461. }
  462. lb_num_leap()
  463. {
  464. #
  465. # number of leap years comprised in <YEAR1> (included) and <YEAR2> (excluded)...
  466. #
  467. if [ "$2" = "" ]; then echo "USAGE: num_leap <YEAR1> <YEAR2>"; exit; fi
  468. icpt=0
  469. jy=${1}
  470. while [ ${jy} -lt ${2} ]; do
  471. if [ `lb_is_leap ${jy}` -eq 1 ]; then icpt=`expr ${icpt} + 1`; fi
  472. jy=`expr ${jy} + 1`
  473. done
  474. echo ${icpt}
  475. }
  476. function lb_leap_day()
  477. {
  478. # We need 2 different methods to know the current date:
  479. # The input argument is the file date.file
  480. #
  481. if [ "$1" = "" ]; then echo "USAGE: lb_check_leap <date.file>"; exit; fi
  482. DATF=$1
  483. #
  484. if [ ! -f ${DATF} ]; then echo "There should be a ${DATF} !"; exit; fi
  485. #
  486. dtg1=`paste ${DATF} | cut -c 11-18`
  487. dtg2=`paste ${DATF} | cut -c 20-27`
  488. #
  489. yyyy=`echo ${dtg2} | cut -c1-4`
  490. mmdd1=`echo ${dtg1} | cut -c5-8`
  491. mmdd2=`echo ${dtg2} | cut -c5-8`
  492. #
  493. ileap=`expr ${yyyy} - 2000`; ileap=`expr ${ileap} % 4`
  494. #
  495. # Last DTG done:
  496. export CLDTG=${dtg2}
  497. #
  498. #if [ ! ${ileap} -eq 0 ]; then echo "Not a leap year !"; fi
  499. #
  500. #
  501. # We were at a DTBR=
  502. # a regular year would make a 19900225_19900229 file
  503. # (should be 19900225_19900301 actually but NEMO seems to keep the same month for a file name!)
  504. # so when we restart an experiment for a leap year and the last dtg was "19900220_19900224", we force
  505. # DTBR=6 !
  506. #
  507. export i_leap_day=0
  508. #
  509. if [ ${ileap} -eq 0 -a "${mmdd2}" = "0224" ]; then
  510. #
  511. # Checking if the frequency was 5 days:
  512. if [ ! "${mmdd1}" = "0220" ]; then echo "Problem (lb_leap_day), expecting different DTG"; exit; fi
  513. #
  514. export i_leap_day=1
  515. fi
  516. #
  517. }
  518. function clock2hour()
  519. {
  520. # convert time like hh:mm:ss
  521. # to decimal hour of the day
  522. # ex: 13:30:00 => 13.5
  523. #
  524. # Replacing ":" by " ":
  525. ca=`echo $1 | sed -e s/:/' '/g`
  526. #
  527. hdec=`echo ${ca} | awk '{print $1+($2*60+$3)/3600}'`
  528. echo ${hdec}
  529. }
  530. check_if_file()
  531. {
  532. lspeak=true
  533. cmesg="$2"
  534. if [ "$2" = "s" ]; then
  535. # Silent!
  536. lspeak=false
  537. cmesg="$3"
  538. fi
  539. if [ ! -f $1 ]; then
  540. echo; echo "PROBLEM: file $1 is missing!!!"; echo "${cmesg}"
  541. exit
  542. else
  543. if ${lspeak}; then echo " ... good, file $1 is here..."; echo; fi
  544. fi
  545. }
  546. set_xtics()
  547. {
  548. if [ "${1}" = "" ]; then echo "ERROR (xtics.sh): YEAR_INI is not defined!!!"; exit; fi
  549. if [ "${2}" = "" ]; then echo "ERROR (xtics.sh): YEAR_END is not defined!!!"; exit; fi
  550. #
  551. dy=`expr ${2} - ${1} + 1` ; export YF2=`expr ${2} + 1`
  552. #
  553. export XTICS=1
  554. if [ ${dy} -gt 14 -a ${dy} -le 30 ]; then export XTICS=2; fi
  555. if [ ${dy} -gt 30 -a ${dy} -le 120 ]; then export XTICS=5; fi
  556. if [ ${dy} -gt 120 -a ${dy} -le 200 ]; then export XTICS=10; fi
  557. if [ ${dy} -gt 200 -a ${dy} -le 400 ]; then export XTICS=20; fi
  558. if [ ${dy} -gt 400 -a ${dy} -le 1200 ]; then export XTICS=50; fi
  559. if [ ${dy} -gt 1200 ]; then export XTICS=100; fi
  560. }
  561. epstopng()
  562. {
  563. #
  564. CMD="convert -density 120x120 -quality 100" ; # Transparent background
  565. #CMD="convert -density 120x120 -quality 100 -background white -flatten" ; # White background :
  566. #
  567. if [ "$1" = "" ]; then
  568. list=`\ls *.eps`
  569. else
  570. list="$1"
  571. fi
  572. #
  573. if [ "`which convert`" = "" ]; then
  574. echo; echo "PROBLEM: convert from ImageMagick was not found in your PATH..."
  575. echo; exit
  576. fi
  577. #
  578. for feps in $list; do
  579. fpng=`echo ${feps} | sed -e s/'\.eps'/'\.png'/g`
  580. if [ ! -f ${fpng} ]; then
  581. echo "Creating ${fpng}"
  582. ${CMD} ${feps} ${fpng}
  583. else
  584. echo "${fpng} already exists!"
  585. fi
  586. done
  587. #
  588. }
  589. ipresent_var_in_ncf()
  590. {
  591. ipv=0
  592. ca=`${NCDUMP} -h $1 | grep "${2}(time_counter" | grep float`
  593. if [ ! "${ca}" = "" ]; then
  594. #echo " variable ${2} is present in file $1"
  595. ipv=1
  596. #else
  597. # echo " variable ${2} is NOT present in file $1"
  598. fi
  599. echo "${ipv}"
  600. }
  601. function contains_string()
  602. {
  603. # Tells if string "s" (argument 1) belongs to a list (argument 2)
  604. nbarg="$#" ; length_list=`expr ${nbarg} - 1`
  605. list_all=($*)
  606. s=${list_all[0]} ; # the string
  607. list=${list_all[@]:1:${length_list}} ; # the list
  608. #echo "the string = ${s}"
  609. #echo "the list = ${list_all[@]:1:${length_list}}"
  610. [[ ${list} =~ ${s} ]] && echo "1" || echo "0"
  611. }
  612. sign_image()
  613. {
  614. w_logo_desired=40 ; h_logo_desired=33 ; #h_logo_desired doesn't seem to matter!
  615. flogo=${BARAKUDA_ROOT}/src/html/logo.png
  616. rw=`identify -format "%w" ${1}` ; # rh=`identify -format "%h" ${1}`
  617. rpx=$((${rw}-${w_logo_desired}))
  618. composite -geometry ${w_logo_desired}x${h_logo_desired}+${rpx}+0 ${flogo} ${1} ${2}
  619. }