#! /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
# ===========
#
#     .. 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="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_lsp_gridOrig.nc"];
#           fileout_cp [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_cp_gridOrig.nc"];
#           fileout_e [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_e_gridOrig.nc"];
#           fileout_slhf [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_slhf_gridOrig.nc"];
#           fileout_ssr [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_ssr_gridOrig.nc"];
#           fileout_trd [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_trd_gridOrig.nc"];
#           fileout_sshf [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_sshf_gridOrig.nc"];
#           fileout_ssrd [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_ssrd_gridOrig.nc"];
#           fileout_str [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_str_gridOrig.nc"];
#           fileout_ewss [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_ewss_gridOrig.nc"];
#           fileout_nsss [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_nsss_gridOrig.nc"];
#           fileout_geopt [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_geopt_gridOrig.nc"];
#           fileout_msl [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_msl_gridOrig.nc"];
#           fileout_sstk [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_sstk_gridOrig.nc"];
#           fileout_t2 [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_t2_gridOrig.nc"];
#           fileout_d2 [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_d2_gridOrig.nc"];
#           fileout_u10 [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/erai_GLOB_1d_yyyy0101_yyyy1231_u10_gridOrig.nc"];
#           fileout_v10 [shape=ellipse,fontname=Courier,label="/bdd/IPCC/Reanalysis/ERAI/${reg}/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}
#
#       }
#
# SEE ALSO
# ========
#
# :ref:`guide data ERA-I <data_in_erai>`
#
# TODO
# ====
#
# trace
#
# hard coded directory - usage of ${TROPFLUX_ID}
#
# coding rules
#
# EVOLUTIONS
# ==========
#
# - fplod 20101216T110626Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * add minimal header
#   * add graph in header
#
#-
#
minlat=-90.
maxlat=90.
minlon=0.
maxlon=360.
reg=GLOB
dirtemp=/home/mlengaig/ERAI/1d_2d_${reg}/
dirin=/bdd/ERAI/NETCDF/GLOBAL_075/4xdaily/
dirout=/bdd/IPCC/Reanalysis/ERAI/${reg}/
mkdir ${dirout}
mkdir ${dirtemp}
\rm ${dirtemp}/*
for var in lsp cp; do
for date in 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010; do
for month in 01 02 03 04 05 06 07 08 09 10 11 12; do
for day in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31; do
ncpdq -U ${dirin}/FC_SF/${date}/${month}/${var}.${date}${month}${day}* ${dirtemp}/${var}.${date}${month}${day}.temp.GLOBAL_075.nc
done
ncrcat ${dirtemp}/${var}.${date}${month}*temp*  ${dirtemp}/${var}.${date}${month}.fshei.GLOBAL_075.nc
\rm ${dirtemp}/${var}.${date}${month}*temp*
ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,0,,5 ${dirtemp}/${var}.${date}${month}.fshei.GLOBAL_075.nc ${dirtemp}/temp_time0.nc
ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,4,,5 ${dirtemp}/${var}.${date}${month}.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}_${date}${month}.nc
\rm ${dirtemp}/temp_time*
\rm ${dirtemp}/${var}.${date}${month}.fshei.GLOBAL_075.nc
done
ncrcat ${dirtemp}/temp_int_${var}_${date}* /bdd/IPCC/Reanalysis/ERAI/${reg}/erai_${reg}_1d_${date}0101_${date}1231_${var}_gridOrig.nc
\rm ${dirtemp}/temp_int_${var}_${date}*
done
done

for var in e slhf ssr trd sshf ssrd str ewss nsss ; do
for date in 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 ; do
for month in 01 02 03 04 05 06 07 08 09 10 11 12; do
for day in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31; do
ncpdq -U ${dirin}/FC_SF/${date}/${month}/${var}.${date}${month}${day}* ${dirtemp}/${var}.${date}${month}${day}.temp.GLOBAL_075.nc
done
ncrcat ${dirtemp}/${var}.${date}${month}*temp*  ${dirtemp}/${var}.${date}${month}.fshei.GLOBAL_075.nc
\rm ${dirtemp}/${var}.${date}${month}*temp*
ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,0,,5 ${dirtemp}/${var}.${date}${month}.fshei.GLOBAL_075.nc ${dirtemp}/temp_time0.nc
ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,4,,5 ${dirtemp}/${var}.${date}${month}.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}_${date}${month}.nc
\rm ${dirtemp}/temp_time*
\rm ${dirtemp}/${var}.${date}${month}.fshei.GLOBAL_075.nc
done
ncrcat ${dirtemp}/temp_int_${var}_${date}* /bdd/IPCC/Reanalysis/ERAI/${reg}/erai_${reg}_1d_${date}0101_${date}1231_${var}_gridOrig.nc
\rm ${dirtemp}/temp_int_${var}_${date}*
done
done


for var in geopt msl sstk t2 d2 u10 v10 ; do
for date in 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010; do
for month in 01 02 03 04 05 06 07 08 09 10 11 12; do

ncpdq -U ${dirin}/AN_SF/${date}/${var}.${date}${month}.ashei.GLOBAL_075.nc ${dirtemp}/${var}.${date}${month}.ashei.GLOBAL_075.nc
ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,0,,4 ${dirtemp}/${var}.${date}${month}.ashei.GLOBAL_075.nc ${dirtemp}/temp_time1.nc
ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,1,,4 ${dirtemp}/${var}.${date}${month}.ashei.GLOBAL_075.nc ${dirtemp}/temp_time2.nc
ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,2,,4 ${dirtemp}/${var}.${date}${month}.ashei.GLOBAL_075.nc ${dirtemp}/temp_time3.nc
ncks -d lat,${minlat},${maxlat} -d lon,${minlon},${maxlon} -d time,3,,4 ${dirtemp}/${var}.${date}${month}.ashei.GLOBAL_075.nc ${dirtemp}/temp_time4.nc
ncea ${dirtemp}/temp_time* ${dirtemp}/temp_int_${date}${month}.nc
\rm ${dirtemp}/temp_time*
\rm ${dirtemp}/${var}.${date}${month}.ashei.GLOBAL_075.nc
done
ncrcat ${dirtemp}/temp_int_${date}* /bdd/IPCC/Reanalysis/ERAI/${reg}/erai_${reg}_1d_${date}0101_${date}1231_${var}_gridOrig.nc

\rm ${dirtemp}/temp_int_${date}*
done
done
