Package 'pRecipe'

Title: Precipitation R Recipes
Description: An open-access tool/framework to download, validate, visualize, and analyze multi-source precipitation data. More information and an example of implementation can be found in Vargas Godoy and Markonis (2023, <doi:10.1016/j.envsoft.2023.105711>).
Authors: Mijael Rodrigo Vargas Godoy [aut, cre] , Yannis Markonis [aut, ths]
Maintainer: Mijael Rodrigo Vargas Godoy <[email protected]>
License: GPL-3
Version: 3.0.2
Built: 2024-11-22 05:08:42 UTC
Source: https://github.com/mirovago/precipe

Help Index


Crop precipitation data sets

Description

The function crop_data crops the data sets using a shapefile mask.

Usage

crop_data(x, y)

## S4 method for signature 'Raster'
crop_data(x, y)

## S4 method for signature 'data.table'
crop_data(x, y)

## S4 method for signature 'character'
crop_data(x, y)

Arguments

x

Raster* object; data.table (see details); filename (character; see details)

y

filename (character). Path to a *.shp file

Details

If 'x' is a data.table, its columns should be named: "lon", "lat", "date", and "value"

If 'x' is a filename, it should point to a *.nc file.

Value

Raster* object; data.table

Examples

## Not run: 
download_data("gldas-vic", tempdir(), timestep = "yearly")
r <- raster::brick(paste0(tempdir(),
"/gldas-vic_tp_mm_land_194801_201412_025_yearly.nc"))
s <- crop_data(r, "cze.shp")

## End(Not run)

Probability of Detection

Description

Function for calculating the critical success index.

Usage

csi(x, ref, th)

Arguments

x

a data.table generated by fldmean

ref

a data.table with data used for evaluation

th

numeric. The value for detection threshold

Value

numeric


Download various precipitation data products

Description

The function download_data downloads the selected data product.

Usage

download_data(
  dataset = "all",
  path = ".",
  domain = "raw",
  timestep = "monthly"
)

Arguments

dataset

a character string with the name(s) of the desired data set. Suitable options are:

  • "all" for all of the below listed data sets (default),

  • "20cr" for 20CR v3,

  • "chirps" for CHIRPS v2.0,

  • "cmap" for CMAP standard version,

  • "cmorph" for CMORPH,

  • "cpc" for CPC-Global,

  • "cru-ts" for CRU_TS v4.06,

  • "em-earth" for EM-EARTH,

  • "era20c" for ERA-20C,

  • "era5" for ERA5,

  • "fldas" for FLDAS,

  • "ghcn" for GHCN-M v2,

  • "gldas-clsm" for GLDAS CLSM,

  • "gldas-noah" for GLDAS NOAH,

  • "gldas-vic" for GLDAS VIC,

  • "gpcc" for GPCC v2020,

  • "gpcp" for GPCP v2.3,

  • "gpm_imerg" for GPM IMERGM Final v06,

  • "jra55" for JRA-55,

  • "merra2" for MERRA-2,

  • "mswep" for MSWEP v2.8,

  • "ncep-doe" for NCEP/DOE,

  • "ncep-ncar" for NCEP/NCAR,

  • "persiann" for PERSIANN-CDR,

  • "precl" for PREC/L,

  • "terraclimate" for TerraClimate,

  • "trmm-3b43" for TRMM 3B43 v7,

  • "udel" for UDEL v501.

path

a character string with the path where the database will be downloaded.

domain

a character string with the desired domain data set. Suitable options are:

  • "raw" for default available spatial coverage,

  • "global" for data sets with global (land and ocean) coverage,

  • "land" for data sets with land only coverage,

  • "ocean", for data sets with ocean only coverage.

timestep

a character string with the desired time resolution. Suitable options are:

  • "monthly",

  • "yearly".

Value

No return value, called to download the required data sets.

Examples

download_data("gldas-vic", tempdir(), timestep = "yearly")

E-OBS data downloader

Description

Function for downloading E-OBS.

Usage

download_e_obs(folder_path = ".", time_res = "monthly")

Arguments

folder_path

a character string with the path where the data will be downloaded.

time_res

a character string with the desired time resolution. Suitable options are:

  • "monthly",

  • "yearly".

Value

No return value, called to download the data set.


False Alarm Rate

Description

Function for calculating the false alarm rate.

Usage

far(x, ref, th)

Arguments

x

a data.table generated by fldmean

ref

a data.table with data used for evaluation

th

numeric. The value for detection threshold

Value

numeric


Field mean

Description

The function fldmean computes the spatial weighted average for each timestep.

Usage

fldmean(x)

## S4 method for signature 'Raster'
fldmean(x)

## S4 method for signature 'data.table'
fldmean(x)

## S4 method for signature 'character'
fldmean(x)

Arguments

x

Raster* object; data.table (see details); filename (character, see details)

Details

If 'x' is a data.table, its columns should be named: "lon", "lat", "date", and "value"

If 'x' is a filename, it should point to a *.nc file.

Value

data.table

Examples

## Not run: 
download_data("gldas-vic", tempdir(), timestep = "yearly")
r <- raster::brick(paste0(tempdir(),
"/gldas-vic_tp_mm_land_194801_201412_025_yearly.nc"))
s <- fldmean(r)

## End(Not run)

Show data content

Description

The function infoNC displays the specification of the desired file.

Usage

infoNC(x)

## S4 method for signature 'Raster'
infoNC(x)

## S4 method for signature 'character'
infoNC(x)

Arguments

x

Raster* Object; character


Labeling

Description

The function label adds data set name and source type.

Usage

label(x, y)

Arguments

x

data.table (see details)

y

character (see details)

Details

columns in 'x' should be named (if present): "lon", "lat", "date", and "value"

Available options are:

  • "20cr" for 20CR v3,

  • "chirps" for CHIRPS v2.0,

  • "cmap" for CMAP standard version,

  • "cmorph" for CMORPH,

  • "cpc" for CPC-Global,

  • "cru-ts" for CRU_TS v4.06,

  • "em-earth" for EM-EARTH,

  • "era20c" for ERA-20C,

  • "era5" for ERA5,

  • "fldas" for FLDAS,

  • "ghcn" for GHCN-M v2,

  • "gldas-clsm" for GLDAS CLSM,

  • "gldas-noah" for GLDAS NOAH,

  • "gldas-vic" for GLDAS VIC,

  • "gleam" for GLEAM v3.7a,

  • "gpcc" for GPCC v2020,

  • "gpcp" for GPCP v2.3,

  • "gpm_imerg" for GPM IMERGM Final v06,

  • "jra55" for JRA-55,

  • "merra2" for MERRA-2,

  • "mswep" for MSWEP v2.8,

  • "ncep-doe" for NCEP/DOE,

  • "ncep-ncar" for NCEP/NCAR,

  • "persiann" for PERSIANN-CDR,

  • "precl" for PREC/L,

  • "terraclimate" for TerraClimate,

  • "trmm-3b43" for TRMM 3B43 v7,

  • "udel" for UDEL v501.

Value

data.table

Examples

## Not run: 
r <- data.table::data.table("date" = as.Date("2000-01-01"), "value" = 42)
s <- label(r, "mswep")

## End(Not run)

Multiply by days per month

Description

The function muldpm multiplies the value by days per month.

Usage

muldpm(x)

## S4 method for signature 'Raster'
muldpm(x)

## S4 method for signature 'data.table'
muldpm(x)

## S4 method for signature 'character'
muldpm(x)

Arguments

x

Raster* object; data.table (see details); filename (character, see details)

Details

'x' object with monthly data in [units/day]

If 'x' is a data.table, its columns should be named: "lon", "lat", "date", and "value"

If 'x' is a filename, it should point to a *.nc file.

Value

Raster* object; data.table

Examples

## Not run: 
tavg_brick <- raster::brick('terraclimate_tavg.nc')
pet_od <- pet(method = "od", tavg = tavg_brick)
pet_od <- muldpm(pet_od)

## End(Not run)

Nash–Sutcliffe Efficiency

Description

Function for calculating the Nash–Sutcliffe efficiency.

Usage

nse(x, ref)

Arguments

x

a data.table generated by fldmean

ref

a data.table with data used for evaluation

Value

numeric


Boxplot ggplot

Description

Convenient and aesthetic visualization of data in a boxplot.

Usage

plot_box(x, var = "Precipitation", unit = "mm")

## S4 method for signature 'Raster'
plot_box(x, var = "Precipitation", unit = "mm")

## S4 method for signature 'data.table'
plot_box(x, var = "Precipitation", unit = "mm")

## S4 method for signature 'character'
plot_box(x, var = "Precipitation", unit = "mm")

Arguments

x

Raster* object; data.table (see details); filename (character, see details)

var

character (see details)

unit

character (see details)

Details

If 'x' is a data.table, its columns should be named: "lon", "lat", "date", and "value"

If 'x' is a filename, it should point to a *.nc file.

'var' is a character string describing the variable to be used for the plot title

'unit' is a character string describing the unit of measurement to be used for the plot title

Value

ggplot object

Examples

## Not run: 
download_data("gldas-vic", tempdir(), timestep = "yearly")
r <- raster::brick(paste0(tempdir(),
"/gldas-vic_tp_mm_land_194801_201412_025_yearly.nc"))
s <- plot_box(r)

## End(Not run)

Histogram ggplot

Description

Convenient and aesthetic visualization of data in a histogram.

Usage

plot_density(x, var = "Precipitation", unit = "mm")

## S4 method for signature 'Raster'
plot_density(x, var = "Precipitation", unit = "mm")

## S4 method for signature 'data.table'
plot_density(x, var = "Precipitation", unit = "mm")

## S4 method for signature 'character'
plot_density(x, var = "Precipitation", unit = "mm")

Arguments

x

Raster* object; data.table (see details); filename (character, see details)

var

character (see details)

unit

character (see details)

Details

If 'x' is a data.table, its columns should be named: "lon", "lat", "date", and "value"

If 'x' is a filename, it should point to a *.nc file.

'var' is a character string describing the variable to be used for the axis title

'unit' is a character string describing the unit of measurement to be used for the axis title

Value

ggplot object

Examples

## Not run: 
download_data("gldas-vic", tempdir(), timestep = "yearly")
r <- raster::brick(paste0(tempdir(),
"/gldas-vic_tp_mm_land_194801_201412_025_yearly.nc"))
s <- plot_density(r)

## End(Not run)

Heatmap ggplot

Description

Convenient and aesthetic visualization of data in a heatmap.

Usage

plot_heatmap(x, unit = "mm")

## S4 method for signature 'Raster'
plot_heatmap(x, unit = "mm")

## S4 method for signature 'data.table'
plot_heatmap(x, unit = "mm")

## S4 method for signature 'character'
plot_heatmap(x, unit = "mm")

Arguments

x

Raster* object; data.table (see details); filename (character, see details)

unit

character (see details)

Details

If 'x' is a data.table, its columns should be named: "lon", "lat", "date", and "value"

If 'x' is a filename, it should point to a *.nc file.

'unit' is a character string describing the unit of measurement to be used for the axis title

Value

ggplot object

Examples

## Not run: 
download_data("gldas-vic", tempdir(), timestep = "yearly")
r <- raster::brick(paste0(tempdir(),
"/gldas-vic_tp_mm_land_194801_201412_025_yearly.nc"))
s <- plot_heatmap(r)

## End(Not run)

Line ggplot

Description

Convenient and aesthetic visualization of data in a line plot.

Usage

plot_line(x, var = "Precipitation", unit = "mm")

## S4 method for signature 'Raster'
plot_line(x, var = "Precipitation", unit = "mm")

## S4 method for signature 'data.table'
plot_line(x, var = "Precipitation", unit = "mm")

## S4 method for signature 'character'
plot_line(x, var = "Precipitation", unit = "mm")

Arguments

x

Raster* object; data.table (see details); filename (character, see details)

var

character (see details)

unit

character (see details)

Details

If 'x' is a data.table, its columns should be named: "lon", "lat", "date", and "value"

If 'x' is a filename, it should point to a *.nc file.

'var' is a character string describing the variable to be used for the axis title

'unit' is a character string describing the unit of measurement to be used for the axis title

Value

ggplot object

Examples

## Not run: 
download_data("gldas-vic", tempdir(), timestep = "yearly")
r <- raster::brick(paste0(tempdir(),
"/gldas-vic_tp_mm_land_194801_201412_025_yearly.nc"))
s <- plot_line(r)

## End(Not run)

Map ggplot

Description

Convenient and aesthetic visualization of data in a map

Usage

plot_map(x, layer = 1, unit = "mm", timestamp = TRUE)

## S4 method for signature 'Raster'
plot_map(x, layer = 1, unit = "mm", timestamp = TRUE)

## S4 method for signature 'data.table'
plot_map(x, layer = 1, unit = "mm", timestamp = TRUE)

## S4 method for signature 'character'
plot_map(x, layer = 1, unit = "mm", timestamp = TRUE)

Arguments

x

Raster* object; data.table (see details); filename (character, see details)

layer

numeric

unit

character

timestamp

logical

Details

If 'x' is a data.table, its columns should be named: "lon", "lat", "date", and "value"

If 'x' is a filename, it should point to a *.nc file.

'unit' is a character string describing the unit of measurement to be used for the legend title

'layer' is the layer number to be plotted.

‘timestamp' if TRUE (default) the plot title is the layer’s date

Value

ggplot object


Summary ggplot

Description

Convenient and aesthetic visualization of data in a summary plot.

Usage

plot_summary(x, var = "Precipitation", unit = "mm")

## S4 method for signature 'Raster'
plot_summary(x, var = "Precipitation", unit = "mm")

## S4 method for signature 'data.table'
plot_summary(x, var = "Precipitation", unit = "mm")

## S4 method for signature 'character'
plot_summary(x, var = "Precipitation", unit = "mm")

Arguments

x

Raster* object; data.table (see details); filename (character, see details)

var

character (see details)

unit

character (see details)

Details

If 'x' is a data.table, its columns should be named: "lon", "lat", "date", and "value"

If 'x' is a filename, it should point to a *.nc file.

'var' is a character string describing the variable to be used for the axis title

'unit' is a character string describing the unit of measurement to be used for the axis title

Value

ggplot object

Examples

## Not run: 
download_data("gldas-vic", tempdir(), timestep = "yearly")
r <- raster::brick(paste0(tempdir(),
"/gldas-vic_tp_mm_land_194801_201412_025_yearly.nc"))
s <- plot_summary(r)

## End(Not run)

Taylor diagram

Description

Convenient and aesthetic visualization of data in a Taylor diagram.

Usage

plot_taylor(x, y, groups = "source", ...)

Arguments

x

data.table

y

data.table

groups

character

...

see details

Details

'x' columns should be named: "lon", "lat", "date", "value", "dataset", and "source". The last two columns are added using the label.

'y' columns should be named: "lon", "lat", "date", "value", "dataset", and "source". The last two columns are added using the label.

'groups' character to define panels. Suitable options are:

  • "source" (default)

  • "seasons" (only works properly with monthly data)

'...' extra arguments passed on to openair::TaylorDiagram

Value

plot object


Probability Of Detection

Description

Function for calculating the probability of detection.

Usage

pod(x, ref, th)

Arguments

x

a data.table generated by fldmean

ref

a data.table with data used for evaluation

th

numeric. The value for detection threshold

Value

numeric


Masks data

Description

Function for various masks.

Usage

pRecipe_masks()

Value

data.table


Spatial aggregation

Description

The function remap aggregates data into a new grid resolution.

Usage

remap(x, y)

## S4 method for signature 'Raster'
remap(x, y)

## S4 method for signature 'data.table'
remap(x, y)

## S4 method for signature 'character'
remap(x, y)

Arguments

x

Raster* object; data.table (see details); filename (character, see details)

y

numeric

Details

If 'x' is a data.table, its columns should be named: "lon", "lat", "date", and "value"

If 'x' is a filename, it should point to a *.nc file.

Value

Raster* object; data.table

Examples

## Not run: 
download_data("gldas-vic", tempdir(), timestep = "yearly")
r <- raster::brick(paste0(tempdir(),
"/gldas-vic_tp_mm_land_194801_201412_025_yearly.nc"))
s <- remap(r, 1)

## End(Not run)

Save .nc file

Description

Function to save data compatible with pRecipe in .nc file

Usage

saveNC(x, file, name = "tp", longname = "Total precipitation", units = "mm")

Arguments

x

Raster* object

file

character

name

character

longname

character

units

character

Value

No return value, called to save a file

Examples

## Not run: 
save_nc(dummie_brick, "gpcp_tp_mm_global_197901_202205_025_monthly.nc")

## End(Not run)

Subset data in space and time

Description

The function subset_data subsets the data in space within a bounding box, and/or in time within a year range.

Usage

subset_data(x, box = NULL, yrs = NULL)

## S4 method for signature 'Raster'
subset_data(x, box = NULL, yrs = NULL)

## S4 method for signature 'data.table'
subset_data(x, box = NULL, yrs = NULL)

## S4 method for signature 'character'
subset_data(x, box = NULL, yrs = NULL)

Arguments

x

Raster* object; data.table (see details); filename (character, see details)

box

numeric. Bounding box in the form: (xmin, xmax, ymin, ymax)

yrs

numeric. Time range in the form: (start_year, end_year)

Details

If 'x' is a data.table, its columns should be named: "lon", "lat", "date", and "value"

If 'x' is a filename, it should point to a *.nc file.

If subsetting only in space or time then the arguments must be passed by name. I.e., subset_data(x, box = ...) (space) or subset_data(x, yrs = ...) (time)

Value

Raster* object; data.table

Examples

## Not run: 
download_data("gldas-vic", tempdir(), timestep = "yearly")
r <- raster::brick(paste0(tempdir(),
"/gldas-vic_tp_mm_land_194801_201412_025_yearly.nc"))
sd <- subset_data(r, c(12.24, 18.85, 48.56, 51.12), c(2000, 2010))
ss <- subset_data(r, box = c(12.24, 18.85, 48.56, 51.12))
st <- subset_data(r, yrs = c(2000, 2010))

## End(Not run)

Transform raster into data.table

Description

Function to transform a raster brick into a data.table

Usage

tabular(x)

## S4 method for signature 'Raster'
tabular(x)

## S4 method for signature 'character'
tabular(x)

Arguments

x

Raster* object; filename (character, see details)

Value

data.table

Examples

## Not run: 
download_data("gldas-vic", tempdir(), timestep = "yearly")
r <- raster::brick(paste0(tempdir(),
"/gldas-vic_tp_mm_land_194801_201412_025_yearly.nc"))
s <- tabular(r)

## End(Not run)

Trends

Description

The function trend computes linear slope.

Usage

trend(x)

## S4 method for signature 'Raster'
trend(x)

## S4 method for signature 'data.table'
trend(x)

## S4 method for signature 'character'
trend(x)

Arguments

x

Raster* object; data.table (see details); filename (character, see details)

Details

If 'x' is a data.table, its columns should be named: "lon", "lat", "date", and "value"

If 'x' is a filename, it should point to a *.nc file.

Value

Raster* object; data.table


Yearly <stat>

Description

The function yearstat aggregates the data from monthly to yearly.

Usage

yearstat(x, stat = "sum")

## S4 method for signature 'Raster'
yearstat(x, stat = "sum")

## S4 method for signature 'data.table'
yearstat(x, stat = "sum")

## S4 method for signature 'character'
yearstat(x, stat = "sum")

Arguments

x

Raster* object; data.table (see details); filename (character, see details)

stat

character

Details

If 'x' is a data.table, its columns should be named: "lon", "lat", "date", and "value"

If 'x' is a filename, it should point to a *.nc file.

'stat' is a character string describing the desired aggregation function. Suitable options are:

  • "max"

  • "mean"

  • "median"

  • "min"

  • "sum" (default)

Value

Raster* object; data.table

Examples

## Not run: 
download_data("gldas-vic", path = tempdir())
r <- raster::brick(paste0(tempdir(),
"/gldas-vic_tp_mm_land_194801_201412_025_monthly.nc"))
s <- yearstat(r, "mean")

## End(Not run)