-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6_nitrogen.R
266 lines (229 loc) · 11.3 KB
/
6_nitrogen.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
# ----------------------------------------------
# Script to extract
# Christine Rollinson, [email protected]
# Original: 29 September, 2015
#
# --------------
# Mask Proceedure (loop over each file)
# --------------
# 1) Open raw nitrogen files
# 2) Crop to paleon domain extent
# 3) Mask to make sure we have consistent land/water coverage
# 4) Fill to 850 by using 1860 as pre-industrialized background N-deposition rates
# 5) Convert to .nc file for consistent convention
#
# NOTE: We're going to maintain the original file structure for
# what we're distributing, but I'll also write a set of
# utils that will organize into separate time series by
# grid cell ID (i.e. ED) or by year (MsTMIP)
# --------------
#
# ----------------------------------------------
# ----------------------------------------------
# Load libaries, Set up Directories, etc
# ----------------------------------------------
library(ncdf4); library(raster); library(rgdal)
paleon.mask <- "~/Dropbox/PalEON_CR/env_regional/env_paleon/domain_mask/paleon_domain.nc"
nitrogen.path <- "~/Dropbox/PalEON_CR/env_regional/env_drivers_raw/nitrogen/NACP_MSTMIP_MODEL_DRIVER/data/"
nitrogen.out <- "~/Dropbox/PalEON_CR/env_regional/env_paleon/nitrogen"
# Create the driver folder if it doesn't already exist
if(!dir.exists(nitrogen.out)) dir.create(nitrogen.out)
# dummy fill values
fillv <- 1e30
# ----------------------------------------------
# ----------------------------------------------
# Create & export mask
# ----------------------------------------------
# Load the mask
paleon <- raster(paleon.mask)
# The "native" nitrogen driver goes 1860-2050
nitrogen.years <- 1860:2050
# 1) load the raw data
nhx.nc <- nc_open(file.path(nitrogen.path, "mstmip_driver_global_hd_nitrogen_nhx_v1.nc4"))
noy.nc <- nc_open(file.path(nitrogen.path, "mstmip_driver_global_hd_nitrogen_noy_v1.nc4"))
nhx.name <- nhx.nc$var[[5]]$name
nhx.longname <- nhx.nc$var[[5]]$longname
nhx.unit <- nhx.nc$var[[5]]$units
# nhx.lat <- ncvar_get(nhx.nc, "lat")
# nhx.lon <- ncvar_get(nhx.nc, "lon")
# nhx.dat <- ncvar_get(nhx.nc, nhx.name)[,,which(nitrogen.years<=2010)]
# dim(nhx.dat) # dim=c(lon, lat, time)
noy.name <- noy.nc$var[[5]]$name
noy.longname <- noy.nc$var[[5]]$longname
noy.unit <- noy.nc$var[[5]]$units
# noy.lat <- ncvar_get(noy.nc, "lat")
# noy.lon <- ncvar_get(noy.nc, "lon")
# noy.dat <- ncvar_get(noy.nc, nhx.name)[,,which(nitrogen.years<=2010)]
# dim(noy.dat) # dim=c(lon, lat, time)
nc_close(nhx.nc); nc_close(noy.nc)
# Taking the spatial data, but ignoring the years beyond the paleon temporal domain
# Note: there will be some weird message about an invalid CRS, but it's fine
nhx.raw <- stack(file.path(nitrogen.path, "mstmip_driver_global_hd_nitrogen_nhx_v1.nc4"))[[which(nitrogen.years<=2010)]]
noy.raw <- stack(file.path(nitrogen.path, "mstmip_driver_global_hd_nitrogen_noy_v1.nc4"))[[which(nitrogen.years<=2010)]]
# nhx.raw
# noy.raw
# plot(nhx.raw[[1:6]]) # Just to double check that things got read in okay
# plot(noy.raw[[1:6]]) # Just to double check that things got read in okay
# 2) crop raw data (extents need to be same before masking)
nhx.crop <- crop(nhx.raw, extent(paleon))
noy.crop <- crop(noy.raw, extent(paleon))
# plot(nhx.crop[[1]])
# plot(paleon, add=T, alpha=0.8)
# plot(noy.crop[[1]])
# plot(paleon, add=T, alpha=0.8)
# 3) mask raw data
nhx.mask <- mask(nhx.crop, paleon)
noy.mask <- mask(noy.crop, paleon)
names(nhx.mask) <- substr(names(nhx.mask), 1, 5)
names(noy.mask) <- substr(names(noy.mask), 1, 5)
# plot(nhx.mask[[1]])
# plot(paleon, add=T, alpha=0.4)
# plot(nhx.mask[[1:6]])
# plot(noy.mask[[1]])
# plot(paleon, add=T, alpha=0.3)
# plot(noy.mask[[1:6]])
# 4) fill 850:1859 with 1860 N-deposition value
# 4.1) Create an emtpy brick that will get filled with the 1860 values
nhx.final <- noy.final <- brick(ncols=ncol(nhx.mask), nrows=nrow(nhx.mask), nl=length(850:1859),
xmn=xmin(nhx.mask), xmx=xmax(nhx.mask), ymn=ymin(nhx.mask), ymx=ymax(nhx.mask),
crs=projection(nhx.mask))
names(nhx.final) <- names(noy.final) <- paste0("X", 850:1859)
nhx.final <- setValues(nhx.final, values(nhx.mask[[1]]))
noy.final <- setValues(noy.final, values(nhx.mask[[1]]))
# 4.2) Stack the pre-N deposition file & the MsTMIP N-deposition
nhx.final <- stack(nhx.final, nhx.mask)
noy.final <- stack(noy.final, noy.mask)
nhx.final[[1:10]]
nhx.final[[(nlayers(nhx.final)-10):nlayers(nhx.final)]]
par(mfrow=c(2,1))
plot(nhx.final[[1]], main=names(nhx.final[[1]]), zlim=range(values(nhx.final), na.rm=T))
plot(nhx.final[[1161]], main=names(nhx.final[[1161]]), zlim=range(values(nhx.final), na.rm=T))
noy.final[[1:10]]
noy.final[[(nlayers(noy.final)-10):nlayers(noy.final)]]
par(mfrow=c(2,1))
plot(noy.final[[1]], main=names(noy.final[[1]]), zlim=range(values(noy.final), na.rm=T))
plot(noy.final[[1161]], main=names(noy.final[[1161]]), zlim=range(values(noy.final), na.rm=T))
# 5) save as netcdf
writeRaster(nhx.final, file.path(nitrogen.out, "paleon_nhx.nc"), format="CDF", overwrite=T, varname=nhx.name, varunit=nhx.unit, longname=nhx.longname, zname="time", zunit="years since 0850")
writeRaster(noy.final, file.path(nitrogen.out, "paleon_noy.nc"), format="CDF", overwrite=T, varname=noy.name, varunit=noy.unit, longname=noy.longname, zname="time", zunit="years since 0850")
# ----------------------------------------------
# ----------------------------------------------
# Create some animations of nitrogen deposition
# 1) Total N Deposition
# 2) NHx
# 3) NOy
# ----------------------------------------------
# Load Spatial & Graphing Libraries
library(raster); library(ggplot2); library(grid);
library(animation)
library(car)
# A couple settings for running on my local mac only; comment these out elsewhere
path.to.convert <- "/opt/local/bin/convert"
ani.options(convert=path.to.convert)
# Get state outlines
paleon.states <- map_data("state")
# get a spatial file for each PFT
nhx <- stack(file.path(nitrogen.out, "paleon_nhx.nc"))
noy <- stack(file.path(nitrogen.out, "paleon_noy.nc"))
# plot(nhx[[1:6]])
# plot(noy[[1:6]])
# Convert each layer to a dataframe
# Extract just the lat/lon
lat.lon <- data.frame(rasterToPoints(nhx))[,1:2]
names(lat.lon) <- c("lon", "lat")
# Extract the N deposition data & code in years
nhx.df <- stack(data.frame(rasterToPoints(nhx))[,3:(nlayers(nhx)+2)])
noy.df <- stack(data.frame(rasterToPoints(noy))[,3:(nlayers(noy)+2)])
years <- as.numeric(substr(nhx.df[,2],2,nchar(as.vector(nhx.df[,2]))))-1+850
# Making a single Nitrogen deposition data frame
nitrogen <- data.frame(lon=lat.lon$lon, lat=lat.lon$lat, year=years, nhx=nhx.df[,1], noy=noy.df[,1])
nitrogen$Total <- nitrogen$nhx + nitrogen$noy
summary(nitrogen)
# Looking at pre-industrial levels & 2010 levels
summary(nitrogen[nitrogen$year==1860,]) # Range nhx = 3 - 534; noy = 27 - 107
summary(nitrogen[nitrogen$year==2010,]) # Range nhx = 21 - 1168; noy = 100 - 1497
# -----------------------
# 1) Total N Deposition
# -----------------------
saveGIF(
for(i in 1800:max(nitrogen$year)){
# for(i in 2000:2005){
print(paste0("Graphing Year : ", i))
print(
ggplot(data= nitrogen[nitrogen$year==i,]) +
geom_raster(aes(x=lon, y=lat, fill=Total)) +
geom_path(data=paleon.states, aes(x=long, y=lat, group=group)) +
scale_x_continuous(limits=range(nitrogen$lon), expand=c(0,0), name="Longitude (degrees)") +
scale_y_continuous(limits=range(nitrogen$lat), expand=c(0,0), name="Latitude (degrees)") +
scale_fill_gradientn(colours=c("gray50", "blue2"), name="mgN/m2/yr", limits=range(nitrogen$Total)) +
ggtitle(paste0("Total N Deposition : ", i)) +
coord_equal(ratio=1) +
theme(legend.position="bottom") +
theme(axis.text.x =element_text(color="black", size=rel(2)),
axis.text.y =element_text(color="black", size=rel(2)),
axis.title.x=element_text(size=rel(2)),
axis.title.y=element_text(size=rel(2)),
plot.title =element_text(size=rel(2))) +
theme(panel.background=element_blank())
)
# }, movie.name=file.path(nitrogen.out,"NitrogenDeposition_Total_1860-2010.gif"), interval=0.3, nmax=10000, autobrowse=F, autoplay=F, ani.height=600, ani.width=800)
# Note: Locally, movie.name doesn't like file.path, so use this setting
}, movie.name="NitrogenDeposition_Total_1800-2010.gif", interval=0.3, nmax=10000, autobrowse=F, autoplay=F, ani.height=600, ani.width=800)# dev.off()
# -----------------------
# -----------------------
# 2) NHx Deposition
# -----------------------
saveGIF(
for(i in 1800:max(nitrogen$year)){
# for(i in 2000:2005){
print(paste0("Graphing Year : ", i))
print(
ggplot(data= nitrogen[nitrogen$year==i,]) +
geom_raster(aes(x=lon, y=lat, fill=nhx)) +
geom_path(data=paleon.states, aes(x=long, y=lat, group=group)) +
scale_x_continuous(limits=range(nitrogen$lon), expand=c(0,0), name="Longitude (degrees)") +
scale_y_continuous(limits=range(nitrogen$lat), expand=c(0,0), name="Latitude (degrees)") +
scale_fill_gradientn(colours=c("gray50", "blue2"), name="mgN/m2/yr", limits=range(nitrogen$nhx)) +
ggtitle(paste0("NHx Deposition : ", i)) +
coord_equal(ratio=1) +
theme(legend.position="bottom") +
theme(axis.text.x =element_text(color="black", size=rel(2)),
axis.text.y =element_text(color="black", size=rel(2)),
axis.title.x=element_text(size=rel(2)),
axis.title.y=element_text(size=rel(2)),
plot.title =element_text(size=rel(2))) +
theme(panel.background=element_blank())
)
# }, movie.name=file.path(nitrogen.out,"NitrogenDeposition_NHx_1860-2010.gif"), interval=0.3, nmax=10000, autobrowse=F, autoplay=F, ani.height=600, ani.width=800)
# Note: Locally, movie.name doesn't like file.path, so use this setting
}, movie.name="NitrogenDeposition_NHx_1800-2010.gif", interval=0.3, nmax=10000, autobrowse=F, autoplay=F, ani.height=600, ani.width=800)# dev.off()
# -----------------------
# -----------------------
# 3) NOy Deposition
# -----------------------
saveGIF(
for(i in 1800:max(nitrogen$year)){
# for(i in 2000:2005){
print(paste0("Graphing Year : ", i))
print(
ggplot(data= nitrogen[nitrogen$year==i,]) +
geom_raster(aes(x=lon, y=lat, fill=noy)) +
geom_path(data=paleon.states, aes(x=long, y=lat, group=group)) +
scale_x_continuous(limits=range(nitrogen$lon), expand=c(0,0), name="Longitude (degrees)") +
scale_y_continuous(limits=range(nitrogen$lat), expand=c(0,0), name="Latitude (degrees)") +
scale_fill_gradientn(colours=c("gray50", "blue2"), name="mgN/m2/yr", limits=range(nitrogen$noy)) +
ggtitle(paste0("NHx Deposition : ", i)) +
coord_equal(ratio=1) +
theme(legend.position="bottom") +
theme(axis.text.x =element_text(color="black", size=rel(2)),
axis.text.y =element_text(color="black", size=rel(2)),
axis.title.x=element_text(size=rel(2)),
axis.title.y=element_text(size=rel(2)),
plot.title =element_text(size=rel(2))) +
theme(panel.background=element_blank())
)
# }, movie.name=file.path(nitrogen.out,"NitrogenDeposition_NOy_1860-2010..gif"), interval=0.3, nmax=10000, autobrowse=F, autoplay=F, ani.height=600, ani.width=800)
# Note: Locally, movie.name doesn't like file.path, so use this setting
}, movie.name="NitrogenDeposition_NOy_1800-2010.gif", interval=0.3, nmax=10000, autobrowse=F, autoplay=F, ani.height=600, ani.width=800)# dev.off()
# -----------------------
# ----------------------------------------------