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

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

Corrected script to generate sflx files for scenarios (bug on 2080's decade).

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