common_ocean_post.txt 51 KB
Newer Older
#         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}
}