GetSubset.R 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. GetSubset <-
  2. function(Lat, Long, Product, Band, StartDate, EndDate, KmAboveBelow, KmLeftRight)
  3. {
  4. if(length(Product) != 1) stop("Incorrect length of Product input. Give only one data product at a time.")
  5. if(length(Band) != 1) stop("Incorrect length of Band input. Give only one data band at a time.")
  6. if(!is.numeric(Lat) | !is.numeric(Long)) stop("Lat and Long inputs must be numeric.")
  7. if(length(Lat) != 1 | length(Long) != 1) stop("Incorrect number of Lats and Longs supplied (only 1 coordinate allowed).")
  8. if(abs(Lat) > 90 | abs(Long) > 180) stop("Detected a lat or long beyond the range of valid coordinates.")
  9. getsubset.xml <- paste('
  10. <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">
  11. <soapenv:Header/>
  12. <soapenv:Body>
  13. <mod:getsubset soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  14. <Latitude xsi:type="xsd:float">', Lat, '</Latitude>
  15. <Longitude xsi:type="xsd:float">', Long, '</Longitude>
  16. <Product xsi:type="xsd:string">', Product, '</Product>
  17. <Band xsi:type="xsd:string">', Band, '</Band>
  18. <MODIS_Subset_Start_Date xsi:type="xsd:string">', StartDate, '</MODIS_Subset_Start_Date>
  19. <MODIS_Subset_End_Date xsi:type="xsd:string">', EndDate, '</MODIS_Subset_End_Date>
  20. <Km_Above_Below xsi:type="xsd:string">', KmAboveBelow, '</Km_Above_Below>
  21. <Km_Left_Right xsi:type="xsd:string">', KmLeftRight, '</Km_Left_Right>
  22. </mod:getsubset>
  23. </soapenv:Body>
  24. </soapenv:Envelope>',
  25. sep = "")
  26. header.fields <- c(Accept = "text/xml",
  27. Accept = "multipart/*",
  28. 'Content-Type' = "text/xml; charset=utf-8",
  29. SOAPAction = "")
  30. reader <- basicTextGatherer()
  31. header <- basicTextGatherer()
  32. curlPerform(url = paste0(daacmodis, wsdl_loc),
  33. httpheader = header.fields,
  34. postfields = getsubset.xml,
  35. writefunction = reader$update,
  36. verbose = FALSE)
  37. # Check the server is not down by insepcting the XML response for internal server error message.
  38. if(grepl("Internal Server Error", reader$value())){
  39. stop("Web service failure: the ORNL DAAC server seems to be down, please try again later.
  40. The online subsetting tool (https://daac.ornl.gov/cgi-bin/MODIS/GLBVIZ_1_Glb/modis_subset_order_global_col5.pl)
  41. will indicate when the server is up and running again.")
  42. }
  43. xmlres <- xmlRoot(xmlTreeParse(reader$value()))
  44. modisres <- xmlSApply(xmlres[[1]],
  45. function(x) xmlSApply(x,
  46. function(x) xmlSApply(x,
  47. function(x) xmlSApply(x,xmlValue))))
  48. if(colnames(modisres) == "Fault"){
  49. if(length(modisres['faultstring.text', ][[1]]) == 0){
  50. stop("Downloading from the web service is currently not working. Please try again later.")
  51. }
  52. stop(modisres['faultstring.text', ])
  53. } else{
  54. modisres <- as.data.frame(t(unname(modisres[-c(7,11)])))
  55. names(modisres) <- c("xll", "yll", "pixelsize", "nrow", "ncol", "band", "scale", "lat", "long", "subset")
  56. return(modisres)
  57. }
  58. }