#!/bin/bash
#### Script to get data from a given specific sounding and surface stations from WRF
##
## e.g. # get_WRFsounding-surface_data.bash true ./ control 87576,-58.52,-34.81 87576,-58.52,-34.81 /home/lluis/PY/wrfout_d01_1995-01-01_00:00:00 WRFt:WRFtda:WRFp:WRFua:WRFva:ws:WRFwd wds:wss:WRFtdas T2
if test $1 = '-h'; then
  echo "************************************"
  echo "*** Script to get snd & sfc data ***"
  echo "***        from WRF output       ***"
  echo "************************************"
  echo "get_WRFsounding-surface_data.bash [scratch] [odir] [expl]                    \
    [sndid],[sndlon],[sndlat] [sfcid],[sfclon],[sfclat] [infilen]                    \
    [wrfsnddiags],[wrfsfcdiags],[wrfsfcnodiags],[wrfcdxdiags],[wrfcdxnodiags]"
else
errmsg='ERROR -- error -- ERROR -- error'

if test $1 = 'true'; then fscratch=true
else fscratch=false; fi

odir=$2
expl=$3
if test ! $4 = 'None'; then
  sndid=`echo $4 | tr ',' ' ' | awk '{print $1}'`
  sndlon=`echo $4 | tr ',' ' ' | awk '{print $2}'`
  sndlat=`echo $4 | tr ',' ' ' | awk '{print $3}'`
else
  sndid='None'
fi

if test ! $5 = 'None'; then
  sfcid=`echo $5 | tr ',' ' ' | awk '{print $1}'`
  sfclon=`echo $5 | tr ',' ' ' | awk '{print $2}'`
  sfclat=`echo $5 | tr ',' ' ' | awk '{print $3}'`
else
  sfcid='None'
fi

infilen=$6

if test ! $7 = 'None'; then
  wrfsnddiags=`echo $7 | tr ',' ' ' | awk '{print $1}'`
  wrfsfcdiags=`echo $7 | tr ',' ' ' | awk '{print $2}'`
  wrfsfcnodiags=`echo $7 | tr ',' ' ' | awk '{print $3}'`
  wrfcdxdiags=`echo $7 | tr ',' ' ' | awk '{print $4}'`
  wrfcdxnodiags=`echo $7 | tr ',' ' ' | awk '{print $5}'`
fi

function isin_list() {
# Function to check whether a value is in a list
  list=$1
  value=$2
  
  is=`echo ${list} | tr ':' '\n' | awk '{print "@"$1"@"}' | grep '@'${value}'@' | wc -w`
  if test ${is} -eq 1
  then
    true
  else
    false
  fi
}

function get_WRFsounding_data(){
# Function to get the data to compute analysis in a sounding point
#   stlon= longitude value of the station
#   stlat= latitude value of the station
#   filen= name of the file
#   lonvarn= name of the longitude variable
#   latvarn= name of the latitude variable
#   snddiagnames= diagnostics to compute (as ':' separated list)

  funcvals=$1

  stlon=`echo ${funcvals} | tr '#' ' ' | awk '{print $1}'`
  stlat=`echo ${funcvals} | tr '#' ' ' | awk '{print $2}'`
  filen=`echo ${funcvals} | tr '#' ' ' | awk '{print $3}'`
  lonvarn=`echo ${funcvals} | tr '#' ' ' | awk '{print $4}'`
  latvarn=`echo ${funcvals} | tr '#' ' ' | awk '{print $5}'`
  snddiagnames=`echo ${funcvals} | tr '#' ' ' | awk '{print $6}'`

  vals=${lonvarn}':'${latvarn}':Time|0'
  gridsndv=`python ${pyHOME}/nc_var.py -o get_point -f ${filen} -S ${vals}           \
    -v ${stlon},${stlat} | tr ' ' '!'`
  echo "* get grid point: "${stlon}", "${stlat} >> ${ofileins}
  echo python ${pyHOME}/nc_var.py -o get_point -f ${filen} -S "'"${vals}"'"          \
    -v ${stlon},${stlat} >> ${ofileins}
  echo " " >> ${ofileins}

  xsnd=`echo ${gridsndv} | tr '!' ' ' | awk '{print $1}' | tr ',' ' ' |              \
   awk '{print $1}'`
  ysnd=`echo ${gridsndv} | tr '!' ' ' | awk '{print $1}' | tr ',' ' ' |              \
  awk '{print $2}'`
  dsnd=`echo ${gridsndv} | tr '!' ' ' | awk '{print $2}'`

  xsnd1=`expr ${xsnd} + 1`
  ysnd1=`expr ${ysnd} + 1`

  # Getting values
  if ${fscratch}; then rm ${simsndptf} >& /dev/null; fi
  if test ! -f ${simsndptf}; then
    values='Time,0,-1,1@bottom_top,0,-1,1@south_north,'${ysnd}','${ysnd}',1@'
    values=${values}'west_east,'${xsnd}','${xsnd}',1@south_north_stag,'${ysnd}','
    values=${values}${ysnd1}',1@west_east_stag,'${xsnd}','${xsnd1}',1:False'
    python ${pyHOME}/nc_var.py -o DataSetSection_multidims -f ${filen}               \
      -S ${values} -v all
    if test $? -ne 0; then
      echo ${errmsg}
      echo "  python failed!!"
      echo python ${pyHOME}/nc_var.py -o DataSetSection_multidims -f ${filen}        \
        -S "'"${values}"'" -v all
      exit
    fi
    echo "* get values at snd point: "${stlon}", "${stlat} >> ${ofileins}
    echo python ${pyHOME}/nc_var.py -o DataSetSection_multidims -f ${filen}          \
      -S "'"${values}"'" -v all >> ${ofileins}
    echo " " >> ${ofileins}

    ofilen='newfile_multidims.nc'
    mv ${ofilen} ${simsndptf}
  fi
    
  # Computing sounding diagnostics
  snddiagd='Time@WRFtime,bottom_top@P'
  diagns=`echo ${snddiagnames} | tr ':' ' '`

  idiag=1
  for diagn in ${diagns}; do
    diagv=`cat $pyHOME/diagnostics.inf | grep ${diagn}',' | tr ',' ' ' |             \
      awk '{print $2"|"$3}'`
    if test ${idiag} -eq 1; then
      snddiagvals=${diagv}
    else
      snddiagvals=${snddiagvals}','${diagv}
    fi
    idiag=`expr ${idiag} + 1`
  done

  if ${fscratch}; then rm ${simsnddiagsf}; fi
  if test ! -f ${simsnddiagsf}; then
    python ${pyHOME}/diagnostics.py -f ${simsndptf} -d ${snddiagd} -v ${snddiagvals}
    if test $? -ne 0; then
      echo ${errmsg}
      echo "  python failed!!"
      echo python ${pyHOME}/diagnostics.py -f ${simsndptf} -d "'"${snddiagd}"'" -v   \
        "'"${snddiagvals}"'"
      exit
    fi
    echo "* computing diagnostics at snd point: "${stlon}", "${stlat} >> ${ofileins}
    echo python ${pyHOME}/diagnostics.py -f "'"${simsndptf}"'" -d "'"${snddiagd}"'"  \
      -v "'"${snddiagvals}"'" >> ${ofileins}
    echo " " >> ${ofileins}

    mv diagnostics.nc ${simsnddiagsf}
    if $(isin_list ${snddiagnames} WRFt); then
      #To deg
      python ${pyHOME}/nc_var.py -o valmod -S subc,273.15 -f ${simsnddiagsf} -v ta
    fi
    if $(isin_list ${snddiagnames} WRFp); then
      #To Pa
      python ${pyHOME}/nc_var.py -o valmod -S divc,100. -f ${simsnddiagsf} -v pres
    fi
  fi
}

function get_WRFsurface_data(){
# Function to get the data to compute analysis in a surface point
#   stlon= longitude value of the station
#   stlat= latitude value of the station
#   filen= name of the file
#   lonvarn= name of the longitude variable
#   latvarn= name of the latitude variable
#   sfcdiagnames= diagnostics to compute (as ':' separated list)
#   nondiagvs= ':' list of variables which are not from a diagnostics ('None' for 
#     without)

  funcvals=$1

  stlon=`echo ${funcvals} | tr '#' ' ' | awk '{print $1}'`
  stlat=`echo ${funcvals} | tr '#' ' ' | awk '{print $2}'`
  filen=`echo ${funcvals} | tr '#' ' ' | awk '{print $3}'`
  lonvarn=`echo ${funcvals} | tr '#' ' ' | awk '{print $4}'`
  latvarn=`echo ${funcvals} | tr '#' ' ' | awk '{print $5}'`
  sfcdiagnames=`echo ${funcvals} | tr '#' ' ' | awk '{print $6}'`
  nondiagvs=`echo ${funcvals} | tr '#' ' ' | awk '{print $7}'`

  vals=${lonvarn}':'${latvarn}':Time|0'
  gridsndv=`python ${pyHOME}/nc_var.py -o get_point -f ${filen} -S ${vals}           \
    -v ${stlon},${stlat} | tr ' ' '!'`
  echo "* get grid point: "${stlon}", "${stlat} >> ${ofileins}
  echo python ${pyHOME}/nc_var.py -o get_point -f ${filen} -S "'"${vals}"'"          \
    -v ${stlon},${stlat} >> ${ofileins}
  echo " " >> ${ofileins}
  xsnd=`echo ${gridsndv} | tr '!' ' ' | awk '{print $1}' | tr ',' ' ' |              \
   awk '{print $1}'`
  ysnd=`echo ${gridsndv} | tr '!' ' ' | awk '{print $1}' | tr ',' ' ' |              \
  awk '{print $2}'`
  dsnd=`echo ${gridsndv} | tr '!' ' ' | awk '{print $2}'`

  xsnd1=`expr ${xsnd} + 1`
  ysnd1=`expr ${ysnd} + 1`

  # Getting values
  if ${fscratch}; then rm ${simsfcptf} >& /dev/null; fi
  if test ! -f ${simsfcptf}; then
    values='Time,0,-1,1@south_north,'${ysnd}','${ysnd}',1@'
    values=${values}'west_east,'${xsnd}','${xsnd}',1@south_north_stag,'${ysnd}','
    values=${values}${ysnd1}',1@west_east_stag,'${xsnd}','${xsnd1}',1:false'
    python ${pyHOME}/nc_var.py -o DataSetSection_multidims -f ${filen}               \
      -S ${values} -v all
    if test $? -ne 0; then
      echo ${errmsg}
      echo "  python failed!!"
      echo python ${pyHOME}/nc_var.py -o DataSetSection_multidims -f ${filen}        \
        -S "'"${values}"'" -v all
      exit
    fi
  echo "* get values at sfc point: "${stlon}", "${stlat} >> ${ofileins}
  echo python ${pyHOME}/nc_var.py -o DataSetSection_multidims -f ${filen}            \
      -S "'"${values}"'" -v all >> ${ofileins}
  echo " " >> ${ofileins}

    ofilen='newfile_multidims.nc'
    mv ${ofilen} ${simsfcptf}
  fi
    
  # Computing sfc diagnostics
  sfcdiagd='Time@WRFtime'
  diagns=`echo ${sfcdiagnames} | tr ':' ' '`

  idiag=1
  for diagn in ${diagns}; do
    diagv=`cat $pyHOME/diagnostics.inf | grep WRF | grep ${diagn}',' | tr ',' ' ' |  \
      awk '{print $2"|"$3}'`
    if test ${idiag} -eq 1; then
      sfcdiagvals=${diagv}
    else
      sfcdiagvals=${sfcdiagvals}','${diagv}
    fi
    idiag=`expr ${idiag} + 1`
  done

  if ${fscratch}; then rm ${simsfcdiagsf}; fi
  if test ! -f ${simsfcdiagsf}; then
    python ${pyHOME}/diagnostics.py -f ${simsfcptf} -d ${sfcdiagd} -v ${sfcdiagvals}
    if test $? -ne 0; then
      echo ${errmsg}
      echo "  python failed!!"
      echo python ${pyHOME}/diagnostics.py -f ${simsfcptf} -d "'"${sfcdiagd}"'"      \
        -v "'"${sfcdiagvals}"'"
      exit
    fi
    echo "* diagnostics at sfc point: "${stlon}", "${stlat} >> ${ofileins}
    echo python ${pyHOME}/diagnostics.py -f ${simsfcptf} -d "'"${sfcdiagd}"'"        \
      -v "'"${sfcdiagvals}"'" >> ${ofileins}
    echo " " >> ${ofileins}
    mv diagnostics.nc ${simsfcdiagsf}
    if $(isin_list ${sfcdiagnames} wds); then
      #To rad
      python ${pyHOME}/nc_var.py -o valmod -S mulc,0.0174532925199 -f ${simsfcdiagsf}\
        -v wds
      python ${pyHOME}/nc_var.py -o varaddattr -S 'units|rad' -f ${simsfcdiagsf} -v wds
    fi
  fi

  #Adding non-diagnostic variables
  if test ! ${nondiagvs} = 'None'; then
    varsadd=`echo ${nondiagvs} | tr ':' ' '`
    for vadd in ${varsadd}; do
      python ${pyHOME}/nc_var.py -o fvaradd -S ${simsfcptf},${vadd} -f ${simsfcdiagsf}
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python ${pyHOME}/nc_var.py -o fvaradd -S "'"${simsfcptf},${vadd}"'"     \
          -f ${simsfcdiagsf}
        exit
        rm ${simsfcdiagsf}
      fi
      echo "* adding variable '"${vadd}"' at sfc point: "${stlon}", "${stlat} >> ${ofileins}
      echo python ${pyHOME}/nc_var.py -o fvaradd -S "'"${simsfcptf},${vadd}"'"       \
        -f ${simsfcdiagsf} >> ${ofileins}
      echo " " >> ${ofileins}
      # CF varname
      CFvarn=`python $pyHOME/generic.py -o variables_values -S ${vadd} | tr ':' ' ' |\
        awk '{print $1}'`
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python $pyHOME/generic.py -o variables_values -S ${vadd}
        exit
        rm ${simsfcdiagsf}
      fi
      python $pyHOME/nc_var.py -o chvarname -S ${CFvarn} -f ${simsfcdiagsf} -v ${vadd}
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python ${pyHOME}/nc_var.py -o chvarname -S ${CFvarn} -f ${simsfcdiagsf} \
          -v ${vadd}
        exit
        rm ${simsfcdiagsf}
      fi
    done
  fi
}

function get_WRFsfc2D_data(){
# Function to get the data to compute analysis in a surface map
#   filen= name of the file
#   sfcdiagnames= diagnostics to compute (as ':' separated list)
#   nondiagvs= ':' list of variables which are not from a diagnostics ('None' for 
#     without)

  funcvals=$1

  filen=`echo ${funcvals} | tr '#' ' ' | awk '{print $1}'`
  sfcdiagnames=`echo ${funcvals} | tr '#' ' ' | awk '{print $2}'`
  nondiagvs=`echo ${funcvals} | tr '#' ' ' | awk '{print $3}'`

  # Computing sfc diagnostics
  if test ! ${sfcdiagnames} = 'None'; then
    sfcmapdiagd='Time@WRFtime:south_north@XLAT:west_east@XLONG'
    diagns=`echo ${sfcdiagnames} | tr ':' ' '`

    # getting that variables only required for the diagnostics
    vardiags=''
    idiag=1
    for diagn in ${diagns}; do
      diagv=`cat $pyHOME/diagnostics.inf | grep WRF | grep ${diagn}',' | tr ',' ' ' |  \
        awk '{print $2"|"$3}'`
      if test ${idiag} -eq 1; then
        sfcdiagvals=${diagv}
        vardiags=`echo ${diagv} | tr '|' ' ' | awk '{print $2}' | tr '@' ':'`
      else
        sfcdiagvals=${sfcdiagvals}','${diagv}
        vardiag=`echo ${diagv} | tr '|' ' ' | awk '{print $2}' | tr '@' ' '`
        for vd in ${vardiag}; do
          if ! $(isin_list ${vardiags} ${vd}); then vardiags=${vardiags}':'${vd}; fi
        done
      fi
      idiag=`expr ${idiag} + 1`
    done

    # Inlcuding non-diagnostics
    if test ! ${nondiagvs} = 'None'; then
      nvns=`echo ${nondiagvs} | tr ':' ' '`
      for vn in ${nvns}; do
        if ! $(isin_list ${vardiags} ${vn}); then vardiags=${vardiags}':'${vn}; fi
      done
    fi
  else
    vardiags='Times:XLONG:XLAT:'${nondiagvs}
  fi

  vardiag=`echo ${vardiags} | tr ':' ','`
  
  # Getting values
  if ${fscratch}; then rm ${simsfcmapf} >& /dev/null; fi
  if test ! -f ${simsfcmapf}; then
    values='Time,0,-1,1@south_north,0,-1,1@west_east,0,-1,1@south_north_stag,0,-1,1@'
    values=${values}'west_east_stag,0,-1,1:False'
    python ${pyHOME}/nc_var.py -o DataSetSection_multidims -f ${filen}               \
      -S ${values} -v ${vardiag}
    if test $? -ne 0; then
      echo ${errmsg}
      echo "  python failed!!"
      echo python ${pyHOME}/nc_var.py -o DataSetSection_multidims -f ${filen}        \
        -S "'"${values}"'"  -v ${vardiag}
      exit
    fi
    echo "* get values for 2D map diagnostics: " >> ${ofileins}
    echo python ${pyHOME}/nc_var.py -o DataSetSection_multidims -f ${filen}          \
      -S "'"${values}"'" -v ${vardiag} >> ${ofileins}
    echo " " >> ${ofileins}

    ofilen='newfile_multidims.nc'
    mv ${ofilen} ${simsfcmapf}
  fi

  if ${fscratch}; then rm ${simsfcmapdiagsf}; fi
    if test ! ${sfcdiagnames} = 'None'; then
      if test ! -f ${simsfcmapdiagsf}; then
      python ${pyHOME}/diagnostics.py -f ${simsfcmapf} -d ${sfcmapdiagd}             \
        -v ${sfcdiagvals}
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python ${pyHOME}/diagnostics.py -f ${simsfcmapf} -d "'"${sfcmapdiagd}"'"\
          -v "'"${sfcdiagvals}"'"
        exit
      fi
      echo "* diagnostics for sfc 2D map: " >> ${ofileins}
      echo python ${pyHOME}/diagnostics.py -f ${simsfcmapf} -d "'"${sfcmapdiagd}"'"  \
        -v "'"${sfcdiagvals}"'" >> ${ofileins}
      echo " " >> ${ofileins}
      mv diagnostics.nc ${simsfcmapdiagsf}
      if $(isin_list ${sfcdiagnames} wds); then
        #To rad
        python ${pyHOME}/nc_var.py -o valmod -S mulc,0.0174532925199                 \
          -f ${simsfcmapdiagsf} -v wds
        python ${pyHOME}/nc_var.py -o varaddattr -S 'units|rad' -f ${simsfcmapdiagsf}\
          -v wds
      fi
    fi
    # If there was not diagnostics
    if test ! -f ${simsfcmapdiagsf}; then
      dddv='Time@Times'
      selvars='XLONG,XLAT,Times'
      python ${pyHOME}/nc_var.py -o selvar -S ${dddv} -f ${simsfcmapf} -v ${selvars}
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python ${pyHOME}/nc_var.py -o selvar -S "'"${ddv}"'" -f ${simsfmapf}    \
          -v ${vars}
        exit
        rm ${simsfcmapf}
      fi
      echo "* creation of diagnostics file for 2D map:" >>  ${ofileins}
      echo python ${pyHOME}/nc_var.py -o selvar -S "'"${dddv}"'" -f ${simsfcmapf}    \
          -v ${selvars} >> ${ofileins}
      echo " " >> ${ofileins}
      mv selvar_new.nc ${simsfcmapdiagsf}
      python ${pyHOME}/nc_var.py -o WRF_CFtime_creation -S 19491201000000,minutes    \
        -f ${simsfcmapdiagsf} -v time
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python ${pyHOME}/nc_var.py -o WRF_CFtime_creation -S 19491201000000,minutes \
        -f ${simsfcmapdiagsf} -v time
        exit
        rm ${simsfcmapdiagsf}
      fi
      echo "* creation of CF-time variable for file at sfc point: "${stlon}", "      \
        ${stlat} >> ${ofileins}
      echo python ${pyHOME}/nc_var.py -o WRF_CFtime_creation -S 19491201000000,minutes \
        -f ${simsfcmapdiagsf} -v time >> ${ofileins}
      echo " " >> ${ofileins}
    fi 
    #Adding non-diagnostic variables
    if test ! ${nondiagvs} = 'None'; then
      varsadd=`echo ${nondiagvs} | tr ':' ' '`
      for vadd in ${varsadd}; do
        python ${pyHOME}/nc_var.py -o fvaradd -S ${simsfcmapf},${vadd}               \
          -f ${simsfcmapdiagsf}
        if test $? -ne 0; then
          echo ${errmsg}
          echo "  python failed!!"
          echo python ${pyHOME}/nc_var.py -o fvaradd -S "'"${simsfcmapf},${vadd}"'"  \
            -f ${simsfcmapdiagsf}
          exit
          rm ${simsfcmapdiagsf}
        fi
        echo "* adding variable '"${vadd}"' at sfc map: " >> ${ofileins}
        echo python ${pyHOME}/nc_var.py -o fvaradd -S "'"${simsfcmapf},${vadd}"'"    \
          -f ${simsfcmapdiagsf} >> ${ofileins}
        echo " " >> ${ofileins}
        # CF varname
        CFvarn=`python $pyHOME/generic.py -o variables_values -S ${vadd} |           \
          tr ':' ' ' | awk '{print $1}'`
        if test $? -ne 0; then
          echo ${errmsg}
          echo "  python failed!!"
          echo python $pyHOME/generic.py -o variables_values -S ${vadd}
          exit
          rm ${simsfcmapdiagsf}
        fi
        python $pyHOME/nc_var.py -o chvarname -S ${CFvarn} -f ${simsfcmapdiagsf}     \
          -v ${vadd}
        if test $? -ne 0; then
          echo ${errmsg}
          echo "  python failed!!"
          echo python ${pyHOME}/nc_var.py -o chvarname -S ${CFvarn}                  \
            -f ${simsfcmapdiagsf} -v ${vadd}
          exit
          rm ${simsfcmapdiagsf}
        fi
      done
    fi
  fi
}

function get_WRFCDXsurface_data(){
# Function to get the WRFCDX data to compute analysis in a surface point
#   stlon= longitude value of the station
#   stlat= latitude value of the station
#   filen= name of the file
#   lonvarn= name of the longitude variable
#   latvarn= name of the latitude variable
#   sfcdiagnames= diagnostics to compute (as ':' separated list)
#   nondiagvs= ':' list of variables which are not from a diagnostics ('None' for 
#     without)

  funcvals=$1

  stlon=`echo ${funcvals} | tr '#' ' ' | awk '{print $1}'`
  stlat=`echo ${funcvals} | tr '#' ' ' | awk '{print $2}'`
  filen=`echo ${funcvals} | tr '#' ' ' | awk '{print $3}'`
  lonvarn=`echo ${funcvals} | tr '#' ' ' | awk '{print $4}'`
  latvarn=`echo ${funcvals} | tr '#' ' ' | awk '{print $5}'`
  sfcdiagnames=`echo ${funcvals} | tr '#' ' ' | awk '{print $6}'`
  nondiagvs=`echo ${funcvals} | tr '#' ' ' | awk '{print $7}'`

  vals=${lonvarn}':'${latvarn}':Time|0'
  gridsndv=`python ${pyHOME}/nc_var.py -o get_point -f ${filen} -S ${vals}           \
    -v ${stlon},${stlat} | tr ' ' '!'`
  echo "* get grid point: "${stlon}", "${stlat} >> ${ofileins}
  echo python ${pyHOME}/nc_var.py -o get_point -f ${filen} -S "'"${vals}"'"          \
    -v ${stlon},${stlat} >> ${ofileins}
  echo " " >> ${ofileins}
  xsnd=`echo ${gridsndv} | tr '!' ' ' | awk '{print $1}' | tr ',' ' ' |              \
   awk '{print $1}'`
  ysnd=`echo ${gridsndv} | tr '!' ' ' | awk '{print $1}' | tr ',' ' ' |              \
  awk '{print $2}'`
  dsnd=`echo ${gridsndv} | tr '!' ' ' | awk '{print $2}'`

  xsnd1=`expr ${xsnd} + 1`
  ysnd1=`expr ${ysnd} + 1`

  # Getting values
  if ${fscratch}; then rm ${simsfcptf} >& /dev/null; fi
  if test ! -f ${simsfcptf}; then
    values='Time,0,-1,1@south_north,'${ysnd}','${ysnd}',1@'
    values=${values}'west_east,'${xsnd}','${xsnd}',1:False'
    python ${pyHOME}/nc_var.py -o DataSetSection_multidims -f ${filen}               \
      -S ${values} -v all
    if test $? -ne 0; then
      echo ${errmsg}
      echo "  python failed!!"
      echo python ${pyHOME}/nc_var.py -o DataSetSection_multidims -f ${filen}        \
        -S "'"${values}"'" -v all
      exit
    fi
  echo "* get values at sfc point: "${stlon}", "${stlat} >> ${ofileins}
  echo python ${pyHOME}/nc_var.py -o DataSetSection_multidims -f ${filen}            \
      -S "'"${values}"'" -v all >> ${ofileins}
  echo " " >> ${ofileins}

    ofilen='newfile_multidims.nc'
    mv ${ofilen} ${simsfcptf}
  fi
    
  # Computing sfc diagnostics
  if test ! ${sfcdiagnames} = 'None'; then
    sfcdiagd='Time@WRFtime'
    diagns=`echo ${sfcdiagnames} | tr ':' ' '`

    idiag=1
    for diagn in ${diagns}; do
      diagv=`cat $pyHOME/diagnostics.inf | grep WRF | grep ${diagn}',' | tr ',' ' ' |\
        awk '{print $2"|"$3}'`
      if test ${idiag} -eq 1; then
        sfcdiagvals=${diagv}
      else
        sfcdiagvals=${sfcdiagvals}','${diagv}
      fi
      idiag=`expr ${idiag} + 1`
    done

    if ${fscratch}; then rm ${simsfcdiagsf}; fi
    if test ! -f ${simsfcdiagsf}; then
      python ${pyHOME}/diagnostics.py -f ${simsfcptf} -d ${sfcdiagd} -v ${sfcdiagvals}
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python ${pyHOME}/diagnostics.py -f ${simsfcptf} -d "'"${sfcdiagd}"'"    \
          -v "'"${sfcdiagvals}"'"
        exit
      fi
      echo "* diagnostics at sfc point: "${stlon}", "${stlat} >> ${ofileins}
      echo python ${pyHOME}/diagnostics.py -f ${simsfcptf} -d "'"${sfcdiagd}"'"      \
        -v "'"${sfcdiagvals}"'" >> ${ofileins}
      echo " " >> ${ofileins}
      mv diagnostics.nc ${simsfcdiagsf}
    fi
  fi

  #Adding non-diagnostic variables
  if test ! ${nondiagvs} = 'None'; then
    # If there was not diagnostics
    if test ! -f ${simsfcdiagsf}; then
      dddv='Time@Times'
      selvars='Times'
      python ${pyHOME}/nc_var.py -o selvar -S ${dddv} -f ${simsfcptf} -v ${selvars}
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python ${pyHOME}/nc_var.py -o selvar -S "'"${dddv}"'" -f ${simsfcptf}   \
          -v ${vars}
        exit
        rm ${simsfcptf}
      fi
      echo "* creation of diagnostics file at sfc point: "${stlon}", "${stlat} >>    \
        ${ofileins}
      echo python ${pyHOME}/nc_var.py -o selvar -S "'"${dddv}"'" -f ${simsfcptf}     \
          -v ${selvars} >> ${ofileins}
      echo " " >> ${ofileins}
      mv selvar_new.nc ${simsfcdiagsf}
      python ${pyHOME}/nc_var.py -o WRF_CFtime_creation -S 19491201000000,minutes    \
        -f ${simsfcdiagsf} -v time
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python ${pyHOME}/nc_var.py -o WRF_CFtime_creation -S 19491201000000,minutes \
        -f ${simsfcdiagsf} -v time
        exit
        rm ${simsfcdiagsf}
      fi
      echo "* creation of CF-time variable for file at sfc point: "${stlon}", "      \
        ${stlat} >> ${ofileins}
      echo python ${pyHOME}/nc_var.py -o WRF_CFtime_creation -S 19491201000000,minutes \
        -f ${simsfcdiagsf} -v time >> ${ofileins}
      echo " " >> ${ofileins}
    fi 
    varsadd=`echo ${nondiagvs} | tr ':' ' '`
    for vadd in ${varsadd}; do
      python ${pyHOME}/nc_var.py -o fvaradd -S ${simsfcptf},${vadd} -f ${simsfcdiagsf}
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python ${pyHOME}/nc_var.py -o fvaradd -S "'"${simsfcptf},${vadd}"'"     \
          -f ${simsfcdiagsf}
        exit
        rm ${simsfcdiagsf}
      fi
      echo "* adding variable '"${vadd}"' at sfc point: "${stlon}", "${stlat} >> ${ofileins}
      echo python ${pyHOME}/nc_var.py -o fvaradd -S "'"${simsfcptf},${vadd}"'"       \
        -f ${simsfcdiagsf} >> ${ofileins}
      echo " " >> ${ofileins}
      # CF varname
      CFvarn=`python $pyHOME/generic.py -o variables_values -S ${vadd} | tr ':' ' ' |\
        awk '{print $1}'`
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python $pyHOME/generic.py -o variables_values -S ${vadd}
        exit
        rm ${simsfcdiagsf}
      fi
      python $pyHOME/nc_var.py -o chvarname -S ${CFvarn} -f ${simsfcdiagsf} -v ${vadd}
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python ${pyHOME}/nc_var.py -o chvarname -S ${CFvarn} -f ${simsfcdiagsf} \
          -v ${vadd}
        exit
        rm ${simsfcdiagsf}
      fi
    done
  fi
}

function get_WRFCDXsfc2D_data(){
# Function to get the data from WRFCDX to compute analysis in a surface map
#   filen= name of the file
#   sfcdiagnames= diagnostics to compute (as ':' separated list)
#   nondiagvs= ':' list of variables which are not from a diagnostics ('None' for 
#     without)
#   sfcmapdiagd= name of the dimensions and dimension variables to use for diagnostics

  funcvals=$1

  filen=`echo ${funcvals} | tr '#' ' ' | awk '{print $1}'`
  sfcdiagnames=`echo ${funcvals} | tr '#' ' ' | awk '{print $2}'`
  nondiagvs=`echo ${funcvals} | tr '#' ' ' | awk '{print $3}'`
  sfcmapdiagd=`echo ${funcvals} | tr '#' ' ' | awk '{print $4}'`

  # Computing sfc diagnostics
  if test ! ${sfcdiagnames} = 'None'; then
    diagns=`echo ${sfcdiagnames} | tr ':' ' '`

    # getting that variables only required for the diagnostics
    vardiags=''
    idiag=1
    for diagn in ${diagns}; do
      diagv=`cat $pyHOME/diagnostics.inf | grep WRF | grep ${diagn}',' | tr ',' ' ' |  \
        awk '{print $2"|"$3}'`
      if test ${idiag} -eq 1; then
        sfcdiagvals=${diagv}
        vardiags=`echo ${diagv} | tr '|' ' ' | awk '{print $2}' | tr '@' ':'`
      else
        sfcdiagvals=${sfcdiagvals}','${diagv}
        vardiag=`echo ${diagv} | tr '|' ' ' | awk '{print $2}' | tr '@' ' '`
        for vd in ${vardiag}; do
          if ! $(isin_list ${vardiags} ${vd}); then vardiags=${vardiags}':'${vd}; fi
        done
      fi
      idiag=`expr ${idiag} + 1`
    done

      # Inlcuding non-diagnostics
      if test ! ${nondiagvs} = 'None'; then
        nvns=`echo ${nondiagvs} | tr ':' ' '`
        for vn in ${nvns}; do
          if ! $(isin_list ${vardiags} ${vn}); then vardiags=${vardiags}':'${vn}; fi
      done
      if ! $(isin_list ${vardiags} 'time'); then vardiags=${vardiags}':time'; fi
    fi
  else
    vardiags='LON:LAT:Times:'${nondiagvs}
  fi
  vardiag=`echo ${vardiags} | tr ':' ','`
  
  # Getting values
  if ${fscratch}; then rm ${simsfcmapf} >& /dev/null; fi
  if test ! -f ${simsfcmapf}; then
    values='Time,0,-1,1@south_north,0,-1,1@west_east,0,-1,1:False'
    python ${pyHOME}/nc_var.py -o DataSetSection_multidims -f ${filen}               \
      -S ${values} -v ${vardiag}
    if test $? -ne 0; then
      echo ${errmsg}
      echo "  python failed!!"
      echo python ${pyHOME}/nc_var.py -o DataSetSection_multidims -f ${filen}        \
        -S "'"${values}"'"  -v ${vardiag}
      exit
    fi
    echo "* get values for 2D map diagnostics: " >> ${ofileins}
    echo python ${pyHOME}/nc_var.py -o DataSetSection_multidims -f ${filen}          \
      -S "'"${values}"'" -v ${vardiag} >> ${ofileins}
    echo " " >> ${ofileins}

    ofilen='newfile_multidims.nc'
    mv ${ofilen} ${simsfcmapf}
  fi

  if ${fscratch}; then rm ${simsfcmapdiagsf}; fi
  if test ! -f ${simsfcmapdiagsf}; then
    if test ! ${sfcdiagnames} = 'None'; then  
      python ${pyHOME}/diagnostics.py -f ${simsfcmapf} -d ${sfcmapdiagd}             \
        -v ${sfcdiagvals}
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python ${pyHOME}/diagnostics.py -f ${simsfcmapf} -d "'"${sfcmapdiagd}"'"\
          -v "'"${sfcdiagvals}"'"
        exit
      fi
      echo "* diagnostics for sfc 2D map: " >> ${ofileins}
      echo python ${pyHOME}/diagnostics.py -f ${simsfcmapf} -d "'"${sfcmapdiagd}"'"  \
        -v "'"${sfcdiagvals}"'" >> ${ofileins}
      echo " " >> ${ofileins}
      mv diagnostics.nc ${simsfcmapdiagsf}
    fi
    # If there was not diagnostics
    if test ! -f ${simsfcmapdiagsf}; then
      dddv='Time@Times'
      selvars='LON,LAT,Times'
      python ${pyHOME}/nc_var.py -o selvar -S ${dddv} -f ${simsfcmapf} -v ${selvars}
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python ${pyHOME}/nc_var.py -o selvar -S "'"${ddv}"'" -f ${simsfmapf}    \
          -v ${vars}
        exit
        rm ${simsfcmapf}
      fi
      echo "* creation of diagnostics file for 2D map:" >>  ${ofileins}
      echo python ${pyHOME}/nc_var.py -o selvar -S "'"${dddv}"'" -f ${simsfcmapf}    \
          -v ${selvars} >> ${ofileins}
      echo " " >> ${ofileins}
      mv selvar_new.nc ${simsfcmapdiagsf}
      python ${pyHOME}/nc_var.py -o WRF_CFtime_creation -S 19491201000000,minutes    \
        -f ${simsfcmapdiagsf} -v time
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python ${pyHOME}/nc_var.py -o WRF_CFtime_creation -S 19491201000000,minutes \
        -f ${simsfcmapdiagsf} -v time
        exit
        rm ${simsfcmapdiagsf}
      fi
      echo "* creation of CF-time variable for file at sfc point: "${stlon}", "      \
        ${stlat} >> ${ofileins}
      echo python ${pyHOME}/nc_var.py -o WRF_CFtime_creation -S 19491201000000,minutes \
        -f ${simsfcmapdiagsf} -v time >> ${ofileins}
      echo " " >> ${ofileins}
    fi 

    #Adding non-diagnostic variables
    if test ! ${nondiagvs} = 'None'; then
      varsadd=`echo ${nondiagvs} | tr ':' ' '`
      for vadd in ${varsadd}; do
        python ${pyHOME}/nc_var.py -o fvaradd -S ${simsfcmapf},${vadd}               \
          -f ${simsfcmapdiagsf}
        if test $? -ne 0; then
          echo ${errmsg}
          echo "  python failed!!"
          echo python ${pyHOME}/nc_var.py -o fvaradd -S "'"${simsfcmapf},${vadd}"'"  \
            -f ${simsfcmapdiagsf}
          exit
          rm ${simsfcmapdiagsf}
        fi
        echo "* adding variable '"${vadd}"' at sfc map: " >> ${ofileins}
        echo python ${pyHOME}/nc_var.py -o fvaradd -S "'"${simsfcmapf},${vadd}"'"    \
          -f ${simsfcmapdiagsf} >> ${ofileins}
        echo " " >> ${ofileins}
        # CF varname
        CFvarn=`python $pyHOME/generic.py -o variables_values -S ${vadd} |           \
          tr ':' ' ' | awk '{print $1}'`
        if test $? -ne 0; then
          echo ${errmsg}
          echo "  python failed!!"
          echo python $pyHOME/generic.py -o variables_values -S ${vadd}
          exit
          rm ${simsfcmapdiagsf}
        fi
        python $pyHOME/nc_var.py -o chvarname -S ${CFvarn} -f ${simsfcmapdiagsf}     \
          -v ${vadd}
        if test $? -ne 0; then
          echo ${errmsg}
          echo "  python failed!!"
          echo python ${pyHOME}/nc_var.py -o chvarname -S ${CFvarn}                  \
            -f ${simsfcmapdiagsf} -v ${vadd}
          exit
          rm ${simsfcmapdiagsf}
        fi
      done
    fi
  fi
}


#######    #######
## MAIN
    #######

  rootsh=`pwd`

  ofileins=${rootsh}'/allins_functions.inf'

  if ${fscratch}; then
    echo "All instructions _______ " > ${ofileins}
    echo "******* ****** ***** **** *** ** *" >> ${ofileins}
    echo " " >> ${ofileins}
  fi

  fileN=`basename ${infilen}`
  fdate=`echo ${fileN} | tr '_' ' ' | awk '{print $3"_"$4}'`

  # Sounding
  if test ! ${sndid} = 'None' && test ! ${wrfsnddiags} = 'None'; then
    stid=${sndid}
    simsndptf=${odir}'/simout_vars_sndpt_'${stid}'_'${expl}'_'${fdate}'.nc'
    simsnddiagsf=${odir}'/simout_snddiags_'${stid}'_'${expl}'_'${fdate}'.nc'

    fvals=${sndlon}'#'${sndlat}'#'${infilen}'#XLONG#XLAT#'${wrfsnddiags}
    get_WRFsounding_data ${fvals}
  fi

  # Surface
  if test ! ${sfcid} = 'None'  && test ! ${wrfsfcdiags} = 'None' ||                  \
    test ! ${wrfsfcnodiags} = 'None'; then
    stid=${sfcid}
    simsfcptf=${odir}'/simout_vars_sfcpt_'${stid}'_'${expl}'_'${fdate}'.nc'
    simsfcdiagsf=${odir}'/simout_sfcdiags_'${stid}'_'${expl}'_'${fdate}'.nc'
  
    fvals=${sfclon}'#'${sfclat}'#'${infilen}'#XLONG#XLAT#'${wrfsfcdiags}'#'${wrfsfcnodiags}
    get_WRFsurface_data ${fvals}
  fi

  # Surface maps
  if test ! ${wrfsfcdiags} = 'None' || test ! ${wrfsfcnodiags} = 'None'; then
    simsfcmapf=${odir}'/simout_vars_sfcmap_'${expl}'_'${fdate}'.nc'
    simsfcmapdiagsf=${odir}'/simout_sfcmapdiags_'${expl}'_'${fdate}'.nc'

    fvals=${infilen}'#'${wrfsfcdiags}'#'${wrfsfcnodiags}
    get_WRFsfc2D_data ${fvals}
  fi

  # cdx snd
  if test ! ${sndid} = 'None'; then
    if test ! ${wrfcdxdiags} = 'None' || test ! ${wrfcdxnodiags} = 'None'; then
      stid=${sndid}
      simsfcptf=${odir}'/simcdx_vars_sndpt_'${stid}'_'${expl}'_'${fdate}'.nc'
      simsfcdiagsf=${odir}'/simcdx_snddiags_'${stid}'_'${expl}'_'${fdate}'.nc'
  
      fvals=${sndlon}'#'${sndlat}'#'${infilen}'#LON#LAT#'${wrfcdxdiags}'#'
      fvals=${fvals}${wrfcdxnodiags}
      get_WRFCDXsurface_data ${fvals}
    fi
  fi

  # cdx Surface
  if test ! ${sfcid} = 'None'; then
   if test ! ${wrfcdxdiags} = 'None' || test ! ${wrfcdxnodiags} = 'None'; then
      stid=${sfcid}
      simsfcptf=${odir}'/simcdx_vars_sfcpt_'${stid}'_'${expl}'_'${fdate}'.nc'
      simsfcdiagsf=${odir}'/simcdx_sfcdiags_'${stid}'_'${expl}'_'${fdate}'.nc'
  
      fvals=${sfclon}'#'${sfclat}'#'${infilen}'#LON#LAT#'${wrfcdxdiags}'#'
      fvals=${fvals}${wrfcdxnodiags}
      get_WRFCDXsurface_data ${fvals}
    fi
  fi

  # cdx Surface maps
  if test ! ${wrfcdxdiags} = 'None' || test ! ${wrfcdxnodiags} = 'None'; then
    simsfcmapf=${odir}'/simcdx_vars_sfcmap_'${expl}'_'${fdate}'.nc'
    simsfcmapdiagsf=${odir}'/simcdx_sfcmapdiags_'${expl}'_'${fdate}'.nc'

    fvals=${infilen}'#'${wrfcdxdiags}'#'${wrfcdxnodiags}
    get_WRFCDXsfc2D_data ${fvals}
  fi

fi
