Index: /TOOLS/INTERP_NUDGE/create_sst_anomaly.ksh
===================================================================
--- /TOOLS/INTERP_NUDGE/create_sst_anomaly.ksh	(revision 2171)
+++ /TOOLS/INTERP_NUDGE/create_sst_anomaly.ksh	(revision 2171)
@@ -0,0 +1,100 @@
+#!/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
+
+
Index: /TOOLS/INTERP_NUDGE/era2gcm.ksh
===================================================================
--- /TOOLS/INTERP_NUDGE/era2gcm.ksh	(revision 2171)
+++ /TOOLS/INTERP_NUDGE/era2gcm.ksh	(revision 2171)
@@ -0,0 +1,78 @@
+#!/bin/ksh
+#set -xv
+# Function that does a linear interpolation of the variable in 
+# filein towards the destionation grid in gridfile. Results in are 
+# stored in fileout.
+#
+# Exemple for use :
+# ./era2gcm.ksh grilles_gcm.nc u_filein.nc u_fileout.nc u
+#
+###################################################################
+# O - Input arguments
+###################################################################
+# gridfile : File containing the target grid (grilles_gcm.nc)
+    gridfile=$1
+# filein : File containing the variable at source grid
+    filein=$2
+# fileout : Output file with the variable at target grid
+    fileout=$3
+# varin : Variable name in source file
+    varin=$4
+
+###################################################################
+# 1 - Define variables
+###################################################################
+# Find number of time step
+    tmax=`ncdump -h ${filein} | grep time | head -1 | awk ' { print $6 } '`
+    tmax=$( echo ${tmax} | awk '-F(' '{print $2}' )
+    
+# Choose grid and output variable name 
+    if [ ${varin} = 'u' ] || [ ${varin} = 'vitu' ] ; then 
+	varout=uwnd
+	grille='grille_u'
+    elif [ ${varin} = 'v' ] || [ ${varin} = 'vitv' ] ; then 
+	varout=vwnd
+	grille='grille_v'
+    elif [ ${varin} = 'ta' ] || [ ${varin} = 'temp' ] ; then 
+	varout=air
+	grille='grille_t'
+    elif [ ${varin} = 'r' ] || [ ${varin} = 'hur' ] ; then 
+	varout=rh
+	grille='grille_t'
+    elif [ ${varin} = 'msl' ] ; then 
+	varout=sp
+	grille='grille_t'
+    else 
+	echo Error : ${varin} unknown!!!!
+	exit
+    fi 
+
+###################################################################
+# 2 - Create ferret script for interpolation
+###################################################################
+    cat << eod >  ${varin}.jnl
+set memory/size=50
+
+use "${gridfile}"
+use "${filein}"
+let ${varout}=${varin}
+
+define grid/like=${varout}[d=2]/x=grille_u[d=1]/y=grille_u[d=1] grille_u
+define grid/like=${varout}[d=2]/x=grille_v[d=1]/y=grille_v[d=1] grille_v
+define grid/like=${varout}[d=2]/x=grille_v[d=1]/y=grille_u[d=1] grille_t
+
+save/clobber/file="${fileout}" ${varout}[d=2,g=${grille},l=1]
+repeat/l=1:${tmax} save/file="${fileout}"/append ${varout}[d=2,g=${grille}]
+
+exit
+eod
+    
+###################################################################
+# 3 - Launch interpolation with ferret
+###################################################################
+    ferret -nojnl <<eod >  /dev/null
+go ${varin}.jnl
+quit
+eod
+
+#}
Index: /TOOLS/INTERP_NUDGE/interp_from_TS.ksh
===================================================================
--- /TOOLS/INTERP_NUDGE/interp_from_TS.ksh	(revision 2171)
+++ /TOOLS/INTERP_NUDGE/interp_from_TS.ksh	(revision 2171)
@@ -0,0 +1,170 @@
+#!/bin/ksh
+set -xv
+set verbose echo
+#
+# This script will interpolate horizontaly time series from IPSLCM5 model 
+# output to the desired model grid 
+#
+# Contact Josefine Ghattas
+###############################################################################
+###############################################################################
+###############################################################################
+# BEGIN USER DEFINITION
+###############################################################################
+
+# gridfile : file grilles_gcm.nc containing longitudes and latitudes of the destination grid
+gridfile=/work/cont003/p86ghatt/GUIDAGE/INTERP_FROM_TS/LMD96x95_grilles_gcm.nc
+
+
+# indir : path for directory containing the files to interpolate
+indir=/dmnfs/cont003/p86denv/IGCM_OUT/IPSLCM5A/PROD/rcp45/v3.rcp45.1/ATM/Analyse/TS_HF
+
+# varlist : list of variables to interpolate
+#varlist="vitu vitv temp hur"
+varlist="vitu"
+
+# filelist_var : list of files to interpolate for variable var
+filelist_vitu="v3.rcp45.1_20060101_20151231_HF_vitu.nc  v3.rcp45.1_20360101_20451231_HF_vitu.nc  v3.rcp45.1_20660101_20751231_HF_vitu.nc v3.rcp45.1_20160101_20251231_HF_vitu.nc  v3.rcp45.1_20460101_20551231_HF_vitu.nc  v3.rcp45.1_20760101_20851231_HF_vitu.nc v3.rcp45.1_20260101_20351231_HF_vitu.nc  v3.rcp45.1_20560101_20651231_HF_vitu.nc  v3.rcp45.1_20860101_20951231_HF_vitu.nc"
+
+filelist_vitv="v3.rcp45.1_20060101_20151231_HF_vitv.nc  v3.rcp45.1_20360101_20451231_HF_vitv.nc  v3.rcp45.1_20660101_20751231_HF_vitv.nc v3.rcp45.1_20160101_20251231_HF_vitv.nc  v3.rcp45.1_20460101_20551231_HF_vitv.nc  v3.rcp45.1_20760101_20851231_HF_vitv.nc v3.rcp45.1_20260101_20351231_HF_vitv.nc  v3.rcp45.1_20560101_20651231_HF_vitv.nc  v3.rcp45.1_20860101_20951231_HF_vitv.nc"
+
+filelist_temp="v3.rcp45.1_20060101_20151231_HF_temp.nc  v3.rcp45.1_20360101_20451231_HF_temp.nc  v3.rcp45.1_20660101_20751231_HF_temp.nc v3.rcp45.1_20160101_20251231_HF_temp.nc  v3.rcp45.1_20460101_20551231_HF_temp.nc  v3.rcp45.1_20760101_20851231_HF_temp.nc v3.rcp45.1_20260101_20351231_HF_temp.nc  v3.rcp45.1_20560101_20651231_HF_temp.nc  v3.rcp45.1_20860101_20951231_HF_temp.nc"
+
+filelist_hur="v3.rcp45.1_20060101_20151231_HF_hur.nc  v3.rcp45.1_20360101_20451231_HF_hur.nc  v3.rcp45.1_20660101_20751231_HF_hur.nc v3.rcp45.1_20160101_20251231_HF_hur.nc  v3.rcp45.1_20460101_20551231_HF_hur.nc  v3.rcp45.1_20760101_20851231_HF_hur.nc v3.rcp45.1_20260101_20351231_HF_hur.nc  v3.rcp45.1_20560101_20651231_HF_hur.nc  v3.rcp45.1_20860101_20951231_HF_hur.nc"
+
+# Define variables for output
+# Complet file name for output : $outdir/$year/$outprefix_$var_$year_$month.nc
+# outdir : directory to store output files
+outdir=$DMFDIR/IGCM_OUT/LMDZOR/NUDGEFILES/LMD9695/RCP45
+
+# outprefix : prefix to output files
+outprefix=v3.rcp45.1_HF_
+# outsuffix : suffix to output files
+outsuffix=.nc
+
+# Time variables
+# first_year : first year to interpolate
+first_year=2006
+# last_year : last year to interpolate
+last_year=2095
+
+# rundir : temporary run directory
+rundir=$CSCRATCHDIR/RUNDIR/interprun
+###############################################################################
+# END USER DEFINITION
+###############################################################################
+###############################################################################
+###############################################################################
+# Create and move to run directory
+if [ ! -d ${rundir} ] ; then
+    mkdir -p ${rundir}
+fi
+cp -f era2gcm.ksh $rundir/.
+cd $rundir
+
+# Create output directory if it does not exist
+if [ ! -d $outdir ] ; then
+    mkdir -p $outdir
+fi
+
+
+###############################################################################
+# Loop over variable list
+#   * Loop over corresponding file list
+#     1 - Interpolate the whole time-serie to destination grid
+#     2 - Devide files into monthly files
+#   * Loop over all years for each variable
+#     3 - Add a time step last in each file coming from next month 
+###############################################################################
+for var in $varlist ; do
+    case $var in
+	vitu)
+	    filelist=$filelist_vitu ;;
+	vitv)
+	    filelist=$filelist_vitv ;;
+        temp)
+            filelist=$fileslist_temp ;;
+        hur)
+            filelist=$filelist_hur ;;
+    esac
+    
+    echo $var : $filelist; echo
+ 
+    for file in $filelist ; do
+# 1 - Interpolate time serie to destiation grid
+	filein=$indir/$file
+	file2=$outdir/$file
+	cp -f $filein file1.nc
+	echo Interpolate file $filein and save in file $file2
+	./era2gcm.ksh $gridfile file1.nc file2.nc $var
+	
+# 2 - Devide files into monthly files
+	cdo splityear file2.nc outyear_
+    done
+    ls -lrt
+
+    year=$first_year
+    while [[ ${year} -le ${last_year} ]] ; do
+	# split in month
+	cdo splitmon outyear_$year.nc outmonth_
+
+	# move monthly files to output directory
+	mkdir -p $outdir/$year
+	month=01
+	for month in 01 02 03 04 05 06 07 08 09 10 11 12 ; do
+	    mv outmonth_$month.nc $outdir/$year/${outprefix}${var}_${year}_${month}${outsuffix}
+	done
+	
+	# update year
+	year=`expr $year + 1`
+    done
+    
+# Loop over all years for one variables
+# 3 - Add first time step last in file
+    echo Before add step
+    ls -lrt $outdir
+    ls -lrt $outdir/*
+
+    year=$first_year
+    month=01
+    while [[ ${year} -le ${last_year} ]] ; do
+	case $month in
+	    12)
+		nextyear=`expr $year + 1`
+		nextmonth=01
+		;;
+	    *)
+		nextyear=$year
+		nextmonth=`expr $month + 1`
+		if [[ $nextmonth -le 9 ]] ; then
+		    nextmonth=0$nextmonth
+		fi
+		;;
+	esac
+	
+	file=$outdir/$year/${outprefix}${var}_${year}_${month}${outsuffix}
+	nextfile=$outdir/$year/${outprefix}${var}_${nextyear}_${nextmonth}${outsuffix}
+	if [ -f ${nextfile} ] ; then
+	    echo First time step from file ${nextfile} will be added last in file ${file}
+	    cp -f $file file.nc
+	    cp -f $nextfile next.nc
+	    ncks -O -d time,0,0 next.nc tmp.nc
+	    ncrcat -O file.nc tmp.nc tmptot.nc
+	    mv tmptot.nc $file
+	else
+	    echo Next file ${nextfile} does not exist. Nothing will be done for ${file}
+	fi
+	
+	# Update month and year
+	year=$nextyear
+	month=$nextmonth
+    
+    done
+	
+# End of loop variable list
+done
+
+### Copy gridfile in output dir
+cp -f $gridfile $outdir/grilles_gcm.nc
+	    
+exit
Index: /TOOLS/INTERP_NUDGE/interp_from_era.ksh
===================================================================
--- /TOOLS/INTERP_NUDGE/interp_from_era.ksh	(revision 2171)
+++ /TOOLS/INTERP_NUDGE/interp_from_era.ksh	(revision 2171)
@@ -0,0 +1,147 @@
+#!/bin/ksh
+set verbose echo
+#
+# This script will interpolate horizontaly ERA-I
+# output to the desired model grid 
+#
+# Contact Josefine Ghattas
+###############################################################################
+###############################################################################
+###############################################################################
+# BEGIN USER DEFINITION
+###############################################################################
+
+# gridfile : file grilles_gcm.nc containing longitudes and latitudes of the destination grid
+gridfile=/work/cont003/p86ghatt/GUIDAGE/INTERP_FROM_TS/LMD96x95_grilles_gcm.nc
+
+# indir : path for directory containing the files to interpolate
+#indir=/u/rech/psl/rpsl376/ERAI/NETCDF/GLOBAL_075/4xdaily/AN_PL
+indir=/dmnfs/cont003/p24data/ERAI/NETCDF/GLOBAL_075/4xdaily/AN_PL
+# insuffix : suffix in input files
+insuffix=.aphei.GLOBAL_075.nc
+
+# varlist : list of variables to interpolate
+varlist="u v ta r"
+
+# Variables for output :
+# outdir : directory to store output files
+outdir=/dmnfs/cont003/p86ghatt/IGCM_OUT/LMDZOR/NUDGEFILES/LMD9695/ERAI
+# outsuffix : suffix to output files
+outsuffix=_erai.nc
+
+# Time variables :
+# first_year : first year to interpolate
+first_year=2000
+# last_year : last year to interpolate
+last_year=2000
+
+# rundir : temporary run directory
+rundir=$CSCRATCHDIR/RUNDIR/interprun
+###############################################################################
+# END USER DEFINITION
+###############################################################################
+###############################################################################
+###############################################################################
+if [ ! -d ${rundir} ] ; then
+    mkdir -p ${rundir}
+fi
+cp -f era2gcm.ksh $rundir/.
+cd $rundir
+
+###############################################################################
+# For all variables do :
+#   1) Interpolate all month and years for each variable
+#   2) Add a time step last in each file coming from next month 
+###############################################################################
+# Loop over all variables
+for var in $varlist ; do
+    year=$first_year
+    month=01
+
+    # 1 - Loop over all files and interpolate the variable
+    while [[ ${year} -le ${last_year} ]] ; do
+        # Create output directory if it does not exist
+	if [ ! -d $outdir/${year} ] ; then
+	    mkdir -p $outdir/${year}
+	fi
+
+	# Define source and target file
+	filein=${indir}/${year}/${var}.${year}${month}${insuffix}
+	fileout=${outdir}/${year}/${var}_${year}${month}${outsuffix}
+
+	# Interpolate filein and save in fileout
+	echo Interpolate file $filein and save in file $fileout
+	./era2gcm.ksh $gridfile $filein tmp.nc $var
+	mv tmp.nc $fileout
+	echo
+
+
+	# Find next month and year
+	case $month in
+	    12)
+		nextyear=`expr $year + 1`
+		nextmonth=01
+		;;
+	    *)
+		nextyear=$year
+		nextmonth=`expr $month + 1`
+		if [[ $nextmonth -le 9 ]] ; then
+		    nextmonth=0$nextmonth
+		fi
+		;;
+	esac
+	# Update month and year
+	year=$nextyear
+	month=$nextmonth
+    done
+
+
+    # 2 - Loop over all files and add a time step : 
+    #     take the first time step from next coming month and add last in current month
+    echo Before add step
+
+    year=$first_year
+    month=01
+    while [[ ${year} -le ${last_year} ]] ; do
+	# Find next month and year
+	case $month in
+	    12)
+		nextyear=`expr $year + 1`
+		nextmonth=01
+		;;
+	    *)
+		nextyear=$year
+		nextmonth=`expr $month + 1`
+		if [[ $nextmonth -le 9 ]] ; then
+		    nextmonth=0$nextmonth
+		fi
+		;;
+	esac
+	
+	file=${outdir}/${year}/${var}_${year}${month}${outsuffix}
+	nextfile=${outdir}/${year}/${var}_${nextyear}${nextmonth}${outsuffix}
+	if [ -f ${nextfile} ] ; then
+	    echo First time step from file ${nextfile} will be added last in file ${file}
+	    cp -f $file file.nc
+	    cp -f $nextfile next.nc
+	    ncks -O -d TIME,0,0 next.nc tmp.nc
+	    ncrcat -O file.nc tmp.nc tmptot.nc
+	    mv tmptot.nc $file
+	else
+	    echo Next file ${nextfile} does not exist. Nothing will be done for ${file}
+	fi
+
+	# Update month and year
+	year=$nextyear
+	month=$nextmonth
+    done
+
+
+    
+# End of loop variable list
+done
+
+### Copy gridfile in output dir
+cp -f $gridfile $outdir/grilles_gcm.nc
+    
+exit
