#! /bin/sh
#+
#
# .. program:: tropflux.sh
#
# .. _tropflux.sh:
#
# ===========
# tropflux.sh
# ===========
#
# SYNOPSIS
# ========
#
# ::
#
#  $ tropflux.sh -b yyyymmdd -e yyyymmdd
#
# DESCRIPTION
# ===========
#
# Run all needed :file:`*.pro` to create the TropFlux dataset between two dates
#
# We suppose here that all inputs data are already available localy.
#
# .. option:: -b beginning date <yyyymmdd>
# .. option:: -e end date <yyyymmdd>
#
# Some IDL programs are launched here using ${IDL_CMD} command
# (see :ref:`project_profile.sh` to know how to set this environnement variable).
#
# Log file is written on
# :file:`${PROJECT_LOG}/tropflux.log.{YYYYMMDDTHHMMSSZ}`
#
# .. only:: man
#
#    Figure is visible on PDF and HTML documents only.
#
# .. only:: html or latex
#
#     .. graphviz::
#
#        digraph tropflux {
#
#           file_oaflux [shape=ellipse,fontname=Courier,label="${PROJECT_ID}/lh_oaflux_2004.nc"];

#           log [shape=ellipse,fontname=Courier,label="${PROJECT_LOG}/tropflux.log{YYYYMMDDTHHMMSSZ}"];
#
#           tropflux [shape=box,
#           fontname=Courier,
#           color=blue,
#           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/tropflux.sh",
#           label="${PROJECT}/src/tropflux.sh"];
#
#           {file_oaflux} -> {tropflux} -> {log fileout}
#
#         }
#
# EXAMPLES
# ========
#
# You already used :ref:`get_erai.sh` to get ERAI data, etc.,
# during [20000101, 20000302[ and you want to produce NetCDF files::
#
#   $ tropflux.sh -b 20000101 -e 20000302
#
# A more global example::
#
#   $ tropflux.sh -b 19890101 -e 20091231
#
# And look at log file with ::
#
#  $ tlogd.sh tropflux
#
# and of course on files in ${PROJECT_ID}.
#
# SEE ALSO
# ========
#
# IDL_
#
# .. _IDL: http://www.ittvis.com/ProductServices/IDL.aspx
#
# :ref:`project_profile.sh`
# :ref:`project_init.pro`
#
# Previous step : :ref:`get_erai.sh`, :ref:`get_oaflux.sh`, :ref:`get_olr.sh`,
# :ref:`get_pirata_netcdf.sh`, :ref:`get_swr.sh`
#
# Use :
# :func:`oaflux_mask_30n30s`, :func:`interp_erai_t2m`, 
# :func:`interp_erai_msl`, :func:`interp_erai_dewt`, :func:`interp_erai_lwr`,
# etc.
#
# Next step : +validation ++ comparison
#
# .. todo::
#
# TODO
# ====
#
# make it work
#
# idl status = 0 even if not ok
#
# test if demo mode idl
#
# EVOLUTIONS
# ==========
#
# $Id$
#
# $URL$
#
# - pinsard 20120319
#
#   * add project_overwrite usage
#   * handling error of inter_erai_msl (now function)
#   * handling error of inter_erai_dewt (now function)
#   * handling error of inter_erai_lwr (now function)
#
# - pinsard 20120306
#
#   * create a pro IDL program to introduce LOGICAL_PREDICATE
#     thanks to http://www.idlcoyote.com/code_tips/bitwiselogical.html
#
# - pinsard 20120305
#
#   * creation (draft)
#   * handling error of oaflux_mask_30n30s (now a function)
#
#-
system=$(uname)
case "${system}" in
   AIX|IRIX64)
     echo "${command} : www : no specific posix checking"
     date_cmd=date
   ;;
   Darwin)
     set -o posix
     date_cmd=gdate
   ;;
   Linux)
     set -o posix
     date_cmd=date
   ;;
   *)
    set -o posix
   ;;
esac
unset system
#
set -u
#
LANG=POSIX
#
command=$(basename ${0})
log_date=$(date -u +"%Y%m%dT%H%M%SZ")
#
usage=" Usage : ${command} [-f] -b yyyymmdd -e yyyymmdd"
#
hostname=$(hostname)
#
yyyymmddb_min=19790101
yyyymmdde_max=20110930
#
# default
debug=0
yyyymmddb=20000101
yyyymmdde=20000302
#
minargcount=4
if [ ${#} -lt ${minargcount} ]
then
   echo "${command} : eee : not enought arguments"
   echo "${usage}"
   exit 1
fi
unset minargcount
#
while [ ${#} -gt 0 ]
do
   case ${1} in
     -b)
        # first date to process
        yyyymmddb=${2}
        shift
     ;;
     -e)
        # last date to process
        yyyymmdde=${2}
        shift
     ;;
     -h)
        echo "${usage}"
        exit 0
     ;;
     *)
        # other choice
        echo "${command} : eee : unknown option ${1}"
        echo "${usage}"
        exit 1
     ;;
   esac
   # next flag
   shift
done
unset usage
#
# check parameters
# and define associated variables
${date_cmd} -d "${yyyymmddb}" > /dev/null
status_date=${?}
if [ ${status_date} -ne 0 ]
then
    echo "${command} : eee : yyyymmddb ${yyyymmddb} argument invalid"
    exit 1
fi
unset status_date
#
if [[ "${yyyymmddb}" < "${yyyymmddb_min}" ]]
then
    echo "${command} : eee : yyyymmddb ${yyyymmddb} must be equal or greater than yyyymmddb_min ${yyyymmddb_min}"
    exit 1
fi
#
${date_cmd} -d "${yyyymmdde}" > /dev/null
status_date=${?}
if [ ${status_date} -ne 0 ]
then
    echo "${command} : eee : yyyymmdde ${yyyymmdde} argument invalid"
    exit 1
fi
unset status_date
#
if [[ "${yyyymmdde}" > "${yyyymmdde_max}" ]]
then
    echo "${command} : eee : yyyymmdde ${yyyymmdde} must be lower or equal to yyyymmdde_max ${yyyymmdde_max}"
    exit 1
fi
#
if [[ "${yyyymmdde}" < "${yyyymmddb}" ]]
then
    echo "${command} : eee : yyyymmdde ${yyyymmdde} must be greater than yyyymmddb ${yyyymmddb}"
    exit 1
fi
#
if [[ "${yyyymmdde}" = "${yyyymmddb}" ]]
then
     echo "${command} : eee : yyyymmdde ${yyyymmdde} must be greater than yyyymmddb ${yyyymmddb}"
     exit 1
fi
#
tool=${IDL_CMD}
type ${tool} 1> /dev/null 2>&1
status=${?}
if [ ${status} -ne 0 ]
then
   echo "${command} : eee : tool ${IDL_CMD} not found"
   echo "${command} : eee : check project_profile.sh sequence"
   exit 1
fi
unset status
unset tool
#
# check for write permission on PROJECT_LOG
if [ ! -w ${PROJECT_LOG} ]
then
   echo "${command} : eee : ${PROJECT_LOG} not writable"
   exit 1
fi
#
log=${PROJECT_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}
echo "IDL_CMD=${IDL_CMD}" 1>> ${log}
unset log_date
echo "" 1>> ${log}
echo "[Parameters]" 1>> ${log}
echo "yyyymmddb=${yyyymmddb}" 1>> ${log}
echo "yyyymmdde=${yyyymmdde}" 1>> ${log}
echo "" 1>> ${log}
#
cat >> ${PROJECT}/src/tropflux_${$}.pro << EOF
pro tropflux_${$}
compile_opt idl2, strictarrsubs, logical_predicate
@cm_project
;
; define overwrite status
; set to 1 to overwrite existing files
project_overwrite = 0
;
result = oaflux_mask_30n30s()
IF result < 0 THEN BEGIN
   msg = 'eee : pb after oaflux_mask_30n30s' + string(result)
   err = report(msg)
   exit
ENDIF
result = interp_erai_dewt(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
   msg = 'eee : pb after interp_erai_dewt' + string(result)
   err = report(msg)
   exit
ENDIF
result = interp_erai_lwr(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
   msg = 'eee : pb after interp_erai_lwr' + string(result)
   err = report(msg)
   exit
ENDIF
result = interp_erai_msl(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
   msg = 'eee : pb after interp_erai_msl' + string(result)
   err = report(msg)
   exit
ENDIF
;interp_erai_sst_1989_2009
result = interp_erai_t2m(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
   msg = 'eee : pb after interp_erai_t2m' + string(result)
   err = report(msg)
   exit
ENDIF
;interp_erai_ws_1989_2009
;interp_olr_30n30s_1989_2009
d2m_to_q2m_erai 
;++Program caused arithmetic error: Floating overflow
;++ Program caused arithmetic error: Floating illegal operand
;.compile TropFlux_swr_DT_19890101_20071231
;tropflux_swr_dt_19890101_20071231
;.compile TropFlux_swr_NRT_19890101_20091231
;tropflux_swr_nrt_19890101_20091231
;.compile TropFlux_swr_BLND_19890101_20091231
;tropflux_swr_blnd_19890101_20091231
;lwr_correction_ncdf
;q2m_correction_ncdf
;sst_correction_ncdf
;t2m_correction_ncdf
;ws_correction_ncdf
;cronin_gustiness_ncdf
;.compile TropFlux_19890101_20091231
;tropflux_19890101_20091231
;.compile TropFlux_NRT_ncdf
;tropflux_nrt_ncdf
end
EOF
#
# run IDL or equivalent
${IDL_CMD} << EOF >> ${log} 2>&1
.compile file_interp
;
tropflux_${$}
EOF
status=${?}
if [ ${status} -ne 0 ]
then
   echo "${command} : eee: ${IDL_CMD} failed : ${status}" >> ${log} 2>&1
   cat ${PROJECT}/src/tropflux_${$}.pro >> ${log} 2>&1
   exit 1
fi
rm ${PROJECT}/src/tropflux_${$}.pro
unset status
#
unset command
unset log
unset hostname
unset usage
#
unset date_cmd
#
# end
#++set
exit
