#! /bin/sh
#
#+
#
# .. program:: compute_erai_daily_region_2d.sh
#
# .. _compute_erai_daily_region_2d.sh:
#
# ==========================================================================
# compute_erai_daily_region_2d.sh - get ERA-I uninterpolated reference files
# ==========================================================================
#
# SYNOPSIS
# ========
#
# ::
#
#  $ compute_erai_daily_region_2d.sh
#
# DESCRIPTION
# ===========
#
# Put in ${TROPFLUX_ID} ERA-Intermin reference files
#
# Log file is written on :file:`${TROPFLUX_LOG}/compute_erai_daily_region_2d.log.{YYYYMMDDTHHMMSSZ}`
#
#     .. graphviz::
#
#        digraph compute_erai_daily_region_2d {
#           graph [
#           rankdir="LR",
#           ]
#           filein_lsp [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/FC_SF/yyyy/mm/lsp.yyyymmdd*"];
#           filein_cp [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/FC_SF/yyyy/mm/cp.yyyymmdd*"];
#           filein_e [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/FC_SF/yyyy/mm/e.yyyymmdd*"];
#           filein_slhf [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/FC_SF/yyyy/mm/slhf.yyyymmdd*"];
#           filein_ssr [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/FC_SF/yyyy/mm/ssr.yyyymmdd*"];
#           filein_trd [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/FC_SF/yyyy/mm/trd.yyyymmdd*"];
#           filein_sshf [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/FC_SF/yyyy/mm/sshf.yyyymmdd*"];
#           filein_ssrd [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/FC_SF/yyyy/mm/ssrd.yyyymmdd*"];
#           filein_str [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/FC_SF/yyyy/mm/str.yyyymmdd*"];
#           filein_ewss [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/FC_SF/yyyy/mm/ewss.yyyymmdd*"];
#           filein_nsss [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/FC_SF/yyyy/mm/nsss.yyyymmdd*"];
#           filein_geopt [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/AN_SF/yyyy/mm/geopt.yyyymm.ashei.GLOBAL_075.nc"];
#           filein_msl [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/AN_SF/yyyy/mm/msl.yyyymm.ashei.GLOBAL_075.nc"];
#           filein_sstk [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/AN_SF/yyyy/mm/sstk.yyyymm.ashei.GLOBAL_075.nc"];
#           filein_t2 [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/AN_SF/yyyy/mm/t2.yyyymm.ashei.GLOBAL_075.nc"];
#           filein_d2 [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/AN_SF/yyyy/mm/d2.yyyymm.ashei.GLOBAL_075.nc"];
#           filein_u10 [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/AN_SF/yyyy/mm/u10.yyyymm.ashei.GLOBAL_075.nc"];
#           filein_v10 [shape=ellipse,fontname=Courier,label="/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/AN_SF/yyyy/mm/v10.yyyymm.ashei.GLOBAL_075.nc"];
#
#           fileout_lsp [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_lsp_gridOrig.nc"];
#           fileout_cp [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_cp_gridOrig.nc"];
#           fileout_e [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_e_gridOrig.nc"];
#           fileout_slhf [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_slhf_gridOrig.nc"];
#           fileout_ssr [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_ssr_gridOrig.nc"];
#           fileout_trd [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_trd_gridOrig.nc"];
#           fileout_sshf [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_sshf_gridOrig.nc"];
#           fileout_ssrd [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_ssrd_gridOrig.nc"];
#           fileout_str [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_str_gridOrig.nc"];
#           fileout_ewss [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_ewss_gridOrig.nc"];
#           fileout_nsss [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_nsss_gridOrig.nc"];
#           fileout_geopt [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_geopt_gridOrig.nc"];
#           fileout_msl [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_msl_gridOrig.nc"];
#           fileout_sstk [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_sstk_gridOrig.nc"];
#           fileout_t2 [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_t2_gridOrig.nc"];
#           fileout_d2 [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_d2_gridOrig.nc"];
#           fileout_u10 [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_u10_gridOrig.nc"];
#           fileout_v10 [shape=ellipse,fontname=Courier,label="${TROPFLUX_ID}/erai_GLOB_1d_yyyy0101_yyyy1231_v10_gridOrig.nc"];
#
#           compute_erai_daily_region_2d [shape=box,
#           fontname=Courier,
#           color=blue,
#           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/compute_erai_daily_region_2d.sh",
#           label="${TROPFLUX}/src/compute_erai_daily_region_2d.sh"];
#
#           {filein_lsp filein_cp filein_e filein_slhf filein_ssr filein_trd filein_sshf filein_ssrd filein_trd filein_str filein_ewss filein_nsss filein_geopt filein_msl filein_sstk filein_t2 filein_d2 filein_u10 filein_v10} -> {compute_erai_daily_region_2d} -> {fileout_lsp fileout_cp fileout_e fileout_slhf fileout_ssr fileout_trd fileout_sshf fileout_ssrd fileout_trd fileout_str fileout_ewss fileout_nsss fileout_geopt fileout_msl fileout_sstk fileout_t2 fileout_d2 fileout_u10 fileout_v10}
#
#       }
#
# EXAMPLES
# ========
#
# On climserv only if you don't have any ERA-I reference data,
# you just have to run this tool ::
#
#  $ compute_erai_daily_region_2d.sh
#
# And look at log file with ::
#
#  $ tlogd.sh compute_erai_daily_region_2d
#
# and of course on files in ${TROPFLUX_ID}.

#
# SEE ALSO
# ========
#
# :ref:`guide data ERA-I <data_in_erai>`
#
# http://climserv.ipsl.polytechnique.fr/fr/les-donnees/era-interim-4.html
# for variables names
#
# :ref:`tropflux_profile.sh`
#
# :func:`ncpdq <nco:ncpdq>`
# :func:`ncks <nco:ncks>`
# :func:`ncrcat <nco:ncrcat>`
# :func:`ncea <nco:ncea>`
# :func:`ncflint <nco:ncflint>`
#
# :ref:`interp_erai_dewt_1989_2009.pro`
# :ref:`interp_erai_lwr_1989_2009.pro`
# :ref:`interp_erai_sst_1989_2009.pro`
# :ref:`interp_erai_t2m_1989_2009.pro`
# :ref:`interp_erai_ws_1989_2009.pro`
#
# TODO
# ====
#
# make it work 
#
# avoid ncrcat interactive question
#
# split get and process files
#
# hard coded directory - usage of ${TROPFLUX_ID}
#
# coding rules
#
# ref. to nco commands not function but programs (pb of Sphinx inventory - nco.inv - syntax)
#
# resolve missing long name variable for Netcdfname in http://climserv.ipsl.polytechnique.fr/fr/les-donnees/era-interim-4.html
#
#  - slhf
#  - trd 
#  - sshf
#
# EVOLUTIONS
# ==========
#
# - fplod 20101223T084153Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * add reference to nco commands
#
# - fplod 20101220T150356Z aedon.locean-ipsl.upmc.fr#
#
#   * indentation
#   * replace date by yyyy (date is a shell command)
#   * replace for yyyy by while yyyy
#   * replace for month by while month
#   * replace for day by while day
#   * work only on existing dates
#
# - fplod 20101220T142107Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * add Long name variables
#   * replace /bdd/IPCC/Reanalysis/ERAI/${reg}/ by ${TROPFLUX_ID}
#   * add examples
#   * add log management
#   * dirtemp not hard coded values
#   * check dirin permission
#
# - fplod 20101216T110626Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * add minimal header
#   * add graph in header
#
#-
system=$(uname)
case "${system}" in
   AIX|IRIX64)
      echo "www : no specific posix checking"
   ;;
   *)
     set -o posix
   ;;
esac
unset system
#
LANG=POSIX
#
command=$(basename ${0})
log_date=$(date -u +"%Y%m%dT%H%M%SZ")
#
usage=" Usage : ${command}"
#
hostname=$(hostname)
# default
# N.A. because no parameters
#
set -u
#
# check for ${TROPFLUX_LOG} definition
if [ "${TROPFLUX_LOG}" = "" ]
then
   echo "${command} : eee : \${TROPFLUX_LOG} not defined"
   exit 1
fi
#
# check for ${TROPFLUX_LOG} existence
if [ ! -d ${TROPFLUX_LOG} ]
then
   echo "${command} : eee : ${TROPFLUX_LOG} not found"
   exit 1
fi
#
# check for permission access on TROPFLUX_LOG
if [ ! -x ${TROPFLUX_LOG} ]
then
   echo "${command} : eee : ${TROPFLUX_LOG} not reachable"
   exit 1
fi
#
# check for write permission on TROPFLUX_LOG
if [ ! -w ${TROPFLUX_LOG} ]
then
   echo "${command} : eee : ${TROPFLUX_LOG} not writable"
   exit 1
fi
#
dirtemp=${TROPFLUX_LOG}/$(basename ${0} .sh).${log_date}.temp/
mkdir -p ${dirtemp}
#
log=${TROPFLUX_LOG}/$(basename ${0} .sh).log.${log_date}
echo "[Context]" 1>> ${log}
echo "command=$(basename ${0})" 1>>${log}
echo "hostname=${hostname}" 1>> ${log}
echo "runtime=${log_date}" 1>> ${log}
unset log_date
echo "" 1>> ${log}
#
minlat=-90.
maxlat=90.
minlon=0.
maxlon=360.
reg=GLOB
yearmin=1989
yearmax=2010
monthmin=1
monthmax=12
cmonthmin=$(printf "%2.2d" ${monthmin})
cmonthmax=$(printf "%2.2d" ${monthmax})
dirin=/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/
# check for dirin existence
if [ ! -d ${dirin} ]
then
   echo "${command} : eee : ${dirin} not found"
   exit 1
fi
#
# check for permission access on dirin
if [ ! -x ${dirin} ]
then
   echo "${command} : eee : ${dirin} not reachable"
   exit 1
fi
#
# lsp = Large scale precipitation
# cp = Convective precipitation
for var in lsp cp; do
   yyyy=${yearmin}
   while [ ${yyyy} -le ${yearmax} ]
   do
      month=${monthmin}
      while [ ${month} -le ${monthmax} ]
      do
         cmonth=$(printf "%2.2d" ${month})
         daymin=1
         daymax=$(cal ${month} ${yyyy} | grep . | fmt -1 | tail -1)
         day=${daymin}
         while [ ${day} -le ${daymax} ]
         do
            cday=$(printf "%2.2d" ${day})
            if [ -f ${dirin}/FC_SF/${yyyy}/${cmonth}/${var}.${yyyy}${cmonth}${cday}* ] 
            then
               ncpdq -U ${dirin}/FC_SF/${yyyy}/${cmonth}/${var}.${yyyy}${cmonth}${cday}* ${dirtemp}/${var}.${yyyy}${cmonth}${cday}.temp.GLOBAL_075.nc
            else
               echo "${command} : iii : no files ${dirin}/FC_SF/${yyyy}/${cmonth}/${var}.${yyyy}${cmonth}${cday}*" >> ${log}
            fi
            day=$(( ${day} + 1 ))
         done
         exist_temp_files=$(find ${dirtemp} -name "${var}.${yyyy}${cmonth}*temp*")
         if [ "${exist_temp_files}" != "" ]
         then 
            ncrcat ${dirtemp}/${var}.${yyyy}${cmonth}*temp*  ${dirtemp}/${var}.${yyyy}${cmonth}.fshei.GLOBAL_075.nc
            rm ${dirtemp}/${var}.${yyyy}${cmonth}*temp* 2> /dev/null
            ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,0,,5 ${dirtemp}/${var}.${yyyy}${cmonth}.fshei.GLOBAL_075.nc ${dirtemp}/temp_time0.nc
            ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,4,,5 ${dirtemp}/${var}.${yyyy}${cmonth}.fshei.GLOBAL_075.nc ${dirtemp}/temp_time4.nc
            ncflint -w -1,1 ${dirtemp}/temp_time0.nc ${dirtemp}/temp_time4.nc ${dirtemp}/temp_time.nc
            ncflint -w 1000.,0.0 ${dirtemp}/temp_time.nc ${dirtemp}/temp_time.nc ${dirtemp}/temp_int_${var}_${yyyy}${cmonth}.nc
            rm ${dirtemp}/temp_time* 2> /dev/null
            rm ${dirtemp}/${var}.${yyyy}${cmonth}.fshei.GLOBAL_075.nc 2> /dev/null
         else
            echo "${command} : iii : no files ${dirtemp}/${var}.${yyyy}${cmonth}*temp*" >> ${log}
         fi
         unset exist_temp_files
         month=$(( ${month} + 1 ))
      done
      ncrcat ${dirtemp}/temp_int_${var}_${yyyy}* ${TROPFLUX_ID}/erai_${reg}_1d_${yyyy}${cmonthmin}01_${yyyy}${cmonthmax}31_${var}_gridOrig.nc
      rm ${dirtemp}/temp_int_${var}_${yyyy}* 2> /dev/null
      yyyy=$(( ${yyyy} + 1 ))
   done
done
#
# e = Evaporation
# slhf = ?
# ssr = Surface solar radiation
# trd = ?
# sshf = ?
# ssrd = Downward surface solar radiation
# str = Surface thermal radiation
# ewss = East /West surface stress
# nsss = North/South surface stress
for var in e slhf ssr trd sshf ssrd str ewss nsss ; do
   yyyy=${yearmin}
   while [ ${yyyy} -le ${yearmax} ]
   do
      month=${monthmin}
      while [ ${month} -le ${monthmax} ]
      do
         cmonth=$(printf "%2.2d" ${month})
         daymin=1
         daymax=$(cal ${month} ${yyyy} | grep . | fmt -1 | tail -1)
         day=${daymin}
         while [ ${day} -le ${daymax} ]
         do
            cday=$(printf "%2.2d" ${day})
            if [ -f ${dirin}/FC_SF/${yyyy}/${cmonth}/${var}.${yyyy}${cmonth}${cday}* ] 
            then
               ncpdq -U ${dirin}/FC_SF/${yyyy}/${cmonth}/${var}.${yyyy}${cmonth}${cday}* ${dirtemp}/${var}.${yyyy}${cmonth}${cday}.temp.GLOBAL_075.nc
            else
               echo "${command} : iii : no files ${dirin}/FC_SF/${yyyy}/${cmonth}/${var}.${yyyy}${cmonth}${cday}*" >> ${log}
            fi
            day=$(( ${day} + 1 ))
         done
         exist_temp_files=$(find  ${dirtemp} -name "${var}.${yyyy}${cmonth}*temp*")
         if [ "${exist_temp_files}" != "" ]
         then
            ncrcat ${dirtemp}/${var}.${yyyy}${cmonth}*temp*  ${dirtemp}/${var}.${yyyy}${cmonth}.fshei.GLOBAL_075.nc
            rm ${dirtemp}/${var}.${yyyy}${cmonth}*temp* 2> /dev/null
            ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,0,,5 ${dirtemp}/${var}.${yyyy}${cmonth}.fshei.GLOBAL_075.nc ${dirtemp}/temp_time0.nc
            ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,4,,5 ${dirtemp}/${var}.${yyyy}${cmonth}.fshei.GLOBAL_075.nc ${dirtemp}/temp_time4.nc
            ncflint -w 1.1574074,-1.1574074 ${dirtemp}/temp_time0.nc ${dirtemp}/temp_time4.nc ${dirtemp}/temp_time.nc
            ncflint -w 0.00001,0.0 ${dirtemp}/temp_time.nc ${dirtemp}/temp_time.nc ${dirtemp}/temp_int_${var}_${yyyy}${cmonth}.nc
            rm ${dirtemp}/temp_time* 2> /dev/null
            rm ${dirtemp}/${var}.${yyyy}${cmonth}.fshei.GLOBAL_075.nc 2> /dev/null
         else
            echo "${command} : iii : no files ${dirtemp}/${var}.${yyyy}${cmonth}*temp*" >> ${log}
         fi
         unset exist_temp_files
         month=$(( ${month} + 1 ))
      done
      ncrcat ${dirtemp}/temp_int_${var}_${yyyy}* ${TROPFLUX_ID}/erai_${reg}_1d_${yyyy}${cmonthmin}01_${yyyy}${cmonthmax}31_${var}_gridOrig.nc
      rm ${dirtemp}/temp_int_${var}_${yyyy}* 2> /dev/null
      yyyy=$(( ${yyyy} + 1 ))
   done
done
#
# geopt = Geopotentiel
# msl = Mean sea level pressure
# sstk = Sea surface temperature
# t2 = Temperature at 2 meters
# d2 = Dew point at 2 meters
# u10 = 10m U wind component
# v10 = 10m V wind component
for var in geopt msl sstk t2 d2 u10 v10 ; do
   yyyy=${yearmin}
   while [ ${yyyy} -le ${yearmax} ]
   do
      month=${monthmin}
      while [ ${month} -le ${monthmax} ]
      do
         cmonth=$(printf "%2.2d" ${month})
         ncpdq -U ${dirin}/AN_SF/${yyyy}/${var}.${yyyy}${cmonth}.ashei.GLOBAL_075.nc ${dirtemp}/${var}.${yyyy}${cmonth}.ashei.GLOBAL_075.nc
         ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,0,,4 ${dirtemp}/${var}.${yyyy}${cmonth}.ashei.GLOBAL_075.nc ${dirtemp}/temp_time1.nc
         ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,1,,4 ${dirtemp}/${var}.${yyyy}${cmonth}.ashei.GLOBAL_075.nc ${dirtemp}/temp_time2.nc
         ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,2,,4 ${dirtemp}/${var}.${yyyy}${cmonth}.ashei.GLOBAL_075.nc ${dirtemp}/temp_time3.nc
         ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,3,,4 ${dirtemp}/${var}.${yyyy}${cmonth}.ashei.GLOBAL_075.nc ${dirtemp}/temp_time4.nc
         ncea ${dirtemp}/temp_time* ${dirtemp}/temp_int_${yyyy}${cmonth}.nc
         rm ${dirtemp}/temp_time* 2> /dev/null
         rm ${dirtemp}/${var}.${yyyy}${cmonth}.ashei.GLOBAL_075.nc 2> /dev/null
         month=$(( ${month} + 1 ))
      done
      ncrcat ${dirtemp}/temp_int_${yyyy}* ${TROPFLUX_ID}/erai_${reg}_1d_${yyyy}${cmonthmin}01_${yyyy}${cmonthmax}31_${var}_gridOrig.nc
      rm ${dirtemp}/temp_int_${yyyy}* 2> /dev/null
      yyyy=$(( ${yyyy} + 1 ))
   done
done
#
# end
exit 0
