#! /bin/sh
#
#+
#
# .. program:: change_time_range.sh
#
# .. _change_time_range.sh:
#
# =======================
# change_time_range.sh
# =======================
#
# SYNOPSIS
# ========
#
# .. code-block:: bash
#
#    change_time_range.sh [--debug] -b yyyymmddb -e yyyymmdde -d directory
#
# DESCRIPTION
# ===========
#
# .. option:: --debug
#
#    If this option is set, :samp:`ncdump -v tt` will be added to log file
#
# .. option:: -b <yyyymmddb>
#
#    first date to be written in the global attributes time_range
#
# .. option:: -e <yyyymmdde>
#
#    second date to be written in the global attributes time_range
#
# .. option:: -d <dirin>
#
#    base location of the dataset to be changed
#
# Modify time_range global attribute in the dataset based under dirin parameter
#
# Log file is written on
# :file:`${PROJECT_LOG}/change_time_range.sh.log.{YYYYMMDDTHHMMSS}Z`.
#
#     .. graphviz::
#
#        digraph change_time_range {
#
#           change_time_range [shape=box,
#           fontname=Courier,
#           color=blue,
#           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/change_time_range.sh",
#           label="${PROJECT}/src/change_time_range.sh"];
#
#           {filein} -> {change_time_range} -> {filein}
#
#       }
#
# EXAMPLES
# ========
#
# To modify files under /usr/lodyc/incas/fplod/tropflux_d/to_be_published/:
#
# .. code-block:: bash
#
#    change_time_range.sh -d /usr/lodyc/incas/fplod/tropflux_d/to_be_published/ -b 19790101 -e 20130331
#
# And look at log file with :
#
# .. code-block:: bash
#
#    tlogd.sh change_time_range
#
# TIPS
# ====
#
# SEE ALSO
# ========
#
# :ref:`updatedata`
#
# :ref:`project_profile.sh`
#
# :func:`ncatted <nco:ncatted>`
#
# TODO
# ====
#
# coding rules
#
# overwrite : may be an other option to avoid scratch
#
# debug not used
#
# terminology 1979_2013 hardcoded
#
# build time period according to time in the file instead of parameter
#
# EVOLUTIONS
# ==========
#
# $Id$
#
# $URL$
#
# - fplod 20130726T112836Z cratos.locean-ipsl.upmc.fr (Linux)
#
#   * 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
#
set -u
#
command=$(basename ${0})
log_date=$(date -u +"%Y%m%dT%H%M%SZ")
#
usage=" Usage : ${command} [--debug] -d dirin -b yyyymmddb -e yyyymmdde"
#
hostname=$(hostname)
#
# default
debug=0
#
minargcount=0
if [ ${#} -lt ${minargcount} ]
then
    echo "${command} : eee : not enough arguments"
    echo "${usage}"
    exit 1
fi
#
while [ ${#} -gt 0 ]
do
    case ${1} in
        -b)
            yyyymmddb=${2}
            shift
        ;;
        -e)
            yyyymmdde=${2}
            shift
        ;;
        -d)
            dirin=${2}
            shift
        ;;
        --debug)
            debug=1
        ;;
        *)
            # anything else
            echo "${command} : eee : unknown option ${1}"
            echo "${command} : eee : ${usage}"
            exit 1
        ;;
    esac
    # next flag
    shift
done
#
# check parameters
if [ ! -d ${dirin} ]
then
    echo " eee : ${dirin} not found"
    exit 1
fi
#
# check for permission on dirin
if [ ! -x ${dirin} ]
then
    echo " eee : ${dirin} not reachable"
    exit 1
fi
#++ check yyyymmddb and yyyymmddb validity
#
tool=ncatted
type ${tool} 1> /dev/null 2>&1
status=${?}
if [ ${status} -ne 0 ]
then
    echo "${command} : ${LINENO} : eee : tool ${tool} not found"
    exit 1
fi
unset status
unset tool
#
# check for ${PROJECT_LOG} definition
if [ "${PROJECT_LOG}" = "" ]
then
    echo "${command} : ${LINENO} : 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}
echo "[Parameters]" 1>> ${log}
echo "dirin=${dirin}" 1>> ${log}
echo "yyyymmddb=${yyyymmddb}" 1>> ${log}
echo "yyyymmdde=${yyyymmdde}" 1>> ${log}
echo "" 1>> ${log}
#
# build list files
varlist="lhf lwr netflux q2m shf sst swr t2m tau taux tauy ws"
period_list="daily monthly"
list_filein=""
for var in ${varlist}
do
    for period in ${period_list}
    do
        if [ "${period}" == "daily" ]
        then
            suffix=1d
        else
            suffix=1m
        fi
        filein=${dirin}/${period}/${var}_tropflux_${suffix}_1979_2013.nc
        if [ ! -f ${filein} ]
        then
            echo " eee : ${filein} not found" 1>> ${log}
            exit 1
        else
            list_filein=${list_filein}" ${filein}"
        fi
    done
    unset period
done
unset period_list
unset var
unset varlist
#
# build time period
datesec=$(${date_cmd} -u -d "${yyyymmddb}" +%s | awk '{printf "%10.10d",$1}')
yyyyb=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%Y)
mmb=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%m)
ddb=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%d)
unset datesec
datesec=$(${date_cmd} -u -d "${yyyymmdde}" +%s | awk '{printf "%10.10d",$1}')
yyyye=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%Y)
mme=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%m)
dde=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%d)
unset datesec
time_ranged="${yyyymmddb} - ${yyyymmdde}"
time_rangem="${yyyyb}${mmb}15 - ${yyyye}${mme}15"
#
# new time_range attribute
for filein in ${list_filein}
do
    echo "iii : change time_range in ${filein}" >> ${log} 2>&1
    period=$(basename $(dirname ${filein}))
    if [ "${period}" == "daily" ]
    then
        time_range=${time_ranged}
    else
        time_range=${time_rangem}
    fi
    ncatted -O -h -a time_range,global,o,c,"${time_range}" ${filein} >> ${log} 2>&1
    status=${?}
    if [ ${status} -ne 0 ]
    then
        echo "eee : pb with ncatted" >> ${log} 2>&1
        ncdump -h ${filein} >> ${log} 2>&1
        exit 1
    fi
    unset status
    ncdump -h ${filein}  | grep time_range >> ${log} 2>&1
done
unset filein
#
# cleaning
#++
#++ set
# end
exit 0
