-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotGeoClaw.r
375 lines (330 loc) · 11.9 KB
/
plotGeoClaw.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
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
#functions for loading GeoClaw Output
# lonReg = c(360-124.20219, 360-124.1791325)
# latReg = c(41.73967, 41.7627285)
# lonReg = c(360-124.203, 360-124.179)
# latReg = c(41.739, 41.763)
#format:
#a bunch of pieces in the form:
#num grid_number
#num AMR_level
#num mx
#num my
#num xlow
#num ylow
#num dx
#num dy
#line
#dat
#line
loadGeoQ = function(fname) {
dat = scan(fname, what="character")
gridStart = 0
ngrid = 0
allgrid_number = list()
allAMR_level = list()
allmx = list()
allmy = list()
allxlow = list()
allylow = list()
alldx = list()
alldy = list()
alldat = list()
alllon =list()
alllat =list()
allwh = list() #water height
allxvel = list()
allyvel = list()
allsurfDisp = list() #surface displacement (water height - height at rest)
while(gridStart < length(dat)) {
ngrid = ngrid + 1
#get header information
allgrid_number[ngrid] <- grid_number <- as.numeric(dat[gridStart+1])
allAMR_level[ngrid] <- AMR_level <- as.numeric(dat[gridStart+3])
allmx[ngrid] <- mx <- as.numeric(dat[gridStart+5])
allmy[ngrid] <- my <- as.numeric(dat[gridStart+7])
allxlow[ngrid] <- xlow <- as.numeric(dat[gridStart+9])
allylow[ngrid] <- ylow <- as.numeric(dat[gridStart+11])
alldx[ngrid] <- dx <- as.numeric(dat[gridStart+13])
alldy[ngrid] <- dy <- as.numeric(dat[gridStart+15])
flood <- as.numeric(dat[(gridStart+16 + 1):(gridStart+16 + mx*my*4)])
wh = array(flood[seq(from=1, length=mx*my, by=4)], dim=c(mx, my))
xvel = array(flood[seq(from=2, length=mx*my, by=4)], dim=c(mx, my))
yvel = array(flood[seq(from=3, length=mx*my, by=4)], dim=c(mx, my))
surfDisp = array(flood[seq(from=4, length=mx*my, by=4)], dim=c(mx, my))
allwh[[ngrid]] = wh
allxvel[[ngrid]] = xvel
allyvel[[ngrid]] = yvel
allsurfDisp[[ngrid]] = surfDisp
alllon[[ngrid]] <- lon <- seq(xlow, xlow+mx*dx, l=mx)
alllat[[ngrid]] <- lat <- seq(ylow, ylow+my*dy, l=my) # don't reverse order because data doesn't start from the top
gridStart = gridStart+16 + mx*my*4
}
out = list(grid_number=allgrid_number, AMR_level=allAMR_level, mx=allmx, my=allmy,
xlow=allxlow, ylow=allylow, dx=alldx, dy=alldy, lon=alllon, lat=alllat,
wh=allwh, xvel=allxvel, yvel=allyvel, surfDisp=allsurfDisp)
return(out)
}
#format:
#in the form:
#num time
#num meqn
#num ngrids
#num naux
#num ndim
#num nghost
#(but all we care about is the time)
loadGeoT = function(fname) {
dat = scan(fname, what="character")
return(as.numeric(dat[1]))
}
# grid_number
# AMR_level
# mx
# my
# xlow
# ylow
# dx
# dy
# lon
# lat
# wh
# xvel
# yvel
# surfDisp
plotGeoQ = function(geoQDat, main="Water Surface Displacement (meters)", zlim=NULL,
land = FALSE, maxToPlot=5000) {
#NOTE: land == FALSE ==> only plot points with water depth > 0
len = length(geoQDat$mx)
calcXRes = function(i) {
thismx = geoQDat$mx[[i]]
thislon = geoQDat$lon[[i]]
return((max(thislon)-min(thislon))/thismx)
}
calcYRes = function(i) {
thismy = geoQDat$my[[i]]
thislat = geoQDat$lat[[i]]
return((max(thislat)-min(thislat))/thismy)
}
if(!land) {
for(i in 1:length(geoQDat$surfDisp)) {
wh = geoQDat$wh[[i]]
geoQDat$surfDisp[[i]][wh <= 0] = NA
}
}
#calculate range of data values (lat, lon, water height, resolution)
rangeLon = range(sapply(geoQDat$lon, range, na.rm=TRUE))
rangeLat = range(sapply(geoQDat$lat, range, na.rm=TRUE))
if(is.null(zlim))
rangeDat = range(sapply(geoQDat$surfDisp, range, na.rm=TRUE, finite=TRUE),
na.rm=TRUE, finite=TRUE)
else
rangeDat = zlim
xres = sapply(1:len, calcXRes)
yres = sapply(1:len, calcYRes)
avgres = (xres + yres)/2
#plot low-resolution data first, high resolution data on top of low-res
sortI = sort(avgres, index.return=TRUE, decreasing=TRUE)$ix
#plot data for each grid in geoQDat
first=TRUE
maxPerGrid = floor(maxToPlot/length(sortI))
for(i in sortI) {
#get data for this grid
thisLon = geoQDat$lon[[i]]
thisLat = geoQDat$lat[[i]]
grid = make.surface.grid(list(lon=thisLon, lat=thisLat))
dat = geoQDat$surfDisp[[i]]
nx = length(geoQDat$lon[[i]])
ny = length(geoQDat$lat[[i]])
thisRangeLon = range(thisLon)
thisRangeLat = range(thisLat)
#randomly subsample maxPerGrid points from dat and grid
if(is.finite(maxPerGrid) && maxPerGrid < length(dat)) {
pts = sample(1:length(dat), maxPerGrid)
grid = grid[pts,]
dat = dat[pts]
}
#get points for plotting rectangle around this grid
polyX = c(thisRangeLon[1], thisRangeLon[1], thisRangeLon[2], thisRangeLon[2], thisRangeLon[1])
polyY = c(thisRangeLat[1], thisRangeLat[2], thisRangeLat[2], thisRangeLat[1], thisRangeLat[1])
#plot water height (surface displacement) and black box around data range:
if(first) {
quilt.plot(grid, dat, xlim=rangeLon, ylim=rangeLat, zlim=rangeDat, nx=nx, ny=ny,
main=main, xlab="Longitude", ylab="Latitude")
first = FALSE
}
else {
quilt.plot(grid, dat, add=TRUE, add.legend=FALSE, zlim=rangeDat, nx=nx, ny=nx)
}
polygon(polyX, polyY, border="black")
}
}
#x is either a directory or a list of geoQDat objects. If list of geoQ
#objects, must also include allGeoQTimes
geoQMovie = function(x, allGeoQTimes=NULL, land=FALSE, range=NULL, maxToPlot=10000) {
#x: either list of outputs from loadGeoQ or the directory with all the fort.* files
#allGeoQTimes: ignored if x is a directory. Vector of times corresponding to
#fort.q* files
#range: if range is NULL, read in all data to get range.
#Else read in files one by one
#maxToPlot: maximum number of points to plot with quilt.plot
#if x is a string, then it is a directory for loading geoQDat
if(is.character(x)) {
#change working directory
wd = getwd()
setwd(x)
#get data files in given data direcotry
qFiles = system("ls fort.q*", intern=TRUE)
tFiles = system("ls fort.t*", intern=TRUE)
#load in data files
allGeoQDat = lapply(qFiles, loadGeoQ)
allGeoQTimes = sapply(tFiles, loadGeoT)
#change working directory back
setwd(wd)
}
# if x is not a string, then geoQ and geoT values were input by user
else {
allGeoQDat = x
if(is.null(allGeoQTimes)) {
stop("no times for geoQ data input")
}
}
# if specified, ignore surfDisp values over land
if(!land) {
for(i in 1:length(allGeoQDat)) {
for(j in 1:length(allGeoQDat[[i]]$surfDisp)) {
wh = allGeoQDat[[i]]$wh[[j]]
allGeoQDat[[i]]$surfDisp[[j]][wh <= 0] = NA
}
}
}
#get range of GeoQ values
geoQRange = function(geoQDat) {
range(geoQDat$surfDisp, na.rm=TRUE, finite=TRUE)
}
if(is.null(range))
datRange = range(sapply(allGeoQDat, geoQRange), na.rm=TRUE, finite=TRUE)
else
datRange = range
for(i in 1:length(allGeoQTimes)) {
geoQDat = allGeoQDat[[i]]
plotGeoQ(geoQDat, zlim=datRange,
main=paste0("Water Surface Displacement (meters) at Time ", allGeoQTimes[i]))
}
}
######################################################################
######################################################################
######################################################################
######################################################################
######################################################################
######################################################################
######################################################################
######################################################################
######################################################################
######################################################################
# Region plotting and data-loading tools
#Like loadGeoQ but for a specific region with latitude and longitude ranges
# given by latReg and lonReg respectively
getGeoQRegion = function(geoQDat, lonReg, latReg) {
#NOTE: land == FALSE ==> only plot points with water depth > 0
len = length(geoQDat$mx)
#get data for each grid in geoQDat
first=TRUE
for(i in 1:len) {
#get data for this grid
thisLon = geoQDat$lon[[i]]
thisLat = geoQDat$lat[[i]]
grid = make.surface.grid(list(lon=thisLon, lat=thisLat))
dat = geoQDat$surfDisp[[i]]
nx = length(geoQDat$lon[[i]])
ny = length(geoQDat$lat[[i]])
thisRangeLon = range(thisLon)
thisRangeLat = range(thisLat)
#determine if this grid is contained within region grid. If not, skip
lonGood = (thisRangeLon[1] >= lonReg[1]) && (thisRangeLat[2] <= lonReg[2])
latGood = (thisRangeLat[1] >= latReg[1]) && (thisRangeLat[2] <= latReg[2])
if(!lonGood || !latGood)
next
#plot water height (surface displacement) and black box around data range:
if(first) {
quilt.plot(grid, dat, xlim=lonReg, ylim=latReg, zlim=rangeDat, nx=nx, ny=ny,
main=main, xlab="Longitude", ylab="Latitude")
first = FALSE
}
else {
quilt.plot(grid, dat, add=TRUE, add.legend=FALSE, zlim=rangeDat, nx=nx, ny=nx)
}
polygon(polyX, polyY, border="black")
}
if(first) {
stop("no data region is within specified lon and lat ranges")
}
}
#Like plotGeoQ but for a specific region with latitude and longitude ranges
# given by latReg and lonReg respectively
plotRegion = function(geoQDat, lonReg, latReg, zlim=NULL,
main="Water Surface Displacement (meters)", land = FALSE) {
#NOTE: land == FALSE ==> only plot points with water depth > 0
len = length(geoQDat$mx)
calcXRes = function(i) {
thismx = geoQDat$mx[[i]]
thislon = geoQDat$lon[[i]]
return((max(thislon)-min(thislon))/thismx)
}
calcYRes = function(i) {
thismy = geoQDat$my[[i]]
thislat = geoQDat$lat[[i]]
return((max(thislat)-min(thislat))/thismy)
}
if(!land) {
for(i in 1:length(geoQDat$surfDisp)) {
wh = geoQDat$wh[[i]]
geoQDat$surfDisp[[i]][wh <= 0] = NA
}
}
#calculate range of data values (lat, lon, water height, resolution)
rangeLon = range(sapply(geoQDat$lon, range, na.rm=TRUE))
rangeLat = range(sapply(geoQDat$lat, range, na.rm=TRUE))
if(is.null(zlim))
rangeDat = range(sapply(geoQDat$surfDisp, range, na.rm=TRUE, finite=TRUE),
na.rm=TRUE, finite=TRUE)
else
rangeDat = zlim
xres = sapply(1:len, calcXRes)
yres = sapply(1:len, calcYRes)
avgres = (xres + yres)/2
#plot low-resolution data first, high resolution data on top of low-res
sortI = sort(avgres, index.return=TRUE, decreasing=TRUE)$ix
#plot data for each grid in geoQDat
first=TRUE
for(i in sortI) {
#get data for this grid
thisLon = geoQDat$lon[[i]]
thisLat = geoQDat$lat[[i]]
grid = make.surface.grid(list(lon=thisLon, lat=thisLat))
dat = geoQDat$surfDisp[[i]]
nx = length(geoQDat$lon[[i]])
ny = length(geoQDat$lat[[i]])
thisRangeLon = range(thisLon)
thisRangeLat = range(thisLat)
#determine if this grid is contained within region grid. If not, skip
lonGood = (thisRangeLon[1] >= lonReg[1]) && (thisRangeLat[2] <= lonReg[2])
latGood = (thisRangeLat[1] >= latReg[1]) && (thisRangeLat[2] <= latReg[2])
next
#get points for plotting rectangle around this grid
polyX = c(thisRangeLon[1], thisRangeLon[1], thisRangeLon[2], thisRangeLon[2], thisRangeLon[1])
polyY = c(thisRangeLat[1], thisRangeLat[2], thisRangeLat[2], thisRangeLat[1], thisRangeLat[1])
#plot water height (surface displacement) and black box around data range:
if(first) {
quilt.plot(grid, dat, xlim=lonReg, ylim=latReg, zlim=rangeDat, nx=nx, ny=ny,
main=main, xlab="Longitude", ylab="Latitude")
first = FALSE
}
else {
quilt.plot(grid, dat, add=TRUE, add.legend=FALSE, zlim=rangeDat, nx=nx, ny=nx)
}
polygon(polyX, polyY, border="black")
}
if(first) {
stop("no data region is within specified lon and lat ranges")
}
}