-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemperature_sept.R
75 lines (58 loc) · 2.33 KB
/
temperature_sept.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
`%>%` <- 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)
north_atlantic <- 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 = -90, xmax = -50,
ymax = 50, ymin = 30))
years <- 1982:2021
dir.create(here::here("data-raw/september"))
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 north atlantic ----
# filter to just september
months <- c(paste0("X", j, ".09.0", 1:9),
paste0("X", j, ".09.", 10:30))
na_temp <- raster::mask(x = data[[months]],
mask = north_atlantic)
message("cropped to north atlantic...")
# calculate mean temp for each location
mean_temp <- raster::calc(na_temp, mean)
names(mean_temp) <- j
raster::writeRaster(mean_temp,
here::here("data-raw/september/", paste0(j, ".grd")),
overwrite = TRUE)
}
message(paste("done with", j))
}
temp_out <- raster::stack(list.files(here::here("data-raw/september"),
pattern = ".grd"))
raster::writeRaster(temp_out,
"data-raw/september_mean_temperature.grd",
overwrite = TRUE)