/
test_xarray.py
1493 lines (1193 loc) · 58 KB
/
test_xarray.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
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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2018,2019 MetPy Developers.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""Test the operation of MetPy's XArray accessors."""
from collections import OrderedDict
from unittest.mock import patch, PropertyMock
import numpy as np
import pyproj
import pytest
import xarray as xr
from metpy.plots.mapping import CFProjection
from metpy.testing import (assert_almost_equal, assert_array_almost_equal, assert_array_equal,
get_test_data)
from metpy.units import DimensionalityError, units
from metpy.xarray import (
add_grid_arguments_from_xarray,
add_vertical_dim_from_xarray,
check_axis,
check_matching_coordinates,
grid_deltas_from_dataarray,
preprocess_and_wrap
)
@pytest.fixture
def test_ds():
"""Provide an xarray dataset for testing."""
return xr.open_dataset(get_test_data('narr_example.nc', as_file_obj=False))
@pytest.fixture
def test_ds_generic():
"""Provide a generic-coordinate dataset for testing."""
return xr.DataArray(np.zeros((1, 3, 3, 5, 5)),
coords={
'a': xr.DataArray(np.arange(1), dims='a'),
'b': xr.DataArray(np.arange(3), dims='b'),
'c': xr.DataArray(np.arange(3), dims='c'),
'd': xr.DataArray(np.arange(5), dims='d'),
'e': xr.DataArray(np.arange(5), dims='e')
}, dims=['a', 'b', 'c', 'd', 'e'], name='test').to_dataset()
@pytest.fixture
def test_var(test_ds):
"""Provide a standard, parsed, variable for testing."""
return test_ds.metpy.parse_cf('Temperature')
@pytest.fixture
def test_var_multidim_full(test_ds):
"""Provide a variable with x/y coords and multidimensional lat/lon auxiliary coords."""
return (test_ds[{'isobaric': [6, 12], 'y': [95, 96], 'x': [122, 123]}]
.squeeze().set_coords(['lat', 'lon'])['Temperature'])
@pytest.fixture
def test_var_multidim_no_xy(test_var_multidim_full):
"""Provide a variable with multidimensional lat/lon coords but without x/y coords."""
return test_var_multidim_full.drop_vars(['y', 'x'])
def test_projection(test_var, ccrs):
"""Test getting the proper projection out of the variable."""
crs = test_var.metpy.crs
assert crs['grid_mapping_name'] == 'lambert_conformal_conic'
assert isinstance(test_var.metpy.cartopy_crs, ccrs.LambertConformal)
def test_pyproj_projection(test_var):
"""Test getting the proper pyproj projection out of the variable."""
proj = test_var.metpy.pyproj_crs
assert isinstance(proj, pyproj.CRS)
assert proj.coordinate_operation.method_name == 'Lambert Conic Conformal (1SP)'
def test_no_projection(test_ds):
"""Test getting the crs attribute when not available produces a sensible error."""
var = test_ds.lat
with pytest.raises(AttributeError) as exc:
var.metpy.crs
assert 'not available' in str(exc.value)
def test_globe(test_var, ccrs):
"""Test getting the globe belonging to the projection."""
globe = test_var.metpy.cartopy_globe
assert globe.to_proj4_params() == OrderedDict([('ellps', 'sphere'),
('a', 6367470.21484375),
('b', 6367470.21484375)])
assert isinstance(globe, ccrs.Globe)
def test_geodetic(test_var, ccrs):
"""Test getting the Geodetic CRS for the projection."""
geodetic = test_var.metpy.cartopy_geodetic
assert isinstance(geodetic, ccrs.Geodetic)
def test_unit_array(test_var):
"""Test unit handling through the accessor."""
arr = test_var.metpy.unit_array
assert isinstance(arr, units.Quantity)
assert arr.units == units.kelvin
def test_units(test_var):
"""Test the units property on the accessor."""
assert test_var.metpy.units == units.kelvin
def test_units_data(test_var):
"""Test units property fetching does not touch variable.data."""
with patch.object(xr.Variable, 'data', new_callable=PropertyMock) as mock_data_property:
test_var.metpy.units
mock_data_property.assert_not_called()
def test_units_percent():
"""Test that '%' is handled as 'percent'."""
test_var_percent = xr.open_dataset(
get_test_data('irma_gfs_example.nc',
as_file_obj=False))['Relative_humidity_isobaric']
assert test_var_percent.metpy.units == units.percent
def test_magnitude_with_quantity(test_var):
"""Test magnitude property on accessor when data is a quantity."""
assert isinstance(test_var.metpy.magnitude, np.ndarray)
np.testing.assert_array_almost_equal(test_var.metpy.magnitude, np.asarray(test_var.values))
def test_magnitude_without_quantity(test_ds_generic):
"""Test magnitude property on accessor when data is not a quantity."""
assert isinstance(test_ds_generic['test'].data, np.ndarray)
np.testing.assert_array_equal(
test_ds_generic['test'].metpy.magnitude,
np.asarray(test_ds_generic['test'].values)
)
def test_convert_units(test_var):
"""Test conversion of units."""
result = test_var.metpy.convert_units('degC')
# Check that units are updated without modifying original
assert result.metpy.units == units.degC
assert test_var.metpy.units == units.kelvin
# Make sure we now get an array back with properly converted values
assert_almost_equal(result[0, 0, 0, 0], 18.44 * units.degC, 2)
def test_convert_coordinate_units(test_ds_generic):
"""Test conversion of coordinate units."""
result = test_ds_generic['test'].metpy.convert_coordinate_units('b', 'percent')
assert result['b'].data[1] == 100.
assert result['b'].metpy.units == units.percent
def test_quantify(test_ds_generic):
"""Test quantify method for converting data to Quantity."""
original = test_ds_generic['test'].values
result = test_ds_generic['test'].metpy.quantify()
assert isinstance(result.data, units.Quantity)
assert result.data.units == units.dimensionless
assert 'units' not in result.attrs
np.testing.assert_array_almost_equal(result.data, units.Quantity(original))
def test_dequantify():
"""Test dequantify method for converting data away from Quantity."""
original = xr.DataArray(units.Quantity([280, 290, 300], 'K'),
attrs={'standard_name': 'air_temperature'})
result = original.metpy.dequantify()
assert isinstance(result.data, np.ndarray)
assert result.attrs['units'] == 'kelvin'
np.testing.assert_array_almost_equal(result.data, original.data.magnitude)
assert result.attrs['standard_name'] == 'air_temperature'
def test_dataset_quantify(test_ds_generic):
"""Test quantify method for converting data to Quantity on Datasets."""
result = test_ds_generic.metpy.quantify()
assert isinstance(result['test'].data, units.Quantity)
assert result['test'].data.units == units.dimensionless
assert 'units' not in result['test'].attrs
np.testing.assert_array_almost_equal(
result['test'].data,
units.Quantity(test_ds_generic['test'].data)
)
def test_dataset_dequantify():
"""Test dequantify method for converting data away from Quantity on Datasets."""
original = xr.Dataset({
'test': ('x', units.Quantity([280, 290, 300], 'K')),
'x': np.arange(3)
})
result = original.metpy.dequantify()
assert isinstance(result['test'].data, np.ndarray)
assert result['test'].attrs['units'] == 'kelvin'
np.testing.assert_array_almost_equal(result['test'].data, original['test'].data.magnitude)
def test_radian_projection_coords():
"""Test fallback code for radian units in projection coordinate variables."""
proj = xr.DataArray(0, attrs={'grid_mapping_name': 'geostationary',
'perspective_point_height': 3})
x = xr.DataArray(np.arange(3),
attrs={'standard_name': 'projection_x_coordinate', 'units': 'radians'})
y = xr.DataArray(np.arange(2),
attrs={'standard_name': 'projection_y_coordinate', 'units': 'radians'})
data = xr.DataArray(np.arange(6).reshape(2, 3), coords=(y, x), dims=('y', 'x'),
attrs={'grid_mapping': 'fixedgrid_projection'})
ds = xr.Dataset({'data': data, 'fixedgrid_projection': proj})
# Check that the coordinates in this case are properly converted
data_var = ds.metpy.parse_cf('data')
assert data_var.coords['x'].metpy.unit_array[1] == 3 * units.meter
assert data_var.coords['y'].metpy.unit_array[1] == 3 * units.meter
def test_missing_grid_mapping_valid():
"""Test falling back to implicit lat/lon projection when valid."""
lon = xr.DataArray(-np.arange(3),
attrs={'standard_name': 'longitude', 'units': 'degrees_east'})
lat = xr.DataArray(np.arange(2),
attrs={'standard_name': 'latitude', 'units': 'degrees_north'})
data = xr.DataArray(np.arange(6).reshape(2, 3), coords=(lat, lon), dims=('y', 'x'))
ds = xr.Dataset({'data': data})
data_var = ds.metpy.parse_cf('data')
assert (
'metpy_crs' in data_var.coords
and data_var.metpy.crs['grid_mapping_name'] == 'latitude_longitude'
)
def test_missing_grid_mapping_invalid(test_var_multidim_no_xy):
"""Test not falling back to implicit lat/lon projection when invalid."""
data_var = test_var_multidim_no_xy.to_dataset(name='data').metpy.parse_cf('data')
assert 'metpy_crs' not in data_var.coords
def test_missing_grid_mapping_var(caplog):
"""Test behavior when we can't find the variable pointed to by grid_mapping."""
x = xr.DataArray(np.arange(3),
attrs={'standard_name': 'projection_x_coordinate', 'units': 'radians'})
y = xr.DataArray(np.arange(2),
attrs={'standard_name': 'projection_y_coordinate', 'units': 'radians'})
data = xr.DataArray(np.arange(6).reshape(2, 3), coords=(y, x), dims=('y', 'x'),
attrs={'grid_mapping': 'fixedgrid_projection'})
ds = xr.Dataset({'data': data})
ds.metpy.parse_cf('data') # Should log a warning
for record in caplog.records:
assert record.levelname == 'WARNING'
assert 'Could not find' in caplog.text
def test_parsecf_crs():
"""Test calling `parse_cf` with the metpy_crs variable."""
ds = xr.Dataset({'metpy_crs': xr.DataArray(1)})
with pytest.warns(UserWarning, match='Attempting to parse metpy_crs'):
ds.metpy.parse_cf('metpy_crs')
def test_parsecf_existing_scalar_crs():
"""Test calling `parse_cf` on a variable with an existing scalar metpy_crs coordinate."""
ds = xr.Dataset({'data': xr.DataArray(1, coords={'metpy_crs': 1})})
with pytest.warns(UserWarning, match='metpy_crs already present'):
ds.metpy.parse_cf('data')
def test_parsecf_existing_vector_crs():
"""Test calling `parse_cf` on a variable with an existing vector metpy_crs coordinate."""
ds = xr.Dataset({'data': xr.DataArray(1, dims=('metpy_crs',), coords=(np.ones(3),))})
with pytest.warns(UserWarning, match='metpy_crs already present'):
ds.metpy.parse_cf('data')
def test_preprocess_and_wrap_only_preprocessing():
"""Test xarray preprocessing and wrapping decorator for only preprocessing."""
data = xr.DataArray(np.ones(3), attrs={'units': 'km'})
data2 = xr.DataArray(np.ones(3), attrs={'units': 'm'})
@preprocess_and_wrap()
def func(a, b):
return a.to('m') + b
assert_array_equal(func(data, b=data2), np.array([1001, 1001, 1001]) * units.m)
def test_coordinates_basic_by_method(test_var):
"""Test that NARR example coordinates are like we expect using coordinates method."""
x, y, vertical, time = test_var.metpy.coordinates('x', 'y', 'vertical', 'time')
assert test_var['x'].identical(x)
assert test_var['y'].identical(y)
assert test_var['isobaric'].identical(vertical)
assert test_var['time'].identical(time)
def test_coordinates_basic_by_property(test_var):
"""Test that NARR example coordinates are like we expect using properties."""
assert test_var['x'].identical(test_var.metpy.x)
assert test_var['y'].identical(test_var.metpy.y)
assert test_var['isobaric'].identical(test_var.metpy.vertical)
assert test_var['time'].identical(test_var.metpy.time)
def test_coordinates_specified_by_name_with_dataset(test_ds_generic):
"""Test that we can manually specify the coordinates by name."""
data = test_ds_generic.metpy.parse_cf(coordinates={'time': 'a', 'vertical': 'b', 'y': 'c',
'x': 'd'})
x, y, vertical, time = data['test'].metpy.coordinates('x', 'y', 'vertical', 'time')
assert data['test']['d'].identical(x)
assert data['test']['c'].identical(y)
assert data['test']['b'].identical(vertical)
assert data['test']['a'].identical(time)
def test_coordinates_specified_by_dataarray_with_dataset(test_ds_generic):
"""Test that we can manually specify the coordinates by DataArray."""
data = test_ds_generic.metpy.parse_cf(coordinates={
'time': test_ds_generic['d'],
'vertical': test_ds_generic['a'],
'y': test_ds_generic['b'],
'x': test_ds_generic['c']
})
x, y, vertical, time = data['test'].metpy.coordinates('x', 'y', 'vertical', 'time')
assert data['test']['c'].identical(x)
assert data['test']['b'].identical(y)
assert data['test']['a'].identical(vertical)
assert data['test']['d'].identical(time)
def test_missing_coordinate_type(test_ds_generic):
"""Test that an AttributeError is raised when an axis/coordinate type is unavailable."""
data = test_ds_generic.metpy.parse_cf('test', coordinates={'vertical': 'e'})
with pytest.raises(AttributeError) as exc:
data.metpy.time
assert 'not available' in str(exc.value)
def test_assign_coordinates_not_overwrite(test_ds_generic):
"""Test that assign_coordinates does not overwrite past axis attributes."""
data = test_ds_generic.copy()
data['c'].attrs['axis'] = 'X'
data['test'] = data['test'].metpy.assign_coordinates({'y': data['c']})
assert data['c'].identical(data['test'].metpy.y)
assert data['c'].attrs['axis'] == 'X'
def test_resolve_axis_conflict_lonlat_and_xy(test_ds_generic):
"""Test _resolve_axis_conflict with both lon/lat and x/y coordinates."""
test_ds_generic['b'].attrs['_CoordinateAxisType'] = 'GeoX'
test_ds_generic['c'].attrs['_CoordinateAxisType'] = 'Lon'
test_ds_generic['d'].attrs['_CoordinateAxisType'] = 'GeoY'
test_ds_generic['e'].attrs['_CoordinateAxisType'] = 'Lat'
assert test_ds_generic['test'].metpy.x.name == 'b'
assert test_ds_generic['test'].metpy.y.name == 'd'
def test_resolve_axis_conflict_double_lonlat(test_ds_generic):
"""Test _resolve_axis_conflict with double lon/lat coordinates."""
test_ds_generic['b'].attrs['_CoordinateAxisType'] = 'Lat'
test_ds_generic['c'].attrs['_CoordinateAxisType'] = 'Lon'
test_ds_generic['d'].attrs['_CoordinateAxisType'] = 'Lat'
test_ds_generic['e'].attrs['_CoordinateAxisType'] = 'Lon'
with pytest.warns(UserWarning, match='More than one x coordinate'),\
pytest.raises(AttributeError):
test_ds_generic['test'].metpy.x
with pytest.warns(UserWarning, match='More than one y coordinate'),\
pytest.raises(AttributeError):
test_ds_generic['test'].metpy.y
def test_resolve_axis_conflict_double_xy(test_ds_generic):
"""Test _resolve_axis_conflict with double x/y coordinates."""
test_ds_generic['b'].attrs['standard_name'] = 'projection_x_coordinate'
test_ds_generic['c'].attrs['standard_name'] = 'projection_y_coordinate'
test_ds_generic['d'].attrs['standard_name'] = 'projection_x_coordinate'
test_ds_generic['e'].attrs['standard_name'] = 'projection_y_coordinate'
with pytest.warns(UserWarning, match='More than one x coordinate'),\
pytest.raises(AttributeError):
test_ds_generic['test'].metpy.x
with pytest.warns(UserWarning, match='More than one y coordinate'),\
pytest.raises(AttributeError):
test_ds_generic['test'].metpy.y
def test_resolve_axis_conflict_double_x_with_single_dim(test_ds_generic):
"""Test _resolve_axis_conflict with double x coordinate, but only one being a dim."""
test_ds_generic['e'].attrs['standard_name'] = 'projection_x_coordinate'
test_ds_generic.coords['f'] = ('e', np.linspace(0, 1, 5))
test_ds_generic['f'].attrs['standard_name'] = 'projection_x_coordinate'
assert test_ds_generic['test'].metpy.x.name == 'e'
def test_resolve_axis_conflict_double_vertical(test_ds_generic):
"""Test _resolve_axis_conflict with double vertical coordinates."""
test_ds_generic['b'].attrs['units'] = 'hPa'
test_ds_generic['c'].attrs['units'] = 'Pa'
with pytest.warns(UserWarning, match='More than one vertical coordinate'),\
pytest.raises(AttributeError):
test_ds_generic['test'].metpy.vertical
criterion_matches = [
('standard_name', 'time', 'time'),
('standard_name', 'model_level_number', 'vertical'),
('standard_name', 'atmosphere_hybrid_sigma_pressure_coordinate', 'vertical'),
('standard_name', 'geopotential_height', 'vertical'),
('standard_name', 'height_above_geopotential_datum', 'vertical'),
('standard_name', 'altitude', 'vertical'),
('standard_name', 'atmosphere_sigma_coordinate', 'vertical'),
('standard_name', 'height_above_reference_ellipsoid', 'vertical'),
('standard_name', 'height', 'vertical'),
('standard_name', 'atmosphere_sleve_coordinate', 'vertical'),
('standard_name', 'height_above_mean_sea_level', 'vertical'),
('standard_name', 'atmosphere_hybrid_height_coordinate', 'vertical'),
('standard_name', 'atmosphere_ln_pressure_coordinate', 'vertical'),
('standard_name', 'air_pressure', 'vertical'),
('standard_name', 'projection_y_coordinate', 'y'),
('standard_name', 'latitude', 'latitude'),
('standard_name', 'projection_x_coordinate', 'x'),
('standard_name', 'longitude', 'longitude'),
('_CoordinateAxisType', 'Time', 'time'),
('_CoordinateAxisType', 'Pressure', 'vertical'),
('_CoordinateAxisType', 'GeoZ', 'vertical'),
('_CoordinateAxisType', 'Height', 'vertical'),
('_CoordinateAxisType', 'GeoY', 'y'),
('_CoordinateAxisType', 'Lat', 'latitude'),
('_CoordinateAxisType', 'GeoX', 'x'),
('_CoordinateAxisType', 'Lon', 'longitude'),
('axis', 'T', 'time'),
('axis', 'Z', 'vertical'),
('axis', 'Y', 'y'),
('axis', 'X', 'x'),
('positive', 'up', 'vertical'),
('positive', 'down', 'vertical')
]
@pytest.mark.parametrize('test_tuple', criterion_matches)
def test_check_axis_criterion_match(test_ds_generic, test_tuple):
"""Test the variety of possibilities for check_axis in the criterion match."""
test_ds_generic['e'].attrs[test_tuple[0]] = test_tuple[1]
assert check_axis(test_ds_generic['e'], test_tuple[2])
unit_matches = [
('Pa', 'vertical'),
('hPa', 'vertical'),
('mbar', 'vertical'),
('degreeN', 'latitude'),
('degreesN', 'latitude'),
('degree_north', 'latitude'),
('degree_N', 'latitude'),
('degrees_north', 'latitude'),
('degrees_N', 'latitude'),
('degreeE', 'longitude'),
('degrees_east', 'longitude'),
('degree_east', 'longitude'),
('degreesE', 'longitude'),
('degree_E', 'longitude'),
('degrees_E', 'longitude')
]
@pytest.mark.parametrize('test_tuple', unit_matches)
def test_check_axis_unit_match(test_ds_generic, test_tuple):
"""Test the variety of possibilities for check_axis in the unit match."""
test_ds_generic['e'].attrs['units'] = test_tuple[0]
assert check_axis(test_ds_generic['e'], test_tuple[1])
regex_matches = [
('time', 'time'),
('time1', 'time'),
('time42', 'time'),
('Time', 'time'),
('TIME', 'time'),
('bottom_top', 'vertical'),
('sigma', 'vertical'),
('HGHT', 'vertical'),
('height', 'vertical'),
('Altitude', 'vertical'),
('depth', 'vertical'),
('isobaric', 'vertical'),
('isobaric1', 'vertical'),
('isobaric42', 'vertical'),
('lv_HTGL5', 'vertical'),
('PRES', 'vertical'),
('pressure', 'vertical'),
('pressure_difference_layer', 'vertical'),
('isothermal', 'vertical'),
('y', 'y'),
('Y', 'y'),
('lat', 'latitude'),
('latitude', 'latitude'),
('Latitude', 'latitude'),
('XLAT', 'latitude'),
('x', 'x'),
('X', 'x'),
('lon', 'longitude'),
('longitude', 'longitude'),
('Longitude', 'longitude'),
('XLONG', 'longitude')
]
@pytest.mark.parametrize('test_tuple', regex_matches)
def test_check_axis_regular_expression_match(test_ds_generic, test_tuple):
"""Test the variety of possibilities for check_axis in the regular expression match."""
data = test_ds_generic.rename({'e': test_tuple[0]})
assert check_axis(data[test_tuple[0]], test_tuple[1])
def test_narr_example_variable_without_grid_mapping(test_ds):
"""Test that NARR example is parsed correctly, with x/y coordinates scaled the same."""
data = test_ds.metpy.parse_cf()
# Make sure that x and y coordinates are parsed correctly, rather than having unequal
# scaling based on whether that variable has the grid_mapping attribute. This would
# otherwise double the coordinates's shapes since xarray tries to combine the coordinates
# with different scaling from differing units.
assert test_ds['x'].shape == data['lon'].metpy.x.shape
assert test_ds['y'].shape == data['lon'].metpy.y.shape
assert data['lon'].metpy.x.identical(data['Temperature'].metpy.x)
assert data['lon'].metpy.y.identical(data['Temperature'].metpy.y)
def test_coordinates_identical_true(test_ds_generic):
"""Test coordinates identical method when true."""
assert test_ds_generic['test'].metpy.coordinates_identical(test_ds_generic['test'])
def test_coordinates_identical_false_number_of_coords(test_ds_generic):
"""Test coordinates identical method when false due to number of coordinates."""
other_ds = test_ds_generic.drop_vars('e')
assert not test_ds_generic['test'].metpy.coordinates_identical(other_ds['test'])
def test_coordinates_identical_false_coords_mismatch(test_ds_generic):
"""Test coordinates identical method when false due to coordinates not matching."""
other_ds = test_ds_generic.copy()
other_ds['e'].attrs['units'] = 'meters'
assert not test_ds_generic['test'].metpy.coordinates_identical(other_ds['test'])
def test_check_matching_coordinates(test_ds_generic):
"""Test xarray coordinate checking decorator."""
other = test_ds_generic['test'].rename({'a': 'time'})
@check_matching_coordinates
def add(a, b):
return a + b
xr.testing.assert_identical(add(test_ds_generic['test'], test_ds_generic['test']),
test_ds_generic['test'] * 2)
with pytest.raises(ValueError):
add(test_ds_generic['test'], other)
def test_time_deltas():
"""Test the time_deltas attribute."""
ds = xr.open_dataset(get_test_data('irma_gfs_example.nc', as_file_obj=False))
time = ds['time1']
truth = 3 * np.ones(8) * units.hr
assert_array_almost_equal(time.metpy.time_deltas, truth)
def test_find_axis_name_integer(test_var):
"""Test getting axis name using the axis number identifier."""
assert test_var.metpy.find_axis_name(2) == 'y'
def test_find_axis_name_axis_type(test_var):
"""Test getting axis name using the axis type identifier."""
assert test_var.metpy.find_axis_name('vertical') == 'isobaric'
def test_find_axis_name_dim_coord_name(test_var):
"""Test getting axis name using the dimension coordinate name identifier."""
assert test_var.metpy.find_axis_name('isobaric') == 'isobaric'
def test_find_axis_name_bad_identifier(test_var):
"""Test getting axis name using the axis type identifier."""
with pytest.raises(ValueError) as exc:
test_var.metpy.find_axis_name('ens')
assert 'axis is not valid' in str(exc.value)
def test_find_axis_number_integer(test_var):
"""Test getting axis number using the axis number identifier."""
assert test_var.metpy.find_axis_number(2) == 2
def test_find_axis_number_axis_type(test_var):
"""Test getting axis number using the axis type identifier."""
assert test_var.metpy.find_axis_number('vertical') == 1
def test_find_axis_number_dim_coord_number(test_var):
"""Test getting axis number using the dimension coordinate name identifier."""
assert test_var.metpy.find_axis_number('isobaric') == 1
def test_find_axis_number_bad_identifier(test_var):
"""Test getting axis number using the axis type identifier."""
with pytest.raises(ValueError) as exc:
test_var.metpy.find_axis_number('ens')
assert 'axis is not valid' in str(exc.value)
def test_cf_parse_with_grid_mapping(test_var):
"""Test cf_parse dont delete grid_mapping attribute."""
assert test_var.grid_mapping == 'Lambert_Conformal'
def test_data_array_loc_get_with_units(test_var):
"""Test the .loc indexer on the metpy accessor."""
truth = test_var.loc[:, 850.]
assert truth.identical(test_var.metpy.loc[:, 8.5e4 * units.Pa])
def test_data_array_loc_set_with_units(test_var):
"""Test the .loc indexer on the metpy accessor for setting."""
temperature = test_var.copy()
temperature.metpy.loc[:, 8.5e4 * units.Pa] = np.nan
assert np.isnan(temperature.loc[:, 850.]).all()
assert not np.isnan(temperature.loc[:, 700.]).any()
def test_data_array_loc_with_ellipsis(test_var):
"""Test the .loc indexer using multiple Ellipses to verify expansion behavior."""
truth = test_var[:, :, -1, :]
assert truth.identical(test_var.metpy.loc[..., 711.3653535503963 * units.km, ...])
def test_data_array_loc_non_tuple(test_var):
"""Test the .loc indexer with a non-tuple indexer."""
truth = test_var[-1]
assert truth.identical(test_var.metpy.loc['1987-04-04T18:00'])
def test_data_array_loc_too_many_indices(test_var):
"""Test the .loc indexer when too many indices are given."""
with pytest.raises(IndexError):
test_var.metpy.loc[:, 8.5e4 * units.Pa, :, :, :]
def test_data_array_sel_dict_with_units(test_var):
"""Test .sel on the metpy accessor with dictionary."""
truth = test_var.squeeze().loc[500.]
assert truth.identical(test_var.metpy.sel({'time': '1987-04-04T18:00:00',
'isobaric': 5e4 * units.Pa}))
def test_data_array_sel_kwargs_with_units(test_var):
"""Test .sel on the metpy accessor with kwargs and axis type."""
truth = test_var.loc[:, 500.][..., 122]
selection = (
test_var.metpy
.sel(vertical=5e4 * units.Pa, x=-16.569 * units.km, tolerance=1., method='nearest')
.metpy
.assign_coordinates(None)
)
assert truth.identical(selection)
def test_dataset_loc_with_units(test_ds):
"""Test .loc on the metpy accessor for Datasets using slices."""
truth = test_ds[{'isobaric': slice(6, 17)}]
assert truth.identical(test_ds.metpy.loc[{'isobaric': slice(8.5e4 * units.Pa,
5e4 * units.Pa)}])
def test_dataset_sel_kwargs_with_units(test_ds):
"""Test .sel on the metpy accessor for Datasets with kwargs."""
truth = test_ds[{'time': 0, 'y': 50, 'x': 122}]
assert truth.identical(test_ds.metpy.sel(time='1987-04-04T18:00:00', y=-1.464e6 * units.m,
x=-17. * units.km, tolerance=1.,
method='nearest'))
def test_dataset_sel_non_dict_pos_arg(test_ds):
"""Test that .sel errors when first positional argument is not a dict."""
with pytest.raises(ValueError) as exc:
test_ds.metpy.sel('1987-04-04T18:00:00')
assert 'must be a dictionary' in str(exc.value)
def test_dataset_sel_mixed_dict_and_kwarg(test_ds):
"""Test that .sel errors when dict positional argument and kwargs are mixed."""
with pytest.raises(ValueError) as exc:
test_ds.metpy.sel({'isobaric': slice(8.5e4 * units.Pa, 5e4 * units.Pa)},
time='1987-04-04T18:00:00')
assert 'cannot specify both keyword and positional arguments' in str(exc.value)
def test_dataset_loc_without_dict(test_ds):
"""Test that .metpy.loc for Datasets raises error when used with a non-dict."""
with pytest.raises(TypeError):
test_ds.metpy.loc[:, 700 * units.hPa]
def test_dataset_parse_cf_keep_attrs(test_ds):
"""Test that .parse_cf() does not remove attributes on the parsed dataset."""
parsed_ds = test_ds.metpy.parse_cf()
assert parsed_ds.attrs # Must be non-empty
assert parsed_ds.attrs == test_ds.attrs # Must match
def test_check_axis_with_bad_unit(test_ds_generic):
"""Test that check_axis does not raise an exception when provided a bad unit."""
var = test_ds_generic['e']
var.attrs['units'] = 'nondimensional'
assert not check_axis(var, 'x', 'y', 'vertical', 'time')
def test_dataset_parse_cf_varname_list(test_ds):
"""Test that .parse_cf() returns correct subset of dataset when given list of vars."""
full_ds = test_ds.copy().metpy.parse_cf()
partial_ds = test_ds.metpy.parse_cf(['u_wind', 'v_wind'])
assert full_ds[['u_wind', 'v_wind']].identical(partial_ds)
def test_coordinate_identification_shared_but_not_equal_coords():
"""Test that non-shared coords are not skipped after parsing shared coords.
See GH Issue #1124.
"""
# Create minimal dataset
temperature = xr.DataArray([[8, 4], [13, 12]], name='temperature',
dims=('isobaric1', 'x'),
coords={
'isobaric1': xr.DataArray([700, 850],
name='isobaric1',
dims='isobaric1',
attrs={'units': 'hPa',
'axis': 'Z'}),
'x': xr.DataArray([0, 450], name='x', dims='x',
attrs={'units': 'km', 'axis': 'X'})},
attrs={'units': 'degC'})
u = xr.DataArray([[30, 20], [10, 10]], name='u', dims=('isobaric2', 'x'),
coords={
'isobaric2': xr.DataArray([500, 850], name='isobaric2',
dims='isobaric2',
attrs={'units': 'hPa', 'axis': 'Z'}),
'x': xr.DataArray([0, 450], name='x', dims='x',
attrs={'units': 'km', 'axis': 'X'})},
attrs={'units': 'kts'})
ds = xr.Dataset({'temperature': temperature, 'u': u})
# Check coordinates on temperature
assert ds['isobaric1'].identical(ds['temperature'].metpy.vertical)
assert ds['x'].identical(ds['temperature'].metpy.x)
# Check vertical coordinate on u
# Fails prior to resolution of Issue #1124
assert ds['isobaric2'].identical(ds['u'].metpy.vertical)
def test_one_dimensional_lat_lon(test_ds_generic):
"""Test that 1D lat/lon coords are recognized as both x/y and longitude/latitude."""
test_ds_generic['d'].attrs['units'] = 'degrees_north'
test_ds_generic['e'].attrs['units'] = 'degrees_east'
var = test_ds_generic.metpy.parse_cf('test')
assert var['d'].identical(var.metpy.y)
assert var['d'].identical(var.metpy.latitude)
assert var['e'].identical(var.metpy.x)
assert var['e'].identical(var.metpy.longitude)
def test_auxilary_lat_lon_with_xy(test_var_multidim_full):
"""Test that auxiliary lat/lon coord identification works with other x/y coords present."""
assert test_var_multidim_full['y'].identical(test_var_multidim_full.metpy.y)
assert test_var_multidim_full['lat'].identical(test_var_multidim_full.metpy.latitude)
assert test_var_multidim_full['x'].identical(test_var_multidim_full.metpy.x)
assert test_var_multidim_full['lon'].identical(test_var_multidim_full.metpy.longitude)
def test_auxilary_lat_lon_without_xy(test_var_multidim_no_xy):
"""Test that multidimensional lat/lon are recognized in absence of x/y coords."""
assert test_var_multidim_no_xy['lat'].identical(test_var_multidim_no_xy.metpy.latitude)
assert test_var_multidim_no_xy['lon'].identical(test_var_multidim_no_xy.metpy.longitude)
def test_auxilary_lat_lon_without_xy_as_xy(test_var_multidim_no_xy):
"""Test that the pre-v1.0 behavior of multidimensional lat/lon errors."""
with pytest.raises(AttributeError):
test_var_multidim_no_xy.metpy.y
with pytest.raises(AttributeError):
test_var_multidim_no_xy.metpy.x
# Declare a sample projection with CF attributes
sample_cf_attrs = {
'grid_mapping_name': 'lambert_conformal_conic',
'earth_radius': 6370000,
'standard_parallel': [30., 40.],
'longitude_of_central_meridian': 260.,
'latitude_of_projection_origin': 35.
}
def test_assign_crs_dataarray_by_argument(test_ds_generic, ccrs):
"""Test assigning CRS to DataArray by projection dict."""
da = test_ds_generic['test']
new_da = da.metpy.assign_crs(sample_cf_attrs)
assert isinstance(new_da.metpy.cartopy_crs, ccrs.LambertConformal)
assert new_da['metpy_crs'] == CFProjection(sample_cf_attrs)
def test_assign_crs_dataarray_by_kwargs(test_ds_generic, ccrs):
"""Test assigning CRS to DataArray by projection kwargs."""
da = test_ds_generic['test']
new_da = da.metpy.assign_crs(**sample_cf_attrs)
assert isinstance(new_da.metpy.cartopy_crs, ccrs.LambertConformal)
assert new_da['metpy_crs'] == CFProjection(sample_cf_attrs)
def test_assign_crs_dataset_by_argument(test_ds_generic, ccrs):
"""Test assigning CRS to Dataset by projection dict."""
new_ds = test_ds_generic.metpy.assign_crs(sample_cf_attrs)
assert isinstance(new_ds['test'].metpy.cartopy_crs, ccrs.LambertConformal)
assert new_ds['metpy_crs'] == CFProjection(sample_cf_attrs)
def test_assign_crs_dataset_by_kwargs(test_ds_generic, ccrs):
"""Test assigning CRS to Dataset by projection kwargs."""
new_ds = test_ds_generic.metpy.assign_crs(**sample_cf_attrs)
assert isinstance(new_ds['test'].metpy.cartopy_crs, ccrs.LambertConformal)
assert new_ds['metpy_crs'] == CFProjection(sample_cf_attrs)
def test_assign_crs_error_with_both_attrs(test_ds_generic):
"""Test ValueError is raised when both dictionary and kwargs given."""
with pytest.raises(ValueError) as exc:
test_ds_generic.metpy.assign_crs(sample_cf_attrs, **sample_cf_attrs)
assert 'Cannot specify both' in str(exc)
def test_assign_crs_error_with_neither_attrs(test_ds_generic):
"""Test ValueError is raised when neither dictionary and kwargs given."""
with pytest.raises(ValueError) as exc:
test_ds_generic.metpy.assign_crs()
assert 'Must specify either' in str(exc)
def test_assign_latitude_longitude_no_horizontal(test_ds_generic):
"""Test that assign_latitude_longitude only warns when no horizontal coordinates."""
with pytest.warns(UserWarning):
xr.testing.assert_identical(test_ds_generic,
test_ds_generic.metpy.assign_latitude_longitude())
def test_assign_y_x_no_horizontal(test_ds_generic):
"""Test that assign_y_x only warns when no horizontal coordinates."""
with pytest.warns(UserWarning):
xr.testing.assert_identical(test_ds_generic,
test_ds_generic.metpy.assign_y_x())
@pytest.fixture
def test_coord_helper_da_yx():
"""Provide a DataArray with y/x coords for coord helpers."""
return xr.DataArray(np.arange(9).reshape((3, 3)),
dims=('y', 'x'),
coords={'y': np.linspace(0, 1e5, 3),
'x': np.linspace(-1e5, 0, 3),
'metpy_crs': CFProjection(sample_cf_attrs)})
@pytest.fixture
def test_coord_helper_da_dummy_latlon(test_coord_helper_da_yx):
"""Provide DataArray with bad dummy lat/lon coords to be overwritten."""
return test_coord_helper_da_yx.assign_coords(latitude=0., longitude=0.)
@pytest.fixture
def test_coord_helper_da_latlon():
"""Provide a DataArray with lat/lon coords for coord helpers."""
return xr.DataArray(
np.arange(9).reshape((3, 3)),
dims=('y', 'x'),
coords={
'latitude': xr.DataArray(
np.array(
[[34.99501239, 34.99875307, 35.],
[35.44643155, 35.45019292, 35.45144675],
[35.89782579, 35.90160784, 35.90286857]]
),
dims=('y', 'x')
),
'longitude': xr.DataArray(
np.array(
[[-101.10219213, -100.55111288, -100.],
[-101.10831414, -100.55417417, -100.],
[-101.11450453, -100.55726965, -100.]]
),
dims=('y', 'x')
),
'metpy_crs': CFProjection(sample_cf_attrs)
}
)
@pytest.fixture
def test_coord_helper_da_dummy_yx(test_coord_helper_da_latlon):
"""Provide DataArray with bad dummy y/x coords to be overwritten."""
return test_coord_helper_da_latlon.assign_coords(y=range(3), x=range(3))
def test_assign_latitude_longitude_basic_dataarray(test_coord_helper_da_yx,
test_coord_helper_da_latlon):
"""Test assign_latitude_longitude in basic usage on DataArray."""
new_da = test_coord_helper_da_yx.metpy.assign_latitude_longitude()
lat, lon = new_da.metpy.coordinates('latitude', 'longitude')
np.testing.assert_array_almost_equal(test_coord_helper_da_latlon['latitude'].values,
lat.values, 3)
np.testing.assert_array_almost_equal(test_coord_helper_da_latlon['longitude'].values,
lon.values, 3)
def test_assign_latitude_longitude_error_existing_dataarray(
test_coord_helper_da_dummy_latlon):
"""Test assign_latitude_longitude failure with existing coordinates."""
with pytest.raises(RuntimeError) as exc:
test_coord_helper_da_dummy_latlon.metpy.assign_latitude_longitude()
assert 'Latitude/longitude coordinate(s) are present' in str(exc)
def test_assign_latitude_longitude_force_existing_dataarray(
test_coord_helper_da_dummy_latlon, test_coord_helper_da_latlon):
"""Test assign_latitude_longitude with existing coordinates forcing new."""
new_da = test_coord_helper_da_dummy_latlon.metpy.assign_latitude_longitude(True)
lat, lon = new_da.metpy.coordinates('latitude', 'longitude')
np.testing.assert_array_almost_equal(test_coord_helper_da_latlon['latitude'].values,
lat.values, 3)
np.testing.assert_array_almost_equal(test_coord_helper_da_latlon['longitude'].values,
lon.values, 3)
def test_assign_latitude_longitude_basic_dataset(test_coord_helper_da_yx,
test_coord_helper_da_latlon):
"""Test assign_latitude_longitude in basic usage on Dataset."""
ds = test_coord_helper_da_yx.to_dataset(name='test').metpy.assign_latitude_longitude()
lat, lon = ds['test'].metpy.coordinates('latitude', 'longitude')
np.testing.assert_array_almost_equal(test_coord_helper_da_latlon['latitude'].values,
lat.values, 3)
np.testing.assert_array_almost_equal(test_coord_helper_da_latlon['longitude'].values,
lon.values, 3)
def test_assign_y_x_basic_dataarray(test_coord_helper_da_yx, test_coord_helper_da_latlon):
"""Test assign_y_x in basic usage on DataArray."""
new_da = test_coord_helper_da_latlon.metpy.assign_y_x()
y, x = new_da.metpy.coordinates('y', 'x')
np.testing.assert_array_almost_equal(test_coord_helper_da_yx['y'].values, y.values, 3)
np.testing.assert_array_almost_equal(test_coord_helper_da_yx['x'].values, x.values, 3)
def test_assign_y_x_error_existing_dataarray(
test_coord_helper_da_dummy_yx):
"""Test assign_y_x failure with existing coordinates."""
with pytest.raises(RuntimeError) as exc:
test_coord_helper_da_dummy_yx.metpy.assign_y_x()
assert 'y/x coordinate(s) are present' in str(exc)
def test_assign_y_x_force_existing_dataarray(
test_coord_helper_da_dummy_yx, test_coord_helper_da_yx):
"""Test assign_y_x with existing coordinates forcing new."""
new_da = test_coord_helper_da_dummy_yx.metpy.assign_y_x(True)