#--Update Th.Lurton 18/06/2018 for v.1.2.0
#
#--Update Th.Lurton 05/02/2018 for paths to new version
# ** species-directory take hyphens instead of underscores previously;
# ** likewise for species names within filenames;
# ** variable names stay with underscores.

#--v3 of my script to extract global mean and put to IGCM format

#--according to Malte Meinsheausen two options are possible 
#    CFC11=CFC11eq and CCFC12=CFC12
# or CFC11=HFC134a-eq * (0.0100/62.7)/(0.062/238) [values from AR5] and CFC12=CFC12eq 
# in the second case the HFC134a-eq is converted into CFC11eq using RF efficiencies from AR5 in Wm-2pptv-1 

#--11/10/2016 New format for the data, now global, NH and SH means are in a sector dimension
#--hence I parse the first value from the cdo outputf to get the global mean
#--outputf is used with format %12.4e to make sure there is a space between values and 3 values per line

#--dir for annual mean data
dirin1='/prodigfs/project/input4MIPs/CMIP6/ScenarioMIP/UoM/'
dirin2='gr1-GMNHSH/v20180611/'

dirout='./OUTPUT/'
if [ ! -d ${dirout} ] ; then mkdir ${dirout} ; fi

rm -f years.txt
for i in `seq -w 2015 2500`
do
echo 'Annee_'$i'=' >> years.txt
done

for scenario in 'ssp119' 'ssp126' 'ssp245' 'ssp370' 'ssp434' 'ssp460' 'ssp534' 'ssp585'
do

rep=${scenario}'/'

if [ ${scenario} == 'ssp119' ] ; then prefixe='IMAGE' ; fi
if [ ${scenario} == 'ssp126' ] ; then prefixe='IMAGE' ; fi
if [ ${scenario} == 'ssp245' ] ; then prefixe='MESSAGE-GLOBIOM' ; fi
if [ ${scenario} == 'ssp370' ] ; then prefixe='AIM' ; fi
if [ ${scenario} == 'ssp434' ] ; then prefixe='GCAM4' ; fi
if [ ${scenario} == 'ssp460' ] ; then prefixe='GCAM4' ; fi
if [ ${scenario} == 'ssp534' ] ; then prefixe='REMIND-MAGPIE' ; fi
if [ ${scenario} == 'ssp585' ] ; then prefixe='REMIND-MAGPIE' ; fi

if [ ${scenario} == 'ssp119' ] ; then suffixe='' ; fi
if [ ${scenario} == 'ssp126' ] ; then suffixe='' ; fi
if [ ${scenario} == 'ssp245' ] ; then suffixe='' ; fi
if [ ${scenario} == 'ssp370' ] ; then suffixe='' ; fi
if [ ${scenario} == 'ssp434' ] ; then suffixe='' ; fi
if [ ${scenario} == 'ssp460' ] ; then suffixe='' ; fi
if [ ${scenario} == 'ssp534' ] ; then suffixe='over-' ; fi
if [ ${scenario} == 'ssp585' ] ; then suffixe='' ; fi

if [ ! -d ${dirout}${rep} ] ; then mkdir -p ${dirout}${rep} ; fi

#--N2O we leave unit to ppb
GHG='N2O'
name='nitrous-oxide'
namevar='nitrous_oxide'
dirin=${dirin1}'UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0/atmos/yr/mole_fraction_of_'${namevar}'_in_air/'${dirin2}
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_ScenarioMIP_UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0_gr1-GMNHSH_2015-2500.nc'
rm -f tmp.txt ${dirout}${GHG}_CMIP6_${scenario}_2015_2500.txt
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${namevar}'_in_air' ${dirin}${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}${rep}${GHG}_CMIP6_2015_2500.txt

#--CH4 here we leave unit in ppb
GHG='CH4'
name='methane'
namevar='methane'
dirin=${dirin1}'UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0/atmos/yr/mole_fraction_of_'${namevar}'_in_air/'${dirin2}
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_ScenarioMIP_UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0_gr1-GMNHSH_2015-2500.nc'
rm -f tmp.txt ${dirout}${GHG}_CMIP6_${scenario}_2015_2500.txt
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${namevar}'_in_air' ${dirin}${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}${rep}${GHG}_CMIP6_2015_2500.txt

#--CO2 here we leave unit in ppm
GHG='CO2'
name='carbon-dioxide'
namevar='carbon_dioxide'
dirin=${dirin1}'UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0/atmos/yr/mole_fraction_of_'${namevar}'_in_air/'${dirin2}
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_ScenarioMIP_UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0_gr1-GMNHSH_2015-2500.nc'
rm -f tmp.txt ${dirout}${GHG}_CMIP6_${scenario}_2015_2500.txt
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${namevar}'_in_air' ${dirin}${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}${rep}${GHG}_CMIP6_2015_2500.txt

#--CFC11eq we leave in pptv
#--not used for CMIP6
GHG='CFC11eq'
name='cfc11eq'
namevar=${name}
dirin=${dirin1}'UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0/atmos/yr/mole_fraction_of_'${namevar}'_in_air/'${dirin2}
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_ScenarioMIP_UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0_gr1-GMNHSH_2015-2500.nc'
rm -f tmp.txt ${dirout}${GHG}_${scenario}_2015_2500.txt
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${namevar}'_in_air' ${dirin}${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}${rep}${GHG}_2015_2500.txt

#--CFC12eq we leave in pptv
GHG='CFC12eq'
name='cfc12eq'
namevar=${name}
dirin=${dirin1}'UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0/atmos/yr/mole_fraction_of_'${namevar}'_in_air/'${dirin2}
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_ScenarioMIP_UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0_gr1-GMNHSH_2015-2500.nc'
rm -f tmp.txt ${dirout}${GHG}_${scenario}_2015_2500.txt ${dirout}/${GHG}_CMIP6_${scenario}_2015_2500.txt
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${namevar}'_in_air' ${dirin}${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}${rep}${GHG}_2015_2500.txt
#--we will use CFC12eq for CMIP6
cp ${dirout}${rep}${GHG}_2015_2500.txt ${dirout}${rep}${GHG}_CMIP6_2015_2500.txt

#--CFC11 we leave in pptv
#--not used for CMIP6
GHG='CFC11'
name='cfc11'
namevar=${name}
dirin=${dirin1}'UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0/atmos/yr/mole_fraction_of_'${namevar}'_in_air/'${dirin2}
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_ScenarioMIP_UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0_gr1-GMNHSH_2015-2500.nc'
rm -f tmp.txt ${dirout}${GHG}_${scenario}_2015_2500.txt
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${namevar}'_in_air' ${dirin}${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}${rep}${GHG}_2015_2500.txt

#--CFC12 we leave in pptv
#--not used for CMIP6
GHG='CFC12'
name='cfc12'
namevar=${name}
dirin=${dirin1}'UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0/atmos/yr/mole_fraction_of_'${namevar}'_in_air/'${dirin2}
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_ScenarioMIP_UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0_gr1-GMNHSH_2015-2500.nc'
rm -f tmp.txt ${dirout}${GHG}_${scenario}_2015_2500.txt
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${namevar}'_in_air' ${dirin}${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}${rep}${GHG}_2015_2500.txt

#--HFC134aeq we leave in pptv
GHG='HFC134aeq'
name='hfc134aeq'
namevar=${name}
dirin=${dirin1}'UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0/atmos/yr/mole_fraction_of_'${namevar}'_in_air/'${dirin2}
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_ScenarioMIP_UoM-'${prefixe}'-'${scenario}'-'${suffixe}'1-2-0_gr1-GMNHSH_2015-2500.nc'
rm -f tmp.txt ${dirout}${GHG}_${scenario}_2015_2500.txt ${dirout}/CFC11eq_CMIP6_${scenario}_2015_2500.txt
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${namevar}'_in_air' ${dirin}${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}${rep}${GHG}_2015_2500.txt 
#--HFC134a-eq converted to CFC11-eq for CMIP6
#--by multiplying by (0.0100/62.7)/(0.062/238)=0.6122344
cdo outputf,%12.4e,3 -mulc,0.6122344 -selvar,'mole_fraction_of_'${namevar}'_in_air' ${dirin}${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}${rep}CFC11eq_CMIP6_2015_2500.txt

done
#--fin boucle sur scenarios

#--clean up
rm -f tmp.txt years.txt

