#! /bin/sh
#
#+
# .. _getmodelout.sh:
#
# ==============
# getmodelout.sh
# ==============
#
# ------------------------------------------
# get model data output file in ${GEOMAG_ID}
# ------------------------------------------
#
# SYNOPSIS
# ========
#
# .. code-block:: bash
#
#    getmodelout.sh -r orcares -exp drakkar_exp -g grid -yearmin yyyy -yearmax yyyy -o list
#
# DESCRIPTION
# ===========
#
# get model data output file in ${GEOMAG_ID}
#
# EXAMPLES
# ========
#
# To get DRAKKAR G42 experiment output on grid T points between year 9 and 10:
#
# .. code-block:: bash
#
#    getmodelout.sh -r ORCA025 -exp G42 -g gridT -yearmin 9 -yearmax 10 -o ${GEOMAG_OD}/list_G42
#
# EVOLUTIONS
# ==========
#
# $Id$
#
# - fplod 20100318T073947Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * unset
#
# - reee522 2007-11-26T11:21:37Z rhodes (IRIX64)
#
#   * creation
#-
#
command=getmodelout.sh
#
usage=" Usage : ${command} -r orcares -exp drakkar_exp -g grid -o list"
#
set -u
while [ ${#} -gt 0 ]
do
   case ${1} in
      -r)
         orcares=${2}
         shift
      ;;
      -exp)
         drakkar_exp=${2}
         shift
      ;;
      -g)
         grid=${2}
         shift
      ;;
      -yearmin)
         yearmin=${2}
         shift
      ;;
      -yearmax)
         yearmax=${2}
         shift
      ;;
      -o)
         list=${2}
         shift
      ;;
      *)
         # other choice
         echo "eee : unknown option ${1}"
         echo "${usage}"
      ;;
   esac
   # next flag
   shift
done
unset usage
#
# check GEOMAG environement
if [ ! -d ${GEOMAG_LOG} ]
then
   echo " eee : \${GEOMAG_LOG} not found"
   exit 1
fi
#
if [ -f ${list} ]
then
   echo "eee : ${list} exist"
   exit 1
fi
# ++ blindage en tout genre
#
case ${orcares} in
   ORCA025)
      # sur l'idris
      dirref=/u/rech/cli/rcli002/
   ;;
   *)
      echo "eee : pb \${orcares} = ${orcares}"
   ;;
esac
#
listfull=${GEOMAG_LOG}/lisfull${$}
#
iyear=${yearmin}
while [ ${iyear} -le ${yearmax} ]
do
   cyear=`printf "%4.4d" ${iyear}`
   ls ${dirref}/${orcares}/${orcares}-${drakkar_exp}-S/${cyear}/${orcares}-${drakkar_exp}_y${cyear}m??d??_${grid}.nc >> ${listfull}
   unset cyear
   # ++ calcul taille cf myls dans faq es dods
   # iyear=$(( ${iyear} + 1 )) ++ pas ok sur rhodes
   # iyear=$(expr  ${iyear} + 1 )  ++ pas ok sur rhodes
   iyear=`expr  ${iyear} + 1 `
done
unset iyear
unset dirref
unset orcares
unset drakkar_exp
unset grid
#
# print total size of model files
# ++ pb syntaxe unitsop="1024**2" # Mo
#ls -l `cat ${listfull}` | \
#awk 'NF>4 {print $5, $9}' | \
#awk 'sum=$1/'$unitsop'+sum {printf("%08.3f '$units' -- %s\n",$1/'$unitsop', $2)}
#
# put files in ${GEOMAG_ID}
# ++ ln cp ou scp
# ++ -f ou test si deja existant
for file in  `cat ${listfull}`
do
   ln -sf ${file} ${GEOMAG_ID}
   echo ${GEOMAG_ID}/`basename ${file}` >> ${list}
done
unset listfull
#
# end
exit 0
