#!/bin/ksh
# X. Wang 2020
#
# FUNCTIONALITY:
# to prepare the forcing files for running the new driver.
#
# HOW:
# 1) to create two new nc files for firstyear-1 and lastyear+1 respectively, given a forcing dataset
# 2) to create softlink to other original forcing files.
# 3) the resulting files are saved in the user-defined output path
# 
#
# USAGE:
# ./AddScript inputpath outputpath
# where the input path for looking for the forcing data ; 
# the desired output path to store the newly created files
# 
# ./AddYears /projsu/igcmg/IGCM/SRF/METEO/CRU-NCEP/v8/twodeg/ user_desired_path
#
# Information:
# The forcing files are saved in the default IGCM path: 
# CICLAD: /projsu/igcmg/IGCM/
# IRENE: /ccc/work/cont003/igcmg/igcmg/IGCM
# jean zay: /gpfswork/rech/psl/commun/IGCM
# obelix: /home/orchideeshare/igcmg/IGCM
# 
# At this path, choose the interested forcing directory
# For example adding SRF/METEO/CRUJRA/v2.1/twodeg/
# The final input=/projsu/igcmg/IGCM/SRF/METEO/CRUJRA/v2.1/twodeg
#
# This script is modified and generalized based on a script of Jan Polcher.


#--------------------------------------------------------------------

input=$1
output=$2

[ -d output ] || mkdir -p $output

#############################################################################
# get the existing nc files of first and last year in the current directory
firstyear_nc=`ls $input/*nc | xargs -n 1 basename | head -n 1 `
lastyear_nc=`ls $input/*nc | xargs -n 1 basename | tail -n 1 `

# find the first year and last year 
if [[ $input == *"CRU-NCEP"* || $input == *"WFDEI"* ]] 
then
    firstyear=`ls $input/*nc | xargs -n 1 basename | head -n 1 | awk '{split($0,a,"_");print a[3]}' | awk -F'.' '{print $1}' `
    lastyear=`ls $input/*nc | xargs -n 1 basename | tail -n 1 | awk '{split($0,a,"_");print a[3]}' | awk -F'.' '{print $1}' `
else
    firstyear=`ls $input/*nc | xargs -n 1 basename | head -n 1 | awk '{split($0,a,"_");print a[4]}' | awk -F'.' '{print $1}' `
    lastyear=`ls $input/*nc | xargs -n 1 basename | tail -n 1 | awk '{split($0,a,"_");print a[4]}' | awk -F'.' '{print $1}' `
fi

# to test if the new extra file already exist or not
firstyearsize=`ls -l  $input/*$firstyear*nc | awk '{print $5}'`
lastyearsize=`ls -l  $input/*$lastyear*nc | awk '{print $5}'`

testyear=$((firstyear + 1))
testyearsize=`ls -l  $input/*$testyear*nc | awk '{print $5}'`

firstyear_sizeratio=`echo "scale=2 ; $firstyearsize / $testyearsize" | bc`
lastyear_sizeratio=`echo "scale=2 ; $lastyearsize / $testyearsize" | bc`

if [[ $firstyear_sizeratio  < 0.1 ]];then
    echo "Maybe the first new forcing file is already created and need not redo."
fi

if [[ $lastyear_sizeratio  < 0.1 ]];then
    echo "Maybe the last new forcing file is already created and need not redo. Exit"
    exit 1
fi

# prepare to create the new forcing files for firstyear-1 and lastyear+1
firstyear_less1=$((firstyear - 1))
lastyear_more1=$((lastyear + 1))

substr=`ls $input/*nc | xargs -n 1 basename | head -n 1 | sed s/"_$firstyear.nc"//`

new_firstyear_nc=$substr"_$firstyear_less1.nc"
new_lastyear_nc=$substr"_$lastyear_more1.nc"

timeunit=`ncdump -h $input$firstyear_nc | grep units | grep seconds | awk '{split($0,a,"=");print a[2]}'`
#
# create the new first year file by using the last 12 hours of $firstyear_nc
#
if [[ ! -e $input$new_firstyear_nc &&  $firstyear_sizeratio  > 0.1 ]] ; then
    tlen=`ncdump -h $input$firstyear_nc | grep currently | cut -d "(" -f 2 | cut -d " " -f 1` 
    tst=$(( $tlen - 12 ))
    if [[ "$firstyear_nc" == *"CRU"* || "$firstyear_nc" == *"cru"*  ]]
    then
	ncks -h -F -d tstep,$tst,$tlen $input$firstyear_nc $output/$new_firstyear_nc
	ncatted -h -a units,timeplussix,o,c,"seconds since $firstyear_less1-01-01 00:00:00" $output/$new_firstyear_nc 
    else 
	ncks -h -F -d time,$tst,$tlen $input$firstyear_nc $output/$new_firstyear_nc
    fi
    ncatted -h -a units,time,o,c,"seconds since $firstyear_less1-01-01 00:00:00" $output/$new_firstyear_nc 
    
    
fi
#
#
# Create the new last year file by using the first 12 hours of $lastyear_nc
#
if [[ ! -e  $input$new_lastyear_nc &&  $firstyear_sizeratio  > 0.1 ]] ; then
    if [[ "$lastyear_nc" == *"CRU"* || "$lastyear_nc" == *"cru"*  ]]
    then
	ncks -h -F -d tstep,1,12 $input$lastyear_nc $output/$new_lastyear_nc
	ncatted -h -a units,timeplussix,o,c,"seconds since $lastyear_more1-01-01 00:00:00"  $output/$new_lastyear_nc
    else
	ncks -h -F -d time,1,12 $input$lastyear_nc $output/$new_lastyear_nc
    fi
    ncatted -h -a units,time,o,c,"seconds since $lastyear_more1-01-01 00:00:00"  $output/$new_lastyear_nc
fi

# 2) create links to all other files, if the output dir is different from the input dir.
ls $output

if [ "$input" != "$output" ]; then
    for i in `ls $input/*nc`
    do
	newname=`basename $i`
	if [ ! -f "$output/$newname" ];then
	    ln -s $i $output/$newname
	fi
    done
fi
