# modis¶

Two satellites, Terra and Aqua, are home to a MODIS sensor, which has produced a large number of data products for over a full decade with minimal interruption. The modis module houses functions specifically related to processing and handling MODIS data, which includes handling of the MODIS sinusoidal projection, and mosaic operations.

Requires arcpy

## Examples¶

MODIS Extracting and mosaicing

This example is written to be a type a follow on to the Fetching MODIS example on the download module page so that users wishing to follow along precisely may easily get example data to work with. So lets assume we have a directory of some MODIS hdf files and you have had some time to fiddle with one or two in arcmap to know which layers you want, but you’d like to quickly extract all the same layers from your directory full of hdfs.

from dnppy import modis

indir  = r"C:\Users\jwely\test"      # input directory
layers = [3]                         # MOD11A1 fractional snow cover index
lnames = ["FracSnowCover"]           # how we want to name this layer
tifdir = r"C:\Users\jwely\test_tifs" # directory to place output tifs.

modis.extract_from_hdf(indir, layers, lnames, tifdir)


The indir input could be a file list, but to allow simple batching, most functions in dnppy will accept a folder as an input and automatically generate a list of all files in that folder. If you want to exercise more control over which files are placed into the function, you can call the core.list_files() function yourself and filter it down. In this case though, we are ok with simply running the operation on the whole directory. The layers and lnames inputs will depend on the specific MODIS product you are using, and both may be lists in case you want to extract multiple layers. The tifdir input may also be left blank, in which case files will simply be saved in the same directory as the input hdfs.

Next, we can mosaic very easily with

mosaicdir  = r"C:\Users\jwely\mosaics"  # directory to place MODIS mosaics
pixel_type = "8_BIT_UNSIGNED"           # arcmap bitdepth to save rasters as

modis.mosaic(tifdir, mosaicdir, pixel_type)


Where tifdir is used as the input directory, with the same value as in the previous code snippet. In this example, the MODIS data we are working with is only 8 bit unsigned integer data, so we manually specify this instead of allowing the default 32 bit float assignment, which will make the files unnecessarily large.

And thats it! You will notice it prints a summary of the different types of MODIS data it found to mosaic, including tiles, years, days, products, and suffixes (such as those generated during extraction). The mosiac function has a lot more optional inputs that a user may wish to use, but consulting help(modis.mosaic) is the best way to get all those details.

The rast series example will continue in the theme of this example MODIS dataset.

## Code Help¶

Auto-documentation for functions and classes within this module is generated below!

class modis_metadata(filename)[source]

Creates metadata object from MODIS filenames. these metadata objects help to interpret files with common MODIS naming conventions into meaningful metadata. In order for this to work, the begining of the filename must remain untouched from the original download name. In order to customize filenames, you should add only suffixes, usually separated by underscores.

Parameters: filename – the filename or filepath to a MODIS named file.
Attribute Description
product the modis product, such as MOD10A1 or MYD11A1
datetime_obj a datetime object of tile date
year the year of the modis tile
j_day the julian day of the modis tile
month the number of the month (ex 1)
month_name the name of the month (ex January)
day the integer day of the month
tile the modis tile, usually of format “h##v##”
type always equal to “MODIS”
version the algorithm version, always 3 digits, usually 005 or 041
tag processing tag (used by the DAACs)
suffix additional file suffixes that are not part of standard naming convention
extension the file extension, such as “hdf”, “tif”, etc...
define_projection(file_list)[source]

Give raster(s) proper MODIS sinusoidal projection metadata. Some MODIS data does not have an adequately defined projection for some software like arcmap to use

Parameters: file_list – a list of one or more filepaths
extract_from_hdf(file_list, layer_list, layer_names=False, outdir=None)[source]

Extracts tifs from MODIS HDF files, ensures proper projection.

Parameters: Return output_filelist: file_list – either a list of ‘.hdf’ files from which data should be extracted, or a directory containing ‘.hdf’ files. layer_list – list of layer numbers to pull out as individual tifs should be integers such as [0,4] for the 0th and 4th layer respectively. layer_names – list of layer names to put more descriptive file suffixes to each layer outdir – directory to which tif files should be saved if outdir is left as ‘None’, files are saved in the same directory as the input file was found. returns a list of all files created by this function
mosaic(filelist, outdir=None, pixel_type=None, bands='1', m_method='LAST', m_colormap='FIRST')[source]

Automatically identify appropriate MODIS files and mosaic them.

This script will find and mosaic all MODIS tiles groups with different time names in a directory. It will automatically identify the date ranges in the MODIS filenames and iterate through the entire range while skipping dates for which there are not at least two tiles. Users should be mindful of file suffixes from previous processing.

This script centers around the ‘arcpy.MosaicToNewRaster_management’ tool [http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000098000000]

Parameters: Return output_filelist: filelist – the directory containing MODIS data or a list of modis files. pixel_type – exactly as the input for the MosaicToNewRaster_management tool. defaults to “32_BIT_FLOAT” bands – exactly as the input for the MosaicToNewRaster_management tool. defaults to 1 m_method – exactly as the input for the MosaicToNewRaster_management tool. defaults to “LAST” m_colormap – exactly as the input for the MosaicToNewRaster_management tool. defaults to “FIRST” outdir – the directory to save output files to. If none is specified, a default directory will be created as ‘[indir]_Mosaicked’ returns list of files created by this function.