Skip to content

Commit

Permalink
dockstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
akorosov committed Jan 10, 2022
1 parent 06a8b88 commit 588ba25
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
54 changes: 53 additions & 1 deletion geodataset/geodataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
25 changes: 9 additions & 16 deletions geodataset/tests/test_geodataset.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -239,17 +239,13 @@ 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))
self.assertEqual(ans, dt.datetime(2022, 1, 3, 12, 0))
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,
Expand All @@ -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(),
Expand All @@ -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(),
}
Expand Down Expand Up @@ -335,33 +329,32 @@ 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]]),
np.array([[1,1,1],[2,2,2],[3,3,3]]))

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),
__exit__=MagicMock(return_value=None),
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]]),
np.array([[1,1,1],[2,2,2],[3,3,3]]))

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()
2 changes: 1 addition & 1 deletion geodataset/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ def test_projection(self):
self.assertIsInstance(ds.projection, pyproj.Proj)

if __name__ == "__main__":
unittest.main(failfast=True)
unittest.main()

0 comments on commit 588ba25

Please sign in to comment.