-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemperature_july.R
220 lines (166 loc) · 6.38 KB
/
temperature_july.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
`%>%` <- magrittr::`%>%`
# shapefile setup
crs <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
# equal area crs
new_crs <- "+proj=utm +zone=12 +datum=NAD83 +no_defs +ellps=GRS80"
# different geom fix
sf::sf_use_s2(FALSE)
mab <- NEesp::shape %>%
dplyr::select(STRATA, geometry) %>%
sf::st_transform(proj4string = new_crs) %>%
# try geom fix?
# dplyr::mutate(geometry = geometry %>%
# s2::s2_rebuild() %>%
# sf::st_as_sfc()) %>%
dplyr::summarise(geometry = sf::st_union(geometry)) %>%
sf::st_crop(y = c(xmin = -80, xmax = -69,
ymax = 41.5, ymin = 35.8327))
strata <- readxl::read_excel(here::here("data-raw/BLF_STRATA.xlsx"),
skip = 3)
colnames(strata) <- c("alb", "big")
strata <- c(strata$alb, strata$big) %>%
unique()
print(strata)
bf_strata <- NEesp::shape %>%
dplyr::select(STRATA, geometry) %>%
dplyr::filter(STRATA %in% strata) %>%
sf::st_transform(proj4string = new_crs) %>%
dplyr::summarise(geometry = sf::st_union(geometry))
years <- 1982:2021
prop_july <- c()
prop_july_bf <- c()
prop_july_cold <- c()
prop_july_bf_cold <- c()
prop_july_warm <- c()
prop_july_bf_warm <- c()
for(j in years) {
message(paste("starting", j))
# download data ----
dir.create(here::here("data-raw","gridded", "sst_data"), recursive = TRUE)
url <- paste0("https://downloads.psl.noaa.gov/Datasets/noaa.oisst.v2.highres/sst.day.mean.", j, ".v2.nc")
download.file(url, destfile = "test.nc")
# R can't open the file (will have to do this in a gh action...)
# download file manually for testing on desktop
# name <- paste0(j, ".nc")
name <- "test.nc"
data <- ecopull::nc_to_raster(nc = name, varname = 'sst') # converts to NAD83
data <- raster::rotate(data)
message("converted to raster...")
# make sure all days are there ----
if(raster::nlayers(data) < 365) {
message(j, " does not have a full year of data! skipping!")
} else {
# filter to just july
july <- c(paste0("X", j, ".07.0", 1:9),
paste0("X", j, ".07.", 10:31))
# crop to MAB ----
# ndays <- raster::nlayers(data) # account for leap years
mab_temp <- raster::mask(x = data[[july]],
mask = mab
)
message("cropped to July MAB...")
# calculate total area ----
total_area <- raster::area(mab_temp, na.rm = TRUE) %>%
raster::as.data.frame(xy = TRUE) %>%
dplyr::select(-c(x, y)) %>%
colSums(na.rm = TRUE)
# calculate area 18-25.6C ----
mab_temp@data@values[which(mab_temp@data@values < 18)] <- NA
# also remove areas that are too warm
mab_temp@data@values[which(mab_temp@data@values > 25.6)] <- NA
july_area <- raster::area(mab_temp, na.rm = TRUE) %>%
raster::as.data.frame(xy = TRUE) %>%
dplyr::select(-c(x, y)) %>%
colSums(na.rm = TRUE)
# * calculate proportion ----
props <- july_area/total_area
this_prop <- mean(props)
prop_july <- c(prop_july, this_prop)
message("calculated MAB proportion...")
# calculate area under 18C ----
mab_temp <- raster::mask(x = data[[july]],
mask = mab
)
mab_temp@data@values[which(mab_temp@data@values >= 18)] <- NA
july_area <- raster::area(mab_temp, na.rm = TRUE) %>%
raster::as.data.frame(xy = TRUE) %>%
dplyr::select(-c(x, y)) %>%
colSums(na.rm = TRUE)
# * calculate proportion ----
props <- july_area/total_area
this_prop <- mean(props)
prop_july_cold <- c(prop_july_cold, this_prop)
message("calculated MAB proportion...")
# calculate area over 25.6C ----
mab_temp <- raster::mask(x = data[[july]],
mask = mab
)
mab_temp@data@values[which(mab_temp@data@values <= 25.6)] <- NA
july_area <- raster::area(mab_temp, na.rm = TRUE) %>%
raster::as.data.frame(xy = TRUE) %>%
dplyr::select(-c(x, y)) %>%
colSums(na.rm = TRUE)
# * calculate proportion ----
props <- july_area/total_area
this_prop <- mean(props)
prop_july_warm <- c(prop_july_warm, this_prop)
message("calculated MAB proportion...")
# crop to bluefish strata ----
mab_temp <- raster::mask(x = data[[july]],
mask = bf_strata
)
message("cropped to July MAB...")
# calculate total area ----
total_area <- raster::area(mab_temp, na.rm = TRUE) %>%
raster::as.data.frame(xy = TRUE) %>%
dplyr::select(-c(x, y)) %>%
colSums(na.rm = TRUE)
# calculate area 18-25.6C ----
mab_temp@data@values[which(mab_temp@data@values < 18)] <- NA
# also remove areas that are too warm
mab_temp@data@values[which(mab_temp@data@values > 25.6)] <- NA
july_area <- raster::area(mab_temp, na.rm = TRUE) %>%
raster::as.data.frame(xy = TRUE) %>%
dplyr::select(-c(x, y)) %>%
colSums(na.rm = TRUE)
# * calculate proportion ----
props <- july_area/total_area
this_prop <- mean(props)
prop_july_bf <- c(prop_july_bf, this_prop)
message("calculated bluefish strata proportion...")
# calculate area under 18C ----
mab_temp <- raster::mask(x = data[[july]],
mask = bf_strata
)
mab_temp@data@values[which(mab_temp@data@values >= 18)] <- NA
july_area <- raster::area(mab_temp, na.rm = TRUE) %>%
raster::as.data.frame(xy = TRUE) %>%
dplyr::select(-c(x, y)) %>%
colSums(na.rm = TRUE)
# * calculate proportion ----
props <- july_area/total_area
this_prop <- mean(props)
prop_july_bf_cold <- c(prop_july_bf_cold, this_prop)
message("calculated bluefish strata proportion...")
# calculate area over 25.6C ----
mab_temp <- raster::mask(x = data[[july]],
mask = bf_strata
)
mab_temp@data@values[which(mab_temp@data@values <= 25.6)] <- NA
july_area <- raster::area(mab_temp, na.rm = TRUE) %>%
raster::as.data.frame(xy = TRUE) %>%
dplyr::select(-c(x, y)) %>%
colSums(na.rm = TRUE)
# * calculate proportion ----
props <- july_area/total_area
this_prop <- mean(props)
prop_july_bf_warm <- c(prop_july_bf_warm, this_prop)
message("calculated bluefish strata proportion...")
}
message(paste("done with", j))
}
# prop_july
out_data <- tibble::tibble(years, prop_july, prop_july_bf,
prop_july_cold, prop_july_bf_cold,
prop_july_warm, prop_july_bf_warm)
write.csv(out_data, here::here("data-raw/temperature_july_bfstrata.csv"))