GetDates.R 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. GetDates <-
  2. function(Lat, Long, Product)
  3. {
  4. if(!any(Product == GetProducts())) stop("Product entered does not match any available products; see ?GetProducts.")
  5. if(!is.numeric(Lat) | !is.numeric(Long)) stop("Lat and Long inputs must be numeric.")
  6. if(length(Lat) != 1 | length(Long) != 1) stop("Incorrect number of Lats and Longs supplied (only 1 coordinate allowed).")
  7. if(abs(Lat) > 90 | abs(Long) > 180) stop("Detected a lat or long beyond the range of valid coordinates.")
  8. getdates.xml <- paste('
  9. <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mod="https://modis.ornl.gov/MODIS_soapservice">
  10. <soapenv:Header/>
  11. <soapenv:Body>
  12. <mod:getdates soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  13. <Latitude xsi:type="xsd:float">', Lat, '</Latitude>
  14. <Longitude xsi:type="xsd:float">', Long, '</Longitude>
  15. <Product xsi:type="xsd:string">', Product, '</Product>
  16. </mod:getdates>
  17. </soapenv:Body>
  18. </soapenv:Envelope>',
  19. sep = "")
  20. header.fields <- c(Accept = "text/xml",
  21. Accept = "multipart/*",
  22. 'Content-Type' = "text/xml; charset=utf-8",
  23. SOAPAction = "")
  24. reader <- basicTextGatherer()
  25. header <- basicTextGatherer()
  26. curlPerform(url = paste0(daacmodis, "/cgi-bin/MODIS/soapservice/MODIS_soapservice.pl"),
  27. httpheader = header.fields,
  28. postfields = getdates.xml,
  29. writefunction = reader$update,
  30. verbose = FALSE)
  31. # Check the server is not down by insepcting the XML response for internal server error message.
  32. if(grepl("Internal Server Error", reader$value())){
  33. stop("Web service failure: the ORNL DAAC server seems to be down, please try again later.
  34. The online subsetting tool (https://daac.ornl.gov/cgi-bin/MODIS/GLBVIZ_1_Glb/modis_subset_order_global_col5.pl)
  35. will indicate when the server is up and running again.")
  36. }
  37. xmlres <- xmlRoot(xmlTreeParse(reader$value()))
  38. datesres <- xmlSApply(xmlres[[1]],
  39. function(x) xmlSApply(x,
  40. function(x) xmlSApply(x,
  41. function(x) xmlSApply(x,xmlValue))))
  42. if(colnames(datesres) == "Fault"){
  43. if(length(datesres['faultstring.text', ][[1]]) == 0){
  44. stop("Downloading from the web service is currently not working. Please try again later.")
  45. }
  46. stop(datesres['faultstring.text', ])
  47. } else{
  48. return(as.vector(datesres))
  49. }
  50. }