Skip to content

Instantly share code, notes, and snippets.

@McCartneyAC
Created April 23, 2020 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McCartneyAC/f43f9f3f38a618656d4bbac09a09c64e to your computer and use it in GitHub Desktop.
Save McCartneyAC/f43f9f3f38a618656d4bbac09a09c64e to your computer and use it in GitHub Desktop.
pulling down PDSI data from treering NADA (https://www.ncdc.noaa.gov/paleo-search/) and cleaning for mapping
# tree ring data
library(mccrr)
library(lubridate)
library(tidyverse)
library(jsonlite)
library(tibble)
library(ggplot2)
library(dplyr)
library(tidyr)
pdsi <- readr::read_csv("C:\\Users\\Andrew\\Desktop\\Statistics and Data Analysis\\climate_data\\NADAv2a.csv")
metadata <- jsonlite::fromJSON(txt = "C:\\Users\\Andrew\\Desktop\\Statistics and Data Analysis\\climate_data\\metadata\\noaa-recon-6319.json", simplifyVector = F)
pdsi_L <- pdsi %>%
pivot_longer(-year,
names_to = "station",
values_to = "pdsi",
values_drop_na = TRUE)
meta_raw <- enframe(unlist(metadata))
meta_clean <- meta_raw %>%
# takes unlisted data, removes the several thousand variables we don't need,
# creates a variable to be widened later
# then recodes them by row number (dangerous! but they *are* in order)
# to join them to the original pdsi data set.
filter(stringr::str_detect(name, 'coordinates')) %>%
mutate(latlong = case_when(
grepl("1", name, fixed=TRUE) ~ "longitude",
grepl("2", name, fixed=TRUE) ~ "latitude"
)) %>%
group_by(name) %>%
mutate(station = row_number()) %>%
ungroup() %>%
select(-name) %>%
pivot_wider(names_from = latlong, values_from = value)
trees <- pdsi_L %>%
mutate(station = as.numeric(station)) %>%
left_join(meta_clean, by = "station")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment