common_ocean_post.txt 58 KB
Newer Older
  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                                                              #
# $3 : output file         ( => 3D )                                          #
#                                                                             #
#         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  /cfu/pub/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 /cfu/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
  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}
}
###############################################################################
#          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         #
###############################################################################
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
 # 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
  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 }')`
  cdfvertmean a1  $2 T $lev1 $lev2
  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 /cfu/autosubmit/con_files/weigths/${NEMOVERSION}/rmp_${NEMOVERSION}_to_*_lev${lev}.nc rmp_${NEMOVERSION}_to_regular_lev${lev}.nc
    ln -sf  /cfu/pub/scripts/interpolation/scrip_use scrip_use
    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
}