#!/bin/bash 
## g.e. # ./make_met-aquaplanet.bash  met_em.d01.1979-01-01_00\:00\:00.nc SST control 273.15:27.
## g.e. # ./make_met-aquaplanet.bash  met_em.d01.1979-01-01_00\:00\:00.nc SST KEQ 273.15:27.:1.:60.0:15.:15.
## g.e. # ./make_met-aquaplanet.bash  met_em.d01.1979-01-01_00\:00\:00.nc SST HS 315.0:0.0:60.0
if test $1 = '-h'; then
  echo "******************************"
  echo "***  Shell script to make  ***"
  echo "*** an aqua-planet met_em  ***"
  echo "******************************"
  echo "make_met-aquaplanet.bash [met_emfile](met_em file to transform) [SSTname](name of the SST) [sstkind](kind of SST profile from Neale & Hoskins, 2001) [ref:amp/addvalues](amplitude or additional values when required (KEQ, KW1), HS: Held & Suarez, 1994 conditions (uses [ref]:[amp]:[perAmp, as /\Ty])"
  echo "[addvalues]=[reference]:[amp]:[perAmp]:[L0]:[dL]:[dl]"
  echo "  [ref]: value for the non-perturbation"
  echo "  [amp]: anomaly amplitude (in [units])"
  echo "  [perAmp]: perturbation anomaly amplitude (in [units])"
  echo "  [L0]: longitude of maximum anomaly (in deg)"
  echo "  [dL]: decayment in longitude (in deg)"
  echo "  [dl]: decayment in latitude (in deg)"
else
  rootsh=`pwd`
  export PATH=/u/lflmd/bin/gcc_Python-2.7.5/bin:${PATH}
  ncvarhome=${HOME}/UNSW-CCRC-WRF/tools/python
  errormsg='ERROR -- error -- ERROR -- error'

  met_emfile=$1
  sstname=$2
  sstkind=$3
  addvalues=$4

  if test ! ${sstkind} = 'original'; then 
    ofile='aqua_met-em_'${sstkind}'.nc'
  else
    ofile='aqua_met-em.nc'
  fi

  rm ${ofile} > /dev/null

  zerovars='LANDMASK:HGT_M:HGT_U:HGT_V:SLPX:SLPY:CON:VAR:OA1:OA2:OA3:OA4:OL1:OL2:OL3:OL4:SKINTEMP:SEAICE:SNOW'
# Fixing p_top at 0.0001 hPa)
##  zTopvars='PRES:GHT:RH:VV:UU:TT'
  zTopvars='PRES@0.0001'

##  keepvars='XLAT_M:XLONG_M:XLAT_U:XLONG_U:XLAT_V:XLONG_V:CLAT:CLONG:MAPFAC_M:MAPFAC_V:MAPFAC_U:MAPFAC_MX:MAPFAC_VX:MAPFAC_UX:MAPFAC_MY:MAPFAC_VY:MAPFAC_UY:E:F:SINALPHA:COSALPHA'
  keepvars=''
  specificvars='LANDUSEF|0:LU_INDEX|1:SOILTEMP|0:SOILCTOP|0:SCT_DOM|14:SOILCBOT|0:SCB_DOM|14:ALBEDO12M|8:GREENFRAC|0:SNOALB|0:SLOPECAT|0'
#  specificvars=''
  thresvars='SST|273.15|below|275.:'
  dimvars='LANDUSEF|z-dimension0024,15,sumc,1:'

  cp ${met_emfile} ${ofile}

  # zerovars 
  ## 
  ivar=1
  echo "zeroing..."
  zerov=`echo ${zerovars} | tr ':' ' '`
  for var in ${zerov}; do
    echo "  "${var}"..."
    python ${ncvarhome}/nc_var.py -f ${ofile} -v ${var} -o valmod -S mulc,0. 
    ivar=`expr ${ivar} + 1`
##    exit
  done # end of zeroingvars

  # giving values
  ##
  echo "giving values..."
  givv=`echo ${specificvars} | tr ':' ' '`
  for var in ${givv}; do
    varn=`echo ${var} | tr '|' ' ' | awk '{print $1}'`
    varv=`echo ${var} | tr '|' ' ' | awk '{print $2}'`
    echo "  "${varn}"..."
    python ${ncvarhome}/nc_var.py -f ${ofile} -v ${varn} -o valmod -S mulc,0. 
    python ${ncvarhome}/nc_var.py -f ${ofile} -v ${varn} -o valmod -S sumc,${varv} 
    ivar=`expr ${ivar} + 1`
##    exit
  done # end of givingvars

  # threshold values
  ##
  echo "threshold values..."
  givv=`echo ${thresvars} | tr ':' ' '`
  for var in ${givv}; do
    varn=`echo ${var} | tr '|' ' ' | awk '{print $1}'`
    vart=`echo ${var} | tr '|' ' ' | awk '{print $2}'`
    vark=`echo ${var} | tr '|' ' ' | awk '{print $3}'`
    varv=`echo ${var} | tr '|' ' ' | awk '{print $4}'`
    if test ${vark} = 'below'; then
      echo "  "${varn}" values below "${vart}" to "${varv}"..."
      python ${ncvarhome}/nc_var.py -f ${ofile} -v ${varn} -o valmod -S lowthres,${vart},${varv} 
    else
      echo "  "${varn}" values above "${vart}" to "${varv}"..."
      python ${ncvarhome}/nc_var.py -f ${ofile} -v ${varn} -o valmod -S upthres,${vart},${varv}   
    fi
    ivar=`expr ${ivar} + 1`
##    exit
  done # end of thresvars

  # dimension values
  ##
  ivar=1
  echo "dimension values..."
  dimv=`echo ${dimvars} | tr ':' ' '`
  for var in ${dimv}; do
    varn=`echo ${var} | tr '|' ' ' | awk '{print $1}'`
    varv=`echo ${var} | tr '|' ' ' | awk '{print $2}'`
    python ${ncvarhome}/nc_var.py -f ${ofile} -v ${varn} -o valmod_dim -S ${varv}
    ivar=`expr ${ivar} + 1`
##    exit
  done # end of thresvars

# Imposing SST
##
  if test ! ${sstkind} = 'original'; then 
    python sfcVAR_global_modification.py -f ${ofile} -v ${sstname} -k ${sstkind} -L XLONG_M -l XLAT_M -s ${addvalues}
    npy=$?
    if test ! ${npy} -eq 0; then
      echo ${errormsg}
      echo "  Pythhon 'sfcVAR_global_modification.py' does not work!"
      echo ${errormsg}
      exit
    fi
  fi

# Without including extra level
 exit

# Ztop variables
##
  echo "  Adding top variables at p=0 i.." 
  ztopvars=`echo ${zTopvars} | tr ':' ' '`
  for zvar in ${ztopvars}; do
    zvarn=`echo ${zvar} | tr '@' ' ' | awk '{print $1}'`
    zvarv=`echo ${zvar} | tr '@' ' ' | awk '{print $2}'`
    echo "    "${zvar}
    python ${ncvarhome}/nc_var.py -f ${ofile} -v ${zvarn} -o addvals -S '1:last:1:'${zvarv}
  done
  python ${ncvarhome}/nc_var.py -f ${ofile} -o gaddattrk -S 'BOTTOM-TOP_GRID_DIMENSION|39|Inp32'

# Giving the geopotential and temperature at the top (assuming p_top 0.0001 hPa)
  python values_Zlevel.py -f ${ofile} -z -1 -g GHT -H HGT_M -L XLONG_M -l XLAT_M -p PRES -r RH -t TT -P -1,-1

  echo "Aqua-planet like '"${ofile}"' file generated"

fi
