R/usgs_month_dl.R

Defines functions usgs_month_dl

Documented in usgs_month_dl

#' Download monthly data from USGS
#'
#' The current dataRetrieval package does not allow for download of monthly stats.  This function downloads this file for read-in later.
#'
#' @param site_id USGS site id
#' @param param_cv USGS parameter id
#' @param destination_folder location to save file
#'
#' @return dl_result a character stating "Success" or "Failure"
#'
#'
#' @export

usgs_month_dl <- function(site_id, parameterCd = "00060", destination_folder = getwd()) {
	### Create url for the data location at the USGS website
	### Based on http://waterservices.usgs.gov/rest/Statistics-Service-Test-Tool.html
	url <- paste0("http://waterservices.usgs.gov/nwis/stat/?format=rdb,1.0&sites=", site_id,"&statReportType=monthly&statTypeCd=mean&missingData=off&parameterCd=",param_cd)

	### Download file with a catch for failures
	dl_location <- file.path(destination_folder, paste0(site_id,"_",param_cd,"_mon.txt"))
	dl_result <- try(download.file(url, destfile = dl_location, method="curl"), silent = TRUE)
	
	### If dl_result works (equals zero) and the first character is a hash, return result.  If not, delete mistake download
	if (dl_result == 0 & readChar(dl_location, 1)=="#") {
			dl_result <- "Success"
		} else {
			### Delete file	and return failure
			file.remove(dl_location)
			dl_result <- "Failure"
		}
	return(dl_result)
	}
	


	
jstagge/staggefuncs documentation built on May 20, 2019, 2:11 a.m.