Skip to content

Commit

Permalink
List of granules for day
Browse files Browse the repository at this point in the history
this was part of Meng's update, as well as changing the key from
YYYYMMDD to YYYY-MM-DD
  • Loading branch information
zmoon committed Aug 30, 2024
1 parent d55adfa commit a250fd3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
11 changes: 8 additions & 3 deletions monetio/sat/_tropomi_l2_no2_mm.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _open_one_dataset(fname, variable_dict):
ds["scan_time"] = ds["time"] + dtime
ds["scan_time"].attrs.update({"long_name": "scan time"})
ds = ds.set_coords(["lon", "lat", "time", "scan_time"])
ds.attrs["reference_time_string"] = ref_time_val.astype(datetime).strftime(r"%Y%m%d")
ds.attrs["reference_time_string"] = ref_time_val.astype(datetime).strftime(r"%Y-%m-%d")

def get_extra(varname_, *, dct_=None, default_group="PRODUCT"):
"""Get non-varname variables."""
Expand Down Expand Up @@ -221,7 +221,8 @@ def open_dataset(fnames, variable_dict, debug=False):
Returns
-------
OrderedDict
Dict mapping reference time string (date) to :class:`xarray.Dataset` of the granule.
Dict mapping reference time string (date, YYYY-MM-DD)
to a list of :class:`xarray.Dataset` granules.
"""
if debug:
logging_level = logging.DEBUG
Expand Down Expand Up @@ -251,6 +252,10 @@ def open_dataset(fnames, variable_dict, debug=False):
for file in files:
granule = _open_one_dataset(file, variable_dict)
apply_quality_flag(granule)
granules[granule.attrs["reference_time_string"]] = granule
key = granule.attrs["reference_time_string"]
if key in granules:
granules[key].append(granule)
else:
granules[key] = [granule]

return granules
22 changes: 12 additions & 10 deletions tests/test_tropomi_l2.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ def test_file_path(tmp_path_factory, worker_id):
return p_test


T_REF = pd.Timestamp("2019-07-15")
KEY = T_REF.strftime(r"%Y-%m-%d")


def test_open_dataset(test_file_path):
vn = "nitrogendioxide_tropospheric_column" # mol m-2
t_ref = pd.Timestamp("2019-07-15")

ds = open_dataset(test_file_path, vn)[t_ref.strftime(r"%Y%m%d")]
ds = open_dataset(test_file_path, vn)[KEY][0]

assert set(ds.coords) == {"time", "lat", "lon", "scan_time"}
assert set(ds) == {vn}
Expand All @@ -68,8 +71,8 @@ def test_open_dataset(test_file_path):
assert ds[vn].min() < 0

assert ds.time.ndim == 0
assert pd.Timestamp(ds.time.values) == t_ref
assert (ds.scan_time.dt.floor("D") == t_ref).all()
assert pd.Timestamp(ds.time.values) == T_REF
assert (ds.scan_time.dt.floor("D") == T_REF).all()

ds2 = open_dataset(
test_file_path,
Expand All @@ -80,7 +83,7 @@ def test_open_dataset(test_file_path):
"preslev": {},
"qa_value": None,
},
)[t_ref.strftime(r"%Y%m%d")]
)[KEY][0]

assert not ds2[vn].isnull().all()
assert ds2[vn].min() >= 1e-9
Expand All @@ -104,7 +107,6 @@ def test_open_dataset(test_file_path):

def test_open_dataset_qa(test_file_path):
vn = "nitrogendioxide_tropospheric_column" # mol m-2
t_ref = pd.Timestamp("2019-07-15")

# Based on example YML from Meng
ds = open_dataset(
Expand All @@ -124,7 +126,7 @@ def test_open_dataset_qa(test_file_path):
"tm5_tropopause_layer_index": {"group": ["PRODUCT"]},
},
},
)[t_ref.strftime(r"%Y%m%d")]
)[KEY][0]

# assert {vn, "ph", "phb", "pb", "p", "T"} <= set(ds.data_vars)

Expand All @@ -134,15 +136,15 @@ def test_open_dataset_qa(test_file_path):

def test_open_dataset_opts(test_file_path):
vn = "nitrogendioxide_tropospheric_column" # mol m-2
t_ref = pd.Timestamp("2019-07-15")

def get(**kwargs):
return open_dataset(
granules = open_dataset(
test_file_path,
{
vn: kwargs,
},
)[t_ref.strftime(r"%Y%m%d")]
)
return granules[KEY][0]

def om(x):
return np.floor(np.log10(x))
Expand Down

0 comments on commit a250fd3

Please sign in to comment.