-
Notifications
You must be signed in to change notification settings - Fork 5
/
priogrid_make_script.R
173 lines (130 loc) · 7.18 KB
/
priogrid_make_script.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
library(ncdf4)
input_folder <- "c:/Users/jonves/data/priogrid_data/"
output_folder <- "c:/Users/jonves/data/priogrid_res/"
input_folder <- "~/data/priogrid_data/"
output_folder <- "~/data/priogrid_res/"
#### Generate all individual data as .rds files in output_folder ###
make_pg(input_folder = input_folder, output_folder = output_folder)
# Combine into netcdf files
RASTER_FILES <- list.files(path = output_folder, full.names = TRUE)
RASTER_FILES <- RASTER_FILES[!grepl("priogrid.nc", RASTER_FILES)]
#### Yearly NetCDF ####
yearly_netcdf <- paste(output_folder, "priogrid_yearly.nc", sep = "")
# Define dimensions #
xvals <- seq(-179.75, 179.75, by = 0.5)
yvals <- seq(-89.75, 89.75, by = 0.5)
years <- 1946:2020 # We have data at least until 2020. Expand this if needed.
nx <- length(xvals)
ny <- length(yvals)
nt <- length(years)
xdim <- ncdim_def( 'lon', 'degrees_east', xvals )
ydim <- ncdim_def( 'lat', 'degrees_north', yvals )
tdim <- ncdim_def( 'year', 'integer', 0, unlim=TRUE )
# Define variables #
integer_mv <- -99 # missing value
float_mv <- 1.e30 # missing value
gwcode <- ncvar_def(name = "gwcode", units = 'gwcode', dim = list(xdim,ydim,tdim), missval = integer_mv, compression = 7)
pop_gpw_sum <- ncvar_def(name = "pop_gpw_sum", units = 'population', dim = list(xdim,ydim,tdim), missval = integer_mv, compression = 7)
pop_gpw_sd <- ncvar_def(name = "pop_gpw_sd", units = 'standard deviation', dim = list(xdim,ydim,tdim), missval = float_mv, compression = 7)
pop_gpw_min <- ncvar_def(name = "pop_gpw_min", units = 'population', dim = list(xdim,ydim,tdim), missval = integer_mv, compression = 7)
pop_gpw_max <- ncvar_def(name = "pop_gpw_max", units = 'population', dim = list(xdim,ydim,tdim), missval = integer_mv, compression = 7)
# Make new output file #
ncid_new <- nc_create(yearly_netcdf, list(gwcode,
pop_gpw_sum,
pop_gpw_sd,
pop_gpw_min,
pop_gpw_max), force_v4 = TRUE)
# Populate netcdf file #
rast <- readRDS(RASTER_FILES[grepl("gwcode", RASTER_FILES)])
ncvar_put(ncid_new, gwcode, aperm(as.array(rast), c(2, 1, 3)), start=c(1,1,1), count=c(nx,ny,nt-4))
rast <- readRDS(RASTER_FILES[grepl("pop_gpw", RASTER_FILES)])
rast <- subset(rast, which(grepl("sum", names(rast))))
for(year in c(2000, 2005, 2010, 2015, 2020)){
rast_year <- subset(rast, which(grepl(year, names(rast))))
ncvar_put(ncid_new, pop_gpw_sum, t(as.matrix(rast_year)), start=c(1,1,which(years == year)), count=c(nx,ny,1))
}
rast <- readRDS(RASTER_FILES[grepl("pop_gpw", RASTER_FILES)])
rast <- subset(rast, which(grepl("sd", names(rast))))
for(year in c(2000, 2005, 2010, 2015, 2020)){
rast_year <- subset(rast, which(grepl(year, names(rast))))
ncvar_put(ncid_new, pop_gpw_sd, t(as.matrix(rast_year)), start=c(1,1,which(years == year)), count=c(nx,ny,1))
}
rast <- readRDS(RASTER_FILES[grepl("pop_gpw", RASTER_FILES)])
rast <- subset(rast, which(grepl("min", names(rast))))
for(year in c(2000, 2005, 2010, 2015, 2020)){
rast_year <- subset(rast, which(grepl(year, names(rast))))
ncvar_put(ncid_new, pop_gpw_min, t(as.matrix(rast_year)), start=c(1,1,which(years == year)), count=c(nx,ny,1))
}
rast <- readRDS(RASTER_FILES[grepl("pop_gpw", RASTER_FILES)])
rast <- subset(rast, which(grepl("max", names(rast))))
for(year in c(2000, 2005, 2010, 2015, 2020)){
rast_year <- subset(rast, which(grepl(year, names(rast))))
ncvar_put(ncid_new, pop_gpw_max, t(as.matrix(rast_year)), start=c(1,1,which(years == year)), count=c(nx,ny,1))
}
# Close netcdf file #
nc_close( ncid_new )
# Example of use
tst <- priogrid::get_array(yearly_netcdf, "gwcode", fillvarname = "_FillValue", lon = "lon", lat = "lat",
start = c(1, 1, which(years == 2000) ), count = c(-1, -1, 1) )
tst <- priogrid::make_raster(tst, transpose = T)
#### Static NetCDF ####
static_netcdf <- paste(output_folder, "priogrid_static.nc", sep = "")
# Define dimensions #
xvals <- seq(-179.75, 179.75, by = 0.5)
yvals <- seq(-89.75, 89.75, by = 0.5)
nx <- length(xvals)
ny <- length(yvals)
xdim <- ncdim_def( 'lon', 'degrees_east', xvals )
ydim <- ncdim_def( 'lat', 'degrees_north', yvals )
diamprim_s <- ncvar_def(name = "diamprim_s", units = "n sites", dim = list(xdim, ydim), missval = integer_mv, compression = 7)
diamsec_s <- ncvar_def(name = "diamsec_s", units = "n sites", dim = list(xdim, ydim), missval = integer_mv, compression = 7)
grow_start <- ncvar_def(name = "grow_start", units = "month", dim = list(xdim, ydim), missval = integer_mc, compression = 7)
grow_end <- ncvar_def(name = "grow_end", units = "month", dim = list(xdim, ydim), missval = integer_mc, compression = 7)
# Make new output file #
ncid_new <- nc_create(static_netcdf, list(diamprim_s, diamsec_s), force_v4 = TRUE)
# Populate netcdf file #
rast <- readRDS(RASTER_FILES[grepl("diamonds", RASTER_FILES)])
rast <- subset(rast, which(grepl("diamprim_s", names(rast))))
ncvar_put(ncid_new, diamprim_s, t(as.matrix(rast)), start=c(1,1), count=c(nx,ny))
rast <- readRDS(RASTER_FILES[grepl("diamonds", RASTER_FILES)])
rast <- subset(rast, which(grepl("diamsec_s", names(rast))))
ncvar_put(ncid_new, diamsec_s, t(as.matrix(rast)), start=c(1,1), count=c(nx,ny))
rast <- readRDS(RASTER_FILES[grepl("mirca_grow_agg", RASTER_FILES)])
rast <- subset(rast, which(grepl("grow_start", names(rast))))
ncvar_put(ncid_new, grow_start, t(as.matrix(rast)), start=c(1,1), count=c(nx,ny))
rast <- readRDS(RASTER_FILES[grepl("mirca_grow_agg", RASTER_FILES)])
rast <- subset(rast, which(grepl("grow_end", names(rast))))
ncvar_put(ncid_new, grow_end, t(as.matrix(rast)), start=c(1,1), count=c(nx,ny))
# Close netcdf file #
nc_close( ncid_new )
# Example of use
tst <- priogrid::get_array(static_netcdf, "diamprim_s", fillvarname = "_FillValue", lon = "lon", lat = "lat",
start = c(1, 1), count = c(-1, -1) )
tst <- priogrid::make_raster(tst, transpose = T)
#### Seasonal NetCDF ####
seasonal_netcdf <- paste(output_folder, "priogrid_seasonal.nc", sep = "")
# Define dimensions #
xvals <- seq(-179.75, 179.75, by = 0.5)
yvals <- seq(-89.75, 89.75, by = 0.5)
months <- 1:12
nx <- length(xvals)
ny <- length(yvals)
nt <- length(months)
xdim <- ncdim_def( 'lon', 'degrees_east', xvals )
ydim <- ncdim_def( 'lat', 'degrees_north', yvals )
tdim <- ncdim_def( 'month', 'integer', months)
mirca_growseason <- ncvar_def(name = "mirca_growseason", units = "weight", dim = list(xdim, ydim, tdim), missval = float_mv, compression = 7)
# Make new output file #
ncid_new <- nc_create(seasonal_netcdf, list(mirca_growseason), force_v4 = TRUE)
# Populate netcdf file #
rast <- readRDS(RASTER_FILES[grepl("mirca_growseas", RASTER_FILES)])
for(month in months){
rast_month <- subset(rast, which(tolower(month.abb[month]) == names(rast)))
ncvar_put(ncid_new, mirca_growseason, t(as.matrix(rast_month)), start=c(1,1,which(months == month)), count=c(nx,ny,1))
}
# Close netcdf file #
nc_close( ncid_new )
# Example of use
tst <- priogrid::get_array(seasonal_netcdf, "mirca_growseason", fillvarname = "_FillValue", lon = "lon", lat = "lat",
start = c(1, 1, which(month.abb == "Jan")), count = c(-1, -1, 1) )
tst <- priogrid::make_raster(tst, transpose = T)