dataRetrieval
dataRetrieval copied to clipboard
Validate characteristic names
https://www.waterqualitydata.us/Codes/characteristicname/validate?value=Depth%2C%20Secchi%20disk%20depth vs https://www.waterqualitydata.us/Codes/characteristicname/validate?value=Secchi%20depth
Also work on a cross-walk of pcode to characteristicName: https://www.waterqualitydata.us/public_srsnames/
Might be easier to work with: https://www.waterqualitydata.us/Codes/public_srsnames/?mimeType=json
Let's verify with WQP developers what service would be best to use here.
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/
- 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.
- Add roxygen documentation
- Add tests
- 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 callvalidateCharacteristicName
. This would probably go after line 310 (but still in that "else" statement). Another place we might want to use this function isreadWQPdots
, in that function there could be something added like this:
if("characteristicName" %in% names(values)){
valid_names <- validateCharacteristicName(values['characteristicName'])
}