#!/bin/bash -x
# Script to analyse results from model outputs following trajectories
# L. Fita, LMD May 2015

# isim: folder with the model outputs
# osim: folder for the outputs of the analysis

# A file called 'trajectory.dat' is mandatory to proceed
# Concatenated file `netcdf_fold_concatenated.nc' is used to retrieve the trajectory
# Output
##
# netcdf_fold_concatenated.nc: File with only `P' to retrieve the trajectory
# full_concatenated.nc: File with a given set of variables concatenated
# diagnostics.nc: file with the computed diagnostics
# fullout.nc: merge of `full_concatenated.nc' and `diagnostics.nc'
# tevol_boxtraj.nc: file with the values following the trajectory

models='wrf:lwrf'

ifold=/media/ExtDiskD/bkup/ciclad/etudes/WL_HyMeX/superstorm
ofold=/home/lluis/etudes/WL_HyMeX/superstorm

#WRFexps='control:mp1:pbl1:pbl2:rad1'
WRFexps='control:cu1:pblsfc1:rad1:shallow'
LMDZexps='lmdza:lmdzb'
LWRFexps='lmdza:lmdzb'

# Domain to analyse
dom='1'

#ofold=/homedata/lfita/etudes/WRFdynamicoSENS
#HOMEpy=/home/lfita/etudes/WRF_LMDZ/svn/LMDZ_WRF/tools
HOMEpy=/home/lluis/etudes/WRF_LMDZ/svn/LMDZ_WRF/tools

use_concatenated=true

####### ###### ##### #### ### ## #
errormsg='ERROR -- error -- ERROR -- error'
main='getting_fields-trajectories.bash'

#WRFvars='all'
WRFvars='Times,XLONG,XLAT,T2,U10,V10,P,PB,PSFC,HGT,T,QVAPOR,RAINC,RAINNC'

#LMDZvars='all'
LMDZars='Time_counter,longitude,latitude,u10m,v10m,t2m,wind10m,slp,precip'

LWRFvars='Times,XLONG,XLAT,T2,U10,V10,P,PB,PSFC,HGT,T,QVAPOR,RAINC,RAINNC'
#LWRFvars='LU_INDEX,ZNU,ZNW,ZS,DZS,U,RU_TEND,V,RV_TEND,W,PH,PHB,T,HFX_FORCE,LH_FORCE,'
#LWRFvars=${LWRFvars}'TSK_FORCE,HFX_FORCE_TEND,LH_FORCE_TEND,TSK_FORCE_TEND,MU,MUB,'
#LWRFvars=${LWRFvars}'NEST_POS,P,PB,FNM,FNP,RDNW,RDN,DNW,DN,CFN,CFN1,P_HYD,Q2,T2,TH2,'
#LWRFvars=${LWRFvars}'PSFC,U10,V10,RDX,RDY,RESM,ZETATOP,CF1,CF2,CF3,XTIME,QVAPOR,'
#LWRFvars=${LWRFvars}'LANDMASK,TOPOSTDV,TSLB,SMOIS,SH2O,SMCREL,SEAICE,XICEM,SFROFF,'
#LWRFvars=${LWRFvars}'UDROFF,VEGFRA,GRDFLX,ACGRDFLX,SNOW,SNOWH,CANWAT,SST,SSTSK,LAI,'
#LWRFvars=${LWRFvars}'H_DIABATIC,MAPFAC_M,MAPFAC_U,MAPFAC_V,MAPFAC_MX,MAPFAC_MY,'
#LWRFvars=${LWRFvars}'MAPFAC_UX,MAPFAC_UY,MAPFAC_VX,MF_VX_INV,MAPFAC_VY,F,E,SINALPHA,'
#LWRFvars=${LWRFvars}'COSALPHA,HGT,TSK,P_TOP,T00,P00,TLP,TISO,MAX_MSTFX,MAX_MSTFY,'
#LWRFvars=${LWRFvars}'RAINC,RAINSH,RAINNC,SNOWNC,GRAUPELNC,HAILNC,CLDFRA,SWDOWN,GLW,'
#LWRFvars=${LWRFvars}'SWNORM,OLR,XLAT,XLONG,XLAT_U,XLONG_U,XLAT_V,XLONG_V,ALBEDO,'
#LWRFvars=${LWRFvars}'ALBBCK,EMISS,NOAHRES,TMN,XLAND,ZNT,UST,PBLH,HFX,QFX,LH,ACHFX,'
#LWRFvars=${LWRFvars}'ACLHF,SNOWC,SR,POTEVP,SNOPCX,SOILTB,LAGES_LIC,LAGES_OCE,'
#LWRFvars=${LWRFvars}'LAGES_SIC,LAGES_TER,LAIRE,LAIRETER,LALB1,LALB2,LALBE_LIC,'
#LWRFvars=${LWRFvars}'LALBE_OCE,LALBE_SIC,LALBE_TER,LALE,LALE_BL,LALE_WK,LALP,LALP_BL,'
#LWRFvars=${LWRFvars}'LALP_WK,LA_TH,LBETA_PREC,LBILS,LBILS_DISS,LBILS_EC,'
#LWRFvars=${LWRFvars}'LBILS_ENTHALP,LBILS_KINETIC,LBILS_LATENT,LBILS_TKE,LCAPE,'
#LWRFvars=${LWRFvars}'LCAPE_MAX,LCDRH,LCDRM,LCLDEMI,LCLDH,LCLDL,LCLDM,LCLDQ,LCLDT,'
#LWRFvars=${LWRFvars}'LCLDTAU,LCLWCON,LCONTFRACATM,LCONTFRACOR,LCUMPB,LCUMRN,LDMC,'
#LWRFvars=${LWRFvars}'LDNWD,LDNWD0,LDQAJS,LDQCON,LDQDYN,LDQEVA,LDQLSC,LDQLSCST,'
#LWRFvars=${LWRFvars}'LDQLSCTH,LDQPHY,LDQTHE,LDQVDF,LDQWAK,LDTAJS,LDTCON,LDTDIS,'
#LWRFvars=${LWRFvars}'LDTDYN,LDTEC,LDTEVA,LD_TH,LDTHMIN,LDTLIF,LDTLSC,LDTLSCHR,'
#LWRFvars=${LWRFvars}'LDTLSCST,LDTLSCTH,LDTLW0,LDTLWR,LDTORO,LDTPHY,LDTSVDFG,LDTSVDFI,'
#LWRFvars=${LWRFvars}'LDTSVDFO,LDTSVDFT,LDTSW0,LDTSWR,LDTTHE,LDTVDF,LDTWAK,LDUCON,'
#LWRFvars=${LWRFvars}'LDUDYN,LDULIF,LDUORO,LDUVDF,LDVCON,LDVDYN,LDVLIF,LDVORO,LDVVDF,'
#LWRFvars=${LWRFvars}'LEC550AER,LE_TH,LEVAP,LEVAP_LIC,LEVAP_OCE,LEVAPPOT_LIC,'
#LWRFvars=${LWRFvars}'LEVAPPOT_OCE,LEVAPPOT_SIC,LEVAPPOT_TER,LEVAP_SIC,LEVAP_TER,LEVU,'
#LWRFvars=${LWRFvars}'LFBASE,LFDER,LFFONTE,LFL,LFLAT,LFLW_LIC,LFLW_OCE,LFLW_SIC,'
#LWRFvars=${LWRFvars}'LFLW_TER,LFQCALVING,LFQFONTE,LFRACT_LIC,LFRACT_OCE,LFRACT_SIC,'
#LWRFvars=${LWRFvars}'LFRACT_TER,LFSNOW,LFSW_LIC,LFSW_OCE,LFSW_SIC,LFSW_TER,'
#LWRFvars=${LWRFvars}'LFTIME_CON,LFTIME_TH,LGEOP,LH2O,LIWCON,LIWP,LKZ,LKZ_MAX,'
#LWRFvars=${LWRFvars}'LLAMBDA_TH,LLAT_LIC,LLAT_OCE,LLAT_SIC,LLAT_TER,LLMAXTH,LLWCON,'
#LWRFvars=${LWRFvars}'LLWDN200,LLWDN200CLR,LLWDNSFC,LLWDNSFCCLR,LLWDOWNOR,LLWP,'
#LWRFvars=${LWRFvars}'LLWUP200,LLWUP200CLR,LLWUPSFC,LLWUPSFCCLR,LMA,LMASS,LMC,LMCD,'
#LWRFvars=${LWRFvars}'LMSNOW,LNDAYRAIN,LNETTOP,LOLIQ,LOVAP,LOVAPINIT,LOZONE,LPAPRS,'
#LWRFvars=${LWRFvars}'LPB,LPBASE,LPHIS,LPLCL,LPLFC,LPLUC,LPLUL,LPLULST,LPLULTH,'
#LWRFvars=${LWRFvars}'LPOURC_LIC,LPOURC_OCE,LPOURC_SIC,LPOURC_TER,LPR_CON_I,LPR_CON_L,'
#LWRFvars=${LWRFvars}'LPRECIP,LPRES,LPRESNIVS,LPR_LSC_I,LPR_LSC_L,LPRW,LPSOL,LPTCONV,'
#LWRFvars=${LWRFvars}'LPTOP,LQ10,LQ100,LQ200,LQ2M,LQ50,LQ500,LQ700,LQ850,LQSAT2M,'
#LWRFvars=${LWRFvars}'LQSOL,LQSURF,LQ_TH,LRADSOL,LRATQS,LRE,LREF_ICE,LREF_LIQ,LRH2M,'
#LWRFvars=${LWRFvars}'LRH2M_MAX,LRH2M_MIN,LRHUM,LRLD,LRLDCS,LRLU,LRLUCS,LRN,LRNEB,'
#LWRFvars=${LWRFvars}'LRNEBCON,LRNEBLS,LRSD,LRSDCS,LRSU,LRSUCS,LRUGS,LRUGS_LIC,'
#LWRFvars=${LWRFvars}'LRUGS_OCE,LRUGS_SIC,LRUGS_TER,LSENS,LSENS_LIC,LSENS_OCE,'
#LWRFvars=${LWRFvars}'LSENS_SIC,LSENS_TER,LSICF,LS_LCL,LSLP,LSNOW,LSNOWL,LSOLL,LSOLL0,'
#LWRFvars=${LWRFvars}'LSOLLWDOWN,LSOLS,LSOLS0,LS_PBLH,LS_PBLT,LS_THERM,LSWDN200,'
#LWRFvars=${LWRFvars}'LSWDN200CLR,LSWDNSFC,LSWDNSFCCLR,LSWDNTOA,LSWDNTOACLR,LSWDOWNOR,'
#LWRFvars=${LWRFvars}'LSWNETOR,LSWUP200,LSWUP200CLR,LSWUPSFC,LSWUPSFCCLR,LSWUPTOA,'
#LWRFvars=${LWRFvars}'LSWUPTOACLR,LT10,LT100,LT200,LT2M,LT2M_LIC,LT2M_MAX,LT2M_MIN,'
#LWRFvars=${LWRFvars}'LT2M_OCE,LT2M_SIC,LT2M_TER,LT50,LT500,LT700,LT850,LTAUX,'
#LWRFvars=${LWRFvars}'LTAUX_LIC,LTAUX_OCE,LTAUX_SIC,LTAUX_TER,LTAUY,LTAUY_LIC,'
#LWRFvars=${LWRFvars}'LTAUY_OCE,LTAUY_SIC,LTAUY_TER,LTEMP,LTHETA,LTKE,LTKE_LIC,'
#LWRFvars=${LWRFvars}'LTKE_MAX,LTKE_MAX_LIC,LTKE_MAX_OCE,LTKE_MAX_SIC,LTKE_MAX_TER,'
#LWRFvars=${LWRFvars}'LTKE_OCE,LTKE_SIC,LTKE_TER,LTNHUS,LTNHUSC,LTNHUSSCPBL,LTNT,'
#LWRFvars=${LWRFvars}'LTNTC,LTNTR,LTNTSCPBL,LT_OCE_SIC,LTOPL,LTOPL0,LTOPS,LTOPS0,'
#LWRFvars=${LWRFvars}'LTPOT,LTPOTE,LTSOL,LTSOL_LIC,LTSOL_OCE,LTSOL_SIC,LTSOL_TER,LU10,'
#LWRFvars=${LWRFvars}'LU100,LU10M,LU10M_LIC,LU10M_OCE,LU10M_SIC,LU10M_TER,LU200,LU50,'
#LWRFvars=${LWRFvars}'LU500,LU700,LU850,LUE,LUPWD,LUQ,LUSTAR,LUSTAR_LIC,LUSTAR_OCE,'
#LWRFvars=${LWRFvars}'LUSTAR_SIC,LUSTAR_TER,LV10,LV100,LV10M,LV10M_LIC,LV10M_OCE,'
#LWRFvars=${LWRFvars}'LV10M_SIC,LV10M_TER,LV200,LV50,LV500,LV700,LV850,LVE,LVITU,'
#LWRFvars=${LWRFvars}'LVITV,LVITW,LVPRECIP,LVQ,LW10,LW100,LW200,LW50,LW500,LW700,'
#LWRFvars=${LWRFvars}'LW850,LWAKE_DELTAQ,LWAKE_DELTAT,LWAKE_H,LWAKE_OMG,LWAKE_S,LWAPE,'
#LWRFvars=${LWRFvars}'LWBEFF,LWBILO_LIC,LWBILO_OCE,LWBILO_SIC,LWBILO_TER,LWBILS_LIC,'
#LWRFvars=${LWRFvars}'LWBILS_OCE,LWBILS_SIC,LWBILS_TER,LWDTRAINA,LWDTRAINM,LWEAKINV,'
#LWRFvars=${LWRFvars}'LWIND10M,LWIND10MAX,LZ10,LZ100,LZ200,LZ50,LZ500,LZ700,LZ850,'
#LWRFvars=${LWRFvars}'LZFULL,LZHALF,LZMAX_TH,LCONTROL,LMSK,LTER,LLIC,LOCE,LSIC,'
#LWRFvars=${LWRFvars}'LTKSOIL,LOTTER,LOTLIC,LOTOCE,LOTSIC,LQKSOIL,LWSOL,LALBKSOIL,'
#LWRFvars=${LWRFvars}'LLWALBKSOIL,LEVAPKSOIL,LSNOWKSOIL,LRADS,LSOLSW,LSOLLW,LRAIN,'
#LWRFvars=${LWRFvars}'LRUGKSOIL,LAGESNOKSOIL,LZMEA,LZSTD,LZSIG,LZGAM,LZTHE,LZPIC,'
#LWRFvars=${LWRFvars}'LZVAL,LZRUGSREL,LRUGSEA,LRUNOFFLIC,LZMAX0,LF0,LEMA_WORK1,'
#LWRFvars=${LWRFvars}'LEMA_WORK2,LWAKE_CSTAR,LWAKE_PE,LWAKE_FIP,Times,'
#LWRFvars=${LWRFvars}'ITIMESTEP,IVGTYP,ISLTYP,STEPAVE_COUNT,SAVE_TOPO_FROM_REAL'

LMDZtrjvars='time_counter,u10m,v10m,precip,slp,psol'
#LMDZ lon/lat file
#if test ! -f lmdz_lonlat.nc; then
#  python ${HOMEpy}/nc_var.py -o selvar -v t2m -f \
#    ${ifold}/lmdz/run/lmdza/lmdzout/histins.nc -S 'lon@lon,lat@lat'
#  mv selvar_new.nc lmdz_lonlat.nc
#fi

WRFvdims='XLONG,XLAT,ZNU,time'
LMDZvdims='lon,lat,presnivs,time_counter'
LWRFvdims='XLONG,XLAT,ZNU,time'

WRFtrajvars='T2,wss,psl,pr'
LMDZtrajvars='t2m,wind10m,slp,precip'
LWRFtrajvars='T2,wss,psl,pr'

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

mods=`expr ${models} | tr ':' ' '`


for mod in ${mods}; do
  echo ${mod}"..."
  if test ${mod} = 'wrf'; then 
    experiments=${WRFexps}
    allvars=${WRFvars}
    dimvars=${WRFvdims}
    trjvars=${WRFtrajvars}
  fi
  if test ${mod} = 'lmdz'; then 
    experiments=${LMDZexps}
    allvars=${LMDZvars}
    dimvars=${LMDZvdims}
    trjvars=${LMDZtrajvars}
  fi
  if test ${mod} = 'lwrf'; then 
    experiments=${LWRFexps}
    allvars=${LWRFvars}
    dimvars=${LWRFvdims}
    trjvars=${LWRFtrajvars}
  fi

  exps=`expr ${experiments} | tr ':' ' '`

  for exp in ${exps}; do
    echo "  "${exp}"..."
# Destination folder with the outputs of the analysis
#    osim=${ofold}/${mod}/run/${exp}
    osim=${ofold}/${exp}
    if test ${mod} = 'wrf' || test ${mod} = 'lwrf'; then
      isim=${ifold}/${mod}/run/${exp}/wrfout
# Trajectory file
##
      if test ! -f ${osim}/P_lev1_values.nc; then
        python ${HOMEpy}/nc_var.py -o netcdf_fold_concatenation -f wrfout_d0${dom}   \
          -S ${isim},Time -v P
        if test $? -ne 0; then
          echo ${errormsg}
          echo "  "${main}": python does not work !!"
          echo "  "${HOMEpy}"/nc_var.py -o netcdf_fold_concatenation -f wrfout_d0"${dom}\
            " -S "${isim}",Time -v P"
          exit
        else
          python ${HOMEpy}/nc_var.py -o DataSetSection_multidims                     \
            -f netcdf_fold_concatenated.nc -S bottom_top,0,0,1
          if test $? -ne 0; then
            echo ${errormsg}
            echo "  "${main}": python does not work !!"
            echo "  "${HOMEpy}"/nc_var.py -o DataSetSection_multidims -f "\
              "netcdf_fold_concatenated.nc -S bottom_top,0,0,1"
            exit
          fi
          mv netcdf_fold_concatenated_bottom_top_B0-E0-I1.nc ${osim}/P_lev1_values.nc
          rm netcdf_fold_concatenated.nc
        fi
      fi

# concatenating all file
##
      if test ! -f ${osim}/full_concatenated.nc; then
        python ${HOMEpy}/nc_var.py -o netcdf_fold_concatenation -f wrfout_d0${dom}   \
          -S ${isim},Time -v ${allvars}
        if test $? -ne 0; then
          echo ${errormsg}
          echo "  "${main}": python does not work !!"
          echo "  "${HOMEpy}"/nc_var.py -o netcdf_fold_concatenation -f wrfout_d0"${dom}\
            " -S "${isim}",Time -v "${allvars}
          exit
        else
          mv netcdf_fold_concatenated.nc ${osim}/full_concatenated.nc
        fi
        python ${HOMEpy}/nc_var.py -o WRF_CFtime_creation -S 19491201000000,hours    \
          -f ${osim}/full_concatenated.nc -v time
      fi

# diagnostics
##
      if test ! -f ${osim}/diagnostics.nc; then
        if ${use_concatenated}; then
          diags='RAINTOT|RAINC@RAINNC@time,WRFmslp|WRFp@PSFC@HGT@WRFt@QVAPOR,wss|U10@V10'
          python ${HOMEpy}/diagnostics.py -f ${osim}/full_concatenated.nc              \
            -d 'Time@time,bottom_top@ZNU,south_north@XLAT,west_east@XLONG' -v ${diags} \
            -f ${osim}/full_concatenated.nc
          if test $? -ne 0; then
            echo ${errormsg}
            echo "  "${main}": python does not work !!"
            echo "  python "${HOMEpy}"/diagnostics.py -f "${osim}"/full_concatenated.nc \
              -d 'Time@time,bottom_top@ZNU,south_north@XLAT,west_east@XLONG' -v "${diags} \
              "-f ${osim}/full_concatenated.nc"
            exit
          else
            mv diagnostics.nc ${osim}/diagnostics.nc
          fi
        else
          echo "Computing diagnostics file by file..."
          diags='RAINTOT|RAINC@RAINNC@WRFtime,WRFmslp|WRFp@PSFC@HGT@WRFt@QVAPOR,wss|U10@V10'
          for file in ${isim}/wrfout_d0${dom}*; do
            fdate=`echo ${file} | tr '_' ' ' | awk '{print $3"_"$4}'`
            if test ! ${osim}/diagnostics_${fdate}.nc; then
              echo "  "${file}
              python ${HOMEpy}/diagnostics.py -f ${osim}/full_concatenated.nc              \
                -d 'Time@Times,bottom_top@ZNU,south_north@XLAT,west_east@XLONG' -v ${diags}\
                -f ${file}
              #exit
              if test $? -ne 0; then
                echo ${errormsg}
                echo "  "${main}": python does not work !!"
                echo "  python "${HOMEpy}"/diagnostics.py -f "${osim}"/full_concatenated.nc \
                  -d 'Time@Times,bottom_top@ZNU,south_north@XLAT,west_east@XLONG' -v "${diags} \
                  "-f "${file}
                exit
              else
                mv diagnostics.nc ${osim}/diagnostics_${fdate}.nc
              fi
            fi
          done
          python ${HOMEpy}/nc_var.py -o netcdf_fold_concatenation -f diagnostics_    \
            -S ${isim},Time -v ${allvars}
          if test $? -ne 0; then
            echo ${errormsg}
            echo "  "${main}": python does not work !!"
            echo "  "${HOMEpy}"/nc_var.py -o netcdf_fold_concatenation -f diagnostics_"\
              " -S "${isim}",Time -v all"
            exit
          else
            mv netcdf_fold_concatenated.nc ${osim}/full_concatenated.nc
          fi
          python ${HOMEpy}/nc_var.py -o WRF_CFtime_creation -S 19491201000000,hours    \
            -f ${osim}/full_concatenated.nc -v time
        fi
      fi
# full file
##
      if test ! -f ${osim}/fullout.nc; then
        cdo merge ${osim}/full_concatenated.nc ${osim}/diagnostics.nc                \
          ${osim}/fullout.nc
      fi

# End of if 'wrf', 'lwrf'
    else
      osim=${ofold}/${mod}/run/erai/${exp}
      isim=${osim}/lmdzout
# Trajectory file
##
      if test ! -f ${osim}/trajectory.nc; then
        cdo selvar,${LMDZtrjvars} ${isim}/histins.nc ${osim}/trajectory.nc
      fi

    fi
    if test ${mod} = 'lmdz'; then
      trjfile=${osim}/lmdzout/histins.nc
    else
      trjfile=${osim}/fullout.nc
    fi

# Following trajectory values
##
    if test ! -f ${osim}/tevol_boxtraj.nc && test -f ${osim}/trajectory.dat; then
      tfirst=`cat ${osim}/trajectory.dat | grep -v '#' | head -n 1 | awk '{print $1-1}'`
      echo "tfirst: "${tfirst}
      trajvalues=${osim}'/trajectory.dat@'${tfirst}','${dimvars}',cf,5,5'
      python ${HOMEpy}/nc_var.py -f ${trjfile} -o compute_tevolboxtraj               \
        -S ${trajvalues} -v ${trjvars}
      if test $? -ne 0; then
        echo ${errormsg}
        echo "  "${main}": python does not work !!"
        echo "  python "${HOMEpy}"nc_var.py -f ${trjfile} -o compute_tevolboxtraj -S \
          ${trajvalues}  -v ${trjvars}"
        exit
      else
        mv tevolboxtraj_multi-var.nc ${osim}/tevol_boxtraj.nc
      fi
    fi
#    exit
# end experiments
  done
#  exit
# end models
done
