-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplore_CMIP.R
More file actions
188 lines (125 loc) · 4.63 KB
/
Copy pathexplore_CMIP.R
File metadata and controls
188 lines (125 loc) · 4.63 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
## explore CMIP preindustrial downloads
library(tidyverse)
library(ncdf4)
library(ncdf4.helpers)
library(maps)
library(mapdata)
library(fields)
library(chron)
library(zoo)
library(ggpubr)
library(s2dverification)
nc <- nc_open("./CMIP/tos_Omon_CCSM4_past1000_r1i1p1_085001-134912.nc")
nc
d <- nc.get.time.series(nc, v = "tos",
time.dim.name = "time")
d <- as.Date(format(d, "%Y-%m-%d"))
lon <- ncvar_get(nc, "lon")
lat <- ncvar_get(nc, "lat")
SST <- ncvar_get(nc, "tos")
dim(SST) # 329 lons, 384 lats, 6000 months
names(dim(SST)) <- c("lon", "lat", "time")
names(dim(lon)) <- names(dim(lat)) <- c("lon", "lat")
CDORemap(SST,
lons=lon,
lats=lat,
grid='r180x90',
method='bilinear',
crop=c(180, 240, 40, 70))
# lon are constant across rows, so we just need one column!
lon.dim <- x[,1]
# lat are constant across columns, so we just need one row!
lat.dim <- y[1,]
# for the GOA, we need lat 49 - 61 and long 209 - 231
i.min <- which.min(abs(lon.dim-150))
i.max <- which.min(abs(lon.dim-250))
i.count <- 1 + i.max-i.min
j.min <- which.min(abs(lat.dim-20))
j.max <- which.min(abs(lat.dim-68))
j.count <- 1 + j.max-j.min
# and get date vector
# Change data from a 3-D array to a matrix of monthly data by grid point:
# First, reverse order of dimensions ("transpose" array)
SST <- aperm(SST, 3:1)
# Change to matrix with column for each grid point, rows for monthly means
SST <- matrix(SST, nrow=dim(SST)[1], ncol=prod(dim(SST)[2:3]))
# Keep track of corresponding latitudes and longitudes of each column:
x <- lon.dim[i.min:i.max]
y <- lat.dim[j.min:j.max]
lat <- rep(y, length(x))
lon <- rep(x, each = length(y))
dimnames(SST) <- list(as.character(d), paste("N", lat, "E", lon, sep=""))
# plot to check
SST.mean <- colMeans(SST)
z <- t(matrix(SST.mean,length(y))) # Re-shape to a matrix with latitudes in columns, longitudes in rows
image(x,y,z, col=tim.colors(64))
contour(x, y, z, add=T)
map('world2Hires',fill=F,add=T, lwd=2)
lat <- lat+10
y <- y+10
# plot to check
SST.mean <- colMeans(SST)
z <- t(matrix(SST.mean,length(y))) # Re-shape to a matrix with latitudes in columns, longitudes in rows
image(x,y,z, col=tim.colors(64))
contour(x, y, z, add=T)
map('world2Hires',fill=F,add=T, lwd=2)
dim(sst) # 320 x 384 x 6000
dim(x) # 320 x 384
dim(y) # 320 x 384
x[,1] # 320 (different longitudes)
y[,1] # 320 (same) latitudes
x[1,] # 384 (slightly decreasing) longitudes
y[1,] # 384 latitudes, pole to pole
# try w/ some help from the web
d <- nc.get.time.series(nc, v = "tos",
time.dim.name = "time")
d <- as.Date(format(d, "%Y-%m-%d"))
x.keep <- as.vector(x > 130 & x < 250)
y.keep <- as.vector(y > 20 & y < 64)
#
# lon_index <- x[x > 130 & x < 250]
# lat_index <- y[y > 20 & y < 64]
nc.get.dim.for.axis(nc, "tos", "X")
sst <- nc.get.var.subset.by.axes(nc, "tos",
axis.indices = list(X = x.keep,
Y = y.keep))
# frustrating!
# I know this should be easy!
sst <- ncvar_get(nc, "tos")
nc.get.dim.axes(nc)
nc.get.coordinate.axes(nc, "tos")
nc.is.regular.dimension(nc)
nc.get.variable.list(nc)
nc.get.dim.axes.from.names(nc)
nc.get.coordinate.axes(nc, "tos")
nc.get.proj4.string(nc, "tos")
sst <- nc.get.var.subset.by.axes(nc, "tos",
axis.indices = list(lon = lon_index,
lat = lat_index))
data_frame(time = tas_time,
tas = as.vector(tas)) %>%
mutate(time = as.Date(format(time, "%Y-%m-%d"))) %>%
ggplot(aes(x = time, y = tas)) +
geom_line() +
xlab("Date") + ylab("Temperature (K)") +
ggtitle("Daily modeled near-surface air temprature, 2071-2075",
subtitle = "At model grid point nearest Beijing, China") +
theme_classic()
################################################################################
library(tidync)
nc <- system.file("CMIP/tos_Omon_CCSM4_past1000_r1i1p1_085001-134912.nc", package = "stars")
nc <- tidync("CMIP/tos_Omon_CCSM4_past1000_r1i1p1_085001-134912.nc")
nc
ncmeta::nc_grids("CMIP/tos_Omon_CCSM4_past1000_r1i1p1_085001-134912.nc")
ncmeta::nc_vars("CMIP/tos_Omon_CCSM4_past1000_r1i1p1_085001-134912.nc")
ncmeta::nc_dim("CMIP/tos_Omon_CCSM4_past1000_r1i1p1_085001-134912.nc", 0)
ncmeta::nc_atts("CMIP/tos_Omon_CCSM4_past1000_r1i1p1_085001-134912.nc")
ncmeta::nc_axes("CMIP/tos_Omon_CCSM4_past1000_r1i1p1_085001-134912.nc")
ncmeta::nc_dims("CMIP/tos_Omon_CCSM4_past1000_r1i1p1_085001-134912.nc")
(nc_data <- nc %>% hyper_array())
names(nc_data)
dim(nc_data[[1]])
image(nc_data[[1]])
str(nc_data[[1]])
str(nc_data[[2]])
lapply(nc_data, dim)