Skip to content

Commit 87f856c

Browse files
committed
move time stamp conversion to regulate function. Also regulated single file
1 parent e04c1c2 commit 87f856c

2 files changed

Lines changed: 28 additions & 19 deletions

File tree

sarxarray/_io.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
RE_PATTERNS_DORIS5_IFG,
1919
TIME_FORMAT_DORIS4,
2020
TIME_FORMAT_DORIS5,
21+
TIME_STAMP_KEY,
2122
_dtypes,
2223
_memsize_chunk_mb,
2324
)
@@ -311,7 +312,7 @@ def read_metadata(
311312
- If a metadata key has values in float format, and the standard deviation is less
312313
than 1% of the mean, it takes the average of the values.
313314
- For the two Doris drivers "doris4" or "doris5", if the metadata key is
314-
"first_pixel_azimuth_time", it treats it as the timestamp of acquisition and
315+
TIME_STAMP_KEY, it treats it as the timestamp of acquisition and
315316
converts it to a numpy array of datetime64 format, sorted in ascending order.
316317
317318
@@ -361,7 +362,7 @@ def read_metadata(
361362
metadata[key].append(value)
362363
else:
363364
metadata[key] = [metadata[key], value]
364-
metadata = _regulate_metadata(metadata)
365+
metadata = _regulate_metadata(metadata, driver)
365366

366367
return metadata
367368

@@ -372,11 +373,9 @@ def _parse_metadata(file, driver):
372373
if driver == "doris5":
373374
patterns = RE_PATTERNS_DORIS5
374375
patterns_ifg = RE_PATTERNS_DORIS5_IFG
375-
time_format = TIME_FORMAT_DORIS5
376376
elif driver == "doris4":
377377
patterns = RE_PATTERNS_DORIS4
378378
patterns_ifg = None
379-
time_format = TIME_FORMAT_DORIS4
380379

381380
# Open the file
382381
with open(file) as f:
@@ -388,15 +387,6 @@ def _parse_metadata(file, driver):
388387
match = re.search(pattern, content)
389388
if match:
390389
results[key] = match.group(1)
391-
if key == "first_pixel_azimuth_time": # Convert to datetime
392-
try:
393-
dt = datetime.strptime(results[key], time_format)
394-
results[key] = np.datetime64(dt).astype("datetime64[s]")
395-
except ValueError as e:
396-
raise ValueError(
397-
f"Invalid date format for key: {key}. "
398-
"Expected format is '{time_format}'."
399-
) from e
400390
else:
401391
results[key] = None
402392

@@ -417,15 +407,35 @@ def _parse_metadata(file, driver):
417407
return results
418408

419409

420-
def _regulate_metadata(metadata):
410+
def _regulate_metadata(metadata, driver):
421411
"""Regulate metadata strings.
422412
423413
This function processes the metadata read from the DORIS files, which are strings,
424414
and converts according to the types specified in META_FLOAT_KEYS and META_INT_KEYS.
425415
426416
Check the documentation of `read_metadata` for the rules applied to the metadata.
427417
"""
428-
for key, value in metadata.items():
418+
# Convert time metadata from string to datetime
419+
if driver == "doris5":
420+
time_format = TIME_FORMAT_DORIS5
421+
elif driver == "doris4":
422+
time_format = TIME_FORMAT_DORIS4
423+
list_time = []
424+
# If the time is a single string, convert it to a list
425+
if isinstance(metadata[TIME_STAMP_KEY], str):
426+
metadata[TIME_STAMP_KEY] = [metadata[TIME_STAMP_KEY]]
427+
for time in metadata[TIME_STAMP_KEY]:
428+
try:
429+
dt = datetime.strptime(time, time_format)
430+
list_time.append(np.datetime64(dt).astype("datetime64[s]"))
431+
except ValueError as e:
432+
raise ValueError(
433+
f"Invalid date format for key: '{TIME_STAMP_KEY}'. "
434+
f"Expected format is '{time_format}'."
435+
) from e
436+
metadata[TIME_STAMP_KEY] = np.sort(np.array(list_time))
437+
438+
for key, value in list(metadata.items()):
429439
# raise error if different types are found in value
430440
if len(set(type(v) for v in value)) > 1:
431441
raise TypeError(
@@ -434,7 +444,7 @@ def _regulate_metadata(metadata):
434444
)
435445

436446
# Only keep the unique values
437-
if isinstance(value[0], str):
447+
if isinstance(metadata[key], list):
438448
metadata[key] = set(value)
439449

440450
# Unfold the single value set to strings
@@ -458,9 +468,6 @@ def _regulate_metadata(metadata):
458468
elif len(metadata[key]) > 1: # set with multiple values
459469
metadata[key] = set([int(v) for v in metadata[key]])
460470

461-
if key == "first_pixel_azimuth_time":
462-
metadata[key] = np.sort(np.array(metadata[key]))
463-
464471
if key in ["number_of_lines", "number_of_pixels"]:
465472
if isinstance(metadata[key], set):
466473
warning_msg = f"Multiple values found in {key}: {metadata[key]}."

sarxarray/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,5 @@
9191
# Time formats for DORIS metadata
9292
TIME_FORMAT_DORIS4 = "%d-%b-%Y %H:%M:%S.%f"
9393
TIME_FORMAT_DORIS5 = "%Y-%b-%d %H:%M:%S.%f"
94+
# Time stamp key
95+
TIME_STAMP_KEY = "first_pixel_azimuth_time"

0 commit comments

Comments
 (0)