Skip to content

Commit ae28ac2

Browse files
authored
FIX: Add microseconds to the webpoda queries (#292)
* FIX: Add microseconds to the webpoda queries When doing <= queries, we need to make sure we go up to the end of the time interval. If a packet had a timestamp of 23:59:59.2 then that wouldn't be caught by a query of <= 23:59:59 because there is an implicit floor in the backend web API. * MNT: Change to an exclusive right-edge for querying webpoda Rather than trying to add maximum values to times, we can switch over to using an exclusive right edge with a less-than comparison. This helps avoid issues with microsecond precision in the database timestamps when we were only querying with seconds resolution before.
1 parent 662149c commit ae28ac2

2 files changed

Lines changed: 17 additions & 18 deletions

File tree

imap_data_access/webpoda.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ def get_packet_times_ert(
186186
# are handled correctly, so pass them as params
187187
params = (
188188
# Query the ERT field between start and end date
189-
f"ert>={start_time.strftime('%Y-%m-%dT%H:%M:%S')}"
190-
f"&ert<={end_time.strftime('%Y-%m-%dT%H:%M:%S')}"
189+
f"ert>={start_time.strftime('%Y-%m-%dT%H:%M:%S.%f')}"
190+
f"&ert<{end_time.strftime('%Y-%m-%dT%H:%M:%S.%f')}"
191191
# only get the time (packet time)
192192
# Represent all times in yyyy-MM-dd'T'HH:mm:ss format
193193
"&project(time)&formatTime(\"yyyy-MM-dd'T'HH:mm:ss\")"
@@ -245,8 +245,8 @@ def get_packet_binary_data_sctime(
245245
query_range = f"{WEBPODA_APID_URL}/{SYSTEM_ID}/apid_{apid}.bin"
246246
params = (
247247
# Query the SCT field between start and end date
248-
f"time>={start_time.strftime('%Y-%m-%dT%H:%M:%S')}"
249-
f"&time<={end_time.strftime('%Y-%m-%dT%H:%M:%S')}"
248+
f"time>={start_time.strftime('%Y-%m-%dT%H:%M:%S.%f')}"
249+
f"&time<{end_time.strftime('%Y-%m-%dT%H:%M:%S.%f')}"
250250
# only the raw packet data
251251
"&project(packet)"
252252
)
@@ -303,7 +303,7 @@ def download_daily_data(
303303
logger.info(f"Unique spacecraft dates with packets: {unique_dates}")
304304

305305
# Iterate over the packet dates to make a query for each individual spacecraft day
306-
# packet_date 00:00:00 -> packet_date 23:59:59
306+
# packet_date 00:00:00 -> packet_date+1 00:00:00
307307
for date in unique_dates:
308308
path = _get_latest_version_file_path(
309309
instrument=instrument,
@@ -314,7 +314,7 @@ def download_daily_data(
314314
continue
315315

316316
daily_start_time = datetime.datetime.combine(date, datetime.time.min)
317-
daily_end_time = datetime.datetime.combine(date, datetime.time.max)
317+
daily_end_time = daily_start_time + datetime.timedelta(days=1)
318318

319319
# Some instruments request a buffer of packets on either side of the midnight
320320
# boundary to ensure their packet groupings work together.
@@ -439,13 +439,12 @@ def download_repointing_data(
439439
f"{packet_times[-1]}, skipping"
440440
)
441441
continue
442-
# NOTE: All queries are <= / >= following this, so we need to make sure we
443-
# are not double grabbing packets into the pointings.
444-
# The times included are [repointing_start, repointing_end), exclusive
445-
# on the right edge
442+
# NOTE: We need to make sure we are not double grabbing packets into the
443+
# pointings. The times included are [repointing_start, repointing_end),
444+
# exclusive on the right edge
446445
pointing_end = datetime.datetime.strptime(
447446
repointings[i + 1]["repoint_end_utc"], "%Y-%m-%d %H:%M:%S.%f"
448-
) - datetime.timedelta(seconds=1)
447+
)
449448
if pointing_end < packet_times[0]:
450449
# This pointing is before the first packet time, so skip it
451450
logger.debug(

tests/test_webpoda.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def test_get_packet_times_ert(mock_send_request, mock_request):
4242
f"https://lasp.colorado.edu/ops/imap/poda/dap2/apids/SID1/apid_{apid}.txt",
4343
headers={"Authorization": "Basic test_token"},
4444
params=(
45-
f"ert>={start_time.strftime('%Y-%m-%dT%H:%M:%S')}"
46-
f"&ert<={end_time.strftime('%Y-%m-%dT%H:%M:%S')}"
45+
f"ert>={start_time.strftime('%Y-%m-%dT%H:%M:%S.%f')}"
46+
f"&ert<{end_time.strftime('%Y-%m-%dT%H:%M:%S.%f')}"
4747
"&project(time)&formatTime(\"yyyy-MM-dd'T'HH:mm:ss\")"
4848
),
4949
)
@@ -60,7 +60,7 @@ def test_get_packet_binary_data_sctime(mock_send_request, mock_request):
6060
mock_send_request.return_value = mock_response
6161

6262
start_time = datetime.datetime(2024, 12, 1, 0, 0, 0)
63-
end_time = datetime.datetime(2024, 12, 1, 23, 59, 59)
63+
end_time = datetime.datetime(2024, 12, 1, 23, 59, 59, 999999)
6464
apid = 1136
6565

6666
result = get_packet_binary_data_sctime(apid, start_time, end_time)
@@ -71,8 +71,8 @@ def test_get_packet_binary_data_sctime(mock_send_request, mock_request):
7171
f"https://lasp.colorado.edu/ops/imap/poda/dap2/apids/SID1/apid_{apid}.bin",
7272
headers={"Authorization": "Basic test_token"},
7373
params=(
74-
f"time>={start_time.strftime('%Y-%m-%dT%H:%M:%S')}"
75-
f"&time<={end_time.strftime('%Y-%m-%dT%H:%M:%S')}"
74+
f"time>={start_time.strftime('%Y-%m-%dT%H:%M:%S.%f')}"
75+
f"&time<{end_time.strftime('%Y-%m-%dT%H:%M:%S.%f')}"
7676
"&project(packet)"
7777
),
7878
)
@@ -113,8 +113,8 @@ def test_download_daily_data(
113113
assert call == (
114114
1184,
115115
start_time - datetime.timedelta(minutes=1),
116-
datetime.datetime.combine(start_time, datetime.time.max)
117-
+ datetime.timedelta(minutes=1),
116+
# end time + 1 day, then buffer of 1 minute
117+
start_time + datetime.timedelta(days=1) + datetime.timedelta(minutes=1),
118118
)
119119

120120
# We expect two daily files to be created because we have packets

0 commit comments

Comments
 (0)