Working with Cal-Adapt Climate Data in R:
Intro to Climate Data and Constructing API Requests

About Me…

Thanks also to …


Outcomes

1) Get familiar with caladaptR

2) Hands-on practice with:

a) importing Cal-Adapt into R via the API
b) best practices for working with climate data
c) data wrangling for visualization or analysis
d) working with climate data as rasters

3) Working Code Recipes

a) R Notebooks
b) Climate Data Cookbook:

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


Interpolated Observed Climate Data


Spatial Extent

Spatial extent of LOCA downscaled climate data layers:


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

Climate Data

Cal-Adapt Data

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

Convert statements about climate into actionable info:

The rolling average of maximum daily temperature will increase by X

Species A, B, & C are most likely to survive in the projected climate envelope.

Custom visualizations

Integrate results with other data

  • census data
  • biodiversity / habitat
  • economic data

Take advantage of other R packages

Make your own custom models

Make a decision support tool

caladaptR

Key Features

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


Main Uses


Prerequisites

caladaptR users need to know:


Learning caladaptR

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

caladaptR workflow

In general, there are five steps to using caladaptR:

flowchart tab1 1) Determine your location(s) of interest. You can use your own points or polygons, or one of the preset areas-of-interest tab2 2) Create an API Request object tab1->tab2 tab3 3) Feed the API Request into a function that fetches data tab2->tab3 tab4 4) Wrangle the results into the format you require (e.g., filtering, sorting, joining, reshaping, add calculated columns, etc.) tab3->tab4 tab5 5) Continue on with your analysis or visualization tab4->tab5

API Request Objects

Building an API Request

A complete example:

my_cap <- ca_loc_pt(coords = c(-121.4687, 38.5938)) %>%
  ca_cvar(c("tasmax", "tasmin")) %>%                   
  ca_gcm(c("HadGEM2-ES", "CNRM-CM5", "CanESM2", "MIROC5")) %>%
  ca_scenario(scenarios[1:2]) %>%
  ca_period("year") %>%
  ca_years(start = 2040, end = 2060)


An API Request object consists of between 2 and 4 components:


1. Location (required, pick one)

ca_loc_aoipreset() Query a preset location(s)
ca_loc_pt() Question point location(s)
ca_loc_sf() Query simple feature location(s)


2. Dataset

Option 1 for downscaled modeled climate data from Scripps (including VIC)
Include all 4 of the following constructor functions:

ca_cvar() Select the climate variable(s) (i.e., precip, temperature)
ca_gcm() Pick or more of the 10 Global Climate Models
ca_period() Select temporal aggregation period (year, month, day)
ca_scenario() Choose your emission scenario(s)


Option 2 for Livneh datasets
Include all 3 of the following constructor functions:

ca_cvar() Select the climate variable(s) (i.e., precip, temperature)
ca_livneh TRUE
ca_period() Select temporal aggregation period (year, month, day)


Option 3 for any raster series
Specify any dataset by its ‘slug’:

ca_slug() Select a dataset by its slug

To find a slug, see Searching the Cal-Adapt Data Catalog.


3. Start & end dates (optional, pick one)

ca_years() Specify start & end by year
ca_dates() Specify start & end by date


4. Options (required for polygons)

ca_options() Spatial aggregation function(s)

Quick Example

  1. Load the package:
library(caladaptr)
  1. Create an API request object:
sdzoo_cap <- ca_loc_pt(coords = c(-117.0, 33.1)) %>%  ## 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
sdzoo_cap
## Cal-Adapt API Request
## Location(s): 
##   x: -117
##   y: 33.1
## 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
## 
sdzoo_cap %>% ca_preflight()
## General issues
##  - none found
## Issues for querying values
##  - none found
## Issues for downloading rasters
##  - none found
plot(sdzoo_cap, locagrid = TRUE)
  1. Fetch data:
sdzoo_tbl <- sdzoo_cap %>% ca_getvals_tbl(quiet = TRUE)
  1. View the results
sdzoo_tbl %>% slice(1:10)

Notebook Time!

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


Tips

Every time you hit saves, it generates a HTML file in the background.

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

Common error:
Error creating notebook: path for html_dependency. Path not found: /tmp/RtmpjR1sPw

  • Means RStudio is having trouble rendering the HTML file in the background.
  • Can generally ignore until you’re done working.
  • To clear: Knit button → Clear Knitr Cache. Then save.

Notebook 1. Getting Started

In Notebook 1, you will:

Getting Help

  • Chat window
  • Share screen
  • Breakout rooms

Notebook 1. Getting Started | solutions

END PART I!