#--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/esgf/mirror/input4MIPs/CMIP6/CMIP/UoM/UoM-CMIP-1-2-0/atmos/yr/'
dirin2='gr1-GMNHSH/v20160830'
#--dirin2 versioning date is different from previously

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

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

#--N2O we leave unit to ppb
GHG='N2O'
name='nitrous-oxide'
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_CMIP_UoM-CMIP-1-2-0_gr1-GMNHSH_0000-2014.nc'
rm -f tmp.txt ${dirout}/${GHG}_CMIP6_0000_2014.txt
dirin=${dirin1}'mole-fraction-of-'${name}'-in-air/'${dirin2}
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${name}'_in_air' ${dirin}/${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}/${GHG}_CMIP6_0000_2014.txt

#--CH4 here we leave unit in ppb
GHG='CH4'
name='methane'
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_CMIP_UoM-CMIP-1-2-0_gr1-GMNHSH_0000-2014.nc'
dirin=${dirin1}'mole-fraction-of-'${name}'-in-air/'${dirin2}
rm -f tmp.txt ${dirout}/${GHG}_CMIP6_0000_2014.txt
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${name}'_in_air' ${dirin}/${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}/${GHG}_CMIP6_0000_2014.txt

#--CO2 here we leave unit in ppm
GHG='CO2'
name='carbon-dioxide'
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_CMIP_UoM-CMIP-1-2-0_gr1-GMNHSH_0000-2014.nc'
dirin=${dirin1}'mole-fraction-of-'${name}'-in-air/'${dirin2}
rm -f tmp.txt ${dirout}/${GHG}_CMIP6_0000_2014.txt
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${name}'_in_air' ${dirin}/${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}/${GHG}_CMIP6_0000_2014.txt

#--CFC11eq we leave in en pptv
#--not used for CMIP6
GHG='CFC11eq'
name='cfc11eq'
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_CMIP_UoM-CMIP-1-2-0_gr1-GMNHSH_0000-2014.nc'
dirin=${dirin1}'mole-fraction-of-'${name}'-in-air/'${dirin2}
rm -f tmp.txt ${dirout}/${GHG}_0000_2014.txt
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${name}'_in_air' ${dirin}/${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}/${GHG}_0000_2014.txt

#--CFC12eq we leave in en pptv
GHG='CFC12eq'
name='cfc12eq'
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_CMIP_UoM-CMIP-1-2-0_gr1-GMNHSH_0000-2014.nc'
dirin=${dirin1}'mole-fraction-of-'${name}'-in-air/'${dirin2}
rm -f tmp.txt ${dirout}/${GHG}_0000_2014.txt ${dirout}/${GHG}_CMIP6_0000_2014.txt
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${name}'_in_air' ${dirin}/${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}/${GHG}_0000_2014.txt
#--we will use CFC12eq for CMIP6
cp ${dirout}/${GHG}_0000_2014.txt ${dirout}/${GHG}_CMIP6_0000_2014.txt

#--CFC11 we leave in en pptv
#--not used for CMIP6
GHG='CFC11'
name='cfc11'
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_CMIP_UoM-CMIP-1-2-0_gr1-GMNHSH_0000-2014.nc'
dirin=${dirin1}'mole-fraction-of-'${name}'-in-air/'${dirin2}
rm -f tmp.txt ${dirout}/${GHG}_0000_2014.txt
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${name}'_in_air' ${dirin}/${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}/${GHG}_0000_2014.txt

#--CFC12 we leave in en pptv
#--not used for CMIP6
GHG='CFC12'
name='cfc12'
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_CMIP_UoM-CMIP-1-2-0_gr1-GMNHSH_0000-2014.nc'
dirin=${dirin1}'mole-fraction-of-'${name}'-in-air/'${dirin2}
rm -f tmp.txt ${dirout}/${GHG}_0000_2014.txt
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${name}'_in_air' ${dirin}/${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}/${GHG}_0000_2014.txt

#--HFC134aeq we leave in en pptv
GHG='HFC134aeq'
name='hfc134aeq'
file='mole-fraction-of-'${name}'-in-air_input4MIPs_GHGConcentrations_CMIP_UoM-CMIP-1-2-0_gr1-GMNHSH_0000-2014.nc'
dirin=${dirin1}'mole-fraction-of-'${name}'-in-air/'${dirin2}
rm -f tmp.txt ${dirout}/${GHG}_0000_2014.txt ${dirout}/CFC11eq_CMIP6_0000_2014.txt
cdo outputf,%12.4e,3 -selvar,'mole_fraction_of_'${name}'_in_air' ${dirin}/${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}/${GHG}_0000_2014.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_'${name}'_in_air' ${dirin}/${file} | awk '{print $1}' > tmp.txt
paste years.txt tmp.txt > ${dirout}/CFC11eq_CMIP6_0000_2014.txt

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