def test_get_point(self): with Fvcom('tests/testdata/fvcom_test.nc') as n: data, depth = n.get_point(45.3, -64.0, 0, 0, 'temp', return_depth=True) self.assertAlmostEqual(data, 6.78, places=2) self.assertAlmostEqual(depth, 6.51, places=2)
def test_get_raw_point(self): with Fvcom("tests/testdata/fvcom_test.nc") as n: lat, lon, data = n.get_raw_point(45.3, -64.0, 0, 0, "temp") self.assertEqual(len(lat.ravel()), 156) self.assertEqual(len(lon.ravel()), 156) self.assertEqual(len(data.ravel()), 156) self.assertAlmostEqual(data[75], 6.90, places=1)
def test_variables(self, mock_query_func): mock_query_func.return_value = self.variable_list_mock with Fvcom('tests/testdata/fvcom_test.nc') as n: variables = n.variables self.assertEqual(len(variables), 3) self.assertTrue('h' in variables) self.assertEqual(variables['h'].name, 'Bathymetry') self.assertEqual(variables['h'].unit, 'm') self.assertEqual(variables['h'].dimensions, ["node"])
def test_variables(self, mock_query_func): mock_query_func.return_value = self.variable_list_mock with Fvcom("tests/testdata/fvcom_test.nc") as n: variables = n.variables self.assertEqual(len(variables), 3) self.assertTrue("h" in variables) self.assertEqual(variables["h"].name, "Bathymetry") self.assertEqual(variables["h"].unit, "m") self.assertEqual(variables["h"].dimensions, ["node"])
def test_timestamps(self): with Fvcom('tests/testdata/fvcom_test.nc') as n: self.assertEqual(len(n.timestamps), 2) self.assertEqual(n.timestamps[0], datetime.datetime(2015, 7, 6, 0, 0, 0, 0, pytz.UTC)) # Property is read-only with self.assertRaises(AttributeError): n.timestamps = [] # List is immutable with self.assertRaises(ValueError): n.timestamps[0] = 0
def open_dataset(dataset, **kwargs): """Open a dataset. Creates a CalculatedData (derived from NetCDFData) instance to handle dataset file access and calculation layer operations. Then, determines the type of model the dataset is from and returns the appropriate Model-derived instance (Nemo, Mercator, Fvcom) with the calculation layer instance as an attribute. Note: the returned model object will be LRU-cached internally so frequent calls to open the "same" dataset will have minimal overhead. Params: * dataset -- Either a DatasetConfig object, or a string URL for the dataset Optional Keyword Arguments: * variable {str or list} -- String or list of strings of variable keys to be loaded (e.g. 'votemper' or ['votemper', 'vosaline']). * timestamp {int} -- Integer value of date/time for requested data (e.g. 2128723200). When loading a range of timestamps, this argument serves as the starting time. * endtime {int} -- Integer value of date/time. This argument is only used when loading a range of timestamps, and should hold the ending time. * nearest_timestamp {bool} -- When true, open_dataset will assume the given starttime (and endtime) do not exactly correspond to a timestamp integer in the dataset, and will perform a binary search to find the nearest timestamp that is less-than-or-equal-to the given starttime (and endtime). * meta_only {bool} -- Skip some dataset access operations in order to speed up response. """ if not dataset: raise ValueError("Unknown dataset.") try: url = dataset.url calculated_vars = dataset.calculated_variables except AttributeError: url = dataset calculated_vars = {} if url is None: raise ValueError("Dataset url is None.") args = { "calculated": calculated_vars, "meta_only": kwargs.get("meta_only", False), "grid_angle_file_url": getattr(dataset, "grid_angle_file_url", ""), "dataset_key": getattr(dataset, "key", ""), } nc_data = CalculatedData(url, **args) if not args["meta_only"]: # Get required NC files from database and add to args nc_data.get_nc_file_list(dataset, **kwargs) dimension_list = nc_data.dimensions if not dimension_list: raise RuntimeError(f"Dataset not supported: {url}.") if 'longitude' in dimension_list or 'latitude' in dimension_list: return Mercator(nc_data) if 'siglay' in dimension_list: return Fvcom(nc_data) return Nemo(nc_data)
def test_bottom_point(self): with Fvcom('tests/testdata/fvcom_test.nc') as n: self.assertAlmostEqual( n.get_point(45.3, -64.0, 'bottom', 0, 'temp'), 6.78, places=2 )
def test_get_profile(self): with Fvcom('tests/testdata/fvcom_test.nc') as n: p, d = n.get_profile(45.3, -64.0, 'temp', 0) self.assertAlmostEqual(p[0], 6.78, places=2) self.assertAlmostEqual(p[10], 6.78, places=2)
def test_depths(self): with Fvcom('tests/testdata/fvcom_test.nc') as n: depths = n.depths self.assertEqual(len(depths), 1) self.assertEqual(depths[0], 0)
def test_bottom_point(self): with Fvcom("tests/testdata/fvcom_test.nc") as n: self.assertAlmostEqual(n.get_point(45.3, -64.0, "bottom", 0, "temp"), 6.78, places=2)