DEV Community

kougou
kougou

Posted on • Edited on

Download MODIS files Using PyModis Part1

To download MODIS files directly from NASA Earthdata, we’ll use the PyModis library, which allows us to handle the entire process in Python, from downloading to analyzing data. You can find PyModis here:
https://github.com/lucadelu/pyModis/

If you don’t already have a NASA Earthdata account, please register first at:
https://urs.earthdata.nasa.gov/

Installing Required Libraries

We’ll start by setting up an Anaconda virtual environment and installing the necessary libraries.

conda install -c conda-forge pymodis
conda install -c conda-forge libgdal
Enter fullscreen mode Exit fullscreen mode

When we install PyModis with conda, GDAL should be included automatically as a dependency. However, if GDAL is missing, we can install it manually. If HDF4 support is also required, installing libgdal will resolve any related errors, such as:

warnings.warn("GDAL installation has no support for HDF4, please update GDAL", ImportError)

Enter fullscreen mode Exit fullscreen mode

Using the downmodis Class in PyModis

The downmodis class from PyModis enables us to download MODIS files. Here’s a brief overview of its arguments, with a partial excerpt from the source code:

# This code is part of the PyModis library by Luca Delucchi and Logan C Byers,
# licensed under the GNU General Public License v2.0.

class downModis:
    """A class to download MODIS data from NASA FTP or HTTP repositories

       :param str destinationFolder: where the files will be stored
       :param str password: the password required by NASA authentication system
       :param str user: the user namerequired by NASA authentication system
       :param str url: the base url from where to download the MODIS data,
                       it can be FTP or HTTP but it has to start with
                       'ftp://' or 'http://' or 'https://'
       :param str path: the directory where the data that you want to
                        download are stored on the FTP server. For HTTP
                        requests, this is the part of the url between the 'url'
                        parameter and the 'product' parameter.
       :param str product: the code of the product to download, the code
                           should be idential to the one of the url
       :param str tiles: a set of tiles to be downloaded, None == all tiles.
                         This can be passed as a string of tileIDs separated
                         by commas, or as a list of individual tileIDs
       :param str today: the day to start downloading; in order to pass a
                         date different from today use the format YYYY-MM-DD
       :param str enddate: the day to end downloading; in order to pass a
                           date use the format YYYY-MM-DD. This day must be
                           before the 'today' parameter. Downloading happens
                           in reverse order (currently)

       :param int delta: timelag i.e. the number of days starting from
                         today backwards. Will be overwritten if
                         'enddate' is specifed during instantiation
       :param bool jpeg: set to True if you want to download the JPG overview
                         file in addition to the HDF
       :param bool debug: set to True if you want to obtain debug information
       :param int timeout: Timeout value for HTTP server (seconds)
       :param bool checkgdal: variable to set the GDAL check
    """

    def __init__(self, destinationFolder, password=None, user=None, token=None,
                 url="https://e4ftl01.cr.usgs.gov", tiles=None, path="MOLT",
                 product="MOD11A1.006", today=None, enddate=None, delta=10,
                 jpg=False, debug=False, timeout=30, checkgdal=True):
Enter fullscreen mode Exit fullscreen mode

We can authenticate using either a username and password or a token. To use a token, generate one.
For details, see:
https://urs.earthdata.nasa.gov/documentation/for_users/user_token

Locating the Correct Directory

Accessing the default url in the arguments, https://e4ftl01.cr.usgs.gov, will show a directory structure like this:

ASTT/
COMMUNITY/
ECOSTRESS/
MEASURES/
MOLA/
MOLT/
MOTA/
VIIRS/
Enter fullscreen mode Exit fullscreen mode

To download specific files, we need to locate them within this structure. For example, MYD11A2 files are stored in the MOLA directory. We can access them at:
https://e4ftl01.cr.usgs.gov/MOLA/MYD11A2.061

MOLA/
├── MYD09A1.061/
│   ├── 2002.07.04/
│   ├── 2002.07.12/
│   └── ...
├── ...
├── MYD11A2.061/
│   ├── 2002.07.04/
│   ├── 2002.07.12/
│   └── ...
└── ...
Enter fullscreen mode Exit fullscreen mode

To specify the location in PyModis, we set the path and product arguments.
For MYD11A2.061, we set:
path: "MOLA"
product: "MYD11A2.061" (where .061 is the version)

Example Code

The following example downloads MYD11A2.061 files for the h29v06 tile between 2024-01-01 and 2024-10-23.

from pymodis import downmodis

# Define parameters
destination_folder = "data"
tiles = "h29v06"
path = "MOLA"
product = "MYD11A2.061"
today = "2024-01-01"
enddate = "2024-10-23"

# Initialize downloader
modis_downloader = downmodis.downModis(
    destinationFolder=destination_folder,
    password="your password",
    user="your username",
    tiles=tiles,
    path=path,
    product=product,
    today=today,
    enddate=enddate,
)

# Connect and download files
modis_downloader.connect()
modis_downloader.downloadsAllDay()
Enter fullscreen mode Exit fullscreen mode

This script authenticates and downloads the desired files directly to the specified folder. Adjust parameters as needed for other MODIS products or time frames.

Top comments (0)