#!/bin/csh 

set mois=$1
set an=$2
set tmin=$3
set tmax=$4
set GRILLE_FI=$5
set outsuf=_ERAI_$an-$mois.nc
set climserv=local

#####################################################################
# Grille
#####################################################################
if ( ! -f $GRILLE_FI ) then
    echo fichier start.nc must exist
    exit
endif

set iip1=`ncdump -h $GRILLE_FI | grep rlonu | head -1 | awk ' { print $3 } '`
set jjm=`ncdump -h $GRILLE_FI | grep rlatv | head -1 | awk ' { print $3 } '`
set jjp1=`expr $jjm + 1`
set iim=`expr $iip1 - 1`

echo iim=$iim jjm=$jjm

###################################################################
# Choix de la periode temporelle
###################################################################
set t0="l=$tmin"
set t1tn="l=${tmin}:${tmax}"

###################################################################
# Fichiers donnees
###################################################################
if ( 1 == 0 ) then #ncep

    set varu=ua
    set varv=va
    set vart=ta
    set varq=hur
    set varp=ps
    
    set fileu="$NCEP_DIR/ua.nc"
    set filev="$NCEP_DIR/va.nc"
    set filet="$NCEP_DIR/ta.nc"
    set fileq="$NCEP_DIR/hur.nc"
    set filep="$NCEP_DIR/ps.nc"

else # ERAI

    set suf=ei.GLOBAL_075

    if ( $climserv == poly ) then
	set ANA_DIR='http://dods-ipsl.ipsl.polytechnique.fr/ipsl-bin/nph-dods/ERAI/NETCDF/GLOBAL_075/4xdaily'
    else
	set ANA_DIR='http://dodsp.idris.fr:81/cgi-bin/nph-dods/rpsl376/ERAI/NETCDF/GLOBAL_075/4xdaily'
# at ulam: set ANA_DIR='/u/rech/psl/rpsl376/ERAI/NETCDF/GLOBAL_075/4xdaily'
    endif

    set vart=ta
    set varu=u
    set varv=v
    set varq=r
    set varp=msl
    set filet="$ANA_DIR/AN_PL/$an/$vart.$an$mois.aph$suf.nc"
    set fileu="$ANA_DIR/AN_PL/$an/$varu.$an$mois.aph$suf.nc"
    set filev="$ANA_DIR/AN_PL/$an/$varv.$an$mois.aph$suf.nc"
    set fileq="$ANA_DIR/AN_PL/$an/$varq.$an$mois.aph$suf.nc"
    set filep="$ANA_DIR/AN_SF/$an/$varp.$an$mois.ash$suf.nc"

endif

###################################################################
# script ferret pour interpolation
###################################################################

cat <<eod> ! tmp.jnl
set memory/size=50
use "$GRILLE_FI"
use "$fileu"
use "$filev"
use "$filet"
use "$fileq"
use "$filep"

let uwnd=$varu
let vwnd=$varv
let tempair='T'
let air=$vart
let rh=$varq
let sp=$varp
let year=$an

define axis/t=1-jan-${an}:31-dec-${an}:6/units=hours thour
define grid/like=uwnd[d=2]/x=cu[d=1]/y=cu[d=1] grille_u
define grid/like=vwnd[d=3]/x=cv[d=1]/y=cv[d=1] grille_v
define grid/like=air[d=4]/x=cv[d=1]/y=cu[d=1] grille_T

!save/clobber/file=T$outsuf air[d=4,g=grille_T,i=1:$iip1,j=1:$jjp1,$t0,gt=thour@asn]
!repeat/$t1tn save/file="T$outsuf"/append air[d=4,g=grille_T,i=1:$iip1,j=1:$jjp1,gt=thour@asn]

save/clobber/file=u$outsuf uwnd[d=2,g=grille_u,i=1:$iip1,j=1:$jjp1,$t0,gt=thour@asn]
repeat/$t1tn save/file="u$outsuf"/append uwnd[d=2,g=grille_u,i=1:$iip1,j=1:$jjp1,gt=thour@asn]

save/clobber/file=v$outsuf vwnd[d=3,g=grille_v,i=1:$iip1,j=1:$jjm,$t0,gt=thour@asn]
repeat/$t1tn save/file="v$outsuf"/append vwnd[d=3,g=grille_v,i=1:$iip1,j=1:$jjm,gt=thour@asn]

!save/clobber/file=hur$outsuf rh[d=5,g=grille_T,i=1:$iip1,j=1:$jjp1,$t0,gt=thour@asn]
!repeat/$t1tn save/file="hur$outsuf"/append rh[d=5,g=grille_T,i=1:$iip1,j=1:$jjp1,gt=thour@asn]

!save/clobber/file=ps$outsuf sp[d=6,g=grille_T,i=1:$iip1,j=1:$jjp1,$t0,gt=thour@asn]
!repeat/$t1tn save/file="ps$outsuf"/append sp[d=6,g=grille_T,i=1:$iip1,j=1:$jjp1,gt=thour@asn]

eod

ferret <<eod > & /dev/null
go tmp.jnl
quit
eod
