#!/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
