forked from IMAP-Science-Operations-Center/imap_processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
524 lines (439 loc) · 18.4 KB
/
conftest.py
File metadata and controls
524 lines (439 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
"""Global pytest configuration for the package."""
import logging
import time
from contextlib import contextmanager
from pathlib import Path
import cdflib
import imap_data_access
import numpy as np
import pandas as pd
import pytest
import requests
import spiceypy
from imap_processing import imap_module_directory
from imap_processing.cdf.utils import load_cdf
from imap_processing.spice import config as spice_config
from imap_processing.spice.time import TTJ2000_EPOCH, met_to_ttj2000ns
from imap_processing.tests.external_test_data_config import EXTERNAL_TEST_DATA
@pytest.fixture(autouse=True)
def _set_global_config(monkeypatch, tmp_path):
"""Set the global data directory to a temporary directory."""
monkeypatch.setitem(imap_data_access.config, "DATA_DIR", tmp_path)
monkeypatch.setitem(
imap_data_access.config, "DATA_ACCESS_URL", "https://api.test.com"
)
@pytest.fixture(scope="session")
def imap_tests_path():
return imap_module_directory / "tests"
@pytest.fixture(autouse=True)
def clear_spin_and_repoint_paths(monkeypatch):
"""Clear the spin and repoint paths to avoid having test side effects."""
monkeypatch.setattr(spice_config, "_spin_table_paths", [])
monkeypatch.setattr(spice_config, "_repoint_table_path", None)
@pytest.fixture(scope="session")
def _download_kernels(spice_test_data_path):
_download_external_kernels(spice_test_data_path)
def _download_external_kernels(spice_test_data_path):
"""This fixture downloads externally-located kernels into the tests/spice/test_data
directory if they do not already exist there. The fixture is not intended to be
used directly. It is automatically added to tests marked with "external_kernel"
in the hook below."""
logger = logging.getLogger(__name__)
kernel_urls = [
"https://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/planets/de440s.bsp",
"https://naif.jpl.nasa.gov/pub/naif/generic_kernels/pck/pck00011.tpc",
"https://naif.jpl.nasa.gov/pub/naif/generic_kernels/pck/earth_latest_high_prec.bpc",
]
for kernel_url in kernel_urls:
kernel_name = kernel_url.split("/")[-1]
local_filepath = spice_test_data_path / kernel_name
if local_filepath.exists():
continue
allowed_attempts = 3
for attempt_number in range(allowed_attempts):
try:
with requests.get(kernel_url, stream=True, timeout=30) as r:
r.raise_for_status()
with open(local_filepath, "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
logger.info("Cached kernel file to %s", local_filepath)
continue
except requests.exceptions.RequestException as error:
logger.info(f"Request failed. {error}")
if attempt_number < allowed_attempts:
logger.info(
f"Trying again, retries left "
f"{allowed_attempts - attempt_number}, "
f"Exception: {error}"
)
time.sleep(1)
else:
logger.error(
f"Failed to download file {kernel_name} after "
f"{allowed_attempts} attempts, Final Error: {error}"
)
raise
@pytest.fixture(scope="session")
def _download_test_data():
_download_external_data()
def _download_external_data(external_test_data=EXTERNAL_TEST_DATA):
"""This fixture downloads externally-located test data files into a specific
location. The list of files and their storage locations are specified in
the `external_test_data` parameter, which is a list of tuples; the zeroth
element being the source of the test file in the AWS S3 bucket, and the
first element being the location in which to store the downloaded file."""
logger = logging.getLogger(__name__)
api_path = "https://api.dev.imap-mission.com/download/test_data/"
for source_filename, destination_path in external_test_data:
source = api_path + source_filename
destination = (
Path(f"{imap_module_directory}/tests") / destination_path / source_filename
)
# Create parent directories if they don't exist
destination.parent.mkdir(parents=True, exist_ok=True)
# Download the test data if necessary and write it to the appropriate
# directory
if not destination.exists():
response = requests.get(source, timeout=60)
if response.status_code == 200:
with open(destination, "wb") as file:
file.write(response.content)
logger.info(f"Downloaded file: {source}")
else:
logger.error(
f"Failed to download file: {source} "
f"with response: {response.status_code}"
)
else:
logger.info(f"File already exists: {destination}")
def pytest_collection_modifyitems(items):
"""
The use of this hook allows modification of test `Items` after tests have
been collected. In this case, it automatically adds fixtures based on the
following table:
+---------------------+----------------------------+
| pytest mark | fixture added |
+=====================+============================+
| external_kernel | _download_kernels |
| external_test_data | _download_test_data |
+---------------------+----------------------------+
Notes
-----
See the following link for details about this function, also known as a
pytest hook:
https://docs.pytest.org/en/stable/reference/reference.html#
pytest.hookspec.pytest_collection_modifyitems
"""
markers_to_fixtures = {
"external_kernel": "_download_kernels",
"external_test_data": "_download_test_data",
}
for item in items:
for marker, fixture in markers_to_fixtures.items():
if item.get_closest_marker(marker) is not None:
item.fixturenames.append(fixture)
@pytest.fixture(scope="session")
def spice_test_data_path(imap_tests_path):
return imap_tests_path / "spice/test_data"
@pytest.fixture(autouse=True, scope="session")
def furnish_time_kernels(spice_test_data_path):
"""Furnishes (temporarily) the testing LSK and SCLK"""
spiceypy.kclear()
test_lsk = spice_test_data_path / "naif0012.tls"
test_sclk = spice_test_data_path / "imap_sclk_0000.tsc"
spiceypy.furnsh(str(test_lsk))
spiceypy.furnsh(str(test_sclk))
yield test_lsk, test_sclk
spiceypy.kclear()
@pytest.fixture
def furnish_kernels(spice_test_data_path):
"""
Return a function to use as a context manager to furnish a list of kernels.
Kernel files are assumed to exist in the tests/spice/test_data directory.
Examples
--------
>>> def test_spicey_function(furnish_kernels):
>>> kernels_to_furnish = [
>>> "naif0012.tls",
>>> "kernel_0.tm",
>>> "kernel_1.bsp",
>>> ]
>>> with furnish_kernels(kernels_to_furnish):
>>> result = spicey_function()
"""
@contextmanager
def furnish_kernels(kernels: list[Path]):
with spiceypy.KernelPool(
[str(spice_test_data_path / k) for k in kernels]
) as pool:
yield pool
return furnish_kernels
@pytest.fixture
def use_test_spin_data_csv(monkeypatch):
"""Monkeypatches `spin._spin_table_paths` to the input Path."""
def wrapped_set_spin_data_filepath(paths: list[Path]):
monkeypatch.setattr(spice_config, "_spin_table_paths", paths)
return wrapped_set_spin_data_filepath
@pytest.fixture
def use_fake_spin_data_for_time(
request,
use_test_spin_data_csv,
tmp_path,
generate_spin_data,
spin_period=15.0,
):
"""
Generate and use fake spin data for testing.
Returns
-------
callable
Returns a callable function that takes start_met and optionally end_met
as inputs, generates fake spin data, writes the data to a csv file,
and sets the SPIN_DATA_FILEPATH environment variable to point to the
fake spin data file.
"""
def wrapped_set_spin_data_filepath(
start_met: float,
end_met: int | None = None,
spin_period: float | None = 15.0,
) -> pd.DataFrame:
"""
Generate and use fake spin data for testing.
Parameters
----------
start_met : int
Provides the start time in Mission Elapsed Time (MET).
end_met : int
Provides the end time in MET. If not provided, default to one day
from start time.
spin_period : float, optional
Provides the spin period in seconds. Default is 15.0 seconds.
"""
spin_df = generate_spin_data(
start_met, end_met=end_met, spin_period=spin_period
)
spin_csv_file_path = tmp_path / "spin_data.spin.csv"
spin_df.to_csv(spin_csv_file_path, index=False)
use_test_spin_data_csv([spin_csv_file_path])
return wrapped_set_spin_data_filepath
@pytest.fixture
def generate_spin_data():
def make_data(
start_met: float,
end_met: float | None = None,
spin_period: float | None = None,
) -> pd.DataFrame:
"""
Generate a spin table CSV covering one or more days.
Spin table contains the following fields:
(
spin_number,
spin_start_sec_sclk,
spin_start_subsec_sclk,
spin_start_utc,
spin_period_sec,
spin_period_valid,
spin_phase_valid,
spin_period_source,
thruster_firing
)
This function creates spin data using start MET and end MET time.
Each spin start data uses the nominal 15-second spin period. The spins that
occur from 00:00(Mid-night) to 00:10 UTC are marked with flags for
thruster firing, invalid spin period, and invalid spin phase.
Parameters
----------
start_met : float
Provides the start time in Mission Elapsed Time (MET).
end_met : float
Provides the end time in MET. If not provided, default to one day
from start time.
spin_period : float, optional
Provides the spin period in seconds. Default is 15.0 seconds.
Returns
-------
spin_df : pd.DataFrame
Spin data.
"""
if end_met is None:
# end_time is one day after start_time
end_met = start_met + 86400
# Create spin start second data of 15 seconds increment
spin_start_met = np.arange(start_met, end_met + 0.001, spin_period)
spin_start_sec = np.floor(spin_start_met).astype(int)
spin_start_subsec = ((spin_start_met - spin_start_sec) * 1e6).astype(int)
# Calculate UTC times without spice (accepting ~5 second inaccuracy)
spin_start_dt64 = TTJ2000_EPOCH + (spin_start_met * 1e9).astype(
"timedelta64[ns]"
)
nspins = len(spin_start_sec)
spin_df = pd.DataFrame.from_dict(
{
"spin_number": np.arange(nspins, dtype=np.uint32),
"spin_start_sec_sclk": spin_start_sec,
"spin_start_subsec_sclk": np.full(
nspins, spin_start_subsec, dtype=np.uint32
),
"spin_start_utc": np.datetime_as_string(spin_start_dt64, unit="us"),
"spin_period_sec": np.full(nspins, spin_period, dtype=np.float32),
"spin_period_valid": np.ones(nspins, dtype=np.uint8),
"spin_phase_valid": np.ones(nspins, dtype=np.uint8),
"spin_period_source": np.zeros(nspins, dtype=np.uint8),
"thruster_firing": np.zeros(nspins, dtype=np.uint8),
}
)
# Convert spin_start_sec to datetime to set repointing times flags
spin_start_dates = met_to_ttj2000ns(spin_start_sec + spin_start_subsec / 1e6)
spin_start_dates = cdflib.cdfepoch.to_datetime(spin_start_dates)
# Convert DatetimeIndex to Series for using .dt accessor
spin_start_dates_series = pd.Series(spin_start_dates)
# Find index of all timestamps that fall within 10 minutes after midnight
repointing_times = spin_start_dates_series[
(spin_start_dates_series.dt.time >= pd.Timestamp("00:00:00").time())
& (spin_start_dates_series.dt.time < pd.Timestamp("00:10:00").time())
]
repointing_times_index = repointing_times.index
# Use the repointing times to set thruster firing flag and spin period valid
spin_df.loc[repointing_times_index.values, "thruster_firing"] = 1
spin_df.loc[repointing_times_index.values, "spin_period_valid"] = 0
spin_df.loc[repointing_times_index.values, "spin_phase_valid"] = 0
return spin_df
return make_data
@pytest.fixture
def use_test_repoint_data_csv(monkeypatch):
"""Monkeypatches repoint._repoint_table_path to point to the input path."""
def wrapped_set_repoint_data_filepath(path: Path):
monkeypatch.setattr(spice_config, "_repoint_table_path", path)
return wrapped_set_repoint_data_filepath
def generate_repoint_data(
repoint_start_met: float | np.ndarray,
repoint_end_met: float | np.ndarray | None = None,
repoint_id_start: int | None = 0,
) -> pd.DataFrame:
"""
Generate a repoint dataframe for the star/end times provided.
Parameters
----------
repoint_start_met : float, np.ndarray
Provides the repoint start time(s) in Mission Elapsed Time (MET).
repoint_end_met : float, np.ndarray, optional
Provides the repoint end time(s) in MET. If not provided, end times
will be 15 minutes after start times.
repoint_id_start : int, optional
Provides the starting repoint id number of the first repoint in the
generated data.
Returns
-------
repoint_df : pd.DataFrame
Repoint dataframe with start and end repoint times provided and incrementing
repoint_ids starting at 1.
"""
repoint_start_times = np.atleast_1d(repoint_start_met)
if repoint_end_met is None:
repoint_end_met = repoint_start_times + 15 * 60
# Calculate UTC times without spice (accepting ~5 second inaccuracy)
repoint_start_dt64 = TTJ2000_EPOCH + (repoint_start_times * 1e9).astype(
"timedelta64[ns]"
)
repoint_end_dt64 = TTJ2000_EPOCH + (repoint_end_met * 1e9).astype("timedelta64[ns]")
repoint_df = pd.DataFrame.from_dict(
{
"repoint_start_sec_sclk": repoint_start_times.astype(int),
"repoint_start_subsec_sclk": ((repoint_start_times % 1.0) * 1e6).astype(
int
),
"repoint_start_utc": np.datetime_as_string(repoint_start_dt64, unit="us"),
"repoint_end_sec_sclk": repoint_end_met.astype(int),
"repoint_end_subsec_sclk": ((repoint_end_met % 1.0) * 1e6).astype(int),
"repoint_end_utc": np.datetime_as_string(repoint_end_dt64, unit="us"),
"repoint_id": np.arange(repoint_start_times.size, dtype=int)
+ repoint_id_start,
}
)
return repoint_df
@pytest.fixture
def use_fake_repoint_data_for_time(use_test_repoint_data_csv, tmp_path):
"""
Generate and use fake spin data for testing.
Returns
-------
callable
Returns a callable function that takes start_met and optionally n_repoints
as inputs, generates fake repoint data, writes the data to a csv file,
and sets the REPOINT_DATA_FILEPATH environment variable to point to the
fake repoint data file.
"""
def wrapped_repoint_data_filepath(
repoint_start_met: float | np.ndarray,
repoint_end_met: float | np.ndarray | None = None,
repoint_id_start: int | None = 0,
) -> pd.DataFrame:
"""
Generate and use fake repoint data for testing.
Parameters
----------
repoint_start_met : float, np.ndarray
Provides the repoint start time(s) in Mission Elapsed Time (MET).
repoint_end_met : float, np.ndarray
Provides the repoint end time(s) in MET. If not provided, end times
will be 15 minutes after start times.
repoint_id_start : int, optional
Provides the starting repoint id number of the first repoint in the
generated data.
"""
repoint_df = generate_repoint_data(
repoint_start_met,
repoint_end_met=repoint_end_met,
repoint_id_start=repoint_id_start,
)
repoint_csv_file_path = tmp_path / "repoint_data.repointing.csv"
repoint_df.to_csv(repoint_csv_file_path, index=False)
use_test_repoint_data_csv(repoint_csv_file_path)
return wrapped_repoint_data_filepath
@pytest.fixture
def imap_ena_sim_metakernel(furnish_kernels, _download_kernels):
kernels = [
"imap_sclk_0000.tsc",
"naif0012.tls",
"imap_spk_demo.bsp",
"sim_1yr_imap_attitude.bc",
"imap_130.tf",
"de440s.bsp",
"imap_science_120.tf",
"sim_1yr_imap_pointing_frame.bc",
]
with furnish_kernels(kernels) as k:
yield k
@pytest.fixture
def imap_ialirt_sim_metakernel(furnish_kernels):
kernels = ["imap_130.tf"]
with furnish_kernels(kernels) as k:
yield k
@pytest.fixture
def imap_simple_sim_metakernel(furnish_kernels):
kernels = ["imap_sclk_0000.tsc", "naif0012.tls", "imap_spk_demo.bsp"]
with furnish_kernels(kernels) as k:
yield k
# Shared with i-alirt and mag tests
@pytest.fixture
def mag_test_l1b_calibration_data():
imap_dir = Path(__file__).parent
cal_file = (
imap_dir
/ "mag"
/ "validation"
/ "calibration"
/ "imap_mag_l1b-calibration_20240229_v001.cdf"
)
calibration_data = load_cdf(cal_file)
matrix_mago = calibration_data["MFOTOURFO"]
time_shift_mago = calibration_data["OTS"]
matrix_magi = calibration_data["MFITOURFI"]
time_shift_magi = calibration_data["ITS"]
return matrix_mago, time_shift_mago, matrix_magi, time_shift_magi
if __name__ == "__main__":
# This is to enable downloading files easier by letting us
# run this file directly
_download_external_data()
_download_external_kernels(imap_module_directory / "tests" / "spice" / "test_data")