#! /bin/sh
#+
#
# .. program:: get_tao_netcdf.sh
#
# .. _get_tao_netcdf.sh:
#
# =================
# get_tao_netcdf.sh
# =================
#
# SYNOPSIS
# ========
#
# ::
#
#  $ get_tao_netcdf.sh
#
# DESCRIPTION
# ===========
#
# ``get_tao_netcdf.sh`` get TAO reference files
#
# bf
#   buoyancy flux
# d
#   sigma-theta
# dyn
#   dynamic height
# emp
#   e-p
# iso
#   20C depth
# met
#   wind (u,v,speed,direction),  airT, hum rel, SST   (= w + airt + rh)
# pos
#   lon, lat
# rad
#   shortwave down
# rf
#   rain heat flux
# w
#   wind
#
# Log file is written on :file:`${PROJECT_LOG}/get_tao_netcdf.log.{YYYYMMDDTHHMMSSZ}`
#
#     .. graphviz::
#
#        digraph get_tao_netcdf {
#
#           tao_ref [shape=diamond,fontname=Courier,label="ftp://ftp.pmel.noaa.gov/cdf/sites/daily/*_*_dy.cdf"];
#
#           file_tao  [shape=ellipse,fontname=Courier,label="${PROJECT_ID}/*_*_dy.cdf"];
#
#           get_tao_netcdf [shape=box,
#           fontname=Courier,
#           color=blue,
#           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/get_tao_netcdf.sh"
#           label="${PROJECT}/src/get_tao_netcdf.sh"];
#
#           {tao_ref} -> {get_tao_netcdf} -> {file_tao}
#
#          }
#
# EXAMPLES
# ========
#
# You don't have any TAO reference data, you just have to run this tool ::
#
#  $ get_tao_netcdf.sh
#
# And look at log file with ::
#
#  $ tlogd.sh get_tao_netcdf
#
# and of course on files in ${PROJECT_ID}.
#
# SEE ALSO
# ========
#
# :ref:`guide data TAO <data_in_tao>`
#
# :ref:`project_profile.sh`
#
# TODO
# ====
#
# list of variables and sites in DESCRIPTION
#
# do we really need all these variables and sites
#
# study wget status (no exit now on non null wget status)
#
# EVOLUTIONS
# ==========
#
# $Id$
#
# - fplod 20110421T154046Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * consolidation
#
# - fplod 20101213T161152Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * minimal header
#
# - jv 2008
#
#   * 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
#
# 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}
unset log_date
echo "" 1>> ${log}
#
varlist="adcp airt bf bp cur d dyn emp evap heat iso lw lwnet met pos qlat qnet qsen rad rain rf rh s ssd sss sst swnet t tau w"
#
sitelist="                                        2n137e 5n137e 8n137e"
sitelist="${sitelist}                      0n147e 2n147e 5n147e"
sitelist="${sitelist}        5s156e 2s156e 0n156e 2n156e 5n156e 8n156e"
sitelist="${sitelist} 8s165e 5s165e 2s165e 0n165e 2n165e 5n165e 8n165e"
sitelist="${sitelist} 8s180w 5s180w 2s180w 0n180w 2n180w 5n180w 8n180w"
sitelist="${sitelist} 8s170w 5s170w 2s170w 0n170w 2n170w 5n170w 8n170w"
sitelist="${sitelist} 8s155w 5s155w 2s155w 0n155w 2n155w 5n155w 8n155w"
sitelist="${sitelist}        5s140w 2s140w 0n140w 2n140w 5n140w 9n140w"
sitelist="${sitelist} 8s125w 5s125w 2s125w 0n125w 2n125w 5n125w 8n125w"
sitelist="${sitelist} 8s110w 5s110w 2s110w 0n110w 2n110w 5n110w 8n110w"
sitelist="${sitelist} 8s95w  5s95w  2s95w  0n95w  2n95w  5n95w  8n95w "
#
locref="ftp://ftp.pmel.noaa.gov/cdf/sites/daily"
#
list_url=""
#
# build URL = f(site,var)
for site in ${sitelist}
do
   for var in ${varlist}
   do
       list_url="${list_url} ${locref}/${var}${site}_dy.cdf"
   done
done
unset site
unset var
unset varlist
unset sitelist
unset locref
#
# if file is not already in ${PROJECT_ID}, get it
for url in ${list_url}
do
  file=${PROJECT_ID}/$(basename ${url})
  if [ ! -f ${file} ]
  then
     wget --tries=1 --no-verbose -P ${PROJECT_ID} \
        --user=taopmelftp \
        --password=G10b@LCh@Ng3 \
        ${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} exists" >> ${log}
  fi
done
unset file
unset url
unset list_url
#
unset command
unset log
unset hostname
unset usage
#
# end
set
exit 0
