Skip to content

Commit

Permalink
Update due to climatereanalyzer switch to Copernicus daily data (#27)
Browse files Browse the repository at this point in the history
* Make daily temp region codes consistent with documentation

* Fixing due to  switch to Copernicus data
  • Loading branch information
cortinah authored Dec 31, 2023
1 parent 5f33079 commit 1fee5f2
Show file tree
Hide file tree
Showing 20 changed files with 71 additions and 47 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: hockeystick
Title: Download and Visualize Essential Climate Change Data
Version: 0.8.2
Version: 0.8.3
Authors@R:
person(given = "Hernando",
family = "Cortina",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@
# hockeystick 0.8.2

* Update new get_dailytemp() urls

# hockeystick 0.8.3

* Update to reflect climatereanalyzer switch to Copernicus data
90 changes: 55 additions & 35 deletions R/daily_temp.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#' @param mean_start (numeric) Start year for historic mean, defaults to 1979.
#' @param mean_end (numeric) End year for historic mean, defaults to 2000.
#'
#' @return Invisibly returns a tibble with the daily 2-meter air or sea surface temperatures since 1979 as well as historic mean by day-of-year and current anomaly versus mean.
#' @return Invisibly returns a tibble with the daily 2-meter air or sea surface temperatures since 1940 as well as historic mean by day-of-year and current anomaly versus mean.
#'
#' `get_dailytemp` invisibly returns a tibble with the daily temperatures since 1979 as well as mean by day-of-year and anomaly. Default to world data, but region can be selected among six options.
#' `get_dailytemp` invisibly returns a tibble with the daily temperatures since 1940 as well as mean by day-of-year and anomaly. Default to world data, but region can be selected among six options.
#'
#' Region options include world air (default), Northern Hemisphere air, Southern Hemisphere air, Tropics air, Arctic air, Antarctic air, World sea surface and North Atlantic sea surface and is stored in attribute of output.
#' The historic daily mean-by-day period defaults to 1979-2000. This range can be optionally modified.
Expand Down Expand Up @@ -48,19 +48,20 @@
#' \itemize{
#' \item ClimateReanalyzer.org: \url{https://climatereanalyzer.org/clim/t2_daily/}
#'
#' Notes: The "Daily 2-meter Air Temperature" page shows area-weighted daily means calculated from the 2-meter air temperature variable from the Climate Forecast System version 2 and Climate Forecast System Reanalysis, which are publicly available products of the NOAA National Centers for Environmental Prediction.
#' Sea surface is from NOAA Optimum Interpolation SST (OISST) version 2.1. OISST is a 0.25°x0.25° gridded dataset that provides estimates of temperature based on a blend of satellite, ship, and buoy observations. The dataset spans 1 January 1982 to present with a 1 to 2-day lag from the current day.
#' Notes: daily mean surface air temperature (2-meter height) estimates from the ECMWF Reanalysis version 5 (ERA5) for the period January 1940 to present. ERA5 is a state-of-the-art numerical climate/weather modeling framework that ingests surface, radiosonde, and satellite observations to estimate the state of the atmosphere through time.
#' ERA5 files have a horizontal grid resolution of 0.25° x 0.25° (about 31km x 31km at 45°N). Each daily temperature represents an average across all model gridcells within the defined latitude/longitude bounds for the selected domain. The means are area-weighted to account for the convergence of longitude lines at the poles
#'
#' }
#'
#' @export

get_dailytemp <- function(use_cache = TRUE, write_cache = getOption("hs_write_cache"),
region = 'W',
mean_start = if(region == 'WS' | region == 'AS') 1982 else 1979,
mean_start = if(region %in% c('WS','NS', 'ws', 'ns')) 1982 else 1979,
mean_end = 2000) {

hs_path <- tools::R_user_dir("hockeystick", "cache")
region <- toupper(region)

if (use_cache) {
if (file.exists(file.path(hs_path, 'dailytemp.rds')))
Expand All @@ -72,17 +73,17 @@ get_dailytemp <- function(use_cache = TRUE, write_cache = getOption("hs_write_ca
}

file_url <- switch(region,
W='https://climatereanalyzer.org/clim/t2_daily/json_cfsr/cfsr_world_t2_day.json', # World Air
NH='https://climatereanalyzer.org/clim/t2_daily/json_cfsr/cfsr_nh_t2_day.json', # Northern Hemi Air
SH='https://climatereanalyzer.org/clim/t2_daily/json_cfsr/cfsr_sh_t2_day.json', # Southern Hemi Air
AR='https://climatereanalyzer.org/clim/t2_daily/json_cfsr/cfsr_arctic_t2_day.json', # Arctic Air
AN='https://climatereanalyzer.org/clim/t2_daily/json_cfsr/cfsr_antarctic_t2_day.json', # #Antarctic Air
TR='https://climatereanalyzer.org/clim/t2_daily/json_cfsr/cfsr_tropics_t2_day.json', # Tropics Air
WS='https://climatereanalyzer.org/clim/sst_daily/json/oisst2.1_world2_sst_day.json', # World Sea
AS='https://climatereanalyzer.org/clim/sst_daily/json/oisst2.1_natlan1_sst_day.json') # North Atlantic Sea
W = 'https://climatereanalyzer.org/clim/t2_daily/json/era5_world_t2_day.json', # World Air
NW = 'https://climatereanalyzer.org/clim/t2_daily/json/era5_nh_t2_day.json', # Northern Hemi Air
SW = 'https://climatereanalyzer.org/clim/t2_daily/json/era5_sh_t2_day.json', # Southern Hemi Air
AR = 'https://climatereanalyzer.org/clim/t2_daily/json/era5_arctic_t2_day.json', # Arctic Air
AN = 'https://climatereanalyzer.org/clim/t2_daily/json/era5_antarctic_t2_day.json', # #Antarctic Air
TR = 'https://climatereanalyzer.org/clim/t2_daily/json/era5_tropics_t2_day.json', # Tropics Air
WS = 'https://climatereanalyzer.org/clim/sst_daily/json/oisst2.1_world2_sst_day.json', # World Sea
NS = 'https://climatereanalyzer.org/clim/sst_daily/json/oisst2.1_natlan1_sst_day.json') # North Atlantic Sea

connected <- .isConnected(file_url)
if (!connected) {message("Retrieving remote data requires internet connectivity."); return(invisible(NULL))}
if (!connected) {message("Retrieving remote data requires internet connectivity.\nAvailable regions are: W, NW, SW, AR, AN, TR, WS, NS."); return(invisible(NULL))}

dl <- tempfile()
download.file(file_url, dl)
Expand All @@ -93,22 +94,38 @@ get_dailytemp <- function(use_cache = TRUE, write_cache = getOption("hs_write_ca
} )
if (is.null(temp_json)) return(invisible(NULL))

leap_years <- seq.int(1980, 2032, 4)
leap_years <- seq.int(1940, 2032, 4)

if (region!='WS' && region!='NS') {

suppressWarnings(
daily_temperature <- as_tibble(temp_json) |>
separate_wider_delim('data', delim=',', names_sep = '') |>
daily_temperature <- as_tibble(temp_json) |> head(-3) |>
separate_wider_delim('data', delim=', ', names_sep = '') |>
mutate(data1 = parse_number(data1),
data366 = parse_number(data366, na = 'NA)')) |>
head(-3) |> mutate_all(as.numeric) |>
select(-data_source) |> mutate_all(as.numeric) |>
pivot_longer(2:367, names_to = 'day_of_year') |>
rename(year = name,
temp = value) |>
mutate(day_of_year = parse_number(day_of_year)) |>
filter(!(day_of_year==60 & year %in% leap_years)) |>
drop_na()
)
) } else {

suppressWarnings(
daily_temperature <- as_tibble(temp_json) |> head(-3) |> tail(-1) |>
separate_wider_delim('data', delim=',', names_sep = '') |>
mutate(data1 = parse_number(data1),
data366 = parse_number(data366, na = 'NA)')) |>
mutate_all(as.numeric) |>
pivot_longer(2:367, names_to = 'day_of_year') |>
rename(year = name,
temp = value) |>
mutate(day_of_year = parse_number(day_of_year)) |>
filter(!(day_of_year==60 & year %in% leap_years)) |>
drop_na()
) }



daily_temperature <- daily_temperature |> group_by(year) |>
Expand All @@ -119,13 +136,13 @@ daily_temperature <- daily_temperature |> group_by(year) |>
ungroup()

dates <- data.frame(day_of_year=1:365,
dummy_date=seq(as.Date('1975-01-01'), as.Date('1975-12-31'), by = '1 days'))
dummy_date=seq(as.Date('1925-01-01'), as.Date('1925-12-31'), by = '1 days'))

daily_temperature <- left_join(daily_temperature, dates, by='day_of_year')

colnames(daily_temperature)[4] <- paste0(mean_start,'-',mean_end,' mean')

if (region == 'WS' | region == 'AS') daily_temperature <- daily_temperature |> filter(year!=1981)
if (region == 'WS' | region == 'NS') daily_temperature <- daily_temperature |> filter(year!=1981)

attr(daily_temperature, "hs_daily_region") <- region

Expand All @@ -137,7 +154,7 @@ invisible(daily_temperature)

#' Download and plot essential climate data
#'
#' Plots the daily temperatures and anomaly since 1979 retrieved using `get_dailytemp()` with ggplot2. The output ggplot2 object may be further modified.
#' Plots the daily temperatures since 1940 and current anomaly data retrieved using `get_dailytemp()` with ggplot2. The output ggplot2 object may be further modified.
#'
#'
#' @name plot_dailytemp
Expand Down Expand Up @@ -188,49 +205,51 @@ colnames(dataset)[4] <- 'mean_temp'

region <- attr(dataset, "hs_daily_region")

subtitle_lab <- '2-meter temperature since 1979 and mean'
subtitle_lab <- '2-meter temperature since 1940 and mean'

if (region == 'WS' ||region == 'AS') subtitle_lab <- 'Sea surface temperature since 1982 and mean'
if (region == 'WS' ||region == 'NS') subtitle_lab <- 'Sea surface temperature since 1982 and mean'

if (title_lab == "Daily Average Air Temperature") {

if (region == 'WS' | region == 'AS') title_lab <- 'Daily Average Sea Surface Temperature'
if (region == 'WS' | region == 'NS') title_lab <- 'Daily Average Sea Surface Temperature'

region <- switch(region,
W = 'World',
NH = 'Northern Hemisphere',
SH = 'Southern Hemisphere',
W = 'World',
NW = 'Northern Hemisphere',
SW = 'Southern Hemisphere',
AR = 'Arctic',
AN = 'Antarctic',
TR = 'Tropics',
WS = 'World (60S-60N)',
AS = 'North Atlantic')
NS = 'North Atlantic')

title_lab <- paste(region, title_lab)}

# Color code notes: 'A': all years, 'M': mean, 'L': latest.

plot <- ggplot(dataset) +
geom_line(aes(x = dummy_date, y = temp, group = year), alpha = 0.7, color = 'grey') +
geom_line(aes(x = dummy_date, y = temp, group = year, color = 'A'), alpha = 0.7) +
scale_fill_gradientn(name = 'Anomaly (C\U00B0)', colors = RColorBrewer::brewer.pal(9, 'YlOrRd'), labels = scales::label_number(accuracy = 0.1)) +
geom_line(aes(dummy_date, mean_temp, color = 'M'), linetype = 'dashed', linewidth = 1.1) +
scale_y_continuous(n.breaks = 9) +
theme_bw(base_size = 12) +
scale_x_date(name = 'Day of year', breaks = c(as.Date('1975-01-01'), as.Date('1975-04-01'),
as.Date('1975-07-01'), as.Date('1975-10-01'), as.Date('1975-12-31')),
scale_x_date(name = 'Day of year', breaks = c(as.Date('1925-01-01'), as.Date('1925-04-01'),
as.Date('1925-07-01'), as.Date('1925-10-01'), as.Date('1925-12-31')),
date_labels = '%b%est', date_minor_breaks = '1 month') +
labs(title = title_lab, subtitle = subtitle_lab,
y = 'Temperature (C\U00B0)',
caption = paste0('Source: Climate Change Institute, University of Maine\nClimateReanalyzer.org as of ', latest),
color = NULL) +
scale_color_manual(values = c('firebrick', 'black', 'grey'), labels = c(current_year, meanperiod), breaks = c('L', 'M')) +
scale_color_manual(values = c('firebrick', 'black', 'grey'), labels = c(current_year, meanperiod, 'All years'), breaks = c('L', 'M', 'A')) +
geom_line(data = filter(dataset, year == current_year),
aes(dummy_date, temp, color = 'L'), linewidth = 1.3) +
theme(legend.position = "top") + theme(legend.key.size = unit(0.5, 'cm'),
legend.margin = margin(5, 0, 0, 0))

if (anomaly) {
subtitle_lab <- '2-meter temperature since 1979, mean, and current anomaly'
subtitle_lab <- '2-meter temperature since 1940, mean, and current anomaly'

if (region == 'World (60S-60N)' | region == 'North Atlantic' | region == 'WS' | region == 'AS')
if (region == 'World (60S-60N)' | region == 'North Atlantic' | region == 'WS' | region == 'NS')
subtitle_lab <- 'Sea surface temperature since 1982, mean, and current anomaly'

plot <- plot +
Expand All @@ -242,6 +261,7 @@ if (anomaly) {
fill = temp_anom)) +
geom_line(data = filter(dataset, year == current_year),
aes(dummy_date, temp, color='L'), linewidth = 1.3) +
geom_line(aes(dummy_date, mean_temp, color = 'M'), linetype = 'dashed', linewidth = 1.2) +
labs(subtitle = subtitle_lab) }

if (print) suppressMessages( print(plot) )
Expand Down
2 changes: 1 addition & 1 deletion R/globalvars.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
utils::globalVariables(c('average', 'trend', 'year','Year', 'J-D', 'gmsl', 'method', 'month', 'name',
'age_ice', 'co2', 'land_use_change_co2', 'temp', 'value', 'size', 'measure','RevisedMajorHurricanes', 'RevisedACE',
'cumco2', 'country', 'data1', 'data366', 'day_of_year', 'dummy_date', 'linew', 'mean_temp', 'temp_anom',
'hurr_type', 'ice_region'))
'hurr_type', 'ice_region', 'data_source'))
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ remotes::install_github("cortinah/hockeystick")

## Downloading and viewing global heating data

New: Plot daily global temperatures since 1979 and current anomaly:
New: Plot daily global temperatures since 1940 and current anomaly:

```{r dailytemp, fig.retina=2, message=FALSE, out.width='60%', fig.asp=1.03}
library(hockeystick)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ remotes::install_github("cortinah/hockeystick")

## Downloading and viewing global heating data

New: Plot daily global temperatures since 1979 and current anomaly:
New: Plot daily global temperatures since 1940 and current anomaly:

``` r
library(hockeystick)
Expand Down
4 changes: 2 additions & 2 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ClimateReanalyzer
Cowtan
Delaygue
Dlugokencky
ECMWF
ERSST
ESRL
ESS
Expand Down Expand Up @@ -49,7 +50,6 @@ NOAA's
NOAA’s
NSIDC
Neukom
OISST
ORCID
OWID
Organisation
Expand Down Expand Up @@ -91,7 +91,7 @@ edX
et
ggplot
github
gridded
gridcells
hs
https
icecurves
Expand Down
Binary file modified man/figures/README-carbon-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-dailytemp-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-emissions-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-emissions-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-emissionsmap-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-grid-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-icecurves-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-methane-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-stripes-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-stripes2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-temp-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions man/get_dailytemp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/plot_dailytemp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1fee5f2

Please sign in to comment.