-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_annotate.py
More file actions
808 lines (683 loc) · 28.7 KB
/
test_annotate.py
File metadata and controls
808 lines (683 loc) · 28.7 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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
"""Tests for metadata_annotator.annotate.py."""
from unittest.mock import patch
import numpy as np
import pytest
import xarray as xr
from freezegun import freeze_time
from varinfo import VarInfoFromNetCDF4
from metadata_annotator.annotate import (
annotate_granule,
create_new_variable,
delete_variable,
get_dimension_variables,
get_geotransform_config,
get_grid_start_index,
get_matching_groups_and_variables,
get_referenced_variables,
get_spatial_dimension_type,
get_spatial_dimension_variables,
get_start_index_from_row_col_variable,
get_variables_to_delete,
is_exact_path,
is_excluded_science_variable,
is_temporary_attribute,
update_dimension_names,
update_dimension_variable_attributes,
update_dimension_variables,
update_group_and_variable_attributes,
update_metadata_attributes,
update_metadata_attributes_for_data_array,
update_spatial_dimension_values,
)
from metadata_annotator.exceptions import (
InvalidDimensionAttribute,
InvalidDimensionsConfiguration,
InvalidGridMappingReference,
InvalidSubsetIndexShape,
MissingDimensionAttribute,
MissingStartIndexConfiguration,
MissingSubsetIndexReference,
)
def test_is_exact_path_is_exact():
"""Returns True when the input is a path with no regular expression syntax."""
assert is_exact_path('/path/one')
def test_is_exact_path_wild_card():
"""Returns False when the input has '.*' to match multiple characters."""
assert not is_exact_path('/path/.*')
def test_is_exact_path_alternates():
"""Returns False when the input has '(one|two)', indicating alternates."""
assert not is_exact_path('/(path_one|path_two)/variable')
def test_update_metadata_attributes_variable(sample_netcdf4_file, sample_varinfo):
"""Check that attributes are added and updated."""
with xr.open_datatree(sample_netcdf4_file, decode_times=False) as test_datatree:
# First call the function under test:
update_metadata_attributes(test_datatree, '/variable_one', sample_varinfo)
# Check outputs from the function:
assert set(test_datatree['/variable_one'].attrs.keys()) == set(
['coordinates', 'grid_mapping', 'units']
)
# The units had no overrides, so should be unchanged.
assert (
test_datatree['/variable_one'].attrs['units']
== 'seconds since 2000-00-00T12:34:56'
)
# The coordinates was updated, so should be the value in the configuration file
assert (
test_datatree['/variable_one'].attrs['coordinates']
== 'time latitude longitude'
)
# grid_mapping is a new attribute added due to the configuration file
assert (
test_datatree['/variable_one'].attrs['grid_mapping']
== '/EASE2_north_polar_projection_36km'
)
def test_update_metadata_attributes_deletion(sample_netcdf4_file, sample_varinfo):
"""Check that an existing attribute can be deleted.
Also ensure that no error is raised when trying to delete an attribute that does
not exist. This tests also ensures that the function can handle nested
variables.
The `decode_coords` kwarg has been used to ensure `xarray` doesn't remove
the `coordinates` metadata attribute and use it to define internal `xarray`
objects instead.
"""
with xr.open_datatree(
sample_netcdf4_file, decode_times=False, decode_coords=False
) as test_datatree:
# First call the function under test:
update_metadata_attributes(
test_datatree, '/sub_group/variable_two', sample_varinfo
)
# Check outputs from the function:
# Only coordinates should remain as "delete" should have been removed
assert set(test_datatree['/sub_group/variable_two'].attrs.keys()) == set(
['coordinates']
)
# coordinates had no overrides, so should be unchanged
assert (
test_datatree['/sub_group/variable_two'].attrs['coordinates']
== 'time latitude longitude'
)
def test_update_metadata_attributes_ignore_temporary_variables(
sample_netcdf4_file, sample_varinfo
):
"""Ensure temporary attributes are not added to the variable."""
with xr.open_datatree(
sample_netcdf4_file, decode_times=False, decode_coords=False
) as test_datatree:
# First call the function under test:
update_metadata_attributes(
test_datatree, '/sub_group/variable_four', sample_varinfo
)
# Check outputs from the function:
# Only coordinates should remain as "_*temp" should be ignored
assert set(test_datatree['/sub_group/variable_four'].attrs.keys()) == set(
['coordinates']
)
# The coordinates was updated, so should be the value in the configuration file
assert (
test_datatree['/sub_group/variable_four'].attrs['coordinates']
== 'time latitude longitude'
)
@pytest.mark.parametrize(
'attr, expected',
[
('_*attr', True),
('attr', False),
('_attr', False),
('*attr', False),
('', False),
('_*', False),
],
)
def test_is_temporary_attribute(attr, expected):
"""Ensure correct evaluation of temporary attributes."""
assert is_temporary_attribute(attr) is expected
def test_get_matching_groups_and_variables(sample_varinfo):
"""Ensure variables matching the override rules, and missing variables are found."""
matching_items, missing_variables = get_matching_groups_and_variables(
sample_varinfo
)
# Each of the following matches a rule from the configuration file, trying
# to check: the root group, a sub group, a variable in the root group and
# a nested variable.
assert matching_items == set(
[
'/',
'/sub_group',
'/variable_one',
'/sub_group/variable_two',
'/sub_group/variable_four',
]
)
# The /EASE2_north_polar_projection_36km variable is specifically included in the
# configuration file to test missing variable behaviour.
assert missing_variables == set(
[
'/EASE2_north_polar_projection_36km',
]
)
@freeze_time('2000-01-02T03:04:05+00:00')
def test_annotate_granule(
sample_netcdf4_file,
expected_output_netcdf4_file,
temp_output_file_path,
varinfo_config_file,
mocker,
):
"""Confirm that a granule has all metadata updated as expected.
This test uses the "TEST01" collection short name to ensure that all rules
in the configuration file are applied to the input granule, and metadata
attributes are either added, updated or deleted.
"""
get_spatial_dimension_variables_mock = mocker.patch(
'metadata_annotator.annotate.get_spatial_dimension_variables'
)
get_spatial_dimension_variables_mock.return_value = set()
get_dimension_index_map_mock = mocker.patch(
'metadata_annotator.annotate.get_dimension_index_map'
)
get_dimension_index_map_mock.return_value = None
annotate_granule(
sample_netcdf4_file, temp_output_file_path, varinfo_config_file, 'TEST01'
)
with (
xr.open_datatree(temp_output_file_path, decode_times=False) as results_datatree,
xr.open_datatree(
expected_output_netcdf4_file, decode_times=False
) as expected_datatree,
):
assert results_datatree.identical(expected_datatree)
def test_annotate_granule_no_changes(
sample_netcdf4_file,
temp_output_file_path,
varinfo_config_file,
):
"""Confirm that a granule is unchanged if there are no overrides for it.
The collection short name is set to something that will not match any of
the configuration file overrides.
"""
annotate_granule(
sample_netcdf4_file,
temp_output_file_path,
varinfo_config_file,
'OTHER_SHORT_NAME',
)
with (
xr.open_datatree(sample_netcdf4_file, decode_times=False) as expected_datatree,
xr.open_datatree(temp_output_file_path, decode_times=False) as results_datatree,
):
assert results_datatree.identical(expected_datatree)
def test_annotate_granule_variable_exclusions_only(
sample_netcdf4_file_test05,
expected_output_netcdf4_file_test05,
temp_output_file_path,
varinfo_config_file,
mocker,
):
"""Confirm that a granule with only variable exclusion configuration is updated."""
_ = mocker.patch('metadata_annotator.annotate.update_history_metadata')
annotate_granule(
sample_netcdf4_file_test05,
temp_output_file_path,
varinfo_config_file,
'TEST05',
)
with (
xr.open_datatree(
expected_output_netcdf4_file_test05, decode_times=False
) as expected_datatree,
xr.open_datatree(temp_output_file_path, decode_times=False) as results_datatree,
):
assert results_datatree.identical(expected_datatree)
def test_annotate_granule_with_dimension_variable_updates(temp_output_file_path):
"""Confirm that a granule has all metadata updated as expected.
This test uses the sample SPL3FTP collection.
"""
annotate_granule(
'tests/data/SC_SPL3FTP_spatially_subsetted.nc4',
temp_output_file_path,
'metadata_annotator/earthdata_varinfo_config.json',
'SPL3FTP',
)
with (
xr.open_datatree(temp_output_file_path, decode_times=False) as datatree,
):
# Ensure that the attributes are updated.
assert (
set(datatree['/Freeze_Thaw_Retrieval_Data_Global'].dataset['y'].attrs)
== set(datatree['/Freeze_Thaw_Retrieval_Data_Global'].dataset['x'].attrs)
== set(
[
'axis',
'dimensions',
'grid_mapping',
'long_name',
'standard_name',
'type',
'units',
]
)
)
def test_update_group_and_variable_attributes() -> None:
"""Confirm the attributes are updated for existing variables.
This is based on configuration including pseudo dimension variables.
"""
with xr.open_datatree('tests/data/SC_SPL3FTP_spatially_subsetted.nc4') as datatree:
items_to_update = [
'/Freeze_Thaw_Retrieval_Data_Global/surface_flag',
]
granule_varinfo = VarInfoFromNetCDF4(
'tests/data/SC_SPL3FTP_spatially_subsetted.nc4',
short_name='SPL3FTP',
config_file='metadata_annotator/earthdata_varinfo_config.json',
)
update_group_and_variable_attributes(datatree, items_to_update, granule_varinfo)
# Check attributes expected are added.
assert all(
item
in datatree['/Freeze_Thaw_Retrieval_Data_Global/surface_flag'].attrs.keys()
for item in ['grid_mapping', 'dimensions']
)
# Check dimension renames are as expected.
assert set(
datatree['/Freeze_Thaw_Retrieval_Data_Global/surface_flag'].dims
) == set(['am_pm', 'y', 'x'])
def test_update_dimension_names() -> None:
"""Verify that the dimension names are renamed as expected."""
with xr.open_datatree('tests/data/SC_SPL3FTP_spatially_subsetted.nc4') as datatree:
variable_to_update = '/Freeze_Thaw_Retrieval_Data_Global/transition_direction'
datatree[variable_to_update] = datatree[variable_to_update].assign_attrs(
dimensions='y x'
)
update_dimension_names(datatree, variable_to_update)
# Check dimension renames are as expected.
assert set(
datatree['/Freeze_Thaw_Retrieval_Data_Global/transition_direction'].dims
) == set(['y', 'x'])
# Check for incorrect dimensions list
datatree[variable_to_update] = datatree[variable_to_update].assign_attrs(
dimensions='am_pm y x'
)
with pytest.raises(InvalidDimensionsConfiguration):
update_dimension_names(datatree, variable_to_update)
def test_create_new_variable() -> None:
"""Test if a new variable is successfully created."""
with xr.open_datatree('tests/data/SC_SPL3FTP_spatially_subsetted.nc4') as datatree:
variable_to_create = '/EASE2_global_projection_36km'
granule_varinfo = VarInfoFromNetCDF4(
'tests/data/SC_SPL3FTP_spatially_subsetted.nc4',
short_name='SPL3FTP',
config_file='metadata_annotator/earthdata_varinfo_config.json',
)
create_new_variable(datatree, variable_to_create, granule_varinfo)
# Check if the new variable is created.
assert 'EASE2_global_projection_36km' in datatree['/'].data_vars
# Check if attributes are updated for the variable.
assert set(datatree['/EASE2_global_projection_36km'].attrs.keys()) == set(
[
'false_easting',
'false_northing',
'grid_mapping_name',
'longitude_of_central_meridian',
'standard_parallel',
'inverse_flattening',
'semi_minor_axis',
'semi_major_axis',
'horizontal_datum_name',
]
)
def test_get_dimension_variables() -> set[str]:
"""Ensure return of dimension variables."""
with xr.open_datatree('tests/data/SC_SPL3FTP_spatially_subsetted.nc4') as datatree:
dimension_variables = get_dimension_variables(datatree)
assert dimension_variables == set(
[
'/Freeze_Thaw_Retrieval_Data_Global/dim0',
'/Freeze_Thaw_Retrieval_Data_Global/dim1',
'/Freeze_Thaw_Retrieval_Data_Global/dim2',
]
)
def test_update_dimension_variable_attributes() -> None:
"""Ensure attributes of a dimension variable are updated as expected."""
with xr.open_datatree('tests/data/SC_SPL3FTP_spatially_subsetted.nc4') as datatree:
granule_varinfo = VarInfoFromNetCDF4(
'tests/data/SC_SPL3FTP_spatially_subsetted.nc4',
short_name='SPL3FTP',
config_file='metadata_annotator/earthdata_varinfo_config.json',
)
# Rename the dim variables.
da = datatree['/Freeze_Thaw_Retrieval_Data_Global/surface_flag']
renamed_da = da.rename({'dim0': 'am_pm', 'dim1': 'y', 'dim2': 'x'})
datatree['/Freeze_Thaw_Retrieval_Data_Global/surface_flag'] = renamed_da
update_dimension_variable_attributes(
datatree, '/Freeze_Thaw_Retrieval_Data_Global/y', granule_varinfo
)
# Ensure that the attributes are updated.
assert set(
datatree['/Freeze_Thaw_Retrieval_Data_Global'].dataset['y'].attrs
) == set(
[
'axis',
'dimensions',
'grid_mapping',
'long_name',
'standard_name',
'type',
'units',
]
)
def test_update_metadata_attributes_for_data_array() -> None:
"""Update the metadata attributes on the supplied group or variable.
The attributes are updated on the data array based on the metadata
overrides matched by earthdata-varinfo for the requested path.
"""
with xr.open_datatree('tests/data/SC_SPL3FTP_spatially_subsetted.nc4') as datatree:
granule_varinfo = VarInfoFromNetCDF4(
'tests/data/SC_SPL3FTP_spatially_subsetted.nc4',
short_name='SPL3FTP',
config_file='metadata_annotator/earthdata_varinfo_config.json',
)
# Rename the dimension variables.
da = datatree['/Freeze_Thaw_Retrieval_Data_Global/surface_flag']
renamed_da = da.rename({'dim0': 'am_pm', 'dim1': 'y', 'dim2': 'x'})
datatree['/Freeze_Thaw_Retrieval_Data_Global/surface_flag'] = renamed_da
ds = xr.Dataset(datatree['/Freeze_Thaw_Retrieval_Data_Global'])
da_dim = ds['x']
update_metadata_attributes_for_data_array(
da_dim, '/Freeze_Thaw_Retrieval_Data_Global/x', granule_varinfo
)
# Ensure that the expected attributes are updated.
assert set(da_dim.attrs) == set(
[
'axis',
'dimensions',
'grid_mapping',
'long_name',
'standard_name',
'type',
'units',
]
)
def test_update_spatial_dimension_values(
sample_netcdf4_file_test02, sample_varinfo_test02
) -> None:
"""Ensure spatial dimension values are updated as expected."""
with xr.open_datatree(
sample_netcdf4_file_test02, decode_times=False
) as test_datatree:
variables = {'/x', '/y'}
update_spatial_dimension_values(test_datatree, variables, sample_varinfo_test02)
expected_x_result = np.array([-8802000, -8766000, -8730001], dtype=np.float64)
expected_y_result = np.array([8802000, 8766000, 8730000], dtype=np.float64)
assert np.allclose(test_datatree['x'], expected_x_result)
assert np.allclose(test_datatree['y'], expected_y_result)
def test_update_spatial_dimension_values_missing_dimension(
sample_netcdf4_file_test02, sample_varinfo_test02
) -> None:
"""Ensure exception is raised if dimension variable is missing."""
with xr.open_datatree(
sample_netcdf4_file_test02, decode_times=False
) as test_datatree:
with pytest.raises(Exception):
variables = {'/missing_dimension'}
update_spatial_dimension_values(
test_datatree, variables, sample_varinfo_test02
)
def test_get_spatial_dimension_variables(sample_netcdf4_file_test02) -> None:
"""Ensure only spatial variable dimensions are returned."""
with xr.open_datatree(
sample_netcdf4_file_test02, decode_times=False
) as test_datatree:
variables = {'/x', '/y', '/variable_two'}
assert get_spatial_dimension_variables(test_datatree, variables) == {'/x', '/y'}
def test_get_spatial_dimension_variables_no_matches(sample_netcdf4_file_test02) -> None:
"""Ensure an empty set is returned when no spatial dimensions are present."""
with xr.open_datatree(
sample_netcdf4_file_test02, decode_times=False
) as test_datatree:
variables = {'/variable_two'}
assert get_spatial_dimension_variables(test_datatree, variables) == set()
def test_get_grid_start_index_uses_subset_index_reference(
sample_netcdf4_file_test02, sample_varinfo_test02
) -> None:
"""Ensure the expected grid start index is returned for a given dimension."""
with xr.open_datatree(
sample_netcdf4_file_test02, decode_times=False
) as test_datatree:
assert (
get_grid_start_index(test_datatree, None, '/x', sample_varinfo_test02) == 5
)
def test_get_grid_start_index_uses_history_subset_index_ranges(
sample_netcdf4_file_test03, sample_varinfo_test03
) -> None:
"""Ensure the expected grid start index is returned for a given dimension."""
with xr.open_datatree(
sample_netcdf4_file_test03, decode_times=False
) as test_datatree:
assert get_grid_start_index(test_datatree, {}, '/x', sample_varinfo_test03) == 0
assert (
get_grid_start_index(test_datatree, {'/y': 10}, '/y', sample_varinfo_test03)
== 10
)
def test_get_grid_start_index_missing_configuration(
sample_netcdf4_file_test02, sample_varinfo_test02
) -> None:
"""Ensure the expected exception is raised when required configuration is missing.
The method used to determine the grid offset index is determined based off the
dimension's attributes configured in earthdata-varinfo. Currently, the attribute
'subset_index_reference' is the only supported attribute. If it is missing, the
'MissingStartIndexConfiguration' should be raised.
"""
with xr.open_datatree(
sample_netcdf4_file_test02, decode_times=False
) as test_datatree:
with pytest.raises(MissingStartIndexConfiguration):
get_grid_start_index(
test_datatree, None, '/variable_two', sample_varinfo_test02
)
def test_get_start_index_from_row_col_variable(sample_netcdf4_file_test02) -> None:
"""Ensure the expected start index is returned for a given row/col variable."""
with xr.open_datatree(
sample_netcdf4_file_test02, decode_times=False
) as test_datatree:
assert (
get_start_index_from_row_col_variable(test_datatree, 'EASE_column_index')
== 5
)
def test_get_start_index_from_row_col_variable_missing_reference(
sample_netcdf4_file_test02,
) -> None:
"""Ensure the expected exception is raised when the index reference is invalid."""
with xr.open_datatree(
sample_netcdf4_file_test02, decode_times=False
) as test_datatree:
with pytest.raises(MissingSubsetIndexReference):
get_start_index_from_row_col_variable(test_datatree, 'missing_variable')
def test_get_start_index_from_row_col_variable_invalid_index_shape(
sample_netcdf4_file_test02,
) -> None:
"""Ensure the expected exception is raised when index variable shape is invalid."""
with xr.open_datatree(
sample_netcdf4_file_test02, decode_times=False
) as test_datatree:
with pytest.raises(InvalidSubsetIndexShape):
get_start_index_from_row_col_variable(test_datatree, 'x')
def test_get_spatial_dimension_type(sample_netcdf4_file_test02) -> None:
"""Ensure the correct spatial dimension type is returned."""
with xr.open_datatree(
sample_netcdf4_file_test02, decode_times=False
) as test_datatree:
assert get_spatial_dimension_type(test_datatree['x']) == 'x'
assert get_spatial_dimension_type(test_datatree['y']) == 'y'
def test_get_spatial_dimension_type_invalid_standard_name(
sample_netcdf4_file_test02,
) -> None:
"""Ensure an exception is raised when an invalid standard_name is encountered."""
with xr.open_datatree(
sample_netcdf4_file_test02, decode_times=False
) as test_datatree:
with pytest.raises(InvalidDimensionAttribute):
get_spatial_dimension_type(test_datatree['variable_one'])
def test_get_spatial_dimension_type_missing_standard_name(
sample_netcdf4_file_test02,
) -> None:
"""Ensure an exception is raised when a standard_name attribute is missing."""
with xr.open_datatree(
sample_netcdf4_file_test02, decode_times=False
) as test_datatree:
with pytest.raises(MissingDimensionAttribute):
get_spatial_dimension_type(test_datatree['variable_two'])
def test_get_geotransform_config(
sample_netcdf4_file_test02, sample_varinfo_test02
) -> None:
"""Ensure the expected geotransform list is returned."""
with xr.open_datatree(
sample_netcdf4_file_test02, decode_times=False
) as test_datatree:
expected_geotransform = [-9000000, 36000, 0, 9000000, 0, -36000]
assert (
get_geotransform_config(test_datatree['x'], sample_varinfo_test02)
== expected_geotransform
)
def test_get_geotransform_config_missing_grid_mapping_reference(
sample_netcdf4_file_test02, sample_varinfo_test02
) -> None:
"""Ensure the expected geotransform list is returned."""
with xr.open_datatree(
sample_netcdf4_file_test02, decode_times=False
) as test_datatree:
with pytest.raises(MissingDimensionAttribute):
get_geotransform_config(
test_datatree['variable_two'], sample_varinfo_test02
)
def test_get_geotransform_config_invalid_grid_mapping_reference(
sample_varinfo_test02,
) -> None:
"""Ensure an exception is raised when there is an invalid grid_mapping reference."""
data_array = xr.DataArray([], attrs={'grid_mapping': 'fake_grid_mapping_variable'})
with pytest.raises(InvalidGridMappingReference):
get_geotransform_config(data_array, sample_varinfo_test02)
def test_get_geotransform_config_missing_master_geotransform(
sample_netcdf4_file_test02, sample_varinfo_test02
) -> None:
"""Ensure an exception is raised when master_geotransform attribute is missing."""
with xr.open_datatree(
sample_netcdf4_file_test02, decode_times=False
) as test_datatree:
with pytest.raises(MissingDimensionAttribute):
get_geotransform_config(
test_datatree['variable_one'], sample_varinfo_test02
)
def test_get_referenced_variables(sample_varinfo_test02):
"""Ensure the expected referenced variable set is returned."""
expected_result = {
'/EASE2_north_polar_projection_36km',
'/EASE2_variable_missing_geotransform',
'/ancillary_variable_one',
'/ancillary_variable_two',
}
assert (
get_referenced_variables(
sample_varinfo_test02, ['grid_mapping', 'ancillary_variables']
)
== expected_result
)
def test_update_dimension_variables(sample_netcdf4_file_test04, sample_varinfo_test04):
"""Ensure the dimension variables are updated correctly."""
expected_x_attributes = {
'standard_name': 'projection_x_coordinate',
'long_name': 'x coordinate of projection',
'dimensions': 'x',
'axis': 'X',
'units': 'm',
'type': 'float64',
'grid_mapping': '/EASE2_north_polar_projection_3km',
}
expected_y_attributes = {
'standard_name': 'projection_y_coordinate',
'long_name': 'y coordinate of projection',
'dimensions': 'y',
'axis': 'Y',
'units': 'm',
'type': 'float64',
'grid_mapping': '/EASE2_north_polar_projection_3km',
}
expected_x_values = np.array([-8998500.0, -8995500.0, -8992500.0], dtype=np.float64)
expected_y_values = np.array([8998500.0, 8995500.0, 8992500.0], dtype=np.float64)
with patch(
'metadata_annotator.annotate.get_dimension_index_map',
return_value={'/sub_group/y': 0, '/sub_group/x': 0},
):
with xr.open_datatree(
sample_netcdf4_file_test04, decode_times=False
) as test_datatree:
dimension_variables = {'/sub_group/x', '/sub_group/y'}
update_dimension_variables(
test_datatree, dimension_variables, sample_varinfo_test04
)
assert (
test_datatree['sub_group'].dataset['x'].attrs == expected_x_attributes
)
assert (
test_datatree['sub_group'].dataset['y'].attrs == expected_y_attributes
)
assert np.allclose(
test_datatree['sub_group'].dataset['x'], expected_x_values
)
assert np.allclose(
test_datatree['sub_group'].dataset['y'], expected_y_values
)
def test_get_variables_to_delete(sample_varinfo_test05):
"""Ensure correct list of variables to delete is obtained."""
expected_result = set(
[
'/string_time_utc_seconds',
'/sub_group/string_time_utc_seconds',
'/sub_group/nested_group/string_time_utc_seconds',
]
)
assert set(get_variables_to_delete(sample_varinfo_test05)) == expected_result
def test_is_excluded_science_variable(sample_varinfo_test05):
"""Ensure excluded science variables are determined correctly."""
assert is_excluded_science_variable(
sample_varinfo_test05, '/string_time_utc_seconds'
)
assert is_excluded_science_variable(
sample_varinfo_test05, '/sub_group/string_time_utc_seconds'
)
assert is_excluded_science_variable(
sample_varinfo_test05, '/sub_group/nested_group/string_time_utc_seconds'
)
assert not is_excluded_science_variable(
sample_varinfo_test05, '/string_time_seconds'
)
assert not is_excluded_science_variable(
sample_varinfo_test05, '/sub_group/string_time_seconds'
)
assert not is_excluded_science_variable(
sample_varinfo_test05, '/sub_group/nested/string_time_seconds'
)
@pytest.mark.parametrize(
'group_name',
[
'/',
'/sub_group',
'/sub_group/nested_group',
],
)
def test_delete_variable(sample_netcdf4_file_test05, group_name):
"""Ensure correct variables are deleted from the datatree."""
with xr.open_datatree(
sample_netcdf4_file_test05, decode_times=False
) as test_datatree:
variable_name = 'string_time_utc_seconds'
if group_name == '/':
full_path = f'/{variable_name}'
else:
full_path = f'{group_name}/{variable_name}'
# check that variable is in datatree before deletion
assert variable_name in test_datatree[group_name].ds.data_vars
initial_var_count = len(test_datatree[group_name].ds.data_vars)
delete_variable(test_datatree, full_path)
assert variable_name not in test_datatree[group_name].ds.data_vars
assert len(test_datatree[group_name].ds.data_vars) == initial_var_count - 1