forked from IMAP-Science-Operations-Center/imap_processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.py
More file actions
192 lines (160 loc) · 6.06 KB
/
test_utils.py
File metadata and controls
192 lines (160 loc) · 6.06 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
"""Tests for the ``cdf.utils`` module."""
from unittest import mock
import imap_data_access
import numpy as np
import pytest
import xarray as xr
from imap_processing.cdf.imap_cdf_manager import ImapCdfAttributes
from imap_processing.cdf.utils import (
load_cdf,
parse_filename_like,
write_cdf,
)
from imap_processing.spice.time import met_to_ttj2000ns
@pytest.fixture
def test_dataset():
"""Create a simple ``xarray`` dataset to be used in testing
Returns
-------
dataset : xarray.Dataset
The ``xarray`` dataset object
"""
# Load the CDF attrs
swe_attrs = ImapCdfAttributes()
swe_attrs.add_instrument_global_attrs("swe")
swe_attrs.add_global_attribute("Data_version", "001")
dataset = xr.Dataset(
{
"epoch": (
"epoch",
met_to_ttj2000ns([1, 2, 3]),
),
"nan_data": ("epoch", np.array([1.0, 2.0, np.nan]), {"FILLVAL": -1.0e31}),
},
attrs=swe_attrs.get_global_attributes("imap_swe_l1a_sci")
| {
"Logical_file_id": "imap_swe_l1a_sci_20100101_v001",
},
)
dataset["epoch"].attrs = swe_attrs.get_variable_attributes("epoch")
dataset["epoch"].attrs["DEPEND_0"] = "epoch"
return dataset
def test_load_cdf(test_dataset):
"""Tests the ``load_cdf`` function."""
# Write the dataset to a CDF to be used to test the load function
file_path = write_cdf(test_dataset)
# Load the CDF and ensure the function returns a dataset
dataset = load_cdf(file_path)
assert isinstance(dataset, xr.core.dataset.Dataset)
# Test that epoch is represented as a 64bit integer
assert dataset["epoch"].data.dtype == np.int64
# Test removal of attributes that are added on by cdf_to_xarray and
# are specific to xarray plotting
xarray_attrs = ["units", "standard_name", "long_name"]
for _, data_array in dataset.variables.items():
for attr in xarray_attrs:
assert attr not in data_array.attrs
assert np.isnan(dataset["nan_data"].data[2])
def test_load_cdf_extra_kwargs(test_dataset):
"""Test that load_cdf passes the correct extra kwargs to xarray_to_cdf"""
# Write the dataset to a CDF to be used to test the load function
file_path = write_cdf(test_dataset)
with mock.patch(
"imap_processing.cdf.utils.cdf_to_xarray", autospec=True
) as mock_cdf_to_xarray:
load_cdf(file_path, to_datetime=False)
assert mock_cdf_to_xarray.call_args.kwargs["to_datetime"] is False
def test_write_cdf(test_dataset):
"""Tests the ``write_cdf`` function.
Parameters
----------
dataset : xarray.Dataset
An ``xarray`` dataset object to test with
"""
file_path = write_cdf(test_dataset)
assert file_path.exists()
assert file_path.name == "imap_swe_l1a_sci_20100101_v001.cdf"
assert file_path.relative_to(imap_data_access.config["DATA_DIR"])
def test_written_and_loaded_dataset(test_dataset):
"""Tests that a dataset that is written to CDF and then loaded results in
the original dataset.
Parameters
----------
dataset : xarray.Dataset
An ``xarray`` dataset object to test with
"""
new_dataset = load_cdf(write_cdf(test_dataset))
assert str(test_dataset) == str(new_dataset)
def test_repoint_start_date(test_dataset):
output_file_path = write_cdf(test_dataset)
assert "imap_swe_l1a_sci_20100101_v001.cdf" in output_file_path.name
test_dataset.attrs["Start_date"] = "20001212"
output_file_path = write_cdf(test_dataset)
assert "imap_swe_l1a_sci_20001212_v001.cdf" in output_file_path.name
test_dataset.attrs["Repointing"] = "12345"
output_file_path = write_cdf(test_dataset)
assert "imap_swe_l1a_sci_20001212-repoint12345_v001.cdf" in output_file_path.name
def test_write_cdf_extra_cdf_kwargs(test_dataset):
"""Test the kwargs passed to cdflib.xarray.xarray_to_cdf by write_cdf()"""
with mock.patch(
"imap_processing.cdf.utils.xarray_to_cdf", autospec=True
) as xarray_to_cdf:
write_cdf(test_dataset)
assert xarray_to_cdf.call_args.kwargs["terminate_on_warning"] is False
assert xarray_to_cdf.call_args.kwargs["compression"] == 6
test_dataset.attrs["Logical_source"] = "imap_swe_l2_sci"
write_cdf(test_dataset, compression=9)
assert xarray_to_cdf.call_args.kwargs["terminate_on_warning"] is True
assert xarray_to_cdf.call_args.kwargs["istp"] is True
assert xarray_to_cdf.call_args.kwargs["compression"] == 9
@pytest.mark.parametrize(
"test_str, compare_dict",
[
(
"imap_hi_l1b_45sensor-de",
{
"mission": "imap",
"instrument": "hi",
"data_level": "l1b",
"sensor": "45sensor",
"descriptor": "de",
},
),
(
"imap_hi_l1a_hist_20250415_v001",
{
"mission": "imap",
"instrument": "hi",
"data_level": "l1a",
"descriptor": "hist",
"start_date": "20250415",
"version": "v001",
},
),
(
"imap_hi_l1c_90sensor-pset_20250415-repoint12345_v001.cdf",
{
"mission": "imap",
"instrument": "hi",
"data_level": "l1c",
"sensor": "90sensor",
"descriptor": "pset",
"start_date": "20250415",
"repointing": "12345",
"version": "v001",
"extension": "cdf",
},
),
("foo_hi_l1c_90sensor-pset_20250415_v001.cdf", None),
("imap_hi_l1c", None),
],
)
def test_parse_filename_like(test_str, compare_dict):
"""Test coverage for parse_filename_like function"""
if compare_dict:
match = parse_filename_like(test_str)
for key, value in compare_dict.items():
assert match[key] == value
else:
with pytest.raises(ValueError, match="Filename like string did not contain"):
_ = parse_filename_like(test_str)