|
1 | 1 | import os |
2 | | -from unittest import TestCase |
3 | 2 | from pathlib import Path |
4 | 3 | import numpy.testing as nt |
5 | | - |
6 | 4 | import uxarray as ux |
7 | | -import xarray as xr |
8 | 5 | import numpy as np |
| 6 | +import pytest |
9 | 7 |
|
10 | 8 | try: |
11 | 9 | import constants |
|
14 | 12 |
|
15 | 13 | current_path = Path(os.path.dirname(os.path.realpath(__file__))) |
16 | 14 |
|
| 15 | +geoflow_data_path = current_path / "meshfiles" / "ugrid" / "geoflow-small" |
| 16 | +gridfile_geoflow = current_path / "meshfiles" / "ugrid" / "geoflow-small" / "grid.nc" |
| 17 | +geoflow_data_v1 = geoflow_data_path / "v1.nc" |
| 18 | +geoflow_data_v2 = geoflow_data_path / "v2.nc" |
| 19 | +geoflow_data_v3 = geoflow_data_path / "v3.nc" |
17 | 20 |
|
18 | | -class TestAPI(TestCase): |
19 | | - geoflow_data_path = current_path / "meshfiles" / "ugrid" / "geoflow-small" |
20 | | - gridfile_geoflow = current_path / "meshfiles" / "ugrid" / "geoflow-small" / "grid.nc" |
21 | | - geoflow_data_v1 = geoflow_data_path / "v1.nc" |
22 | | - geoflow_data_v2 = geoflow_data_path / "v2.nc" |
23 | | - geoflow_data_v3 = geoflow_data_path / "v3.nc" |
24 | | - |
25 | | - gridfile_ne30 = current_path / "meshfiles" / "ugrid" / "outCSne30" / "outCSne30.ug" |
26 | | - dsfile_var2_ne30 = current_path / "meshfiles" / "ugrid" / "outCSne30" / "outCSne30_var2.nc" |
27 | | - dsfiles_mf_ne30 = str( |
28 | | - current_path) + "/meshfiles/ugrid/outCSne30/outCSne30_*.nc" |
29 | | - |
30 | | - def test_open_geoflow_dataset(self): |
31 | | - """Loads a single dataset with its grid topology file using uxarray's |
32 | | - open_dataset call.""" |
| 21 | +gridfile_ne30 = current_path / "meshfiles" / "ugrid" / "outCSne30" / "outCSne30.ug" |
| 22 | +dsfile_var2_ne30 = current_path / "meshfiles" / "ugrid" / "outCSne30" / "outCSne30_var2.nc" |
| 23 | +dsfiles_mf_ne30 = str(current_path) + "/meshfiles/ugrid/outCSne30/outCSne30_*.nc" |
33 | 24 |
|
34 | | - # Paths to Data Variable files |
35 | | - data_paths = [ |
36 | | - self.geoflow_data_v1, self.geoflow_data_v2, self.geoflow_data_v3 |
37 | | - ] |
| 25 | +def test_open_geoflow_dataset(): |
| 26 | + """Loads a single dataset with its grid topology file using uxarray's |
| 27 | + open_dataset call.""" |
38 | 28 |
|
39 | | - uxds_v1 = ux.open_dataset(self.gridfile_geoflow, data_paths[0]) |
| 29 | + # Paths to Data Variable files |
| 30 | + data_paths = [ |
| 31 | + geoflow_data_v1, geoflow_data_v2, geoflow_data_v3 |
| 32 | + ] |
40 | 33 |
|
41 | | - # Ideally uxds_v1.uxgrid should NOT be None |
42 | | - with self.assertRaises(AssertionError): |
43 | | - nt.assert_equal(uxds_v1.uxgrid, None) |
| 34 | + uxds_v1 = ux.open_dataset(gridfile_geoflow, data_paths[0]) |
44 | 35 |
|
45 | | - def test_open_dataset(self): |
46 | | - """Loads a single dataset with its grid topology file using uxarray's |
47 | | - open_dataset call.""" |
| 36 | + # Ideally uxds_v1.uxgrid should NOT be None |
| 37 | + nt.assert_equal(uxds_v1.uxgrid is not None, True) |
48 | 38 |
|
49 | | - uxds_var2_ne30 = ux.open_dataset(self.gridfile_ne30, |
50 | | - self.dsfile_var2_ne30) |
| 39 | +def test_open_dataset(): |
| 40 | + """Loads a single dataset with its grid topology file using uxarray's |
| 41 | + open_dataset call.""" |
51 | 42 |
|
52 | | - nt.assert_equal(uxds_var2_ne30.uxgrid.node_lon.size, |
53 | | - constants.NNODES_outCSne30) |
54 | | - nt.assert_equal(len(uxds_var2_ne30.uxgrid._ds.data_vars), |
55 | | - constants.DATAVARS_outCSne30) |
56 | | - nt.assert_equal(uxds_var2_ne30.source_datasets, |
57 | | - str(self.dsfile_var2_ne30)) |
| 43 | + uxds_var2_ne30 = ux.open_dataset(gridfile_ne30, dsfile_var2_ne30) |
58 | 44 |
|
59 | | - def test_open_mf_dataset(self): |
60 | | - """Loads multiple datasets with their grid topology file using |
61 | | - uxarray's open_dataset call.""" |
| 45 | + nt.assert_equal(uxds_var2_ne30.uxgrid.node_lon.size, constants.NNODES_outCSne30) |
| 46 | + nt.assert_equal(len(uxds_var2_ne30.uxgrid._ds.data_vars), constants.DATAVARS_outCSne30) |
| 47 | + nt.assert_equal(uxds_var2_ne30.source_datasets, str(dsfile_var2_ne30)) |
62 | 48 |
|
63 | | - uxds_mf_ne30 = ux.open_mfdataset(self.gridfile_ne30, |
64 | | - self.dsfiles_mf_ne30) |
| 49 | +def test_open_mf_dataset(): |
| 50 | + """Loads multiple datasets with their grid topology file using |
| 51 | + uxarray's open_dataset call.""" |
65 | 52 |
|
66 | | - nt.assert_equal(uxds_mf_ne30.uxgrid.node_lon.size, |
67 | | - constants.NNODES_outCSne30) |
68 | | - nt.assert_equal(len(uxds_mf_ne30.uxgrid._ds.data_vars), |
69 | | - constants.DATAVARS_outCSne30) |
| 53 | + uxds_mf_ne30 = ux.open_mfdataset(gridfile_ne30, dsfiles_mf_ne30) |
70 | 54 |
|
71 | | - nt.assert_equal(uxds_mf_ne30.source_datasets, self.dsfiles_mf_ne30) |
| 55 | + nt.assert_equal(uxds_mf_ne30.uxgrid.node_lon.size, constants.NNODES_outCSne30) |
| 56 | + nt.assert_equal(len(uxds_mf_ne30.uxgrid._ds.data_vars), constants.DATAVARS_outCSne30) |
| 57 | + nt.assert_equal(uxds_mf_ne30.source_datasets, dsfiles_mf_ne30) |
72 | 58 |
|
73 | | - def test_open_grid(self): |
74 | | - """Loads only a grid topology file using uxarray's open_grid call.""" |
75 | | - uxgrid = ux.open_grid(self.gridfile_geoflow) |
| 59 | +def test_open_grid(): |
| 60 | + """Loads only a grid topology file using uxarray's open_grid call.""" |
| 61 | + uxgrid = ux.open_grid(gridfile_geoflow) |
76 | 62 |
|
77 | | - nt.assert_almost_equal(uxgrid.calculate_total_face_area(), |
78 | | - constants.MESH30_AREA, |
79 | | - decimal=3) |
| 63 | + nt.assert_almost_equal(uxgrid.calculate_total_face_area(), constants.MESH30_AREA, decimal=3) |
80 | 64 |
|
81 | | - def test_copy_dataset(self): |
82 | | - """Loads a single dataset with its grid topology file using uxarray's |
83 | | - open_dataset call and make a copy of the object.""" |
| 65 | +def test_copy_dataset(): |
| 66 | + """Loads a single dataset with its grid topology file using uxarray's |
| 67 | + open_dataset call and make a copy of the object.""" |
84 | 68 |
|
85 | | - uxds_var2_ne30 = ux.open_dataset(self.gridfile_ne30, |
86 | | - self.dsfile_var2_ne30) |
| 69 | + uxds_var2_ne30 = ux.open_dataset(gridfile_ne30, dsfile_var2_ne30) |
87 | 70 |
|
88 | | - # make a shallow and deep copy of the dataset object |
89 | | - uxds_var2_ne30_copy_deep = uxds_var2_ne30.copy(deep=True) |
90 | | - uxds_var2_ne30_copy = uxds_var2_ne30.copy(deep=False) |
| 71 | + # make a shallow and deep copy of the dataset object |
| 72 | + uxds_var2_ne30_copy_deep = uxds_var2_ne30.copy(deep=True) |
| 73 | + uxds_var2_ne30_copy = uxds_var2_ne30.copy(deep=False) |
91 | 74 |
|
92 | | - # Ideally uxds_var2_ne30_copy.uxgrid should NOT be None |
93 | | - with self.assertRaises(AssertionError): |
94 | | - nt.assert_equal(uxds_var2_ne30_copy.uxgrid, None) |
| 75 | + # Ideally uxds_var2_ne30_copy.uxgrid should NOT be None |
| 76 | + nt.assert_equal(uxds_var2_ne30_copy.uxgrid is not None, True) |
95 | 77 |
|
96 | | - # Check that the copy is a shallow copy |
97 | | - assert (uxds_var2_ne30_copy.uxgrid is uxds_var2_ne30.uxgrid) |
98 | | - assert (uxds_var2_ne30_copy.uxgrid == uxds_var2_ne30.uxgrid) |
| 78 | + # Check that the copy is a shallow copy |
| 79 | + assert uxds_var2_ne30_copy.uxgrid is uxds_var2_ne30.uxgrid |
| 80 | + assert uxds_var2_ne30_copy.uxgrid == uxds_var2_ne30.uxgrid |
99 | 81 |
|
100 | | - # Check that the deep copy is a deep copy |
101 | | - assert (uxds_var2_ne30_copy_deep.uxgrid == uxds_var2_ne30.uxgrid) |
102 | | - assert (uxds_var2_ne30_copy_deep.uxgrid is not uxds_var2_ne30.uxgrid) |
| 82 | + # Check that the deep copy is a deep copy |
| 83 | + assert uxds_var2_ne30_copy_deep.uxgrid == uxds_var2_ne30.uxgrid |
| 84 | + assert uxds_var2_ne30_copy_deep.uxgrid is not uxds_var2_ne30.uxgrid |
103 | 85 |
|
104 | | - def test_copy_dataarray(self): |
105 | | - """Loads an unstructured grid and data using uxarray's open_dataset |
106 | | - call and make a copy of the dataarray object.""" |
| 86 | +def test_copy_dataarray(): |
| 87 | + """Loads an unstructured grid and data using uxarray's open_dataset |
| 88 | + call and make a copy of the dataarray object.""" |
107 | 89 |
|
108 | | - # Paths to Data Variable files |
109 | | - data_paths = [ |
110 | | - self.geoflow_data_v1, self.geoflow_data_v2, self.geoflow_data_v3 |
111 | | - ] |
| 90 | + # Paths to Data Variable files |
| 91 | + data_paths = [ |
| 92 | + geoflow_data_v1, geoflow_data_v2, geoflow_data_v3 |
| 93 | + ] |
112 | 94 |
|
113 | | - uxds_v1 = ux.open_dataset(self.gridfile_geoflow, data_paths[0]) |
| 95 | + uxds_v1 = ux.open_dataset(gridfile_geoflow, data_paths[0]) |
114 | 96 |
|
115 | | - # get the uxdataarray object |
116 | | - v1_uxdata_array = uxds_v1['v1'] |
| 97 | + # get the uxdataarray object |
| 98 | + v1_uxdata_array = uxds_v1['v1'] |
117 | 99 |
|
118 | | - # make a shallow and deep copy of the dataarray object |
119 | | - v1_uxdata_array_copy_deep = v1_uxdata_array.copy(deep=True) |
120 | | - v1_uxdata_array_copy = v1_uxdata_array.copy(deep=False) |
| 100 | + # make a shallow and deep copy of the dataarray object |
| 101 | + v1_uxdata_array_copy_deep = v1_uxdata_array.copy(deep=True) |
| 102 | + v1_uxdata_array_copy = v1_uxdata_array.copy(deep=False) |
121 | 103 |
|
122 | | - # Check that the copy is a shallow copy |
123 | | - assert (v1_uxdata_array_copy.uxgrid is v1_uxdata_array.uxgrid) |
124 | | - assert (v1_uxdata_array_copy.uxgrid == v1_uxdata_array.uxgrid) |
| 104 | + # Check that the copy is a shallow copy |
| 105 | + assert v1_uxdata_array_copy.uxgrid is v1_uxdata_array.uxgrid |
| 106 | + assert v1_uxdata_array_copy.uxgrid == v1_uxdata_array.uxgrid |
125 | 107 |
|
126 | | - # Check that the deep copy is a deep copy |
127 | | - assert (v1_uxdata_array_copy_deep.uxgrid == v1_uxdata_array.uxgrid) |
128 | | - assert (v1_uxdata_array_copy_deep.uxgrid is not v1_uxdata_array.uxgrid) |
| 108 | + # Check that the deep copy is a deep copy |
| 109 | + assert v1_uxdata_array_copy_deep.uxgrid == v1_uxdata_array.uxgrid |
| 110 | + assert v1_uxdata_array_copy_deep.uxgrid is not v1_uxdata_array.uxgrid |
129 | 111 |
|
130 | | - def test_open_dataset_grid_kwargs(self): |
131 | | - """Drops ``Mesh2_face_nodes`` from the inputted grid file using |
132 | | - ``grid_kwargs``""" |
| 112 | +def test_open_dataset_grid_kwargs(): |
| 113 | + """Drops ``Mesh2_face_nodes`` from the inputted grid file using |
| 114 | + ``grid_kwargs``""" |
133 | 115 |
|
134 | | - with self.assertRaises(ValueError): |
135 | | - # attempt to open a dataset after dropping face nodes should raise a KeyError |
136 | | - uxds = ux.open_dataset( |
137 | | - self.gridfile_ne30, |
138 | | - self.dsfile_var2_ne30, |
139 | | - grid_kwargs={"drop_variables": "Mesh2_face_nodes"}) |
| 116 | + with pytest.raises(ValueError): |
| 117 | + # attempt to open a dataset after dropping face nodes should raise a KeyError |
| 118 | + uxds = ux.open_dataset( |
| 119 | + gridfile_ne30, |
| 120 | + dsfile_var2_ne30, |
| 121 | + grid_kwargs={"drop_variables": "Mesh2_face_nodes"} |
| 122 | + ) |
0 commit comments