#!/bin/ksh
#  $Header: /home/ssipsl/CVSREP/ORCHIDEE_OL/EXP00/job.run,v 1.3 2004/10/22 14:48:49 ssipsl Exp $
##  @$-q test
##  @$-lT 3600
##  @$-lM 0mb
##  @$-eo
## Job de soumission de ORCHIDEE_OL
##  
##
##      Rachid BENSHILA & Patricia CADULE
##########################################################
#
#        DEFINITIONS DES OPTIONS
#
##########################################################
##
                #- Saisie et validation des options
x_q='';
x_t="";
x_s="Cabauw";
x_f="SECHIBA";
x_n="0";
x_e="Test";
while getopts :hqt:s:f:n:d:e: V
  do
    case $V in
      (h)  echo "Usage    : "${b_n} \
		" [-h] [-q] [-t h]  [-s x] [-f y] [-n nbre] [-e exp]";
	   echo " -h       : help";
	   echo " -q       : quiet";
	   echo " -t h     : target host_name";
	   echo " -s x     : Site";
	   echo " -f y     : Flag";
	   echo " -n nbre  : Initialization";
	   echo " -e exp   : Experience name";
	   echo "Defaults  : -t local_host";
	   echo "Defaults  : -s Cabauw";
	   echo "Defaults  : -f SECHIBA";
	   echo "Defaults  : -n 0";
	   echo "Defaults  : -e Test ";
	   exit 0;;
      (q)  x_q=1;;
      (t)  x_t=${OPTARG};;
      (s)  x_s=${OPTARG};;
      (f)  x_f=${OPTARG};;
      (n)  x_n=${OPTARG};;
      (e)  x_e=${OPTARG};;
      (:)  echo ${b_n}" : -"${OPTARG}" option : missing value" 1>&2;
	   exit 2;;
      (\?) echo ${b_n}" : -"${OPTARG}" option : not supported" 1>&2;
	   exit 2;;
    esac
  done
##
#########################################################################
##
##       Initialisation  
##
#########################################################################
##
## BEGINING OF USER MODIFICATIONS
##
####################
# Experience dates #
####################  
##
##-- First day of experience
#JOUR_DEBUT_EXP=1  ; MOIS_DEBUT_EXP=1  ; AN_DEBUT_EXP=1987
##--
##-- Last day of experience
#JOUR_FINAL_EXP=31  ; MOIS_FINAL_EXP=12 ; AN_FINAL_EXP=1987
##--
##--  Job lenght
#JOURS=0 ; MOIS=0 ; ANS=1
##
####################
# Job iteration    #
####################
##
#NITER_MAX=5
##
########################
# Nom du repertoire    #
########################
##
#D_NAME=Test
##
##############################
##                           #
## END OF USER MODIFICATIONS #
##                           #
##############################
D_NAME=${x_e}
##-- fichier de forcage
F_FILE=${x_s}
export F_FILE=${F_FILE}
##-- iteration
NITER_MAX=${x_n}
##-- flag    
if [ ${x_f} = all ]; then
x_f="SECHIBA OK_CO2 OK_STOMATE"
fi;
typeset -R rach
set -A rach 3 ${x_f}

##-- repertoire de stockage et de lancement, plate-forme
R_STOCKAGE=""

if [ '\?'${x_t} = '\?' ]; then
x_t=`../../../util/w_i_h`  ;
fi

if [ "${x_t}" = "sxnec" ]; then
rsh uqbar mkdir ${WORKDIR}/${D_NAME}
R_STOCKAGE="/u/rech/ces/rces803/FORCAGE_OL"
R_SH="rsh uqbar  -n "
R_SUB=${WORKDIR}/${D_NAME}
QSUB="qsub -x"
elif [ "${x_t}" = "fjvpp" ]; then
R_STOCKAGE="/G/nymphea0/dmnfs/p86bensh/FORCAGE_OL"
R_SH="remsh nymphea0 -n "
R_SUB=${QSUB_WORKDIR:-${PWD}}
QSUB="qsub -x"
elif [ "${x_t}" = "sx6nec" ]; then
R_STOCKAGE="/dmnfs/p86bensh/FORCAGE_OL"
R_SH="rsh mercure0  "
R_SUB=${QSUB_WORKDIR:-${PWD}}
QSUB="/usr/bin/nqsII/qsub -q prod -V"
fi
export R_STOCKAGE=${R_STOCKAGE}

##Scripts d'Atlas
if [ ! -f ../Utilitaire/Job_atlas ]; then
sed -e "s,^#-Q- "${x_t}" *,," ../Utilitaire/AA_atlas | grep -v '^#-Q- ' > ../Utilitaire/Job_atlas 
fi

##-- repertoire de controle des enchainements
 x_m=$(pwd) 
for i in 1 2 3
do
if [ -n "${rach[i]}" ] 
 then
  ../../../util/ins_script -t ${x_t} -n ${F_FILE}_${rach[i]}
   make -f make_ORCHIDEE ${rach[i]}  || exit 1
   sed -e "s/F_FILE=WG_cru/F_FILE=${F_FILE}/g" -e "s/#FLAG=/FLAG=${rach[i]}/g" -e "s/OK_SITE=n/OK_SITE=y/g" -e "s/F_RUN=orchidee/F_RUN=${F_FILE}_${rach[i]}/g" -e "s/CEXPER=${F_FILE}_${rach[i]}/CEXPER=${D_NAME}/g" -e "s/OK_WEATHER=y/OK_WEATHER=n/g" -e "s/duree_sechiba=0/duree_sechiba=1/g" -e "s/n_iter=0/n_iter=${x_n}/g" Job_${F_FILE}_${rach[i]} >Job_${rach[i]}.tmp 
   mv Job_${rach[i]}.tmp Job_${F_FILE}_${rach[i]}
 if [ "${x_q}" != 1 ]; then
   if [ "${x_t}" = "sxnec" ]; then
	ftput -h uqbar $(pwd)/Job_${F_FILE}_${rach[i]} ${WORKDIR}/${D_NAME}/Job_${F_FILE}_${rach[i]} || exit 1
	ftput -h uqbar $(pwd)/${F_FILE}_${rach[i]}.def ${WORKDIR}/${D_NAME}/${F_FILE}_${rach[i]}.def || exit 1
       fi
       ${R_SH} "export F_FILE=${F_FILE} ; export FLAG=${rach[i]} ; export JOUR_DEBUT_EXP=${JOUR_DEBUT_EXP} ; export MOIS_DEBUT_EXP=${MOIS_DEBUT_EXP} ; export AN_DEBUT_EXP=${AN_DEBUT_EXP} ; export JOUR_FINAL_EXP=${JOUR_FINAL_EXP} ; export MOIS_FINAL_EXP=${MOIS_FINAL_EXP} ;  export AN_FINAL_EXP=${AN_FINAL_EXP} ; export JOURS=${JOURS} ; export MOIS=${MOIS} ; export ANS=${ANS} ; export NITER_MAX=${NITER_MAX} ; export D_NAME=${D_NAME}  ; export F_RUN=${F_FILE}_${rach[i]} ; cd ${R_SUB} ;  ${QSUB} Job_${F_FILE}_${rach[i]}"
     fi   
    fi
    done

