From e0d687fa0063e8c523dbab3aaabd79167abb34d4 Mon Sep 17 00:00:00 2001
From: unknown <ldecicco@usgs.gov>
Date: Fri, 26 Sep 2014 16:54:15 -0500
Subject: [PATCH] Adding getUserInfo function.

---
 NAMESPACE                    |   1 +
 R/getMetaData.r              | 105 +++++++++++++++++++++++++++++++++--
 R/getSampleDataFromFile.r    |   5 +-
 inst/extdata/infoTest.csv    |   2 +
 man/getNWISInfo.Rd           |   1 -
 man/getSampleDataFromFile.Rd |   2 -
 man/getUserInfo.Rd           |  41 ++++++++++++++
 man/getUserSample.Rd         |   5 +-
 man/getWQPInfo.Rd            |   5 +-
 9 files changed, 146 insertions(+), 21 deletions(-)
 create mode 100644 inst/extdata/infoTest.csv
 create mode 100644 man/getUserInfo.Rd

diff --git a/NAMESPACE b/NAMESPACE
index fce5c3d0..d246fb10 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -30,6 +30,7 @@ export(getSampleData)
 export(getSampleDataFromFile)
 export(getSiteFileData)
 export(getUserDaily)
+export(getUserInfo)
 export(getUserSample)
 export(getWQPData)
 export(getWQPInfo)
diff --git a/R/getMetaData.r b/R/getMetaData.r
index ff47453d..4a01959a 100644
--- a/R/getMetaData.r
+++ b/R/getMetaData.r
@@ -52,7 +52,7 @@ getMetaData <- function(siteNumber="", parameterCd="",interactive=TRUE){
 #' @param siteNumber string USGS site number.  This is usually an 8 digit number
 #' @param parameterCd string USGS parameter code.  This is usually an 5 digit number.
 #' @param interactive logical Option for interactive mode.  If true, there is user interaction for error handling and data checks.
-#' @keywords data import USGS web service WRTDS
+#' @keywords data import USGS web service
 #' @export
 #' @return INFO dataframe with at least param.nm, param.units, parameShortName, paramNumber
 #' @examples
@@ -85,10 +85,7 @@ getNWISInfo <- function(siteNumber, parameterCd,interactive=TRUE){
 
 #' Import Metadata for Water Quality Portal Data
 #'
-#' Populates INFO data frame for EGRET study.  If either station number or parameter code supplied, imports data about a particular USGS site from NWIS web service. 
-#' This function gets the data from here: \url{http://waterservices.usgs.gov/}
-#' A list of parameter codes can be found here: \url{http://nwis.waterdata.usgs.gov/nwis/pmcodes/}
-#' If either station number or parameter code is not supplied, the user will be asked to input data.
+#' Populates INFO data frame for EGRET study.  
 #' Additionally, the user will be asked for:
 #' staAbbrev - station abbreviation, will be used in naming output files and for structuring batch jobs
 #' constitAbbrev - constitute abbreviation
@@ -178,7 +175,96 @@ getWQPInfo <- function(siteNumber, parameterCd, interactive=FALSE){
   
   if(interactive){
     if(is.na(siteInfo$drainSqKm)){
-      cat("No drainage area was listed in the USGS site file for this site.\n")
+      cat("No drainage area was listed in the WQP site file for this site.\n")
+      cat("Please enter the drainage area, you can enter it in the units of your choice.\nEnter the area, then enter drainage area code, \n1 is square miles, \n2 is square kilometers, \n3 is acres, \n4 is hectares.\n")
+      cat("Area(no quotes):\n")
+      siteInfo$drain.area.va <- readline()
+      siteInfo$drain.area.va <- as.numeric(siteInfo$drain.area.va)
+      cat("Unit Code (1-4, no quotes):")
+      qUnit <- readline()
+      qUnit <- as.numeric(qUnit)
+      conversionVector <- c(2.5899881, 1.0, 0.0040468564, 0.01)
+      siteInfo$drainSqKm <- siteInfo$drain.area.va * conversionVector[qUnit]
+    }
+  }
+  
+  siteInfo$queryTime <- Sys.time()
+  siteInfo$paStart <- 10
+  siteInfo$paLong <- 12
+  
+  return(siteInfo)
+}
+
+
+
+#' Import Metadata from User-Generated File
+#'
+#' Populates INFO data frame for EGRET study.
+#' Additionally, the user will be asked for:
+#' staAbbrev - station abbreviation, will be used in naming output files and for structuring batch jobs
+#' constitAbbrev - constitute abbreviation
+#'
+#' @param filePath string specifying the path to the file
+#' @param fileName string name of file to open
+#' @param hasHeader logical true if the first row of data is the column headers
+#' @param separator string character that separates data cells
+#' @param interactive logical Option for interactive mode.  If true, there is user interaction for error handling and data checks.
+#' @keywords data import USGS web service WRTDS
+#' @export
+#' @return INFO dataframe with agency, site, dateTime, value, and code columns
+#' @examples
+#' filePath <- system.file("extdata", package="dataRetrieval")
+#' filePath <- paste(filePath,"/",sep="")
+#' fileName <- 'infoTest.csv'
+#' INFO <- getUserInfo(filePath,fileName, separator=",",interactive=FALSE)
+getUserInfo <- function(filePath,fileName,hasHeader=TRUE,separator=",",interactive=FALSE){
+  
+  totalPath <- paste(filePath,fileName,sep="");  
+  siteInfo <- read.delim(  
+    totalPath, 
+    header = hasHeader,
+    sep=separator,
+    colClasses=c('character'),
+    fill = TRUE, 
+    comment.char="#")
+  
+  if(interactive){
+
+    if (!nzchar(siteInfo$station.nm)){
+      cat("No station name was listed. Please enter a station name here(no quotes): \n")
+      siteInfo$station.nm <- readline()
+    }
+    cat("Your site name is", siteInfo$station.nm,",")
+    cat("but you can modify this to a short name in a style you prefer. \nThis name will be used to label graphs and tables. \n")
+    cat("If you want the program to use the name given above, just do a carriage return, otherwise enter the preferred short name(no quotes):\n")
+    siteInfo$shortName <- readline()
+    if (!nzchar(siteInfo$shortName)) siteInfo$shortName <- siteInfo$station.nm
+    
+    if (!nzchar(siteInfo$param.nm)){
+      cat("No water quality parameter name was listed.\nPlease enter the name here(no quotes): \n")
+      siteInfo$param.nm <- readline()
+    }
+    
+    cat("Your water quality data are for '", siteInfo$param.nm, "'.\n")
+    cat("Typically you will want a shorter name to be used in graphs and tables. The suggested short name is:'", siteInfo$paramShortName, "'.\n")
+    cat("If you would like to change the short name, enter it here, otherwise just hit enter (no quotes):")
+    shortNameTemp <- readline()
+    
+    if (nchar(shortNameTemp)>0) siteInfo$paramShortName <- shortNameTemp
+    
+    if (!nzchar(siteInfo$param.units)){
+      cat("No water quality parameter unit was listed.\nPlease enter the units here(no quotes): \n")
+      siteInfo$param.nm <- readline()
+    }
+    cat("The units for the water quality data are: ", siteInfo$param.units, ".\n")
+    cat("It is helpful to set up a constiuent abbreviation when doing multi-constituent studies, enter a unique id (three or four characters should work something like tn or tp or NO3).\nIt is case sensitive.  Even if you don't feel you need an abbreviation you need to enter something (no quotes):\n")
+    siteInfo$constitAbbrev <- readline()
+
+    cat("It is helpful to set up a station abbreviation when doing multi-site studies, enter a unique id (three or four characters should work).\nIt is case sensitive.  Even if you don't feel you need an abbreviation for your site you need to enter something(no quotes):\n")
+    siteInfo$staAbbrev <- readline()
+  
+    if(is.na(siteInfo$drainSqKm)){
+      cat("No drainage area was listed as a column named 'drainSqKm'.\n")
       cat("Please enter the drainage area, you can enter it in the units of your choice.\nEnter the area, then enter drainage area code, \n1 is square miles, \n2 is square kilometers, \n3 is acres, \n4 is hectares.\n")
       cat("Area(no quotes):\n")
       siteInfo$drain.area.va <- readline()
@@ -189,6 +275,13 @@ getWQPInfo <- function(siteNumber, parameterCd, interactive=FALSE){
       conversionVector <- c(2.5899881, 1.0, 0.0040468564, 0.01)
       siteInfo$drainSqKm <- siteInfo$drain.area.va * conversionVector[qUnit]
     }
+  } else {
+    requiredColumns <- c("drainSqKm", "staAbbrev", "constitAbbrev", 
+                         "param.units", "paramShortName","shortName")
+    if(!all(requiredColumns %in% names(siteInfo))){
+      message("The following columns are expected in the EGRET package:\n")
+      message(requiredColumns[!(requiredColumns %in% names(siteInfo))])
+    }
   }
   
   siteInfo$queryTime <- Sys.time()
diff --git a/R/getSampleDataFromFile.r b/R/getSampleDataFromFile.r
index 89d90742..c69d8b60 100644
--- a/R/getSampleDataFromFile.r
+++ b/R/getSampleDataFromFile.r
@@ -8,7 +8,6 @@
 #' @param separator string character that separates data cells
 #' @param interactive logical Option for interactive mode.  If true, there is user interaction for error handling and data checks.
 #' @keywords data import file
-#' @keywords data import USGS WRTDS
 #' @export
 #' @return Sample dataframe
 #' @examples
@@ -28,8 +27,7 @@ getSampleDataFromFile <- function (filePath,fileName,hasHeader=TRUE,separator=",
 
 #' Import user sample data for EGRET analysis
 #'
-#' Imports data from a user-supplied file, and converts it to a Sample data frame (including summing multiple constituents), appropriate for WRTDS calculations. See
-#' section 3.4 of the vignette for more details.
+#' Imports data from a user-supplied file, and converts it to a Sample data frame (including summing multiple constituents), appropriate for WRTDS calculations. 
 #'
 #' @param filePath string specifying the path to the file
 #' @param fileName string name of file to open
@@ -37,7 +35,6 @@ getSampleDataFromFile <- function (filePath,fileName,hasHeader=TRUE,separator=",
 #' @param separator string character that separates data cells
 #' @param interactive logical Option for interactive mode.  If true, there is user interaction for error handling and data checks.
 #' @keywords data import file
-#' @keywords data import USGS WRTDS
 #' @seealso \code{\link{compressData}}, \code{\link{populateSampleColumns}}
 #' @export
 #' @return Sample dataframe
diff --git a/inst/extdata/infoTest.csv b/inst/extdata/infoTest.csv
new file mode 100644
index 00000000..16ea455a
--- /dev/null
+++ b/inst/extdata/infoTest.csv
@@ -0,0 +1,2 @@
+drainSqKm, staAbbrev, constitAbbrev, param.units, paramShortName,shortName
+50, sampleSite, N, mg/l, Nitrogen, Example Station
\ No newline at end of file
diff --git a/man/getNWISInfo.Rd b/man/getNWISInfo.Rd
index 5003e951..10f3bdf5 100644
--- a/man/getNWISInfo.Rd
+++ b/man/getNWISInfo.Rd
@@ -30,7 +30,6 @@ constitAbbrev - constitute abbreviation
 INFO <- getNWISInfo('05114000','00010')
 }
 \keyword{USGS}
-\keyword{WRTDS}
 \keyword{data}
 \keyword{import}
 \keyword{service}
diff --git a/man/getSampleDataFromFile.Rd b/man/getSampleDataFromFile.Rd
index 293ee4bb..ee25a09c 100644
--- a/man/getSampleDataFromFile.Rd
+++ b/man/getSampleDataFromFile.Rd
@@ -29,8 +29,6 @@ filePath <- paste(filePath,"/",sep="")
 fileName <- 'ChoptankRiverNitrate.csv'
 \dontrun{Sample <- getSampleDataFromFile(filePath,fileName, separator=";",interactive=FALSE)}
 }
-\keyword{USGS}
-\keyword{WRTDS}
 \keyword{data}
 \keyword{file}
 \keyword{import}
diff --git a/man/getUserInfo.Rd b/man/getUserInfo.Rd
new file mode 100644
index 00000000..0f56f530
--- /dev/null
+++ b/man/getUserInfo.Rd
@@ -0,0 +1,41 @@
+% Generated by roxygen2 (4.0.2): do not edit by hand
+\name{getUserInfo}
+\alias{getUserInfo}
+\title{Import Metadata from User-Generated File}
+\usage{
+getUserInfo(filePath, fileName, hasHeader = TRUE, separator = ",",
+  interactive = FALSE)
+}
+\arguments{
+\item{filePath}{string specifying the path to the file}
+
+\item{fileName}{string name of file to open}
+
+\item{hasHeader}{logical true if the first row of data is the column headers}
+
+\item{separator}{string character that separates data cells}
+
+\item{interactive}{logical Option for interactive mode.  If true, there is user interaction for error handling and data checks.}
+}
+\value{
+INFO dataframe with agency, site, dateTime, value, and code columns
+}
+\description{
+Populates INFO data frame for EGRET study.
+Additionally, the user will be asked for:
+staAbbrev - station abbreviation, will be used in naming output files and for structuring batch jobs
+constitAbbrev - constitute abbreviation
+}
+\examples{
+filePath <- system.file("extdata", package="dataRetrieval")
+filePath <- paste(filePath,"/",sep="")
+fileName <- 'infoTest.csv'
+INFO <- getUserInfo(filePath,fileName, separator=",",interactive=FALSE)
+}
+\keyword{USGS}
+\keyword{WRTDS}
+\keyword{data}
+\keyword{import}
+\keyword{service}
+\keyword{web}
+
diff --git a/man/getUserSample.Rd b/man/getUserSample.Rd
index a15153eb..b6b98441 100644
--- a/man/getUserSample.Rd
+++ b/man/getUserSample.Rd
@@ -21,8 +21,7 @@ getUserSample(filePath, fileName, hasHeader = TRUE, separator = ",",
 Sample dataframe
 }
 \description{
-Imports data from a user-supplied file, and converts it to a Sample data frame (including summing multiple constituents), appropriate for WRTDS calculations. See
-section 3.4 of the vignette for more details.
+Imports data from a user-supplied file, and converts it to a Sample data frame (including summing multiple constituents), appropriate for WRTDS calculations.
 }
 \examples{
 filePath <- system.file("extdata", package="dataRetrieval")
@@ -33,8 +32,6 @@ Sample <- getUserSample(filePath,fileName, separator=";",interactive=FALSE)
 \seealso{
 \code{\link{compressData}}, \code{\link{populateSampleColumns}}
 }
-\keyword{USGS}
-\keyword{WRTDS}
 \keyword{data}
 \keyword{file}
 \keyword{import}
diff --git a/man/getWQPInfo.Rd b/man/getWQPInfo.Rd
index f400f9c9..362b267a 100644
--- a/man/getWQPInfo.Rd
+++ b/man/getWQPInfo.Rd
@@ -16,10 +16,7 @@ getWQPInfo(siteNumber, parameterCd, interactive = FALSE)
 INFO dataframe with agency, site, dateTime, value, and code columns
 }
 \description{
-Populates INFO data frame for EGRET study.  If either station number or parameter code supplied, imports data about a particular USGS site from NWIS web service.
-This function gets the data from here: \url{http://waterservices.usgs.gov/}
-A list of parameter codes can be found here: \url{http://nwis.waterdata.usgs.gov/nwis/pmcodes/}
-If either station number or parameter code is not supplied, the user will be asked to input data.
+Populates INFO data frame for EGRET study.
 Additionally, the user will be asked for:
 staAbbrev - station abbreviation, will be used in naming output files and for structuring batch jobs
 constitAbbrev - constitute abbreviation
-- 
GitLab