Skip to content

Commit a598171

Browse files
committed
improve processing of temporal extent to include ranges as well as single dates
1 parent 2e6cc06 commit a598171

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

scripts/ej/cmr_processing.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,36 +69,38 @@ def _check_temporal_range(self, range_datetime: dict) -> tuple[datetime, datetim
6969
return begin_date, end_date
7070

7171
def _process_temporal_extents(self) -> TemporalInfo:
72-
"""Process all temporal information."""
7372
temporal_extents = self.umm.get("TemporalExtents", [])
7473
latest_end_date = None
7574
total_duration = 0
76-
single_date_times = []
75+
all_temporal_strings = []
7776

7877
for extent in temporal_extents:
79-
single_date_times.extend(extent.get("SingleDateTimes", []))
80-
range_datetimes = extent.get("RangeDateTimes", [])
78+
# Process single dates
79+
all_temporal_strings.extend(extent.get("SingleDateTimes", []))
8180

82-
for range_dt in range_datetimes:
81+
# Process range dates
82+
for range_dt in extent.get("RangeDateTimes", []):
8383
try:
8484
begin_date, end_date = self._check_temporal_range(range_dt)
85+
range_str = f"{range_dt['BeginningDateTime']} - {range_dt['EndingDateTime']}"
86+
all_temporal_strings.append(range_str)
87+
8588
if latest_end_date is None or end_date > latest_end_date:
8689
latest_end_date = end_date
8790
total_duration += (end_date - begin_date).days
8891
except (KeyError, ValueError):
8992
continue
9093

91-
# Fix: Extract Value and Unit correctly from the TemporalResolution dictionary
9294
temporal_resolution_dict = temporal_extents[0].get("TemporalResolution", {}) if temporal_extents else {}
9395
resolution_value = temporal_resolution_dict.get("Value", "")
9496
resolution_unit = temporal_resolution_dict.get("Unit", "")
9597

9698
return TemporalInfo(
9799
latest_end_date=latest_end_date,
98100
total_duration=total_duration,
99-
resolution=str(resolution_value), # Convert to string in case it's a number
101+
resolution=str(resolution_value),
100102
resolution_unit=resolution_unit,
101-
single_date_times=single_date_times,
103+
single_date_times=sorted(all_temporal_strings),
102104
)
103105

104106
def _process_spatial_info(self) -> SpatialInfo:

scripts/ej/test_cmr_processing.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ def cmr_dataset(self):
2121

2222
def test_full_dataset_processing(self, cmr_dataset):
2323
"""Test that all properties can be extracted from real data without errors"""
24-
# Test all property accessors
2524
assert cmr_dataset.dataset_name == "2000 Pilot Environmental Sustainability Index (ESI)"
2625
assert cmr_dataset.description.startswith("The 2000 Pilot Environmental Sustainability Index")
2726
assert cmr_dataset.limitations == "None"
2827
assert cmr_dataset.format == "PDF"
29-
assert cmr_dataset.temporal_extent == "" # No SingleDateTimes in example
30-
assert cmr_dataset.intended_use == "Path A" # ProcessingLevel is 4
28+
assert cmr_dataset.temporal_extent == "1978-01-01T00:00:00.000Z - 1999-12-31T00:00:00.000Z"
29+
assert cmr_dataset.intended_use == "Path A"
3130
assert cmr_dataset.source_link == "https://doi.org/10.7927/H4NK3BZJ"
3231
assert "Long temporal extent" in cmr_dataset.strengths
3332
assert "No recent data available" in cmr_dataset.weaknesses
@@ -133,6 +132,62 @@ def test_missing_temporal_data(self):
133132
assert dataset.temporal_info.latest_end_date is None
134133
assert dataset.temporal_resolution == ""
135134

135+
def test_single_date_only(self):
136+
data = {
137+
"meta": {},
138+
"umm": {"TemporalExtents": [{"SingleDateTimes": ["2020-01-01T00:00:00.000Z", "2020-06-01T00:00:00.000Z"]}]},
139+
}
140+
dataset = CmrDataset(data)
141+
assert dataset.temporal_extent == "2020-01-01T00:00:00.000Z, 2020-06-01T00:00:00.000Z"
142+
143+
def test_range_date_only(self):
144+
data = {
145+
"meta": {},
146+
"umm": {
147+
"TemporalExtents": [
148+
{
149+
"RangeDateTimes": [
150+
{
151+
"BeginningDateTime": "2020-01-01T00:00:00.000Z",
152+
"EndingDateTime": "2020-12-31T23:59:59.999Z",
153+
},
154+
{
155+
"BeginningDateTime": "2021-01-01T00:00:00.000Z",
156+
"EndingDateTime": "2021-12-31T23:59:59.999Z",
157+
},
158+
]
159+
}
160+
]
161+
},
162+
}
163+
dataset = CmrDataset(data)
164+
assert (
165+
dataset.temporal_extent
166+
== "2020-01-01T00:00:00.000Z - 2020-12-31T23:59:59.999Z, 2021-01-01T00:00:00.000Z - 2021-12-31T23:59:59.999Z" # noqa
167+
)
168+
169+
def test_combined_single_and_range_dates(self):
170+
data = {
171+
"meta": {},
172+
"umm": {
173+
"TemporalExtents": [
174+
{
175+
"SingleDateTimes": ["2020-01-01T00:00:00.000Z"],
176+
"RangeDateTimes": [
177+
{
178+
"BeginningDateTime": "2021-01-01T00:00:00.000Z",
179+
"EndingDateTime": "2021-12-31T23:59:59.999Z",
180+
}
181+
],
182+
}
183+
]
184+
},
185+
}
186+
dataset = CmrDataset(data)
187+
assert (
188+
dataset.temporal_extent == "2020-01-01T00:00:00.000Z, 2021-01-01T00:00:00.000Z - 2021-12-31T23:59:59.999Z"
189+
)
190+
136191

137192
class TestSpatialProcessing:
138193
"""Unit tests for spatial information processing"""

0 commit comments

Comments
 (0)