build_clim.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #!/usr/bin/env bash
  2. #==============================================================
  3. #
  4. # B A R A K U D A
  5. #
  6. # An OCEAN MONITORING python environment for NEMO
  7. #
  8. # L. Brodeau, 2009-2017
  9. #
  10. #===============================================================
  11. ivt=1 ; # Create a climatology for VT
  12. iamoc=1 ; # Create a climatology for 2D lat-depth AMOC?
  13. ibpsi=1 ; # Create a climatology for barotropic stream function
  14. icurl=1 ; # Create a climatology of the windstress curl
  15. export script=build_clim
  16. [ -z ${BARAKUDA_ROOT+x} ] && export BARAKUDA_ROOT=${PWD}
  17. # Display available configs:
  18. list_conf="`\ls ${BARAKUDA_ROOT}/configs/config_*.sh | sed -e "s|${BARAKUDA_ROOT}/configs\/config_||g" -e s/'.sh'/''/g`"
  19. # User configs, potentially in the directory from which barakuda.sh is called:
  20. list_conf+=" `\ls ./config_*.sh 2>/dev/null | sed -e "s|.\/config_||g" -e s/'.sh'/''/g`"
  21. # Important bash functions:
  22. . ${BARAKUDA_ROOT}/src/bash/bash_functions.bash
  23. barakuda_init
  24. while getopts R:f:i:e:C:h option ; do
  25. case $option in
  26. R) export EXP=${OPTARG};;
  27. f) export IFREQ_SAV_YEARS=${OPTARG} ;;
  28. i) export Y1=${OPTARG} ;;
  29. e) export Y2=${OPTARG} ;;
  30. C) export CONFIG=${OPTARG};;
  31. h) build_clim_usage ; exit ;;
  32. \?) build_clim_usage ; exit ;;
  33. esac
  34. done
  35. barakuda_check
  36. if [ -f ./config_${CONFIG}.sh ]; then
  37. # sourcing local configuration file if present:
  38. fconfig=./config_${CONFIG}.sh
  39. else
  40. # sourcing barakuda-distribution configuration file:
  41. fconfig=${BARAKUDA_ROOT}/configs/config_${CONFIG}.sh
  42. fi
  43. if [ -f ${fconfig} ]; then
  44. echo "Sourcing configuration file: ${fconfig} !"
  45. . ${fconfig}
  46. else
  47. echo "PROBLEM: cannot find file ${fconfig} !"; exit
  48. fi
  49. echo
  50. # If auto-submit experiment (ece_exp=10) then overides a few functions with:
  51. if [ ${ece_exp} -ge 10 ]; then
  52. . ${BARAKUDA_ROOT}/src/bash/bash_functions_autosub.bash
  53. fi
  54. # If 3D fieds are annual averaged then overides a few functions with:
  55. if [ ! "${ANNUAL_3D}" = "" ]; then
  56. . ${BARAKUDA_ROOT}/src/bash/bash_functions_1y.bash
  57. fi
  58. barakuda_setup
  59. CPRMN="${CPREF}"
  60. CPRAN=""
  61. if [ ! "${ANNUAL_3D}" = "" ]; then
  62. CPRAN=`echo ${CPREF} | sed -e s/"_${TSTAMP}_"/"_${ANNUAL_3D}_"/g`
  63. fi
  64. echo
  65. echo " SETTINGS: "
  66. echo " *** DIAG_D = ${DIAG_D} "
  67. echo " *** CLIM_DIR = ${CLIM_DIR} "
  68. echo " *** TMP_DIR = ${TMP_DIR} "
  69. echo " *** Y1 = ${Y1} "
  70. echo " *** Y2 = ${Y2} "
  71. echo " *** CONFIG = ${CONFIG} "
  72. echo " *** GRID = ${ORCA} "
  73. echo " *** CONFEXP = ${CONFEXP} "
  74. echo " *** EXP = ${EXP} "
  75. echo " *** CPREF = ${CPREF} "
  76. echo " *** Monthly files prefix = ${CPRMN} (${NEMO_SAVED_FILES})"
  77. if [ ! "${CPRAN}" = "" ]; then
  78. echo " *** Annual files prefix = ${CPRAN} (${NEMO_SAVED_FILES_3D})"
  79. fi
  80. echo " *** IFREQ_SAV_YEARS = ${IFREQ_SAV_YEARS} "
  81. echo " *** NCDF_DIR = ${NCDF_DIR} "
  82. echo
  83. mkdir -p ${DIAG_D}
  84. barakuda_first_last_years ; # look at NEMO files to know what are first and last years available...
  85. echo ${IFREQ_SAV_YEARS} > ${DIAG_D}/numb_year_per_file.info
  86. echo ${YEAR_INI} > ${DIAG_D}/first_year.info
  87. # How to convert from nc3 to nc4?
  88. wc=`which nccopy`
  89. if [ "${wc}" = "" ]; then
  90. CP2NC4="${NCDF_DIR}/bin/nccopy -k 4 -d 9"
  91. else
  92. CP2NC4="nccopy -k 4 -d 9"
  93. fi
  94. wc=`which nc3tonc4` ; # Comes with Canopy...
  95. if [ ! "${wc}" = "" ]; then
  96. CP2NC4="nc3tonc4"
  97. fi
  98. export PATH=${BARAKUDA_ROOT}/cdftools_light/bin:${PYTHON_HOME}/bin:${PATH}
  99. Y1=$((${Y1}+0))
  100. Y2=$((${Y2}+0))
  101. CY1=`printf "%04d" ${Y1}`
  102. CY2=`printf "%04d" ${Y2}`
  103. mkdir -p ${CLIM_DIR}
  104. # Variables to extract:
  105. V2E="${NN_SST},${NN_SSS},${NN_SSH},${NN_T},${NN_S},${NN_MLD}"
  106. C2E="nav_lon,nav_lat,olevel"
  107. C2ET="nav_lon,nav_lat,deptht"
  108. C2EU="nav_lon,nav_lat,depthu"
  109. C2EV="nav_lon,nav_lat,depthv"
  110. C2EW="nav_lon,nav_lat,depthw"
  111. GRID_IMP="grid_T"
  112. if [ ${ivt} -eq 1 ] || [ ${ibpsi} -eq 1 ] || [ ${icurl} -eq 1 ]; then
  113. GRID_IMP+=" grid_U"
  114. fi
  115. if [ ${iamoc} -eq 1 ] || [ ${ivt} -eq 1 ] || [ ${ibpsi} -eq 1 ] || [ ${icurl} -eq 1 ]; then
  116. GRID_IMP+=" grid_V"
  117. fi
  118. if [ `contains_string ${FILE_ICE_SUFFIX} ${NEMO_SAVED_FILES}` -eq 1 ]; then
  119. GRID_IMP+=" ${FILE_ICE_SUFFIX}"
  120. fi
  121. if [ `contains_string SBC ${NEMO_SAVED_FILES}` -eq 1 ]; then
  122. GRID_IMP+=" SBC"
  123. fi
  124. echo; echo " GRID_IMP = ${GRID_IMP}"; echo
  125. # Checking what files we have / plan to use:
  126. if [ -z "${NEMO_SAVED_FILES}" ]; then
  127. echo "Please specify which NEMO files are saved (file suffixes, grid_T, ..., icemod) ?"
  128. echo " => set the variable NEMO_SAVED_FILES in your config_${CONFIG}.sh file!"; exit
  129. fi
  130. VAF=( "grid_T" "grid_U" "grid_V" "icemod" "SBC" )
  131. js=0 ; gimp_new=""
  132. for sf in ${VAF[*]}; do
  133. echo "Checking ${sf}..."
  134. ca=`echo "${NEMO_SAVED_FILES} ${NEMO_SAVED_FILES_3D}" | grep ${sf}`
  135. cb=`echo "${GRID_IMP}" | grep ${sf}`
  136. if [ "${ca}" = "" ]; then
  137. if [ "${cb}" != "" ]; then
  138. echo "PROBLEM! The diags you specified say you need ${sf} files"
  139. echo " => but you have not specified ${sf} in NEMO_SAVED_FILES !"; exit
  140. fi
  141. else
  142. gimp_new="${sf} ${gimp_new}"
  143. fi
  144. ((js++))
  145. done
  146. GRID_IMP=${gimp_new}
  147. echo; echo "File types to import: ${GRID_IMP}"; echo; echo
  148. VCM=( "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" )
  149. cd ${TMP_DIR}/
  150. echo; echo "In:"; pwd
  151. barakuda_import_mesh_mask
  152. ls -l ; echo; echo
  153. nby=$((${Y2}-${Y1}+1))
  154. if [ ! $((${nby}%${IFREQ_SAV_YEARS})) -eq 0 ]; then
  155. echo " Number of years should be a multiple of ${IFREQ_SAV_YEARS}!"; exit
  156. fi
  157. if [ ${ece_exp} -gt 0 ] && [ ${ece_exp} -lt 10 ] ; then
  158. if [ ! -d ${NEMO_OUT_D}/001 ]; then echo "ERROR: since ece_exp=${ece_exp}, there should be a directory 001 in:"; echo " ${NEMO_OUT_D}"; fi
  159. fi
  160. ffirsty="${DIAG_D}/first_year.info"
  161. if [ ! -f ${ffirsty} ]; then echo "ERROR: file ${ffirsty} not found!!!"; exit; fi
  162. export YEAR_INI_F=`cat ${ffirsty}`
  163. export jyear=${Y1}
  164. while [ ${jyear} -le ${Y2} ]; do
  165. export cyear=`printf "%04d" ${jyear}` ; echo ; echo "Year = ${cyear}"
  166. cpf=""
  167. if [ ${ece_exp} -gt 0 ]; then
  168. iy=$((${jyear}-${Y1}+1+${Y1}-${YEAR_INI_F}))
  169. dir_ece=`printf "%03d" ${iy}`
  170. echo " *** ${cyear} => dir_ece = ${dir_ece}"
  171. cpf="${dir_ece}/"
  172. fi
  173. i_get_file=0
  174. if [ $((${jyear}%${IFREQ_SAV_YEARS})) -eq 0 ]; then
  175. barakuda_check_year_is_complete ; # lcontinue might be updated to false!
  176. fi
  177. CRT1M=${CPRMN}${cyear}${cmmdd1}_${cyear}${cmmdd2}
  178. barakuda_import_files
  179. # Monthly files to work with for current year:
  180. ft1m=${CRT1M}_grid_T.nc
  181. fu1m=${CRT1M}_grid_U.nc
  182. fv1m=${CRT1M}_grid_V.nc
  183. # Annual files to work with for current year:
  184. CRT1Y=`echo ${CRT1M} | sed -e s/"_${TSTAMP}_"/"_${ANNUAL_3D}_"/g`
  185. ft1y=${CRT1Y}_grid_T.nc
  186. fu1y=${CRT1Y}_grid_U.nc
  187. fv1y=${CRT1Y}_grid_V.nc
  188. fj1y=${CRT1Y}_${FILE_ICE_SUFFIX}.nc ; # can be icemod or grid_T ....
  189. CFG3D=${CRT1M}
  190. CPREF3D=${CPRMN}
  191. #
  192. # Files that contain the 3D fields (might be monthly or annaual sometimes (when "${ANNUAL_3D}" = "1y") $
  193. ft3d=${ft1m}
  194. fu3d=${fu1m}
  195. fv3d=${fv1m}
  196. if [ "${ANNUAL_3D}" = "1y" ]; then
  197. [[ ${NEMO_SAVED_FILES_3D} =~ (^|[[:space:]])"grid_U"($|[[:space:]]) ]] \
  198. && CPREF3D=${CPRAN}; CFG3D=${CRT1Y}; ft3d=${ft1y}; fu3d=${fu1y}; fv3d=${fv1y} \
  199. || echo "...default"
  200. echo ""
  201. fi
  202. fvt=${CFG3D}_VT.nc
  203. echo
  204. if [ ${ivt} -eq 1 ]; then
  205. # Creating VT files:
  206. echo " *** CALLING: cdfvT.x ${CFG3D} ${NN_T} ${NN_S} ${NN_U} ${NN_V} ${NN_U_EIV} ${NN_V_EIV} &"
  207. cdfvT.x ${CFG3D} ${NN_T} ${NN_S} ${NN_U} ${NN_V} ${NN_U_EIV} ${NN_V_EIV} &
  208. echo
  209. fi
  210. echo
  211. if [ ${iamoc} -eq 1 ]; then
  212. rm -f moc.nc
  213. echo " *** CALLING: cdfmoc.x ${fv3d} ${NN_V} ${NN_V_EIV} &"
  214. cdfmoc.x ${fv3d} ${NN_V} ${NN_V_EIV} &
  215. echo
  216. fi
  217. if [ ${ibpsi} -eq 1 ]; then
  218. rm -f psi.nc
  219. echo " *** CALLING: cdfpsi.x ${fu3d} ${fv3d} ${NN_U} ${NN_V} &"
  220. cdfpsi.x ${fu3d} ${fv3d} ${NN_U} ${NN_V} &
  221. echo
  222. fi
  223. if [ ${icurl} -eq 1 ]; then
  224. # Curl of wind stress
  225. if [ ! "${NN_TAUX}" = "X" ] && [ ! "${NN_TAUY}" = "X" ]; then
  226. rm -f curl.nc
  227. echo " *** CALLING: cdfcurl.x ${fu3d} ${fv3d} ${NN_TAUX} ${NN_TAUY} 1 &"
  228. cdfcurl.x ${fu3d} ${fv3d} ${NN_TAUX} ${NN_TAUY} 1 &
  229. else
  230. echo " You need to define NN_TAUX and NN_TAUY if you want to compute the curl of the wind stress!"
  231. fi
  232. echo
  233. fi
  234. wait
  235. ncwa -O -a x moc.nc ${CFG3D}_MOC.nc ; # removing degenerate x record...
  236. rm -f moc.nc
  237. if [ ${ibpsi} -eq 1 ]; then mv -f psi.nc ${CFG3D}_PSI.nc; fi
  238. if [ ${icurl} -eq 1 ]; then mv -f curl.nc ${CFG3D}_TCURL.nc; fi
  239. echo "After year ${jyear}:"; ls -l *.nc*
  240. echo
  241. ((jyear++))
  242. export jyear=${jyear}
  243. done
  244. echo
  245. echo "Phase 2:"; ls ; echo
  246. # Mean monthly climatology
  247. SUFF_FOR_MONTHLY="${NEMO_SAVED_FILES}"
  248. if [ "${ANNUAL_3D}" = "" ]; then SUFF_FOR_MONTHLY+=" VT MOC PSI TCURL"; fi
  249. echo; echo "Will build monthly clim for files with these suffixes:"; echo ${SUFF_FOR_MONTHLY}; echo
  250. for suff in ${SUFF_FOR_MONTHLY}; do
  251. if [ -f ./${CRT1M}_${suff}.nc ]; then
  252. echo ; echo " Treating ${suff} files!"; echo
  253. f2c=mclim_${CONFEXP}_${CY1}-${CY2}_${suff}.nc
  254. rm -f ${CLIM_DIR}/${f2c}*
  255. echo
  256. jm=0
  257. for cm in ${VCM[*]}; do
  258. ((jm++))
  259. if [ -f ./${CRT1M}_${suff}.nc ]; then
  260. echo; ls ; echo
  261. echo "ncra -F -O -d time_counter,${jm},,12 ${CPRMN}*${cmmdd1}_*${cmmdd2}_${suff}.nc -o mean_m${cm}_${suff}.nc"
  262. ncra -F -O -d time_counter,${jm},,12 ${CPRMN}*${cmmdd1}_*${cmmdd2}_${suff}.nc -o mean_m${cm}_${suff}.nc &
  263. echo
  264. fi
  265. done
  266. wait
  267. echo; ls ; echo
  268. echo "ncrcat -O mean_m*_${suff}.nc out_${suff}.nc"
  269. ncrcat -O mean_m*_${suff}.nc out_${suff}.nc
  270. rm mean_m*_${suff}.nc
  271. echo
  272. echo "mv -f out_${suff}.nc ${f2c}"
  273. mv -f out_${suff}.nc ${f2c}
  274. echo
  275. else
  276. echo ; echo " Ignoring monthly ${suff} files!"; echo
  277. fi
  278. done ; # loop along monthly files suffixes
  279. wait
  280. # Mean annual climatology for 3D-variable-based fields:
  281. SUFF_FOR_ANNUAL="VT MOC PSI TCURL"
  282. if [ ! "${ANNUAL_3D}" = "" ]; then SUFF_FOR_ANNUAL+=" ${NEMO_SAVED_FILES_3D}"; fi
  283. echo; echo "Will build annual clim for files with these suffixes:"; echo ${SUFF_FOR_ANNUAL}; echo
  284. for suff in ${SUFF_FOR_ANNUAL}; do
  285. if [ -f ./${CRT1Y}_${suff}.nc ] || [ -f ./${CRT1M}_${suff}.nc ]; then
  286. echo ; echo " Treating ${suff} files!"; echo
  287. f2c=aclim_${CONFEXP}_${CY1}-${CY2}_${suff}.nc
  288. rm -f ${CLIM_DIR}/${f2c}*
  289. #
  290. echo "ncra -O ${CPREF3D}*_${suff}.nc -o ${f2c} &"
  291. ncra -O ${CPREF3D}*_${suff}.nc -o ${f2c} &
  292. echo
  293. else
  294. echo ; echo " Ignoring annual ${suff} files!"; echo
  295. fi
  296. done ; # loop along annual files suffixes
  297. wait
  298. rm -f ${CPRMN}*${cmmdd1}_*${cmmdd2}_*.nc ${CPRAN}*${cmmdd1}_*${cmmdd2}_*.nc
  299. list=`\ls [am]clim_${CONFEXP}*.nc`
  300. for ff in ${list}; do
  301. fn=`echo ${ff} | sed -e s/'.nc'/'.nc4'/g`
  302. echo "${CP2NC4} ${ff} ${fn} &"
  303. ${CP2NC4} ${ff} ${fn} &
  304. echo
  305. done
  306. wait
  307. for cl in aclim mclim; do
  308. echo "mv -f ${cl}_${CONFEXP}*.nc4 ${CLIM_DIR}/"
  309. mv -f ${cl}_${CONFEXP}*.nc4 ${CLIM_DIR}/
  310. echo
  311. done
  312. echo;echo
  313. echo "${CY1}-${CY2}" > ${CLIM_DIR}/last_clim
  314. echo "Climatology saved into: ${CLIM_DIR}/"
  315. echo;echo
  316. cd /tmp/
  317. rm -rf ${TMP_DIR} 2>/dev/null
  318. exit 0