From c04ada42682fbe5ba9e8876681198538e3c07cb0 Mon Sep 17 00:00:00 2001
From: unknown <ldecicco@usgs.gov>
Date: Tue, 4 Nov 2014 13:42:44 -0600
Subject: [PATCH] Moved to use regular importRDB in readNWISsites.

---
 R/importRDB1.r   |  4 +++-
 R/readNWISsite.r | 45 ++-------------------------------------------
 2 files changed, 5 insertions(+), 44 deletions(-)

diff --git a/R/importRDB1.r b/R/importRDB1.r
index 0b5f4547..d28ef958 100644
--- a/R/importRDB1.r
+++ b/R/importRDB1.r
@@ -88,7 +88,9 @@ importRDB1 <- function(obs_url, asDateTime=FALSE, qw=FALSE, convertType = TRUE,
     })
   } else {
     doc <- obs_url
-
+    fileVecChar <- scan(obs_url, what = "", sep = "\n", quiet=TRUE)
+    pndIndx<-regexpr("^#", fileVecChar)
+    hdr <- fileVecChar[pndIndx > 0L]
   }
   
   tmp <- read.delim(  
diff --git a/R/readNWISsite.r b/R/readNWISsite.r
index a3a508a8..6d6b724d 100644
--- a/R/readNWISsite.r
+++ b/R/readNWISsite.r
@@ -15,47 +15,6 @@ readNWISsite <- function(siteNumbers){
   siteNumber <- paste(siteNumbers,collapse=",")
   urlSitefile <- paste("http://waterservices.usgs.gov/nwis/site/?format=rdb&siteOutput=Expanded&sites=",siteNumber,sep = "")
   
-  if(url.exists(urlSitefile)){
-    doc = tryCatch({
-      h <- basicHeaderGatherer()
-      doc <- getURL(urlSitefile, headerfunction = h$update)
-      
-    }, warning = function(w) {
-      message(paste("URL caused a warning:", urlSitefile))
-      message(w)
-    }, error = function(e) {
-      message(paste("URL does not seem to exist:", urlSitefile))
-      message(e)
-      return(NA)
-    }) 
-    
-    if(h$value()["Content-Type"] == "text/plain;charset=UTF-8"){
-    
-      SiteFile <- read.delim(
-        textConnection(doc),
-        header = TRUE,
-        quote="\"",
-        dec=".",
-        sep='\t',
-        colClasses=c('character'),
-        fill = TRUE,
-        comment.char="#")
-      
-      INFO <- SiteFile[-1,]
-      names(INFO) <- gsub("_",".",names(INFO))
-      
-      INFO$queryTime <- Sys.time()
-      INFO$dec.lat.va <- as.numeric(INFO$dec.lat.va)
-      INFO$dec.long.va <- as.numeric(INFO$dec.long.va)
-      INFO$alt.va <- as.numeric(INFO$alt.va)
-      
-      return(INFO)
-    } else {
-      message(paste("URL caused an error:", urlSitefile))
-      message("Content-Type=",h$value()["Content-Type"])
-      return(NA)
-    }
-  } else {
-    message("URL caused an error:", urlSitefile)
-  }
+  data <- importRDB1(urlSitefile,asDateTime=FALSE)
+ 
 }
-- 
GitLab