-
Notifications
You must be signed in to change notification settings - Fork 28
/
test_copernicus.py
127 lines (111 loc) · 4.07 KB
/
test_copernicus.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
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
from os import path
from tempfile import TemporaryDirectory
import netCDF4
import numpy as np
import pytest
from cloudnetpy.exceptions import ValidTimeStampError
from cloudnetpy.instruments import copernicus
from tests.unit.all_products_fun import Check
from tests.unit.radar_fun import RadarFun
SCRIPT_PATH = path.dirname(path.realpath(__file__))
FILEPATH = f"{SCRIPT_PATH}/data/copernicus/"
class TestCopernicus2nc(Check):
site_meta = {
"name": "Chilbolton",
"latitude": 50,
"longitude": 104.5,
"altitude": 50,
"range_offset": 20,
}
temp_dir = TemporaryDirectory()
temp_path = temp_dir.name + "/copernicus.nc"
uuid = copernicus.copernicus2nc(FILEPATH, temp_path, site_meta)
date = "2022-07-10"
def test_variable_names(self):
keys = {
"Zh",
"v",
"width",
"ldr",
"SNR",
"time",
"range",
"radar_frequency",
"nyquist_velocity",
"latitude",
"longitude",
"altitude",
"zenith_angle",
"azimuth_angle",
"height",
"calibration_offset",
"beamwidthH",
"beamwidthV",
"antenna_diameter",
"range_offset",
}
assert set(self.nc.variables.keys()) == keys
def test_variables(self):
assert np.isclose(
self.nc.variables["radar_frequency"][:].data,
34.96,
) # Hard coded
assert np.all(
np.isclose(self.nc.variables["zenith_angle"][:].data, -0.13, atol=0.1),
)
def test_common_radar(self):
radar_fun = RadarFun(self.nc, self.site_meta, self.date, self.uuid)
for name, method in RadarFun.__dict__.items():
if "test_" in name:
getattr(radar_fun, name)()
def test_long_names(self):
data = [
("SNR", "Signal-to-noise ratio"),
]
for key, expected in data:
if key in self.nc.variables:
value = self.nc.variables[key].long_name
assert value == expected, f"{value} != {expected}"
def test_global_attributes(self):
assert self.nc.source == "RAL Space Copernicus"
assert self.nc.title == f'Copernicus cloud radar from {self.site_meta["name"]}'
def test_range(self):
for key in ("range", "height"):
assert np.all(self.nc.variables[key][:] > 0)
assert self.nc.variables["range_offset"][:] == self.site_meta["range_offset"]
def test_filename_argument(self, tmp_path):
test_path = tmp_path / "date.nc"
filename = f"{FILEPATH}/copernicus-file-20220710.nc"
copernicus.copernicus2nc(filename, test_path, self.site_meta)
def test_correct_date_validation(self, tmp_path):
test_path = tmp_path / "date.nc"
copernicus.copernicus2nc(FILEPATH, test_path, self.site_meta, date=self.date)
def test_wrong_date_validation(self, tmp_path):
test_path = tmp_path / "invalid.nc"
with pytest.raises(ValidTimeStampError):
copernicus.copernicus2nc(
FILEPATH,
test_path,
self.site_meta,
date="2021-01-03",
)
def test_uuid_from_user(self, tmp_path):
test_path = tmp_path / "uuid.nc"
uuid_from_user = "kissa"
uuid = copernicus.copernicus2nc(
FILEPATH,
test_path,
self.site_meta,
uuid=uuid_from_user,
)
with netCDF4.Dataset(test_path) as nc:
assert nc.file_uuid == uuid_from_user
assert uuid == uuid_from_user
def test_geolocation_from_source_file(self, tmp_path):
test_path = tmp_path / "geo.nc"
meta_without_geolocation = {"name": "Kumpula"}
copernicus.copernicus2nc(FILEPATH, test_path, meta_without_geolocation)
with netCDF4.Dataset(test_path) as nc:
for key in ("latitude", "longitude", "altitude"):
assert key in nc.variables
assert nc.variables[key][:] > 0