"""
Aina Johannessen. 
2017/2018 - Masterthesis

"""



#1 = 1000 hpa, 2 = 950 hpa, 3 = 925 hpa, 4 = 900 hpa, 5 = 850 hpa, 6 = 800 , 7 =  700, 8 = 600 hpa, 9 = 500 hpa, 10 = 400 hpa, 11 = 300 hpa, 12 = 250 hpa, 13 = 200 hpa, 14 = 150 hpa, 15 = 100 hpa. 

#-------------------------------------------------------------------------------------------------------
# This is my main bash program: 
#   - Works for ECMWF analysis data in gridType: 
#	- Generates a file containing the integrated IVT values
#	- Generates a plot by calling python program, IVT().  
#   - Remember its bash.. carefull with spacing!
#	- I have silenced allot of processes, which might hide errors. Remove > /dev/null 2>&1 to see errors
#   - It gets file non locally, so you have to connect to norstore to get a folder with norstore data
#-------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------


#connect_to_data="sshfs ainajoh@login.nird.sigma2.no:/projects/NS1000K/ainajoh data_norstore"
#"sshfs ainajoh@login3.norstore.uio.no:/projects/NS1000K/ainajoh data_norstore"
#$connect_to_data
dates=(20171215 20171216 20171217 20171218 20171219 20171220 20171221 20171222 20171223 20171224 20171225 20171226) #20171201 20171202 20171203 20171204 20171205 20171206 20171207 20171208 20171209 20171210 )
times=(0000 0600 1200 1800) #0600 1200 1800 ) # 1200 1800 )
g=9.81
#------PATHS------------------------------------------------------------------------------------------
path_tmp_files="./tmp/"
path_integrated_files="../gribs/"
path_norestore_files="./"
path_script="./IVT_integrate.sh"

#-----PRE-SET Filename--------------------------------------------------------------------------------
mult_q_uv=$path_tmp_files"mult_q_uv.grib"
merged_file=$path_tmp_files"all.grib"

#---Removes a file such that I dont have to specify to overwrite it everytime----------
if [ -e "$merged_file" ]
	then
		remove="rm $merged_file"
		$remove
fi

remove="rm IVT_gen_output.out"
$remove

for date in ${dates[@]}; do
	
	echo "----------------------------------">> IVT_gen_output.out
	echo "---------------$date--------------">> IVT_gen_output.out  
	echo "----------------------------------">> IVT_gen_output.out
	for time in ${times[@]}; do
		
		remove_tmp="rm $path_tmp_files*" 
		$remove_tmp           #Remove precious temperory files needed for calculation
		
		fileq=$path_integrated_files"param_q_$date""_$time.grib"  		#kg/kg
		fileuv=$path_integrated_files"param_uv_$date""_$time.grib"  		#m/s
		
		#level_idx=(1   2  3  4  5  6  7  8  9 10 11 12 13 14) 
	   levels=(100000 95000 92500 90000 85000 80000 70000 60000 50000 40000 30000 25000 20000 15000 10000)
		
		echo "Using files----->"
		echo $fileq
		echo $fileuv
		echo "<----------------"
		echo "<----------------"
		#------------MULTIPLY----------------------------------------------------------------------
		mult_q_uv=$path_tmp_files"mult_q_uv.grib"
		echo "start mul"
		mult="cdo -R mul $fileq $fileuv $mult_q_uv" #m/s * kg/kg
		echo $mult
		$mult #> /dev/null 2>&1
		echo "Done multiplying"
		
		
		#----------START INTEGRATION---Trapezoidal method-------------
		echo "--------------------------------------------------------"
		echo "START: Integration--->Trapezoidal method----------------"
		for idx in ${!levels[@]}; do
			P0=${levels[$idx]} 			   #i:lower level  --> high pressure
			P1=${levels[$(( $idx+1))]} 	#highest level-->lower pressure
			if [ $P0 -eq 10000 ]  #Breaks this inner loop if we are at level 10000 to not override it. 
				then
				 echo "enough"
				 break
			 fi
			dP=$((P0-P1)) # pressure difference in each level. -used for integration
			echo "---> In Pressure level $P0 to $P1 ---> pressure difference: $dP"
			
			summed_file=$path_tmp_files"sum$P0.grib" #filename of part sum of two and two levels. 
			t="cdo -mulc,$dP -add -sellevel,$P0 $mult_q_uv -sellevel,"$P1" $mult_q_uv $summed_file"
			$t  #> /dev/null 2>&1   						# > /dev/null 2>&1 makes output silence					
		done
		echo "---> For loop done"
		
		#--------Merging all sum files to be able to use vertsum for summing up all levels----
      u="cdo merge $path_tmp_files""sum* $merged_file"

		$u #> /dev/null 2>&1		  						# > /dev/null 2>&1 makes output silence
		integrated_file=$path_integrated_files"IVT_$date""_$time.grib"
		s="cdo -divc,$g -mulc,0.5 -vertsum $merged_file $integrated_file"
		$s #> /dev/null 2>&1		 						 # > /dev/null 2>&1 makes output silence
		echo "DONE integrating--------------------------------------------"
		echo "--------------------------------------------------------"
		
		echo "$fileq -----\ "                      >> IVT_gen_output.out  
		echo "$fileuv----------> $integrated_file" >> IVT_gen_output.out  
		
		
		#--------Plots-------------------------------------------------------------------------
		#python_file=$path_script"IVT.py"
		#plot="python $python_file --date $date $time"
		#$plot #> /dev/null 2>&1	 						# > /dev/null 2>&1 makes output silence
		#echo "Done plotting"
		
  		#---Removes a file such that I dont have to specify to overwrite it everytime----------
		remove="rm $path_tmp_files*"
		$remove
		echo "!!all temporary files are removed!!"	

	done
done
echo "--**SUCESS**--"

#echo "removes start"
#remove="rm $merged_file"
#$remove	
#echo "removed"
