Skip to content

Commit

Permalink
hockeystick 0.8.5 on CRAN (#31)
Browse files Browse the repository at this point in the history
* Remove unnnecessary fill causing ggplot warning

* Control date labels

* add merged temp carbon function and plots

* tweak temp scatter

* add daily cop

* update daily temp plot

* Update News

* fixed bugs in globalvars and examples

* Update Mauna Loa carbon to http from ftp

* V0.8.5 for CRAN
  • Loading branch information
cortinah authored Nov 25, 2024
1 parent f2cb5c8 commit 83a0924
Show file tree
Hide file tree
Showing 41 changed files with 598 additions and 251 deletions.
4 changes: 2 additions & 2 deletions 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.4
Version: 0.8.5
Authors@R:
person(given = "Hernando",
family = "Cortina",
Expand All @@ -11,7 +11,7 @@ Description: Provides easy access to essential climate change datasets to non-cl
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
URL: https://cortinah.github.io/hockeystick/, https://github.com/cortinah/hockeystick
BugReports: https://github.com/cortinah/hockeystick/issues
Imports: ggplot2, lubridate, tools, readr, dplyr, tidyr, patchwork, scales, rvest, tibble, treemapify, RColorBrewer, jsonlite
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
YEAR: 2023
YEAR: 2024
COPYRIGHT HOLDER: Hernando Cortina
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License

Copyright (c) 2023 Hernando Cortina
Copyright (c) 2024 Hernando Cortina

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(climate_grid)
export(emissions_map)
export(get_carbon)
export(get_dailytemp)
export(get_dailytempcop)
export(get_emissions)
export(get_hurricanes)
export(get_icecurves)
Expand All @@ -19,7 +20,9 @@ export(hockeystick_cache_delete_all)
export(hockeystick_cache_details)
export(hockeystick_cache_list)
export(hockeystick_update_all)
export(merge_carbontemp)
export(plot_carbon)
export(plot_carbontemp)
export(plot_dailytemp)
export(plot_emissions)
export(plot_emissions_with_land)
Expand All @@ -37,6 +40,7 @@ export(plot_temp_scatter)
export(warming_stripes)
import(dplyr)
import(ggplot2)
import(lubridate)
import(patchwork)
import(rvest)
import(tidyr)
Expand Down
25 changes: 3 additions & 22 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,87 +1,68 @@
# hockeystick 0.0.0.9000

* Added a `NEWS.md` file to track changes to the package.

# hockeystick 0.1.0

* Five datasets and warming stripes included in package

# hockeystick 0.2.0

* Add climate_grid() and hockeystick_update_all()

# hockeystick 0.3.0

* Add get_icecurves() and plot_icecurves() to visualize current and historic sea ice

# hockeystick 0.3.4

* Don't cache data by default, added write_cache options

# hockeystick 0.4.0

* Add get_temp2k() and plot_temp2k() to visualize global Common Era reconstructed temp

# hockeystick 0.5.0

* Add Atlantic hurricane data, get_hurricanes(), plot_hurricanes(), plot_hurricane_nrg()

# hockeystick 0.5.1

* Update caching approach to comply with CRAN policies

# hockeystick 0.5.2

* Add date/value annotations to charts

# hockeystick 0.6.0

* Add global emissions from GCP: get_emissions(), plot_emissions()

# hockeystick 0.6.1

* Fix leaving example data in cache

# hockeystick 0.6.2

* Update documentation to conform with R version 4.2

# hockeystick 0.6.3

* Add connectivity checks to conform with CRAN requirements

# hockeystick 0.7.0

* Added methane data and cumulative co2 emissions treemap visualization

# hockeystick 0.7.1

* Plot GCP emissions with land use change

# hockeystick 0.7.2

* url-specific connectivity checks

# hockeystick 0.7.3

* Fix climate grid connectivity checks

# hockeystick 0.8.0

* Add daily temperature data from ClimateReanalyzer.org

# hockeystick 0.8.1

* Select hurricane category in get_hurricanes() / Fix daily temp caching

# hockeystick 0.8.2

* Update new get_dailytemp() urls

# hockeystick 0.8.3

* Update to reflect climatereanalyzer switch to Copernicus data

# hockeystick 0.8.4

* Update to plot_temp_scatter() and plot_temp_monthly()

# hockeystick 0.8.5
* Add plot_carbontemp() and get_dailytemp_cop(), default to Copernicus
4 changes: 2 additions & 2 deletions R/2kyears_temp.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' Retrieves the Common Era Global Surface Temperature Reconstructions.
#' Source is PAGES2k Consortium and NOAA National Centers for Environmental Information.
#' \url{https://www.ncei.noaa.gov/access/paleo-search/study/26872)}
#' \url{https://www.ncei.noaa.gov/access/paleo-search/study/26872}
#'
#' @name get_temp2k
#' @param use_cache (boolean) Return cached data if available, defaults to TRUE. Use FALSE to fetch updated data.
Expand Down Expand Up @@ -50,7 +50,7 @@ hs_path <- tools::R_user_dir("hockeystick","cache")
if (use_cache) {
if (file.exists(file.path(hs_path,'temp2k.rds'))) return(invisible(readRDS((file.path(hs_path,'temp2k.rds'))))) }

file_url <- 'https://www1.ncdc.noaa.gov/pub/data/paleo/pages2k/neukom2019temp/recons/Full_ensemble_median_and_95pct_range.txt'
file_url <- 'https://www.ncei.noaa.gov/pub/data/paleo/pages2k/neukom2019temp/recons/Full_ensemble_median_and_95pct_range.txt'
connected <- .isConnected(file_url)
if (!connected) {message("Retrieving remote data requires internet connectivity."); return(invisible(NULL))}

Expand Down
4 changes: 2 additions & 2 deletions R/carbon.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ if (use_cache & !write_cache) {
if (file.exists(file.path(hs_path,'maunaloa.rds'))) return(invisible(readRDS((file.path(hs_path,'maunaloa.rds')))))
}

file_url <- 'ftp://aftp.cmdl.noaa.gov/products/trends/co2/co2_mm_mlo.txt'
file_url <- 'https://gml.noaa.gov/webdata/ccgg/trends/co2/co2_mm_mlo.csv'
connected <- .isConnected(file_url)
if (!connected) {message("Retrieving remote data requires internet connectivity."); return(invisible(NULL))}

dl <- tempfile()
download.file(file_url, dl)
maunaloa <- suppressMessages( read_table(dl, col_names = FALSE, skip = 63) )
maunaloa <- suppressMessages( read_csv(dl, col_names = FALSE, skip = 41) )
colnames(maunaloa) <- c('year', 'month', 'date', 'average', 'trend', 'ndays','stdev','unc')
maunaloa$date <- ceiling_date(ymd(paste(maunaloa$year, maunaloa$month, '01',sep='-')), unit='month')-1

Expand Down
114 changes: 104 additions & 10 deletions R/daily_temp.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Download and plot essential climate data
#'
#' Retrieves the daily air or sea-surface temperature data since 1979 from ClimateReanalyzer.org
#' Retrieves the daily air or sea-surface temperature data since 1940 from ClimateReanalyzer.org
#' Source is University of Maine Climate Change Institute.
#' \url{https://climatereanalyzer.org/clim/t2_daily/}
#'
Expand Down Expand Up @@ -158,16 +158,17 @@ invisible(daily_temperature)

#' Download and plot essential climate data
#'
#' Plots the daily temperatures since 1940 and current anomaly data 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_dailytempcop()` with ggplot2. The output ggplot2 object may be further modified.
#'
#'
#' @name plot_dailytemp
#' @param dataset Name of the tibble generated by `get_dailytemp`
#' @param dataset Name of the tibble generated by `get_dailytempcop` or `get_dailytemp`
#' @param print (boolean) Display daily temperature ggplot2 chart, defaults to TRUE. Use FALSE to not display chart.
#' @param anomaly (boolean) Display current anomaly versus historic mean, defaults to TRUE.
#' @param maxtemp (boolean) Display current deviation versus historic max, defaults to FALSE.
#' @param current_year (numeric) Year to highlight in alternate color, defaults to current year.
#' @param title_lab (string) Title to override default chart title. Default title pulls region name from dataset attributes.
#' @param cop (boolean) Flag for chart caption, TRUE = Copernicus, FALSE =. ClimateReanalyzer.org
#'
#' @return Invisibly returns a ggplot2 object with daily temperature anomaly chart
#'
Expand All @@ -180,15 +181,15 @@ invisible(daily_temperature)
#' @examples
#' \donttest{
#' # Fetch temperature anomaly:
#' dailydata <- get_dailytemp()
#' dailydata <- get_dailytempcop()
#' #
#' # Plot output using package's built-in ggplot2 defaults
#' plot_dailytemp(dailydata)
#'
#' # Don't plot anomaly shading and highight specific year
#' plot_dailytemp(anomaly = FALSE, current_year = 2012)
#'
#' # Or just call plot_temp(), which defaults to get_temp() dataset
#' # Or just call plot_temp(), which defaults to get_dailytempcop() dataset
#' plot_dailytemp()
#'
#' p <- plot_dailytemp(dailydata, print = FALSE)
Expand All @@ -198,9 +199,9 @@ invisible(daily_temperature)
#'
#' @export

plot_dailytemp <- function(dataset = get_dailytemp(), print = TRUE, anomaly = FALSE, maxtemp = FALSE,
plot_dailytemp <- function(dataset = get_dailytempcop(), print = TRUE, anomaly = FALSE, maxtemp = FALSE,
current_year = as.numeric(substr(Sys.Date(), 1, 4)),
title_lab = 'Daily Average Air Temperature') {
title_lab = 'Daily Average Air Temperature', cop=TRUE) {

if (is.null(dataset)) return(invisible(NULL))

Expand Down Expand Up @@ -234,9 +235,12 @@ if (title_lab == "Daily Average Air Temperature") {

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

captionsource <- if (cop) paste0('Source: EU Copernicus Climate Service\ncds.climate.copernicus.eu as of ', latest)
else
paste0('Source: Climate Change Institute, University of Maine\nClimateReanalyzer.org as of ', latest)

plot <- ggplot(dataset) +
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) +
Expand All @@ -245,7 +249,7 @@ plot <- ggplot(dataset) +
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),
caption = captionsource,
color = NULL) +
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),
Expand Down Expand Up @@ -273,7 +277,7 @@ if (anomaly) {
labs(subtitle = subtitle_lab) }

if (maxtemp) {
subtitle_lab <- '2-meter temperature since 1979, mean, and deviation from historic max'
subtitle_lab <- '2-meter temperature since 1940, mean, and deviation from historic max'

if (region == 'World (60S-60N)' | region == 'North Atlantic' | region == 'WS' | region == 'AS')
subtitle_lab <- 'Sea surface temperature since 1982, mean, and current anomaly'
Expand All @@ -297,3 +301,93 @@ if (maxtemp) {
if (print) suppressMessages( print(plot) )
invisible(plot)
}


#' Download and plot essential climate data
#'
#' Retrieves the daily air temperature since 1940 from the EU Copernicus Service
#' \url{https://cds.climate.copernicus.eu/#!/home}
#'
#' @name get_dailytempcop
#' @param use_cache (boolean) Return cached data if available, defaults to TRUE. Use FALSE to fetch updated data.
#' @param write_cache (boolean) Write data to cache, defaults to FALSE. Use TRUE to write data to cache for later use. Can also be set using options(hs_write_cache=TRUE)
#' @param region (string) Region selection, defaults to world air temperature. Options are: World Air "W".
#'
#' @return Invisibly returns a tibble with the daily 2-meter air temperatures since 1940 as well as historic mean by day-of-year and current anomaly versus mean.
#'
#' `get_dailytempcop` invisibly returns a tibble with the daily temperatures since 1940 as well as mean by day-of-year and anomaly.
#'
#' Region options include world air (default).
#' The historic daily mean-by-day period defaults to 1991-2020.
#'
#' Data are updated daily.
#'
#' @importFrom utils download.file head tail
#' @importFrom readr parse_number
#' @import tidyr
#' @import dplyr
#'
#' @examples
#' \donttest{
#' # Fetch temp anomaly from cache if available:
#' dailytemps <- get_dailytempcop()
#' #
#' # Force cache refresh:
#' dailytemps <- get_dailytempcop(use_cache=FALSE)
#' #
#' # Review cache contents and last update dates:
#' hockeystick_cache_details()
#' #
#' # Plot output using package's built-in ggplot2 settings
#' plot_dailytemp(dailytemps) }
#'
#' @author Hernando Cortina, \email{hch@@alum.mit.edu}
#' @references
#' \itemize{
#' \item Copernicus: \url{https://cds.climate.copernicus.eu/#!/home}
#'
#' 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_dailytempcop <- function(use_cache = TRUE, write_cache = getOption("hs_write_cache"), region = 'W') {

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

if (use_cache) {
if (file.exists(file.path(hs_path, 'dailytempcop.rds')))

{cached_temp <- readRDS((file.path(hs_path, 'dailytempcop.rds')))
cached_region <- attr(cached_temp, "hs_daily_region")

if (region==cached_region) return(invisible(cached_temp)) }
}

file_url <- 'https://sites.ecmwf.int/data/climatepulse/data/series/era5_daily_series_2t_global.csv'

connected <- .isConnected(file_url)
if (!connected) {message("Retrieving remote data requires internet connectivity."); return(invisible(NULL))}

dl <- tempfile()
download.file(file_url, dl)
temp_csv <- read.csv(dl, skip = 18)

colnames(temp_csv) <- c('date', 'temp', '1991-2020 mean', 'temp_anom', 'status' )
temp_csv$date <- as.Date(temp_csv$date)
temp_csv$year <- lubridate::year(temp_csv$date)
temp_csv$dummy_date <- as.Date(paste("1925", lubridate::month(temp_csv$date), lubridate::day(temp_csv$date),sep = '-'))

temp_csv <- temp_csv |> filter(!is.na(dummy_date))
temp_csv <- temp_csv |> group_by(year) |> mutate(day_of_year = row_number()) |> ungroup()

daily_temperature <- temp_csv |> select(year, day_of_year, date, temp, `1991-2020 mean`, temp_anom, dummy_date) |> as_tibble()

attr(daily_temperature, "hs_daily_region") <- region

if (write_cache) saveRDS(daily_temperature, file.path(hs_path, 'dailytempcop.rds'))

invisible(daily_temperature) }
Loading

0 comments on commit 83a0924

Please sign in to comment.