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
26 changes: 22 additions & 4 deletions monetio/sat/_modis_l2_mm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import sys
from collections import OrderedDict
from datetime import datetime, timezone
from glob import glob

import numpy as np
Expand All @@ -20,18 +21,23 @@ def read_dataset(fname, variable_dict):
"""
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")
# convert seconds since 1993 to since 1970
start_time = hdf_read(f, "Scan_Start_Time") + epoch_1993
zmoon 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())
if "scale" in variable_dict[varname]:
values = variable_dict[varname]["scale"] * values
if "minimum" in variable_dict[varname]:
Expand All @@ -46,6 +52,15 @@ 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),
"time": (["dim_0", "dim_1"], start_time),
}
)
ds = ds.rename_dims({"dim_0": "Cell_Along_Swath", "dim_1": "Cell_Across_Swath"})

return ds


Expand Down Expand Up @@ -81,7 +96,10 @@ def read_mfdataset(fnames, variable_dict, debug=False):
logging_level = logging.DEBUG
logging.basicConfig(stream=sys.stdout, level=logging_level)

files = sorted(glob(fnames))
if isinstance(fnames, str):
files = sorted(glob(fnames))
else:
files = fnames

granules = OrderedDict()

Expand Down