source: TOOLS/CMIP6_FORCING/CO2_EMISSIONS/regrid_v1.job @ 4320

Last change on this file since 4320 was 4045, checked in by oboucher, 6 years ago

Scripts to prepare CO2 emissions for IPSL-CM6 ESM

  • Property svn:executable set to *
File size: 9.8 KB
Line 
1#PBS -N process_co2
2#PBS -S /bin/bash
3#PBS -q week # there exist: short, day, days3, week...
4#PBS -k eo  # to write the output of stdin
5### Max memory
6#PBS -l vmem=10gb   # virtual memory
7#PBS -l  mem=10gb
8
9#--updated on 4/5/2017 with improved Sheng & Zwiers algorithm, ThL
10#--corrected some interpolation preprocessing (compared to v4)
11#--updated on 9/5/2017 with output separation BB/anthro for SO2, NOx and NH3
12#--corrected on 22/6/2017 for BB: undef values zeroed before remapping
13#
14#--input directory for anthropogenic (non-BB) emissions
15dirinPNNL_2D="/prodigfs/project/input4MIPs/CMIP6/CMIP/PNNL-JGCRI/CEDS-2017-05-18/"
16dirinPNNL_3D="/prodigfs/project/input4MIPs/CMIP6/CMIP/PNNL-JGCRI/CEDS-2017-08-30/"
17
18#--LMDz grid information
19#grid="144x143"
20#nbpoint=$((144*141+2))
21grid="48x36"
22nbpoint=$((48*35+2))
23
24gridfile="/home/oboucher/CMIP6/GRID/grid-lmdz-lonlat_"${grid}
25
26#--output directory
27dirout="/data/obolmd/CMIP6/CO2_EMISSIONS_"${grid}"/"
28if [ ! -d ${dirout} ] ; then mkdir -p ${dirout} ; fi
29
30#--year
31for year in {1850..2014}
32#for year in {2000..2000}
33do
34
35#--species
36for species in "CO2"
37do
38
39#--finding correct file for PNNL data
40if [ $year -ge 1750 -a $year -lt 1800 ]; then
41year1=1750
42year2=1799
43elif [ $year -ge 1800 -a $year -lt 1850 ]; then
44year1=1800
45year2=1849
46elif [ $year == 1850 ]; then
47year1=1850
48year2=1850
49elif [ $year -ge 1851 -a $year -lt 1900 ]; then
50year1=1851
51year2=1899
52elif [ $year -ge 1900 -a $year -lt 1950 ]; then
53year1=1900
54year2=1949
55elif [ $year -ge 1950 -a $year -lt 2000 ]; then
56year1=1950
57year2=1999
58elif [ $year -ge 2000 -a $year -lt 2015 ]; then
59year1=2000
60year2=2014
61else
62echo 'Houston we have a problem for the PNNL data'
63exit 1
64fi
65
66#--input file PNNL surface emissions
67filename2D=${dirinPNNL_2D}/atmos/mon/${species}-em-anthro/gn/v20170519/${species}-em-anthro_input4MIPs_emissions_CMIP_CEDS-2017-05-18_gn_${year1}01-${year2}12.nc
68#--input file PNNL air emissions
69filename3D=${dirinPNNL_3D}/atmos/mon/${species}-em-AIR-anthro/gn/v20170907/${species}-em-AIR-anthro_input4MIPs_emissions_CMIP_CEDS-2017-08-30_gn_${year1}01-${year2}12.nc
70
71#--output files
72filenameout1=${dirout}flux_${species}_${year}.nc
73filenameout2=${dirout}flux_2D_${species}_${year}.nc
74filenameout3=${dirout}flux_1D_${species}_${year}.nc
75
76echo ${filename2D} ${filename3D} ${filenameout1} ${filenameout2} ${filenameout3}
77rm -f ${filenameout1} ${filenameout2} ${filenameout3}
78
79#--unfortunately idl not happy with PNNL netcdf files so need to ferretize files
80#--I also sum over sectors for the 2D file and over altitude for the 3D file and I extract the correct year as well
81rm -f rewrite.jnl
82cat << eod > rewrite.jnl
83use "${filename2D}"
84use "${filename3D}"
85set region/t=16-jan-${year}:16-dec-${year}
86let flux=${species}_EM_ANTHRO[d=1,k=@sum]+${species}_EM_AIR_ANTHRO[d=2,k=@sum]
87save/clobber/file="${filenameout1}" flux
88eod
89
90#--run ferret script
91ferret << eod
92go rewrite.jnl
93exit
94eod
95rm -f rewrite.jnl ferret.jnl
96
97#--remap to LMDz grid
98echo cdo remapcon,${gridfile} -chname,FLUX,flux  ${filenameout1} ${filenameout2}
99cdo remapcon,${gridfile} -chname,FLUX,flux ${filenameout1} ${filenameout2}
100
101#--Improved Sheng & Zwiers algorithm + transform into vector
102rm -f regrid.pro
103cat << eod >> regrid.pro
104pro regrid
105filename='${filenameout2}'
106print, filename
107NETCDFREAD,filename,'flux',flux,dimflux
108NETCDFREAD,filename,'lat',lat,dimlat0
109NETCDFREAD,filename,'lon',lon,dimlon0
110NETCDFREAD,filename,'TIME',time,dimtime0
111dimlat=dimlat0(0)
112dimlon=dimlon0(0)
113dimtime=dimtime0(0)
114print, 'dim flux=', dimflux
115A = float([     [3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8.],$
116                [1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0.],$
117                [0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0.],$
118                [0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0.],$
119                [0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0.],$
120                [0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0.],$
121                [0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0.],$
122                [0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0.],$
123                [0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0.],$
124                [0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0.],$
125                [0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8.],$
126                [1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4.]   ])
127A = float([     [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],$
128                [0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],$
129                [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],$
130                [0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],$
131                [0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],$
132                [0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],$
133                [0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],$
134                [0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],$
135                [0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],$
136                [0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],$
137                [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],$
138                [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]   ])
139fluxinit=flux
140fluxinit=flux
141flux_check=flux
142for lo=0,dimlon-1 do begin
143for la=0,dimlat-1 do begin
144flux_check(lo,la,*) = invert(A) ## transpose(fluxinit(lo,la,*))
145endfor
146endfor                 
147m_bloq = make_array(dimlon,dimlat,12,value=0)           ; Matrice booléenne "mois à bloquer ou non"
148if total(where(flux_check lt 0)) ne -1 then m_bloq(where(flux_check lt 0)) = 1
149; Correction/adaptation de la matrice S&Z en fonction du masque booléen m_bloq
150for lo=0,dimlon-1 do begin
151for la=0,dimlat-1 do begin
152whereneg = where(flux_check(lo,la,*) lt 0)              ; (12 pts max) Identification de potentiels points à problÚmes, corrigés négativement
153nbannul=n_elements(whereneg)*(total(whereneg) ne -1)
154flux_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
155A2 = A                                                  ; Je repars de la matrice A initiale, ce pour chaque point de grille
156;                                                       ; Potentiellement plusieurs passages pour éliminer toutes les valeurs négatives
157while nbannul ne 0 do begin                             ; Si l'on a effectivement des émissions corrigées négativement...
158m_bloq(lo,la,whereneg) = 1                              ; Update de la matrice m_bloq
159for m=0,11 do begin
160if m eq 11 then begin                                   ; Pour plus de facilité, mois précédents et suivants codés ici
161p=10
162s=0
163endif else if m eq 0 then begin
164p=11
165s=1
166endif else begin
167p = m-1
168s = m+1
169endelse
170if m_bloq(lo,la,m) then begin                           ; Je traite les mois bloqués en eux-mêmes
171A2(p,m) = 0.
172A2(m,m) = 1.
173A2(s,m) = 0.
174endif                                                   ; Fin du cas si l'on est sur un mois bloqué
175if ~m_bloq(lo,la,m) then begin                          ; Je traite les mois non bloqués, pour ceux adjacents à un mois bloqué
176if m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin               ; Mois encadré de deux mois bloqués
177A2(p,m) = 1./4.
178A2(m,m) = 1./2.
179A2(s,m) = 1./4.
180endif else if m_bloq(lo,la,p) and ~m_bloq(lo,la,s) then begin   ; Mois précédent bloqué (uniquement)
181A2(p,m) = 2./8.
182A2(m,m) = 5./8.
183A2(s,m) = 1./8.
184endif else if ~m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin   ; Mois suivant bloqué (uniquement)
185A2(p,m) = 1./8.
186A2(m,m) = 5./8.
187A2(s,m) = 2./8.
188endif
189endif                                                           ; Fin du cas mois non bloqué
190endfor                                                          ; Fin de la boucle sur les mois, balayage de la matrice
191flux_corr = invert(A2) ## transpose(fluxinit(lo,la,*))          ; Ré-itération de la multiplication matricielle avec la matrice A modifiée (A2)
192whereneg = where(flux_corr lt 0)                                ; (12 pts max) Ré-identification de potentiels nouveaux points à problÚmes, corrigés négativement
193nbannul=n_elements(whereneg)*(total(whereneg) ne -1)
194endwhile                                                        ; Fin du cas où l'on avait des problÚmes d'émissions corrigées négativement
195; *** IMPORTANT ! *** Pour signaler les mois bloqués, on prend la convention suivante :
196;       valeur négative ou nulle <=> mois bloqué
197;       valeur positive <=> mois à interpolation classique
198flux(lo,la,*) = flux_corr                                       ; En sortie de la boucle while, normalement flux_corr est complÚtement positif
199endfor                                                          ; Fin boucle lat
200endfor                                                          ; Fin boucle lon
201nbnegtotal = n_elements(where(m_bloq eq 1)) * (total(where(m_bloq eq 1)) ne -1)
202if nbnegtotal ne 0 then flux(where(m_bloq eq 1)) = -flux(where(m_bloq eq 1))            ; Je force à des valeurs négatives
203;
204month_in_year=12
205nbpoint=${nbpoint}
206flux2=fltarr(nbpoint,month_in_year)
207flux2(*,*)=0.0
208;
209for l=0,month_in_year-1 do begin
210flux2(0,l)=TOTAL(flux(*,0,l))/float(dimlon)
211flux2(nbpoint-1,l)=TOTAL(flux(*,dimlat-1,l))/float(dimlon)
212endfor
213;
214pt=1
215for j=1,dimlat-2  do begin
216for i=0,dimlon-1  do begin
217for l=0,month_in_year-1 do begin
218  flux2(pt,l)=flux(i,j,l)
219endfor
220pt=pt+1
221endfor
222endfor
223;
224;saving netcdf file
225;
226fluxstruct={vector:fltarr(nbpoint),time:fltarr(month_in_year), $
227            flx_${species}:fltarr(nbpoint,month_in_year) }
228;
229fluxstruct.vector=float(indgen(nbpoint)+1)
230;;fluxstruct.time=float(indgen(month_in_year)+1)
231fluxstruct.time=[15, 45, 75, 105, 135, 165, 195, 225, 255, 285, 315, 345]
232fluxstruct.flx_${species}=flux2
233;
234attributes = {units:strarr(3),long_name:strarr(3)}
235attributes.units = ['vector','days since 1960-01-01','flux']
236attributes.long_name = ['vector', 'time', 'flux']
237;
238dimensions = {isdim:intarr(3), links:intarr(2,3)}
239       dimensions.isdim =  [1,1,0]  ; (1=dimension, 0=variable)
240       dimensions.links = [ [-1,-1],[-1,-1],[0,1]    ]
241;
242netcdfwrite,'${filenameout3}',fluxstruct,clobber=1, attributes=attributes, dimensions=dimensions
243;
244end
245eod
246
247#
248#--calling IDL
249#
250/opt/idl-6.4/idl/bin/idl << eod
251.r netcdf
252.r netcdfwrite
253.r struct_dims
254.r regrid
255regrid
256exit
257eod
258#
259
260#--end loop on species
261done
262
263#--deleting output file if already there
264#rm -f ${dirout}co2ff_lmdz_cmip6_${year}.nc
265
266#--rename a few things
267#cdo expr,'flx_co2=FLX_CO2' ${dirout}flux_vector_CO2_${year}.nc ${dirout}co2ff_lmdz_cmip6_${year}.nc
268#ncrename -d VECTOR,vector -v VECTOR,vector ${dirout}co2ff_lmdz_cmip6_${year}.nc
269#ncrename -d TIME,time     -v TIME,time     ${dirout}co2ff_lmdz_cmip6_${year}.nc
270
271#--cleaning up
272#rm -f ${dirout}flux*_${year}.nc
273rm -f ${filenameout1}
274
275#--end loop on years
276done
277
278#--cleaning the mess
279rm -f ferret*
280rm -f regrid.pro
281rm -f rewrite.jnl
Note: See TracBrowser for help on using the repository browser.