GetSubset <- function(Lat, Long, Product, Band, StartDate, EndDate, KmAboveBelow, KmLeftRight) { if(length(Product) != 1) stop("Incorrect length of Product input. Give only one data product at a time.") if(length(Band) != 1) stop("Incorrect length of Band input. Give only one data band at a time.") if(!is.numeric(Lat) | !is.numeric(Long)) stop("Lat and Long inputs must be numeric.") if(length(Lat) != 1 | length(Long) != 1) stop("Incorrect number of Lats and Longs supplied (only 1 coordinate allowed).") if(abs(Lat) > 90 | abs(Long) > 180) stop("Detected a lat or long beyond the range of valid coordinates.") getsubset.xml <- paste(' ', Lat, ' ', Long, ' ', Product, ' ', Band, ' ', StartDate, ' ', EndDate, ' ', KmAboveBelow, ' ', KmLeftRight, ' ', sep = "") header.fields <- c(Accept = "text/xml", Accept = "multipart/*", 'Content-Type' = "text/xml; charset=utf-8", SOAPAction = "") reader <- basicTextGatherer() header <- basicTextGatherer() curlPerform(url = paste0(daacmodis, wsdl_loc), httpheader = header.fields, postfields = getsubset.xml, writefunction = reader$update, verbose = FALSE) # Check the server is not down by insepcting the XML response for internal server error message. if(grepl("Internal Server Error", reader$value())){ stop("Web service failure: the ORNL DAAC server seems to be down, please try again later. The online subsetting tool (https://daac.ornl.gov/cgi-bin/MODIS/GLBVIZ_1_Glb/modis_subset_order_global_col5.pl) will indicate when the server is up and running again.") } xmlres <- xmlRoot(xmlTreeParse(reader$value())) modisres <- xmlSApply(xmlres[[1]], function(x) xmlSApply(x, function(x) xmlSApply(x, function(x) xmlSApply(x,xmlValue)))) if(colnames(modisres) == "Fault"){ if(length(modisres['faultstring.text', ][[1]]) == 0){ stop("Downloading from the web service is currently not working. Please try again later.") } stop(modisres['faultstring.text', ]) } else{ modisres <- as.data.frame(t(unname(modisres[-c(7,11)]))) names(modisres) <- c("xll", "yll", "pixelsize", "nrow", "ncol", "band", "scale", "lat", "long", "subset") return(modisres) } }