Created
April 23, 2020 21:04
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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