[4045] | 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 |
---|
| 15 | dirinPNNL_2D="/prodigfs/project/input4MIPs/CMIP6/CMIP/PNNL-JGCRI/CEDS-2017-05-18/" |
---|
| 16 | dirinPNNL_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)) |
---|
| 21 | grid="48x36" |
---|
| 22 | nbpoint=$((48*35+2)) |
---|
| 23 | |
---|
| 24 | gridfile="/home/oboucher/CMIP6/GRID/grid-lmdz-lonlat_"${grid} |
---|
| 25 | |
---|
| 26 | #--output directory |
---|
| 27 | dirout="/data/obolmd/CMIP6/CO2_EMISSIONS_"${grid}"/" |
---|
| 28 | if [ ! -d ${dirout} ] ; then mkdir -p ${dirout} ; fi |
---|
| 29 | |
---|
| 30 | #--year |
---|
| 31 | for year in {1850..2014} |
---|
| 32 | #for year in {2000..2000} |
---|
| 33 | do |
---|
| 34 | |
---|
| 35 | #--species |
---|
| 36 | for species in "CO2" |
---|
| 37 | do |
---|
| 38 | |
---|
| 39 | #--finding correct file for PNNL data |
---|
| 40 | if [ $year -ge 1750 -a $year -lt 1800 ]; then |
---|
| 41 | year1=1750 |
---|
| 42 | year2=1799 |
---|
| 43 | elif [ $year -ge 1800 -a $year -lt 1850 ]; then |
---|
| 44 | year1=1800 |
---|
| 45 | year2=1849 |
---|
| 46 | elif [ $year == 1850 ]; then |
---|
| 47 | year1=1850 |
---|
| 48 | year2=1850 |
---|
| 49 | elif [ $year -ge 1851 -a $year -lt 1900 ]; then |
---|
| 50 | year1=1851 |
---|
| 51 | year2=1899 |
---|
| 52 | elif [ $year -ge 1900 -a $year -lt 1950 ]; then |
---|
| 53 | year1=1900 |
---|
| 54 | year2=1949 |
---|
| 55 | elif [ $year -ge 1950 -a $year -lt 2000 ]; then |
---|
| 56 | year1=1950 |
---|
| 57 | year2=1999 |
---|
| 58 | elif [ $year -ge 2000 -a $year -lt 2015 ]; then |
---|
| 59 | year1=2000 |
---|
| 60 | year2=2014 |
---|
| 61 | else |
---|
| 62 | echo 'Houston we have a problem for the PNNL data' |
---|
| 63 | exit 1 |
---|
| 64 | fi |
---|
| 65 | |
---|
| 66 | #--input file PNNL surface emissions |
---|
| 67 | filename2D=${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 |
---|
| 69 | filename3D=${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 |
---|
| 72 | filenameout1=${dirout}flux_${species}_${year}.nc |
---|
| 73 | filenameout2=${dirout}flux_2D_${species}_${year}.nc |
---|
| 74 | filenameout3=${dirout}flux_1D_${species}_${year}.nc |
---|
| 75 | |
---|
| 76 | echo ${filename2D} ${filename3D} ${filenameout1} ${filenameout2} ${filenameout3} |
---|
| 77 | rm -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 |
---|
| 81 | rm -f rewrite.jnl |
---|
| 82 | cat << eod > rewrite.jnl |
---|
| 83 | use "${filename2D}" |
---|
| 84 | use "${filename3D}" |
---|
| 85 | set region/t=16-jan-${year}:16-dec-${year} |
---|
| 86 | let flux=${species}_EM_ANTHRO[d=1,k=@sum]+${species}_EM_AIR_ANTHRO[d=2,k=@sum] |
---|
| 87 | save/clobber/file="${filenameout1}" flux |
---|
| 88 | eod |
---|
| 89 | |
---|
| 90 | #--run ferret script |
---|
| 91 | ferret << eod |
---|
| 92 | go rewrite.jnl |
---|
| 93 | exit |
---|
| 94 | eod |
---|
| 95 | rm -f rewrite.jnl ferret.jnl |
---|
| 96 | |
---|
| 97 | #--remap to LMDz grid |
---|
| 98 | echo cdo remapcon,${gridfile} -chname,FLUX,flux ${filenameout1} ${filenameout2} |
---|
| 99 | cdo remapcon,${gridfile} -chname,FLUX,flux ${filenameout1} ${filenameout2} |
---|
| 100 | |
---|
| 101 | #--Improved Sheng & Zwiers algorithm + transform into vector |
---|
| 102 | rm -f regrid.pro |
---|
| 103 | cat << eod >> regrid.pro |
---|
| 104 | pro regrid |
---|
| 105 | filename='${filenameout2}' |
---|
| 106 | print, filename |
---|
| 107 | NETCDFREAD,filename,'flux',flux,dimflux |
---|
| 108 | NETCDFREAD,filename,'lat',lat,dimlat0 |
---|
| 109 | NETCDFREAD,filename,'lon',lon,dimlon0 |
---|
| 110 | NETCDFREAD,filename,'TIME',time,dimtime0 |
---|
| 111 | dimlat=dimlat0(0) |
---|
| 112 | dimlon=dimlon0(0) |
---|
| 113 | dimtime=dimtime0(0) |
---|
| 114 | print, 'dim flux=', dimflux |
---|
| 115 | A = 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.] ]) |
---|
| 127 | A = 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.] ]) |
---|
| 139 | fluxinit=flux |
---|
| 140 | fluxinit=flux |
---|
| 141 | flux_check=flux |
---|
| 142 | for lo=0,dimlon-1 do begin |
---|
| 143 | for la=0,dimlat-1 do begin |
---|
| 144 | flux_check(lo,la,*) = invert(A) ## transpose(fluxinit(lo,la,*)) |
---|
| 145 | endfor |
---|
| 146 | endfor |
---|
| 147 | m_bloq = make_array(dimlon,dimlat,12,value=0) ; Matrice booléenne "mois à bloquer ou non" |
---|
| 148 | if 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 |
---|
| 150 | for lo=0,dimlon-1 do begin |
---|
| 151 | for la=0,dimlat-1 do begin |
---|
| 152 | whereneg = where(flux_check(lo,la,*) lt 0) ; (12 pts max) Identification de potentiels points à problÚmes, corrigés négativement |
---|
| 153 | nbannul=n_elements(whereneg)*(total(whereneg) ne -1) |
---|
| 154 | 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 |
---|
| 155 | A2 = 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 |
---|
| 157 | while nbannul ne 0 do begin ; Si l'on a effectivement des émissions corrigées négativement... |
---|
| 158 | m_bloq(lo,la,whereneg) = 1 ; Update de la matrice m_bloq |
---|
| 159 | for m=0,11 do begin |
---|
| 160 | if m eq 11 then begin ; Pour plus de facilité, mois précédents et suivants codés ici |
---|
| 161 | p=10 |
---|
| 162 | s=0 |
---|
| 163 | endif else if m eq 0 then begin |
---|
| 164 | p=11 |
---|
| 165 | s=1 |
---|
| 166 | endif else begin |
---|
| 167 | p = m-1 |
---|
| 168 | s = m+1 |
---|
| 169 | endelse |
---|
| 170 | if m_bloq(lo,la,m) then begin ; Je traite les mois bloqués en eux-mêmes |
---|
| 171 | A2(p,m) = 0. |
---|
| 172 | A2(m,m) = 1. |
---|
| 173 | A2(s,m) = 0. |
---|
| 174 | endif ; Fin du cas si l'on est sur un mois bloqué |
---|
| 175 | if ~m_bloq(lo,la,m) then begin ; Je traite les mois non bloqués, pour ceux adjacents à un mois bloqué |
---|
| 176 | if m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin ; Mois encadré de deux mois bloqués |
---|
| 177 | A2(p,m) = 1./4. |
---|
| 178 | A2(m,m) = 1./2. |
---|
| 179 | A2(s,m) = 1./4. |
---|
| 180 | endif else if m_bloq(lo,la,p) and ~m_bloq(lo,la,s) then begin ; Mois précédent bloqué (uniquement) |
---|
| 181 | A2(p,m) = 2./8. |
---|
| 182 | A2(m,m) = 5./8. |
---|
| 183 | A2(s,m) = 1./8. |
---|
| 184 | endif else if ~m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin ; Mois suivant bloqué (uniquement) |
---|
| 185 | A2(p,m) = 1./8. |
---|
| 186 | A2(m,m) = 5./8. |
---|
| 187 | A2(s,m) = 2./8. |
---|
| 188 | endif |
---|
| 189 | endif ; Fin du cas mois non bloqué |
---|
| 190 | endfor ; Fin de la boucle sur les mois, balayage de la matrice |
---|
| 191 | flux_corr = invert(A2) ## transpose(fluxinit(lo,la,*)) ; Ré-itération de la multiplication matricielle avec la matrice A modifiée (A2) |
---|
| 192 | whereneg = where(flux_corr lt 0) ; (12 pts max) Ré-identification de potentiels nouveaux points à problÚmes, corrigés négativement |
---|
| 193 | nbannul=n_elements(whereneg)*(total(whereneg) ne -1) |
---|
| 194 | endwhile ; 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 |
---|
| 198 | flux(lo,la,*) = flux_corr ; En sortie de la boucle while, normalement flux_corr est complÚtement positif |
---|
| 199 | endfor ; Fin boucle lat |
---|
| 200 | endfor ; Fin boucle lon |
---|
| 201 | nbnegtotal = n_elements(where(m_bloq eq 1)) * (total(where(m_bloq eq 1)) ne -1) |
---|
| 202 | if nbnegtotal ne 0 then flux(where(m_bloq eq 1)) = -flux(where(m_bloq eq 1)) ; Je force à des valeurs négatives |
---|
| 203 | ; |
---|
| 204 | month_in_year=12 |
---|
| 205 | nbpoint=${nbpoint} |
---|
| 206 | flux2=fltarr(nbpoint,month_in_year) |
---|
| 207 | flux2(*,*)=0.0 |
---|
| 208 | ; |
---|
| 209 | for l=0,month_in_year-1 do begin |
---|
| 210 | flux2(0,l)=TOTAL(flux(*,0,l))/float(dimlon) |
---|
| 211 | flux2(nbpoint-1,l)=TOTAL(flux(*,dimlat-1,l))/float(dimlon) |
---|
| 212 | endfor |
---|
| 213 | ; |
---|
| 214 | pt=1 |
---|
| 215 | for j=1,dimlat-2 do begin |
---|
| 216 | for i=0,dimlon-1 do begin |
---|
| 217 | for l=0,month_in_year-1 do begin |
---|
| 218 | flux2(pt,l)=flux(i,j,l) |
---|
| 219 | endfor |
---|
| 220 | pt=pt+1 |
---|
| 221 | endfor |
---|
| 222 | endfor |
---|
| 223 | ; |
---|
| 224 | ;saving netcdf file |
---|
| 225 | ; |
---|
| 226 | fluxstruct={vector:fltarr(nbpoint),time:fltarr(month_in_year), $ |
---|
| 227 | flx_${species}:fltarr(nbpoint,month_in_year) } |
---|
| 228 | ; |
---|
| 229 | fluxstruct.vector=float(indgen(nbpoint)+1) |
---|
| 230 | ;;fluxstruct.time=float(indgen(month_in_year)+1) |
---|
| 231 | fluxstruct.time=[15, 45, 75, 105, 135, 165, 195, 225, 255, 285, 315, 345] |
---|
| 232 | fluxstruct.flx_${species}=flux2 |
---|
| 233 | ; |
---|
| 234 | attributes = {units:strarr(3),long_name:strarr(3)} |
---|
| 235 | attributes.units = ['vector','days since 1960-01-01','flux'] |
---|
| 236 | attributes.long_name = ['vector', 'time', 'flux'] |
---|
| 237 | ; |
---|
| 238 | dimensions = {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 | ; |
---|
| 242 | netcdfwrite,'${filenameout3}',fluxstruct,clobber=1, attributes=attributes, dimensions=dimensions |
---|
| 243 | ; |
---|
| 244 | end |
---|
| 245 | eod |
---|
| 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 |
---|
| 255 | regrid |
---|
| 256 | exit |
---|
| 257 | eod |
---|
| 258 | # |
---|
| 259 | |
---|
| 260 | #--end loop on species |
---|
| 261 | done |
---|
| 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 |
---|
| 273 | rm -f ${filenameout1} |
---|
| 274 | |
---|
| 275 | #--end loop on years |
---|
| 276 | done |
---|
| 277 | |
---|
| 278 | #--cleaning the mess |
---|
| 279 | rm -f ferret* |
---|
| 280 | rm -f regrid.pro |
---|
| 281 | rm -f rewrite.jnl |
---|