forked from noaa-oar-arl/monetio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tempo_l2.py
86 lines (67 loc) · 2.32 KB
/
test_tempo_l2.py
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
import shutil
import warnings
from pathlib import Path
import pytest
from filelock import FileLock
from monetio.sat._tempo_l2_no2_mm import open_dataset
HERE = Path(__file__).parent
def retrieve_test_file():
fn = "sample_TEMPO_NO2_L2_V03_20240826T204005Z_S012G01.nc"
p = HERE / "data" / fn
if not p.is_file():
warnings.warn(f"Downloading test file {fn} for TEMPO NO2 L2 test")
import requests
r = requests.get(
"https://csl.noaa.gov/groups/csl4/modeldata/melodies-monet/data/"
f"example_observation_data/satellite/{fn}",
stream=True,
)
r.raise_for_status()
with open(p, "wb") as f:
f.write(r.content)
return p
@pytest.fixture(scope="module")
def test_file_path(tmp_path_factory, worker_id):
if worker_id == "master":
# Not executing with multiple workers;
# let pytest's fixture caching do its job
return retrieve_test_file()
# Get the temp directory shared by all workers
root_tmp_dir = tmp_path_factory.getbasetemp().parent
# Copy to the shared test location
p_test = root_tmp_dir / "tempo_l2_test.nc"
with FileLock(p_test.as_posix() + ".lock"):
if p_test.is_file():
return p_test
else:
p = retrieve_test_file()
shutil.copy(p, p_test)
return p_test
def test_open_dataset(test_file_path):
vn = "vertical_column_troposphere"
t_ref = "2024-08-26T20:40:05Z"
ds = open_dataset(test_file_path, {vn: {}})[t_ref]
assert set(ds.coords) == {"time", "lat", "lon"}
assert set(ds) == {vn}
assert set(ds.attrs) == {"granule_number", "reference_time_string", "scan_num"}
with pytest.warns(
UserWarning,
match=(
"Calculating pressure in TEMPO data requires surface_pressure. "
+ "Adding surface_pressure to output variables"
),
):
ds2 = open_dataset(
test_file_path,
{vn: {}, "main_data_quality_flag": {"quality_flag_max": 0}, "pressure": {}},
)[t_ref]
assert set(ds2.variables) == {
"lat",
"lon",
"main_data_quality_flag",
"pressure",
"surface_pressure",
"time",
"vertical_column_troposphere",
}
assert ds2["pressure"].dims == ("swt_level_stagg", "x", "y")