source: TOOLS/CMIP6_FORCING/SCENARIOS/AER_TROP_EMISSIONS/REGRID/regrid_scen.sh @ 4007

Last change on this file since 4007 was 4007, checked in by tlurton, 6 years ago

Adding scripts to regrid tropospheric aerosol emissions for scenarios.

  • Property svn:executable set to *
File size: 33.2 KB
Line 
1#--reworked 7/2018 for scenario emissions, ThL
2#--updated on 5/2/2018 with new paths, ThL
3#--updated on 4/5/2017 with improved Sheng & Zwiers algorithm, ThL
4#--corrected some interpolation preprocessing (compared to v4)
5#--updated on 5/5/2017 with correction factor (30/46) on NOx vs. NO
6#--updated on 9/5/2017 with output separation BB/anthro for SO2, NOx and NH3
7#--corrected on 22/6/2017 for BB: undef values zeroed before remapping
8#--corrected on 26/09/2017 for NOx units: PNNL dataset is kg NO2, VUA is kg NO.
9#  INCA expects NO in its AER version.
10#
11#INCA  conc_dms flx_nox flx_bc flx_pom flx_bbbc flx_bbpom flx_so2 flx_so4 flx_h2s flx_nh3
12#CMIP6 species  NOx     BC     OC                         SO2                     NH3
13# + NMVOC CO
14
15#--INCA example file where dms_conc can be reused
16fileINCAex="/home/oboucher/CMIP6/AER_EMISSIONS/INCAfile/sflx_lmdz_phy_1997.nc"
17
18#--input directory for emissions
19dirin="/prodigfs/project/input4MIPs/CMIP6/ScenarioMIP/IAMC/"
20
21#--LMDz grid information
22grid="144x143"
23gridfile="../GRID/grid-lmdz-lonlat_"${grid}
24nbpoint=$((144*141+2))
25
26for scen in "ssp119" "ssp126" "ssp245" "ssp370" "ssp434" "ssp460" "ssp534-over" "ssp585"
27do
28
29#--output directory
30dirout="/data/"${USER}"/CMIP6/AEROSOL/ScenarioMIP/${scen}/v1/"
31if [ ! -d ${dirout} ] ; then mkdir -p ${dirout} ; fi
32
33if [ ${scen} == "ssp119" ] ; then prefix="IMAGE" ; fi
34if [ ${scen} == "ssp126" ] ; then prefix="IMAGE" ; fi
35if [ ${scen} == "ssp245" ] ; then prefix="MESSAGE-GLOBIOM" ; fi
36if [ ${scen} == "ssp370" ] ; then prefix="AIM" ; fi
37if [ ${scen} == "ssp434" ] ; then prefix="GCAM4" ; fi
38if [ ${scen} == "ssp460" ] ; then prefix="GCAM4" ; fi
39if [ ${scen} == "ssp534-over" ] ; then prefix="REMIND-MAGPIE" ; fi
40if [ ${scen} == "ssp585" ] ; then prefix="REMIND-MAGPIE" ; fi
41
42#--Unique file for the whole period, so fixed variables:
43year1=2015
44year2=2100
45
46#--Loop on species (anthro)
47for species in "BC" "NOx" "OC" "SO2" "NH3"
48do
49
50echo "... "${year}" : Dealing with "${species}" anthro input file..."
51
52if [ ${species} = "BC"  ] ; then speciesinca="bc"  ; fi
53if [ ${species} = "NOx" ] ; then speciesinca="nox" ; fi
54if [ ${species} = "OC"  ] ; then speciesinca="pom" ; fi
55if [ ${species} = "SO2" ] ; then speciesinca="so2" ; fi
56if [ ${species} = "NH3" ] ; then speciesinca="nh3" ; fi
57
58if [ ${species} = "BC"  ] ; then speciesUp="BC"  ; fi
59if [ ${species} = "NOx" ] ; then speciesUp="NOX" ; fi
60if [ ${species} = "OC"  ] ; then speciesUp="OC" ; fi
61if [ ${species} = "SO2" ] ; then speciesUp="SO2" ; fi
62if [ ${species} = "NH3" ] ; then speciesUp="NH3" ; fi
63
64#--input file anthro
65filename=${dirin}IAMC-${prefix}-${scen}-1-1/atmos/mon/${species}_em_anthro/gn/v20180628/${species}-em-anthro_input4MIPs_emissions_ScenarioMIP_IAMC-${prefix}-${scen}-1-1_gn_${year1}01-${year2}12.nc
66
67#--input file summed over sectors
68filesum=${dirout}${species}-em-anthro_input4MIPs_emissions_ScenarioMIP_IAMC-${prefix}-${scen}-1-1_gn_${year1}01-${year2}12_sum.nc
69#--reworked input file
70filetmp=${dirout}${species}-em-anthro_input4MIPs_emissions_ScenarioMIP_IAMC-${prefix}-${scen}-1-1_gn_${year1}01-${year2}12_tmp.nc
71#--time-interpolated input file
72fileintanthro=${dirout}${species}-em-anthro_input4MIPs_emissions_ScenarioMIP_IAMC-${prefix}-${scen}-1-1_gn_${year1}01-${year2}12_interp.nc
73
74if [ -f ${fileintanthro} ]
75then
76   echo "...Nothing to do!"
77else
78
79rm -f ${filesum} ${filetmp} ${fileintanthro}
80
81#--re-formatting the input file
82#--summing over sectors
83rm -f sumsectors.jnl
84cat << eod > sumsectors.jnl
85set memory/size=100
86use "${filename}"
87save/clobber/file="${filesum}" ${species}_EM_ANTHRO[k=@sum]
88eod
89#--run ferret script
90ferret << eod
91go sumsectors.jnl
92exit
93eod
94
95#--reformatting input file for IDL to be able to read
96ncks -3 ${filesum} ${filetmp}
97#--cleaning up
98rm -f ${filesum}
99
100#--IDL code for time interpolation, to fill in 9-year gaps in input file
101rm -f time_interp_over_gaps.pro
102cat << eod >> time_interp_over_gaps.pro
103pro time_interp_over_gaps
104
105; Les dimensions des fichiers
106nlon=720
107nlat=360
108nt=1032
109
110une_annee=[31,30,29,31,30,31,30,31,31,30,31,30]
111les_mois = [31,28,31,30,31,30,31,31,30,31,30,31]
112
113jours_in = [15, 45, 74, 105, 135, 166, 196, 227, 258, 288, 319, 349, 1840, 1870, $
114    1899, 1930, 1960, 1991, 2021, 2052, 2083, 2113, 2144, 2174, 5490, 5520, $
115    5549, 5580, 5610, 5641, 5671, 5702, 5733, 5763, 5794, 5824, 9140, 9170, $
116    9199, 9230, 9260, 9291, 9321, 9352, 9383, 9413, 9444, 9474, 12790, 12820, $
117    12849, 12880, 12910, 12941, 12971, 13002, 13033, 13063, 13094, 13124, $
118    16440, 16470, 16499, 16530, 16560, 16591, 16621, 16652, 16683, 16713, $
119    16744, 16774, 20090, 20120, 20149, 20180, 20210, 20241, 20271, 20302, $
120    20333, 20363, 20394, 20424, 23740, 23770, 23799, 23830, 23860, 23891, $
121    23921, 23952, 23983, 24013, 24044, 24074, 27390, 27420, 27449, 27480, $
122    27510, 27541, 27571, 27602, 27633, 27663, 27694, 27724, 31040, 31070, $
123    31099, 31130, 31160, 31191, 31221, 31252, 31283, 31313, 31344, 31374]
124
125jours_out=make_array(1032)
126jours_out(0) = 15
127for cpt=1,1031 do jours_out(cpt) = jours_out(cpt-1) + une_annee(cpt mod 12)
128
129idin = ncdf_open('${filetmp}')
130varid = ncdf_varid(idin,'${speciesUp}_EM_ANTHRO')
131lonid = ncdf_varid(idin,'LON')
132latid = ncdf_varid(idin,'LAT')
133;lonbndid = ncdf_varid(idin,'LON_bnds')
134;latbndid = ncdf_varid(idin,'LAT_bnds')
135timbndid = ncdf_varid(idin,'TIME_bnds')
136ncdf_varget, idin, varid, emiss_init
137ncdf_attget, idin, varid, "units", emiss_unit
138ncdf_attget, idin, varid, "long_name", emiss_longname
139ncdf_varget, idin, lonid, lon
140ncdf_varget, idin, latid, lat
141;ncdf_varget, idin, lonbndid, lonbnds
142;ncdf_varget, idin, latbndid, latbnds
143ncdf_varget, idin, timbndid, timbnds_in
144ncdf_close, idin
145
146timbnds_out = make_array(2,1032)
147timbnds_out(*,0) = [0,31]
148timbnds_out(*,1) = [31,59]
149for cpt=2,1031 do timbnds_out(0,cpt) = timbnds_out(0,cpt-1) + les_mois((cpt-1) mod 12)
150for cpt=1,1030 do timbnds_out(1,cpt) = timbnds_out(0,cpt+1)
151timbnds_out(1,1031) = 31390
152
153emiss_interpol=make_array(nlon,nlat,1032,value=0.)
154
155indices_out_mois = make_array(86,12,value=0)
156for m=0,11 do begin
157   for n=0,85 do indices_out_mois(n,m) = 12*n + m
158endfor
159
160jours_out_mois = make_array(86,12,value=0)
161for n=0,85 do begin
162   for m=0,11 do jours_out_mois(n,m) = jours_out(indices_out_mois(n,m))
163endfor
164
165jours_in_mois = make_array(n_elements(jours_in)/12,12,value=0)
166for m=0,11 do begin
167   for n=0,n_elements(jours_in)/12-1 do jours_in_mois(n,m) = jours_in(n*12 + m)
168endfor
169
170indices_in_mois = make_array(n_elements(jours_in)/12,12,value=0)
171for m=0,11 do begin
172   for n=0,n_elements(jours_in)/12-1 do indices_in_mois(n,m) = where(jours_in eq jours_in_mois(n,m))
173endfor
174
175; Interpolation mois par mois...
176for i=0,nlon-1 do begin
177   for j=0,nlat-1 do begin
178      for m=0,11 do begin
179         emiss_interpol(i,j,indices_out_mois(*,m)) = interpol(emiss_init(i,j,indices_in_mois(*,m)),jours_in_mois(*,m),jours_out_mois(*,m))
180      endfor
181   endfor
182endfor
183
184idout = ncdf_create('${fileintanthro}', /clobber)
185dimlonid = ncdf_dimdef(idout,'lon',nlon)
186dimlatid = ncdf_dimdef(idout,'lat',nlat)
187dimtimid = ncdf_dimdef(idout,'time',1032)
188dimbndid = ncdf_dimdef(idout,'bound',2)
189varlonid = ncdf_vardef(idout, 'lon', [dimlonid], /double)
190varlatid = ncdf_vardef(idout, 'lat', [dimlatid], /double)
191vartimid = ncdf_vardef(idout, 'time', [dimtimid], /double)
192;varlonbndsid = ncdf_vardef(idout, 'lon_bnds', [dimbndid,dimlonid], /double)
193;varlatbndsid = ncdf_vardef(idout, 'lat_bnds', [dimbndid,dimlatid], /double)
194vartimbndsid = ncdf_vardef(idout, 'time_bnds', [dimbndid,dimtimid], /double)
195varemissid = ncdf_vardef(idout, '${species}_em_anthro', [dimlonid,dimlatid,dimtimid], /float)
196ncdf_control, idout, /endef
197ncdf_varput, idout, varlonid, lon
198ncdf_varput, idout, varlatid, lat
199;ncdf_varput, idout, varlonbndsid, lonbnds
200;ncdf_varput, idout, varlatbndsid, latbnds
201ncdf_varput, idout, vartimbndsid, timbnds_out
202ncdf_varput, idout, varemissid, emiss_interpol
203ncdf_varput, idout, vartimid, jours_out
204ncdf_control, idout, /redef
205ncdf_attput, idout, varlonid, "units", "degrees_east"
206ncdf_attput, idout, varlonid, "long_name", "longitude"
207ncdf_attput, idout, varlonid, "axis", "X"
208ncdf_attput, idout, varlonid, "bounds", "lon_bnds"
209ncdf_attput, idout, varlonid, "modulo", 360.
210ncdf_attput, idout, varlonid, "realtopology", "circular"
211ncdf_attput, idout, varlonid, "standard_name", "longitude"
212ncdf_attput, idout, varlonid, "topology", "circular"
213ncdf_attput, idout, varlatid, "units", "degrees_north"
214ncdf_attput, idout, varlatid, "long_name", "latitude"
215ncdf_attput, idout, varlatid, "axis", "Y"
216ncdf_attput, idout, varlatid, "bounds", "lat_bnds"
217ncdf_attput, idout, varlatid, "realtopology", "linear"
218ncdf_attput, idout, varlatid, "standard_name", "latitude"
219ncdf_attput, idout, varemissid, "units", string(emiss_unit)
220ncdf_attput, idout, varemissid, "_FillValue", 1.e+20
221ncdf_attput, idout, varemissid, "cell_methods", "time: mean"
222ncdf_attput, idout, varemissid, "long_name", string(emiss_longname)
223ncdf_attput, idout, varemissid, "missing_value", 1.e+20
224ncdf_attput, idout, vartimid, "units", "days since 2015-01-01 0:0:0"
225ncdf_attput, idout, vartimid, "long_name", "time"
226ncdf_attput, idout, vartimid, "calendar", '365_day'
227ncdf_attput, idout, vartimid, "axis", "T"
228ncdf_attput, idout, vartimid, "bounds", "time_bnds"
229ncdf_attput, idout, vartimid, "realtopology", "linear"
230ncdf_attput, idout, vartimid, "standard_name", "time"
231ncdf_close, idout
232end
233eod
234#
235#--calling IDL
236#
237/opt/idl-6.4/idl/bin/idl << eod
238.r time_interp_over_gaps.pro
239time_interp_over_gaps
240exit
241eod
242#
243
244#--adding lat_bnds and lon_bnds variables
245ncks -A -v lat_bnds,lon_bnds ${filename} ${fileintanthro}
246
247#--cleaning up
248rm -f ${filetmp}
249
250#--end test whether file tmp already exists
251fi
252
253
254
255#--now dealing with the BB input file
256
257echo "... "${year}" : Dealing with "${species}" openburning input file..."
258
259#--input file BB
260filename=${dirin}IAMC-${prefix}-${scen}-1-1/atmos/mon/${species}_em_openburning/gn/v20180628/${species}-em-openburning_input4MIPs_emissions_ScenarioMIP_IAMC-${prefix}-${scen}-1-1_gn_${year1}01-${year2}12.nc
261#--reworked input file
262filetmp=${dirout}${species}-em-openburning_input4MIPs_emissions_ScenarioMIP_IAMC-${prefix}-${scen}-1-1_gn_${year1}01-${year2}12_tmp.nc
263#--time-interpolated input file
264fileintbb=${dirout}${species}-em-openburning_input4MIPs_emissions_ScenarioMIP_IAMC-${prefix}-${scen}-1-1_gn_${year1}01-${year2}12_interp.nc
265
266if [ -f ${fileintbb} ]
267then
268   echo "...Nothing to do!"
269else
270
271rm -f ${filetmp} ${fileintbb}
272
273#--no need to sum over sectors, as none in BB files
274
275#--reformatting input file for IDL to be able to read
276ncks -3 ${filename} ${filetmp}
277
278#--IDL code for time interpolation, to fill in 9-year gaps in input file
279rm -f time_interp_over_gaps.pro
280cat << eod >> time_interp_over_gaps.pro
281pro time_interp_over_gaps
282
283; Les dimensions des fichiers
284nlon=720
285nlat=360
286nt=1032
287
288une_annee=[31,30,29,31,30,31,30,31,31,30,31,30]
289les_mois = [31,28,31,30,31,30,31,31,30,31,30,31]
290
291jours_in = [15, 45, 74, 105, 135, 166, 196, 227, 258, 288, 319, 349, 1840, 1870, $
292    1899, 1930, 1960, 1991, 2021, 2052, 2083, 2113, 2144, 2174, 5490, 5520, $
293    5549, 5580, 5610, 5641, 5671, 5702, 5733, 5763, 5794, 5824, 9140, 9170, $
294    9199, 9230, 9260, 9291, 9321, 9352, 9383, 9413, 9444, 9474, 12790, 12820, $
295    12849, 12880, 12910, 12941, 12971, 13002, 13033, 13063, 13094, 13124, $
296    16440, 16470, 16499, 16530, 16560, 16591, 16621, 16652, 16683, 16713, $
297    16744, 16774, 20090, 20120, 20149, 20180, 20210, 20241, 20271, 20302, $
298    20333, 20363, 20394, 20424, 23740, 23770, 23799, 23830, 23860, 23891, $
299    23921, 23952, 23983, 24013, 24044, 24074, 27390, 27420, 27449, 27480, $
300    27510, 27541, 27571, 27602, 27633, 27663, 27694, 27724, 31040, 31070, $
301    31099, 31130, 31160, 31191, 31221, 31252, 31283, 31313, 31344, 31374]
302
303jours_out=make_array(1032)
304jours_out(0) = 15
305for cpt=1,1031 do jours_out(cpt) = jours_out(cpt-1) + une_annee(cpt mod 12)
306
307idin = ncdf_open('${filetmp}')
308varid = ncdf_varid(idin,'${species}_em_openburning')      ; Here, small caps
309lonid = ncdf_varid(idin,'lon')
310latid = ncdf_varid(idin,'lat')
311;lonbndid = ncdf_varid(idin,'lon_bnds')
312;latbndid = ncdf_varid(idin,'lat_bnds')
313timbndid = ncdf_varid(idin,'time_bnds')
314ncdf_varget, idin, varid, emiss_init
315ncdf_attget, idin, varid, "units", emiss_unit
316ncdf_attget, idin, varid, "long_name", emiss_longname
317ncdf_varget, idin, lonid, lon
318ncdf_varget, idin, latid, lat
319;ncdf_varget, idin, lonbndid, lonbnds
320;ncdf_varget, idin, latbndid, latbnds
321ncdf_varget, idin, timbndid, timbnds_in
322ncdf_close, idin
323
324timbnds_out = make_array(2,1032)
325timbnds_out(*,0) = [0,31]
326timbnds_out(*,1) = [31,59]
327for cpt=2,1031 do timbnds_out(0,cpt) = timbnds_out(0,cpt-1) + une_annee((cpt-1) mod 12)
328for cpt=1,1030 do timbnds_out(1,cpt) = timbnds_out(0,cpt+1)
329timbnds_out(1,1031) = 31390
330
331emiss_interpol=make_array(nlon,nlat,1032,value=0.)
332
333indices_out_mois = make_array(86,12,value=0)
334for m=0,11 do begin
335   for n=0,85 do indices_out_mois(n,m) = 12*n + m
336endfor
337
338jours_out_mois = make_array(86,12,value=0)
339for n=0,85 do begin
340   for m=0,11 do jours_out_mois(n,m) = jours_out(indices_out_mois(n,m))
341endfor
342
343jours_in_mois = make_array(n_elements(jours_in)/12,12,value=0)
344for m=0,11 do begin
345   for n=0,n_elements(jours_in)/12-1 do jours_in_mois(n,m) = jours_in(n*12 + m)
346endfor
347
348indices_in_mois = make_array(n_elements(jours_in)/12,12,value=0)
349for m=0,11 do begin
350   for n=0,n_elements(jours_in)/12-1 do indices_in_mois(n,m) = where(jours_in eq jours_in_mois(n,m))
351endfor
352
353; Interpolation mois par mois...
354for i=0,nlon-1 do begin
355   for j=0,nlat-1 do begin
356      for m=0,11 do begin
357         emiss_interpol(i,j,indices_out_mois(*,m)) = interpol(emiss_init(i,j,indices_in_mois(*,m)),jours_in_mois(*,m),jours_out_mois(*,m))
358      endfor
359   endfor
360endfor
361
362idout = ncdf_create('${fileintbb}', /clobber)
363dimlonid = ncdf_dimdef(idout,'lon',nlon)
364dimlatid = ncdf_dimdef(idout,'lat',nlat)
365dimtimid = ncdf_dimdef(idout,'time',1032)
366dimbndid = ncdf_dimdef(idout,'bound',2)
367varlonid = ncdf_vardef(idout, 'lon', [dimlonid], /double)
368varlatid = ncdf_vardef(idout, 'lat', [dimlatid], /double)
369vartimid = ncdf_vardef(idout, 'time', [dimtimid], /double)
370;varlonbndsid = ncdf_vardef(idout, 'lon_bnds', [dimbndid,dimlonid], /double)
371;varlatbndsid = ncdf_vardef(idout, 'lat_bnds', [dimbndid,dimlatid], /double)
372vartimbndsid = ncdf_vardef(idout, 'time_bnds', [dimbndid,dimtimid], /double)
373varemissid = ncdf_vardef(idout, '${species}_em_openburning', [dimlonid,dimlatid,dimtimid], /float)
374ncdf_control, idout, /endef
375ncdf_varput, idout, varlonid, lon
376ncdf_varput, idout, varlatid, lat
377;ncdf_varput, idout, varlonbndsid, lonbnds
378;ncdf_varput, idout, varlatbndsid, latbnds
379ncdf_varput, idout, vartimbndsid, timbnds_out
380ncdf_varput, idout, varemissid, emiss_interpol
381ncdf_varput, idout, vartimid, jours_out
382ncdf_control, idout, /redef
383ncdf_attput, idout, varlonid, "units", "degrees_east"
384ncdf_attput, idout, varlonid, "long_name", "longitude"
385ncdf_attput, idout, varlonid, "axis", "X"
386ncdf_attput, idout, varlonid, "bounds", "lon_bnds"
387ncdf_attput, idout, varlonid, "modulo", 360.
388ncdf_attput, idout, varlonid, "realtopology", "circular"
389ncdf_attput, idout, varlonid, "standard_name", "longitude"
390ncdf_attput, idout, varlonid, "topology", "circular"
391ncdf_attput, idout, varlatid, "units", "degrees_north"
392ncdf_attput, idout, varlatid, "long_name", "latitude"
393ncdf_attput, idout, varlatid, "axis", "Y"
394ncdf_attput, idout, varlatid, "bounds", "lat_bnds"
395ncdf_attput, idout, varlatid, "realtopology", "linear"
396ncdf_attput, idout, varlatid, "standard_name", "latitude"
397ncdf_attput, idout, varemissid, "units", string(emiss_unit)
398ncdf_attput, idout, varemissid, "_FillValue", 1.e+20
399ncdf_attput, idout, varemissid, "cell_methods", "time: mean"
400ncdf_attput, idout, varemissid, "long_name", string(emiss_longname)
401ncdf_attput, idout, varemissid, "missing_value", 1.e+20
402ncdf_attput, idout, vartimid, "units", "days since 2015-01-01 0:0:0"
403ncdf_attput, idout, vartimid, "long_name", "time"
404ncdf_attput, idout, vartimid, "calendar", '365_day'
405ncdf_attput, idout, vartimid, "axis", "T"
406ncdf_attput, idout, vartimid, "bounds", "time_bnds"
407ncdf_attput, idout, vartimid, "realtopology", "linear"
408ncdf_attput, idout, vartimid, "standard_name", "time"
409ncdf_close, idout
410end
411eod
412#
413#--calling IDL
414#
415/opt/idl-6.4/idl/bin/idl << eod
416.r time_interp_over_gaps.pro
417time_interp_over_gaps
418exit
419eod
420#
421
422#--adding lat_bnds and lon_bnds variables
423ncks -A -v lat_bnds,lon_bnds ${filename} ${fileintbb}
424
425#--cleaning up
426rm -f ${filetmp}
427
428#--end test if interpolated file already exists
429fi
430
431#--end loop on species
432done
433
434
435
436#--anthro and BB input files fileintanthro and fileintbb are now ready, for the whole period and for the current scenario
437#--new double loop on species and years
438
439#--loop on years
440for year in {2015..2100}
441do
442
443#--loop on species
444for species in "BC" "NOx" "OC" "SO2" "NH3"
445do
446
447if [ ${species} = "BC"  ] ; then speciesinca="bc"  ; fi
448if [ ${species} = "NOx" ] ; then speciesinca="nox" ; fi
449if [ ${species} = "OC"  ] ; then speciesinca="pom" ; fi
450if [ ${species} = "SO2" ] ; then speciesinca="so2" ; fi
451if [ ${species} = "NH3" ] ; then speciesinca="nh3" ; fi
452
453#--dealing with anthro
454
455#--input file
456fileintanthro=${dirout}${species}-em-anthro_input4MIPs_emissions_ScenarioMIP_IAMC-${prefix}-${scen}-1-1_gn_${year1}01-${year2}12_interp.nc
457#--output files
458filenameout1=${dirout}flux_${speciesinca}_${year}.nc
459filenameout1b=${dirout}flux_0_${speciesinca}_${year}.nc
460filenameout2=${dirout}flux_lmdz_${speciesinca}_${year}.nc
461filenameout3=${dirout}flux_vector_${speciesinca}_${year}.nc
462
463echo ${fileintanthro} ${filenameout1} ${filenameout2} ${filenameout3}
464rm -f ${filenameout1} ${filenameout2} ${filenameout3}
465
466#--extracting the correct year
467rm -f select_yr.jnl
468cat << eod > select_yr.jnl
469set memory/size=100
470use "${fileintanthro}"
471set region/t=16-jan-${year}:16-dec-${year}
472save/clobber/file="${filenameout1}" ${species}_EM_ANTHRO
473eod
474#--run ferret script
475ferret << eod
476go select_yr.jnl
477exit
478eod
479
480#--replace undef with 0
481cdo setmisstoc,0.0 ${filenameout1} ${filenameout1b}
482
483#--remap to LMDz grid
484#--OC to POM conversion factor
485#--otherwise change to capital letters if not (eg NOx)
486if [ ${species} == "OC"  ] ; then
487echo cdo remapcon,${gridfile} -chname,`echo ${species}_EM_ANTHRO | awk '{print toupper($0)}'`,flux -mulc,1.4 ${filenameout1b} ${filenameout2}
488cdo remapcon,${gridfile} -chname,`echo ${species}_EM_ANTHRO | awk '{print toupper($0)}'`,flux -mulc,1.4 ${filenameout1b} ${filenameout2}
489else
490echo cdo remapcon,${gridfile} -chname,`echo ${species}_EM_ANTHRO | awk '{print toupper($0)}'`,flux  ${filenameout1b} ${filenameout2}
491cdo remapcon,${gridfile} -chname,`echo ${species}_EM_ANTHRO | awk '{print toupper($0)}'`,flux ${filenameout1b} ${filenameout2}
492fi
493
494#--Improved Sheng & Zwiers algorithm + transform into vector
495rm -f regrid.pro
496cat << eod >> regrid.pro
497pro regrid
498filename='${filenameout2}'
499print, filename
500NETCDFREAD,filename,'flux',flux,dimflux
501NETCDFREAD,filename,'lat',lat,dimlat0
502NETCDFREAD,filename,'lon',lon,dimlon0
503NETCDFREAD,filename,'TIME',time,dimtime0
504dimlat=dimlat0(0)
505dimlon=dimlon0(0)
506dimtime=dimtime0(0)
507print, 'dim flux=', dimflux
508A = float([     [3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8.],$
509                [1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0.],$
510                [0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0.],$
511                [0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0.],$
512                [0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0.],$
513                [0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0.],$
514                [0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0.],$
515                [0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0.],$
516                [0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0.],$
517                [0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0.],$
518                [0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8.],$
519                [1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4.]   ])
520fluxinit=flux
521flux_check=flux
522for lo=0,dimlon-1 do begin
523for la=0,dimlat-1 do begin
524flux_check(lo,la,*) = invert(A) ## transpose(fluxinit(lo,la,*))
525endfor
526endfor                 
527m_bloq = make_array(dimlon,dimlat,12,value=0)           ; Matrice booléenne "mois à bloquer ou non"
528if total(where(flux_check lt 0)) ne -1 then m_bloq(where(flux_check lt 0)) = 1
529; Correction/adaptation de la matrice S&Z en fonction du masque booléen m_bloq
530for lo=0,dimlon-1 do begin
531for la=0,dimlat-1 do begin
532whereneg = where(flux_check(lo,la,*) lt 0)              ; (12 pts max) Identification de potentiels points à problÚmes, corrigés négativement
533nbannul=n_elements(whereneg)*(total(whereneg) ne -1)
534flux_corr=flux_check(lo,la,*)                           ; Création d'un vecteur pour recevoir les valeurs corrigées, initialisé à flux_check au cas où on n'ait rien à faire d'autre qu'une seule itération
535A2 = A                                                  ; Je repars de la matrice A initiale, ce pour chaque point de grille
536;                                                       ; Potentiellement plusieurs passages pour éliminer toutes les valeurs négatives
537while nbannul ne 0 do begin                             ; Si l'on a effectivement des émissions corrigées négativement...
538m_bloq(lo,la,whereneg) = 1                              ; Update de la matrice m_bloq
539for m=0,11 do begin
540if m eq 11 then begin                                   ; Pour plus de facilité, mois précédents et suivants codés ici
541p=10
542s=0
543endif else if m eq 0 then begin
544p=11
545s=1
546endif else begin
547p = m-1
548s = m+1
549endelse
550if m_bloq(lo,la,m) then begin                           ; Je traite les mois bloqués en eux-mêmes
551A2(p,m) = 0.
552A2(m,m) = 1.
553A2(s,m) = 0.
554endif                                                   ; Fin du cas si l'on est sur un mois bloqué
555if ~m_bloq(lo,la,m) then begin                          ; Je traite les mois non bloqués, pour ceux adjacents à un mois bloqué
556if m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin               ; Mois encadré de deux mois bloqués
557A2(p,m) = 1./4.
558A2(m,m) = 1./2.
559A2(s,m) = 1./4.
560endif else if m_bloq(lo,la,p) and ~m_bloq(lo,la,s) then begin   ; Mois précédent bloqué (uniquement)
561A2(p,m) = 2./8.
562A2(m,m) = 5./8.
563A2(s,m) = 1./8.
564endif else if ~m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin   ; Mois suivant bloqué (uniquement)
565A2(p,m) = 1./8.
566A2(m,m) = 5./8.
567A2(s,m) = 2./8.
568endif
569endif                                                           ; Fin du cas mois non bloqué
570endfor                                                          ; Fin de la boucle sur les mois, balayage de la matrice
571flux_corr = invert(A2) ## transpose(fluxinit(lo,la,*))          ; Ré-itération de la multiplication matricielle avec la matrice A modifiée (A2)
572whereneg = where(flux_corr lt 0)                                ; (12 pts max) Ré-identification de potentiels nouveaux points à problÚmes, corrigés négativement
573nbannul=n_elements(whereneg)*(total(whereneg) ne -1)
574endwhile                                                        ; Fin du cas où l'on avait des problÚmes d'émissions corrigées négativement
575; *** IMPORTANT ! *** Pour signaler les mois bloqués, on prend la convention suivante :
576;       valeur négative ou nulle <=> mois bloqué
577;       valeur positive <=> mois à interpolation classique
578flux(lo,la,*) = flux_corr                                       ; En sortie de la boucle while, normalement flux_corr est complÚtement positif
579endfor                                                          ; Fin boucle lat
580endfor                                                          ; Fin boucle lon
581nbnegtotal = n_elements(where(m_bloq eq 1)) * (total(where(m_bloq eq 1)) ne -1)
582if nbnegtotal ne 0 then flux(where(m_bloq eq 1)) = -flux(where(m_bloq eq 1))            ; Je force à des valeurs négatives
583;
584month_in_year=12
585nbpoint=${nbpoint}
586flux2=fltarr(nbpoint,month_in_year)
587flux2(*,*)=0.0
588;
589for l=0,month_in_year-1 do begin
590flux2(0,l)=TOTAL(flux(*,0,l))/float(dimlon)
591flux2(nbpoint-1,l)=TOTAL(flux(*,dimlat-1,l))/float(dimlon)
592endfor
593;
594pt=1
595for j=1,dimlat-2  do begin
596for i=0,dimlon-1  do begin
597for l=0,month_in_year-1 do begin
598  flux2(pt,l)=flux(i,j,l)
599endfor
600pt=pt+1
601endfor
602endfor
603;
604;saving netcdf file
605;
606fluxstruct={vector:fltarr(nbpoint),time:fltarr(month_in_year), $
607            flx_${speciesinca}:fltarr(nbpoint,month_in_year) }
608;
609fluxstruct.vector=float(indgen(nbpoint)+1)
610;;fluxstruct.time=float(indgen(month_in_year)+1)
611fluxstruct.time=[15, 45, 75, 105, 135, 165, 195, 225, 255, 285, 315, 345]
612fluxstruct.flx_${speciesinca}=flux2
613;
614attributes = {units:strarr(3),long_name:strarr(3)}
615attributes.units = ['vector','days since 1960-01-01','flux']
616attributes.long_name = ['vector', 'time', 'flux']
617;
618dimensions = {isdim:intarr(3), links:intarr(2,3)}
619       dimensions.isdim =  [1,1,0]  ; (1=dimension, 0=variable)
620       dimensions.links = [ [-1,-1],[-1,-1],[0,1]    ]
621;
622netcdfwrite,'${filenameout3}',fluxstruct,clobber=1, attributes=attributes, dimensions=dimensions
623;
624end
625eod
626#
627#--calling IDL
628#
629/opt/idl-6.4/idl/bin/idl << eod
630.r netcdf
631.r netcdfwrite
632.r struct_dims
633.r regrid
634regrid
635exit
636eod
637#
638
639#--cleaning the mess
640rm -f ferret*
641rm -f regrid.pro
642
643
644#--now dealing with BB
645
646#--input file
647fileintbb=${dirout}${species}-em-openburning_input4MIPs_emissions_ScenarioMIP_IAMC-${prefix}-${scen}-1-1_gn_${year1}01-${year2}12_interp.nc
648#--output files
649filenameout1=${dirout}flux_${speciesinca}bb_${year}.nc
650filenameout1b=${dirout}flux_0_${speciesinca}bb_${year}.nc
651filenameout2=${dirout}flux_lmdz0_${speciesinca}bb_${year}.nc
652filenameout3=${dirout}flux_vector_${speciesinca}bb_${year}.nc
653
654echo ${fileintbb} ${filenameout1} ${filenameout1b} ${filenameout2} ${filenameout3}
655rm -f ${filenameout1} ${filenameout1b} ${filenameout2} ${filenameout3}
656
657#--unfortunately idl not happy with input netcdf files so need to ferretize files
658#--I extract the correct year as well; no need to sum up sectors, as none in BB files
659rm -f rewrite.jnl
660cat << eod > rewrite.jnl
661set memory/size=100
662use "${fileintbb}"
663set region/t=16-jan-${year}:16-dec-${year}
664save/clobber/file="${filenameout1}" ${species}_EM_OPENBURNING
665eod
666#--run ferret script
667ferret << eod
668go rewrite.jnl
669exit
670eod
671
672#--replace undef with 0
673cdo setmisstoc,0.0 ${filenameout1} ${filenameout1b}
674
675#--remap to LMDz grid
676#--OC to POM conversion factor
677#--as ferret returns NOX, treat NOx NOX inconsistency in names by converting to upper case
678if [ ${species} != "OC" ] ; then
679echo cdo remapcon,${gridfile} -chname,`echo ${species}_EM_OPENBURNING | awk '{print toupper($0)}'`,flux ${filenameout1b} ${filenameout2}
680cdo remapcon,${gridfile} -chname,`echo ${species}_EM_OPENBURNING | awk '{print toupper($0)}'`,flux ${filenameout1b} ${filenameout2}
681else
682echo cdo remapcon,${gridfile} -chname,${species}_EM_OPENBURNING,flux -mulc,1.4 ${filenameout1b} ${filenameout2}
683cdo remapcon,${gridfile} -chname,${species}_EM_OPENBURNING,flux -mulc,1.4 ${filenameout1b} ${filenameout2}
684fi
685
686#--Improved Sheng & Zwiers algorithm + transform into vector
687rm -f regrid.pro
688cat << eod >> regrid.pro
689pro regrid
690filename='${filenameout2}'
691print, filename
692NETCDFREAD,filename,'flux',flux,dimflux
693NETCDFREAD,filename,'lat',lat,dimlat0
694NETCDFREAD,filename,'lon',lon,dimlon0
695NETCDFREAD,filename,'TIME',time,dimtime0
696dimlat=dimlat0(0)
697dimlon=dimlon0(0)
698dimtime=dimtime0(0)
699print, 'dim flux=', dimflux
700A = float([     [3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8.],$
701                [1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0.],$
702                [0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0.],$
703                [0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0.],$
704                [0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0.],$
705                [0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0.],$
706                [0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0.],$
707                [0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0.],$
708                [0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0.],$
709                [0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0.],$
710                [0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8.],$
711                [1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4.]   ])
712fluxinit=flux
713flux_check=flux
714for lo=0,dimlon-1 do begin
715for la=0,dimlat-1 do begin
716flux_check(lo,la,*) = invert(A) ## transpose(fluxinit(lo,la,*))
717endfor
718endfor                 
719m_bloq = make_array(dimlon,dimlat,12,value=0)           ; Matrice booléenne "mois à bloquer ou non"
720if total(where(flux_check lt 0)) ne -1 then m_bloq(where(flux_check lt 0)) = 1
721; Correction/adaptation de la matrice S&Z en fonction du masque booléen m_bloq
722for lo=0,dimlon-1 do begin
723for la=0,dimlat-1 do begin
724whereneg = where(flux_check(lo,la,*) lt 0)              ; (12 pts max) Identification de potentiels points à problÚmes, corrigés négativement
725nbannul=n_elements(whereneg)*(total(whereneg) ne -1)
726flux_corr=flux_check(lo,la,*)                           ; Création d'un vecteur pour recevoir les valeurs corrigées, initialisé à flux_check au cas où on n'ait rien à faire (à part 1 seule correction matricielle)
727A2 = A                                                  ; Je repars de la matrice A initiale, ce pour chaque point de grille
728;                                                       ; Potentiellement plusieurs passages pour éliminer toutes les valeurs négatives
729while nbannul ne 0 do begin                             ; Si l'on a effectivement des émissions corrigées négativement...
730m_bloq(lo,la,whereneg) = 1                              ; Update de la matrice m_bloq
731for m=0,11 do begin
732if m eq 11 then begin                                   ; Pour plus de facilité, mois précédents et suivants codés ici
733p=10
734s=0
735endif else if m eq 0 then begin
736p=11
737s=1
738endif else begin
739p = m-1
740s = m+1
741endelse
742if m_bloq(lo,la,m) then begin                           ; Je traite les mois bloqués en eux-mêmes
743A2(p,m) = 0.
744A2(m,m) = 1.
745A2(s,m) = 0.
746endif                                                   ; Fin du cas si l'on est sur un mois bloqué
747if ~m_bloq(lo,la,m) then begin                          ; Je traite les mois non bloqués, pour ceux adjacents à un mois bloqué
748if m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin               ; Mois encadré de deux mois bloqués
749A2(p,m) = 1./4.
750A2(m,m) = 1./2.
751A2(s,m) = 1./4.
752endif else if m_bloq(lo,la,p) and ~m_bloq(lo,la,s) then begin   ; Mois précédent bloqué (uniquement)
753A2(p,m) = 2./8.
754A2(m,m) = 5./8.
755A2(s,m) = 1./8.
756endif else if ~m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin   ; Mois suivant bloqué (uniquement)
757A2(p,m) = 1./8.
758A2(m,m) = 5./8.
759A2(s,m) = 2./8.
760endif
761endif                                                           ; Fin du cas mois non bloqué
762endfor                                                          ; Fin de la boucle sur les mois, balayage de la matrice
763flux_corr = invert(A2) ## transpose(fluxinit(lo,la,*))          ; Ré-itération de la multiplication matricielle avec la matrice A modifiée (A2)
764whereneg = where(flux_corr lt 0)                                ; (12 pts max) Ré-identification de potentiels nouveaux points à problÚmes, corrigés négativement
765nbannul=n_elements(whereneg)*(total(whereneg) ne -1)
766endwhile                                                        ; Fin du cas où l'on avait des problÚmes d'émissions corrigées négativement
767; *** IMPORTANT ! *** Pour signaler les mois bloqués, on prend la convention suivante :
768;       valeur négative ou nulle <=> mois bloqué
769;       valeur positive <=> mois à interpolation classique
770flux(lo,la,*) = flux_corr                                       ; En sortie de la boucle while, normalement flux_corr est complÚtement positif
771endfor                                                          ; Fin boucle lat
772endfor                                                          ; Fin boucle lon
773nbnegtotal = n_elements(where(m_bloq eq 1)) * (total(where(m_bloq eq 1)) ne -1)
774if nbnegtotal ne 0 then flux(where(m_bloq eq 1)) = -flux(where(m_bloq eq 1))            ; Je force à des valeurs négatives
775;
776month_in_year=12
777nbpoint=${nbpoint}
778flux2=fltarr(nbpoint,month_in_year)
779flux2(*,*)=0.0
780;
781for l=0,month_in_year-1 do begin
782flux2(0,l)=TOTAL(flux(*,0,l))/float(dimlon)
783flux2(nbpoint-1,l)=TOTAL(flux(*,dimlat-1,l))/float(dimlon)
784endfor
785;
786pt=1
787for j=1,dimlat-2  do begin
788for i=0,dimlon-1  do begin
789for l=0,month_in_year-1 do begin
790  flux2(pt,l)=flux(i,j,l)
791endfor
792pt=pt+1
793endfor
794endfor
795;
796;saving netcdf file
797;
798fluxstruct={vector:fltarr(nbpoint),time:fltarr(month_in_year), $
799            flx_bb${speciesinca}:fltarr(nbpoint,month_in_year) }
800;
801fluxstruct.vector=float(indgen(nbpoint)+1)
802;;fluxstruct.time=float(indgen(month_in_year)+1)
803fluxstruct.time=[15, 45, 75, 105, 135, 165, 195, 225, 255, 285, 315, 345]
804fluxstruct.flx_bb${speciesinca}=flux2
805;
806attributes = {units:strarr(3),long_name:strarr(3)}
807attributes.units = ['vector','days since 1960-01-01','flux']
808attributes.long_name = ['vector', 'time', 'flux']
809;
810dimensions = {isdim:intarr(3), links:intarr(2,3)}
811       dimensions.isdim =  [1,1,0]  ; (1=dimension, 0=variable)
812       dimensions.links = [ [-1,-1],[-1,-1],[0,1]    ]
813;
814netcdfwrite,'${filenameout3}',fluxstruct,clobber=1, attributes=attributes, dimensions=dimensions
815;
816end
817eod
818#
819#--calling IDL
820#
821/opt/idl-6.4/idl/bin/idl << eod
822.r netcdf
823.r netcdfwrite
824.r struct_dims
825.r regrid
826regrid
827exit
828eod
829#
830
831#--end loop on species
832done
833
834#--unfortunately idl use capital letters for variable names so need to change to small letters for now
835rm -f ${dirout}flux_vector_h2s_${year}.nc ${dirout}flux_vector_so4_${year}.nc
836cdo expr,'flx_h2s=0.*FLX_SO2' ${dirout}flux_vector_so2_${year}.nc ${dirout}flux_vector_h2s_${year}.nc
837cdo expr,'flx_so4=0.*FLX_SO2' ${dirout}flux_vector_so2_${year}.nc ${dirout}flux_vector_so4_${year}.nc
838
839rm -f ${dirout}flux_vector_${year}.nc
840
841#--combining everything into a single file with some final preprocessing
842rm -f ${dirout}flux_vector_noxtot_${year}.nc ${dirout}flux_vector_so2tot_${year}.nc ${dirout}flux_vector_nh3tot_${year}.nc
843
844#--deleting output file if already there
845rm -f ${dirout}sflx_lmdz_cmip6_${year}.nc
846#--merging all files into a single one
847#--anthro NOx is NO2 so 30/46 scaling factor to change to NO
848#--openburning NOx is NO so no change in unit
849cdo merge -expr,'flx_bc=FLX_BC' ${dirout}flux_vector_bc_${year}.nc -expr,'flx_bbbc=FLX_BBBC' ${dirout}flux_vector_bcbb_${year}.nc -expr,'flx_pom=FLX_POM' ${dirout}flux_vector_pom_${year}.nc -expr,'flx_bbpom=FLX_BBPOM' ${dirout}flux_vector_pombb_${year}.nc -expr,'flx_nox=30.*FLX_NOX/46.' ${dirout}flux_vector_nox_${year}.nc -expr,'flx_bbnox=FLX_BBNOX;' ${dirout}flux_vector_noxbb_${year}.nc -expr,'flx_so2=FLX_SO2' ${dirout}flux_vector_so2_${year}.nc -expr,'flx_bbso2=FLX_BBSO2' ${dirout}flux_vector_so2bb_${year}.nc -expr,'flx_nh3=FLX_NH3' ${dirout}flux_vector_nh3_${year}.nc -expr,'flx_bbnh3=FLX_BBNH3' ${dirout}flux_vector_nh3bb_${year}.nc  ${dirout}flux_vector_h2s_${year}.nc ${dirout}flux_vector_so4_${year}.nc -selname,conc_dms ${fileINCAex} ${dirout}sflx_lmdz_cmip6_${year}.nc
850
851ncrename -d VECTOR,vector -v VECTOR,vector ${dirout}sflx_lmdz_cmip6_${year}.nc
852ncrename -d TIME,time     -v TIME,time     ${dirout}sflx_lmdz_cmip6_${year}.nc
853
854#--cleaning up
855#rm -f ${dirout}flux*_${year}.nc
856#--to be uncommented in final version
857
858#--end loop on years
859done
860
861rm -f ${fileintanthro} ${fileintbb}
862
863#--end loop on scenarios
864done
865
866#--cleaning the mess
867rm -f ferret*
868rm -f regrid.pro
869rm -f time_interp_over_gaps.pro sumsectors.jnl select_yr.jnl rewrite.jnl
Note: See TracBrowser for help on using the repository browser.