data-raw/creates_basin_datasets.R

# Creates basin-specific NoRWeST data frames for R package
#
# defines functions for downloading data, and formatting
# applies functions to create data frames
# saves created data frames in data/

library(usethis)
library(utils)
library(foreign)

# site information--------------------------------------------------------------

# function to read in site data by basin
get_norwestSites <-function(basin) {

  temp_sites <-tempfile()

  site_url <-ifelse(basin == "Clearwater", "https://www.fs.fed.us/rm/boise/AWAE/projects/NorWeST/downloads/ObservedStreamTemperatureMaps/170603_Clearwater/NorWeST_ObservedTempPoints_Clearwater.zip",
                    ifelse(basin == "Salmon", "https://www.fs.fed.us/rm/boise/AWAE/projects/NorWeST/downloads/ObservedStreamTemperatureMaps/170602_Salmon/NorWeST_ObservedTempPoints_Salmon.zip",
                           ifelse(basin == "SpoKoot", "https://www.fs.fed.us/rm/boise/AWAE/projects/NorWeST/downloads/ObservedStreamTemperatureMaps/170101_02_03_SpoKoot/NorWeST_ObservedTempPoints_Spokoot.zip",
                                  ifelse(basin == "MidSnake", "https://www.fs.fed.us/rm/boise/AWAE/projects/NorWeST/downloads/ObservedStreamTemperatureMaps/170501_02_MidSnake/NorWeST_ObservedTempPoints_MiddleSnake.zip",
                                         ifelse(basin == "Snake-Bear", "https://www.fs.fed.us/rm/boise/AWAE/projects/NorWeST/downloads/ObservedStreamTemperatureMaps/160101_02_170401_02_SnakeBear/NorWeST_ObservedTempPoints_SnakeBear.zip",
                                                NA)))))
  download.file(site_url, temp_sites)

  filelist <-utils::unzip(temp_sites)

  dbfname <-filelist[grepl(".dbf", filelist)]

  dbf <-foreign::read.dbf(dbfname, as.is = TRUE)

}


# alldays daily summaries-------------------------------------------------------

# function to get data by basin
get_norwestDaily <-function(basin) {
  temp_alldays <-tempfile()

  # basin-specific urls for alldays excel files
  basin_url <-ifelse(basin == "Clearwater", "https://www.fs.fed.us/rm/boise/AWAE/projects/NorWeST/downloads/ObservedStreamTemperatureMaps/170603_Clearwater/NorWeST_ObservedStreamTempDailySummaries_Clearwater_AllDays.zip",
                     ifelse(basin == "Salmon", "https://www.fs.fed.us/rm/boise/AWAE/projects/NorWeST/downloads/ObservedStreamTemperatureMaps/170602_Salmon/NorWeST_ObservedStreamTempDailySummaries_SalmonRiverBasin_AllDays.zip",
                            ifelse(basin == "SpoKoot", "https://www.fs.fed.us/rm/boise/AWAE/projects/NorWeST/downloads/ObservedStreamTemperatureMaps/170101_02_03_SpoKoot/NorWeST_ObservedStreamTempDailySummaries_SpoKoot_AllDays.zip",
                                   ifelse(basin == "MidSnake", "https://www.fs.fed.us/rm/boise/AWAE/projects/NorWeST/downloads/ObservedStreamTemperatureMaps/170501_02_MidSnake/NorWeST_ObservedStreamTempDailySummaries_MidSnake_AllDays.zip",
                                          ifelse(basin == "Snake-Bear", "https://www.fs.fed.us/rm/boise/AWAE/projects/NorWeST/downloads/ObservedStreamTemperatureMaps/160101_02_170401_02_SnakeBear/NorWeST_ObservedStreamTempDailySummaries_SnakeBear_AllDays.zip",
                                                 NA)))))

  # basin-specific filenames for alldays excel files
  filename <-ifelse(basin == "Clearwater", "NorWeST_ObservedStreamTempDailySummaries_Clearwater_AllDays.xlsx",
                    ifelse(basin == "Salmon", "NorWeST_ObservedStreamTempDailySummaries_SalmonRiverBasin_AllDays.xlsx",
                           ifelse(basin == "SpoKoot", "NorWeST_ObservedStreamTempDailySummaries_SpoKoot_AllDays.xlsx",
                                  ifelse(basin == "MidSnake", "NorWeST_ObservedStreamTempDailySummaries_MidSnake_AllDays.xlsx",
                                         ifelse(basin == "Snake-Bear", "NorWeST_ObservedStreamTempDailySummaries_SnakeBear_AllDays.xlsx",
                                                NA)))))

  # basin-specific sheet names for alldays excel files
  sheetname <-ifelse(basin == "Clearwater", "qryNorWeST_ObservedStreamTempDa",
                     ifelse(basin == "Salmon", "SalmonRiverUnit_DailyMetrics",
                            ifelse(basin == "SpoKoot", "SpoKoot_DailyMetrics",
                                   ifelse(basin == "MidSnake", "MidSnake_DailyMetrics",
                                          ifelse(basin == "Snake-Bear", "SnakeBear_DailyMetrics",
                                                 NA)))))

  # dowlnoad alldays data as data frame 'daily'
  download.file(basin_url, temp_alldays)
  utils::unzip(temp_alldays)
  daily <-readxl::read_excel(filename, sheet = sheetname)

}


# function to combine site and all days data into one data frame----------------
get_norwest <-function(basin) {

  tempdata <-get_norwestDaily(basin)

  sitedata <-get_norwestSites(basin)

  formatted <-merge(tempdata, sitedata, by = "OBSPRED_ID", all.x = TRUE)
}

# apply function to create data frame for each basin---------------------------

Clearwater <-get_norwest("Clearwater")
Salmon <-get_norwest("Salmon")
SpoKoot <-get_norwest("SpoKoot")
MidSnake <-get_norwest("MidSnake")
SnakeBear <-get_norwest("Snake-Bear")

# save data to R package folder data/
usethis::use_data(Clearwater)
usethis::use_data(Salmon)
usethis::use_data(SpoKoot)
usethis::use_data(MidSnake)
usethis::use_data(SnakeBear)
jjwill2/idtemp documentation built on Aug. 10, 2020, 12:44 a.m.