-
Notifications
You must be signed in to change notification settings - Fork 2
/
15_Had_maps.r
209 lines (185 loc) · 7.07 KB
/
15_Had_maps.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# module load R/3.4.4-intel-2018a-X11-20180131
rm(list=ls())
Sys.setenv(TZ='GMT')
library(ncdf4)
library(fields)
library(abind)
library(RColorBrewer)
library(rgdal)
library(rworldmap)
library(raster)
library(sp)
##----------------------------------------------------------------
# User settings
##----------------------------------------------------------------
# dates etc.
syyyy = 1980
eyyyy = 2016
expids = c("RH-10-20","SOD08-SCH19","SCH20","ALLPBL")
cexpids = c("RH-10","SCH19","SCH20","ALL-ABL")
exps = c("linear")
cities = c(1001,3001,5002)
rr = 1.5 # grid box around city: +-1 (+0.5 for the grid cell)
## paths
opath = "figures"
ipath = "data/postpro/"
spath = "data/staticdata/"
# colors and breaks
ccol = "black"
ibreaks = c(seq(0,0.9,0.1),seq(1,10,1),20,30)
icols = c(colorRampPalette(c("white",brewer.pal("YlOrBr",n=9)))(length(ibreaks)-3),"#800026","#e31a1c")
# additional functions
source("functions/mask2dfield.r")
source("functions/rotate.r")
source("functions/matrix2dany.r")
source("functions/coords2continent.r")
source("functions/mapplot.r")
source("functions/plotcity.r")
source("functions/plotlegend.r")
source("citysettings.r")
##----------------------------------------------------------------
## --- MAIN PROGRAM --- ##
##----------------------------------------------------------------
## -- (1) -- READ STATIC DATA
# -- grid
lon = seq(-180,179,1)
lat = seq(-90,90,1)
nlon = length(lon)
nlat = length(lat)
alon = array(rep(lon, nlat),c(nlon,nlat))
alat = array(rep(lat,each=nlon),c(nlon,nlat))
# -- ERA-Interim land mask
ifile = paste(spath,"/eafc_1x1.nc",sep="")
ncfile= nc_open(ifile)
olandmask = rotate(t(ncvar_get(ncfile,"var172")))
landmask = array(NA, dim(olandmask))
landmask[1:180,] = olandmask[181:360,]
landmask[181:360,] = olandmask[1:180,]
# -- continent mask: 0: ocean, 2: Africa, 3: Antarctica, 4: Australia, 5: Europe, 6: North America, 7: South America, 8: Asia
points = data.frame(lon=c(alon),lat=c(alat))
cmask = array(coords2continent(points),c(nlon,nlat))
cname = c(NA,"Africa","Antarctica","Australia","Europe","North America","South America","Asia")
cvalue = seq(1,8,1)
# coastlines
coastlines= readOGR(path.expand(paste(spath,"/coastlines/",sep="")),layer="ne_10m_coastline")
# areas
areafile = paste(spath,"/areas_1x1.nc",sep="")
areas2d = ncvar_get(nc_open(areafile),"area")
# city mask
citymaskf = paste("data/masks/mask_cities3x3.nc",sep="")
citymask = ncvar_get(nc_open(citymaskf),"mask")
##----------------------------------------------------------------
## -- (2) -- READ ATTRIBUTION DATA
##----------------------------------------------------------------
attdata = list()
contributionmask = list()
sourcemask = list()
# loop over all cities
for (icity in as.character(cities)){
# read data for each city: varying experiments (exps) and setups (expids), all variables
for (iexp in exps){
for (iexpid in expids){
iipath=sprintf("%s/%s/%s",ipath,icity,iexp)
ifile=sprintf("%s/%s_biascor-attr_%s_%s_%s-%s_mean.nc",iipath,icity,iexpid,iexp,syyyy,eyyyy)
idata = list()
for (ivar in c("Had","Had_Hs")){
idata[[ivar]] = ncvar_get(nc_open(ifile),ivar)
}
attdata[[icity]][[iexpid]][[iexp]] = idata
}
}
## source region masks per city (set grid cells which are never a source region to NA)
# - contribution mask (x/48 experiments indicate a source region)
# - source mask (any: yes/no)
ialldata = unlist(unlist(attdata[[icity]],recursive = FALSE),recursive = FALSE)
contributionmask[[icity]] = array(rowSums(sapply(ialldata,FUN=matrixany)),dim(alon))
sourcemask[[icity]] = (contributionmask[[icity]]>=1)+0
# set never-source-regions to NA
for (iexp in exps){
for (iexpid in expids){
attdata[[icity]][[iexpid]][[iexp]] = lapply(attdata[[icity]][[iexpid]][[iexp]],
FUN=mask2dfield,
mask=sourcemask[[icity]],keep=1)
}
}
}
# get totals for text...
had = list()
for (icity in as.character(cities)){
# city settings
carea = sum(mask2dfield(areas2d,mask=citymask,keep=as.numeric(icity)),na.rm=T)
# get heat advection values
iexp="linear"
ivars=c("Had","Had_Hs")
for (ivar in ivars){
for (iexpid in expids){
itoplot = sum(attdata[[icity]][[iexpid]][[iexp]][[ivar]]*areas2d, na.rm=T)
ivalue = itoplot / carea
had[[icity]][[iexpid]][[iexp]][[ivar]] = ivalue
}
}
}
##----------------------------------------------------------------
## -- (3) -- PLOT
##----------------------------------------------------------------
for (icity in as.character(cities)){
# city settings
cname = gdata[[icity]]$cname
clon = gdata[[icity]]$clon
clat = gdata[[icity]]$clat
xrange = gdata[[icity]]$xrange
yrange = gdata[[icity]]$yrange
carea = sum(mask2dfield(areas2d,mask=citymask,keep=icity),na.rm=T)
## PLOT: one pdf for each exps (linear, linear_upscaled, ...)
for (iexp in exps){
ofile = paste(opath,"/Had_maps_bias-correction_",cname,"_",iexp,"_",syyyy,"-",eyyyy,"_mean.pdf",sep="")
pdf(ofile, width=20, height=8.25, onefile = TRUE, family = "sans", fonts = NULL, version = "1.2", pointsize=14)
# 4x 3-panel plot incl. legend
par(oma=c(0,3,3,0.5),mgp=c(1.75,0.5,0))
layout(matrix(c(seq(1,8,1),rep(9,4)), ncol=4, byrow=T), width=c(0.25,0.25,0.25,0.25),height=c(0.45,0.45,0.1))
par(mar=c(0.75,0.75,0.75,0.75))
i=1
ivars=c("Had","Had_Hs")
for (ivar in ivars){
for (iexpid in expids){
itoplot = attdata[[icity]][[iexpid]][[iexp]][[ivar]]
#
print(iexpid)
print(sum(itoplot*areas2d,na.rm=T)/sum(carea,na.rm=T))
#
mapplot(alon,alat,itoplot,
mybreaks=ibreaks,
mycol=icols,
mxlim=xrange,mylim=yrange,
plotcoast=TRUE,
ret="plot",
scoastlines=coastlines)
plotcity(xc=clon,yc=clat,rr=rr,ccol=ccol)
legend("topleft",paste(letters[i],".",sep=""),bty="n",text.font=2,cex=2.75)
# 1-W-m2-heatshed
hval = 1 # threshold for heatshed in Wm-2
ished = (itoplot>=hval)+0
mydf = subset(data.frame(x=c(alon),y=c(alat),z=c(ished)),z==1)
myrast = rasterFromXYZ(mydf,crs=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))
hshed = aggregate(rasterToPolygons(myrast, n=4, na.rm=TRUE, digits=12, dissolve=FALSE),by="z")
lines(hshed,col="grey30",lwd=1.75)
i=i+1
}
}
# legend
par(mgp=c(1.75,1.5,0))
plotlegend(mybreaks=ibreaks,
ltext="",ltext2=expression('H [W m'^-2*']'),
mycols=icols,isel=c(1,11,20,21,22,23),
horizontal=TRUE,
daxis=-3.0)
# margin texts
for (i in 1:length(ivars)){
nvars=c("raw","source-corrected")
mtext(side=2,outer=TRUE,rev(nvars)[i],at=seq(0.335,0.79,length.out=length(ivars))[i],cex=1.5,font=1)}
for (i in 1:length(expids)){
mtext(side=3,outer=TRUE,cexpids[i],at=c(0.125,0.375,0.625,0.875)[i],cex=1.5,font=1)}
dev.off()
print(sprintf("Successfully created: %s",ofile))
}
}