build_clim.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. C2ET="nav_lon,nav_lat,deptht"
  107. C2EU="nav_lon,nav_lat,depthu"
  108. C2EV="nav_lon,nav_lat,depthv"
  109. C2EW="nav_lon,nav_lat,depthw"
  110. GRIDIMP="gridT"
  111. if [ ${ivt} -eq 1 ] || [ ${ibpsi} -eq 1 ] || [ ${icurl} -eq 1 ]; then
  112. GRIDIMP+=" gridU"
  113. fi
  114. if [ ${iamoc} -eq 1 ] || [ ${ivt} -eq 1 ] || [ ${ibpsi} -eq 1 ] || [ ${icurl} -eq 1 ]; then
  115. GRIDIMP+=" gridV"
  116. fi
  117. if [ `contains_string ${FILE_ICE_SUFFIX} ${NEMO_SAVED_FILES}` -eq 1 ]; then
  118. GRIDIMP+=" ${FILE_ICE_SUFFIX}"
  119. fi
  120. if [ `contains_string SBC ${NEMO_SAVED_FILES}` -eq 1 ]; then
  121. GRIDIMP+=" ${FILE_FLX_SUFFIX}"
  122. fi
  123. echo; echo " GRIDIMP = ${GRIDIMP}"; echo
  124. # Checking what files we have / plan to use:
  125. if [ -z "${NEMO_SAVED_FILES}" ]; then
  126. echo "Please specify which NEMO files are saved (file suffixes, gridT, ..., icemod) ?"
  127. echo " => set the variable NEMO_SAVED_FILES in your config_${CONFIG}.sh file!"; exit
  128. fi
  129. VAF=( "gridT" "gridU" "gridV" "${FILE_ICE_SUFFIX}" "${FILE_FLX_SUFFIX}" )
  130. js=0 ; gimp_new=""
  131. for sf in ${VAF[*]}; do
  132. echo "Checking ${sf}..."
  133. ca=`echo "${NEMO_SAVED_FILES} ${NEMO_SAVED_FILES_3D}" | grep ${sf}`
  134. cb=`echo "${GRIDIMP}" | grep ${sf}`
  135. if [ "${ca}" = "" ]; then
  136. if [ "${cb}" != "" ]; then
  137. echo "PROBLEM! The diags you specified say you need ${sf} files"
  138. echo " => but you have not specified ${sf} in NEMO_SAVED_FILES !"; exit
  139. fi
  140. else
  141. gimp_new="${sf} ${gimp_new}"
  142. fi
  143. ((js++))
  144. done
  145. GRIDIMP=${gimp_new}
  146. echo; echo "File types to import: ${GRIDIMP}"; echo; echo
  147. VCM=( "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" )
  148. cd ${TMP_DIR}/
  149. echo; echo "In:"; pwd
  150. barakuda_import_mesh_mask
  151. ls -l ; echo; echo
  152. nby=$((${Y2}-${Y1}+1))
  153. if [ ! $((${nby}%${IFREQ_SAV_YEARS})) -eq 0 ]; then
  154. echo " Number of years should be a multiple of ${IFREQ_SAV_YEARS}!"; exit
  155. fi
  156. if [ ${ece_exp} -gt 0 ] && [ ${ece_exp} -lt 10 ] ; then
  157. 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
  158. fi
  159. ffirsty="${DIAG_D}/first_year.info"
  160. if [ ! -f ${ffirsty} ]; then echo "ERROR: file ${ffirsty} not found!!!"; exit; fi
  161. export YEAR_INI_F=`cat ${ffirsty}`
  162. export jyear=${Y1}
  163. while [ ${jyear} -le ${Y2} ]; do
  164. export cyear=`printf "%04d" ${jyear}` ; echo ; echo "Year = ${cyear}"
  165. cpf=""
  166. if [ ${ece_exp} -gt 0 ]; then
  167. iy=$((${jyear}-${Y1}+1+${Y1}-${YEAR_INI_F}))
  168. dir_ece=`printf "%03d" ${iy}`
  169. echo " *** ${cyear} => dir_ece = ${dir_ece}"
  170. cpf="${dir_ece}/"
  171. fi
  172. i_get_file=0
  173. if [ $((${jyear}%${IFREQ_SAV_YEARS})) -eq 0 ]; then
  174. barakuda_check_year_is_complete ; # lcontinue might be updated to false!
  175. fi
  176. CRT1M=${CPRMN}${cyear}${cmmdd1}_${cyear}${cmmdd2}
  177. barakuda_import_files
  178. # Monthly files to work with for current year:
  179. ft1m=${CRT1M}_gridT.nc
  180. fu1m=${CRT1M}_gridU.nc
  181. fv1m=${CRT1M}_gridV.nc
  182. # Annual files to work with for current year:
  183. CRT1Y=`echo ${CRT1M} | sed -e s/"_${TSTAMP}_"/"_${ANNUAL_3D}_"/g`
  184. ft1y=${CRT1Y}_gridT.nc
  185. fu1y=${CRT1Y}_gridU.nc
  186. fv1y=${CRT1Y}_gridV.nc
  187. fj1y=${CRT1Y}_${FILE_ICE_SUFFIX}.nc ; # can be icemod or gridT ....
  188. CFG3D=${CRT1M}
  189. CPREF3D=${CPRMN}
  190. #
  191. # Files that contain the 3D fields (might be monthly or annaual sometimes (when "${ANNUAL_3D}" = "1y") $
  192. ft3d=${ft1m}
  193. fu3d=${fu1m}
  194. fv3d=${fv1m}
  195. if [ "${ANNUAL_3D}" = "1y" ]; then
  196. [[ ${NEMO_SAVED_FILES_3D} =~ (^|[[:space:]])"gridU"($|[[:space:]]) ]] \
  197. && CPREF3D=${CPRAN}; CFG3D=${CRT1Y}; ft3d=${ft1y}; fu3d=${fu1y}; fv3d=${fv1y} \
  198. || echo "...default"
  199. echo ""
  200. fi
  201. fvt=${CFG3D}_VT.nc
  202. echo
  203. if [ ${ivt} -eq 1 ]; then
  204. # Creating VT files:
  205. echo " *** CALLING: cdfvT.x ${CFG3D} ${NN_T} ${NN_S} ${NN_U} ${NN_V} ${NN_U_EIV} ${NN_V_EIV} &"
  206. cdfvT.x ${CFG3D} ${NN_T} ${NN_S} ${NN_U} ${NN_V} ${NN_U_EIV} ${NN_V_EIV} &
  207. echo
  208. fi
  209. echo
  210. if [ ${iamoc} -eq 1 ]; then
  211. rm -f moc.nc
  212. echo " *** CALLING: cdfmoc.x ${fv3d} ${NN_V} ${NN_V_EIV} &"
  213. cdfmoc.x ${fv3d} ${NN_V} ${NN_V_EIV} &
  214. echo
  215. fi
  216. if [ ${ibpsi} -eq 1 ]; then
  217. rm -f psi.nc
  218. echo " *** CALLING: cdfpsi.x ${fu3d} ${fv3d} ${NN_U} ${NN_V} &"
  219. cdfpsi.x ${fu3d} ${fv3d} ${NN_U} ${NN_V} &
  220. echo
  221. fi
  222. if [ ${icurl} -eq 1 ]; then
  223. # Curl of wind stress
  224. if [ ! "${NN_TAUX}" = "X" ] && [ ! "${NN_TAUY}" = "X" ]; then
  225. rm -f curl.nc
  226. echo " *** CALLING: cdfcurl.x ${fu3d} ${fv3d} ${NN_TAUX} ${NN_TAUY} 1 &"
  227. cdfcurl.x ${fu3d} ${fv3d} ${NN_TAUX} ${NN_TAUY} 1 &
  228. else
  229. echo " You need to define NN_TAUX and NN_TAUY if you want to compute the curl of the wind stress!"
  230. fi
  231. echo
  232. fi
  233. wait
  234. ncwa -O -a x moc.nc ${CFG3D}_MOC.nc ; # removing degenerate x record...
  235. rm -f moc.nc
  236. if [ ${ibpsi} -eq 1 ]; then mv -f psi.nc ${CFG3D}_PSI.nc; fi
  237. if [ ${icurl} -eq 1 ]; then mv -f curl.nc ${CFG3D}_TCURL.nc; fi
  238. echo "After year ${jyear}:"; ls -l *.nc*
  239. echo
  240. ((jyear++))
  241. export jyear=${jyear}
  242. done
  243. echo
  244. echo "Phase 2:"; ls ; echo
  245. # Mean monthly climatology
  246. SUFF_FOR_MONTHLY="${NEMO_SAVED_FILES}"
  247. if [ "${ANNUAL_3D}" = "" ]; then SUFF_FOR_MONTHLY+=" VT MOC PSI TCURL"; fi
  248. echo; echo "Will build monthly clim for files with these suffixes:"; echo ${SUFF_FOR_MONTHLY}; echo
  249. for suff in ${SUFF_FOR_MONTHLY}; do
  250. if [ -f ./${CRT1M}_${suff}.nc ]; then
  251. echo ; echo " Treating ${suff} files!"; echo
  252. f2c=mclim_${CONFEXP}_${CY1}-${CY2}_${suff}.nc
  253. rm -f ${CLIM_DIR}/${f2c}*
  254. echo
  255. jm=0
  256. for cm in ${VCM[*]}; do
  257. ((jm++))
  258. if [ -f ./${CRT1M}_${suff}.nc ]; then
  259. echo; ls ; echo
  260. echo "ncra -F -O -d time_counter,${jm},,12 ${CPRMN}*${cmmdd1}_*${cmmdd2}_${suff}.nc -o mean_m${cm}_${suff}.nc"
  261. ncra -F -O -d time_counter,${jm},,12 ${CPRMN}*${cmmdd1}_*${cmmdd2}_${suff}.nc -o mean_m${cm}_${suff}.nc &
  262. echo
  263. fi
  264. done
  265. wait
  266. echo; ls ; echo
  267. echo "ncrcat -O mean_m*_${suff}.nc out_${suff}.nc"
  268. ncrcat -O mean_m*_${suff}.nc out_${suff}.nc
  269. rm mean_m*_${suff}.nc
  270. echo
  271. echo "mv -f out_${suff}.nc ${f2c}"
  272. mv -f out_${suff}.nc ${f2c}
  273. echo
  274. else
  275. echo ; echo " Ignoring monthly ${suff} files!"; echo
  276. fi
  277. done ; # loop along monthly files suffixes
  278. wait
  279. # Mean annual climatology for 3D-variable-based fields:
  280. SUFF_FOR_ANNUAL="VT MOC PSI TCURL"
  281. if [ ! "${ANNUAL_3D}" = "" ]; then SUFF_FOR_ANNUAL+=" ${NEMO_SAVED_FILES_3D}"; fi
  282. echo; echo "Will build annual clim for files with these suffixes:"; echo ${SUFF_FOR_ANNUAL}; echo
  283. for suff in ${SUFF_FOR_ANNUAL}; do
  284. if [ -f ./${CRT1Y}_${suff}.nc ] || [ -f ./${CRT1M}_${suff}.nc ]; then
  285. echo ; echo " Treating ${suff} files!"; echo
  286. f2c=aclim_${CONFEXP}_${CY1}-${CY2}_${suff}.nc
  287. rm -f ${CLIM_DIR}/${f2c}*
  288. #
  289. echo "ncra -O ${CPREF3D}*_${suff}.nc -o ${f2c} &"
  290. ncra -O ${CPREF3D}*_${suff}.nc -o ${f2c} &
  291. echo
  292. else
  293. echo ; echo " Ignoring annual ${suff} files!"; echo
  294. fi
  295. done ; # loop along annual files suffixes
  296. wait
  297. rm -f ${CPRMN}*${cmmdd1}_*${cmmdd2}_*.nc ${CPRAN}*${cmmdd1}_*${cmmdd2}_*.nc
  298. list=`\ls [am]clim_${CONFEXP}*.nc`
  299. for ff in ${list}; do
  300. fn=`echo ${ff} | sed -e s/'.nc'/'.nc4'/g`
  301. echo "${CP2NC4} ${ff} ${fn} &"
  302. ${CP2NC4} ${ff} ${fn} &
  303. echo
  304. done
  305. wait
  306. for cl in aclim mclim; do
  307. echo "mv -f ${cl}_${CONFEXP}*.nc4 ${CLIM_DIR}/"
  308. mv -f ${cl}_${CONFEXP}*.nc4 ${CLIM_DIR}/
  309. echo
  310. done
  311. echo;echo
  312. echo "${CY1}-${CY2}" > ${CLIM_DIR}/last_clim
  313. echo "Climatology saved into: ${CLIM_DIR}/"
  314. echo;echo
  315. cd /tmp/
  316. rm -rf ${TMP_DIR} 2>/dev/null
  317. exit 0