Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MODIS L2 reader update #201

Merged
merged 11 commits into from
Oct 8, 2024
37 changes: 26 additions & 11 deletions monetio/sat/_modis_l2_mm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
from collections import OrderedDict
from glob import glob
from datetime import datetime, timezone
dwfncar marked this conversation as resolved.
Show resolved Hide resolved

import numpy as np
import xarray as xr
Expand All @@ -10,28 +11,33 @@
def read_dataset(fname, variable_dict):
"""
Parameters
----------
__________
fname : str
Input file path.

Returns
-------
_______
xarray.Dataset
"""
from monetio.sat.hdfio import hdf_close, hdf_list, hdf_open, hdf_read

epoch_1993 = int(datetime(1993, 1, 1, tzinfo=timezone.utc).timestamp())

print("reading " + fname)

ds = xr.Dataset()

f = hdf_open(fname)
hdf_list(f)
latitude = hdf_read(f, "Latitude") # noqa: F841
longitude = hdf_read(f, "Longitude") # noqa: F841
start_time = hdf_read(f, "Scan_Start_Time") # noqa: F841
# Geolocation and Time Parameters
latitude = hdf_read(f, "Latitude")
longitude = hdf_read(f, "Longitude")
start_time = hdf_read(f, "Scan_Start_Time") \
+ epoch_1993 # convert seconds since 1993 to since 1970
dwfncar marked this conversation as resolved.
Show resolved Hide resolved
for varname in variable_dict:
print(varname)
values = hdf_read(f, varname)
print('min, max: ', values.min(), ' ', values.max())
dwfncar marked this conversation as resolved.
Show resolved Hide resolved
if "scale" in variable_dict[varname]:
values = variable_dict[varname]["scale"] * values
if "minimum" in variable_dict[varname]:
Expand All @@ -46,13 +52,17 @@ def read_dataset(fname, variable_dict):
ds.attrs["quality_thresh"] = variable_dict[varname]["quality_flag"]
hdf_close(f)

ds = ds.assign_coords({'lon': (['dim_0', 'dim_1'], longitude),
'lat': (['dim_0', 'dim_1'], latitude)})
dwfncar marked this conversation as resolved.
Show resolved Hide resolved
ds = ds.rename_dims({'dim_0': 'Cell_Along_Swath', 'dim_1': 'Cell_Across_Swath'})

return ds


def apply_quality_flag(ds):
"""
Parameters
----------
__________
ds : xarray.Dataset
"""
if "quality_flag" in ds.attrs:
Expand All @@ -69,19 +79,24 @@ def apply_quality_flag(ds):
def read_mfdataset(fnames, variable_dict, debug=False):
"""
Parameters
----------
__________
fnames : str
Regular expression for input file paths.

Returns
-------
_______
xarray.Dataset
"""
if debug:
logging_level = logging.DEBUG
logging.basicConfig(stream=sys.stdout, level=logging_level)

files = sorted(glob(fnames))
else:
logging_level = logging.INFO
dwfncar marked this conversation as resolved.
Show resolved Hide resolved
logging.basicConfig(stream=sys.stdout, level=logging_level)

if isinstance(fnames, list):
files = fnames
else:
files = sorted(glob(fnames))
dwfncar marked this conversation as resolved.
Show resolved Hide resolved

granules = OrderedDict()

Expand Down