Newer
Older
Virginie Guemas
committed
ncrename -d x_2,x tmpohc.nc
fi
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
if [[ ${lstvars/somxl010} != ${lstvars} ]] ; then
ncks -v somxl010 tmpohc.nc mxl.nc
else
cdfmxl tmpohc.nc mxl.nc
fi
$exec tmpohc.nc $para $up $down > tmp.log
cat tmp.log
thc=`cat tmp.log | grep "Total Heat content :" | awk '{print $5}'`;
uhc=`cat tmp.log | grep "Total Heat content/volume" | awk '{print $5}'`;
sed -e "s/thc =.*/thc = $thc ;/" template_heatc.cdl > template_heatc2.cdl
sed -e "s/uhc =.*/uhc = $uhc ;/" template_heatc2.cdl > template_heatc.cdl
ncgen -o heatc_${jt}.nc template_heatc.cdl
rm -f template_heatc2.cdl tmpohc.nc mxl.nc tmp.log
list=$list" "heatc_${jt}.nc
done
cdo cat $list ${output}$2
ncks -h -A -v time $1 ${output}$2
rm -f $list template_heatc.nc template_heatc.cdl depth.txt
setminmax ${output}$2 thc uhc
}
###############################################################################
# #
# Cut a meridional or zonal section #
# #
# #
# $1 : input file #
# $2 : input var #
# $3 : Z/M (zonal / meridional section) #
# $4 : lat/lon #
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
# #
# Created in September 2012 Author : vguemas@ic3.cat #
# #
###############################################################################
function cutsection {
typeset var ntime=`cdo ntime $1`
typeset var nx=`ncdump -h $1|grep 'x = '|head -1|cut -f3 -d" "`
typeset var ny=`ncdump -h $1|grep 'y = '|head -1|cut -f3 -d" "`
typeset var nz=`ncdump -h $1|grep 'depth'|head -1|cut -f3 -d" "`
cat>section.R<<EOF1
library(ncdf)
# Get latitudes, longitudes, depth, mask
fncm=open.ncdf('mesh_hgr.nc')
lon=get.var.ncdf(fncm,'nav_lon')
lat=get.var.ncdf(fncm,'nav_lat')
close.ncdf(fncm)
fncm=open.ncdf('mesh_zgr.nc')
depth=get.var.ncdf(fncm,'nav_lev')
close.ncdf(fncm)
fncm=open.ncdf('mask.nc')
mask=get.var.ncdf(fncm,switch('$2','vozocrtx'='umask','vomecrty'='vmask','tmask'))
close.ncdf(fncm)
# Latitude / longitude of the zonal / meridional section
exactpos=$4
if ('$3'=='M') {
while (exactpos<min(lon)) {exactpos=exactpos+360}
while (exactpos>max(lon)) {exactpos=exactpos-360}
}
# Collect the indexes defining the section
listi=array(dim=switch('$3','Z'=$nx-2,'M'=$ny-1))
listj=array(dim=switch('$3','Z'=$nx-2,'M'=$ny-1))
for (jpt in 1:length(listi)) {
vect=switch('$3','Z'=lat[jpt,],'M'=lon[,jpt+1])
if (min(abs(vect-exactpos))<(2*360./$nx)) {
pos=sort(abs(vect-exactpos),index.return=T)\$ix[1]
listi[jpt]=switch('$3','Z'=jpt+1,'M'=pos)
listj[jpt]=switch('$3','Z'=pos,'M'=jpt)
}
}
listi=listi[is.na(listi)==F]
listj=listj[is.na(listj)==F]
print(listi)
print(listj)
# Select variable at those indexes
fnc1=open.ncdf('$1')
varout=array(dim=c(length(listi),$nz,$ntime))
for (jt in 1:$ntime) {
varin=get.var.ncdf(fnc1,'$2',start=c(1,1,1,jt),count=c($nx,$ny,$nz,1))
varin[which(mask<0.5)]=1e20
for (jpt in 1:length(listi)) {
varout[jpt,,jt]=varin[listi[jpt],listj[jpt],]
}
}
close.ncdf(fnc1)
# Write the output
wtime=dim.def.ncdf("time","",seq(1,$ntime),unlim=TRUE)
dimout=array(dim=length(listi))
for (jpt in 1:length(listi)) {
dimout[jpt]=switch('$3','Z'=lon[listi[jpt],listj[jpt]],'M'=lat[listi[jpt],listj[jpt]])
}
wsec=switch('$3','Z'=dim.def.ncdf("lon","",dimout),'M'=dim.def.ncdf("lat","",dimout))
wdep=dim.def.ncdf("deptht","",depth)
wvar=var.def.ncdf("$2","",list(wsec,wdep,wtime),1e20)
fnc2=create.ncdf('$5',wvar)
put.var.ncdf(fnc2,wvar,varout)
close.ncdf(fnc2)
EOF1
R CMD BATCH section.R
ncks -h -A -v time $1 $5
}
###############################################################################
# #
# 3-dimensional conservative interpolation to the regular atmospheric grid #
# #
# $1 : input file #
# $2 : input var #
# #
# Created in November 2012 Author : vguemas@ic3.cat #
# #
###############################################################################
function interp3d {
typeset var nz=`ncdump -h $1|grep 'deptht'|head -1|cut -f3 -d" "`
ln -sf /shared/earth/software/scripts/interpolation/scrip_use scrip_use
for lev in $(seq 1 $nz) ; do
ncks -d deptht,$((lev-1)) -v $2 $1 tmp_${lev}.nc
ncwa -O -h -a deptht tmp_${lev}.nc tmp_${lev}.nc
ln -sf /esnas/autosubmit/con_files/weigths/${NEMOVERSION}/rmp_${NEMOVERSION}_to_*_lev${lev}.nc rmp_${NEMOVERSION}_to_regular_lev${lev}.nc
cat > scrip_use_in <<EOF
&remap_inputs
remap_wgt = 'rmp_${NEMOVERSION}_to_regular_lev${lev}.nc'
infile = 'tmp_${lev}.nc'
invertlat = FALSE
var = '$2'
fromregular = FALSE
outfile = 'tmp_$(printf "%02d" $lev).nc'
/
EOF
./scrip_use
ncecat -O -h tmp_$(printf "%02d" $lev).nc tmp_$(printf "%02d" $lev).nc
ncrename -h -O -d record,deptht tmp_$(printf "%02d" $lev).nc tmp_$(printf "%02d" $lev).nc
done
ncrcat -n $nz,2,1 -v $2 tmp_01.nc tmpout.nc
ncpdq -O -h -a time,deptht tmpout.nc tmp2out.nc
ncks -A -v deptht $1 tmp2out.nc
cdo setgrid,t106grid tmp2out.nc tmp3out.nc
orca_res=`echo $NEMOVERSION | cut -c 7-9`
if ((${orca_res} \= "O25")); then
cdo invertlatdata tmp3out.nc $3
else
cp tmp3out.nc $3
fi
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
rm -f tmp_* tmpout.nc tmp2out.nc tmp3out.nc rmp_${NEMOVERSION}_to_regular_lev* scrip_use scrip_use_in
}
###############################################################################
# #
# Set minimum and maximum attributes #
# #
# $1 : input file #
# $2 : input vars provided as a list #
# #
# Created in May 2012 Author : vguemas@ic3.cat #
###############################################################################
function setminmax {
cdo fldmax -timmax $1 intmax.nc
cdo fldmin -timmin $1 intmin.nc
typeset var listvars=""
typeset args=("$@")
typeset var ind
for ((ind=1;ind<$#;ind++)) ; do
listvars=$listvars" "${args[$ind]}
done
typeset name
for name in ${listvars[@]} ; do
typeset var var_max=`ncdump -v $name intmax.nc | tail -2 | head -1 | awk '{print $1}'`
ncatted -h -a valid_max,$name,m,f,$var_max $1
typeset var var_min=`ncdump -v $name intmin.nc | tail -2 | head -1 | awk '{print $1}'`
ncatted -h -a valid_min,$name,m,f,$var_min $1
done
rm -f intmax.nc intmin.nc
}
###############################################################################
# #
# Concatenate with a weight of the results obtained #
# on partial years #
# #
# $1 : input file 1 #
# $2 : input file 2 #
# $3 : ending month file 1 #
# $4 : output file #
# #
# Created in May 2012 Author : vguemas@ic3.cat #
###############################################################################
function concat {
typeset var wgt=$((10#${3}%12))
typeset var ntimes=`cdo ntime $1`
ncks -O -d time,$(($ntimes-1)) $1 part1.nc
ncks -O -d time,0 $2 part2.nc
cdo mulc,$wgt part1.nc part1_bis.nc
cdo mulc,$((12-$wgt)) part2.nc part2_bis.nc
cdo add part1_bis.nc part2_bis.nc tmp2_bis.nc
cdo divc,12 tmp2_bis.nc tmp2.nc
lstdims=`ncdump -h tmp2.nc | awk /dimensions:/,/variables:/ | grep -v dimensions: | grep -v variables: | awk '{print $1}'`
if [[ ${lstdims/lev} != ${lstdims} ]] ; then
ncrename -d lev,ensemble tmp2.nc
else
ncecat -O -h tmp2.nc tmp2.nc
ncrename -h -O -d record,ensemble tmp2.nc tmp2.nc
ncpdq -O -h -a time,ensemble tmp2.nc tmp2.nc
fi
rm -f part1.nc part2.nc part1_bis.nc part2_bis.nc tmp2_bis.nc
ncks -O -d time,0,$((ntimes-2+10#${3}/12)) $1 tmp1.nc
ncks -O -d time,1, $2 tmp3.nc
ncrcat -O tmp1.nc tmp2.nc tmp3.nc $4
rm -f tmp1.nc tmp2.nc tmp3.nc
}
###############################################################################
# #
# Gather the members in a single netcdf file #
# #
# $1 : prefix netcdf file name for all the members #
# $2 : suffix netcdf file name for all the members #
# $3 : output file name #
# #
# Created in May 2012 Author : vguemas@ic3.cat #
###############################################################################
function gather_memb {
typeset var lstfiles1=`ls ${1}*${2}`
typeset var file
typeset var ind=1
typeset var lstfiles2=""
for file in ${lstfiles1[@]} ; do
typeset var lstvars=`cdo showvar $file`
typeset var lstexclude="nav_lon nav_lat"
for varex in $lstexclude ; do
if [[ ${lstvars/${varex}/} != ${lstvars} ]] ; then
ncks -O -x -v ${varex} $file $file
fi
done
if [[ ! -z "`ncdump -h ${file} | grep ensemble | head -1 | awk '{print $3}' | grep [0-9]`" ]] ; then
typeset var nrec=`ncdump -h ${file} | grep ensemble | head -1 | awk '{print $3}' | grep [0-9]`
typeset var jrec
for jrec in $(seq 1 $nrec) ; do
ncks -d ensemble,$((jrec-1)) $file tmp_record_${ind}.nc
ncwa -O -a ensemble tmp_record_${ind}.nc tmp_record_${ind}.nc
lstfiles2=${lstfiles2}" "tmp_record_${ind}.nc
ind=$((ind+1))
done
else
lstfiles2=${lstfiles2}" "$file
fi
done
ncecat -h ${lstfiles2} ${3}
rm -f tmp_record_*.nc
ncrename -h -O -d record,ensemble ${3} ${3}
ncpdq -O -h -a time,ensemble ${3} ${3}
}
Eleftheria Exarchou
committed
###############################################################################
# Choose vertical level in ocean, or vertically average between
# 2 or more ocean levels #
# #
# $1 : input file name #
# $2: input variable name (votemper or vosaline) #
# $3 : upper depth of the layer (in level number) #
# $4 : lower depth of the layer (in level number. If same as the #
# upper layer, then the script just selects this layer. If its #
# different, a vertical integral (weighted) between upper and lower #
# level is computed #
# $5 : output file name (=> 2D field ) #
# #
# Created in February 2012 Author : vguemas@ic3.cat #
# Modified (more generic,
# i.e. for any input var) in December 2014 #
# Author : eleftheria.exarchou@ic3.cat #
###############################################################################
Eleftheria Exarchou
committed
function vertmeanvar {
typeset var ntime=`cdo ntime $1`
typeset var list=""
typeset var jt
for jt in $(seq 1 $ntime); do
ncks -d time,$((jt-1)) $1 a1
Eleftheria Exarchou
committed
# The oras4 data do not have gdepth data in their mask, but only gdept_0, so:
if grep -q nemovar_s4 $1 ; then
l1=$(( $3 % 6 ))
l2=$(( $4 % 6 ))
ll1=$(( $3 / 6 + 1 ))
ll2=$(( $4 / 6 + 1 ))
lev1=`echo $(cdo output -selvar,gdept_0 mesh_zgr.nc | sed -n ${ll1}p | awk '{ print $'$l1' }')`
lev2=`echo $(cdo output -selvar,gdept_0 mesh_zgr.nc | sed -n ${ll2}p | awk '{ print $'$l2' }')`
else
Eleftheria Exarchou
committed
l1=$(($3+1))
l2=$(($4+1))
lev1=`echo $(cdo info -seltimestep,1 -selvar,gdept mesh_zgr.nc | sed -n ${l1}p | awk '{ print $10 }')`
lev2=`echo $(cdo info -seltimestep,1 -selvar,gdept mesh_zgr.nc | sed -n ${l2}p | awk '{ print $10 }')`
Eleftheria Exarchou
committed
fi
cdfvertmean a1 $2 T $lev1 $lev2
Eleftheria Exarchou
committed
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
rm -f a1
ncrename -O -v sovertmean,vertmean -d time_counter,time -v time_counter,time vertmean.nc
mv vertmean.nc outputvertmean_$jt.nc
list=$list" "outputvertmean_$jt.nc
rm -f intvertmean.nc a?
# #test on cdo version: if >1.5.6, remove valid_min/max attributes to avoid values out of that range to be replaced by NaN
ncatted -O -a valid_max,vertmean,d,, outputvertmean_$jt.nc outputvertmean_$jt.nc
ncatted -O -a valid_min,vertmean,d,, outputvertmean_$jt.nc outputvertmean_$jt.nc
ncatted -O -a standard_name,time,a,c,time outputvertmean_$jt.nc outputvertmean_$jt.nc
ncatted -O -a units,time,o,c,'seconds since 1993-05-01 00:00:00' outputvertmean_$jt.nc outputvertmean_$jt.nc
ncatted -O -a long_name,time,a,c,'Time axis' outputvertmean_$jt.nc outputvertmean_$jt.nc
done
cdo cat $list $5
ncks -A -v time $1 $5
rm -f $list
setminmax $5 vertmean
ncrename -v vertmean,$2 $5
#typeset var level=`echo $(cdo info -selvar,$2 -setctomiss,0 $5 | sed -n 2p | awk '{ print $7 }')`
#typeset var lev=`echo $(cdo info -seltimestep,1 -selvar,$2 -setctomiss,0 $1 | grep $level | awk '{ print $1 }')`
lev=$3
echo $lev
cp $5 tmp_${lev}.nc
## Here we interpolate horizontally onto a regular grid
ln -sf /esnas/autosubmit/con_files/weigths/${NEMOVERSION}/rmp_${NEMOVERSION}_to_*_lev${lev}.nc rmp_${NEMOVERSION}_to_regular_lev${lev}.nc
ln -sf /shared/earth/software/scripts/interpolation/scrip_use scrip_use
Eleftheria Exarchou
committed
cat > scrip_use_in <<EOF
&remap_inputs
remap_wgt = 'rmp_${NEMOVERSION}_to_regular_lev${lev}.nc'
infile = 'tmp_${lev}.nc'
invertlat = FALSE
var = '$2'
fromregular = FALSE
outfile = 'tmp_${lev}_int.nc'
/
EOF
./scrip_use
cdo setgrid,t106grid tmp_${lev}_int.nc tmpout1.nc
cdo invertlatdata tmpout1.nc $5
ncatted -O -a long_name,${2},o,c,'vertical average of temp/sal between '${lev1}'m and '${lev2}'m' $5
rm -f tmp_* tmpout* rmp_${NEMOVERSION}_to_regular_lev* scrip_use scrip_use_in
}