Using JSON to Simplify MODIS Product Path Management
In a previous article, I explained how to download MODIS files using PyModis.
One of the challenges with PyModis is determining the server path for a given product. While PyModis includes a script called productmodis.py to address this, the script was designed for Python 2 and is incompatible with Python 3. Additionally, it does not cover all MODIS products, which may limit its usefulness for some users.
To resolve this issue, I created a JSON file that maps MODIS product names to their corresponding server directories. You can download the JSON file here.
JSON File Structure
Here is a sample of the JSON file:
{
"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 consists of:
- Key: The MODIS product name.
Value: The corresponding server directory.
For example:"AG100.003": "ASTT"
indicates that the productAG100.003
is in theASTT
directory."MOD09A1.061": "MOLT"
means thatMOD09A1.061
is located in theMOLT
directory.
This JSON structure allows for easy and efficient searching of server paths for MODIS products.
This JSON structure allows for easy and efficient searching of server paths for MODIS products.
Python Script for Searching the JSON File
The following Python script demonstrates how to use the JSON file to search for MODIS product paths, both for exact and partial matches.
Loading the JSON File
import json
import os
from typing import Dict
def load_json_data(file_path: str) -> Dict[str, str]:
"""
Load and return JSON data from a file.
Parameters:
file_path (str): Path to the JSON file.
Returns:
dict: A dictionary containing the JSON data.
Raises:
FileNotFoundError: If the file does not exist.
ValueError: If the file contains invalid JSON.
"""
if not os.path.exists(file_path):
raise FileNotFoundError(f"JSON file '{file_path}' not found.")
try:
with open(file_path, "r") as file:
return json.load(file)
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSON format in file '{file_path}': {e}")
Searching for Products
from typing import List, Tuple
def search_product_directory(
product: str, data: Dict[str, str], case_insensitive: bool = True
) -> List[Tuple[str, str]]:
"""
Search for exact or partial matches of a product in the provided data dictionary.
Parameters:
product (str): The product name or keyword to search for.
data (dict): A dictionary where keys are product names and values are their corresponding directories.
case_insensitive (bool): If True, perform a case-insensitive search.
Returns:
list: A list of tuples containing matching product names and their corresponding directories.
Returns an empty list if no matches are found.
"""
if case_insensitive:
product = product.lower()
matches = [(key, value) for key, value in data.items() if product in key.lower()]
else:
matches = [(key, value) for key, value in data.items() if product in key]
return matches
Example usage
if __name__ == "__main__":
json_path = "modis_dir.json"
mapping = load_json_data(json_path)
# Case of exact match
product = "MOD11A1.061"
result = search_product_directory(product, mapping)
print(result)
# Output: [('MOD11A1.061', 'MOLT')]
# Case of partial match
product = "MOD11A"
result = search_product_directory(product, mapping)
print(result)
# Output: [('MOD11A1.061', 'MOLT'), ('MOD11A2.061', 'MOLT')]
# Case of no match
product = "NO_MATCH"
result = search_product_directory(product, mapping)
print(result)
# Output: []
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)