In a previous article, I explained how to download MODIS files using PyModis.
However, a common challenge when working with PyModis is determining the product's server path.
PyModis includes a script called productmodis.py for this purpose. Unfortunately, it was developed for Python 2 and is incompatible with Python 3.
Additionally, it does not include all MODIS products, so it may not meet the needs of all use cases.
To address this, I created a JSON file that maps MODIS product names to their corresponding server directories, as shown below.
You can download the file here.
{
"AG100.003": "ASTT",
"AG1km.003": "ASTT",
"AG5KMMOH.041": "ASTT",
"...": "...",
"MOD09A1.061": "MOLT",
"MOD09CMG.061": "MOLT",
"MOD09GA.061": "MOLT",
"...": "...",
"MCD12C1.061": "MOTA",
"MCD12Q1.061": "MOTA",
"MCD12Q2.061": "MOTA"
}
Each entry in the JSON consists of a product name as the key and the associated directory name on the server as the value.
For example:
-"AG100.003": "ASTT"
This indicates that the MODIS product AG100.003 can be found in the ASTT directory on the server.
-Similarly, "MOD09A1.061": "MOLT"
specifies that the MODIS product MOD09A1.061 is located in the MOLT directory.
The JSON structure is designed to make it easy to search for the server path of a product you want to download. Below is a Python script demonstrating how to perform exact and partial matches using this JSON file.
import json
# Function to load modis_dir.json file
def load_json(file_path):
"""Loads the JSON file containing MODIS product and directory mapping."""
with open(file_path, "r") as file:
return json.load(file)
# Function to search for a MODIS product's directory
def search_product_directory(query, data):
"""
Search for exact and partial matches of MODIS product names
and return corresponding directories as a dictionary.
"""
results = {}
# Exact match search
if query in data:
results[query] = data[query]
# Partial match search
for key, value in data.items():
# Exclude exact matches and only target partial matches
if query in key and query != key:
results[key] = value
return results
# Set the file path for modis_dir.json
file_path = "modis_dir.json" # Specify the path to the JSON file
# Load modis_dir.json
modis_dir = load_json(file_path)
# Set the search query (e.g., "MOD09A1")
query = "MOD09A1" # Enter the string you want to search for
# Perform the search
results = search_product_directory(query, modis_dir)
# Display results
if results:
print("Search results:")
for product, directory in results.items():
print(f"path: {directory}, product: {product}")
else:
print("No matching products found. Please check your query.")
Using this JSON file and script, you can easily integrate the product's server path into your download code. This simplifies the process and reduces manual effort when managing MODIS data.
Top comments (0)