Skip to content

Commit

Permalink
Merge pull request #48 from sbegueria/devel
Browse files Browse the repository at this point in the history
Version 1.8.0
  • Loading branch information
sbegueria authored Nov 23, 2022
2 parents d213970 + 397afba commit 18f388f
Show file tree
Hide file tree
Showing 44 changed files with 422 additions and 333 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
^.*\.Rproj$
^\.Rproj\.user$
^data-raw$
^_config\.yml$
^cran-comments\.md$
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Package: SPEI
Type: Package
Version: 1.8
Date: 2022-05-05
Version: 1.8.0
Title: Calculation of the Standardised Precipitation-Evapotranspiration Index
Authors@R: c(
person('Santiago', 'Beguería', role=c('aut','cre'),
Expand All @@ -12,8 +11,9 @@ Description: A set of functions for computing potential evapotranspiration and s
Depends: R (>= 3.5.0)
Imports: lmomco, lmom, TLMoments, reshape, ggplot2, checkmate, zoo, lubridate
License: GPL-2
URL: http://spei.csic.es
LazyLoad: yes
URL: http://spei.csic.es, https://github.com/sbegueria/SPEI
BugReports: https://github.com/sbegueria/SPEI/issues
LazyLoad: true
Encoding: UTF-8
Suggests: covr, testthat
RoxygenNote: 7.1.1
RoxygenNote: 7.2.0
10 changes: 8 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ importFrom(graphics,par)
importFrom(graphics,plot)
importFrom(graphics,points)
importFrom(graphics,polygon)
importFrom(lmom,cdfgam)
importFrom(lmom,cdfglo)
importFrom(lmom,cdfpe3)
importFrom(lmom,pelgam)
importFrom(lmom,pelglo)
importFrom(lmom,pelpe3)
Expand All @@ -40,12 +38,20 @@ importFrom(lmomco,pwm2lmom)
importFrom(lubridate,days_in_month)
importFrom(lubridate,yday)
importFrom(reshape,melt)
importFrom(stats,aggregate)
importFrom(stats,cycle)
importFrom(stats,end)
importFrom(stats,frequency)
importFrom(stats,is.ts)
importFrom(stats,optim)
importFrom(stats,pnorm)
importFrom(stats,qnorm)
importFrom(stats,sd)
importFrom(stats,start)
importFrom(stats,time)
importFrom(stats,ts)
importFrom(stats,window)
importFrom(zoo,as.Date.yearmon)
importFrom(zoo,as.yearmon)
importFrom(zoo,na.trim)
importFrom(zoo,rollapply)
16 changes: 8 additions & 8 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

__Version history:

_Version 1.8, May 2022 (current development version on github).
_### _Version 1.8.0, November 2022 (current version on GitHub, submitted to CRAN).

1. Solving several minor bugs in `<thornthwaite>`, `<hargreaves>`, and `<penman>` functions (output difference less than 0.1%).
2. Solving a bug in `<spei>` that resulted in bad cumulative data when using a non-rectangular kernel, resulting in incorrect SPEI values.
Expand All @@ -16,16 +16,16 @@ _Version 1.8, May 2022 (current development version on github).
6. Implementation of different versions of the Penman-Monteith ETo calculation in function `<penman>`.
7. Implementation of an option to include CO2 concentration data in function `<penman>`.
8. Implementation of a new option for when no wind data are available in function `<penman>`.
9. Funtion `<plot.spei>` completely rewritten based on `ggplot2`, solving some bugs and enabling more flexibility.
9. Function `<plot.spei>` completely rewritten based on `ggplot2`, solving some bugs and enabling more flexibility.

_Version 1.7.2, June 2019 (current stable version on github).
_Version 1.7.2, June 2019 (only on GitHub).

1. Allowing for data with frequency other than 12 in `<spei>` function.

_Version 1.7.1, June 2017.

1. Corrected an error in `<spei>` function, which was not working when distribution was Gamma or PeasonIII and using user provided parameters. (Fixed by Emanuele Cordano, [email protected] -- ecor)
2. Added probability of monthly precipitation = 0 (pze) when using user provided parameters. (Fixed by Emanuele Cordano, [email protected] -- ecor)
1. Corrected an error in `<spei>` function, which was not working when distribution was Gamma or PeasonIII and using user provided parameters. (Fixed by Emanuele Cordano, [email protected] -- 'ecor')
2. Added probability of monthly precipitation = 0 (pze) when using user provided parameters. (Fixed by Emanuele Cordano, [email protected] -- 'ecor')

_Version 1.7, June 2017 (current on CRAN).

Expand All @@ -45,7 +45,7 @@ _Version 1.5, May 2013.

_Version 1.4, May 2013.
1. Minor fixes to functions \code{\link{penman}} and \code{\link{pwm}}.
2. Documentation of the penman function defined by mistake ed as the saturation vapour pressure, while it should read 'actual vapour pressure'.
2. Documentation of the penman function defined by mistake ed as the saturation vapor pressure, while it should read 'actual vapor pressure'.
3. Function zzz.R added to display basic information about the SPEI package at startup.
4. Function \code{\link{SPEINews}} added to display the NEWS file.

Expand All @@ -56,7 +56,7 @@ _Version 1.3, March 2013.

_Version 1.2, October 2012.
1. Fixed a bug causing several functions to fail when a time series not belonging to matrix class was provided.
2. Function \code{\link{plot.spei}} now distinguises between calls to spei and spi and labels the axis accordingly.
2. Function \code{\link{plot.spei}} now distinguishes between calls to \code{\link{spei}} and \code{\link{spi}} and labels the axis accordingly.

_Version 1.1, March 2012.
1. Functions \code{\link{spei}} and \code{\link{spi}} now yield an object of class "spei".
Expand All @@ -69,6 +69,6 @@ First release of the SPEI package.
__To do (work in progress):

1. Complete documentation for pwmLC.Rd.
2. Review method plot.spei() that produces wrong results in some cases.
2. Review method \code{\link{plot.speispei}} that produces wrong results in some cases.
3. Implement parallel processing.
4. Analysis functions.
7 changes: 6 additions & 1 deletion R/SPEI-package.r
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@
#'
#' @importFrom checkmate makeAssertCollection
#' @importFrom stats cycle end frequency start ts optim
#' @importFrom zoo as.yearmon rollapply
#' @importFrom zoo as.yearmon as.Date.yearmon rollapply na.trim
#' @importFrom stats aggregate is.ts pnorm qnorm sd time window
#' @importFrom TLMoments PWM
#' @importFrom lmomco are.lmom.valid are.parglo.valid cdfgam cdfpe3 cdfgam pargam parglo parpe3 pwm.pp pwm2lmom
#' @importFrom lmom pelgam pelglo pelpe3 cdfglo

NULL
74 changes: 12 additions & 62 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#' @name Datasets
#'
#'
#' @aliases balance cabinda cruts4
#'
#'
#' @aliases wichita balance cabinda cruts4
#' @title Data sets for illustrating the functions in the SPEI package.
#'
#'
#' @keywords datasets
#' @description
#' Data used in the examples of the SPEI package:
#' \code{wichita} dataset: monthly climate in Wichita (Kansas, lat=37.6475,
Expand All @@ -18,19 +13,7 @@
#' Allen et al. (1998);
#' \code{cruts4}: 120 years of monthly climatic water balance (precipitation
#' minus reference evapotranspiration) data at six grid points from CRU TS 4.05.
#'
#'
#'
#'
#' @details See description.
#'
#'
#' @usage
#' data(wichita)
#' data(balance)
#' data(cabinda)
#'
#'
#' @format
#' \code{wichita} dataset: a data frame with:
#' \describe{
Expand All @@ -44,13 +27,11 @@
#' \item{ACSH}{ monthly mean sun hours, in h.}
#' \item{ACSH}{ monthly mean cloud cover, in \%.}
#'}
#'
#' \code{balance} dataset: a data frame with monthly climatic water balance
#' (precipitation minus potential evapotranspiration) at Indore (India),
#' Kimberley (South Africa), Albuquerque (US), Valencia (Spain), Wien (Austria),
#' Abashiri (Japan), Tampa (US), Sao Paulo (Brasil), Lahore (India), Punta
#' Abashiri (Japan), Tampa (US), Sao Paulo (Brazil), Lahore (India), Punta
#' Arenas (Chile) and Helsinki (Finland), in mm.
#'
#' \code{cabinda} dataset: a data frame with one year of monthly climatic data
#' at Cabinda (Angola, -5.33S 12.11E 20 m), with:
#'
Expand All @@ -61,7 +42,7 @@
#' \item{RH}{ monthly mean relative humidity, in \%.}
#' \item{U2}{ monthly mean wind speed, in km h-1}
#' \item{tsun}{ monthly mean sunshine hours, in h.}
#' \item{Rs}{ monthly mean dialy incoming solar radiation, MJ m-2 d-1.}
#' \item{Rs}{ monthly mean diaily incoming solar radiation, MJ m-2 d-1.}
#' \item{ET0}{ monthly ET0 from the original publication, in mm.}
#'}
#'
Expand Down Expand Up @@ -101,50 +82,19 @@
#' summary(wichita)
#' data(balance)
#' summary(balance)
#' data(cruts4)
#' summary(cruts4)
#'
#'
#'
"wichita"
NULL

#' @rdname wichita
"wichita"

#'
#' @title cabinda
#'
#'
#' @rdname Datasets
#'
#'
#' @description See wichita
#'
#'
#' @details See wichita
#'
#' @rdname cabinda
"cabinda"


#'
#' @title balance
#'
#'
#' @rdname Datasets
#'
#'
#' @description See wichita
#'
#'
#' @details See wichita
#'
#' @rdname balance
"balance"

#'
#' @title cruts4
#'
#'
#' @rdname Datasets
#'
#'
#' @description See wichita
#'
#'
#' @details See wichita
#' @rdname cruts4
"cruts4"
31 changes: 13 additions & 18 deletions R/hargreaves.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
#' @name Potential-evapotranspiration
#'
#'
#' @title Computation of potential and reference evapotranspiration.
#'
#'
#' @aliases thornthwaite penman
#'
#'
#' @usage
#' thornthwaite(Tave, lat, na.rm = FALSE, verbose=TRUE)
#'
#' hargreaves(Tmin, Tmax, Ra = NA, lat = NA, Pre = NA, na.rm = FALSE, verbose=TRUE)
#' hargreaves(Tmin, Tmax, Ra = NULL, lat = NULL, Pre = NULL, na.rm = FALSE, verbose=TRUE)
#'
#' penman(Tmin, Tmax, U2, Ra = NA, lat = NA, Rs = NA, tsun = NA,
#' CC = NA, ed = NA, Tdew = NA, RH = NA, P = NA, P0 = NA,
#' z = NA, crop='short', na.rm = FALSE, method='ICID',
#' verbose=TRUE)
#' penman(Tmin, Tmax, U2, Ra = NULL, lat = NULL, Rs = NULL, tsun = NULL,
#' CC = NULL, ed = NULL, Tdew = NULL, RH = NULL, P = NULL, P0 = NULL,
#' CO2 = NULL, z = NULL, crop='short', na.rm = FALSE, method='ICID',
#' verbose=TRUE)
#'
#'
#' @description
Expand All @@ -35,10 +29,10 @@
#' @param Ra optional, a numeric vector, tsvector, matrix, tsmatrix, or 3-d array of monthly mean daily external radiation, MJ m-2 d-1.
#' @param Pre optional, a numeric vector, tsvector, matrix, tsmatrix, or 3-d array of monthly total precipitation, mm.
#' @param U2 a numeric vector, tsvector, matrix, tsmatrix, or 3-d array of monthly mean daily wind speeds at 2 m height, m s-1.
#' @param Rs optional, a numeric vector, tsvector, matrix, tsmatrix, or 3-d array of monthly mean dialy incoming solar radiation, MJ m-2 d-1.
#' @param Rs optional, a numeric vector, tsvector, matrix, tsmatrix, or 3-d array of monthly mean daily incoming solar radiation, MJ m-2 d-1.
#' @param tsun optional, a numeric vector, tsvector, matrix, tsmatrix, or 3-d array of monthly mean daily bright sunshine hours, h.
#' @param CC optional, numeric a vector, matrix or time series of monthly mean cloud cover, \%.
#' @param ed optional, numeric a vector, matrix or time series of monthly mean actual vapour pressure at 2 m height, kPa.
#' @param ed optional, numeric a vector, matrix or time series of monthly mean actual vapor pressure at 2 m height, kPa.
#' @param Tdew optional, a numeric vector, tsvector, matrix, tsmatrix, or 3-d array of monthly mean daily dewpoint temperature (used for estimating ed), ºC.
#' @param RH optional, a numeric vector, tsvector, matrix, tsmatrix, or 3-d array of monthly mean relative humidity (used for estimating ed), \%.
#' @param P optional, a numeric vector, tsvector, matrix, tsmatrix, or 3-d array of monthly mean atmospheric pressure at surface, kPa.
Expand Down Expand Up @@ -109,7 +103,7 @@
#'
#' @references
#' Thornthwaite, C. W., 1948. An approach toward a rational classification of climate.
#' \emph{Geographical Review} \bold{38}: 55–94. doi:10.2307/2107309.
#' \emph{Geographical Review} \bold{38}: 55–94. DOI:10.2307/2107309.
#'
#' Hargreaves G.H., 1994. Defining and using reference evapotranspiration.
#' \emph{Journal of Irrigation and Drainage Engineering} \bold{120}: 1132–1139.
Expand All @@ -131,7 +125,7 @@
#' Reston, VA, 57 pp.
#'
#' Yang, Y., Roderick, M.L., Zhang, S. McVicar, T., Donohue, R.J., 2019. Hydrologic implications of vegetation
#' response to elevated CO2 in climate projections. \emph{Nature Clim Change} \bold{9}: 44–48.
#' response to elevated CO2 in climate projections. \emph{Nature Climate Change} \bold{9}: 44–48.
#'
#'
#' @author Santiago Beguería
Expand Down Expand Up @@ -308,6 +302,7 @@ hargreaves <- function(Tmin, Tmax, Ra=NULL, lat=NULL, Pre=NULL,
# 3D array input (gridded data)
int_dims <- tmin_dims
} else {
int_dims <- tmin_dims
check$push('Input data can not have more than three dimensions.')
}
n_sites <- prod(int_dims[[2]], int_dims[[3]])
Expand Down Expand Up @@ -341,7 +336,7 @@ hargreaves <- function(Tmin, Tmax, Ra=NULL, lat=NULL, Pre=NULL,
}
ym <- as.yearmon(time(Tmin))
warn$push(paste0('Time series spanning ', ym[1], ' to ', ym[n_times], '.'))
date <- as.Date(ym)
date <- as.Date.yearmon(ym)
mlen_array <- array(as.numeric(lubridate::days_in_month(date)), dim=int_dims)
msum_array <- array(yday(date) + round((mlen_array/2) - 1), dim=int_dims)
} else {
Expand All @@ -355,7 +350,7 @@ hargreaves <- function(Tmin, Tmax, Ra=NULL, lat=NULL, Pre=NULL,
# Verify the length of each input variable
input_len <- prod(int_dims)
if (sum(lengths(Tmin))!=input_len || sum(lengths(Tmax))!=input_len) {
check$push('`Tmin` and `Tmax` should not have different lengths.')
check$push('`Tmin` and `Tmax` cannot have different lengths.')
}
if (using$Ra && sum(lengths(Ra))!=input_len) {
check$push('`Ra` has incorrect length.')
Expand Down Expand Up @@ -462,4 +457,4 @@ hargreaves <- function(Tmin, Tmax, Ra=NULL, lat=NULL, Pre=NULL,
dimnames(ET0) <- names

return(ET0)
}
}
16 changes: 3 additions & 13 deletions R/kern.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
#' @name Kernel-functions
#'
#'
#' @title Time kernel for computing the SPEI at different time scales.
#'
#'
#' @aliases kern.plot
#'
#'
#' @description Function \code{kern} is used internally by \code{\link{spei}} and
#' \code{\link{spi}}for computing drought indices at different time scales.
#'
#'
#' @param scale numeric, time scale or length of the kernel.
#' @param type character, shape of the kernel function.
#' @param shift numeric, shifting of the kernel peak.
#'
#'
#' @details
#' Drought indices, such as the SPEI or the SPI, are usually
#' computed at different time scales to adapt to the different response
#' times of systems affected by drought. This is acomplished by applying
#' times of systems affected by drought. This is accomplished by applying
#' a kernel function to the data prior to computation of the SPEI.
#' Application of a kernel has the effect of smoothing the temporal
#' variability of the resulting SPEI, allowing for the major patterns
Expand Down Expand Up @@ -50,7 +40,7 @@
#' time lag for the four different kernel shapes so they can be compared.
#'
#'
#' @return A vector of lenght equal to \code{scale} with weights used for computing the drought index.
#' @return A vector of length equal to \code{scale} with weights used for computing the drought index.
#'
#'
#' @references
Expand Down Expand Up @@ -96,7 +86,7 @@ kern <- function(scale, type='rectangular', shift=0) {

if(s == 1) type == "rectangular"

k = switch(type,
k <- switch(type,
rectangular = rep(1,s),
triangular = s:1,
circular = (s^2+(1-(1:s)^2)),
Expand Down
Loading

1 comment on commit 18f388f

@sbegueria
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly syntax and spelling changes.

Please sign in to comment.