Title: | UK National River Flow Archive Data from R |
---|---|
Description: | Utility functions to retrieve data from the UK National River Flow Archive (<https://nrfa.ceh.ac.uk/>, terms and conditions: <https://nrfa.ceh.ac.uk/costs-terms-and-conditions>). The package contains R wrappers to the UK NRFA data temporary-API. There are functions to retrieve stations falling in a bounding box, to generate a map and extracting time series and general information. The package is fully described in Vitolo et al (2016) "rnrfa: An R package to Retrieve, Filter and Visualize Data from the UK National River Flow Archive" <https://journal.r-project.org/archive/2016/RJ-2016-036/RJ-2016-036.pdf>. |
Authors: | Ilaria Prosdocimi [ctb, cre] |
Maintainer: | Ilaria Prosdocimi <[email protected]> |
License: | GPL-3 |
Version: | 2.1.0.6 |
Built: | 2025-02-14 04:12:43 UTC |
Source: | https://github.com/ilapros/rnrfa |
rnrfa: UK National River Flow Archive Data from R.
Utility functions to retrieve data from the UK National River Flow Archive (https://nrfa.ceh.ac.uk/). The package contains R wrappers to the UK NRFA data temporary-API. There are functions to retrieve stations falling in a bounding box, to generate a map and extracting time series and general information.
This function pulls the list of stations (and related metadata), falling within a given bounding box, from the CEH National River Flow Archive website.
catalogue( bbox = NULL, column_name = NULL, column_value = NULL, min_rec = NULL, all = TRUE )
catalogue( bbox = NULL, column_name = NULL, column_value = NULL, min_rec = NULL, all = TRUE )
bbox |
this is a geographical bounding box (e.g. list(lon_min = -3.82, lon_max = -3.63, lat_min = 52.43, lat_max = 52.52)) |
column_name |
name of column to filter |
column_value |
string to search in column_name |
min_rec |
minimum number of recording years |
all |
if TRUE it returns all the available metadata. If FALSE, it returns only the following columns: id, name, river, hydrometricArea, catchmentArea, lat, lon, selected feh catchment descriptors. |
coordinates of bounding box are required in WGS84 (EPSG: 4326). If BB coordinates are missing, the function returns the list corresponding to the maximum extent of the network.
tibble table containing the list of stations and related metadata
Claudia Vitolo
## Not run: # Retrieve all the stations in the network x <- catalogue() # Define a bounding box: bbox <- list(lon_min=-3.82, lon_max=-3.63, lat_min=52.43, lat_max=52.52) # Get stations within the bounding box x <- catalogue(bbox) # Get stations based on minimum catchment area x <- catalogue(column_name = "catchment-area", column_value = 2000) # Get stations based on minimum number of recording years x <- catalogue(min_rec=30) ## End(Not run)
## Not run: # Retrieve all the stations in the network x <- catalogue() # Define a bounding box: bbox <- list(lon_min=-3.82, lon_max=-3.63, lat_min=52.43, lat_max=52.52) # Get stations within the bounding box x <- catalogue(bbox) # Get stations based on minimum catchment area x <- catalogue(column_name = "catchment-area", column_value = 2000) # Get stations based on minimum number of recording years x <- catalogue(min_rec=30) ## End(Not run)
Given the station ID number(s), this function retrieves data (time series in zoo format with accompanying metadata) from the WaterML2 service on the NRFA database. Catchment Mean Rainfall is measured in mm/month.
cmr(id, metadata = FALSE, cl = NULL, verbose = FALSE)
cmr(id, metadata = FALSE, cl = NULL, verbose = FALSE)
id |
station ID number(s), each number should be in the range [3002,236051]. |
metadata |
Logical, FALSE by default. If metadata = TRUE means that the result for a single station is a list with two elements: data (the time series) and meta (metadata). |
cl |
(optional) This is a cluster object, created by the parallel package. This is set to NULL by default, which sends sequential calls to the server. |
verbose |
(FALSE by default). If set to TRUE prints GET request on the console. |
list composed of as many objects as in the list of station ID numbers. Each object can be accessed using their names or index (e.g. x[[1]], x[[2]], and so forth). Each object contains a zoo time series.
Claudia Vitolo
## Not run: cmr(18019) cmr(c(54022,54090,54091)) ## End(Not run)
## Not run: cmr(18019) cmr(c(54022,54090,54091)) ## End(Not run)
This function converts flow time series from cumecs (m3/s) to mm/d by dividing the flow by the catchment area and converting it to mm/day.
convert_flow(flow_cumecs, catchment_area)
convert_flow(flow_cumecs, catchment_area)
flow_cumecs |
This is the flow time series in cumecs (m3/s) |
catchment_area |
This is the catchment are in Km2. |
Flow time series in mm/d
## Not run: convert_flow(30, 2) ## End(Not run)
## Not run: convert_flow(30, 2) ## End(Not run)
Given the station ID number(s), this function retrieves data
(time series in zoo format with accompanying metadata) from the WaterML2
service on the NRFA database. Gauged Daily Flow is measured in /s.
gdf(id, metadata = FALSE, cl = NULL)
gdf(id, metadata = FALSE, cl = NULL)
id |
station ID number(s), each number should be in the range [3002,236051]. |
metadata |
Logical, FALSE by default. If metadata = TRUE means that the result for a single station is a list with two elements: data (the time series) and meta (metadata). |
cl |
(optional) This is a cluster object, created by the parallel package. This is set to NULL by default, which sends sequential calls to the server. |
list composed of as many objects as in the list of station ID numbers. Each object can be accessed using their names or index (e.g. x[[1]], x[[2]], and so forth). Each object contains a zoo time series.
Claudia Vitolo
## Not run: gdf(18019) gdf(c(54022,54090,54091)) ## End(Not run)
## Not run: gdf(18019) gdf(c(54022,54090,54091)) ## End(Not run)
Given the station identification number(s), this function
retrieves data (time series in zoo format with accompanying metadata) from
the WaterML2 service on the NRFA database. The time series can be of two
types: cmr
(catchment mean rainfall, monthly) or gdf
(gauged daily flows, daily).
get_ts(id, type, metadata = FALSE, cl = NULL, full_info = FALSE)
get_ts(id, type, metadata = FALSE, cl = NULL, full_info = FALSE)
id |
station identification number(s), each number should be in the range [3002,236051]. |
type |
The following data-types are available:
|
metadata |
Logical, FALSE by default. When metadata = TRUE the result for a single station is a list with two elements: data (the time series) and meta (metadata). |
cl |
(optional) This is a cluster object, created by the parallel package. This is set to NULL by default, which sends sequential calls to the server. |
full_info |
Logical, FALSE by default. If full_info = TRUE, the function will retrieve information on rejected periods. |
list composed of as many objects as in the list of station
identification numbers. Each object can be accessed using their names or
indexes (e.g. x[[1]], x[[2]], and so forth). Each object contains a time
series of class zoo/xts
.
Claudia Vitolo
## Not run: get_ts(18019, type = "cmr") get_ts(c(54022,54090,54091), type = "cmr") get_ts(18019, type = "gdf") get_ts(c(54022,54090,54091), type = "gdf") plot(get_ts(id = 23001, type = "ndf")) plot(get_ts(id = 23001, type = "nmf")) ## End(Not run)
## Not run: get_ts(18019, type = "cmr") get_ts(c(54022,54090,54091), type = "cmr") get_ts(18019, type = "gdf") get_ts(c(54022,54090,54091), type = "gdf") plot(get_ts(id = 23001, type = "ndf")) plot(get_ts(id = 23001, type = "nmf")) ## End(Not run)
This function converts an Ordnance Survey (OS) grid reference to easting/northing or latitude/longitude coordinates.
osg_parse(grid_refs, coord_system = c("BNG", "WGS84"))
osg_parse(grid_refs, coord_system = c("BNG", "WGS84"))
grid_refs |
This is a string (or a character vector) that contains the OS grid Reference. |
coord_system |
By default, this is "BNG" which stands for British National Grids. The other option is to set coord_system = "WGS84", which returns latitude/longitude coordinates (more info can be found here https://www.epsg-registry.org/). |
vector made of two elements: the easting and northing (by default) or latitude and longitude coordinates.
Claudia Vitolo (Ilaria Prosdocimi ported to sf)
## Not run: # single entry osg_parse(grid_refs = "TQ722213") # multiple entries osg_parse(grid_refs = c("SN831869","SN829838")) # multiple entries with missing values, NA will be returned osg_parse(grid_refs = c("SN831869",NA, "SN829838", NA)) ## End(Not run)
## Not run: # single entry osg_parse(grid_refs = "TQ722213") # multiple entries osg_parse(grid_refs = c("SN831869","SN829838")) # multiple entries with missing values, NA will be returned osg_parse(grid_refs = c("SN831869",NA, "SN829838", NA)) ## End(Not run)
This function retrieves rainfall and flow time series for a given catchment, divides the flow by the catchment area and converts it to mm/day to that it can be comparable with the rainfall (mm/month). Finally it generates a plots combining rainfall and flow information.
plot_rain_flow(id = NULL, rain = NULL, flow = NULL, area = NULL, title = "")
plot_rain_flow(id = NULL, rain = NULL, flow = NULL, area = NULL, title = "")
id |
Station identification number |
rain |
Rainfall time series, measured in mm/month |
flow |
Flow time series, measured in m3/s |
area |
Catchment area in Km2 |
title |
(optional) Plot title |
Plot rainfall and flow for a given station
## Not run: plot_rain_flow(id = 54090) ## End(Not run)
## Not run: plot_rain_flow(id = 54090) ## End(Not run)
This function plots a previously calculated trend.
plot_trend(df, column_name, maptype = "stamen_toner_lite", showmap = TRUE)
plot_trend(df, column_name, maptype = "stamen_toner_lite", showmap = TRUE)
df |
Data frame containing at least 4 column:
lat (latitude), lon (longitude), slope and an additional user-defined column
|
column_name |
name of the column to use for grouping the results. |
maptype |
maptype, was need to choose the stamenmap type, now useless since stamenmap are no longer reachable |
showmap |
set to FALSE to avoid plotting the map when running the function |
The function relies on the 'ggmap' package for the map, and this package has in time gone through many changes due to changes in API of map providers. Currently to be able to create the map one needs to register to the stadiamaps service. More information at ?ggmap::register_stadiamaps().
Two plots, the first showing the distribution of the
trend over a map, based on the slope of the linear model that describes the
trend. The second plot shows a boxplot of the slope grouped based on the
column column_name
and slope can be user-defined
(notice that in the plot the very extreme slope values are not
displayed to avoid skewed visualisations).
## Not run: # some fake data around London df <- data.frame(lat = 51.5+runif(40,-0.3,0.3), lon = 0+runif(40, -0.3,0.3), slope = rnorm(40, c(rep(-0.4,20),rep(0.4,20))), g = factor(c(rep("a",20), rep("b",20)))) theplots <- plot_trend(df, "g", maptype = "terrain-background") theplots$A # map theplots$B + labs(subtitle = "Use ggplot usual commands to modify the plots") # boxplots ## End(Not run)
## Not run: # some fake data around London df <- data.frame(lat = 51.5+runif(40,-0.3,0.3), lon = 0+runif(40, -0.3,0.3), slope = rnorm(40, c(rep(-0.4,20),rep(0.4,20))), g = factor(c(rep("a",20), rep("b",20)))) theplots <- plot_trend(df, "g", maptype = "terrain-background") theplots$A # map theplots$B + labs(subtitle = "Use ggplot usual commands to modify the plots") # boxplots ## End(Not run)
Not to be used by user. Internal function used to print error in nrfaapi.
## S3 method for class 'nrfa_api' print(x, ...)
## S3 method for class 'nrfa_api' print(x, ...)
x |
an nrfa call |
... |
additional items, not really used |
This calculates the seasonal averages from a time series.
seasonal_averages(timeseries, season = "Spring")
seasonal_averages(timeseries, season = "Spring")
timeseries |
Time series (zoo class). |
season |
Name of the season, which corresponds to a quarter: Winter (Q1), Spring (Q2), Summer (Q3), Autumn (Q4) |
A vector containing the seasonal average and significance level (p-value) for each time series.
## Not run: seasonal_averages(timeseries = cmr(18019), season = "Spring") seasonal_averages(list(cmr(18019), cmr(18019)), season = "Spring") ## End(Not run)
## Not run: seasonal_averages(timeseries = cmr(18019), season = "Spring") seasonal_averages(list(cmr(18019), cmr(18019)), season = "Spring") ## End(Not run)
This function pulls the list of station identification numbers.
station_ids()
station_ids()
vector integer identification numbers (one for each station)
Claudia Vitolo
## Not run: # Retrieve all the stations ids x <- station_ids() ## End(Not run)
## Not run: # Retrieve all the stations ids x <- station_ids() ## End(Not run)