Test.R 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # Testing for internet connectivity, the connection to the MODIS SOAP WSDL Server and it's Web Service
  2. # Description Language, for the XML response from the Web Service method, and for the functions of
  3. # MODISTools.
  4. # Load data to be used for testing.
  5. rm(list = ls())
  6. library(MODISTools)
  7. data(SubsetExample, FindIDExample, QualityCheckExample, TransectExample, EndCoordinatesExample, ConvertExample)
  8. library(RCurl) # Will use some RCurl and XML functions explicitly in testing.
  9. library(XML)
  10. options(warn = 2)
  11. ## Following lines of code testing for internet connectivity and server access, are from
  12. ## R testing: .../tests/internet.R
  13. # Check for internet capability.
  14. if(!capabilities("http/ftp")) q()
  15. # Check for internet connectivity.
  16. if(.Platform$OS.type == "unix" && is.null(nsl("cran.r-project.org"))) q()
  17. # Check we can reach the server for lpdaac modis web service.
  18. if(.Platform$OS.type == "unix" && is.null(nsl("daacmodis.ornl.gov"))) q()
  19. # Check the web service is currently responsive.
  20. if(class(try(GetProducts(), silent = TRUE)) == "try-error") q()
  21. ##
  22. # Check the XML response is as expected.
  23. getsubset.xml <- paste('
  24. <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  25. xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mod="', daacmodis, '/MODIS_webservice">
  26. <soapenv:Header/>
  27. <soapenv:Body>
  28. <mod:getsubset soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  29. <Latitude xsi:type="xsd:float">', 51.41363, '</Latitude>
  30. <Longitude xsi:type="xsd:float">', -0.64875, '</Longitude>
  31. <Product xsi:type="xsd:string">', "MOD13Q1", '</Product>
  32. <Band xsi:type="xsd:string">', "250m_16_days_EVI", '</Band>
  33. <MODIS_Subset_Start_Date xsi:type="xsd:string">', "A2001001", '</MODIS_Subset_Start_Date>
  34. <MODIS_Subset_End_Date xsi:type="xsd:string">', "A2001025", '</MODIS_Subset_End_Date>
  35. <Km_Above_Below xsi:type="xsd:string">', 0, '</Km_Above_Below>
  36. <Km_Left_Right xsi:type="xsd:string">', 0, '</Km_Left_Right>
  37. </mod:getsubset>
  38. </soapenv:Body>
  39. </soapenv:Envelope>',
  40. sep = "")
  41. header.fields <- c(Accept = "text/xml",
  42. Accept = "multipart/*",
  43. 'Content-Type' = "text/xml; charset=utf-8",
  44. SOAPAction = "")
  45. reader <- basicTextGatherer()
  46. header <- basicTextGatherer()
  47. curlPerform(url = paste0(daacmodis, wsdl_loc),
  48. httpheader = header.fields,
  49. postfields = getsubset.xml,
  50. writefunction = reader$update,
  51. verbose = FALSE)
  52. # Check the server is not down by insepcting the XML response for internal server error message.
  53. if(grepl("Internal Server Error", reader$value())) q()
  54. xmlRoot(xmlTreeParse(reader$value()))
  55. ###
  56. # Check FindID example
  57. FindID(ID = SubsetExample, Data = FindIDExample)
  58. # Check QualityCheck example
  59. EVIdata <- QualityCheckExample[1:5, ]
  60. QAdata <- QualityCheckExample[6:10, ]
  61. QualityCheck(Data = EVIdata, Product = "MOD13Q1", Band = "250m_16_days_EVI", NoDataFill = -3000,
  62. QualityBand = "250m_16_days_pixel_reliability", QualityScores = QAdata, QualityThreshold = 0)
  63. ###
  64. # Check MODIS subset uses this output to produce correctly downloaded files.
  65. request <- GetSubset(Lat = SubsetExample$lat, Long = SubsetExample$long, Product = "MCD12Q1", Band = "Land_Cover_Type_1",
  66. StartDate = "A2005001", EndDate = "A2005001", KmAboveBelow = 0, KmLeftRight = 0)$subset[1]
  67. if(grepl("Server is busy handling other requests", request) | grepl("System overloaded", request) |
  68. grepl("Downloading from the web service is currently not working", request)){
  69. q()
  70. } else {
  71. # Check GetSubset is producing the correct output.
  72. # Use GetProducts, GetBands, and GetDates, to specify the GetSubset request.
  73. Product <- GetProducts()[1]
  74. Band <- GetBands(Product)[1]
  75. Dates <- GetDates(SubsetExample$lat, SubsetExample$long, Product)[1:2]
  76. GetSubset(Lat = SubsetExample$lat, Long = SubsetExample$long, Product = Product, Band = Band,
  77. StartDate = Dates[1], EndDate = Dates[1], KmAboveBelow = 0, KmLeftRight = 0)
  78. MODISSubsets(LoadDat = SubsetExample, Product = "MCD12Q1", Bands = c("Land_Cover_Type_1"),
  79. Size = c(1,1), StartDate = TRUE)
  80. MODISSummaries(LoadDat = SubsetExample, Product = "MCD12Q1", Band = "Land_Cover_Type_1",
  81. ValidRange = c(0,254), NoDataFill = 255, ScaleFactor = 1, StartDate = TRUE)
  82. }
  83. # Check the MODISSummaries file outputs are consistent.
  84. SummaryFile <- read.csv(list.files(pattern = "MODIS_Summary"))
  85. DataFile <- read.csv(list.files(pattern = "MODIS_Data"))
  86. file.check <- all(SummaryFile$mean.band == DataFile[1,which(grepl("pixel", names(DataFile)))])
  87. if(is.na(file.check)){
  88. warning("The two output files from MODISSummaries are not consistent.")
  89. }
  90. if(!file.check){
  91. warning("The two output files from MODISSummaries are not consistent.")
  92. }
  93. # Check example of MODISTransects
  94. request <- GetSubset(Lat = SubsetExample$lat, Long = SubsetExample$long, Product = "MOD13Q1", Band = "250m_16_days_EVI",
  95. StartDate = "A2000049", EndDate = "A2000049", KmAboveBelow = 0, KmLeftRight = 0)$subset[1]
  96. if(grepl("Server is busy handling other requests", request) | grepl("System overloaded", request) |
  97. grepl("Downloading from the web service is currently not working", request)){
  98. q()
  99. } else {
  100. MODISTransects(LoadData = TransectExample, Product = "MCD12Q1", Bands = c("Land_Cover_Type_1"),
  101. Size = c(0,0), StartDate = TRUE)
  102. }
  103. # Check EndCoordinates example
  104. EndCoordinates(LoadDat = EndCoordinatesExample, Distance = 2000, Angle = 90, AngleUnits = "degrees")
  105. # Check ConvertToDD example
  106. ConvertToDD(XY = ConvertExample, LatColName = "lat", LongColName = "long")
  107. # Check ExtractTile example
  108. TileExample <- read.csv(list.files(pattern = "MODIS_Data"))
  109. TileExample <- TileExample[ ,which(grepl("pixel", names(TileExample)))]
  110. dim(TileExample)
  111. dim(ExtractTile(Data = TileExample, Rows = c(5,1), Cols = c(5,1), Grid = FALSE))
  112. ExtractTile(Data = TileExample, Rows = c(5,1), Cols = c(5,1), Grid = FALSE)
  113. matrix(TileExample, nrow = 5, ncol = 5, byrow = TRUE)
  114. ExtractTile(Data = TileExample, Rows = c(5,1), Cols = c(5,1), Grid = TRUE)
  115. # Check LandCover on previously downloaded data from MODISSubsets
  116. LandCover(Band = "Land_Cover_Type_1")
  117. rm(list = ls())
  118. options(warn = 0)