-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconftest.py
More file actions
472 lines (404 loc) · 15 KB
/
conftest.py
File metadata and controls
472 lines (404 loc) · 15 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
"""Set up fixtures for unit tests."""
from datetime import datetime
from os.path import join as path_join
from shutil import copy, move, rmtree
from tempfile import mkdtemp
import numpy as np
import xarray as xr
from harmony_service_lib.message import Message as HarmonyMessage
from harmony_service_lib.util import bbox_to_geometry
from pystac import Asset, Catalog, Item
from pytest import fixture
from varinfo import VarInfoFromNetCDF4
from metadata_annotator.history_functions import PROGRAM, get_semantic_version
@fixture(scope='function')
def temp_dir() -> str:
"""A temporary directory for test isolation."""
temp_directory = mkdtemp()
yield temp_directory
rmtree(temp_directory)
@fixture(scope='function')
def temp_output_file_path(temp_dir) -> str:
"""The file path for an output file in the test temporary directory."""
return path_join(temp_dir, 'annotated_output.nc')
@fixture(scope='function')
def varinfo_config_file(temp_dir) -> str:
"""Return file path of configuration file."""
config_path = path_join(temp_dir, 'earthdata_varinfo_test_config.json')
copy('tests/data/earthdata_varinfo_test_config.json', config_path)
return config_path
@fixture(scope='function')
def sample_varinfo(sample_netcdf4_file, varinfo_config_file) -> VarInfoFromNetCDF4:
"""Create sample VarInfoFromNetCDF4 instance."""
return VarInfoFromNetCDF4(
sample_netcdf4_file, config_file=varinfo_config_file, short_name='TEST01'
)
@fixture(scope='function')
def sample_netcdf4_file(temp_dir) -> str:
"""Create a sample NetCDF-4 file."""
file_name = path_join(temp_dir, 'test_input.nc')
sample_datatree = xr.DataTree(
dataset=xr.Dataset(
attrs={
'short_name': 'TEST01',
'update': 'original value',
'delete': 'attribute should not exist',
},
data_vars={
'variable_one': xr.DataArray(
np.array([]),
attrs={
'_FillValue': -9999.0,
'coordinates': 'original_value',
'units': 'seconds since 2000-00-00T12:34:56',
},
),
'variable_three': xr.DataArray(
np.array([]),
attrs={
'_FillValue': -9999.0,
'coordinates': 'time latitude longitude',
'notes': 'this variable does not match any override rules',
},
),
},
),
)
sample_datatree['/sub_group'] = xr.Dataset(
attrs={
'delete': 'attribute should not exist',
'update': 'original value',
},
data_vars={
'variable_two': xr.DataArray(
np.array([]),
attrs={
'_FillValue': -9999.0,
'coordinates': 'time latitude longitude',
'delete': 'attribute needs to be deleted',
},
),
'variable_four': xr.DataArray(
np.array([]),
attrs={
'_FillValue': -9999.0,
'coordinates': 'original_value',
},
),
},
)
sample_datatree.to_netcdf(file_name, encoding=None)
return file_name
@fixture(scope='function')
def expected_output_netcdf4_file(temp_dir) -> str:
"""NetCDF-4 file with metadata updated per earthdata-varinfo config file."""
file_name = path_join(temp_dir, 'expected_output.nc')
sample_datatree = xr.DataTree(
dataset=xr.Dataset(
attrs={
'short_name': 'TEST01',
'update': 'corrected root group value',
'addition': 'new root group value',
'history': f'2000-01-02T03:04:05+00:00 {PROGRAM} '
f'{get_semantic_version()}',
},
data_vars={
'EASE2_north_polar_projection_36km': xr.DataArray(
b'',
attrs={
'false_easting': 0.0,
'false_northing': 0.0,
'grid_mapping_name': 'lambert_azimuthal_equal_area',
'latitude_of_projection_origin': 90.0,
'longitude_of_projection_origin': 0.0,
},
),
'variable_one': xr.DataArray(
np.array([]),
attrs={
'_FillValue': -9999.0,
'coordinates': 'time latitude longitude',
'grid_mapping': '/EASE2_north_polar_projection_36km',
'units': 'seconds since 2000-00-00T12:34:56',
},
),
'variable_three': xr.DataArray(
np.array([]),
attrs={
'_FillValue': -9999.0,
'coordinates': 'time latitude longitude',
'notes': 'this variable does not match any override rules',
},
),
},
),
)
sample_datatree['/sub_group'] = xr.Dataset(
attrs={
'update': 'corrected subgroup value',
'nested_addition': 'new subgroup value',
},
data_vars={
'variable_two': xr.DataArray(
np.array([]),
attrs={
'_FillValue': -9999.0,
'coordinates': 'time latitude longitude',
},
),
'variable_four': xr.DataArray(
np.array([]),
attrs={
'_FillValue': -9999.0,
'coordinates': 'time latitude longitude',
},
),
},
)
sample_datatree.to_netcdf(file_name, encoding=None)
return file_name
@fixture(scope='function')
def downloaded_netcdf4_file(temp_dir, sample_netcdf4_file) -> str:
"""Provide a downloaded file path more like Harmony.
This makes the downloaded filename distinct from the STAC Asset.href, so
that errors do not occur when trying to copy the file to its own location.
"""
downloaded_file_name = path_join(temp_dir, 'SHA256_scramble.nc')
move(sample_netcdf4_file, downloaded_file_name)
return downloaded_file_name
@fixture(scope='session')
def stac_asset_href() -> str:
"""Return a URL for a STAC Asset."""
return 'https://www.example.com/test_input.nc'
@fixture(scope='session')
def sample_stac(stac_asset_href) -> Catalog:
"""Create a sample SpatioTemporal Asset Catalog (STAC).
Spatial and temporal information is not used by the service, so default
values are used.
"""
catalog = Catalog(id='input catalog', description='test input')
item = Item(
id='input granule',
bbox=[-180, -90, 180, 90],
geometry=bbox_to_geometry([-180, -90, 180, 90]),
datetime=datetime(2000, 1, 2, 3, 4, 5),
properties={'props': 'None'},
)
item.add_asset(
'input data',
Asset(
stac_asset_href,
media_type='application/x-netcdf4',
roles=['data'],
),
)
catalog.add_item(item)
return catalog
@fixture(scope='session')
def sample_harmony_message() -> HarmonyMessage:
"""Create a sample Harmony message to send to service."""
return HarmonyMessage(
{
'accessToken': 'fake_token',
'callback': 'https://example.com/',
'sources': [{'collection': 'C1234-EEDTEST', 'shortName': 'TEST01'}],
'stagingLocation': 's3://bucket/staging-location',
'user': 'fakeuser',
}
)
@fixture(scope='function')
def sample_netcdf4_file_test02(temp_dir) -> str:
"""Create a sample NetCDF-4 file."""
file_name = path_join(temp_dir, 'test_input_02.nc')
sample_datatree = xr.DataTree(
dataset=xr.Dataset(
attrs={
'short_name': 'TEST02',
},
data_vars={
'x': xr.DataArray(
np.array([0, 1, 2]),
attrs={
'standard_name': 'projection_x_coordinate',
'_*subset_index_reference': 'EASE_column_index',
'grid_mapping': '/EASE2_north_polar_projection_36km',
},
dims=['x'],
),
'y': xr.DataArray(
np.array([0, 1, 2]),
attrs={
'standard_name': 'projection_y_coordinate',
'_*subset_index_reference': 'EASE_row_index',
'grid_mapping': '/EASE2_north_polar_projection_36km',
},
dims=['y'],
),
'variable_one': xr.DataArray(
np.ones((3, 3)),
attrs={
'standard_name': 'invalid_standard_name',
'grid_mapping': '/EASE2_variable_missing_geotransform',
},
dims=['y', 'x'],
),
'variable_two': xr.DataArray(
np.ones((3, 3)),
attrs={},
dims=['y', 'x'],
),
'EASE_column_index': xr.DataArray(
np.array([[5, 6, 7], [5, 6, 7], [5, 6, 7]]),
attrs={},
dims=['y', 'x'],
),
'EASE_row_index': xr.DataArray(
np.array([[5, 6, 7], [5, 6, 7], [5, 6, 7]]),
attrs={},
dims=['y', 'x'],
),
},
),
)
sample_datatree.to_netcdf(file_name, encoding=None)
return file_name
@fixture(scope='function')
def sample_varinfo_test02(
sample_netcdf4_file_test02, varinfo_config_file
) -> VarInfoFromNetCDF4:
"""Create sample VarInfoFromNetCDF4 instance."""
return VarInfoFromNetCDF4(
sample_netcdf4_file_test02, config_file=varinfo_config_file, short_name='TEST02'
)
@fixture(scope='function')
def sample_netcdf4_file_test03(temp_dir) -> str:
"""Create a sample NetCDF-4 file to test index ranges from history."""
file_name = path_join(temp_dir, 'test_input_03.nc')
sample_datatree = xr.DataTree(
dataset=xr.Dataset(
attrs={
'short_name': 'TEST03',
},
data_vars={
'x': xr.DataArray(
np.array([0, 1, 2]),
attrs={
'standard_name': 'projection_x_coordinate',
'grid_mapping': '/EASE2_north_polar_projection_36km',
},
dims=['x'],
),
'y': xr.DataArray(
np.array([0, 1, 2]),
attrs={
'standard_name': 'projection_y_coordinate',
'grid_mapping': '/EASE2_north_polar_projection_36km',
},
dims=['y'],
),
'test_variable': xr.DataArray(
np.ones((3, 3)),
attrs={},
dims=['y', 'x'],
),
},
),
)
sample_datatree.to_netcdf(file_name, encoding=None)
return file_name
@fixture(scope='function')
def sample_varinfo_test03(
sample_netcdf4_file_test03, varinfo_config_file
) -> VarInfoFromNetCDF4:
"""Create sample VarInfoFromNetCDF4 instance."""
return VarInfoFromNetCDF4(
sample_netcdf4_file_test03, config_file=varinfo_config_file, short_name='TEST03'
)
@fixture(scope='function')
def sample_netcdf4_file_test04(temp_dir) -> str:
"""Create a sample NetCDF-4 file for testing updating dimension variables."""
file_name = path_join(temp_dir, 'test_input_04.nc')
sample_datatree = xr.DataTree(xr.Dataset())
sample_datatree['/sub_group'] = xr.Dataset(
attrs={'short_name': 'TEST04'},
data_vars={
'x': xr.DataArray(np.array([0, 1, 2]), attrs={}, dims=['x']),
'y': xr.DataArray(np.array([0, 1, 2]), attrs={}, dims=['y']),
'variable_one': xr.DataArray(
np.ones((3, 3)),
attrs={},
dims=['y', 'x'],
),
},
)
sample_datatree.to_netcdf(file_name, encoding=None)
return file_name
@fixture(scope='function')
def sample_varinfo_test04(
sample_netcdf4_file, varinfo_config_file
) -> VarInfoFromNetCDF4:
"""Create sample VarInfoFromNetCDF4 instance."""
return VarInfoFromNetCDF4(
sample_netcdf4_file, config_file=varinfo_config_file, short_name='TEST04'
)
@fixture(scope='function')
def sample_netcdf4_file_test05(temp_dir) -> str:
"""Create a sample NetCDF-4 file for testing excluding variables."""
file_name = path_join(temp_dir, 'test_input_05.nc')
sample_datatree = xr.DataTree(
xr.Dataset(
data_vars={
'string_time_utc_seconds': xr.DataArray(['time1', 'time2', 'time3']),
'string_time_seconds': xr.DataArray(np.ones((3, 3))),
},
)
)
sample_datatree['/sub_group'] = xr.Dataset(
data_vars={
'string_time_utc_seconds': xr.DataArray(['time1', 'time2', 'time3']),
'string_time_seconds': xr.DataArray(np.ones((3, 3))),
},
)
sample_datatree['/sub_group/nested_group'] = xr.Dataset(
data_vars={
'string_time_utc_seconds': xr.DataArray(['time1', 'time2', 'time3']),
'string_time_seconds': xr.DataArray(np.ones((3, 3))),
},
)
sample_datatree.to_netcdf(file_name, encoding=None)
return file_name
@fixture(scope='function')
def expected_output_netcdf4_file_test05(temp_dir) -> str:
"""Create a sample NetCDF-4 file for testing excluding variables.
The generated file omits the 'string_time_utc_seconds' variable from each group.
This ensures that the metadata annotator correctly excludes these variables
from its output during testing.
"""
file_name = path_join(temp_dir, 'test_input_05.nc')
sample_datatree = xr.DataTree(
xr.Dataset(
data_vars={
'string_time_seconds': xr.DataArray(np.ones((3, 3))),
},
)
)
sample_datatree['/sub_group'] = xr.Dataset(
data_vars={
'string_time_seconds': xr.DataArray(np.ones((3, 3))),
},
)
sample_datatree['/sub_group/nested_group'] = xr.Dataset(
data_vars={
'string_time_seconds': xr.DataArray(np.ones((3, 3))),
},
)
sample_datatree.to_netcdf(file_name, encoding=None)
return file_name
@fixture(scope='function')
def sample_varinfo_test05(
sample_netcdf4_file_test05, varinfo_config_file
) -> VarInfoFromNetCDF4:
"""Create sample VarInfoFromNetCDF4 instance."""
return VarInfoFromNetCDF4(
sample_netcdf4_file_test05, config_file=varinfo_config_file, short_name='TEST05'
)