#! /bin/sh
#+
#
# .. program:: get_swr.sh
#
# .. _get_swr.sh:
#
# ==========
# get_swr.sh
# ==========
#
# SYNOPSIS
# ========
#
# .. code-block:: bash
#
#    get_swr.sh
#
# DESCRIPTION
# ===========
#
# ``get_swr.sh`` get SWR reference files.
#
# Log file is written on :file:`${PROJECT_LOG}/get_swr.log.{YYYYMMDDTHHMMSSZ}`
#
#     .. graphviz::
#
#        digraph get_swr {
#
#           swr_ref [shape=diamond,
#           fontname=Courier,
#           label="ftp://ftp.whoi.edu/pub/science/oaflux/data_v3/daily/radiation_1985-2007/sw_icssp_yyyy.nc.gz"];
#
#           file_swr [shape=ellipse,
#           fontname=Courier,
#           label="${PROJECT_ID}/sw_icssp_yyyy.nc"];
#
#           get_swr [shape=box,
#           fontname=Courier,
#           color=blue,
#           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/get_swr.sh"
#           label="${PROJECT}/src/get_swr.sh"];
#
#           {swr_ref} -> {get_swr} -> {file_swr}
#
#          }
#
# EXAMPLES
# ========
#
# You don't have any RAMA reference data, you just have to run this tool :
#
# .. code-block:: bash
#
#    get_swr.sh
#
# And look at log file with :
#
# .. code-block:: bash
#
#    tlogd.sh get_swr
#
# and of course on files in ${PROJECT_ID}.
#
# SEE ALSO
# ========
#
# :ref:`data_in_swr`
#
# :ref:`project_profile.sh`
#
# TODO
# ====
#
# complete description
#
# Eventually: we should retrieve them from ISCCP page
# (ftp://isccp.giss.nasa.gov/pub/data/FC/ ) and interpolate them we
# need FD${YYYY}${MM}__${SS}${FF}SFC.EQ files where:
#
# - YYYY and MM are year and month
# - SS is LW (longwave) and SW (shortwave)
#
# study wget status (no exit now on non null wget status)
#
# chain to concatenation and geobox reduction .. and rename sw\_ to swr file
# variable name ?? to swr
#
# EVOLUTIONS
# ==========
#
# $Id$
#
# $URL$
#
# - fplod 20110811T153513Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * creation
#
#-
system=$(uname)
case "${system}" in
    AIX|IRIX64)
        echo "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
#
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
#
# 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}
#
yearmin=1985
yearmax=2007
#
varlist="sw"
#
locref="ftp://ftp.whoi.edu/pub/science/oaflux/data_v3/daily/radiation_1985-2007/"
#
list_url=""
#
# build URL = f(year,var)
yyyy=${yearmin}
while [ ${yyyy} -le ${yearmax} ]
do
    for var in ${varlist}
    do
        list_url="${list_url} ${locref}/${var}_isccp_${yyyy}.nc.gz"
    done
    unset var
    yyyy=$(( ${yyyy} + 1 ))
done
unset varlist
unset locref
#
# if file_gz is not already in ${PROJECT_ID}, get it
for url in ${list_url}
do
    file_gz=${PROJECT_ID}/$(basename ${url})
    file=${PROJECT_ID}/$(basename ${file_gz} .gz)
    if [ ! -f ${file} ]
    then
        if [ ! -f ${file_gz} ]
        then
            wget --tries=1 --no-verbose -P ${PROJECT_ID} \
            ${url} >> ${log} 2>&1
            wget_status=${?}
            if [ ${wget_status} -ne 0 ]
            then
                echo "${command} : eee : ${url} not found" >> ${log} 2>&1
                # ++ exit 1
            else
                echo "${command} : iii : ${url} found" >> ${log} 2>&1
            fi
            unset wget_status
        else
            echo "iii : ${file_gz} exists" >> ${log}
            gunzip -c ${file_gz} > ${file}
        fi
    else
        echo "iii : ${file} exists" >> ${log}
    fi
done
unset file_gz
unset file
unset url
unset list_url
#
unset command
unset log
unset hostname
unset usage
#
# end
set
exit 0
