Skip to content

Commit 182e3b8

Browse files
committed
Handle updated brdf xml format
1 parent 90bca87 commit 182e3b8

1 file changed

Lines changed: 99 additions & 35 deletions

File tree

eodatasets3/prepare/nasa_c_m_mcd43a1_6_prepare.py

Lines changed: 99 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,30 @@ def parse_xml(filepath: Path):
2323
"""
2424
root = ElementTree.parse(str(filepath), forbid_dtd=False).getroot()
2525

26-
granule_id = root.find("*//ECSDataGranule/LocalGranuleID").text
26+
# Handle both old and new XML formats
27+
granule_id_elem = root.find(".//ECSDataGranule/LocalGranuleID")
28+
if granule_id_elem is not None:
29+
granule_id = granule_id_elem.text
30+
else:
31+
# New CMR format
32+
granule_id = root.find(".//GranuleUR").text
2733

28-
collection_name = root.find("*//CollectionMetaData/ShortName").text
29-
collection_version = root.find("*//CollectionMetaData/VersionID").text
34+
collection_name_elem = root.find(".//CollectionMetaData/ShortName")
35+
if collection_name_elem is not None:
36+
collection_name = collection_name_elem.text
37+
collection_version = root.find(".//CollectionMetaData/VersionID").text
38+
else:
39+
# New CMR format
40+
collection_name = root.find(".//Collection/ShortName").text
41+
collection_version = root.find(".//Collection/VersionId").text
3042

31-
instrument_node = root.find("*//Platform/Instrument/InstrumentShortName")
43+
instrument_node = root.find(".//Platform/Instrument/InstrumentShortName")
3244

3345
if instrument_node is not None:
3446
instrument = instrument_node.text
3547
platform = "+".join(
3648
sorted(
37-
(ele.text for ele in root.findall("*//Platform/PlatformShortName")),
49+
(ele.text for ele in root.findall(".//Platform/PlatformShortName")),
3850
reverse=True,
3951
)
4052
)
@@ -46,30 +58,88 @@ def parse_xml(filepath: Path):
4658
f"Could not determine instrument and platform from collection name {collection_name}"
4759
)
4860

49-
start_date = root.find("*//RangeDateTime/RangeBeginningDate").text
50-
start_time = root.find("*//RangeDateTime/RangeBeginningTime").text
51-
end_date = root.find("*//RangeDateTime/RangeEndingDate").text
52-
end_time = root.find("*//RangeDateTime/RangeEndingTime").text
53-
v_tile = (
54-
next(
55-
ele
56-
for ele in root.findall("*//PSA")
57-
if ele.find("PSAName").text == "VERTICALTILENUMBER"
61+
# Handle both old and new temporal formats
62+
start_date_elem = root.find(".//RangeDateTime/RangeBeginningDate")
63+
if start_date_elem is not None:
64+
# Old format: separate date and time elements
65+
start_date = start_date_elem.text
66+
start_time = root.find(".//RangeDateTime/RangeBeginningTime").text
67+
end_date = root.find(".//RangeDateTime/RangeEndingDate").text
68+
end_time = root.find(".//RangeDateTime/RangeEndingTime").text
69+
else:
70+
# New CMR format: combined date-time elements
71+
beginning_dt = root.find(".//RangeDateTime/BeginningDateTime").text
72+
ending_dt = root.find(".//RangeDateTime/EndingDateTime").text
73+
# Split the datetime into date and time components
74+
start_date, start_time = beginning_dt.replace("Z", "").split("T")
75+
end_date, end_time = ending_dt.replace("Z", "").split("T")
76+
77+
psa_elements = root.findall(".//PSA")
78+
if psa_elements:
79+
# Old format: PSA elements with PSAName and PSAValue
80+
v_tile = (
81+
next(
82+
ele
83+
for ele in psa_elements
84+
if ele.find("PSAName").text == "VERTICALTILENUMBER"
85+
)
86+
.find("PSAValue")
87+
.text
88+
)
89+
h_tile = (
90+
next(
91+
ele
92+
for ele in psa_elements
93+
if ele.find("PSAName").text == "HORIZONTALTILENUMBER"
94+
)
95+
.find("PSAValue")
96+
.text
97+
)
98+
else:
99+
# New CMR format: AdditionalAttribute elements with Name and Values/Value
100+
additional_attrs = root.findall(".//AdditionalAttribute")
101+
v_tile = (
102+
next(
103+
ele
104+
for ele in additional_attrs
105+
if ele.find("Name").text == "VERTICALTILENUMBER"
106+
)
107+
.find("Values/Value")
108+
.text
58109
)
59-
.find("PSAValue")
60-
.text
61-
)
62-
h_tile = (
63-
next(
64-
ele
65-
for ele in root.findall("*//PSA")
66-
if ele.find("PSAName").text == "HORIZONTALTILENUMBER"
110+
h_tile = (
111+
next(
112+
ele
113+
for ele in additional_attrs
114+
if ele.find("Name").text == "HORIZONTALTILENUMBER"
115+
)
116+
.find("Values/Value")
117+
.text
67118
)
68-
.find("PSAValue")
69-
.text
70-
)
71119

72-
creation_dt = root.find("*//InsertTime").text
120+
creation_dt = root.find(".//InsertTime").text
121+
if start_date_elem is not None:
122+
# Old format: separate date and time components
123+
from_dt = datetime.datetime.strptime(
124+
start_date + " " + start_time, "%Y-%m-%d %H:%M:%S.%f"
125+
).replace(tzinfo=datetime.timezone.utc)
126+
to_dt = datetime.datetime.strptime(
127+
end_date + " " + end_time, "%Y-%m-%d %H:%M:%S.%f"
128+
).replace(tzinfo=datetime.timezone.utc)
129+
creation_dt_parsed = datetime.datetime.strptime(
130+
creation_dt, "%Y-%m-%d %H:%M:%S.%f"
131+
).replace(tzinfo=datetime.timezone.utc)
132+
else:
133+
# New CMR format: ISO format with Z timezone
134+
from_dt = datetime.datetime.strptime(
135+
start_date + " " + start_time, "%Y-%m-%d %H:%M:%S.%f"
136+
).replace(tzinfo=datetime.timezone.utc)
137+
to_dt = datetime.datetime.strptime(
138+
end_date + " " + end_time, "%Y-%m-%d %H:%M:%S.%f"
139+
).replace(tzinfo=datetime.timezone.utc)
140+
creation_dt_parsed = datetime.datetime.strptime(
141+
creation_dt, "%Y-%m-%dT%H:%M:%S.%fZ"
142+
).replace(tzinfo=datetime.timezone.utc)
73143

74144
return {
75145
"collection_version": collection_version,
@@ -78,15 +148,9 @@ def parse_xml(filepath: Path):
78148
"platform": platform,
79149
"vertical_tile": int(v_tile),
80150
"horizontal_tile": int(h_tile),
81-
"from_dt": datetime.datetime.strptime(
82-
start_date + " " + start_time, "%Y-%m-%d %H:%M:%S.%f"
83-
).replace(tzinfo=datetime.timezone.utc),
84-
"to_dt": datetime.datetime.strptime(
85-
end_date + " " + end_time, "%Y-%m-%d %H:%M:%S.%f"
86-
).replace(tzinfo=datetime.timezone.utc),
87-
"creation_dt": datetime.datetime.strptime(
88-
creation_dt, "%Y-%m-%d %H:%M:%S.%f"
89-
).replace(tzinfo=datetime.timezone.utc),
151+
"from_dt": from_dt,
152+
"to_dt": to_dt,
153+
"creation_dt": creation_dt_parsed,
90154
}
91155

92156

0 commit comments

Comments
 (0)