-
Notifications
You must be signed in to change notification settings - Fork 0
/
temperature_cod.R
115 lines (85 loc) · 3.21 KB
/
temperature_cod.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
`%>%` <- 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)
cod <- sf::st_read(here::here("data-raw/CodShapefiles/codstox.shp"))
cod <- cod %>%
sf::st_transform(proj4string = new_crs)
years <- 1982:2021
total_data <- tibble::tibble()
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 {
# crop to regions & calculate ----
# loop over months ----
for (i in c(paste0("0", 1:9), paste0("1", 0:2))) {
this_month <- stringr::str_subset(
names(data),
paste0("X", j, ".", i)
)
month_data <- data[[this_month]]
message(paste("filtered to", i, "..."))
# loop over regions ----
for (k in c("EGOM", "GBK", "WGOM", "SNE")) {
region_data <- raster::mask(
x = month_data,
mask = cod %>%
dplyr::filter(STOCK == k)
)
# calculate total area ----
area <- raster::area(region_data, na.rm = TRUE) %>%
raster::as.data.frame(xy = TRUE) %>%
dplyr::select(-c(x, y))
total_area <- area %>%
colSums(na.rm = TRUE)
# weighted mean temp
weighted_temp <- (region_data@data@values * area) %>%
colSums(na.rm = TRUE)
final_temp <- mean(weighted_temp, na.rm = TRUE) / total_area
temp_sd <- sd(weighted_temp, na.rm = TRUE) / total_area
# area under 11C
region_data@data@values[which(region_data@data@values > 11)] <- NA
month_area <- raster::area(region_data, na.rm = TRUE) %>%
raster::as.data.frame(xy = TRUE) %>%
dplyr::select(-c(x, y)) %>%
colSums(na.rm = TRUE)
final_area <- mean(month_area, na.rm = TRUE) / total_area
sd_area <- sd(month_area, na.rm = TRUE) / total_area
out_data <- tibble::tibble(
Year = j,
Month = i,
Region = k,
DATA_VALUE = c(final_temp[1], final_area[1]),
DATA_SD = c(temp_sd[1], sd_area[1]),
INDICATOR_NAME = c("temperature", "area")
)
if(nrow(total_data) == 0) {
total_data <- out_data
} else {
total_data <- dplyr::full_join(total_data, out_data)
}
message("calculated regional mean monthly temp...")
}
}
}
message(paste("done with", j))
}
total_data
write.csv(total_data, here::here("data-raw/cod_temperature.csv"))