1 | #--corrected 2080 bug 9/2018, ThL |
---|
2 | #--reworked 7/2018 for scenario emissions, ThL |
---|
3 | #--updated on 5/2/2018 with new paths, ThL |
---|
4 | #--updated on 4/5/2017 with improved Sheng & Zwiers algorithm, ThL |
---|
5 | #--corrected some interpolation preprocessing (compared to v4) |
---|
6 | #--updated on 5/5/2017 with correction factor (30/46) on NOx vs. NO |
---|
7 | #--updated on 9/5/2017 with output separation BB/anthro for SO2, NOx and NH3 |
---|
8 | #--corrected on 22/6/2017 for BB: undef values zeroed before remapping |
---|
9 | #--corrected on 26/09/2017 for NOx units: PNNL dataset is kg NO2, VUA is kg NO. |
---|
10 | # INCA expects NO in its AER version. |
---|
11 | # |
---|
12 | #INCA conc_dms flx_nox flx_bc flx_pom flx_bbbc flx_bbpom flx_so2 flx_so4 flx_h2s flx_nh3 |
---|
13 | #CMIP6 species NOx BC OC SO2 NH3 |
---|
14 | # + NMVOC CO |
---|
15 | |
---|
16 | #--INCA example file where dms_conc can be reused |
---|
17 | fileINCAex="/home/oboucher/CMIP6/AER_EMISSIONS/INCAfile/sflx_lmdz_phy_1997.nc" |
---|
18 | |
---|
19 | #--input directory for anthropogenic (non-BB) emissions |
---|
20 | dirin="/prodigfs/project/input4MIPs/CMIP6/ScenarioMIP/IAMC/" |
---|
21 | |
---|
22 | #--LMDz grid information |
---|
23 | grid="144x143" |
---|
24 | gridfile="../GRID/grid-lmdz-lonlat_"${grid} |
---|
25 | nbpoint=$((144*141+2)) |
---|
26 | |
---|
27 | for scen in "ssp119" "ssp126" "ssp245" "ssp370" "ssp434" "ssp460" "ssp534-over" "ssp585" |
---|
28 | do |
---|
29 | |
---|
30 | #--output directory |
---|
31 | dirout="/data/"${USER}"/CMIP6/AEROSOL/ScenarioMIP/${scen}/v2_correcte/" |
---|
32 | if [ ! -d ${dirout} ] ; then mkdir -p ${dirout} ; fi |
---|
33 | |
---|
34 | if [ ${scen} == "ssp119" ] ; then prefix="IMAGE" ; fi |
---|
35 | if [ ${scen} == "ssp126" ] ; then prefix="IMAGE" ; fi |
---|
36 | if [ ${scen} == "ssp245" ] ; then prefix="MESSAGE-GLOBIOM" ; fi |
---|
37 | if [ ${scen} == "ssp370" ] ; then prefix="AIM" ; fi |
---|
38 | if [ ${scen} == "ssp434" ] ; then prefix="GCAM4" ; fi |
---|
39 | if [ ${scen} == "ssp460" ] ; then prefix="GCAM4" ; fi |
---|
40 | if [ ${scen} == "ssp534-over" ] ; then prefix="REMIND-MAGPIE" ; fi |
---|
41 | if [ ${scen} == "ssp585" ] ; then prefix="REMIND-MAGPIE" ; fi |
---|
42 | |
---|
43 | #--loop on years |
---|
44 | for year in {2015..2100} |
---|
45 | do |
---|
46 | |
---|
47 | if [ ${year} -ge "2015" -a ${year} -lt "2020" ] |
---|
48 | then |
---|
49 | year1=2015 |
---|
50 | year2=2020 |
---|
51 | fi |
---|
52 | if [ ${year} -ge "2020" -a ${year} -lt "2030" ] |
---|
53 | then |
---|
54 | year1=2020 |
---|
55 | year2=2030 |
---|
56 | fi |
---|
57 | if [ ${year} -ge "2030" -a ${year} -lt "2040" ] |
---|
58 | then |
---|
59 | year1=2030 |
---|
60 | year2=2040 |
---|
61 | fi |
---|
62 | if [ ${year} -ge "2040" -a ${year} -lt "2050" ] |
---|
63 | then |
---|
64 | year1=2040 |
---|
65 | year2=2050 |
---|
66 | fi |
---|
67 | if [ ${year} -ge "2050" -a ${year} -lt "2060" ] |
---|
68 | then |
---|
69 | year1=2050 |
---|
70 | year2=2060 |
---|
71 | fi |
---|
72 | if [ ${year} -ge "2060" -a ${year} -lt "2070" ] |
---|
73 | then |
---|
74 | year1=2060 |
---|
75 | year2=2070 |
---|
76 | fi |
---|
77 | if [ ${year} -ge "2070" -a ${year} -lt "2080" ] |
---|
78 | then |
---|
79 | year1=2070 |
---|
80 | year2=2080 |
---|
81 | fi |
---|
82 | if [ ${year} -ge "2080" -a ${year} -lt "2090" ] |
---|
83 | then |
---|
84 | year1=2080 |
---|
85 | year2=2090 |
---|
86 | fi |
---|
87 | if [ ${year} -ge "2090" -a ${year} -lt "2100" ] |
---|
88 | then |
---|
89 | year1=2090 |
---|
90 | year2=2100 |
---|
91 | fi |
---|
92 | if [ ${year} == 2100 ] |
---|
93 | then |
---|
94 | year1=2100 |
---|
95 | year2=2100 |
---|
96 | fi |
---|
97 | #--weights for linear interpolation |
---|
98 | if [ ${year} == 2015 ] |
---|
99 | then |
---|
100 | pond1="1" |
---|
101 | pond2="0" |
---|
102 | fi |
---|
103 | if [ ${year} == 2016 ] |
---|
104 | then |
---|
105 | pond1="0.8" |
---|
106 | pond2="0.2" |
---|
107 | fi |
---|
108 | if [ ${year} == 2017 ] |
---|
109 | then |
---|
110 | pond1="0.6" |
---|
111 | pond2="0.4" |
---|
112 | fi |
---|
113 | if [ ${year} == 2018 ] |
---|
114 | then |
---|
115 | pond1="0.4" |
---|
116 | pond2="0.6" |
---|
117 | fi |
---|
118 | if [ ${year} == 2019 ] |
---|
119 | then |
---|
120 | pond1="0.2" |
---|
121 | pond2="0.8" |
---|
122 | fi |
---|
123 | if [ ${year} == 2020 -o ${year} == 2030 -o ${year} == 2040 -o ${year} == 2050 -o ${year} == 2060 -o ${year} == 2070 -o ${year} == 2080 -o ${year} == 2090 ] |
---|
124 | then |
---|
125 | pond1="1" |
---|
126 | pond2="0" |
---|
127 | fi |
---|
128 | if [ ${year} == 2021 -o ${year} == 2031 -o ${year} == 2041 -o ${year} == 2051 -o ${year} == 2061 -o ${year} == 2071 -o ${year} == 2081 -o ${year} == 2091 ] |
---|
129 | then |
---|
130 | pond1="0.9" |
---|
131 | pond2="0.1" |
---|
132 | fi |
---|
133 | if [ ${year} == 2022 -o ${year} == 2032 -o ${year} == 2042 -o ${year} == 2052 -o ${year} == 2062 -o ${year} == 2072 -o ${year} == 2082 -o ${year} == 2092 ] |
---|
134 | then |
---|
135 | pond1="0.8" |
---|
136 | pond2="0.2" |
---|
137 | fi |
---|
138 | if [ ${year} == 2023 -o ${year} == 2033 -o ${year} == 2043 -o ${year} == 2053 -o ${year} == 2063 -o ${year} == 2073 -o ${year} == 2083 -o ${year} == 2093 ] |
---|
139 | then |
---|
140 | pond1="0.7" |
---|
141 | pond2="0.3" |
---|
142 | fi |
---|
143 | if [ ${year} == 2024 -o ${year} == 2034 -o ${year} == 2044 -o ${year} == 2054 -o ${year} == 2064 -o ${year} == 2074 -o ${year} == 2084 -o ${year} == 2094 ] |
---|
144 | then |
---|
145 | pond1="0.6" |
---|
146 | pond2="0.4" |
---|
147 | fi |
---|
148 | if [ ${year} == 2025 -o ${year} == 2035 -o ${year} == 2045 -o ${year} == 2055 -o ${year} == 2065 -o ${year} == 2075 -o ${year} == 2085 -o ${year} == 2095 ] |
---|
149 | then |
---|
150 | pond1="0.5" |
---|
151 | pond2="0.5" |
---|
152 | fi |
---|
153 | if [ ${year} == 2026 -o ${year} == 2036 -o ${year} == 2046 -o ${year} == 2056 -o ${year} == 2066 -o ${year} == 2076 -o ${year} == 2086 -o ${year} == 2096 ] |
---|
154 | then |
---|
155 | pond1="0.4" |
---|
156 | pond2="0.6" |
---|
157 | fi |
---|
158 | if [ ${year} == 2027 -o ${year} == 2037 -o ${year} == 2047 -o ${year} == 2057 -o ${year} == 2067 -o ${year} == 2077 -o ${year} == 2087 -o ${year} == 2097 ] |
---|
159 | then |
---|
160 | pond1="0.3" |
---|
161 | pond2="0.7" |
---|
162 | fi |
---|
163 | if [ ${year} == 2028 -o ${year} == 2038 -o ${year} == 2048 -o ${year} == 2058 -o ${year} == 2068 -o ${year} == 2078 -o ${year} == 2088 -o ${year} == 2098 ] |
---|
164 | then |
---|
165 | pond1="0.2" |
---|
166 | pond2="0.8" |
---|
167 | fi |
---|
168 | if [ ${year} == 2029 -o ${year} == 2039 -o ${year} == 2049 -o ${year} == 2059 -o ${year} == 2069 -o ${year} == 2079 -o ${year} == 2089 -o ${year} == 2099 ] |
---|
169 | then |
---|
170 | pond1="0.1" |
---|
171 | pond2="0.9" |
---|
172 | fi |
---|
173 | if [ ${year} == 2100 ] |
---|
174 | then |
---|
175 | pond1="0" |
---|
176 | pond2="1" |
---|
177 | fi |
---|
178 | |
---|
179 | #--species |
---|
180 | for species in "BC" "NOx" "OC" "SO2" "NH3" |
---|
181 | do |
---|
182 | |
---|
183 | if [ ${species} = "BC" ] ; then speciesinca="bc" ; fi |
---|
184 | if [ ${species} = "NOx" ] ; then speciesinca="nox" ; fi |
---|
185 | if [ ${species} = "OC" ] ; then speciesinca="pom" ; fi |
---|
186 | if [ ${species} = "SO2" ] ; then speciesinca="so2" ; fi |
---|
187 | if [ ${species} = "NH3" ] ; then speciesinca="nh3" ; fi |
---|
188 | |
---|
189 | if [ ${species} = "BC" ] ; then speciesUp="BC" ; fi |
---|
190 | if [ ${species} = "NOx" ] ; then speciesUp="NOX" ; fi |
---|
191 | if [ ${species} = "OC" ] ; then speciesUp="OC" ; fi |
---|
192 | if [ ${species} = "SO2" ] ; then speciesUp="SO2" ; fi |
---|
193 | if [ ${species} = "NH3" ] ; then speciesUp="NH3" ; fi |
---|
194 | |
---|
195 | #--Dealing with anthro emissions file... |
---|
196 | |
---|
197 | echo "... "${year}" : Dealing with "${species}" anthro input file..." |
---|
198 | |
---|
199 | #--input file anthro (fossil fuel emissions) |
---|
200 | filename=${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_201501-210012.nc |
---|
201 | |
---|
202 | #--two temporary files for interpolation |
---|
203 | filetmp1=${dirout}tmp1.nc |
---|
204 | filetmp2=${dirout}tmp2.nc |
---|
205 | |
---|
206 | #--output files |
---|
207 | filenameout1=${dirout}flux_${speciesinca}_${year}.nc |
---|
208 | filenameout1b=${dirout}flux_0_${speciesinca}_${year}.nc |
---|
209 | filenameout2=${dirout}flux_lmdz_${speciesinca}_${year}.nc |
---|
210 | filenameout3=${dirout}flux_vector_${speciesinca}_${year}.nc |
---|
211 | |
---|
212 | echo ${filename} ${filenameout1} ${filenameout2} ${filenameout3} |
---|
213 | rm -f ${filetmp1} ${filetmp2} ${filenameout1} ${filenameout2} ${filenameout3} |
---|
214 | |
---|
215 | #--unfortunately idl not happy with input netcdf files so need to ferretize files |
---|
216 | #--I also sum over sectors and I extract the correct years as well |
---|
217 | #--first year (start of 5-Y or 10-Y period, for later interpolation) |
---|
218 | rm -f rewrite.jnl |
---|
219 | cat << eod > rewrite.jnl |
---|
220 | use "${filename}" |
---|
221 | set region/t=16-jan-${year1}:16-dec-${year1} |
---|
222 | save/clobber/file="${filetmp1}" ${speciesUp}_EM_ANTHRO[k=@sum] |
---|
223 | eod |
---|
224 | #--run ferret script |
---|
225 | ferret << eod |
---|
226 | go rewrite.jnl |
---|
227 | exit |
---|
228 | eod |
---|
229 | #--second year (end of 5-Y or 10-Y period, for later interpolation) |
---|
230 | rm -f rewrite.jnl |
---|
231 | cat << eod > rewrite.jnl |
---|
232 | use "${filename}" |
---|
233 | set region/t=16-jan-${year2}:16-dec-${year2} |
---|
234 | save/clobber/file="${filetmp2}" ${speciesUp}_EM_ANTHRO[k=@sum] |
---|
235 | eod |
---|
236 | #--run ferret script |
---|
237 | ferret << eod |
---|
238 | go rewrite.jnl |
---|
239 | exit |
---|
240 | eod |
---|
241 | |
---|
242 | #--performing time interpolation |
---|
243 | cdo add -mulc,${pond1} -selname,${speciesUp}_EM_ANTHRO ${filetmp1} -mulc,${pond2} -selname,${speciesUp}_EM_ANTHRO ${filetmp2} ${filenameout1} |
---|
244 | |
---|
245 | cdo setmisstoc,0.0 ${filenameout1} ${filenameout1b} |
---|
246 | |
---|
247 | #--remap to LMDz grid |
---|
248 | #--OC to POM conversion factor |
---|
249 | #--otherwise change to capital letters if not (eg NOx) |
---|
250 | if [ ${species} == "OC" ] ; then |
---|
251 | echo cdo remapcon,${gridfile} -chname,`echo ${species}_EM_ANTHRO | awk '{print toupper($0)}'`,flux -mulc,1.4 ${filenameout1b} ${filenameout2} |
---|
252 | cdo remapcon,${gridfile} -chname,`echo ${species}_EM_ANTHRO | awk '{print toupper($0)}'`,flux -mulc,1.4 ${filenameout1b} ${filenameout2} |
---|
253 | else |
---|
254 | echo cdo remapcon,${gridfile} -chname,`echo ${species}_EM_ANTHRO | awk '{print toupper($0)}'`,flux ${filenameout1b} ${filenameout2} |
---|
255 | cdo remapcon,${gridfile} -chname,`echo ${species}_EM_ANTHRO | awk '{print toupper($0)}'`,flux ${filenameout1b} ${filenameout2} |
---|
256 | fi |
---|
257 | |
---|
258 | #--Improved Sheng & Zwiers algorithm + transform into vector |
---|
259 | rm -f regrid.pro |
---|
260 | cat << eod >> regrid.pro |
---|
261 | pro regrid |
---|
262 | filename='${filenameout2}' |
---|
263 | print, filename |
---|
264 | NETCDFREAD,filename,'flux',flux,dimflux |
---|
265 | NETCDFREAD,filename,'lat',lat,dimlat0 |
---|
266 | NETCDFREAD,filename,'lon',lon,dimlon0 |
---|
267 | NETCDFREAD,filename,'TIME',time,dimtime0 |
---|
268 | dimlat=dimlat0(0) |
---|
269 | dimlon=dimlon0(0) |
---|
270 | dimtime=dimtime0(0) |
---|
271 | print, 'dim flux=', dimflux |
---|
272 | A = float([ [3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8.],$ |
---|
273 | [1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0.],$ |
---|
274 | [0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0.],$ |
---|
275 | [0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0.],$ |
---|
276 | [0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0.],$ |
---|
277 | [0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0.],$ |
---|
278 | [0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0.],$ |
---|
279 | [0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0.],$ |
---|
280 | [0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0.],$ |
---|
281 | [0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0.],$ |
---|
282 | [0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8.],$ |
---|
283 | [1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4.] ]) |
---|
284 | fluxinit=flux |
---|
285 | flux_check=flux |
---|
286 | for lo=0,dimlon-1 do begin |
---|
287 | for la=0,dimlat-1 do begin |
---|
288 | flux_check(lo,la,*) = invert(A) ## transpose(fluxinit(lo,la,*)) |
---|
289 | endfor |
---|
290 | endfor |
---|
291 | m_bloq = make_array(dimlon,dimlat,12,value=0) ; Matrice booléenne "mois à bloquer ou non" |
---|
292 | if total(where(flux_check lt 0)) ne -1 then m_bloq(where(flux_check lt 0)) = 1 |
---|
293 | ; Correction/adaptation de la matrice S&Z en fonction du masque booléen m_bloq |
---|
294 | for lo=0,dimlon-1 do begin |
---|
295 | for la=0,dimlat-1 do begin |
---|
296 | whereneg = where(flux_check(lo,la,*) lt 0) ; (12 pts max) Identification de potentiels points à problÚmes, corrigés négativement |
---|
297 | nbannul=n_elements(whereneg)*(total(whereneg) ne -1) |
---|
298 | flux_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 |
---|
299 | A2 = A ; Je repars de la matrice A initiale, ce pour chaque point de grille |
---|
300 | ; ; Potentiellement plusieurs passages pour éliminer toutes les valeurs négatives |
---|
301 | while nbannul ne 0 do begin ; Si l'on a effectivement des émissions corrigées négativement... |
---|
302 | m_bloq(lo,la,whereneg) = 1 ; Update de la matrice m_bloq |
---|
303 | for m=0,11 do begin |
---|
304 | if m eq 11 then begin ; Pour plus de facilité, mois précédents et suivants codés ici |
---|
305 | p=10 |
---|
306 | s=0 |
---|
307 | endif else if m eq 0 then begin |
---|
308 | p=11 |
---|
309 | s=1 |
---|
310 | endif else begin |
---|
311 | p = m-1 |
---|
312 | s = m+1 |
---|
313 | endelse |
---|
314 | if m_bloq(lo,la,m) then begin ; Je traite les mois bloqués en eux-mêmes |
---|
315 | A2(p,m) = 0. |
---|
316 | A2(m,m) = 1. |
---|
317 | A2(s,m) = 0. |
---|
318 | endif ; Fin du cas si l'on est sur un mois bloqué |
---|
319 | if ~m_bloq(lo,la,m) then begin ; Je traite les mois non bloqués, pour ceux adjacents à un mois bloqué |
---|
320 | if m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin ; Mois encadré de deux mois bloqués |
---|
321 | A2(p,m) = 1./4. |
---|
322 | A2(m,m) = 1./2. |
---|
323 | A2(s,m) = 1./4. |
---|
324 | endif else if m_bloq(lo,la,p) and ~m_bloq(lo,la,s) then begin ; Mois précédent bloqué (uniquement) |
---|
325 | A2(p,m) = 2./8. |
---|
326 | A2(m,m) = 5./8. |
---|
327 | A2(s,m) = 1./8. |
---|
328 | endif else if ~m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin ; Mois suivant bloqué (uniquement) |
---|
329 | A2(p,m) = 1./8. |
---|
330 | A2(m,m) = 5./8. |
---|
331 | A2(s,m) = 2./8. |
---|
332 | endif |
---|
333 | endif ; Fin du cas mois non bloqué |
---|
334 | endfor ; Fin de la boucle sur les mois, balayage de la matrice |
---|
335 | flux_corr = invert(A2) ## transpose(fluxinit(lo,la,*)) ; Ré-itération de la multiplication matricielle avec la matrice A modifiée (A2) |
---|
336 | whereneg = where(flux_corr lt 0) ; (12 pts max) Ré-identification de potentiels nouveaux points à problÚmes, corrigés négativement |
---|
337 | nbannul=n_elements(whereneg)*(total(whereneg) ne -1) |
---|
338 | endwhile ; Fin du cas où l'on avait des problÚmes d'émissions corrigées négativement |
---|
339 | ; *** IMPORTANT ! *** Pour signaler les mois bloqués, on prend la convention suivante : |
---|
340 | ; valeur négative ou nulle <=> mois bloqué |
---|
341 | ; valeur positive <=> mois à interpolation classique |
---|
342 | flux(lo,la,*) = flux_corr ; En sortie de la boucle while, normalement flux_corr est complÚtement positif |
---|
343 | endfor ; Fin boucle lat |
---|
344 | endfor ; Fin boucle lon |
---|
345 | nbnegtotal = n_elements(where(m_bloq eq 1)) * (total(where(m_bloq eq 1)) ne -1) |
---|
346 | if nbnegtotal ne 0 then flux(where(m_bloq eq 1)) = -flux(where(m_bloq eq 1)) ; Je force à des valeurs négatives |
---|
347 | ; |
---|
348 | month_in_year=12 |
---|
349 | nbpoint=${nbpoint} |
---|
350 | flux2=fltarr(nbpoint,month_in_year) |
---|
351 | flux2(*,*)=0.0 |
---|
352 | ; |
---|
353 | for l=0,month_in_year-1 do begin |
---|
354 | flux2(0,l)=TOTAL(flux(*,0,l))/float(dimlon) |
---|
355 | flux2(nbpoint-1,l)=TOTAL(flux(*,dimlat-1,l))/float(dimlon) |
---|
356 | endfor |
---|
357 | ; |
---|
358 | pt=1 |
---|
359 | for j=1,dimlat-2 do begin |
---|
360 | for i=0,dimlon-1 do begin |
---|
361 | for l=0,month_in_year-1 do begin |
---|
362 | flux2(pt,l)=flux(i,j,l) |
---|
363 | endfor |
---|
364 | pt=pt+1 |
---|
365 | endfor |
---|
366 | endfor |
---|
367 | ; |
---|
368 | ;saving netcdf file |
---|
369 | ; |
---|
370 | fluxstruct={vector:fltarr(nbpoint),time:fltarr(month_in_year), $ |
---|
371 | flx_${speciesinca}:fltarr(nbpoint,month_in_year) } |
---|
372 | ; |
---|
373 | fluxstruct.vector=float(indgen(nbpoint)+1) |
---|
374 | ;;fluxstruct.time=float(indgen(month_in_year)+1) |
---|
375 | fluxstruct.time=[15, 45, 75, 105, 135, 165, 195, 225, 255, 285, 315, 345] |
---|
376 | fluxstruct.flx_${speciesinca}=flux2 |
---|
377 | ; |
---|
378 | attributes = {units:strarr(3),long_name:strarr(3)} |
---|
379 | attributes.units = ['vector','days since 1960-01-01','flux'] |
---|
380 | attributes.long_name = ['vector', 'time', 'flux'] |
---|
381 | ; |
---|
382 | dimensions = {isdim:intarr(3), links:intarr(2,3)} |
---|
383 | dimensions.isdim = [1,1,0] ; (1=dimension, 0=variable) |
---|
384 | dimensions.links = [ [-1,-1],[-1,-1],[0,1] ] |
---|
385 | ; |
---|
386 | netcdfwrite,'${filenameout3}',fluxstruct,clobber=1, attributes=attributes, dimensions=dimensions |
---|
387 | ; |
---|
388 | end |
---|
389 | eod |
---|
390 | |
---|
391 | # |
---|
392 | #--calling IDL |
---|
393 | # |
---|
394 | /opt/idl-6.4/idl/bin/idl << eod |
---|
395 | .r netcdf |
---|
396 | .r netcdfwrite |
---|
397 | .r struct_dims |
---|
398 | .r regrid |
---|
399 | regrid |
---|
400 | exit |
---|
401 | eod |
---|
402 | # |
---|
403 | |
---|
404 | |
---|
405 | #--now dealing with BB sources |
---|
406 | echo "... "${year}" : Dealing with "${species}" openburning input file..." |
---|
407 | |
---|
408 | filename=${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_201501-210012.nc |
---|
409 | |
---|
410 | if [ ${species} = "BC" ] ; then speciesinca="bc" ; fi |
---|
411 | if [ ${species} = "NOx" ] ; then speciesinca="nox" ; fi |
---|
412 | if [ ${species} = "OC" ] ; then speciesinca="pom" ; fi |
---|
413 | if [ ${species} = "SO2" ] ; then speciesinca="so2" ; fi |
---|
414 | if [ ${species} = "NH3" ] ; then speciesinca="nh3" ; fi |
---|
415 | |
---|
416 | #--two temporary files for interpolation |
---|
417 | filetmp1=${dirout}tmp1.nc |
---|
418 | filetmp2=${dirout}tmp2.nc |
---|
419 | |
---|
420 | #--output files |
---|
421 | filenameout1=${dirout}flux_${speciesinca}bb_${year}.nc |
---|
422 | filenameout1b=${dirout}flux_0_${speciesinca}bb_${year}.nc |
---|
423 | filenameout2=${dirout}flux_lmdz0_${speciesinca}bb_${year}.nc |
---|
424 | filenameout3=${dirout}flux_vector_${speciesinca}bb_${year}.nc |
---|
425 | |
---|
426 | echo ${filename} ${filenameout1} ${filenameout1b} ${filenameout2} ${filenameout3} |
---|
427 | rm -f ${filetmp1} ${filetmp2} ${filenameout1} ${filenameout1b} ${filenameout2} ${filenameout3} |
---|
428 | |
---|
429 | #--unfortunately idl not happy with input netcdf files so need to ferretize files |
---|
430 | #--I also sum over sectors and I extract the correct years as well |
---|
431 | #--first year (start of 5-Y or 10-Y period, for later interpolation) |
---|
432 | rm -f rewrite.jnl |
---|
433 | cat << eod > rewrite.jnl |
---|
434 | use "${filename}" |
---|
435 | set region/t=16-jan-${year1}:16-dec-${year1} |
---|
436 | save/clobber/file="${filetmp1}" ${speciesUp}_EM_OPENBURNING[k=@sum] |
---|
437 | eod |
---|
438 | #--run ferret script |
---|
439 | ferret << eod |
---|
440 | go rewrite.jnl |
---|
441 | exit |
---|
442 | eod |
---|
443 | #--second year (end of 5-Y or 10-Y period, for later interpolation) |
---|
444 | rm -f rewrite.jnl |
---|
445 | cat << eod > rewrite.jnl |
---|
446 | use "${filename}" |
---|
447 | set region/t=16-jan-${year2}:16-dec-${year2} |
---|
448 | save/clobber/file="${filetmp2}" ${speciesUp}_EM_OPENBURNING[k=@sum] |
---|
449 | eod |
---|
450 | #--run ferret script |
---|
451 | ferret << eod |
---|
452 | go rewrite.jnl |
---|
453 | exit |
---|
454 | eod |
---|
455 | |
---|
456 | #--performing time interpolation |
---|
457 | cdo add -mulc,${pond1} -selname,${speciesUp}_EM_OPENBURNING ${filetmp1} -mulc,${pond2} -selname,${speciesUp}_EM_OPENBURNING ${filetmp2} ${filenameout1} |
---|
458 | |
---|
459 | #--replace undef with 0 |
---|
460 | cdo setmisstoc,0.0 ${filenameout1} ${filenameout1b} |
---|
461 | |
---|
462 | #--remap to LMDz grid |
---|
463 | #--OC to POM conversion factor |
---|
464 | #--as ferret returns NOX, treat NOx NOX inconsistency in names by converting to upper case |
---|
465 | if [ ${species} != "OC" ] ; then |
---|
466 | echo cdo remapcon,${gridfile} -chname,`echo ${species}_EM_OPENBURNING | awk '{print toupper($0)}'`,flux ${filenameout1b} ${filenameout2} |
---|
467 | cdo remapcon,${gridfile} -chname,`echo ${species}_EM_OPENBURNING | awk '{print toupper($0)}'`,flux ${filenameout1b} ${filenameout2} |
---|
468 | else |
---|
469 | echo cdo remapcon,${gridfile} -chname,${species}_EM_OPENBURNING,flux -mulc,1.4 ${filenameout1b} ${filenameout2} |
---|
470 | cdo remapcon,${gridfile} -chname,${species}_EM_OPENBURNING,flux -mulc,1.4 ${filenameout1b} ${filenameout2} |
---|
471 | fi |
---|
472 | |
---|
473 | #--Improved Sheng & Zwiers algorithm + transform into vector |
---|
474 | rm -f regrid.pro |
---|
475 | cat << eod >> regrid.pro |
---|
476 | pro regrid |
---|
477 | filename='${filenameout2}' |
---|
478 | print, filename |
---|
479 | NETCDFREAD,filename,'flux',flux,dimflux |
---|
480 | NETCDFREAD,filename,'lat',lat,dimlat0 |
---|
481 | NETCDFREAD,filename,'lon',lon,dimlon0 |
---|
482 | NETCDFREAD,filename,'TIME',time,dimtime0 |
---|
483 | dimlat=dimlat0(0) |
---|
484 | dimlon=dimlon0(0) |
---|
485 | dimtime=dimtime0(0) |
---|
486 | print, 'dim flux=', dimflux |
---|
487 | A = float([ [3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8.],$ |
---|
488 | [1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0.],$ |
---|
489 | [0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0.],$ |
---|
490 | [0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0.],$ |
---|
491 | [0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0.],$ |
---|
492 | [0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0.],$ |
---|
493 | [0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0.],$ |
---|
494 | [0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0.],$ |
---|
495 | [0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0.],$ |
---|
496 | [0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0.],$ |
---|
497 | [0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8.],$ |
---|
498 | [1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4.] ]) |
---|
499 | fluxinit=flux |
---|
500 | flux_check=flux |
---|
501 | for lo=0,dimlon-1 do begin |
---|
502 | for la=0,dimlat-1 do begin |
---|
503 | flux_check(lo,la,*) = invert(A) ## transpose(fluxinit(lo,la,*)) |
---|
504 | endfor |
---|
505 | endfor |
---|
506 | m_bloq = make_array(dimlon,dimlat,12,value=0) ; Matrice booléenne "mois à bloquer ou non" |
---|
507 | if total(where(flux_check lt 0)) ne -1 then m_bloq(where(flux_check lt 0)) = 1 |
---|
508 | ; Correction/adaptation de la matrice S&Z en fonction du masque booléen m_bloq |
---|
509 | for lo=0,dimlon-1 do begin |
---|
510 | for la=0,dimlat-1 do begin |
---|
511 | whereneg = where(flux_check(lo,la,*) lt 0) ; (12 pts max) Identification de potentiels points à problÚmes, corrigés négativement |
---|
512 | nbannul=n_elements(whereneg)*(total(whereneg) ne -1) |
---|
513 | flux_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) |
---|
514 | A2 = A ; Je repars de la matrice A initiale, ce pour chaque point de grille |
---|
515 | ; ; Potentiellement plusieurs passages pour éliminer toutes les valeurs négatives |
---|
516 | while nbannul ne 0 do begin ; Si l'on a effectivement des émissions corrigées négativement... |
---|
517 | m_bloq(lo,la,whereneg) = 1 ; Update de la matrice m_bloq |
---|
518 | for m=0,11 do begin |
---|
519 | if m eq 11 then begin ; Pour plus de facilité, mois précédents et suivants codés ici |
---|
520 | p=10 |
---|
521 | s=0 |
---|
522 | endif else if m eq 0 then begin |
---|
523 | p=11 |
---|
524 | s=1 |
---|
525 | endif else begin |
---|
526 | p = m-1 |
---|
527 | s = m+1 |
---|
528 | endelse |
---|
529 | if m_bloq(lo,la,m) then begin ; Je traite les mois bloqués en eux-mêmes |
---|
530 | A2(p,m) = 0. |
---|
531 | A2(m,m) = 1. |
---|
532 | A2(s,m) = 0. |
---|
533 | endif ; Fin du cas si l'on est sur un mois bloqué |
---|
534 | if ~m_bloq(lo,la,m) then begin ; Je traite les mois non bloqués, pour ceux adjacents à un mois bloqué |
---|
535 | if m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin ; Mois encadré de deux mois bloqués |
---|
536 | A2(p,m) = 1./4. |
---|
537 | A2(m,m) = 1./2. |
---|
538 | A2(s,m) = 1./4. |
---|
539 | endif else if m_bloq(lo,la,p) and ~m_bloq(lo,la,s) then begin ; Mois précédent bloqué (uniquement) |
---|
540 | A2(p,m) = 2./8. |
---|
541 | A2(m,m) = 5./8. |
---|
542 | A2(s,m) = 1./8. |
---|
543 | endif else if ~m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin ; Mois suivant bloqué (uniquement) |
---|
544 | A2(p,m) = 1./8. |
---|
545 | A2(m,m) = 5./8. |
---|
546 | A2(s,m) = 2./8. |
---|
547 | endif |
---|
548 | endif ; Fin du cas mois non bloqué |
---|
549 | endfor ; Fin de la boucle sur les mois, balayage de la matrice |
---|
550 | flux_corr = invert(A2) ## transpose(fluxinit(lo,la,*)) ; Ré-itération de la multiplication matricielle avec la matrice A modifiée (A2) |
---|
551 | whereneg = where(flux_corr lt 0) ; (12 pts max) Ré-identification de potentiels nouveaux points à problÚmes, corrigés négativement |
---|
552 | nbannul=n_elements(whereneg)*(total(whereneg) ne -1) |
---|
553 | endwhile ; Fin du cas où l'on avait des problÚmes d'émissions corrigées négativement |
---|
554 | ; *** IMPORTANT ! *** Pour signaler les mois bloqués, on prend la convention suivante : |
---|
555 | ; valeur négative ou nulle <=> mois bloqué |
---|
556 | ; valeur positive <=> mois à interpolation classique |
---|
557 | flux(lo,la,*) = flux_corr ; En sortie de la boucle while, normalement flux_corr est complÚtement positif |
---|
558 | endfor ; Fin boucle lat |
---|
559 | endfor ; Fin boucle lon |
---|
560 | nbnegtotal = n_elements(where(m_bloq eq 1)) * (total(where(m_bloq eq 1)) ne -1) |
---|
561 | if nbnegtotal ne 0 then flux(where(m_bloq eq 1)) = -flux(where(m_bloq eq 1)) ; Je force à des valeurs négatives |
---|
562 | ; |
---|
563 | month_in_year=12 |
---|
564 | nbpoint=${nbpoint} |
---|
565 | flux2=fltarr(nbpoint,month_in_year) |
---|
566 | flux2(*,*)=0.0 |
---|
567 | ; |
---|
568 | for l=0,month_in_year-1 do begin |
---|
569 | flux2(0,l)=TOTAL(flux(*,0,l))/float(dimlon) |
---|
570 | flux2(nbpoint-1,l)=TOTAL(flux(*,dimlat-1,l))/float(dimlon) |
---|
571 | endfor |
---|
572 | ; |
---|
573 | pt=1 |
---|
574 | for j=1,dimlat-2 do begin |
---|
575 | for i=0,dimlon-1 do begin |
---|
576 | for l=0,month_in_year-1 do begin |
---|
577 | flux2(pt,l)=flux(i,j,l) |
---|
578 | endfor |
---|
579 | pt=pt+1 |
---|
580 | endfor |
---|
581 | endfor |
---|
582 | ; |
---|
583 | ;saving netcdf file |
---|
584 | ; |
---|
585 | fluxstruct={vector:fltarr(nbpoint),time:fltarr(month_in_year), $ |
---|
586 | flx_bb${speciesinca}:fltarr(nbpoint,month_in_year) } |
---|
587 | ; |
---|
588 | fluxstruct.vector=float(indgen(nbpoint)+1) |
---|
589 | ;;fluxstruct.time=float(indgen(month_in_year)+1) |
---|
590 | fluxstruct.time=[15, 45, 75, 105, 135, 165, 195, 225, 255, 285, 315, 345] |
---|
591 | fluxstruct.flx_bb${speciesinca}=flux2 |
---|
592 | ; |
---|
593 | attributes = {units:strarr(3),long_name:strarr(3)} |
---|
594 | attributes.units = ['vector','days since 1960-01-01','flux'] |
---|
595 | attributes.long_name = ['vector', 'time', 'flux'] |
---|
596 | ; |
---|
597 | dimensions = {isdim:intarr(3), links:intarr(2,3)} |
---|
598 | dimensions.isdim = [1,1,0] ; (1=dimension, 0=variable) |
---|
599 | dimensions.links = [ [-1,-1],[-1,-1],[0,1] ] |
---|
600 | ; |
---|
601 | netcdfwrite,'${filenameout3}',fluxstruct,clobber=1, attributes=attributes, dimensions=dimensions |
---|
602 | ; |
---|
603 | end |
---|
604 | eod |
---|
605 | |
---|
606 | # |
---|
607 | #--calling IDL |
---|
608 | # |
---|
609 | /opt/idl-6.4/idl/bin/idl << eod |
---|
610 | .r netcdf |
---|
611 | .r netcdfwrite |
---|
612 | .r struct_dims |
---|
613 | .r regrid |
---|
614 | regrid |
---|
615 | exit |
---|
616 | eod |
---|
617 | # |
---|
618 | |
---|
619 | #--end loop on species |
---|
620 | done |
---|
621 | |
---|
622 | #--cleaning up |
---|
623 | rm -f ferret* |
---|
624 | |
---|
625 | #--unfortunately idl use capital letters for variable names so need to change to small letters for now |
---|
626 | rm -f ${dirout}flux_vector_h2s_${year}.nc ${dirout}flux_vector_so4_${year}.nc |
---|
627 | cdo expr,'flx_h2s=0.*FLX_SO2' ${dirout}flux_vector_so2_${year}.nc ${dirout}flux_vector_h2s_${year}.nc |
---|
628 | cdo expr,'flx_so4=0.*FLX_SO2' ${dirout}flux_vector_so2_${year}.nc ${dirout}flux_vector_so4_${year}.nc |
---|
629 | |
---|
630 | rm -f ${dirout}flux_vector_${year}.nc |
---|
631 | |
---|
632 | #--combining everything into a single file with some final preprocessing |
---|
633 | rm -f ${dirout}flux_vector_noxtot_${year}.nc ${dirout}flux_vector_so2tot_${year}.nc ${dirout}flux_vector_nh3tot_${year}.nc |
---|
634 | |
---|
635 | #--deleting output file if already there |
---|
636 | rm -f ${dirout}sflx_lmdz_cmip6_${year}.nc |
---|
637 | #--merging all files into a single one |
---|
638 | #--PNNL NOx is NO2 so 30/46 scaling factor to change to NO |
---|
639 | #--VUA NOx is NO so no change in unit |
---|
640 | cdo 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 |
---|
641 | |
---|
642 | ncrename -d VECTOR,vector -v VECTOR,vector ${dirout}sflx_lmdz_cmip6_${year}.nc |
---|
643 | ncrename -d TIME,time -v TIME,time ${dirout}sflx_lmdz_cmip6_${year}.nc |
---|
644 | |
---|
645 | #--cleaning up |
---|
646 | rm -f ${dirout}flux*_${year}.nc |
---|
647 | #--to be uncommented in final script |
---|
648 | |
---|
649 | #--end loop on years |
---|
650 | done |
---|
651 | |
---|
652 | #--end loop on scenarios |
---|
653 | done |
---|
654 | |
---|
655 | #--cleaning the mess |
---|
656 | rm -f ferret* |
---|
657 | rm -f regrid.pro |
---|
658 | rm -f rewrite.jnl |
---|