Skip to content

Commit

Permalink
Merge pull request #201 from dwfncar/develop
Browse files Browse the repository at this point in the history
MODIS L2 reader update
  • Loading branch information
zmoon authored Oct 8, 2024
2 parents 51f10f7 + dbacbab commit bf22dc6
Showing 1 changed file with 22 additions and 4 deletions.
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
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

0 comments on commit bf22dc6

Please sign in to comment.