library(tidyverse)
library(stationaRy)
library(sf)
library(tmap)
library(spData)
library(AOI)
library(climateR)
library(raster)
library(rasterVis)
library(patchwork)
# Get the data
#loc2use <- 'Death Valley National Park'
loc2use <- 'Union College'
AOI = AOI::geocode(loc2use,
pt = TRUE)
ts = getGridMET(AOI, param = c("tmax","tmin", "prcp"),
startDate = "2021-01-01",
endDate = "2021-12-30")
# convert units
ts <- ts %>%
mutate(tmax = ((tmax - 273.15)*(9/5) +32),
tmin = ((tmin - 273.15)*(9/5) +32),
prcp = prcp/25.4
)
# Summary table
ts %>%
summarize(pcrp_max = max(prcp),
temp_max = max(tmax),
temp_min = min(tmin),
temp_mean = mean((tmin+tmax)/2),
temp_max_change = max(tmax - tmin)
)
## pcrp_max temp_max temp_min temp_mean temp_max_change
## 1 2.996063 92.57 -4.81 49.84299 40.86
# Make the figures
fig_01 <- ts %>%
ggplot() +
geom_line(aes(x = date, y = tmax), color = "red") +
geom_line(aes(x = date, y = tmin), color = "blue") +
labs(x = "",
y = "Temperature (F)"
) +
geom_hline(yintercept = 32, linetype = "dashed") +
theme_bw()
fig_02 <- ts %>%
ggplot() +
geom_col(aes(x = date, y = prcp), color = "black") +
labs(y = "Precipitation (inches)") +
theme_bw()
# Make the figures
(fig_01 / fig_02) +
plot_annotation(title = paste(loc2use,": Temperature and precipitation", sep = ""),
caption = "Data source: GridMET",
tag_levels = "a"
)
henri = getGridMET(aoi_get(state = c("NY", "NJ","CT","MA")),
param = "prcp",
startDate = "2021-08-16", endDate = "2021-08-24")
#> Spherical geometry (s2) switched off
#> Spherical geometry (s2) switched on
#rasterVis::levelplot(henri$gridmet_prcp, par.settings = BTCTheme, main = "Hurricane Henri")
r = raster::stack(henri)
r_inches <- r/25.4
tmap_mode("plot")
fig_map <- r_inches %>%
tm_shape() +
tm_raster(style = "cont") +
tm_shape(spData::us_states) +
tm_borders()
fig_map
tmap_mode("view")
fig_map
tmap_mode("plot")
Get the maximum precipitation amount for each day of the storm
round(raster::maxValue(r_inches),2) %>% as.data.frame()
## .
## 1 4.52
## 2 3.78
## 3 5.03
## 4 4.02
## 5 2.06
## 6 8.13
## 7 7.17
## 8 3.19
## 9 1.10
temperature_US = getGridMET(aoi_get(state = "conus"),
param = "tmax",
startDate = "2021-12-30", endDate = "2021-12-30")
#> Spherical geometry (s2) switched off
#> Spherical geometry (s2) switched on
temperature_US <- raster::stack(temperature_US)
temperature_US <- temperature_US - 273.15
temperature_US <- (temperature_US*9/5) + 32
temperature_US %>%
tm_shape() +
tm_raster(style = "cont", palette = "-RdBu", midpoint = 32) +
tm_shape(spData::us_states) +
tm_borders()