#! /bin/sh
#+
#
# .. program:: get_pirata_netcdf.sh
#
# .. _get_pirata_netcdf.sh:
#
# ====================
# get_pirata_netcdf.sh
# ====================
#
# SYNOPSIS
# ========
#
# ::
#
#  $ get_pirata_netcdf.sh
#
# DESCRIPTION
# ===========
#
# ``get_pirata_netcdf.sh`` get PIRATA 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_pirata_netcdf.log.{YYYYMMDDTHHMMSSZ}`
#
#     .. graphviz::
#
#        digraph get_pirata_netcdf {
#
#           pirata_ref [shape=diamond,fontname=Courier,label="ftp://ftp.pmel.noaa.gov/cdf/sites/daily/*_*_dy.cdf"];
#
#           file_pirata  [shape=ellipse,fontname=Courier,label="${PROJECT_ID}/*_*_dy.cdf"];
#
#           get_pirata_netcdf [shape=box,
#           fontname=Courier,
#           color=blue,
#           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/get_pirata_netcdf.sh"
#           label="${PROJECT}/src/get_pirata_netcdf.sh"];
#
#           {pirata_ref} -> {get_pirata_netcdf} -> {file_pirata}
#
#          }
#
# EXAMPLES
# ========
#
# You don't have any PIRATA reference data, you just have to run this tool ::
#
#  $ get_pirata_netcdf.sh
#
# And look at log file with ::
#
#  $ tlogd.sh get_pirata_netcdf
#
# and of course on files in ${PROJECT_ID}.
#
# SEE ALSO
# ========
#
# :ref:`guide data PIRATA <data_in_pirata>`
#
# :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 20110421T125622Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * consolidation
#
# - fplod 20101213T160729Z 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="          0n0e"
sitelist="${sitelist} 10s10w 6s10w 0n10w"
sitelist="${sitelist} 0n23w 4n23w 12n23w 21n23w"
sitelist="${sitelist} 19s34w 14s32w 8s30w 0n35w"
sitelist="${sitelist} 4n38w 8n38w 12n38w 15n38w 20n38w"
#
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
