Searches NASA's CMR catalog, downloads MODIS HDF files via
httr2, extracts the requested band, mosaics tiles, crops to
the area of interest, scales values, and saves one GeoTIFF per
time step. Output files are named modis_{band}_{YYYY}_{MM}.tif
and work directly with process_raster_collection().
Usage
download_modis(
shapefile,
start,
end,
product = "MOD13A3",
band = "1 km monthly EVI",
out_dir = ".",
username = NULL,
password = NULL,
version = "061",
scale_factor = "auto",
keep_hdf = FALSE,
overwrite = FALSE
)Arguments
- shapefile
An
sfobject defining the area of interest. Reprojected to WGS84 (EPSG:4326) for the NASA query.- start
Character. Start date in any common format (e.g.
"2023-01-01").- end
Character. End date in any common format.
- product
Character. MODIS product short name (default
"MOD13A3"for monthly vegetation indices at 1 km).- band
Character. HDF subdataset name to extract (default
"1 km monthly EVI"). Useterra::describe(hdf_file)on a downloaded HDF to see available subdatasets.- out_dir
Character. Output directory for GeoTIFFs (default
"."). Created if it does not exist.- username
Character or
NULL. NASA Earthdata username. Falls back toSys.getenv("EARTHDATA_USERNAME").- password
Character or
NULL. NASA Earthdata password. Falls back toSys.getenv("EARTHDATA_PASSWORD").- version
Character. MODIS collection version (default
"061").- scale_factor
"auto", numeric, orNULL. When"auto"(default), the scale factor is looked up from a built-in table of known MODIS bands (e.g. 0.0001 for EVI/NDVI, 0.02 for LST). Pass a number to override, orNULLto skip scaling.- keep_hdf
Logical. Keep raw HDF files after processing? Default
FALSE(deletes them). WhenTRUE, saved to{out_dir}/hdf/.- overwrite
Logical. Re-download and reprocess existing files? Default
FALSE.
Details
Use modis_options() to browse available products.
Examples
if (FALSE) { # \dontrun{
boundary <- sf::st_read("guinea_adm0.geojson")
# download monthly EVI for 2023
paths <- download_modis(
shapefile = boundary,
start = "2023-01-01",
end = "2023-12-31",
out_dir = "data/evi",
username = "my_user",
password = "my_pass"
)
# then use the existing raster processing pipeline
result <- process_raster_collection(
directory = "data/evi",
shapefile = sf::st_read("guinea_adm3.geojson"),
id_cols = c("adm0", "adm1", "adm2", "adm3"),
aggregations = "mean"
)
# download NDVI instead
download_modis(
shapefile = boundary,
start = "2023-01-01",
end = "2023-12-31",
band = "1 km monthly NDVI",
out_dir = "data/ndvi"
)
# keep raw HDF files for inspection
download_modis(
shapefile = boundary,
start = "2023-01-01",
end = "2023-12-31",
out_dir = "data/evi",
keep_hdf = TRUE
)
} # }
