#!/bin/ksh
set -vx
#
# create_sst_anomaly.ksh
#
# This script will correct the sea-surface temperature (SST) from a coupled model simulation 
# using the difference between an AMIP reference SST field and an historical coupled model 
# reference SST field. The two reference SST fields are both monthly mean over the period 1979-2005.
# 
# The output is stored in the directory out_anomaly, set below, and is directly usable with 
# the program ce0l (create_etat0_limit) in LMDZ5 revision 1508 or later. For each year there 
# are two output files : one at model original grid and one at a higer resolution grid(with 
# suffix _HR.nc). If the SST are to be used at a higer resolution grid or with a zoom it is 
# better to use the high resolution SST (_HR.nc). 
#
# 1 - Create reference file
# 2 - Add reference file to the scenario simulation
# 3 - Interpolate to higer regular resolution
# 
#
# Author and contact Josefine Ghattas, IPSL, 2011
#
########## User definitions
# Three time series are needed : amip_TS, hist_TS and simul_TS. They should all contain 
# the same variable tsol_oce at the same model grid resolution with monthly values.

# amip_TS : AMIP SST containg at least the reference period 1979-2005
amip_TS=/dmnfs/cont003/p86musat/IGCM_OUT/LMDZOR/PROD/amip/v3.amip1/ATM/Analyse/TS_MO/v3.amip1_19790101_20091231_1M_tsol_oce.nc

# hist_TS : model output SST from reference simulation containg at least the reference period 1979-2005
hist_TS=/dmnfs/cont003/p86denv/IGCM_OUT/IPSLCM5A/PROD/historical/v3.historical1/ATM/Analyse/TS_MO/v3.historical1_18500101_20051231_1M_tsol_oce.nc

# simul_TS : model output SST from the simulation to be corrected containing at least the years [year_begin,year_end] to be set below
simul_TS=/dmnfs/cont003/p86denv/IGCM_OUT/IPSLCM5A/PROD/rcp45/v3.rcp45.1/ATM/Analyse/TS_MO/v3.rcp45.1_20060101_21651231_1M_tsol_oce.nc

# year_begin and year_end : interval for simul_TS
year_begin=2006
year_end=2095

# outname : basename for output files
outname=v3.rcp45.1_tsol_oce_

# out_simul : directory where the original simulation will be stored splitted in years
out_simul=$SCRATCHDIR/OUTTEST/simul

# out_anomaly : directory where the final simulation with anomaly will be created and stored
out_anomaly=$SCRATCHDIR/OUTTEST/anomaly

# rundir : directory where temporary files will be created and stored
rundir=$SCRATCHDIR/OUTTEST/rundir

########## End User definitions

########## 0 Create output and run directories
mkdir -p $out_simul
mkdir -p $out_anomaly

mkdir -p $rundir 
cd $rundir


########## 1 Create reference file
# extract reference period from AMIP SST and create reference file
cdo seldate,1979-01-01,2005-12-31 $amip_TS amip_ts_ref.nc
cdo ymonmean amip_ts_ref.nc amip_ref.nc

# extract reference period from histroical simulation SST and create reference file
cdo seldate,1979-01-01,2005-12-31 $hist_TS hist_ts_ref.nc
cdo ymonmean hist_ts_ref.nc hist_ref.nc

# create difference file AMIP - HIST
cdo sub amip_ref.nc hist_ref.nc refdiff.nc


########## 2 Add reference file to the scenario simulation
# split time serie into yearly files
cdo splityear $simul_TS $out_simul/${outname}

# add refdiff anomalies to each year of RCP4.5
year=$year_begin
while [[ ${year} -le ${year_end} ]] ; do
   cdo add $out_simul/${outname}$year.nc2 refdiff.nc $out_anomaly/${outname}anomaly_amip_${year}.nc

   # next year
   year=`expr $year + 1`
done

########## 3 Interpolate to higer regular resolution
year=$year_begin
while [[ ${year} -le ${year_end} ]] ; do
   cdo remapbic,r720x360 $out_anomaly/${outname}anomaly_amip_$year.nc $out_anomaly/${outname}anomaly_amip_${year}_HR.nc

   # next year
   year=`expr $year + 1`
done


exit


