#!/bin/bash

#LL=29
LL=31
#MODELNAME=CAM53-Oslo
MODELNAME=CAM6-Oslo

#AVAILABLEYEARS=(2003 2004 2005 2006 2007 2008 2009 2010 2011 2012)
#AVAILABLEYEARS=(2006 2007 2008 2009 2010 2011 2012 2013 2014)
AVAILABLEYEARS=(2010)
#AVAILABLEYEARS=(0030)
#AVAILABLEYEARS=(0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 0020 0021 0022 0023 0024 0025 0026 0027 0028 0029 0030)
#AVAILABLEYEARS=(0020 0021 0022 0023 0024 0025 0026 0027 0028 0029 0030)
AVAILABLEMONTHS=(01 02 03 04 05 06 07 08 09 10 11 12)

#INPUTDIRECTORY=/projects/NS2345K/noresm/cases/aeroCAM53Ref/
#INPUTDIRECTORY=/projects/NS2345K/noresm/cases/AlfKtests/CAM6-Oslo/NF2kNucl-all/
#INPUTDIRECTORY=/projects/NS2345K/noresm/cases/NF2k2rosc1dp/
#INPUTDIRECTORY=/projects/NS9560K/noresm/cases/NF1850norbc_aer2014_f19_20190727/
#INPUTDIRECTORY=/projects/NS9560K/noresm/cases/
#INPUTDIRECTORY=/projects/NS2345K/noresm/cases/divClim4aerosoldiag/
INPUTDIRECTORY=/projects/NS2345K/noresm/cases/divClim4aerosoldiag/NHIST_f19_tn14_20190710_yr2010/nc3/

#EXPERIMENTNAME=7310AMIP20002
#EXPERIMENTNAME=F2000ERF
#EXPERIMENTNAME=NF2kNucl
#EXPERIMENTNAME=NF2k2rosc1dp
#EXPERIMENTNAME=NF1850norbc_aer2014_f19_20190727
#EXPERIMENTNAME=altRHpiclimaer
EXPERIMENTNAME=NHIST_f19_tn14_20190710

#EXPERIMENTNAME2=PD_ERA_2001-2015
#FULLEXPERIMENTNAME=CAM53-Oslo_7310_MG15CLM45_5feb2017IHK
#FULLEXPERIMENTNAME=NF2k2rosc1dp_7sep2018AK
#FULLEXPERIMENTNAME=NF1850norbc_aer2014_f19_20190727
#FULLEXPERIMENTNAME=altRHpiclimaer
FULLEXPERIMENTNAME=NHIST_f19_tn14_20190710

#can chose final directory below, or just cp there later after checking the results:
#OUTPUTDIRECTORY=/projects/NS2345K/CAM-Oslo/DO_AEROCOM/${MODELNAME}_${FULLEXPERIMENTNAME}/renamed
#OUTPUTDIRECTORY=/scratch/kirkevag/NF2k2rosc1dp/
#OUTPUTDIRECTORY=/scratch/kirkevag/NF1850norbc_aer2014_f19_20190727/
OUTPUTDIRECTORY=/scratch/kirkevag/NHIST_f19_tn14_20190710/Oct24Version/

coordinateType="" #Just for initialization , leave this
#PERIOD=9999
PERIOD=2010
FREQUENCY="monthly"
#converts from sulfuric acid (H2SO4) to SO4 (96/98 MW)
SF1="0.9796f"
#converts from ammonium sulfate (NH4_2SO4) to SO4 (96/134 MW)
SF2="0.7273f"

#Function to get Level coordinate
#This is just needed since I think is cleaner to use
#abbreviations for this in the variable-array below
function getVariableCoordinateString()
{
	local __input=$1
	if [ $__input = "M" ];then
		coordinateType="ModelLevel"
	elif [ $__input = "S" ];then 
		coordinateType="Surface"
	elif [ $__input = "C" ];then 
		coordinateType="Column"
	elif [ $__input = "SS" ];then 
		coordinateType="SurfaceAtStations"
	elif [ $__input = "MS" ];then 
		coordinateType="ModelLevelAtStations"
	else
		coordinateType="INVALIDCOORDINATETYPE"
	fi
}

#Create output directory if it does not exist
if [ ! -d "$OUTPUTDIRECTORY" ];then
	echo CREATING OUTPUT DIR $OUTPUTDIRECTORY
	mkdir -p $OUTPUTDIRECTORY
fi

#ARRAY HAS SYNTAX: 
#AEROCOMNAME&CAMOSLONAME(OR FORMULA)&UNIT&CoordinateType
ARRAY=("area&GRIDAREA&m2&S"
	"landf&LANDFRAC&1&S"
	"ps&PS&Pa&S"
   "od550csaer&CDOD550/(CLDFREE+1.e-4f)&1&C"
   "od440csaer&CDOD440/(CLDFREE+1.e-4f)&1&C"
   "od870csaer&CDOD870/(CLDFREE+1.e-4f)&1&C"
	"od550aer&DOD550&1&C"
	"od440aer&DOD440&1&C"
	"od500aer&DOD500&1&C"
	"od670aer&DOD670&1&C"
	"od870aer&DOD870&1&C"
	"abs440aer&ABS440&1&C"
	"abs500aer&ABS500&1&C"
	"abs670aer&ABS670&1&C"
	"abs870aer&ABS870&1&C"
	"abs550aer&ABS550AL&1&C"
	"abs550bc&A550_BC&1&C"
	"abs550dust&A550_DU&1&C"
	"abs550oa&A550_POM&1&C"
	"abs550so4&A550_SO4&1&C"
	"abs550ss&A550_SS&1&C"
	"od550so4&D550_SO4&1&C"
	"od550bc&D550_BC&1&C"
	"od550oa&D550_POM&1&C"
	"od550ss&D550_SS&1&C"
	"od550dust&D550_DU&1&C"
	"od550lt1dust&DLT_DUST&1&C"
	"abs550aercs&CABS550/(CLDFREE+1.e-4f)&1&C"
	"od550lt1aer&DLT_SS+DLT_DUST+DLT_SO4+DLT_BC+DLT_POM&1&C"
	"od550gt1aer&DOD550-DLT_SS-DLT_DUST-DLT_SO4-DLT_BC-DLT_POM&1&C"
	"od550aerh2o&DOD550-OD550DRY&1&C"
	"emiso2&SFSO2+SO2_CMXF&kg m-2 s-1&S"
	"emidms&SFDMS&kg m-2 s-1&S"
	"emidust&SFDST_A2+SFDST_A3&kg m-2 s-1&S"
	"emiss&SFSS_A1+SFSS_A2+SFSS_A3&kg m-2 s-1&S"
	"emibc&SFBC_A+SFBC_AX+SFBC_AC+SFBC_N+SFBC_NI+SFBC_AI+BC_N_CMXF+BC_NI_CMXF+BC_AX_CMXF&kg m-2 s-1&S"
	"emioa&SFOM_AC+SFOM_AI+SFOM_NI+OM_NI_CMXF&kg m-2 s-1&S"
	"emiso4&$SF1*(SFSO4_A1+${SF2}/${SF1}*SFSO4_A2+SFSO4_AC+SFSO4_NA+SFSO4_PR+SO4_PR_CMXF)&kg m-2 s-1&S"
	"chepso2&(GS_SO2-SO2_CMXF)-GS_H2SO4&kg m-2 s-1&S"
	"chegpso4&$SF1*GS_H2SO4&kg m-2 s-1&S"
	"cheaqpso4&$SF2*AQ_SO4_A2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UTC&W m-2&C"
	"rlut&FLUT&W m-2&C"
	"rlds&FLDS&W m-2&C"
	"rlus&FLDS-FLNS&W m-2&C"
	"orog&PHIS/9.81f&m&S"
	"precip&(PRECC+PRECL)*1.e3f&kg m-2 s-1&M"
	"temp&T&K&M"
	"hus&Q&K&M"
	"airmass&AIRMASS&kg m-3&M"
	"ec550aer&EC550AER&m-1&M"
	"ec550dryaer&ECDRYAER&m-1&M"
	"abs550dryaer&ABSDRYAE&m-1&M"
	"absc550aer&ABS550_A&m-1&M"
	"bc5503Daer&BS550AER&m-1 sr-1&M"
	"cl3D&CLOUD&1&M"
	"asy3Daer&ASYMMVIS&1&M"
	"mmraerh2o&MMR_AH2O&kg kg-1&M"
	"vmrso2&SO2&m3 m-3&M"
	"vmrdms&DMS&m3 m-3&M"
	"mmrso4&$SF1*(SO4_A1+${SF2}/${SF1}*SO4_A2+SO4_AC+SO4_NA+SO4_PR+SO4_A1_OCW+${SF2}/${SF1}*SO4_A2_OCW+SO4_AC_OCW+SO4_NA_OCW+SO4_PR_OCW)&kg kg-1&M"
	"mmroa&OM_AC+OM_AI+OM_NI+SOA_NA+SOA_A1+OM_AC_OCW+OM_AI_OCW+OM_NI_OCW+SOA_NA_OCW+SOA_A1_OCW&kg kg-1&M"
	"mmrbc&BC_A+BC_AC+BC_AX+BC_N+BC_NI+BC_AI+BC_A_OCW+BC_AC_OCW+BC_N_OCW+BC_NI_OCW+BC_AI_OCW&kg kg-1&M"
	"mmrss&SS_A1+SS_A2+SS_A3+SS_A1_OCW+SS_A2_OCW+SS_A3_OCW&kg kg-1&M"
	"mmrdu&DST_A2+DST_A3+DST_A2_OCW+DST_A3_OCW&kg kg-1&M"
	"pressure[time,lev,lat,lon]&float(P0*hyam+PS*hybm)&Pa&M"
	"sconcso4[time,lat,lon]&(PS(:,:,:)/287.0f/TS(:,:,:))*$SF1*(SO4_A1(:,$LL,:,:)+${SF2}/${SF1}*SO4_A2(:,$LL,:,:)+SO4_PR(:,$LL,:,:)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AI_OCW(:,$LL,:,:)+OM_NI_OCW(:,$LL,:,:)+SOA_NA_OCW(:,$LL,:,:)+SOA_A1_OCW(:,$LL,:,:))*1.e9f&ug m-3&S"
	)


#For each month ==> do an ensemble average of the variables in question
for aMonth in ${AVAILABLEMONTHS[@]};do
	fileList=""
	for aYear in ${AVAILABLEYEARS[@]}; do
		echo $aYear $aMonth
#		fileList+="$INPUTDIRECTORY/$EXPERIMENTNAME/atm/hist/$EXPERIMENTNAME.cam.h0.$aYear-$aMonth.nc"
#		fileList+="$INPUTDIRECTORY/$EXPERIMENTNAME/atm_clim_yr3-30/nc3/$EXPERIMENTNAME.cam.h0.0003-0030_$aMonth.nc"
		fileList+="$INPUTDIRECTORY/$EXPERIMENTNAME.cam.h0.$aYear-$aMonth.nc"
		fileList+=" "
		#echo $fileList
	done
	echo Will perform ncea for month $aMonth $fileList $OUTPUTDIRECTORY/ENSEMBLE_AVG_${EXPERIMENTNAME}_$aMonth.nc
	ncea -O $fileList $OUTPUTDIRECTORY/ENSEMBLE_AVG_${EXPERIMENTNAME}_$aMonth.nc
	echo $fileList
done


#Now that we have an enesmble for each month ==> merge them with ncrcat!
TMPOUTPUTFILE=$OUTPUTDIRECTORY/${EXPERIMENTNAME}_MONTHLY.nc
ncrcat -O $OUTPUTDIRECTORY/ENSEMBLE_AVG_${EXPERIMENTNAME}_*.nc $TMPOUTPUTFILE 

#The rest of the script operates on the ensemble average file

#Go through all entries and apply formulas as specified by table
for entry in "${ARRAY[@]}"; do
	#VARIABLE TO CREATE
	KEY=${entry%%&*}     #This is Aerocom-name (including dimensions if needed by ncap2)
	UNITKEY=${KEY%%[*}   #This is Aerocom-name (skip anything following "[")
   TMPVALUE=${entry#*&} #This is the formula to be used (AND UNITS AND COORDINATES)
	#VALUE TO EXTRACT
	VALUE=${TMPVALUE%%&*} #This is the formula to be used (REMOVED UNITS)
	#UNIT OF OUTPUT VALUE
	TMPUNIT=${TMPVALUE#*&}   #These are the units AND MODEL COORDINATE

	UNIT=${TMPUNIT%%&*}
	TMPCOORD=${TMPUNIT#*&}

	getVariableCoordinateString $TMPCOORD
	
	#BASED ON VARIABLE NAME, THIS IS OUTPUT FILE
#	OUTFILE=$OUTPUTDIRECTORY/aerocom3_${MODELNAME}_${FULLEXPERIMENTNAME}_${UNITKEY}_${coordinateType}_${PERIOD}_${FREQUENCY}.nc
	OUTFILE=$OUTPUTDIRECTORY/${MODELNAME}_${FULLEXPERIMENTNAME}_${UNITKEY}_${coordinateType}_${PERIOD}_${FREQUENCY}.nc
	echo "...extracting ${KEY} using formula " $KEY "=" $VALUE "  , unit= " $UNIT
	ncap2 -O -v -s "$KEY=$VALUE" $TMPOUTPUTFILE $OUTFILE
	#Add some more variables to final output file
	ncks -A -v gw,time_bnds $TMPOUTPUTFILE $OUTFILE
	#Make sure unit is correct
	#echo "...changing unit for ${UNITKEY} "
	ncatted -O -a units,${UNITKEY},o,c,"${UNIT}" ${OUTFILE}
done

#Clean up and make sure anyone can read this..
chmod -R a+r $OUTPUTDIRECTORY
rm $OUTPUTDIRECTORY/ENSEMBLE_AVG_${EXPERIMENTNAME}_??.nc
rm $OUTPUTDIRECTORY/${EXPERIMENTNAME}_MONTHLY.nc 

exit
