Skip to content

Commit

Permalink
started MODIS downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
annajungbluth committed Jan 19, 2024
1 parent 64d5f2d commit 902e801
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 4 deletions.
2 changes: 1 addition & 1 deletion notebooks/0.2.1-MODIS-download.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.0"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions scripts/goes-download.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def goes_download(
time_step: Optional[str]=None,
satellite_number: int=16,
save_dir: Optional[str]=".",
instrument: str = 'ABI',
instrument: str = "ABI",
processing_level: str = 'L1b',
data_product: str = 'Rad',
domain: str = 'F',
Expand Down Expand Up @@ -265,7 +265,7 @@ def _check_satellite_number(satellite_number: str) -> bool:


def _check_input_processing_level(processing_level: str) -> bool:
"""checks processing level for GOES datas"""
"""checks processing level for GOES data"""
if processing_level in ["L1b"]:
return True
else:
Expand Down
115 changes: 114 additions & 1 deletion scripts/modis-download.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,117 @@ class GOES16(SatelliteFOV):
ds: xr.Dataset = transform_coords(ds, target_coords)
# resave
ds.to_netcdf(...)
"""
"""

from typing import Optional, List, Union
import os
import xarray as xr
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import tqdm
import typer
from loguru import logger
from datetime import datetime, timedelta

## SAT02XXX.AYYYYDD.HHDD.061.??????????????????.hdf
# MOD - TERRA
# MYD - AQUA
# XXX = 1KM (QKM, HKM)
# server - https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/61/
MOD021KM/YEAR/DAY

def modis_download(
start_date: str,
end_date: Optional[str]=None,
start_time: Optional[str]='00:00:00',
end_time: Optional[str]='23:59:00',
day_step: Optional[str]=None,
satellite: str='Terra',
save_dir: Optional[str]=".",
processing_level: str = 'L1b',
resolution: str = "1KM",
collection: str = '61'
):
# run checks
_check_input_processing_level(processing_level=processing_level)
satellite_code = _check_satellite(satellite=satellite)
resolution_code = _check_resolution(resolution=resolution)
logger.info(f"Satellite: {satellite}")
# check data product
data_product = f"{satellite_code}02{resolution_code}"
logger.info(f"Data Product: {data_product}")
_check_data_product_name(data_product=data_product)

# check start/end dates/times
if end_date is None:
end_date = start_date

_check_date_format(start_date, end_date)

# datetime conversion
start_datetime = datetime.strptime(start_date, "%Y-%m-%d")
end_datetime = datetime.strptime(end_date, "%Y-%m-%d")

_check_start_end_dates(start_datetime=start_datetime, end_datetime=end_datetime)


def _check_input_processing_level(processing_level: str) -> bool:
"""checks processing level for MODIS data"""
if processing_level in ["L1b"]:
return True
else:
msg = "Unrecognized processing level"
msg += f"\nNeeds to be 'L1b'. Others are not yet implemented"
raise ValueError(msg)

def _check_satellite(satellite: str) -> str:
if satellite == 'Aqua':
return 'MYD'
elif satellite == 'Terra':
return 'MOD'
else:
msg = "Unrecognized satellite"
msg += f"\nNeeds to be 'Aqua' or 'Terra'. Others are not yet implemented"
raise ValueError(msg)

def _check_resolution(resolution: str) -> str:
if resolution in ["1KM", "1Km", "1km"]:
return "1KM"
elif resolution in ["500M", "500m"]:
return "HKM"
elif resolution in ["250M", "250m"]:
return "QKM"
else:
msg = "Unrecognized resolution"
msg += f"\nNeeds to be '1KM', '500M', '250M. Others are not available"
raise ValueError(msg)

def _check_data_product_name(data_product: str) -> bool:
if data_product in ['MOD021KM', 'MOD02HKM', 'MOD02QKM', 'MYD021KM', 'MYD02HKM', 'MYD02QKM']:
return True
else:
msg = "Unrecognized data product"
msg += f"\nOnly implemented for TERRA/AQUA MODIS and 1KM, 500M, 250M resolution."
raise ValueError(msg)

def _check_date_format(start_date: str, end_date: str) -> bool:
try:
datetime.strptime(start_date, "%Y-%m-%d")
datetime.strptime(end_date, "%Y-%m-%d")
return True
except Exception as e:
msg = "Please check date/time format"
msg += "\nExpected date format: %Y-%m-%d"
raise SyntaxError(msg)

def _check_start_end_dates(start_datetime: datetime, end_datetime: datetime) -> bool:
""" check end_datetime is after start_datetime """
if start_datetime < end_datetime:
return True
else:
msg = "Start datetime must be before end datetime\n"
msg += f"This does not hold for start = {str(start_datetime)} and end = {str(end_datetime)}"
raise ValueError(msg)


0 comments on commit 902e801

Please sign in to comment.