#! /bin/sh
#+
#
# .. program:: tropflux.sh
#
# .. _tropflux.sh:
#
# ===========
# tropflux.sh
# ===========
#
# SYNOPSIS
# ========
#
# .. code-block:: bash
#
#    tropflux.sh [-f] -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 locally.
#
# .. option:: -b beginning date <yyyymmdd>
# .. option:: -e end date <yyyymmdd>
# .. option:: -f to overwrite existing files
#
# Some IDL programs are launched here using ${IDL_CMD} command
# (see :ref:`project_profile.sh` to know how to set this
# environment 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:
#
# .. code-block:: bash
#
#    tropflux.sh -b 20000101 -e 20000302
#
# A more global example:
#
# .. code-block:: bash
#
#    tropflux.sh -b 19890101 -e 20091231
#
# And look at log file with :
#
# .. code-block:: bash
#
#    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 :
# :ref:`concat_eraiy.sh`,
# :func:`oaflux_mask_30n30s`, :func:`interp_erai_t2m`,
# :func:`interp_erai_msl`, :func:`interp_erai_dewt`, :func:`interp_erai_lwr`,
# :func:`interp_erai_sst`, :func:`interp_erai_ws`, :func:`interp_olr_30n30s`,
# :func:`d2m_to_q2m_erai`, :func:`sst_correction_ncdf`,
# :func:`lwr_correction_ncdf`, :func:`q2m_correction_ncdf`,
# :func:`t2m_correction_ncdf`, :func:`ws_correction_ncdf`,
# :func:`tropflux_swr_dt`, :func:`tropflux_swr_nrt`, :func:`tropflux_swr_blnd`,
# :func:`tropflux_nrt_ncdf`,:func:`tropflux_wind_stress`
# etc.
#
# Next step : +validation ++ comparison
#
# .. todo::
#
# TODO
# ====
#
# make it work
#
# if yyyymmdd < 20071231, is it meanningfull to call all the functions ?
#
# idl status = 0 even if not ok
#
# test if demo mode idl
#
# EVOLUTIONS
# ==========
#
# $Id$
#
# $URL$
#
# - fplod 20120704T123711Z cratos (Linux)
#
#   * typo
#   * add usage of concat_eraiy.sh
#   * really implement -f option
#
# - pinsard 20120329
#
#   * add tropflux_wind_stress (to be consolidated)
#
# - pinsard 20120326
#
#   * TropFlux_NRT_ncdf replace by tropflux_nrt_ncdf which is now a function
#
# - pinsard 20120322
#
#   * handling error of sst_correction_ncdf (now function)
#   * handling error of lwr_correction_ncdf (now function)
#   * handling error of q2m_correction_ncdf (now function)
#   * handling error of t2m_correction_ncdf (now function)
#   * handling error of ws_correction_ncdf (now function)
#
# - pinsard 20120321
#
#   * TropFlux_swr_DT_19890101_20071231 replace by tropflux_swr_dt
#     which is now a function
#   * TropFlux_swr_NRT_19890101_20071231 replace by tropflux_swr_nrt
#     which is now a function
#   * TropFlux_swr_BLND_19890101_20071231 replace by tropflux_swr_blnd
#     which is now a function
#   * correction of messages when return badly
#   * reorder for gustiness before tropflux_swr_blnd
#   * cronin_gustiness_ncdf is now a function
#
# - pinsard 20120320
#
#   * handling error of interp_erai_sst (now function)
#   * handling error of interp_erai_ws (now function)
#   * handling error of interp_olr_30n_30s (now function)
#   * handling error of d2m_to_q2m_erai (now function)
#
# - pinsard 20120319
#
#   * add project_overwrite usage
#   * handling error of interp_erai_msl (now function)
#   * handling error of interp_erai_dewt (now function)
#   * handling error of interp_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
force=0
yyyymmddb=20000101
yyyymmdde=20000302
#
minargcount=4
if [ ${#} -lt ${minargcount} ]
then
    echo "${command} : eee : not enough 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
        ;;
        -f)
            force=1
        ;;
        -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 "log=${log}" 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}
#
# prepare era-i files
concat_eraiy.sh -b ${yyyymmddb} -e ${yyyymmdde}
# ++ test if ok see TODO in concat_eraiy.sh
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 = ${force}
; define eraitype
eraitype=2
;
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}, eraitype)
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}, eraitype)
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}, eraitype)
IF result < 0 THEN BEGIN
    msg = 'eee : pb after interp_erai_msl ' + string(result)
    err = report(msg)
    exit
ENDIF
result = interp_erai_sst(${yyyymmddb}, ${yyyymmdde}, eraitype)
IF result < 0 THEN BEGIN
    msg = 'eee : pb after interp_erai_sst ' + string(result)
    err = report(msg)
    exit
ENDIF
result = interp_erai_t2m(${yyyymmddb}, ${yyyymmdde}, eraitype)
IF result < 0 THEN BEGIN
    msg = 'eee : pb after interp_erai_t2m ' + string(result)
    err = report(msg)
    exit
ENDIF
result = interp_erai_ws(${yyyymmddb}, ${yyyymmdde}, eraitype)
IF result < 0 THEN BEGIN
    msg = 'eee : pb after interp_erai_ws ' + string(result)
    err = report(msg)
    exit
ENDIF
result = interp_olr_30n30s(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
    msg = 'eee : pb after interp_olr_30n30s ' + string(result)
    err = report(msg)
    exit
ENDIF
result = d2m_to_q2m_erai(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
    msg = 'eee : pb after d2m_to_q2m_erai ' + string(result)
    err = report(msg)
    ;++Program caused arithmetic error: Floating overflow
    ;++ Program caused arithmetic error: Floating illegal operand
    ;++exit
ENDIF
result = tropflux_swr_dt(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
    msg = 'eee : pb after tropflux_swr_dt ' + string(result)
    err = report(msg)
    exit
ENDIF
result = tropflux_swr_nrt(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
    msg = 'eee : pb after tropflux_swr_nrt ' + string(result)
    err = report(msg)
    exit
ENDIF
result = sst_correction_ncdf(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
    msg = 'eee : pb after sst_correction_ncdf ' + string(result)
    err = report(msg)
    exit
ENDIF
result = cronin_gustiness_ncdf(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
    msg = 'eee : pb after cronin_gustiness_ncdf ' + string(result)
    err = report(msg)
    exit
ENDIF
result = tropflux_swr_blnd(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
    msg = 'eee : pb after tropflux_swr_blnd ' + string(result)
    err = report(msg)
    exit
ENDIF
result = lwr_correction_ncdf(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
    msg = 'eee : pb after lwr_correction_ncdf ' + string(result)
    err = report(msg)
    exit
ENDIF
result = q2m_correction_ncdf(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
    msg = 'eee : pb after q2m_correction_ncdf ' + string(result)
    err = report(msg)
    exit
ENDIF
result = t2m_correction_ncdf(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
    msg = 'eee : pb after t2m_correction_ncdf ' + string(result)
    err = report(msg)
    exit
ENDIF
result = ws_correction_ncdf(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
    msg = 'eee : pb after ws_correction_ncdf ' + string(result)
    err = report(msg)
    exit
ENDIF
result = tropflux(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
    msg = 'eee : pb after tropflux' + string(result)
    err = report(msg)
    exit
ENDIF
result = tropflux_nrt_ncdf(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
    msg = 'eee : pb after tropflux_nrt_ncdf' + string(result)
    err = report(msg)
    exit
ENDIF
result = tropflux_wind_stress(${yyyymmddb}, ${yyyymmdde})
IF result < 0 THEN BEGIN
    msg = 'eee : pb after tropflux_wind_stress' + string(result)
    err = report(msg)
    exit
ENDIF
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
