Title: | Download Weather Station Data from GHCNd |
---|---|
Description: | The goal of 'GHCNr' is to provide a fast and friendly interface with the Global Historical Climatology Network daily (GHCNd) database, which contains daily summaries of weather station data worldwide (<https://www.ncei.noaa.gov/products/land-based-station/global-historical-climatology-network-daily>). GHCNd is accessed through the web API <https://www.ncei.noaa.gov/access/services/data/v1>. 'GHCNr' main functionalities consist of downloading data from GHCNd, filter it, and to aggregate it at monthly and annual scales. |
Authors: | Emilio Berti [aut, cre] |
Maintainer: | Emilio Berti <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.4.5 |
Built: | 2025-03-08 05:33:10 UTC |
Source: | https://github.com/emilio-berti/ghcnr |
Add Columns to Handle Summarize
.add_variables(x)
.add_variables(x)
x |
Object of class |
Table with number of days in the months.
Handles API Errors
.api_error(resp)
.api_error(resp)
resp |
Object of class |
NULL, called for side effects.
Check Flags Columns
.check_flags(x)
.check_flags(x)
x |
Object of class |
NULL, called for side effects
Request Daily Summaries
.daily_request(url)
.daily_request(url)
url |
Character, URL of the request. |
Body of the JSON request.
Create Request URL for Daily Summaries
.daily_url(station_id, start_date, end_date, variables)
.daily_url(station_id, start_date, end_date, variables)
station_id |
Character, station id(s). |
start_date |
Character, start date. |
end_date |
Character, end date. |
variables |
Character, vector of the variables to include. |
station_id can be a vector with multiple stations.
Dates should be given in YYYY-mm-dd
format.
Character string with the API URL.
Drop Flags Columns
.drop_flags(x)
.drop_flags(x)
x |
Object of class |
The original objects without flags column.
The GHCNd Station URL with Elevation
.elevation_url()
.elevation_url()
The URL of the GHCNd stations.
Extract GHCNd Flags
.extract_flag(x)
.extract_flag(x)
x |
Character, vector of the flag as returned by the GHCNd API call. |
https://www.ncei.noaa.gov/products/land-based-station/global-historical-climatology-network-daily
Character of the flag.
GHCNd Flags
.flags(strict)
.flags(strict)
strict |
Logical, if to include all flags (TRUE) or not (FALSE). |
Table with flags.
The GHCNd Inventory URL
.inventory_url()
.inventory_url()
The URL of the GHCNd inventory.
Calculate Maximum
.max(x)
.max(x)
x |
Numeric vector |
Numeric.
Calculate Mean
.mean(x)
.mean(x)
x |
Numeric vector |
Numeric.
Calculate Minimum
.min(x)
.min(x)
x |
Numeric vector |
Numeric.
Check Which Variables Are Absent
.missing_variables(x)
.missing_variables(x)
x |
Object of class |
Character vector
Annual Class Constructor
.s3_annual(data = tibble::tibble())
.s3_annual(data = tibble::tibble())
data |
A data frame or tibble to be used as the underlying data. |
Creates a new object of class ghcn_annual
.
An object of class ghcn_annual
.
Anomaly Constructor
.s3_anomaly(data = tibble::tibble())
.s3_anomaly(data = tibble::tibble())
data |
A data frame or tibble to be used as the underlying data. |
Creates a new object of class ghcn_anomaly
.
An object of class ghcn_anomaly
.
Daily Class Constructor
.s3_daily(data = tibble::tibble())
.s3_daily(data = tibble::tibble())
data |
A data frame or tibble to be used as the underlying data. |
Creates a new object of class ghcn_daily
.
An object of class ghcn_daily
.
Monthly Class Constructor
.s3_monthly(data = tibble::tibble())
.s3_monthly(data = tibble::tibble())
data |
A data frame or tibble to be used as the underlying data. |
Creates a new object of class ghcn_monthly
.
An object of class ghcn_monthly
.
Annual Quarter Constructor
.s3_quarterly(data = tibble::tibble())
.s3_quarterly(data = tibble::tibble())
data |
A data frame or tibble to be used as the underlying data. |
Creates a new object of class ghcn_quarterly
.
An object of class ghcn_quarterly
.
Calculate Sum
.sum(x)
.sum(x)
x |
Numeric vector |
Numeric.
annual()
aggregates the daily timeseries into an annual one.
Aggregation is done differently for TMIN, TMAX, and PRCP.
annual(x)
annual(x)
x |
Object of class |
Aggregation is done as:
Maximum temperature recorded in the year
Minimum temperature recorded in the year
Total (cumulative) precipitation amount in the year
A tibble with the annual timeseries at the stations.
annual(CA003076680)
annual(CA003076680)
annual_coverage()
calculates how many days have been recorded for
each year in the time period.
annual_coverage(x)
annual_coverage(x)
x |
Object of class |
To calculate the coverage, a full daily time range is full joined to the timeseries. Missing days are set to NA. Coverage is then calculated as the number of values that are not NAs over the number of NAs.
A table with annual coverage.
cleaned <- remove_flagged(CA003076680) cover <- annual_coverage(cleaned) cover
cleaned <- remove_flagged(CA003076680) cover <- annual_coverage(cleaned) cover
anomaly()
calculates the temperature anomalies compared to a baseline
reference period. Anomalies are the difference between annual temperature
extremes and the average across the baseline period.
If aggregate_stations = TRUE
, anomalies are averaged across all weather stations.
anomaly(x, cutoff, aggregate_stations = FALSE)
anomaly(x, cutoff, aggregate_stations = FALSE)
x |
Object of class |
cutoff |
Numeric, last year of the baseline period (inclusive). |
aggregate_stations |
Logical, if anomaly should be calculated aggregating data from all weather stations. |
cutoff
must be a character with the date, e.g. "2000-01-01".
A tibble with the anomaly timeseries at the stations.
x <- USC00010655 x <- remove_flagged(x) cover <- annual_coverage(x) years <- cover$year[cover$"annual_coverage_tmax" > .99 & cover$"annual_coverage_tmin" > .99] years <- setdiff(years, 2024) x$years <- as.numeric(format(x$date, "%Y")) x <- x[x$years %in% years, ] a <- annual(x) anom <- anomaly(a, cutoff = 2012) plot(anom)
x <- USC00010655 x <- remove_flagged(x) cover <- annual_coverage(x) years <- cover$year[cover$"annual_coverage_tmax" > .99 & cover$"annual_coverage_tmin" > .99] years <- setdiff(years, 2024) x$years <- as.numeric(format(x$date, "%Y")) x <- x[x$years %in% years, ] a <- annual(x) anom <- anomaly(a, cutoff = 2012) plot(anom)
Cast Table to Daily
as_daily(data)
as_daily(data)
data |
A data frame or tibble to be used as the underlying data. |
An object of class ghcn_daily
.
## Not run: df <- read.csv(...) df <- as_daily(df) ## End(Not run)
## Not run: df <- read.csv(...) df <- as_daily(df) ## End(Not run)
Daily data for Station CA003076680
CA003076680
CA003076680
CA003076680
A 'ghcn-daily' object, i.e. table 7,574 x 8:
Date of measurment
Station name, i.e. 'CA003076680'
Maximum temperature
Minimum temperature
Total precipitation
Flags for the measurments
https://www.countrycallingcodes.com/iso-country-codes/europe-codes.php
Countries ISO Codes
country_codes
country_codes
europe_codes
A table 253 x 2:
Country name
3 letter ISO country code
coverage()
calculates the temporal coverage of the time series.
See also monthly_coverage()
, annual_coverage()
, and period_coverage()
.
coverage(x, graph = FALSE)
coverage(x, graph = FALSE)
x |
Object of class |
graph |
Logical, if to show a graph of annual coverage. |
Returns a table with:
mothly_coverage The proportion of the days with records in the month
annual_coverage The proportion of the days with records in the year
annual_coverage The proportion of the years with records in the reference period
A table with coverage.
cleaned <- remove_flagged(CA003076680) cover <- coverage(cleaned) cover[cover$month == 1, ]
cleaned <- remove_flagged(CA003076680) cover <- coverage(cleaned) cover[cover$month == 1, ]
Download Daily Summaries
daily(station_id, start_date, end_date, variables = c("tmax", "tmin", "prcp"))
daily(station_id, start_date, end_date, variables = c("tmax", "tmin", "prcp"))
station_id |
Character, station id(s). |
start_date |
Character, start date. |
end_date |
Character, end date. |
variables |
Character, vector of the variables to include. |
station_id can be a vector with multiple stations.
Dates should be given in YYYY-mm-dd
format.
Available variables can be found at https://www.ncei.noaa.gov/pub/data/ghcn/daily/readme.txt.
A tibble with the daily timeseries at the stations.
## Not run: CA003076680 <- daily("CA003076680", "1990-01-01", "2024-12-31") ## End(Not run)
## Not run: CA003076680 <- daily("CA003076680", "1990-01-01", "2024-12-31") ## End(Not run)
Download GHCNd Inventory File
download_inventory(filename)
download_inventory(filename)
filename |
Character of the filename of the inventory, if already downloaded. |
Download the inventory from <"https://www.ncei.noaa.gov/pub/data/ghcn/daily/ghcnd-inventory.txt">.
Character, the location of the file where the inventory has been saved.
## Not run: download_inventory(...) ## End(Not run)
## Not run: download_inventory(...) ## End(Not run)
Get GHCNd Station Elevation
elevation_stations()
elevation_stations()
The table with the elevation of GHCNd stations.
## Not run: el <- elevation_stations() ## End(Not run)
## Not run: el <- elevation_stations() ## End(Not run)
Spatial Filtering of Stations
filter_stations(stations, roi)
filter_stations(stations, roi)
stations |
the table with station data. See |
roi |
the geometry of the region of interest. See |
Table with filtered stations.
## Not run: inventory <- stations() roi <- get_country("ITA") s <- filter_stations(inventory, roi) ## End(Not run)
## Not run: inventory <- stations() roi <- get_country("ITA") s <- filter_stations(inventory, roi) ## End(Not run)
Download multiple countries' shapefiles from geoBoundaries
get_countries(countries_code, simplified = TRUE)
get_countries(countries_code, simplified = TRUE)
countries_code |
Vector of three letter ISO code. |
simplified |
Logical. |
https://github.com/wmgeolab/geoBoundaries.
A shapefile.
## Not run: eu <- get_countries(country_code$iso3, simplified = TRUE) ## End(Not run)
## Not run: eu <- get_countries(country_code$iso3, simplified = TRUE) ## End(Not run)
Download country shapefile from geoBoundaries
get_country(country_code, simplified = TRUE)
get_country(country_code, simplified = TRUE)
country_code |
Three letter ISO code. |
simplified |
Logical. |
https://github.com/wmgeolab/geoBoundaries.
A shapefile.
## Not run: italy <- get_country("ITA") ## End(Not run)
## Not run: italy <- get_country("ITA") ## End(Not run)
Calculate Monthly Summaries
monthly(x)
monthly(x)
x |
Object of class |
x is the table returned from daily()
or
remove_flagged()
or any subset of them.
A tibble with the monthly timeseries at the stations.
monthly(CA003076680)
monthly(CA003076680)
monthly_coverage()
calculates how many days have been recorded for
each month in the time period.
monthly_coverage(x)
monthly_coverage(x)
x |
Object of class |
To calculate the coverage, a full daily time range is full joined to the timeseries. Missing days are set to NA. Coverage is then calculated as the number of values that are not NAs over the number of NAs.
A table with mothly coverage.
cleaned <- remove_flagged(CA003076680) cover <- monthly_coverage(cleaned) cover[cover$year == 2020, ]
cleaned <- remove_flagged(CA003076680) cover <- monthly_coverage(cleaned) cover[cover$year == 2020, ]
period_coverage()
calculates how many days have been recorded for
the whole time period.
period_coverage(x)
period_coverage(x)
x |
Object of class |
To calculate the coverage, a full daily time range is full joined to the
timeseries. Missing days are set to NA. Coverage is then calculated as
the number of values that are not NAs over the number of NAs.
Period coverage is a constant value for each station in the ghcn_daily
object.
A table with period coverage.
cleaned <- remove_flagged(CA003076680) cover <- period_coverage(cleaned) cover
cleaned <- remove_flagged(CA003076680) cover <- period_coverage(cleaned) cover
Plot GHCNd Timeseries
## S3 method for class 'ghcn_annual' plot(x, variable, ...)
## S3 method for class 'ghcn_annual' plot(x, variable, ...)
x |
Object of class |
variable |
Name of the variable to plot. |
... |
additional arguments to be passed to |
NULL, called for side effects.
plot(annual(CA003076680), "tmax")
plot(annual(CA003076680), "tmax")
Plot GHCN Timeseries
## S3 method for class 'ghcn_anomaly' plot(x, ...)
## S3 method for class 'ghcn_anomaly' plot(x, ...)
x |
Object of class |
... |
additional arguments to be passed to |
NULL, called for side effects.
plot(anomaly(remove_flagged(CA003076680), 2015))
plot(anomaly(remove_flagged(CA003076680), 2015))
Plot GHCNd Timeseries
## S3 method for class 'ghcn_daily' plot(x, variable, ...)
## S3 method for class 'ghcn_daily' plot(x, variable, ...)
x |
Object of class |
variable |
Name of the variable to plot. |
... |
additional arguments to be passed to |
NULL, called for side effects.
plot(CA003076680, "tmax")
plot(CA003076680, "tmax")
Plot GHCNd Timeseries
## S3 method for class 'ghcn_monthly' plot(x, variable, ...)
## S3 method for class 'ghcn_monthly' plot(x, variable, ...)
x |
Object of class |
variable |
Name of the variable to plot. |
... |
additional arguments to be passed to |
NULL, called for side effects.
plot(monthly(CA003076680), "tmax")
plot(monthly(CA003076680), "tmax")
Plot GHCNd Timeseries
## S3 method for class 'ghcn_quarterly' plot(x, variable, ...)
## S3 method for class 'ghcn_quarterly' plot(x, variable, ...)
x |
Object of class |
variable |
Name of the variable to plot. |
... |
additional arguments to be passed to |
NULL, called for side effects.
plot(monthly(CA003076680), "tmax")
plot(monthly(CA003076680), "tmax")
quarterly()
aggregates the daily timeseries into a quarterly one.
Aggregation is done differently for TMIN, TMAX, and PRCP.
quarterly(x)
quarterly(x)
x |
Object of class |
Quarters are defined as:
January to March
April to June
July to September
October to December
Aggregation is done as:
Maximum temperature recorded in the quarter
Minimum temperature recorded in the quarter
Total (cumulative) precipitation amount in the quarter
A tibble with the quarterly timeseries at the stations.
quarterly(CA003076680)
quarterly(CA003076680)
Remove Flagged Recrods
remove_flagged(x, strict = TRUE)
remove_flagged(x, strict = TRUE)
x |
Object of class |
strict |
Logical, if to remove also |
x
without flagged records.
remove_flagged(CA003076680)
remove_flagged(CA003076680)
Get GHCNd Inventory
stations( filename, variables = c("tmin", "tmax", "prcp"), first_year, last_year )
stations( filename, variables = c("tmin", "tmax", "prcp"), first_year, last_year )
filename |
Character, the filename of the inventory, if already downloaded. |
variables |
Character, vector of the variables to include. |
first_year |
Integer, the year since when data should be recorded. |
last_year |
Integer, the year until when data should be recorded. |
If filename is not provided, this will download the inventory from <"https://www.ncei.noaa.gov/pub/data/ghcn/daily/ghcnd-inventory.txt">. In alternative, you can download the invetory yourself and load it (see examples).
The table with the GHCNd stations.
## Not run: dest <- tempfile() download_inventory(dest) s <- stations(dest) ## End(Not run)
## Not run: dest <- tempfile() download_inventory(dest) s <- stations(dest) ## End(Not run)
Daily data for Station USC00010655
USC00010655
USC00010655
USC00010655
A 'ghcn-daily' object, i.e. table 7,809 x 8:
Date of measurment
Station name, i.e. 'USC00010655'
Maximum temperature
Minimum temperature
Total precipitation
Flags for the measurments
https://www.countrycallingcodes.com/iso-country-codes/europe-codes.php