#!/bin/bash
### Script to get sounding and surface points for validation purposes

scratch='false'

# Input folder
infolder='/ccc/scratch/cont003/lmd/fitaborl/estudios/FPS_ALPS/additional/IOP'

# Output folder
ofolder='/ccc/scratch/cont003/lmd/fitaborl/estudios/FPS_ALPS/additional/IOP/validation'

# Labels of the experiments
experiments='120lev:80lev:50lev:50lev_assigned:38lev:NOaerosol'

# Sounding stations [sndid],[sndlon],[sndlat]:[sndid2],[sndlon2],[sndlat2]:...
sndstations='10868,11.55,48.25:16080,9.28,45.43:16144,11.61,44.65:16546,8.85,39.35'

# Surface stations [sfcid],[sfclon],[sfclat]:[sfcid2],[sfclon2],[sfclat2]:...
sfcstations='None'

# Sounding diagnostics
snddiags='WRFt:WRFtda:WRFp:WRFua:WRFva:ws:WRFwd'

# Surface diagnostics
sfcdiags='wds:wss:WRFtdas'

# Surface NON-diag variables
sfcnondiags='T2'

#######    #######
## MAIN
    #######
insoutf='allins_functions.inf'
module load python/2.7.14

exps=`echo ${experiments} | tr ':' ' '`
sndsts=`echo ${sndstations} | tr ':' ' '`
sfcsts=`echo ${sfcstations} | tr ':' ' '`

for expn in ${exps}; do
  ifold=${infolder}/${expn}/wrfout
  ofold=${ofolder}/${expn}

  files=`ls -1 ${infolder}/${expn}/wrfout/wrfout_d02*`

  mkdir -p ${ofold}

  if test ! ${sndstations} = 'None'; then
  for sndvn in ${sndsts}; do
    sndid=`echo ${sndvn} | tr ',' ' ' | awk '{print $1}'`
    sndlon=`echo ${sndvn} | tr ',' ' ' | awk '{print $2}'`
    sndlat=`echo ${sndvn} | tr ',' ' ' | awk '{print $3}'`

    for filen in ${files}; do
      $pyHOME/get_WRFsounding-surface_data.bash ${scratch} ${ofold} ${expn} ${sndvn} \
        None ${filen} ${snddiags} ${sfcdiags} ${sfcnondiags}
    # end of files
    done

    # Concatenating files
    ofile=${ofold}/simout_sndvars_${sndid}_${expn}.nc
    if test ${scratch} = 'true'; then rm ${ofile} >& /dev/null; fi
    if test ! -f ${ofile}; then
      values=${ofold}',Time,time'
      HMT='simout_snddiags_,'${sndid},'nc'
      python ${pyHOME}/nc_var.py -o netcdf_fold_concatenation_HMT -S ${values}         \
       -f ${HMT} -v all
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python ${pyHOME}/nc_var.py -o netcdf_fold_concatenation_HMT -S ${values}  \
        -f ${HMT} -v all
        exit
      fi
      echo " * Concatenating sounding data at point: "${sndlon}", "${sndlat} >> ${insoutf}
      echo python  ${pyHOME}/nc_var.py -o netcdf_fold_concatenation_HMT -S "'"${values}"'" \
       -f ${HMT} -v all >> ${insoutf}
      echo " " >> ${insoutf}
      mv netcdf_fold_concatenated_HMT.nc ${ofile}
    fi
    #exit

  # end of soundings
  done
  fi

  if test ! ${sfcstations} = 'None'; then
  for sfcvn in ${sfcsts}; do
    sfcid=`echo ${sfcvn} | tr ',' ' ' | awk '{print $1}'`
    sfclon=`echo ${sfcvn} | tr ',' ' ' | awk '{print $2}'`
    sfclat=`echo ${sfcvn} | tr ',' ' ' | awk '{print $3}'`

    for filen in ${files}; do
      $pyHOME/get_WRFsounding-surface_data.bash ${scratch} ${ofold} ${expn} None     \
        ${sfcvn} ${filen} ${snddiags} ${sfcdiags} ${sfcnondiags}
    # end of files
    done

    # Concatenating files
    ofile=${ofold}/simout_sfcvars_${sfcid}_${expn}.nc
    if test ${scratch} = 'true'; then rm ${ofile} >& /dev/null; fi
    if test ! -f ${ofile}; then
      values=${ofold}',Time,time'
      HMT='simout_sfcdiags_,'${sfcid},'nc'
      python ${pyHOME}/nc_var.py -o netcdf_fold_concatenation_HMT -S ${values}         \
       -f ${HMT} -v all
      if test $? -ne 0; then
        echo ${errmsg}
        echo "  python failed!!"
        echo python ${pyHOME}/nc_var.py -o netcdf_fold_concatenation_HMT -S ${values}  \
        -f ${HMT} -v all
        exit
      fi
      echo " * Concatenating surface data at point: "${sfclon}", "${sfclat} >> ${insoutf}
      echo python  ${pyHOME}/nc_var.py -o netcdf_fold_concatenation_HMT -S "'"${values}"'" \
       -f ${HMT} -v all >> ${insoutf}
      echo " " >> ${insoutf}
      mv netcdf_fold_concatenated_HMT.nc ${ofile}
    fi
    #exit

  # end of surfaces
  done
  fi

# End of experiments
done
