rhlee12 / RNRCS

Public repository for the RNRCS package, which pulls data from NRCS for use in R.
5 stars 1 forks source link

Including Snow Course data #10

Open davechristianson opened 4 years ago

davechristianson commented 4 years ago

Great package and would be nice if data from "SNOW" snow course networks could be included. As snow course sites only have WTEQ and SNWD values and collection dates, perhaps bypassing a call to grabNRCS.elements could be used for sites in this network? In the meantime, here is a work-around that retrieves both SNTL snotel data and SNOW course data from a list generate by grabNRCS.meta and it might be helpful to others:

# David Christianson
# University of Wyoming
# 31 March 2020
# Extract NRCS SNOTEL and SNOW COURSE data simultaneously
# First access current list of all manual NRCS Snow course sites and SNOTEL sites
# RNRCS can access the metadata for SNOTEL and SNOW COURSE sites, but does not
# currently (as of 31 March 2020) access the data from Snow Course sites

# use RNRCS::grabNRCS.meta function to get the metadata for the sites of interest
# this metadata is used to access the individual site records
# see help on this function for more details on how to subset stations
  library(RNRCS)
  allsites<-do.call(rbind,grabNRCS.meta(ntwrks=c("SNTL","SNOW")))

# now retrive the site records from the NRCS server, one site at a time (slow, depending on connection).
# the RNRCS::grabNRCS.data function is used for SNOTEL records but a direct call to the server
# is used for SNOW COURSE DATA 

  # The NRCS web service url is required as an object for accessing the SNOW COurse data 
    baseURL <- "https://wcc.sc.egov.usda.gov/reportGenerator/view_csv/customSingleStationReport/"
  # SNOW COURSE Stations only provide Snow Water equivalent and snow depth as data elements we
  # and we will specifiy these here so they can be called each time in the url for Snow Couse sites.
    eCodeString<-paste0("WTEQ::collectionDate",",WTEQ::value",",","SNWD::value")

  # create an empty list to hold data from each site
  # when the loop below is finished, the 'snowlist' will be populated with one data.frame for each site in allsites
  # in the same order that they are listed in all sites.
    snowlist<-list()

  # loop through each site and get data from NRCS web service
    for (i in 1:nrow(allsites)){
      print(paste0(i," ",allsites$site_id[i])) #show progress tracker
      if(allsites$ntwk[i]=="SNTL"){
        snowlist[[i]]<-grabNRCS.data(network=allsites$ntwk[i], # uses the RNRCS function to get the data, one site at a time
                                     site_id=unlist(strsplit(allsites$site_id[i],":"))[2],
                                     timescale="monthly",
                                     DayBgn = ymd(paste0(allsites$start[i],"-01")),
                                     DayEnd = ymd(Sys.Date())
        )
      }
      else { # Snow Course sites cannot be retrieved by RCNRS, so a seperate chunk of code is used here that access the data directly
        snowlist[[i]]<-utils::read.csv(paste0(baseURL, "monthly", "/", unlist(strsplit(allsites$site_id[i],":"))[2], 
                                              ":", allsites$state[i], ":", allsites$ntwk[i], "|id=\"\"|name/", 
                                              ymd(paste0(allsites$start[i],"-01")), ",", ymd(Sys.Date()), "/", eCodeString),
                                       comment.char = "#", 
                                       quote = "")
      }      
    }
sagesteppe commented 3 years ago

Great Dave, thanks so much for this! Was just about to work through it myself. One item to mention here is that function 'ymd' is imported from 'lubridate'.