R/ExportMonthlyValuesToCSV.R

Defines functions ExportMonthlyValuesToCSV

Documented in ExportMonthlyValuesToCSV

#' ExportMonthlyValuesToCSV
#'
#' This function exports monthly values generated by the readNWISstat
#' function in the dataRetrieval package.
#'
#' This function outputs to the working directory.  The directory can be set with 
#' the setwd command.
#' 
#' This function only supports discharge data.
#'
#' @param monthlyValues daily values output by the importDVs in the waterData package
#' @param fileName file name to save CSV file to
#' @param  metric if TRUE the output is in metric units
#' @keywords USGS
#' @examples
#'
#' site = list(
#'    staid = "02131000",                     # site number
#'    code = "00060",                         # code for discharge 
#'    stat = "00003",                         # code for mean   
#' )
#' 
#' MonthlyValues <- readNWISstat(StationInfo$siteID,
#'                               parameterCd = StationInfo$site$code,
#'                               statReportType = "monthly") 
#' 
#' ExportDailyMonthlyToCSV(MonthlyValues, 
#'                        paste0("Monthly_Values_for_Site_", site$staid))
#'
#' @export

ExportMonthlyValuesToCSV <- function(monthlyValues, 
                                     fileName,
                                     metric = TRUE){
  
  # reorder columns & drop irrelevant columns
  monthlyValues <- monthlyValues[c("year_nu", "month_nu", "mean_va")]
  
  if(all(monthlyValues$parameter_cd == "00060")){
    dataType = "mean discharge"
    if (metric){
       monthlyValues$mean_va <- monthlyValues$mean_va * 0.0283168  # convert to m^3/s
    }
  }
  
  # rename columns
  colnames(monthlyValues)[colnames(monthlyValues)=="mean_va"] <- dataType
  
  write.csv(monthlyValues, file = paste0(fileName, ".csv"))
  
  
}
Agriculturist/waterDataSupport documentation built on Aug. 12, 2018, 9:05 a.m.