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