Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add weekly precipitation data #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' @name Datasets
#' @aliases wichita balance cabinda cruts4
#' @aliases wichita balance cabinda cruts4 campinas
#' @title Data sets for illustrating the functions in the SPEI package.
#' @keywords datasets
#' @description
Expand All @@ -12,7 +12,9 @@
#' \code{cabinda}: one year of data for computing Penman-Monteith ET0 from
#' 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.
#' minus reference evapotranspiration) data at six grid points from CRU TS 4.05.
#' \code{campinas} dataset: weekly precipitation for Campinas (SP, Brazil,
#' lat = -23, lon -47) from 1981 to Nov 2022. Data extreacted fom CHIRPS dataset.
#' @details See description.
#' @format
#' \code{wichita} dataset: a data frame with:
Expand Down Expand Up @@ -53,6 +55,19 @@
#' are (0.25, 0.75), and latitudes (42.25, 42.75, 43.25), corresponding to the
#' Central Pyrenees between Spain and France.
#'
#' \code{campinas} dataset: weekly precipitation for Campinas (SP, Brazil,
#' lat = -23, lon -47) from 1981 to Nov 2022.
#' Data aggregated using \code{lubridate::week} rules, so every year has 53 weeks.
#' Must be converted to a TS object before extractin SPI.
#' Daily precipitation obtained from CHIRPS v2.0 dataset on 2022-12-19.
#' URL: https://www.chc.ucsb.edu/data/chirps
#' see \code{data-raw/campinas_daily2weekly.R} for daily data processing steps
#'
#' \describe{
#' \item{year_week}{ year and week, followin ISO 8601 definitions}
#' \item{date}{ first day of the year-week period, date}
#' \item{prcp}{ accumulated precipitation for the year-week period, mm}
#'}
#'
#' @references
#' S.M. Vicente-Serrano, S. Beguería, J.I. López-Moreno. 2010. A Multi-scalar
Expand All @@ -72,8 +87,10 @@
#' Data for the \code{balance} dataset were taken from Allen et al. (1998), page 69, figure 18.
#' The \code{cruts4} data were obtained from the CRU (Climatic Research Unit, University
#' of East Anglia \url{https://crudata.uea.ac.uk/cru/data/hrg/cru_ts_4.05/}) TS V4.05 data set.
#' Data for the \code{campinas} dataset were aggregated from daily
#' CHIRPS v2.0 precipitation - https://www.chc.ucsb.edu/data/chirps
#'
#' @author Data ported to R by S. Beguería.
#' @author Data ported to R by S. Beguería. \code{campinas} data prepared by Daniel C. Victoria
#'
#'
#' @examples
Expand All @@ -97,4 +114,11 @@ NULL
"balance"

#' @rdname cruts4
"cruts4"
"cruts4"

#' @rdname campinas
#' @example
#' data(campinas)
#' campinas_ts <- ts(campinas$prcp, frequency = 53, start = c(1981, 1))
#' spi(campinas_ts, scale = 12)
"campinas"
27 changes: 27 additions & 0 deletions data-raw/campinas_daily2weekly.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Convert Campinas, SP, Brazil daily precip to weekly
# Daily dataset extracted from CHIRPS v2.0, for coordinates lon -47, lat -23
# Daniel de Castro Victoria - 2022-12-19
# [email protected]

cmp <- jsonlite::fromJSON('data-raw/daily_precip_campinas_chirps.json')
names(cmp) <- c('date', 'prcp')
cmp$date <- as.Date(cmp$date)

# using lubridate::wek in order to allways have 53 weeks per year
# My tests indicated that a TS object with a fractional frequency
# will not work with SPEI
cmp$year_week <- sprintf('%s-%02i', lubridate::year(cmp$date),
lubridate::week(cmp$date))

campinas <- aggregate(prcp ~ year_week,
data = cmp,
FUN = sum)

first_day <- aggregate(date ~ year_week,
data = cmp,
FUN = min)

campinas <- merge(campinas, first_day)
campinas <- campinas[c('year_week', 'date', 'prcp')]

save(campinas, file = 'data/campinas.rda')
Loading