set -e #makes script exit if I raise an exit # 
#-------------------------------------
#INFO::
#-------------------------------------
#This script main function: makes accumulated precip for a set accumulationtime
# Usen in Masterthesis of Aina Johannessen in 2018
#
# Heavy handed, but works.. 
# A new folder is created for every new prefered acc time
# NB! Does not work if you accumulate over a month shift or year shift. (Have to do some small adjustments if this is needed)
#-------------------------------------
#-------------------------------------


dates=(20160926 20160927 20160928 20160929 20160930)
declare -a times=(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23)
modeloption="seNorge_Prec1h"
start_acc=0 #+1 #Depends on which time you want to accumulate over. 
acc_time=6 #Hours of accumulation time you want: Obs! Need to change for loop if you change this
i=$start_acc  #keeps track of previoud times in while loop
j=$((acc_time+start_acc))  #keeps track of next times in while loop

for date in ${dates[@]}; do
   for hour in ${times[@]}; do
   hour0=$((hour+0))
   hour1=$((hour+1))
   hour2=$((hour+2))
   hour3=$((hour+3))
   hour4=$((hour+4))
   hour5=$((hour+5))
   hourout=$((hour+6))
   date0h=$date
   date1h=$date
   date2h=$date
   date3h=$date
   date4h=$date
   date5h=$date
   dateout=$date


   if [[ $hour0 -gt 23 ]]; then  #If we pass 23 hour giving us a new date and new time
     day=${date:6:8}
     newday=$((day+1))
     newhour=$((hour0-24))
     if [[ $newday -lt 10 ]]; then #To keep notation consistent. If less than 10, day 1 becomes 01, 2 becomes 02, etc.
        newday="0$newday"
     fi
     newdate=${date:0:6}$newday #Keep year and month from original date, update with newday
     newhour=$newhour
     hour0=$newhour
     date0h=$newdate
   fi



if [[ $hour1 -gt 23 ]]; then
     day=${date:6:8}
     newday=$((day+1))
     newhour=$((hour1-24))
     if [[ $newday -lt 10 ]]; then
        newday="0$newday"
     fi
     newdate=${date:0:6}$newday
     newhour=$newhour
     hour1=$newhour
     date1h=$newdate
 fi

   if [[ $hour2 -gt 23 ]]; then
     day=${date:6:8}
     newday=$((day+1))
     newhour=$((hour2-24))
     if [[ $newday -lt 10 ]]; then
        newday="0$newday"
     fi
     newdate=${date:0:6}$newday
     newhour=$newhour
     hour2=$newhour
     date2h=$newdate
   fi

   if [[ $hour3 -gt 23 ]]; then
     day=${date:6:8}
     newday=$((day+1))
     newhour=$((hour3-24))
     if [[ $newday -lt 10 ]]; then
        newday="0$newday"
     fi
     newdate=${date:0:6}$newday
     newhour=$newhour
     hour3=$newhour
     date3h=$newdate
   fi

   if [[ $hour4 -gt 23 ]]; then
     day=${date:6:8}
     newday=$((day+1))
     newhour=$((hour4-24))
     if [[ $newday -lt 10 ]]; then
        newday="0$newday"
     fi
     newdate=${date:0:6}$newday
     newhour=$newhour
     hour4=$newhour
     date4h=$newdate
   fi

   if [[ $hour5 -gt 23 ]]; then
     day=${date:6:8}
     newday=$((day+1))
     newhour=$((hour5-24))
     if [[ $newday -lt 10 ]]; then
        newday="0$newday"
     fi
     newdate=${date:0:6}$newday
     newhour=$newhour
     hour5=$newhour
     date5h=$newdate
   fi

   if [[ $hourout -gt 23 ]]; then
     day=${date:6:8}
     newday=$((day+1))
     newhour=$((hourout-24))
     if [[ $newday -lt 10 ]]; then
        newday="0$newday"
     fi
     newdate=${date:0:6}$newday
     newhour=$newhour
     hourout=$newhour
     dateout=$newdate
   fi

if [[ $hour0 -lt 10 ]]; then
      hour0="0$hour0"
fi
if [[ $hour1 -lt 10 ]]; then
      hour1="0$hour1"
fi
if [[ $hour2 -lt 10 ]]; then
      hour2="0$hour2"
fi
if [[ $hour3 -lt 10 ]]; then
      hour3="0$hour3"
fi
if [[ $hour4 -lt 10 ]]; then
      hour4="0$hour4"
fi    
if [[ $hour5 -lt 10 ]]; then
      hour5="0$hour5"
fi
if [[ $hourout -lt 10 ]]; then
      hourout="0$hourout"
fi

#----
#Files made with 1h_UTM33_to_WGS84.bash script: i.e. seNorge_v2_0_PREC1h_grid_2016093001_2016093001.nc ---> WGS84seNorge_v2_0_PREC1h_grid_2016092704_2016092704.nc
#----
file0="../latlon1h/WGS84seNorge_v2_0_PREC1h_grid_${date0h}${hour0}_${date0h}${hour0}.nc"
file1="../latlon1h/WGS84seNorge_v2_0_PREC1h_grid_${date1h}${hour1}_${date1h}${hour1}.nc"
file2="../latlon1h/WGS84seNorge_v2_0_PREC1h_grid_${date2h}${hour2}_${date2h}${hour2}.nc"
file3="../latlon1h/WGS84seNorge_v2_0_PREC1h_grid_${date3h}${hour3}_${date3h}${hour3}.nc"
file4="../latlon1h/WGS84seNorge_v2_0_PREC1h_grid_${date4h}${hour4}_${date4h}${hour4}.nc"
file5="../latlon1h/WGS84seNorge_v2_0_PREC1h_grid_${date5h}${hour5}_${date5h}${hour5}.nc"
#---

#-----------------
#ACCUMULTE 6h ####
echo "${file0}  ${file1}   ${file2}    ${file3}    ${file4}     ${file5}"   # Display filename used for visual check
fileout="../6h_precip/WGS84seNorge_v2_0_PREC6h_grid_ends_${date5h}${hour5}.nc" #6h acc file that will me made
make6h="cdo enssum ${file0} ${file1} ${file2} ${file3} ${file4} ${file5} $fileout" # Command line for accumulating 6hrs
#----------------
#----------------
#DATE CORRECTION
## When using cdo enssum, the validtime in the netcdf is not set at the end accumulated time. 
## We want 20160920 (01+02+03+04+05+06 hours) to have validdate of 201609200600 
corrfile="../6h_precip/corrtime_WGS84seNorge_v2_0_PREC6h_grid_ends_${date5h}${hour5}.nc" #6h acc file with adjusted time made
nicedate="${date5h:0:4}-${date5h:4:2}-${date5h:6:2}"
echo $nicedate #displays date in a nice way for a visual check
correcttime="cdo settaxis,${date5h},${hour5}:00:00 ${fileout} ${corrfile}" #command line for new validdate
#---------------

#Run the comand lines
eval $make6h 
eval $correcttime

#cdo settaxis,2010-01-15,00:00,31days in.nc out.nc
#while ((${#times[$i]})) && ((${#times[$j]})); do #makesure we have
#                
#                test="file"
#                file_pre=$path_selparam_files"prectot_$date+${times[$i]}""grib_fp.grib"
#                file_next=$path_selparam_files"prectot_$date+${times[$j]}""grib_fp.grib"
#                file_new="$path1hfile""precip_ac$acc_time""h_ending_$date+${times[$j]}"".grib"
#                sub_job="cdo sub $file_next $file_pre $file_new"
#                echo $sub_job
                #$sub_job
#                let i+=1 #previoustime
#                let j+=1 #nexttime
#        done
done
done





