Title: | Extraction of Bands from MODIS Calibrated Radiances MOD02 NRT |
---|---|
Description: | Package for processing downloaded MODIS Calibrated radiances Product HDF files. Specifically, MOD02 calibrated radiance 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 |
Maintainer: | Rishabh Gupta <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.14 |
Built: | 2024-11-16 03:02:52 UTC |
Source: | https://github.com/cran/proccalibrad |
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
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 MOD02 calibrated radiance product file requires a corresponding MOD03 geolocation file to be successfully processed with the MRTSwath tool.
check_for_matching_geolocation_files_mod02nrt(moddir = getwd(), modtxt = "MOD02", geoloctxt = "MOD03", return_geoloc = FALSE, return_product = FALSE)
check_for_matching_geolocation_files_mod02nrt(moddir = getwd(), modtxt = "MOD02", geoloctxt = "MOD03", return_geoloc = FALSE, return_product = FALSE)
moddir |
the string describing the directory containing the MOD02 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 calibrated radiance product (or hypothetically, other product). Default: MOD02 (MODIS calibrated radiance 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. MOD02) |
MRTSwath is the MRT (MODIS Reprojection Tool) for the MODIS
E.g. this calibrated radiance file:
MOD021KM.A2016209.0515.005.NRT.hdf
...goes with this corresponding geolocation file:
MOD03.A2016209.0515.005.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 mod02nrt/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_mod02nrt(moddir=moddir, modtxt="MOD02", geoloctxt="MOD03", return_geoloc=FALSE, return_product=FALSE) ## End(Not run)
# Check your working directory moddir = getwd() # Here are some example MODIS files in mod02nrt/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_mod02nrt(moddir=moddir, modtxt="MOD02", 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
fn_with_path = "/Library/Frameworks/R.framework/Versions/2.15/Resources/library/ MOD021KM.A2016209.0515.005.NRT.hdf" extract_fn_from_path(fn_with_path)
fn_with_path = "/Library/Frameworks/R.framework/Versions/2.15/Resources/library/ MOD021KM.A2016209.0515.005.NRT.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_mod02nrt(mrtpath = "swath2grid", prmfn = "tmpMRTparams.prm", tifsdir, modfn, geoloc_fn, ul_lon, ul_lat, lr_lon, lr_lat)
run_swath2grid_mod02nrt(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
write_MRTSwath_param_file_mod02nrt
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="MOD02", 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_mod02nrt(prmfn="tmpMRTparams.prm", tifsdir=tifsdir, modfn=fns_df$mod02_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_mod02nrt(mrtpath="swath2grid", prmfn="tmpMRTparams.prm", tifsdir=tifsdir, modfn=fns_df$mod302_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="MOD02", 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_mod02nrt(prmfn="tmpMRTparams.prm", tifsdir=tifsdir, modfn=fns_df$mod02_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_mod02nrt(mrtpath="swath2grid", prmfn="tmpMRTparams.prm", tifsdir=tifsdir, modfn=fns_df$mod302_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
tmpstr = "/Library/Frameworks/R.framework/Versions/2.15/Resources/library/ MOD03.A2016209.0515.005.NRT.hdf" outstr = slashslash(tmpstr) outstr
tmpstr = "/Library/Frameworks/R.framework/Versions/2.15/Resources/library/ MOD03.A2016209.0515.005.NRT.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_mod02nrt(prmfn = "tmpMRTparams.prm", tifsdir, modfn, geoloc_fn, ul_lon, ul_lat, lr_lon, lr_lat)
write_MRTSwath_param_file_mod02nrt(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_mod02nrt(moddir, modtxt="MOD02", 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_mod02nrt(prmfn="tmpMRTparams.prm", tifsdir=tifsdir, modfn=fns_df$mod02_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_mod02nrt(moddir, modtxt="MOD02", 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_mod02nrt(prmfn="tmpMRTparams.prm", tifsdir=tifsdir, modfn=fns_df$mod02_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)