#!/bin/ksh
#
#
#
#
# The model to be tested
#
model='../modipsl/bin/orchidee_ol'
#
# File with the forcing data
#
forcing='../ISLSCP/islscp_Euro_87.nc'
#
# Files of the various maps needed by the model
#
vegmap='../surfmap/carteveg5km.nc'
irrigmap='../surfmap/irrigated.nc'
routmap='../surfmap/routing.nc'
soilmap='../surfmap/soils_param.nc'
reftemp='../surfmap/reftemp.nc'
#
# Some tools we will need to process the data
#
ncks=$(which ncks)
ncrcat=$(which ncrcat)
ncdump=$(which ncdump)
#
rm="/bin/rm"
mv="/bin/mv"
diff="/usr/bin/diff"
#
#
#  DO NOT CHANGE BELOW THIS LINE
#------------------------------------------------------
#------------------------------------------------------
#
######################################################
#
# Verifications
#
######################################################
#
if [ ! -x ${model} ]; then
   echo "The model executable does not exist"
   echo "Retry the compilation of ORCHIDEE in the modipsl strcture"
   exit
fi 
#
if [ ! -x ${ncks} ]; then
   echo "The ncks tool of nco does not exist"
   echo "Check it out at http://www.cgd.ucar.edu/cms/nco/nco.html"
   exit
fi
#
#
if [ ! -x ${rm} ]; then
   echo "How do I delete a file ?"
   exit
fi
#
if [ ! -f ${forcing} ]; then
   echo "I could not find the forcing file"
   exit
fi
#
if [ ! -f ${vegmap} ]; then
   echo "I could not find the file with the vegetation map"
   exit
fi
#
if [ ! -f ${irrigmap} ]; then
   echo "I could not find the file with the irrigation map"
   exit
fi
#
if [ ! -f ${routmap} ]; then
   echo "I could not find the file with the basin map for the routing"
   exit
fi
#
if [ ! -f ${soilmap} ]; then
   echo "I could not find the file with the map with the soil types"
   exit
fi
#
if [ ! -f ${reftemp} ]; then
   echo "I could not find the file with the map of reference temperatures"
   exit
fi
#
#
######################################################
#
# generate the default run.def
#
######################################################
#
#
${rm} -f run_default.def
cat > run_default.def << %
#
# SECHIBA_2dim
#
DEBUG_INFO=n
FORCING_FILE=${forcing}
HEIGHT_LEV1=2.0
SPLIT_DT=12
RESTART_FILEIN=XXDR_RINXX
RESTART_FILEOUT=XXDR_ROUTXX
TIME_SKIP=0
TIME_LENGTH=XXTLXX
# IMPLICIT is not set
NO_INTER=y
SPRED_PREC=1
NETRAD_CONS=n 
#
LIMIT_WEST = XX_WEST_XX
LIMIT_EAST = XX_EAST_XX
LIMIT_NORTH = XX_NORTH_XX
LIMIT_SOUTH = XX_SOUTH_XX
#
# IOIPSL
#
RIVER_ROUTING=y
ROUTING_RIVERS = 2
ROUTING_FILE=${routmap}
IRRIGATION_FILE=${irrigmap}
DO_IRRIGATION=y
DO_FLOODPLAINS=y
#
# SECHIBA
#
CONDVEG_SNOWA=default
# IMPOSE_AZE is not set
SOILALB_FILE=${soilmap}
SOILTYPE_FILE=${soilmap}
DIFFUCO_LEAFCI=233.
ENERBIL_TSURF=280.
HYDROL_SNOW=0.0
HYDROL_SNOWAGE=0.0
HYDROL_SNOWICE=0.0
HYDROL_SNOWICEAGE=0.0
HYDROL_HDRY=0.0
HYDROL_HUMR=1.0
HYDROL_BQSB=default
HYDROL_GQSB=0.0
HYDROL_DSG=0.0
HYDROL_DSP=default
HYDROL_QSV=0.0
# STOMATE_OK_CO2=y
# STOMATE_OK_STOMATE is not set
# STOMATE_OK_DGVM is not set
# STOMATE_WATCHOUT is not set
SECHIBA_restart_in=XXSE_RINXX
SECHIBA_rest_out=XXSE_ROUTXX
SECHIBA_reset_time=y
STOMATE_RESTART_FILEIN=XXST_RINXX
STOMATE_RESTART_FILEOUT=XXST_ROUTXX
OUTPUT_FILE=XX_SEOUT_XX
WRITE_STEP=86400
SECHIBA_HISTLEVEL=5
STOMATE_OUTPUT_FILE=XX_STOUT_XX
STOMATE_HIST_DT=10.
STOMATE_HISTLEVEL=10
SECHIBA_DAY=0.0
SECHIBA_ZCANOP=0.5
DT_SLOW=86400.
# IMPOSE_VEG is not set
VEGETATION_FILE=${vegmap}
THERMOSOIL_TPRO=280.
%
#
######################################################
#
# Launch our first simulation. It will be called RLarge
#
######################################################
#
# Build the run.def file for 10 days on the large domaine.
#
exp_a='Rlarge'
#
${rm} -f wo
cat << % > wo
s/XXDR_RINXX/NONE/
s/XXDR_ROUTXX/${exp_a}_drv_rest.nc/
s/XXTLXX/10d/
s/XX_WEST_XX/-20.0/
s/XX_EAST_XX/10.0/
s/XX_NORTH_XX/20.0/
s/XX_SOUTH_XX/2.0/
s/XXSE_RINXX/NONE/
s/XXSE_ROUTXX/${exp_a}_sec_rest.nc/
s/XXST_RINXX/NONE/
s/XXST_ROUTXX/${exp_a}_sto_rest.nc/
s/XX_SEOUT_XX/sec_${exp_a}.nc/
s/XX_STOUT_XX/sto_${exp_a}.nc/
%
${rm} -f run.def
sed -f wo run_default.def > run.def
#
# Run the model
#
#
${rm} -f ${exp_a}.log ${exp_a}_*_rest.nc
time ${model} > ${exp_a}.log
${mv} used_run.def used_run_${exp_a}.def
${rm} -f ${exp_a}_drv_rest.nc
#
#
######################################################
#
# Launch our second simulation. It will test the restart of the model
#
######################################################
#
# Build the run.def file for the first 5 days on the large domaine.
#
exp_b='RlargeA'
#
${rm} -f wo
cat << % > wo
s/XXDR_RINXX/NONE/
s/XXDR_ROUTXX/${exp_b}_drv_rest.nc/
s/XXTLXX/5d/
s/XX_WEST_XX/-20.0/
s/XX_EAST_XX/10.0/
s/XX_NORTH_XX/20.0/
s/XX_SOUTH_XX/2.0/
s/XXSE_RINXX/NONE/
s/XXSE_ROUTXX/${exp_b}_sec_rest.nc/
s/XXST_RINXX/NONE/
s/XXST_ROUTXX/${exp_b}_sto_rest.nc/
s/XX_SEOUT_XX/sec_${exp_b}.nc/
s/XX_STOUT_XX/sto_${exp_b}.nc/
%
${rm} -f run.def
sed -f wo run_default.def > run.def
#
# Run the model for the first 5 days
#
${rm} -f ${exp_b}.log ${exp_b}_*_rest.nc
${model} > ${exp_b}.log
${mv} used_run.def used_run_${exp_b}.def
#
# Build the run.def file for the second set of 5 days.
#
exp_c='RlargeB'
#
${rm} -f wo
cat << % > wo
s/XXDR_RINXX/${exp_b}_drv_rest.nc/
s/XXDR_ROUTXX/${exp_c}_drv_rest.nc/
s/XXTLXX/5d/
s/XX_WEST_XX/-20.0/
s/XX_EAST_XX/10.0/
s/XX_NORTH_XX/20.0/
s/XX_SOUTH_XX/2.0/
s/XXSE_RINXX/${exp_b}_sec_rest.nc/
s/XXSE_ROUTXX/${exp_c}_sec_rest.nc/
s/XXST_RINXX/${exp_b}_sto_rest.nc/
s/XXST_ROUTXX/${exp_c}_sto_rest.nc/
s/XX_SEOUT_XX/sec_${exp_c}.nc/
s/XX_STOUT_XX/sto_${exp_c}.nc/
%
${rm} -f run.def
sed -f wo run_default.def > run.def
#
# Run the model for the first 5 days
#
${rm} -f ${exp_c}.log ${exp_c}_*_rest.nc
${model} > ${exp_c}.log
${mv} used_run.def used_run_${exp_c}.def
${rm} -f ${exp_b}_drv_rest.nc ${exp_c}_drv_rest.nc
#
#################################################################
#
# Do the diagnostics
#
#################################################################
#
${rm} -f sec_${exp_a}_pt.nc sec_${exp_b}_pt.nc sec_${exp_c}_pt.nc sec_${exp_a}_combi_pt.nc
${ncks} -d lon,5.25,5.75 -d lat,10.25,10.75 sec_${exp_a}.nc sec_${exp_a}_pt.nc
${ncks} -d lon,5.25,5.75 -d lat,10.25,10.75 sec_${exp_b}.nc sec_${exp_b}_pt.nc
${ncks} -d lon,5.25,5.75 -d lat,10.25,10.75 sec_${exp_c}.nc sec_${exp_c}_pt.nc
#
${ncrcat} sec_${exp_b}_pt.nc sec_${exp_c}_pt.nc sec_${exp_a}_combi_pt.nc
#
vars='evap gqsb bqsb temp_sol'
for v in $vars ; do
  ${rm} -f full combi
  ${ncdump} -v $v -p 9,17 -f f sec_${exp_a}_pt.nc | tail -12 > full
  ${ncdump} -v $v -p 9,17 -f f sec_${exp_a}_combi_pt.nc | tail -12 > combi
  ${diff} full combi > /dev/null
  if [ $? -eq 1 ]; then
    echo 'The restart did not work and we have differences for variable ' $v
    paste full combi
  else
    echo 'The restart worked for variable ' $v
  fi
done
${rm} full combi sec_${exp_b}.nc sec_${exp_c}.nc
#
# Clean up !
#
#
${rm} -f wo run.def run_default.def

