diff --git a/geodataset/geodataset.py b/geodataset/geodataset.py index 3596a17..8977ad2 100644 --- a/geodataset/geodataset.py +++ b/geodataset/geodataset.py @@ -291,7 +291,14 @@ class GeoDatasetRead(GeoDatasetBase): @property @lru_cache(1) def lonlat_names(self): - """ Get names of latitude longitude following CF and ACDD standards """ + """ Get names of latitude longitude following CF and ACDD standards + + Returns + ------- + lon_var_name : str + lat_var_name : str + + """ lon_standard_name = 'longitude' lat_standard_name = 'latitude' lon_var_name = lat_var_name = None @@ -327,19 +334,48 @@ def variable_names(self): @property def projection(self): + """ Read projection of the dataset from self.area_definition + + Returns + ------- + projection : pyproj.Proj + + """ return pyproj.Proj(self.area_definition.crs) @property def area_definition(self): + """ Read area definition of the dataset from self._area_def_cf_info + + Returns + ------- + area_definition : pyresample.AreaDefinition + + """ return self._area_def_cf_info[0] @property def grid_mapping_variable(self): + """ Read name of the grid mapping variable from self._area_def_cf_info + + Returns + ------- + grid_mapping_variable : str + + """ return self._area_def_cf_info[1]['grid_mapping_variable'] @property @lru_cache(1) def _area_def_cf_info(self): + """ Read area definition and grid mapping information using pyresample.load_cf_area + + Returns + ------- + area_def_cf_info : tuple + area_definition and grid_mapping_information + + """ try: area_def_cf_info = load_cf_area(self.filename) except (MissingDimensionsError, CRSError, KeyError, ValueError) as e: @@ -348,6 +384,22 @@ def _area_def_cf_info(self): return area_def_cf_info def get_variable_array(self, var_name, time_index=0): + """ Get array with values from a given variable. + If variable has time dimension, time_index is used. + + Parameters + ---------- + var_name : str + name of variable + time_index: int + from which time layer to read data + + Returns + ------- + array : 2D numpy.array + data from variable from time_index + + """ ds_var = self[var_name] array = ds_var[:] if 'time' in ds_var.dimensions: diff --git a/geodataset/tests/test_geodataset.py b/geodataset/tests/test_geodataset.py index 7671a24..d9039f3 100644 --- a/geodataset/tests/test_geodataset.py +++ b/geodataset/tests/test_geodataset.py @@ -1,13 +1,13 @@ +import datetime as dt import glob +from mock import patch, call, Mock, MagicMock, DEFAULT import os -import numpy as np +import subprocess import unittest -import datetime as dt -from mock import patch, call, Mock, MagicMock, DEFAULT + +import numpy as np import pyproj from pyresample.utils import load_cf_area -import subprocess - from geodataset.geodataset import GeoDatasetBase, GeoDatasetWrite, GeoDatasetRead, Dataset, ProjectionInfo from geodataset.utils import InvalidDatasetError @@ -239,8 +239,6 @@ def test_init(self): self.assertFalse(nc.is_lonlat_dim) def test_method_get_nearest_date(self): - """Test the ability of finding the nearest date to a specific date. 2007 is near to 2006 (in - netcdf file) than the 2010.""" with GeoDatasetRead(self.osisaf_filename, 'r') as ds: #ds.datetimes.append(dt.datetime(2000, 1, 1, 12, 0)) ans, ans_index = ds.get_nearest_date(dt.datetime(2020, 1, 1, 12, 0)) @@ -248,8 +246,6 @@ def test_method_get_nearest_date(self): self.assertEqual(ans_index, 0) def test_get_var_names(self): - """Test the ability of finding the nearest date to a specific date. 2007 is near to 2006 (in - netcdf file) than the 2010.""" with GeoDatasetRead(self.osisaf_filename, 'r') as ds: var_names = ds.variable_names self.assertEqual(var_names, @@ -275,7 +271,6 @@ def test_get_lonlat_arrays(self): variables=DEFAULT, ) def test_get_lonlat_names(self, **kwargs): - ''' test f4 with _FillValue defined ''' variables = { 'lon': Mock(), 'lat': Mock(), @@ -301,7 +296,6 @@ def test_get_lonlat_names(self, **kwargs): variables=DEFAULT, ) def test_get_lonlat_names_raises(self, **kwargs): - ''' test f4 with _FillValue defined ''' variables = { 'lon': Mock(), } @@ -335,7 +329,6 @@ def test_projection(self): get_lonlat_arrays=DEFAULT, ) def test_get_area_euclidean(self, **kwargs): - ''' test f4 with _FillValue defined ''' p = pyproj.Proj(3411) GeoDatasetRead.get_lonlat_arrays.return_value = ( np.array([[1,2,3],[1,2,3],[1,2,3]]), @@ -343,7 +336,7 @@ def test_get_area_euclidean(self, **kwargs): with GeoDatasetRead() as ds: area = ds.get_area_euclidean(p) - self.assertAlmostEqual(area, 23354252971.32609) + self.assertAlmostEqual(area, 23354252971.32609, 1) @patch.multiple(GeoDatasetRead, __init__=MagicMock(return_value=None), @@ -351,7 +344,6 @@ def test_get_area_euclidean(self, **kwargs): get_lonlat_arrays=DEFAULT, ) def test_get_bbox(self, **kwargs): - ''' test f4 with _FillValue defined ''' p = pyproj.Proj(3411) GeoDatasetRead.get_lonlat_arrays.return_value = ( np.array([[1,2,3],[1,2,3],[1,2,3]]), @@ -359,9 +351,10 @@ def test_get_bbox(self, **kwargs): with GeoDatasetRead() as ds: bbox = ds.get_bbox(p) - self.assertAlmostEqual(bbox, + np.testing.assert_almost_equal(bbox, [8420199.606917838, 9005961.652806347, - -8418368.037664523, -7832478.150085783]) + -8418368.037664523, -7832478.150085783], + 1) if __name__ == "__main__": unittest.main() diff --git a/geodataset/tests/test_tools.py b/geodataset/tests/test_tools.py index 28871aa..6bd0a4d 100644 --- a/geodataset/tests/test_tools.py +++ b/geodataset/tests/test_tools.py @@ -41,4 +41,4 @@ def test_projection(self): self.assertIsInstance(ds.projection, pyproj.Proj) if __name__ == "__main__": - unittest.main(failfast=True) + unittest.main()