dataRetrieval icon indicating copy to clipboard operation
dataRetrieval copied to clipboard

Validate characteristic names

Open ldecicco-USGS opened this issue 5 years ago • 5 comments

https://www.waterqualitydata.us/Codes/characteristicname/validate?value=Depth%2C%20Secchi%20disk%20depth vs https://www.waterqualitydata.us/Codes/characteristicname/validate?value=Secchi%20depth

ldecicco-USGS avatar Jan 21 '20 22:01 ldecicco-USGS

Also work on a cross-walk of pcode to characteristicName: https://www.waterqualitydata.us/public_srsnames/

ldecicco-USGS avatar Mar 08 '21 15:03 ldecicco-USGS

Might be easier to work with: https://www.waterqualitydata.us/Codes/public_srsnames/?mimeType=json

ldecicco-USGS avatar Mar 08 '21 15:03 ldecicco-USGS

Let's verify with WQP developers what service would be best to use here.

ldecicco-USGS avatar Aug 03 '22 15:08 ldecicco-USGS

OK, looks like the service we want is: https://www.waterqualitydata.us/Codes/characteristicname?text=Oxygen&pagesize=200&pagenumber=1&mimeType=json

There's a blurb about it at the very bottom here: https://www.waterqualitydata.us/webservices_documentation/

ldecicco-USGS avatar Aug 03 '22 17:08 ldecicco-USGS

  1. Construct a URL to: https://www.waterqualitydata.us/Codes/characteristicname?text= [USERS Request!] &pagesize=200&pagenumber=1&mimeType=json Check constructNWISURL, constructWQPURL Create a whole new function constructWQP_validate_url. Something like.....
constructWQP_validate_url <- function(characteristicName){
    url <- paste0("https://www.waterqualitydata.us/Codes/characteristicname?text=",
                          URLencode(characteristicName), 
                          "&pagesize=200&pagenumber=1&mimeType=json")
   return(url)
}

Look at the other "construct" functions for URLencode. The user should only be able to request 1 characteristic name at a time. Do a check if(length(characteristicName) > 1){warning()}. 2. Create roxygen documentation for the new function 3. Add some tests to the testthat folder 4. Look at the whatWQPdata function to see json being parsed. 5. Create a new function. Something like...


validateCharacteristicName <- function(characteristicNames){
   
   all_names <- data.frame()
   for(name in characteristicNames){
     url <- constructWQP_validate_url (name)
     doc <- getWebServiceData(url)
     retval <- as.data.frame(jsonlite::fromJSON(doc), stringsAsFactors = FALSE)

     if(nrows(retval) == 0){
        warning(name, "not valid")
     }

     all_names <- rbind(all_names , retval)
   }

   return(retval)
}

This step is probably going to be the most challenging. Use debug or run the code line by line to figure out where problems occur. Try single and multiple characteristic names, try names with spaces, any weird thing you can think of.

If there are issues in getWebServiceData, look at whatWQPdata and try to follow the same pattern for the saveFile_zip workflow.

If the errors seem to be jsonlite::fromJSON(doc)....you'll need to google around for how to parse json in R, trying to keep it with the jsonlite package.

  1. Add roxygen documentation
  2. Add tests
  3. Last step (?) will be to figure out how we want to incorporate this function into other WQP function calls. Maybe in the constructWQPURL function, we call validateCharacteristicName . This would probably go after line 310 (but still in that "else" statement). Another place we might want to use this function is readWQPdots, in that function there could be something added like this:
  if("characteristicName" %in% names(values)){
     valid_names <- validateCharacteristicName(values['characteristicName'])
  }

ldecicco-USGS avatar Aug 12 '22 16:08 ldecicco-USGS