#! /bin/sh
#
#+
# NAME
# ====
#
# refdataget.sh - get all reference data
#
# SYNOPSIS
# ========
#
# ``refdataget.sh``
#
# DESCRIPTION
# ===========
#
# Put in ${IRCAAM_ID} all the reference data files 
#
# This includes :
#
# - ARPEGE data 
#   4 parameters (rlut_d zg_d pr_d) in 5 simulations experiences
#   (CtIV CtCl AfNQIVIV AsNQIVIV TrNQIVIV) between 1971 and 2000.
#
#   You will need password from authorized people to access to ARPEGE 
#   simulation files.
#
# - OLR observation between 1974 and now.
#
# If one of these files already exists (same name), it won't be download again
# to avoid big transfert.
#
# It might be usefull to check revision of original data ...
#
#
# EXAMPLES
# ========
#
# If you are working on LOCEAN machine and you don't have any reference data, 
# you just have to run this tool :
# ::
#
# $ refdataget.sh
# 
#
# And look at log file wtih :
# ::
#
# $ tlogd.sh refdataget
# 
# and of course on files in ${IRCAAM_ID}.
#
# CAUTIONS
# ========
#
# how to know if the is enough place before ?
#
# SEE ALSO
# ========
#
# ircaam_profile.sh_
#
# .. _ircaam_profile.sh: ircaam_profile.sh.html
#
# FILES
# =====
#
# /usr/home/fplod/incas/ircaam/ircaam_ws/src/refdataget.sh sur aedon.locean-ipsl.upmc.fr
#
# TODO
# ====
#
# ++ les fichiers de benjamin
#
# ++ la carte du monde
#
# ++ pb avec la saisie du mot de passe
#
# ++ et si je travaille sur une machine climserv ?
#
# EVOLUTIONS
# ==========
#
# $Id$
#
# - fplod 2009-01-08T10:40:55Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * creation from varamma_ws/src/paper01_pre.sh
#
#-
system=$(uname)
case "${system}" in
AIX|IRIX64)
 echo " www : no specific posix checking"
;;
*)
 set -o posix
;;
esac
#
command=$(basename ${0})
log_date=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
#
usage=" Usage : ${command}"
#
hostname=$(hostname)
#
set -u
#
# check for ${IRCAAM_LOG} definition
if [ "${IRCAAM_LOG}" = "" ]
then
 echo " eee : \${IRCAAM_LOG} not defined"
 exit 1
fi
#
# check for ${IRCAAM_LOG} existence
if [ ! -d ${IRCAAM_LOG} ]
then
 echo " eee : ${IRCAAM_LOG} not found"
 exit 1
fi
#
# check for permission access on IRCAAM_LOG
if [ ! -x ${IRCAAM_LOG} ]
then
 echo " eee : ${IRCAAM_LOG} not reachable"
 exit 1
fi
#
# check for write permission on IRCAAM_LOG
if [ ! -w ${IRCAAM_LOG} ]
then
 echo " eee : ${IRCAAM_LOG} not writable"
 exit 1
fi
#
log=${IRCAAM_LOG}/$(basename ${0} .sh).log.${log_date}
echo "command = ${command}" 1>> ${log}
echo "hostname = ${hostname}" 1>> ${log}
echo "runtime = ${log_date}" 1>> ${log}
#
# check for write permission on IRCAAM_ID
if [ ! -w ${IRCAAM_ID} ]
then
 echo " eee : ${IRCAAM_ID} not writable"
 exit 1
fi

# ARPEGE
list_param="rlut_d zg_d pr_d"
list_simu="CtIV CtCl AfNQIVIV AsNQIVIV TrNQIVIV" 
year_min=1971
year_max=2000
dirrefb="http://www.cnrm.meteo.fr/dods-ensembles/Datasets/IRCAAM/"
#
read -s -p "enter ircaam-dods password : " dods_passwd
#
for simu in ${list_simu}
do
 case ${simu} in
 CtIV|CtCl|AfNQIVIV)
  dirref="${dirrefb}/Sim/"
 ;;
 AsNQIVIV)
  dirref="${dirrefb}/Sim_As/"
 ;;
 TrNQIVIV)
  dirref="${dirrefb}/Sim_Tr/"
 ;;
 *)
  echo "eee : wrong \${simu}"
  exit 1
 esac
 dirref="${dirref}/${simu}/"
 for param in ${list_param}
 do
  year_i=${year_min}
  while [ ${year_i} -le ${year_max} ]
  do
   year_c=$(echo ${year_i} | awk ' {printf "%4.4d",$0}')
   fileref=${param}.${simu}.${year_c}06-09.nc
   if  [ -f ${IRCAAM_ID}/${fileref} ]
   then
    echo "iii : ${IRCAAM_ID}/${fileref} exist" ${log} 2>&1
    echo "iii : nothing done" >> ${log} 2>&1
   else
    wget --no-verbose --user=ircaam-dods --password=${dods_passwd} \
    -P ${IRCAAM_ID} \
    ${dirref}/${fileref} >> ${log} 2>&1
    # ++ ok ?
    # ++ si climserv faire ln -s ${dirref}/${fileref} ${VARAMMA_ID}/
   fi
   year_i=$(( ${year_i} + 1 ))
  done
 done
done

# OLR
fileref=olr.day.mean.nc
dirref=ftp://ftp.climserv.ipsl.polytechnique.fr/noaa-olr/
if  [ -f ${IRCAAM_ID}/${fileref} ]
then
 echo "iii : ${IRCAAM_ID}/${fileref} exist"  >> ${log} 2>&1
 echo "iii : nothing done" >> ${log} 2>&1
else
 wget --no-verbose -P ${IRCAAM_ID} ${dirref}/${fileref} >> ${log}
 # ++ ok ?
fi
#
# end
exit 0
