#! /bin/sh
#
#+
#
# .. _detectbarotropicmode.sh:
#
# =======================
# detectbarotropicmode.sh
# =======================
#
# -------------------------------------------
# build file for detection of barotropic mode
# -------------------------------------------
#
# SYNOPSIS
# ========
#
# .. code-block:: bash
#
#    detectbarotropicmode.sh -l list -r resolution -exp experience -g grid \
#    -latmin latmin -latmax latmax -longmin longmin -longmax longmax
#
# DESCRIPTION
# ===========
#
#
# EXAMPLES
# ========
#
# For detection on DRAKKAR G42 experiment with ORCA025 resolution on T points
# in the box [-50,30] [295,335]:
#
# .. code-block:: bash
#
#    detectbarotropicmode.sh -l ${GEOMAG_OD}/list_G42 -r ORCA025 -exp G42 -g gridT \
#    -latmin -50 -latmax -30 -longmin 295 -longmax 335
#
# EVOLUTIONS
# ==========
#
# $Id$
#
# pas fini ++
#
# - reee522 2007-11-26T11:33:42Z rhodes (IRIX64)
#
#   * externalization of copy of data file in getmodelout.sh
#
# - reee522 2007-11-26T10:55:44Z rhodes (IRIX64)
#
#   * add ncks, ncrcat and some informations inspired by
#     http://dods.ipsl.jussieu.fr/projets_ES/misc/myls
#
# - fplod 2007-11-23T17:02:56Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * creation
#
#-
#
command=detectbarotropicmode.sh
#
usage="Usage : ${command} -l list -r orcares -exp drakkar_exp -g grid -latmin latmin -latmax latmax -longmin longmin -longmax longmax"
#
set -u
while [ ${#} -gt 0 ]
do
    case ${1} in
        -l)
            list=${2}
            shift
        ;;
        -r)
            orcares=${2}
            shift
        ;;
        -exp)
            drakkar_exp=${2}
            shift
        ;;
        -g)
            grid=${2}
            shift
        ;;
        -latmin)
            latmin=${2}
            shift
        ;;
        -latmax)
            latmax=${2}
            shift
        ;;
        -longmin)
            longmin=${2}
            shift
        ;;
        -longmax)
            longmax=${2}
            shift
        ;;
        *)
            # other choice
            echo "eee : unknown option ${1}"
            echo "${usage}"
        ;;
    esac
    # next flag
    shift
done
#
# check GEOMAG environement
if [ ! -d ${GEOMAG_LOG} ]
then
    echo " eee : \${GEOMAG_LOG} not found"
    exit 1
fi
# ++ blindage en tout genre
#
case ${grid} in
    gridT)
        echo "iii : grid=${orcares}"
    ;;
    *)
        echo "eee : pb \${grid} = ${grid}"
        exit 1
    ;;
esac
#
case ${orcares} in
    ORCA025)
        echo "iii : orcares=${orcares}"
    ;;
    *)
        echo "eee : pb \${orcares} = ${orcares}"
        exit 1
    ;;
esac
#
case ${drakkar_exp} in
    G42)
        echo "iii : drakkar_exp=${drakkar_exp}"
    ;;
    *)
        echo "eee : pb \${drakkar_exp} = ${drakkar_exp}"
        exit 1
    ;;
esac
#
# ++ conversion of latitude and longitude in x and y indexes
case ${orcares} in
    ORCA025)
        xmin=529
        xmax=689
        ymin=266
        ymax=372
        echo "iii : forcage xmin xmax ymin ymax : ${xmin} ${xmax} ${ymin} ${ymax}"
    ;;
    *)
        echo "eee : pb conversion lat,long to x,y"
        exit 1
    ;;
esac
#
# extract ssh and create a new set of files
list_ssh=""
for file in `cat ${list}`
do
    echo "iii : extraction from ${file}" # ++ voir seulement si debug demandé
    filessh=${GEOMAG_OD}/`basename ${file} .nc`_ssh.nc
    rm -f ${filessh} 2> /dev/null
    ncks --dimension x,${xmin},${xmax} --dimension y,${ymin},${ymax}  -v sossheig,nav_lon,nav_lat \
    ${file} ${filessh}
    status_ncks=${?}
    if [ ${status_ncks} -ne 0 ]
    then
        echo "eee : pb with ncks ${file}"
        exit 1
    else
        list_ssh="${list_ssh} ${filessh}"
    fi
done
line=`head -n 1 ${list}`
bfile=`basename ${line}`
datemin=`echo ${bfile}  | awk -F "_" '{print $2}'`
line=`tail -n 1 ${list}`
bfile=`basename ${line}`
datemax=`echo ${bfile}  | awk -F "_" '{print $2}'`
#
# concatenation
filetot=${GEOMAG_OD}/${orcares}-${drakkar_exp}_${datemin}_${datemax}_${grid}_ssh.nc
rm -f ${filetot} 2> /dev/null
ncrcat ${list_ssh} ${filetot}
status_ncrcat=${?}
if [ ${status_ncrcat} -ne 0 ]
then
    echo "eee : pb with ncrcat"
    exit 1
else
    echo "iii : result in ${filetot}"
fi
#
# ++ global attributes
# ++ convention
# ++ nedtcf output or ascii
# clean
rm ${list_ssh}
#
# end
exit 0
