-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoi.R
More file actions
36 lines (30 loc) · 1.09 KB
/
soi.R
File metadata and controls
36 lines (30 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
library(tidyverse)
library(httr)
library(rvest)
library(tsibble)
library(jsonlite)
library(feasts)
library(fabletools)
read_soi <- function() {
path <- "https://www.cpc.ncep.noaa.gov/data/indices/soi"
download.file(path, destfile="temp.data")
colnames <- read_fwf("temp.data",
col_positions=fwf_empty("temp.data", skip=3, n = 13),
skip=3, n_max=1) %>%
gather()
read_fwf("temp.data",
col_positions=fwf_empty("temp.data", skip=3, n = 13, col_names=colnames$value),
skip=4, n_max=80,
na = c("99.9")) %>%
pivot_longer(cols=-1, names_to="month", values_to="Anomaly") %>%
mutate(Date= yearmonth(as.Date(paste(YEAR, month, 1, sep="-"), "%Y-%b-%d"))) %>%
select(-YEAR, -month) %>%
drop_na(Anomaly) %>%
mutate(Month=month(Date),
Year=year(Date)) }
data <- read_soi() %>%
as_tsibble() %>%
model(STL(Anomaly ~ trend() + season(period=12))) %>%
components()
write_json(data %>% mutate(Date=as.Date(Date)), "data/soi.json", force=TRUE)
write_csv(data %>% mutate(Date=as.Date(Date)), "data/soi.csv")