|
| 1 | + |
| 2 | + |
| 3 | +test_that("function mf_get_url() sends back the expected output for a MODIS query", { |
| 4 | + |
| 5 | + skip_on_cran() |
| 6 | + skip_on_ci() |
| 7 | + |
| 8 | + urls_modis <- mf_get_url(collection = "MOD13A3.061", |
| 9 | + variables = c("_1_km_monthly_NDVI"), |
| 10 | + roi = roi, |
| 11 | + time_range = time_range |
| 12 | + ) |
| 13 | + |
| 14 | + expect_is(urls_modis, "data.frame") # output is a data.frame |
| 15 | + expect_named(urls_modis, c("id_roi","time_start","collection","name","url")) # column names are ok |
| 16 | + expect_equal(ncol(urls_modis), 5) # there are 5 columns |
| 17 | + expect_equal(nrow(urls_modis), 5)# there are 5 rows (corresponding to 5 tiles) |
| 18 | + expect_match(urls_modis$url[1], "https://opendap.cr.usgs.gov") # urls starts with the right OPENDAP url |
| 19 | + |
| 20 | + expect_equal(urls_modis, data.frame(id_roi = rep("madagascar",5), |
| 21 | + time_start = rep(as.Date("2023-01-01"), 5), |
| 22 | + collection = rep("MOD13A3.061", 5), |
| 23 | + name = c("MOD13A3.061.2023001_2023060.h21v10.nc4", "MOD13A3.061.2023001_2023060.h21v11.nc4", "MOD13A3.061.2023001_2023060.h22v10.nc4", "MOD13A3.061.2023001_2023060.h22v11.nc4", "MOD13A3.061.2023001_2023060.h23v10.nc4"), |
| 24 | + url = c( "https://opendap.cr.usgs.gov/opendap/hyrax/MOD13A3.061/h21v10.ncml.nc4?MOD_Grid_monthly_1km_VI_eos_cf_projection,_1_km_monthly_NDVI%5B275:277%5D%5B922:1199%5D%5B1132:1199%5D,time%5B275:277%5D,YDim%5B922:1199%5D,XDim%5B1132:1199%5D", |
| 25 | + "https://opendap.cr.usgs.gov/opendap/hyrax/MOD13A3.061/h21v11.ncml.nc4?MOD_Grid_monthly_1km_VI_eos_cf_projection,_1_km_monthly_NDVI%5B275:277%5D%5B0:749%5D%5B919:1199%5D,time%5B275:277%5D,YDim%5B0:749%5D,XDim%5B919:1199%5D", |
| 26 | + "https://opendap.cr.usgs.gov/opendap/hyrax/MOD13A3.061/h22v10.ncml.nc4?MOD_Grid_monthly_1km_VI_eos_cf_projection,_1_km_monthly_NDVI%5B275:277%5D%5B165:1199%5D%5B0:1199%5D,time%5B275:277%5D,YDim%5B165:1199%5D,XDim%5B0:1199%5D", |
| 27 | + "https://opendap.cr.usgs.gov/opendap/hyrax/MOD13A3.061/h22v11.ncml.nc4?MOD_Grid_monthly_1km_VI_eos_cf_projection,_1_km_monthly_NDVI%5B275:277%5D%5B0:749%5D%5B0:982%5D,time%5B275:277%5D,YDim%5B0:749%5D,XDim%5B0:982%5D", |
| 28 | + "https://opendap.cr.usgs.gov/opendap/hyrax/MOD13A3.061/h23v10.ncml.nc4?MOD_Grid_monthly_1km_VI_eos_cf_projection,_1_km_monthly_NDVI%5B275:277%5D%5B165:351%5D%5B0:40%5D,time%5B275:277%5D,YDim%5B165:351%5D,XDim%5B0:40%5D") |
| 29 | + ) |
| 30 | + ) |
| 31 | + |
| 32 | +}) |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +test_that("function mf_get_url() sends back the expected output for a GPM query", { |
| 37 | + |
| 38 | + skip_on_cran() |
| 39 | + skip_on_ci() |
| 40 | + |
| 41 | + urls_gpm <- mf_get_url(collection = "GPM_3IMERGDF.07", |
| 42 | + variables = c("precipitation"), |
| 43 | + roi = roi, |
| 44 | + time_range = time_range |
| 45 | + ) |
| 46 | + |
| 47 | + expect_is(urls_gpm, "data.frame") # output is a data.frame |
| 48 | + expect_named(urls_gpm, c("id_roi","time_start","collection","name","url")) # column names are ok |
| 49 | + expect_equal(ncol(urls_gpm), 5) # there are 5 columns |
| 50 | + expect_equal(nrow(urls_gpm), 90) # there are 90 rows (corresponding to 90 dates) |
| 51 | + expect_match(urls_gpm$url[1], "https://gpm1.gesdisc.eosdis.nasa.gov") # urls starts with the right OPENDAP url |
| 52 | + |
| 53 | + expect_equal(urls_gpm[1,], data.frame(id_roi = "madagascar", |
| 54 | + time_start = as.Date("2023-01-01"), |
| 55 | + collection = "GPM_3IMERGDF.07", |
| 56 | + name = "3B-DAY.MS.MRG.3IMERG.20230101-S000000-E235959.V07B.nc4", |
| 57 | + url ="https://gpm1.gesdisc.eosdis.nasa.gov/opendap/GPM_L3/GPM_3IMERGDF.07/2023/01/3B-DAY.MS.MRG.3IMERG.20230101-S000000-E235959.V07B.nc4.nc4?precipitation%5B0:0%5D%5B2220:2313%5D%5B639:787%5D,time%5B0:0%5D,lon%5B2220:2313%5D,lat%5B639:787%5D" |
| 58 | + ) |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +}) |
| 63 | + |
| 64 | + |
| 65 | +test_that("function mf_get_url() sends back the expected errors when necessary", { |
| 66 | + |
| 67 | + skip_on_cran() |
| 68 | + skip_on_ci() |
| 69 | + |
| 70 | + # wrong type for roi |
| 71 | + expect_error(mf_get_url(collection = "MOD13A3.061", |
| 72 | + roi = "not_a_sf_object", |
| 73 | + time_range = as.Date(c("2017-01-01","2017-02-01"))), |
| 74 | + "Argument roi must be an object of class sf or sfc with POLYGON-type feature geometry and at least two columns : 'id' and a geometry column that must not be NULL or NA", |
| 75 | + fixed = TRUE) |
| 76 | + |
| 77 | + # wrong type for time range |
| 78 | + expect_error(mf_get_url(collection = "MOD13A3.061", |
| 79 | + roi = roi, |
| 80 | + time_range = c("2017-01-01","2017-02-01")), |
| 81 | + "Argument time_range is not of class Date or POSIXlt or is not of length 1 or 2 \n", |
| 82 | + fixed = TRUE) |
| 83 | + |
| 84 | + # wrong dates |
| 85 | + expect_error(mf_get_url(collection = "MOD13A3.061", |
| 86 | + roi = roi, |
| 87 | + time_range = as.Date(c("2000-01-01","2017-02-01"))), |
| 88 | + "First time frame in time_range argument is before the beginning of the mission\n", |
| 89 | + fixed = TRUE) |
| 90 | + |
| 91 | + expect_error(mf_get_url(collection = "MOD13A3.061", |
| 92 | + roi = roi, |
| 93 | + time_range = as.Date(c("2000-01-01","1999-02-01"))), |
| 94 | + "Time end is superior to time start in time_range argument \n", |
| 95 | + fixed = TRUE) |
| 96 | + |
| 97 | + # wrong length for time range |
| 98 | + expect_error(mf_get_url(collection = "MOD13A3.061", |
| 99 | + roi = roi, |
| 100 | + time_range = as.Date(c("2017-01-01","2017-02-01","2017-02-03"))), |
| 101 | + "Argument time_range is not of class Date or POSIXlt or is not of length 1 or 2 \n", |
| 102 | + fixed = TRUE) |
| 103 | + |
| 104 | + # wrong type for singleNetcdf |
| 105 | + expect_error(mf_get_url(collection = "MOD13A3.061", |
| 106 | + roi = roi, |
| 107 | + time_range = as.Date(c("2017-01-01","2017-02-01")), |
| 108 | + single_netcdf = "TRUE"), |
| 109 | + "single_netcdf argument must be boolean\n", |
| 110 | + fixed = TRUE) |
| 111 | + |
| 112 | + # Collection does not exist |
| 113 | + expect_error(mf_get_url(collection = "MOD13A3v006", |
| 114 | + roi = roi, |
| 115 | + time_range = as.Date(c("2017-01-01","2017-02-01"))), |
| 116 | + "The collection that you specified does not exist. Check mf_list_collections() to see which collections are implemented\n", |
| 117 | + fixed = TRUE) |
| 118 | + |
| 119 | + # output format is not specified |
| 120 | + expect_error(mf_get_url(collection = "MOD13A3.061", |
| 121 | + roi = roi, |
| 122 | + time_range = as.Date(c("2017-01-01","2017-02-01")), |
| 123 | + output_format = NA), |
| 124 | + "Specified output format is not valid. Please specify a valid output format \n", |
| 125 | + fixed = TRUE) |
| 126 | + |
| 127 | + # wrong variables specified |
| 128 | + expect_error(mf_get_url(collection = "MOD13A3.061", |
| 129 | + roi = roi, |
| 130 | + time_range = as.Date(c("2017-01-01","2017-02-01")), |
| 131 | + variables = c("LST_Day_1km","not_a_good_var")), |
| 132 | + "Specified variables do not exist or are not extractable for the specified collection. Use the function mf_list_variables to check which variables are available and extractable for the collection\n", |
| 133 | + fixed = TRUE) |
| 134 | + |
| 135 | + expect_error(mf_get_url(collection = "MOD13A3.061", |
| 136 | + roi = roi, |
| 137 | + time_range = as.Date(c("2017-01-01","2017-02-01")), |
| 138 | + variables = c("LST_Day_1km","time")), |
| 139 | + "Specified variables do not exist or are not extractable for the specified collection. Use the function mf_list_variables to check which variables are available and extractable for the collection\n", |
| 140 | + fixed = TRUE) |
| 141 | + |
| 142 | +}) |
0 commit comments