diff --git a/indicator_processing/automated_download/sst.R b/indicator_processing/automated_download/sst.R index 153a01d..facffa9 100644 --- a/indicator_processing/automated_download/sst.R +++ b/indicator_processing/automated_download/sst.R @@ -1,4 +1,3 @@ - # fix for dealing with internet timeout error # https://stackoverflow.com/questions/68666187/internetopenurl-failed-a-connection-with-the-server-could-not-be-established @@ -12,9 +11,6 @@ library(rerddap) load("indicator_processing/spec_file.RData") -# devtools::install_github("mdsumner/ncdf4") -# library(ncdf4) - # define years -------------------------------- styear <- 1982 enyear <- terminal_year @@ -23,8 +19,6 @@ enyear <- terminal_year sst <- info('ncdcOisst21Agg_LonPM180') # this may work better # empty data ------------------------------------------------- -# dat <- data.frame(row.names = c("year", "mon", "PR_mean", "PR_min", "PR_max", "VI_mean", "VI_min", "VI_max")) - dat <- setNames(data.frame(matrix(NA,length(styear:enyear)*12,5)), c("year", "mon", 'mean', 'min', 'max')) m <- 1 @@ -46,35 +40,10 @@ for (yr in styear:enyear) { n <- n + 12 dat[m:n,] <- data.frame(sst_agg[,1:2], unlist(sst_agg[,3])) m <- n + 1 - -# # url from ERDDAP for OISST, download and read ---------------- -# url <- paste0("https://coastwatch.pfeg.noaa.gov/erddap/griddap/ncdcOisst21Agg.csv?sst[(", yr, "-01-01T12:00:00Z):1:(", yr, "-12-31T12:00:00Z)][(0.0):1:(0.0)][(17):1:(19)][(292):1:(296)]") -# -# # url_vi <- paste0("https://coastwatch.pfeg.noaa.gov/erddap/griddap/ncdcOisst21Agg.csv?sst[(", yr, "-01-01T12:00:00Z):1:(", yr, "-12-31T12:00:00Z)][(0.0):1:(0.0)][(17):1:(19)][(294.75):1:(296)]") -# # url_pr <- paste0("https://coastwatch.pfeg.noaa.gov/erddap/griddap/ncdcOisst21Agg.csv?sst[(", yr, "-01-01T12:00:00Z):1:(", yr, "-12-31T12:00:00Z)][(0.0):1:(0.0)][(17):1:(19)][(292):1:(294.75)]") -# # download for entire Caribbean because USVI and PR highly correlated. -# download.file(url = url, destfile = "st.csv") -# -# labs <- read.table("st.csv", sep = ",", header = T, skip = 0) -# sst_vi <- read.table("st.csv", sep = ",", header = T, skip = 1) -# names(sst_vi) <- names(labs) -# -# # extract month ----------------------------------------------- -# sst_vi$mon <- strftime(sst_vi$time, format = "%m") -# -# # calculate mean, min, max and concatenate -------------------- -# ind_vi <- as.numeric(tapply(sst_vi$sst, sst_vi$mon, mean, na.rm = T)) -# min_vi <- as.numeric(tapply(sst_vi$sst, sst_vi$mon, min, na.rm = T)) -# max_vi <- as.numeric(tapply(sst_vi$sst, sst_vi$mon, max, na.rm = T)) -# -# tempdat <- data.frame(yr, unique(sst_vi$mon), ind_vi, min_vi, max_vi, stringsAsFactors = F) -# dat <- data.frame(rbind(dat, tempdat), stringsAsFactors = F) -} -# file.remove("st.csv") +} -# add row names and yearmonth column -------------------------- -# names(dat) <- c("year", "mon", "mean", "min", "max") +# add yearmonth column -------------------------- dat$yrmon <- paste0(dat$mon, "-", dat$year) dat head(dat) @@ -83,7 +52,7 @@ tail(dat) # check outputs and look at correlations --------------------- table(dat$year) table(dat$mon) -matplot(dat[3:5], type = "l") #, col = c(4, 4, 4, 2, 2, 2), lty = c(1, 2, 3, 1, 2, 3)) +matplot(dat[3:5], type = "l") cor(dat[3:5]) plot(dat[3:5]) @@ -91,13 +60,12 @@ plot(dat[3:5]) labs <- c(rep("U.S. Caribbean sea surface temperature", 3), rep("degrees Celsius", 3), "monthly mean", "monthly minimum", "monthly maximum") - # "Puerto Rico - mean", "USVI - mean", "Puerto Rico - monthly minimum", "USVI - monthly minimum", "Puerto Rico - monthly maximum", "USVI - monthly maximum") indnames <- data.frame(matrix(labs, nrow = 3, byrow = T)) inddata <- data.frame(dat[c(3:5)]) datdata <- dat$yrmon -s <- list(labels = indnames, indicators = inddata, datelist = datdata) #, ulim = ulidata, llim = llidata) +s <- list(labels = indnames, indicators = inddata, datelist = datdata) class(s) <- "indicatordata" s