Skip to content

Instantly share code, notes, and snippets.

@eliocamp
Created April 7, 2019 00:17
Show Gist options
  • Select an option

  • Save eliocamp/3875c0392a35b73c7e42326bf45bd79d to your computer and use it in GitHub Desktop.

Select an option

Save eliocamp/3875c0392a35b73c7e42326bf45bd79d to your computer and use it in GitHub Desktop.
Example ecmwfr
library(ecmwfr)
wf_set_key(service = "webapi")
request <- list(
class = "ei",
dataset = "interim",
expver = "1",
date = "19790101",
grid = "0.75/0.75",
levtype = "sfc",
param = "167.128",
stream = "moda",
type = "an",
target = "monthly_temp.nc",
format = "netcdf")
sfc_temps <- wf_archetype(request, c("date", "grid"))
format_dates <- function(dates) {
paste0(lubridate::year(dates),
formatC(lubridate::month(dates), width = 2, flag = "0"),
formatC(lubridate::day(dates), width = 2, flag = "0"),
collapse = "/")
}
dates <- seq.Date(as.Date("1988-08-01"), as.Date("2018-08-01"), "1 year")
wf_request(request = sfc_temps(date = dates, grid = "3/3"),
user = "eliocampitelli@gmail.com",
transfer = TRUE,
path = ".")
library(metR)
library(ggplot2)
library(data.table)
library(magrittr)
temperature <- ReadNetCDF("monthly_temp.nc")
setnames(temperature, c("longitude", "latitude"), c("lon", "lat"))
ggplot(temperature[time == time[1]], aes(lon, lat)) +
geom_contour_fill(aes(z = t2m)) +
scale_fill_divergent(midpoint = 273)
trends <- temperature[, FitLm(year = year(time), t2m, se = TRUE),
by = .(lon, lat)]
trends[, p.value := pt(abs(estimate)/std.error, df, lower.tail = FALSE)]
ggplot(trends[term == "year"], aes(lon, lat)) +
geom_contour_fill(aes(z = estimate*10)) +
stat_subset(aes(subset = p.value <= 0.01),
geom = "point", size = 0.1, alpha = 0.5) +
scale_fill_divergent()
gmt <- temperature[, .(t2m = weighted.mean(t2m, cos(lat*pi/180))), by = year(time)]
ggplot(gmt, aes(year, t2m)) +
geom_line() +
geom_smooth(method = "lm")
trend <- lm(t2m ~ year, data = gmt)
coef(trend)[2]*10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment