Index: /trunk/tools/get_WRFsounding-surface_data.bash
===================================================================
--- /trunk/tools/get_WRFsounding-surface_data.bash	(revision 2000)
+++ /trunk/tools/get_WRFsounding-surface_data.bash	(revision 2000)
@@ -0,0 +1,325 @@
+#!/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]"
+else
+
+if test $1 = 'true'; then fscratch=true
+else fscratch=false; fi
+
+odir=$2
+expl=$3
+sndid=`echo $4 | tr ',' ' ' | awk '{print $1}'`
+sndlon=`echo $4 | tr ',' ' ' | awk '{print $2}'`
+sndlat=`echo $4 | tr ',' ' ' | awk '{print $3}'`
+
+sfcid=`echo $5 | tr ',' ' ' | awk '{print $1}'`
+sfclon=`echo $5 | tr ',' ' ' | awk '{print $2}'`
+sfclat=`echo $5 | tr ',' ' ' | awk '{print $3}'`
+
+infilen=$6
+wrfsnddiags=$7
+wrfsfcdiags=$8
+wrfsfcnodiags=$9
+
+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'
+    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}
+
+    yslc='south_north_B'${ysnd}'-E'${ysnd}'-I1'
+    ystslc='south_north_stag_B'${ysnd}'-E'${ysnd1}'-I1'
+    xslc='west_east_B'${xsnd}'-E'${xsnd}'-I1'
+    xstslc='west_east_stag_B'${xsnd}'-E'${xsnd1}'-I1'
+
+    ofilen=${filen}'_Time_B0-E-1-I1_bottom_top_B0-E-1-I1_'${yslc}'_'${xslc}'_'
+    ofilen=${ofilen}${ystslc}'_'${xstslc}'.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'
+    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}
+    yslc='south_north_B'${ysnd}'-E'${ysnd}'-I1'
+    ystslc='south_north_stag_B'${ysnd}'-E'${ysnd1}'-I1'
+    xslc='west_east_B'${xsnd}'-E'${xsnd}'-I1'
+    xstslc='west_east_stag_B'${xsnd}'-E'${xsnd1}'-I1'
+
+    ofilen=${filen}'_Time_B0-E-1-I1_'${yslc}'_'${xslc}'_'${ystslc}'_'${xstslc}'.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
+}
+rootsh=`pwd`
+
+ofileins='allins_functions.inf'
+
+if ${fscratch}; then
+  echo "All instructions _______ " > ${ofileins}
+  echo "******* ****** ***** **** *** ** *" >> ${ofileins}
+  echo " " >> ${ofileins}
+fi
+
+fdate=`echo ${infilen} | tr '_' ' ' | awk '{print $3"_"$4}'`
+
+# Sounding
+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}
+
+# Surface
+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
