#! /bin/sh
#+
#
# .. program:: get_oaflux.sh
#
# .. _get_oaflux.sh:
#
# =============
# get_oaflux.sh
# =============
#
# SYNOPSIS
# ========
#
# .. code-block:: bash
#
#    get_oaflux.sh
#
# DESCRIPTION
# ===========
#
# Put in ${PROJECT_ID} OAFLUX reference file
#
# Once this tool executed :func:`oaflux_mask_30n30s` can be launched.
#
# Log file is written on
# :file:`${PROJECT_LOG}/get_oaflux.log.{YYYYMMDDTHHMMSS}Z`.
#
#     .. graphviz::
#
#        digraph get_oaflux {
#
#           oaflux_ref [shape=diamond,
#           fontname=Courier,
#           label="ftp://ftp.whoi.edu/pub/science/oaflux/data_v3/monthly/turbulence/lh_oaflux_2004.nc.gz"];
#
#           file_oaflux [shape=ellipse,
#           fontname=Courier,
#           label="${PROJECT_ID}/lh_oaflux_2004.nc"];
#
#           get_oaflux [shape=box,
#           fontname=Courier,
#           color=blue,
#           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/get_oaflux.sh"
#           label="${PROJECT}/src/get_oaflux.sh"];
#           get_oaflux [shape=box,
#           fontname=Courier,
#           color=blue,
#           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/get_oaflux.sh"
#           label="${PROJECT}/src/get_oaflux.sh"];
#
#           {oaflux_ref} -> {get_oaflux} -> {file_oaflux}
#
#          }
#
#
# EXAMPLES
# ========
#
# If you are working on LOCEAN machine and you don't have any reference data,
# you just have to run this tool :
#
# .. code-block:: bash
#
#    get_oaflux.sh
#
# And look at log file with :
#
# .. code-block:: bash
#
#    tlogd.sh get_oaflux
#
# and of course on files in ${PROJECT_ID}.
#
# TODO
# ====
#
# get
# ftp://ftp.whoi.edu/pub/science/oaflux/data_v3/daily/radiation_1985-2007/sw_isccp_yyyy.nc.gz
# and generate ${PROJECT_ID}/swr_oafluxgrid_1985_2007.nc somewhere else
#
# SEE ALSO
# ========
#
# :ref:`guide data OAFLUX <data_in_oaflux>`
#
# :ref:`project_profile.sh`
#
# :func:`oaflux_mask_30n30s`
#
# EVOLUTIONS
# ==========
#
# - fplod 20110421T125407Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * typo
#
# - fplod 20101216T152647Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * creation
#
#-
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
#
# test if wget available
tool=wget
type ${tool} 1> /dev/null 2>&1
status=${?}
if [ ${status} -ne 0 ]
then
    echo "${command} : eee : tool ${tool} not found"
    exit 1
fi
unset status
unset tool
#
# test if gunzip available
tool=gunzip
type ${tool} 1> /dev/null 2>&1
status=${?}
if [ ${status} -ne 0 ]
then
    echo "${command} : eee : tool ${tool} not found"
    exit 1
fi
unset status
unset tool
#
# check for ${PROJECT_LOG} definition
if [ "${PROJECT_LOG}" = "" ]
then
    echo "${command} : eee : \${PROJECT_LOG} not defined"
    exit 1
fi
#
# check for ${PROJECT_LOG} existence
if [ ! -d ${PROJECT_LOG} ]
then
    echo "${command} : eee : ${PROJECT_LOG} not found"
    exit 1
fi
#
# check for permission access on PROJECT_LOG
if [ ! -x ${PROJECT_LOG} ]
then
    echo "${command} : eee : ${PROJECT_LOG} not reachable"
    exit 1
fi
#
# 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}
unset log_date
echo "" 1>> ${log}
#
fileref=lh_oaflux_2004.nc
fileref_gz=${fileref}.gz
locref=ftp://ftp.whoi.edu/pub/science/oaflux/data_v3/monthly/turbulence/
if  [ -f ${PROJECT_ID}/${fileref} ]
then
    echo "${command} : iii : ${PROJECT_ID}/${fileref} exist" 1>> ${log}
    echo "${command} : iii : nothing done" 1>> ${log}
else
    wget --tries=1 --no-verbose -P ${PROJECT_ID} ${locref}/${fileref_gz} 1>> ${log} 2>&1
    wget_status=${?}
    if [ ${wget_status} -ne 0 ]
    then
        echo "${command} : eee : ${locref}/${fileref_gz} not found" >> ${log} 2>&1
        # exit 1 #++
    else
        echo "${command} : iii : ${locref}/${fileref_gz} found" >> ${log} 2>&1
    fi
    unset wget_status
    #
    # decompress
    gunzip ${PROJECT_ID}${fileref_gz}
    gunzip_status=${?}
    if [ ${gunzip_status} -ne 0 ]
    then
        echo "${command} : eee : ${locref}/${fileref_gz} not found" >> ${log} 2>&1
        # exit 1 #++
    else
        echo "${command} : iii : ${locref}/${fileref_gz} found" >> ${log} 2>&1
    fi
fi
unset fileref
unset fileref_gz
unset locref
#
unset command
unset log
unset hostname
#
# end
exit 0
