|
17 | 17 |
|
18 | 18 | def test_accessor_gridline_cartesian():
|
19 | 19 | """
|
20 |
| - Check that a grid returns a registration value of 0 when Gridline registered, and a |
21 |
| - gtype value of 1 when using Geographic coordinates. |
| 20 | + Check that the accessor returns the correct registration and gtype values for a |
| 21 | + Cartesian, gridline-registered grid. |
22 | 22 | """
|
23 | 23 | fname = which(fname="@test.dat.nc", download="a")
|
24 |
| - grid = xr.open_dataarray(fname) |
| 24 | + grid = xr.open_dataarray(fname, engine="netcdf4") |
25 | 25 | assert grid.gmt.registration == GridReg.GRIDLINE
|
26 | 26 | assert grid.gmt.gtype == GridType.CARTESIAN
|
27 | 27 |
|
28 | 28 |
|
29 | 29 | def test_accessor_pixel_geographic():
|
30 | 30 | """
|
31 |
| - Check that a grid returns a registration value of 1 when Pixel registered, and a |
32 |
| - gtype value of 0 when using Cartesian coordinates. |
| 31 | + Check that the accessor returns the correct registration and gtype values for a |
| 32 | + geographic, pixel-registered grid. |
33 | 33 | """
|
34 | 34 | fname = which(fname="@earth_relief_01d_p", download="a")
|
35 | 35 | grid = xr.open_dataarray(fname, engine="netcdf4")
|
36 | 36 | assert grid.gmt.registration == GridReg.PIXEL
|
37 | 37 | assert grid.gmt.gtype == GridType.GEOGRAPHIC
|
38 | 38 |
|
39 | 39 |
|
40 |
| -def test_accessor_set_pixel_registration(): |
| 40 | +def test_accessor_set_registration(): |
41 | 41 | """
|
42 |
| - Check that we can set a grid to be Pixel registered with a registration value of 1. |
| 42 | + Check that we can set the registration of a grid. |
43 | 43 | """
|
44 | 44 | grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]])
|
45 |
| - assert grid.gmt.registration == GridReg.GRIDLINE |
| 45 | + assert grid.gmt.registration == GridReg.GRIDLINE == 0 # Default registration |
| 46 | + |
| 47 | + # Set the registration to pixel |
46 | 48 | grid.gmt.registration = GridReg.PIXEL
|
47 |
| - assert grid.gmt.registration == GridReg.PIXEL |
| 49 | + assert grid.gmt.registration == GridReg.PIXEL == 1 |
| 50 | + |
| 51 | + # Set the registration to gridline |
| 52 | + grid.gmt.registration = GridReg.GRIDLINE |
| 53 | + assert grid.gmt.registration == GridReg.GRIDLINE == 0 |
| 54 | + |
| 55 | + # Set the registration to pixel but using a numerical value |
| 56 | + grid.gmt.registration = 1 |
| 57 | + assert grid.gmt.registration == GridReg.PIXEL == 1 |
| 58 | + |
| 59 | + # Set the registration to gridline but using a numerical value |
| 60 | + grid.gmt.registration = 0 |
| 61 | + assert grid.gmt.registration == GridReg.GRIDLINE == 0 |
48 | 62 |
|
49 | 63 |
|
50 | 64 | @pytest.mark.benchmark
|
51 |
| -def test_accessor_set_geographic_cartesian_roundtrip(): |
| 65 | +def test_accessor_set_gtype(): |
52 | 66 | """
|
53 |
| - Check that we can set a grid to switch between the default Cartesian coordinate type |
54 |
| - using a gtype of 1, set it to Geographic 0, and then back to Cartesian again 1. |
| 67 | + Check that we can set the gtype of a grid. |
55 | 68 | """
|
56 | 69 | grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]])
|
57 |
| - assert grid.gmt.gtype == GridType.CARTESIAN |
| 70 | + assert grid.gmt.gtype == GridType.CARTESIAN == 0 # Default gtype |
| 71 | + |
| 72 | + # Set the gtype to geographic |
58 | 73 | grid.gmt.gtype = GridType.GEOGRAPHIC
|
59 |
| - assert grid.gmt.gtype == GridType.GEOGRAPHIC |
| 74 | + assert grid.gmt.gtype == GridType.GEOGRAPHIC == 1 |
| 75 | + |
| 76 | + # Set the gtype to Cartesian |
60 | 77 | grid.gmt.gtype = GridType.CARTESIAN
|
61 |
| - assert grid.gmt.gtype == GridType.CARTESIAN |
| 78 | + assert grid.gmt.gtype == GridType.CARTESIAN == 0 |
| 79 | + |
| 80 | + # Set the gtype to geographic but using a numerical value |
| 81 | + grid.gmt.gtype = 1 |
| 82 | + assert grid.gmt.gtype == GridType.GEOGRAPHIC == 1 |
62 | 83 |
|
| 84 | + # Set the gtype to Cartesian but using a numerical value |
| 85 | + grid.gmt.gtype = 0 |
| 86 | + assert grid.gmt.gtype == GridType.CARTESIAN == 0 |
63 | 87 |
|
64 |
| -def test_accessor_set_non_boolean(): |
| 88 | + |
| 89 | +def test_accessor_set_invalid_registration_and_gtype(): |
65 | 90 | """
|
66 |
| - Check that setting non boolean values on registration and gtype do not work. |
| 91 | + Check that setting invalid values on registration and gtype do not work. |
67 | 92 | """
|
68 | 93 | grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]])
|
69 | 94 |
|
70 | 95 | with pytest.raises(GMTInvalidInput):
|
71 | 96 | grid.gmt.registration = "2"
|
72 |
| - |
| 97 | + with pytest.raises(GMTInvalidInput): |
| 98 | + grid.gmt.registration = "pixel" |
73 | 99 | with pytest.raises(GMTInvalidInput):
|
74 | 100 | grid.gmt.gtype = 2
|
| 101 | + with pytest.raises(GMTInvalidInput): |
| 102 | + grid.gmt.gtype = "geographic" |
75 | 103 |
|
76 | 104 |
|
77 | 105 | @pytest.mark.xfail(
|
@@ -105,23 +133,23 @@ def test_accessor_grid_source_file_not_exist():
|
105 | 133 | Check that the accessor fallbacks to the default registration and gtype when the
|
106 | 134 | grid source file (i.e., grid.encoding["source"]) doesn't exist.
|
107 | 135 | """
|
108 |
| - # Load the 05m earth relief grid, which is stored as tiles |
| 136 | + # Load the 05m earth relief grid, which is stored as tiles. |
109 | 137 | grid = load_earth_relief(
|
110 | 138 | resolution="05m", region=[0, 5, -5, 5], registration="pixel"
|
111 | 139 | )
|
112 |
| - # Registration and gtype are correct |
| 140 | + # Registration and gtype are correct. |
113 | 141 | assert grid.gmt.registration == GridReg.PIXEL
|
114 | 142 | assert grid.gmt.gtype == GridType.GEOGRAPHIC
|
115 | 143 | # The source grid file is undefined.
|
116 | 144 | assert grid.encoding.get("source") is None
|
117 | 145 |
|
118 |
| - # For a sliced grid, fallback to default registration and gtype, |
119 |
| - # because the source grid file doesn't exist. |
| 146 | + # For a sliced grid, fallback to default registration and gtype, because the source |
| 147 | + # grid file doesn't exist. |
120 | 148 | sliced_grid = grid[1:3, 1:3]
|
121 | 149 | assert sliced_grid.gmt.registration == GridReg.GRIDLINE
|
122 | 150 | assert sliced_grid.gmt.gtype == GridType.CARTESIAN
|
123 | 151 |
|
124 |
| - # Still possible to manually set registration and gtype |
| 152 | + # Still possible to manually set registration and gtype. |
125 | 153 | sliced_grid.gmt.registration = GridReg.PIXEL
|
126 | 154 | sliced_grid.gmt.gtype = GridType.GEOGRAPHIC
|
127 | 155 | assert sliced_grid.gmt.registration == GridReg.PIXEL
|
|
0 commit comments