Title: | Extraction of Bands from MODIS Surface Reflectance Product MOD09 NRT |
---|---|
Description: | Package for processing downloaded MODIS Surface reflectance Product HDF files. Specifically, MOD09 surface reflectance product files, and the associated MOD03 geolocation files (for MODIS-TERRA). The package will be most effective if the user installs MRTSwath (MODIS Reprojection Tool for swath products; <https://lpdaac.usgs.gov/tools/modis_reprojection_tool_swath>, and adds the directory with the MRTSwath executable to the default R PATH by editing ~/.Rprofile. |
Authors: | Rishabh Gupta <[email protected]>, Nicholas J. Matzke, Dept. of Integrative Biology, U.C. Berkeley |
Maintainer: | Rishabh Gupta <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.14 |
Built: | 2024-11-16 03:32:55 UTC |
Source: | https://github.com/cran/mod09nrt |
Shortcut for: as.data.frame(x, row.names=NULL, stringsAsFactors=FALSE)
adf(x)
adf(x)
x |
matrix or other object transformable to data.frame |
This function is useful for dealing with errors due to
automatic conversion of some columns to factors. Another solution may be to prepend
options(stringsAsFactors = FALSE)
at the start of one's script, to turn off all default stringsAsFactors silliness.
data.frame
Nicholas J. Matzke [email protected]
x = matrix(c(1,2,3,4,5,6), nrow=3, ncol=2) adf(x)
x = matrix(c(1,2,3,4,5,6), nrow=3, ncol=2) adf(x)
Each MOD09 surface reflectance product file requires a corresponding MOD03 geolocation file to be successfully processed with the MRTSwath tool.
check_for_matching_geolocation_files_mod09nrt(moddir = getwd(), modtxt = "MOD09", geoloctxt = "MOD03", return_geoloc = FALSE, return_product = FALSE)
check_for_matching_geolocation_files_mod09nrt(moddir = getwd(), modtxt = "MOD09", geoloctxt = "MOD03", return_geoloc = FALSE, return_product = FALSE)
moddir |
the string describing the directory containing the MOD09 and MOD03 files; both must be in the same directory. Default: getwd(), which gives the present working directory. |
modtxt |
the text string indicating which HDF files are the MODIS surface reflectance product (or hypothetically, other product). Default: MOD09 (MODIS surface reflectance product) |
geoloctxt |
the text string indicating which HDF files are the MODIS geolocation files (or hypothetically, another set of files). Default: MOD03 |
return_geoloc |
if TRUE, return the list of unmatched geolocation files (e.g. MOD03 ) |
return_product |
if TRUE, return the list of unmatched product files (e.g. MOD09) |
MRTSwath is the MRT (MODIS Reprojection Tool) for the MODIS
E.g. this surface reflectance file:
MOD09.A2016204.0500.005.2016204062219.NRT.hdf
...goes with this corresponding geolocation file:
MOD03.A2016204.0500.005.2016204060250.NRT.hdf
...which is a large file (~30 MB) containing detailed information on the position, tilt, etc. of the MODIS satellite. MRTSwath tool needs one of each, however.
data.frame of matching files; or a list of non-matching files, if return_geoloc
or return_product
are TRUE.
Rishabh Gupta [email protected]
# Check your working directory moddir = getwd() # Here are some example MODIS files in mod09nrt/extdata/ # Code excluded from CRAN check because it depends on modiscdata ## Not run: library(devtools) library(modiscdata) moddir = system.file("extdata/2002raw/", package="modiscdata") # You need to have some e.g. MOD files in it (from the MODIS-TERRA platform) list.files(path=moddir, pattern="MOD") list.files(path=moddir, pattern="MOD") # Check for matches (for MODIS-TERRA platform) check_for_matching_geolocation_files_mod09nrt(moddir=moddir, modtxt="MOD09", geoloctxt="MOD03", return_geoloc=FALSE, return_product=FALSE) ## End(Not run)
# Check your working directory moddir = getwd() # Here are some example MODIS files in mod09nrt/extdata/ # Code excluded from CRAN check because it depends on modiscdata ## Not run: library(devtools) library(modiscdata) moddir = system.file("extdata/2002raw/", package="modiscdata") # You need to have some e.g. MOD files in it (from the MODIS-TERRA platform) list.files(path=moddir, pattern="MOD") list.files(path=moddir, pattern="MOD") # Check for matches (for MODIS-TERRA platform) check_for_matching_geolocation_files_mod09nrt(moddir=moddir, modtxt="MOD09", geoloctxt="MOD03", return_geoloc=FALSE, return_product=FALSE) ## End(Not run)
The filename is split on slashes, and the last item is taken; this should be just the filename.
extract_fn_from_path(fn_with_path)
extract_fn_from_path(fn_with_path)
fn_with_path |
The filename, with partial or full path |
fn
The extracted filename
Nicholas J. Matzke [email protected]
fn_with_path = "/Library/Frameworks/R.framework/Versions/2.15/Resources/library/modiscloud/extdata /2002raw/MYD35_L2.A2002185.1910.005.2007206043609.hdf" extract_fn_from_path(fn_with_path)
fn_with_path = "/Library/Frameworks/R.framework/Versions/2.15/Resources/library/modiscloud/extdata /2002raw/MYD35_L2.A2002185.1910.005.2007206043609.hdf" extract_fn_from_path(fn_with_path)
MRTSwath is the "MODIS Reprojection Tool for swath products". See: https://lpdaac.usgs.gov/tools/modis_reprojection_tool_swath).
run_swath2grid_mod09nrt(mrtpath = "swath2grid", prmfn = "tmpMRTparams.prm", tifsdir, modfn, geoloc_fn, ul_lon, ul_lat, lr_lon, lr_lat)
run_swath2grid_mod09nrt(mrtpath = "swath2grid", prmfn = "tmpMRTparams.prm", tifsdir, modfn, geoloc_fn, ul_lon, ul_lat, lr_lon, lr_lat)
mrtpath |
This is the path to the MRTSwath executable |
prmfn |
The name of the parameter/control file which will be the input to MRTSwath's |
tifsdir |
The directory to save the output TIF files in |
modfn |
The filename of the MODIS data |
geoloc_fn |
The filename of the corresponding geolocation file (annoyingly, this is a much larger file than the data file!) |
ul_lon |
Upper left (ul) longitude (x-coordinate) for subsetting |
ul_lat |
Upper left (ul) latitude (y-coordinate) for subsetting |
lr_lon |
Lower right (lr) longitude (x-coordinate) for subsetting |
lr_lat |
Lower right (lr) latitude (y-coordinate) for subsetting |
If you want this function to use MRTSwath tool successfully, you should
add the directory with the MRTSwath executable to the default R PATH
by editing ~/.Rprofile
.
cmdstr
The string giving the system command that ran swath2grid
Nicholas J. Matzke [email protected]
write_MRTSwath_param_file_mod09nrt
http://landweb.nascom.nasa.gov/cgi-bin/QA_WWW/newPage.cgi?fileName=hdf_filename @cite NASA2001
####################################################### # Run MRTSwath tool "swath2grid" ####################################################### # Source MODIS files (both data and geolocation) # Code excluded from CRAN check because it depends on modiscdata ## Not run: library(devtools) library(modiscdata) moddir = system.file("extdata/2002raw/", package="modiscdata") # Get the matching data/geolocation file pairs fns_df = check_for_matching_geolocation_files(moddir, modtxt="MOD09", geoloctxt="MOD03") fns_df # Resulting TIF files go in this directory tifsdir = getwd() # Box to subset ul_lat = 13 ul_lon = -87 lr_lat = 8 lr_lon = -82 for (i in 1:nrow(fns_df)) { prmfn = write_MRTSwath_param_file_mod09nrt(prmfn="tmpMRTparams.prm", tifsdir=tifsdir, modfn=fns_df$mod09_fns[i], geoloc_fn=fns_df$mod03_fns[i], ul_lon=ul_lon, ul_lat=ul_lat, lr_lon=lr_lon, lr_lat=lr_lat) print(scan(file=prmfn, what="character", sep="\n")) run_swath2grid_mod09nrt(mrtpath="swath2grid", prmfn="tmpMRTparams.prm", tifsdir=tifsdir, modfn=fns_df$mod309_fns[i], geoloc_fn=fns_df$mod03_fns[i], ul_lon=ul_lon, ul_lat=ul_lat, lr_lon=lr_lon, lr_lat=lr_lat) } list.files(tifsdir, pattern=".tif", full.names=TRUE) ## End(Not run)
####################################################### # Run MRTSwath tool "swath2grid" ####################################################### # Source MODIS files (both data and geolocation) # Code excluded from CRAN check because it depends on modiscdata ## Not run: library(devtools) library(modiscdata) moddir = system.file("extdata/2002raw/", package="modiscdata") # Get the matching data/geolocation file pairs fns_df = check_for_matching_geolocation_files(moddir, modtxt="MOD09", geoloctxt="MOD03") fns_df # Resulting TIF files go in this directory tifsdir = getwd() # Box to subset ul_lat = 13 ul_lon = -87 lr_lat = 8 lr_lon = -82 for (i in 1:nrow(fns_df)) { prmfn = write_MRTSwath_param_file_mod09nrt(prmfn="tmpMRTparams.prm", tifsdir=tifsdir, modfn=fns_df$mod09_fns[i], geoloc_fn=fns_df$mod03_fns[i], ul_lon=ul_lon, ul_lat=ul_lat, lr_lon=lr_lon, lr_lat=lr_lat) print(scan(file=prmfn, what="character", sep="\n")) run_swath2grid_mod09nrt(mrtpath="swath2grid", prmfn="tmpMRTparams.prm", tifsdir=tifsdir, modfn=fns_df$mod309_fns[i], geoloc_fn=fns_df$mod03_fns[i], ul_lon=ul_lon, ul_lat=ul_lat, lr_lon=lr_lon, lr_lat=lr_lat) } list.files(tifsdir, pattern=".tif", full.names=TRUE) ## End(Not run)
Shortcut for: gsub(pattern="//", replacement="/", x=tmpstr)
slashslash(tmpstr)
slashslash(tmpstr)
tmpstr |
a path that you want to remove double slashes from |
This function is useful for removing double slashes that can appear in full pathnames due to inconsistencies in trailing slashes in working directories etc.
outstr a string of the fixed path
Nicholas J. Matzke [email protected]
tmpstr = "/Library/Frameworks/R.framework/Versions/2.15/Resources/library/modiscloud/extdata/ 2002raw//MYD03.A2002185.0645.005.2009192031332.hdf" outstr = slashslash(tmpstr) outstr
tmpstr = "/Library/Frameworks/R.framework/Versions/2.15/Resources/library/modiscloud/extdata/ 2002raw//MYD03.A2002185.0645.005.2009192031332.hdf" outstr = slashslash(tmpstr) outstr
MRTSwath is the "MODIS Reprojection Tool for swath products". See: https://lpdaac.usgs.gov/tools/modis_reprojection_tool_swath).
write_MRTSwath_param_file_mod09nrt(prmfn = "tmpMRTparams.prm", tifsdir, modfn, geoloc_fn, ul_lon, ul_lat, lr_lon, lr_lat)
write_MRTSwath_param_file_mod09nrt(prmfn = "tmpMRTparams.prm", tifsdir, modfn, geoloc_fn, ul_lon, ul_lat, lr_lon, lr_lat)
prmfn |
The name of the parameter/control file which will be the input to MRTSwath's |
tifsdir |
The directory to save the output TIF files in |
modfn |
The filename of the MODIS data |
geoloc_fn |
The filename of the corresponding geolocation file (annoyingly, this is a much larger file than the data file!) |
ul_lon |
Upper left (ul) longitude (x-coordinate) for subsetting |
ul_lat |
Upper left (ul) latitude (y-coordinate) for subsetting |
lr_lon |
Lower right (lr) longitude (x-coordinate) for subsetting |
lr_lat |
Lower right (lr) latitude (y-coordinate) for subsetting |
If you want this function to use MRTSwath tool successfully, you should
add the directory with the MRTSwath executable to the default R PATH
by editing ~/.Rprofile
.
This function hard-codes these options into the parameter file:
* all the bands are extracted
* the output file is a GeoTIFF
* the output projection is Geographic (plain unprojected Latitude/Longitude)
* the resampling is Nearest Neighbor (NN), which of course is the only one which makes sense when the pixels encode bytes that encode bits that encode discrete classification results, 0/1 error flags, etc.
MRTswath can do many other projections and output formats; users can modify this function to run those options.
prmfn
The name of the temporary parameter file
Rishabh Gupta [email protected]
http://landweb.nascom.nasa.gov/cgi-bin/QA_WWW/newPage.cgi?fileName=hdf_filename @cite NASA2001
# Source MODIS files (both data and geolocation) # Code excluded from CRAN check because it depends on modiscdata ## Not run: library(devtools) library(modiscdata) moddir = system.file("extdata/2002raw/", package="modiscdata") # Get the matching data/geolocation file pairs fns_df = check_for_matching_geolocation_files_mod09nrt(moddir, modtxt="MOD09", geoloctxt="MOD03") fns_df # Resulting TIF files go in this directory tifsdir = getwd() # Box to subset ul_lat = 13 ul_lon = -87 lr_lat = 8 lr_lon = -82 for (i in 1:nrow(fns_df)) { prmfn = write_MRTSwath_param_file_mod09nrt(prmfn="tmpMRTparams.prm", tifsdir=tifsdir, modfn=fns_df$mod09_fns[i], geoloc_fn=fns_df$mod03_fns[i], ul_lon=ul_lon, ul_lat=ul_lat, lr_lon=lr_lon, lr_lat=lr_lat) print(scan(file=prmfn, what="character", sep="\n")) } ## End(Not run)
# Source MODIS files (both data and geolocation) # Code excluded from CRAN check because it depends on modiscdata ## Not run: library(devtools) library(modiscdata) moddir = system.file("extdata/2002raw/", package="modiscdata") # Get the matching data/geolocation file pairs fns_df = check_for_matching_geolocation_files_mod09nrt(moddir, modtxt="MOD09", geoloctxt="MOD03") fns_df # Resulting TIF files go in this directory tifsdir = getwd() # Box to subset ul_lat = 13 ul_lon = -87 lr_lat = 8 lr_lon = -82 for (i in 1:nrow(fns_df)) { prmfn = write_MRTSwath_param_file_mod09nrt(prmfn="tmpMRTparams.prm", tifsdir=tifsdir, modfn=fns_df$mod09_fns[i], geoloc_fn=fns_df$mod03_fns[i], ul_lon=ul_lon, ul_lat=ul_lat, lr_lon=lr_lon, lr_lat=lr_lat) print(scan(file=prmfn, what="character", sep="\n")) } ## End(Not run)