Working with Climate Data in R with caladaptR

October 1, 2021
Andy Lyons

About Me…

Thanks also to …


About You

Goals

1) Get familiar with caladaptR

2) Hands-on practice with:

  1. importing Cal-Adapt into R via the API
  2. data wrangling techniques to prep data for visualization or analysis
  3. techniques for dealing with large volumes of data

3) More caladaptR users!

  1. use-cases for analyses and visualization
  2. ideas for code recipes, features, and documentation


Learning Strategy


+ foundational concepts
+ code recipes
+ working examples
+ practice

functional pRoficiency!

Cal-Adapt

Cal-Adapt is California’s official portal for peer-reviewed climate data.

Datasets are selected with guidance and priorities from California State agencies.

Modeled Climate Data:

See also: What climate data does Cal-Adapt provide?.

Spatial extent of LOCA downscaled climate data layers:


Cal-Adapt Data

Options for Accessing Cal-Adapt Data

Feature Cal-Adapt website Cal-Adapt FTP caladapt-py caladaptR
Download rasters
Statewide
User area-of-interest
10 recommended GCMs
All 32 GCMs
Query features
Points (user-provided)
Lines (user-provided)
Polygons (user-provided)
Polygons (presets, boundary layers)
Other
Extract underlying tables from preset charts

More info:

Why you might want to work with Cal-Adapt data in R

Custom Visualization

Integrate results with other data

Take advantage of other R packages

Run custom models

Research

Custom decision support

caladaptR

Key Features

  • pipe friendly functions
  • accepts and returns standard data classes (e.g., tibble, sf, stars)
  • units encoded in the results
  • follows development guidelines from rOpenSci


Main Uses


Prerequisites

caladaptR users need to know:

caladaptR workflow

In general, there are five steps to using caladaptR:

Quick Example

  1. Load the package:
library(caladaptr)
  1. Create an API request object:
cap1 <- ca_loc_pt(coords = c(-121.4687, 38.5938)) %>%  ## specify a location
  ca_cvar(c("tasmax", "tasmin")) %>%                   ## climate variables
  ca_gcm(gcms[1:4]) %>%                                ## GCM(s)
  ca_scenario(scenarios[1:2]) %>%                      ## emission scenarios(s)
  ca_period("year") %>%                                ## temporal aggregation period
  ca_years(start = 2040, end = 2060)                   ## start and end dates
  1. Check API request
cap1
## Cal-Adapt API Request
## Location(s): 
##   x: -121.469
##   y: 38.594
## Variable(s): tasmax, tasmin
## Temporal aggregration period(s): year
## GCM(s): HadGEM2-ES, CNRM-CM5, CanESM2, MIROC5
## Scenario(s): rcp45, rcp85
## Dates: 2040-01-01 to 2060-12-31
## 
cap1 %>% ca_preflight()
## General issues
##  - none found
## Issues for querying values
##  - none found
## Issues for downloading rasters
##  - none found
plot(cap1, locagrid = TRUE)
  1. Fetch data:
cap1_tbl <- ca_getvals_tbl(cap1, quiet = TRUE)
  1. View the results
cap1_tbl %>% slice(1:10)

Large Queries

cap1_rtbl <- cap1 %>% ca_getvals_db(db_fn = “c:/data/mydata.sqlite”, db_tbl = “temp_min_max”)

See also Large Queries vignette

Rasters

cap1_tifs <- cap1 %>% ca_getrst_stars(out_dir = “c:/data/tifs”)

See also Raster Vignettes

Shiny Apps

→ browser based GUIs

Samples:

## Install caladaptr.apps and all dependent packages
remotes::install_github("ucanr-igis/caladaptr.apps")
library(caladaptr.apps)

## Launch the time series app
ca_launch("timeseries")

## Launch the projected chill portions app
ca_launch("chill")

https://ucanr-igis.github.io/caladapt-py/


Resources

https://ucanr-igis.github.io/caladaptr/


Works in progress…


Help wanted!

R Notebooks

R Notebooks are written in “R Markdown”, which combines text and R code.

Remember:

When you’re in a R Notebook, the working directory is where the Rmd file resides.