-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlotMaps.R
More file actions
392 lines (339 loc) · 15.1 KB
/
PlotMaps.R
File metadata and controls
392 lines (339 loc) · 15.1 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
SetupMap <- function(file) {
ncid <- ncdf4::nc_open(file)
lats<-ncdf4::ncvar_get(ncid, "XLAT_M")
lons<-ncdf4::ncvar_get(ncid, "XLONG_M")
ncdf4::nc_close(ncid)
myLocation <- c(min(lons)-0.1, min(lats)-0.1, max(lons)+0.1, max(lats)+0.1)
myMap <- ggmap::get_map(location=myLocation, source="stamen", maptype="terrain", crop=TRUE)
myMap
}
PlotMapErrors <- function(myMap, statsObj,
statsTag, statsVar, statsSeas,
plotTitle="Model Errors", plotSubTitle="",
sizeVar="t_mae", colorVar="t_msd",
sizeLab="Mean Absolute Error", colorLab="Mean Signed Deviation",
colorLow="blue", colorMid="white", colorHigh="red",
minThreshSize=NULL, maxThreshSize=NULL,
minThreshCol=NULL, maxThreshCol=NULL,
minPtsize=1, maxPtsize=8,
exclVar="t_n", exclThresh=0.8,
colBreaks,
valBreaks) {
if (is.null(statsVar)) {
myData <- subset(statsObj, statsObj$tag==statsTag & statsObj$seas==statsSeas)
} else {
myData <- subset(statsObj, statsObj$tag==statsTag & statsObj$var==statsVar & statsObj$seas==statsSeas)
}
maxCnt <- max(myData[,exclVar], na.rm=TRUE)
myData <- subset(myData, myData[,exclVar] >= exclThresh*maxCnt)
#myData <- subset(myData, myData[,sizeVar]>quantile(myData[,sizeVar], 0.05, na.rm=TRUE) &
# myData[,sizeVar]<quantile(myData[,sizeVar], 0.95, na.rm=TRUE))
if (is.null(minThreshSize)) minThreshSize <- min(myData[,sizeVar], na.rm=TRUE)
if (is.null(maxThreshSize)) maxThreshSize <- max(myData[,sizeVar], na.rm=TRUE)
if (is.null(minThreshCol)) minThreshCol <- min(myData[,colorVar], na.rm=TRUE)
if (is.null(maxThreshCol)) maxThreshCol <- max(myData[,colorVar], na.rm=TRUE)
xCol <- ifelse("lon" %in% names(statsObj), "lon", "st_lon")
yCol <- ifelse("lat" %in% names(statsObj), "lat", "st_lat")
myData$plotcol <- cut(myData[,colorVar], breaks = valBreaks, right = FALSE)
valBreaksScaled <- scales::rescale(valBreaks, from=range(myData[,colorVar], na.rm = TRUE, finite = TRUE))
gg <- ggmap::ggmap(myMap) +
ggplot2::geom_point(aes_string(x=xCol, y=yCol, size=sizeVar, fill="plotcol"), data=myData, alpha=0.8, shape=21) +
ggplot2::scale_size(sizeLab, range=c(minPtsize, maxPtsize), limits=c(minThreshSize, maxThreshSize)) +
ggplot2::scale_fill_manual(colorLab, values=colBreaks) +
#ggplot2::scale_fill_gradient2(colorLab, low=colorLow, mid=colorMid, high=colorHigh, midpoint=0, limits=c(minThreshCol, maxThreshCol)) +
#ggplot2::scale_fill_gradientn(colorLab, colours = colBreaks, values = scales::rescale(valBreaks)) +
ggplot2::ggtitle(bquote(atop(.(plotTitle), atop(italic(.(plotSubTitle)), "")))) +
ggplot2::theme(plot.title = element_text(size=18, face="bold", vjust=-1)) +
ggplot2::guides(fill = guide_legend(override.aes = list(size=3)))
gg
}
PlotPeakSweMap <- function(myMap,dStart,dEnd,geoNX,geoNY) {
# Generate map of peak SWE julian day from beginning
# of water year. Also output to NetCDF for further analysis
# Establish datetime information based on passed data.
dUnits <- "days"
diff1 <- difftime(dEnd,dStart,units=dUnits)
nSteps <- diff1 <- as.numeric(diff1)
dt <- 24*3600
numMod <- length(modPathList)
# Initialize peak swe date to -9999.
peakDaySnodas <- array(-9999,c(geoNX,geoNY))
peakDayMod <- array(-9999,c(geoNX,geoNY,numMod))
# Initialize count array to filter days out later
dayCountSnodas <- array(0,c(geoNX,geoNY))
dayCountMod <- array(0,c(geoNX,geoNY,numMod))
# Loop through and read in data
for (i in 1:nSteps) {
dCurrent <- dStart + dt*i
message(paste0('Processing: ',dCurrent))
# Processing SNODAS first
snodasPath <- paste0(snodasPathList[1],"/SNODAS_REGRIDDED_",
strftime(dCurrent,"%Y%m%d"),".nc")
message(snodasPath)
id <- nc_open(snodasPath)
sweSnodas <- ncvar_get(id,'SNEQV')
nc_close(id)
if (i == 1) {
snodasTest <- sweSnodas
} else {
maxInd <- which(sweSnodas > snodasTest)
peakDaySnodas[maxInd] <- i
snodasTest[maxInd] <- sweSnodas[maxInd]
dayCountSnodas[maxInd] <- dayCountSnodas[maxInd] + 1
}
# Processing WRF-Hydro output
for (j in 1:numMod) {
modPath <- paste0(modPathList[[j]],"/",strftime(dCurrent,"%Y%m%d"),
"00.LDASOUT_DOMAIN1")
id <- nc_open(modPath)
sweModel <- ncvar_get(id,'SNEQV')
nc_close(id)
if (i == 1) {
if (j == 1) {
modTest <- array(-9999.0,c(geoNX,geoNY,numMod))
}
modTest[,,j] <- sweModel
} else {
testTemp <- modTest[,,j]
peakDayTemp <- peakDayMod[,,j]
dayCountTemp <- dayCountMod[,,j]
maxInd <- which(sweModel > testTemp)
testTemp[maxInd] <- sweModel[maxInd]
peakDayTemp[maxInd] <- i
dayCountTemp[maxInd] <- dayCountTemp[maxInd] + 1
peakDayMod[,,j] <- peakDayTemp
modTest[,,j] <- testTemp
dayCountMod[,,j] <- dayCountTemp
}
}
}
# Cleanup data
indTmp <- which(dayCountSnodas <= 21)
peakDaySnodas[indTmp] <- -9999
indTmp <- which(dayCountMod <= 21)
peakDayMod[indTmp] <- -9999
# Caculate difference grid between SNODAS and Model on peak timing
gridDiff <- array(-9999,c(geoNX,geoNY,numMod))
for (j in 1:numMod) {
peakDayModTmp <- peakDayMod[,,j]
diffTmp <- array(-9999,c(geoNX,geoNY))
indTmp <- which(peakDaySnodas != -9999 & peakDayModTmp != -9999)
diffTmp[indTmp] <- peakDayModTmp[indTmp] - peakDaySnodas[indTmp]
gridDiff[,,j] <- diffTmp
}
# Output to NetCDF
varList <- list()
varList[[1]] <- list(name='SNODAS_PEAK_DAY_SWE',
longname='WY DOY Peak SWE is achieved',
units='Julian', precision='integer',
missing=-9999,
dimensionList=list(x=list(name='longitude',values=1:geoNX,
units='Degrees East',
unlimited=FALSE,
create_dimvar=FALSE),
y=list(name='latitude',values=1:geoNY,
units='Degrees North',
unlimited=FALSE,create_dimvar=FALSE)),
data=peakDaySnodas)
for (j in 1:numMod) {
varList[[(j+1)]] <- list(name=paste0('MODEL',toString(j),'_PEAK_DAY_SWE'),
longname='WY DOY Peak SWE is achieved',
model_tags=modTagList[[j]],
units='Julian', precision='integer',
missing=-9999,
dimensionList=list(x=list(name='longitude',values=1:geoNX,
units='Degrees East',
unlimited=FALSE,
create_dimvar=FALSE),
y=list(name='latitude',values=1:geoNY,
units='Degrees North',
unlimited=FALSE,create_dimvar=FALSE)),
data=peakDayMod[,,j])
}
for (j in 1:numMod) {
varList[[(j+numMod+1)]] <- list(name=paste0('MODEL',toString(j),'_PEAK_DAY_DIFFERENCE'),
longname='Model Minus SNODAS Peak DOY',
model_tags=modTagList[[j]],
units='Julian', precision='integer',
missing=-9999,
dimensionList=list(x=list(name='longitude',values=1:geoNX,
units='Degrees East',
unlimited=FALSE,
create_dimvar=FALSE),
y=list(name='latitude',values=1:geoNY,
units='Degrees North',
unlimited=FALSE,create_dimvar=FALSE)),
data=gridDiff[,,j])
}
globalAttList <- list()
globalAttList[[1]] <- list(name='Institution',value='NCAR-RAL',precision="text")
outPath <- paste0(writeDir,"/SNODAS_WRF_HYDRO_SWE_PEAK_DOY_WY",strftime(dEnd,"%Y"),".nc")
MkNcdf(varList,filename=outPath,globalAttList=globalAttList)
# Create GeoTiff for GIS plotting
outPathTif <- paste0(writeDir,"/SNODAS_PEAK_DOY_WY",strftime(dEnd,"%Y"),".tif")
ExportGeogrid(outPath,'SNODAS_PEAK_DAY_SWE',outPathTif,inCoordFile=geoFile)
for (j in 1:numMod) {
outPathTif <- paste0(writeDir,"/",modTagList[[j]],
"_WRF_HYDRO_SWE_PEAK_DOY_WY",strftime(dEnd,"%Y"),".tif")
var <- paste0('MODEL',toString(j),'_PEAK_DAY_SWE')
ExportGeogrid(outPath,var,outPathTif,inCoordFile=geoFile)
outPathTif <- paste0(writeDir,"/",modTagList[[j]],
"_WRF_HYDRO_SWE_PEAK_DOY_DIFF_WY",strftime(dEnd,"%Y"),".tif")
var <- paste0('MODEL',toString(j),'_PEAK_DAY_DIFFERENCE')
ExportGeogrid(outPath,var,outPathTif,inCoordFile=geoFile)
}
}
PlotSnodasGridMap <- function(dStart,dEnd,geoNX,geoNY) {
# Generate spatial gridded map of model vs SNODAS error metrics:
# 1.) bias
# 2.) temporal correlation
# 3.) snowmelt production efficiency
# Establish datetime information based on passed data.
dUnits <- "days"
diff1 <- difftime(dEnd,dStart,units=dUnits)
nSteps <- diff1 <- as.numeric(diff1)
dt <- 24*3600
resSquared <- (resMod*1000.0)*(resMod*1000.0)
validMin <- 0.0
validMax <- 10000.0
validMinBias <- 10.0
validMax <- 1000.0
numMod <- length(modPathList)
# Initialize output arrays to -9999.
biasOut <- array(-9999.0,c(geoNX,geoNY,numMod))
corrOut <- array(-9999.0,c(geoNX,geoNY,numMod))
snodasMeanSwe <- array(-9999.0,c(geoNX,geoNY))
modMeanSwe <- array(-9999.0,c(geoNX,geoNY,numMod))
# Initialize input arrays
sweSnodasIn <- array(-9999,c(geoNX,geoNY,nSteps))
sweModIn <- array(-9999,c(geoNX,geoNY,nSteps,numMod))
# Loop through and read in data
for (i in 1:nSteps) {
dCurrent <- dStart + dt*i
message(paste0('Processing: ',dCurrent))
# Processing SNODAS first
snodasPath <- paste0(snodasPathList[1],"/SNODAS_REGRIDDED_",
strftime(dCurrent,"%Y%m%d"),".nc")
print(snodasPath)
idSnodas <- nc_open(snodasPath)
sweSnodas <- ncvar_get(idSnodas,'SNEQV')
sweSnodasIn[,,i] <- sweSnodas
rm(sweSnodas)
nc_close(idSnodas)
# Processing WRF-Hydro output
for (j in 1:numMod) {
modPath <- paste0(modPathList[[j]],"/",strftime(dCurrent,"%Y%m%d"),
"00.LDASOUT_DOMAIN1")
id <- nc_open(modPath)
sweModel <- ncvar_get(id,'SNEQV')
sweModIn[,,i,j] <- sweModel
rm(sweModel)
nc_close(id)
}
}
# Loop through grid and calculate error metrics
for (i in 1:geoNX){
message(paste0('Processing Column: ',toString(i)))
for (j in 1:geoNY){
snodasTmp <- sweSnodasIn[i,j,]
# Loop through model groups
for (k in 1:numMod){
acc_bias <- 0.0
countBias <- 0
sweModSum <- 0.0
modelTmp <- sweModIn[i,j,,k]
for (m in 1:nSteps){
v1 <- snodasTmp[m]
v2 <- modelTmp[m]
if (! is.na(v1) & ! is.na(v2)){
if (v1 > validMin & v2 > validMin & v1 < validMax & v2 < validMax){
sweVolSnodasTmp <- (v1/1000.0)*resSquared # Cubic meters
sweVolModTmp <- (v2/1000.0)*resSquared
# Calculate bias
if (v1 > validMinBias & v2 > validMinBias) {
biasTmp <- v2 - v1
acc_bias <- acc_bias + biasTmp
countBias <- countBias + 1
}
sweModSum <- sweModSum + modelTmp[m]
}
}
}
if (countBias > 0){
biasOut[i,j,k] <- acc_bias/countBias
}
# Calculate temporal correlation
ind1 <- which(snodasTmp > 0.0 & modelTmp > 0.0)
corrOut[i,j,k] <- cor(snodasTmp[ind1],modelTmp[ind1])
# Mean SWE for time period
modMeanSwe[i,j,k] <- sweModSum/nSteps
}
indSnodas <- which(snodasTmp >= 0.0 & snodasTmp < 10000.0)
snodasMeanSwe[i,j] <- (sum(snodasTmp[indSnodas]))/length(indSnodas)
}
}
# Output to NetCDF
varList <- list()
varList[[1]] <- list(name='SNODAS_mean_SWE',
longname='SNODAS Mean SWE',
units='mm', precision='float',
missing=-9999,
dimensionList=list(x=list(name='longitude',values=1:geoNX,
units='Degrees East',
unlimited=FALSE,
create_dimvar=FALSE),
y=list(name='latitude',values=1:geoNY,
units='Degrees North',
unlimited=FALSE,create_dimvar=FALSE)),
data=snodasMeanSwe)
for (n in 1:numMod) {
varList[[n+1]] <- list(name=paste0('MODEL',toString(n),'_mean_SWE'),
longname='MODEL Mean SWE',
model_tags=modTagList[[n]],
units='mm', precision='float',
missing=-9999,
dimensionList=list(x=list(name='longitude',values=1:geoNX,
units='Degrees East',
unlimited=FALSE,
create_dimvar=FALSE),
y=list(name='latitude',values=1:geoNY,
units='Degrees North',
unlimited=FALSE,create_dimvar=FALSE)),
data=modMeanSwe[,,n])
}
for (n in 1:numMod) {
varList[[(n+numMod+1)]] <- list(name=paste0('MODEL',toString(n),'_swe_bias'),
longname='MODEL SWE bias against SNODAS',
model_tags=modTagList[[n]],
units='mm', precision='float',
missing=-9999,
dimensionList=list(x=list(name='longitude',values=1:geoNX,
units='Degrees East',
unlimited=FALSE,
create_dimvar=FALSE),
y=list(name='latitude',values=1:geoNY,
units='Degrees North',
unlimited=FALSE,create_dimvar=FALSE)),
data=biasOut[,,n])
}
globalAttList <- list()
globalAttList[[1]] <- list(name='Institution',value='NCAR-RAL',precision="text")
outPath <- paste0(writeDir,"/SNODAS_WRF_HYDRO_GRIDDED_SWE_ANALYSIS_",strftime(dStart,"%Y%m%d"),
"_",strftime(dEnd,"%Y%m%d"),".nc")
MkNcdf(varList,filename=outPath,globalAttList=globalAttList)
# Export to Tif format for GIS plotting
outPathTif <- paste0(writeDir,"/SNODAS_MEAN_SWE_",strftime(dStart,"%Y%m%d"),
"_",strftime(dEnd,"%Y%m%d"),".tif")
var <- "SNODAS_mean_SWE"
ExportGeogrid(outPath,var,outPathTif,inCoordFile=geoFile)
for (n in 1:numMod){
outPathTif <- paste0(writeDir,"/",modTagList[[n]],"_MEAN_SWE_",strftime(dStart,"%Y%m%d"),
"_",strftime(dEnd,"%Y%m%d"),".tif")
var <- paste0("MODEL",toString(n),"_mean_SWE")
ExportGeogrid(outPath,var,outPathTif,inCoordFile=geoFile)
outPathTif <- paste0(writeDir,"/",modTagList[[n]],"_SWE_BIAS_",strftime(dStart,"%Y%m%d"),
"_",strftime(dEnd,"%Y%m%d"),".tif")
var <- paste0("MODEL",toString(n),"_swe_bias")
ExportGeogrid(outPath,var,outPathTif,inCoordFile=geoFile)
}
}