Skip to content

Instantly share code, notes, and snippets.

@igproj-fusion
Last active May 3, 2024 12:15
Show Gist options
  • Save igproj-fusion/097ed49f86455dff847d296a83aea568 to your computer and use it in GitHub Desktop.
Save igproj-fusion/097ed49f86455dff847d296a83aea568 to your computer and use it in GitHub Desktop.
pacman::p_load(
rvest,
ncdf4,
sf,
rnaturalearth,
tidyverse,
reshape2,
scales)
box = c(xmin = 125, ymin = 25, xmax = 150, ymax = 50)
sf_use_s2(FALSE)
aroundJapan <- ne_countries(scale = "large",
returnclass = "sf") |> st_crop(box)
URL_base <- "https://www.ncei.noaa.gov/data/sea-surface-temperature-optimum-interpolation/v2.1/access/avhrr/"
lastDir <- read_html(URL_base) |>
html_nodes("a") |>
html_attr("href") |>
tail(1)
lastFile <- read_html(paste0(URL_base, lastDir)) |>
html_nodes("a") |>
html_attr("href") |>
tail(1)
download.file(paste0(URL_base, lastDir, lastFile),
destfile = file.path(tempdir(), lastFile), mode = "wb")
YMD <- ymd(substr(strsplit(lastFile , "\\.")[[1]][2], 1, 8))
nc4data <- nc_open(file.path(tempdir(), lastFile))
sst <- as.vector(ncvar_get(nc4data, "sst"))
melt(matrix(sst, ncol = 1440, byrow =T)) |>
filter(value != is.na(value)) |>
mutate(lon = Var2 / 4 - 0.125,
lat = Var1 / 4 - 90.125) |>
select(lon, lat, sst = value) |>
filter(lon >= 125) |>
filter(lon <= 150) |>
filter(lat >= 25) |>
filter(lat <= 50) |>
ggplot() +
geom_point(aes(x = lon, y = lat, color = sst), size = 2) +
geom_sf(data = aroundJapan, linewidth = 0.1,
color = "gray60", fill = "#dcffca") +
scale_color_gradient2(low = muted("blue"), mid = "white",
high = "red", midpoint = 15,
breaks = c(0, 5, 10, 15, 20, 25, 30)) +
annotate("text", x = 130 , y = 48.5, label = YMD,
color = "black", size = 3.5) +
xlim(c(125, 150)) + ylim(c(25, 50)) +
labs(color = "SST(°C)") +
theme_void() +
theme(plot.margin= unit(c(1, 1, 1, 1), "lines"),
legend.title = element_text(size = rel(0.8)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment