Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated several docstrings to Xarray style #118

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
autoapi_dirs = [ '../../trajan' ]
autoapi_keep_files = False # set to True when debugging autoapi generated files
autoapi_python_class_content = 'both'
autodoc_typehints = 'description'
autodoc_typehints = 'none'

import warnings
warnings.filterwarnings("ignore", category=UserWarning,
Expand Down Expand Up @@ -64,6 +64,7 @@
'scipy': ('https://docs.scipy.org/doc/scipy', None),
'cftime': ('https://unidata.github.io/cftime', None),
'sphinx': ('https://www.sphinx-doc.org/en/master/', None),
'pyproj': ('https://pyproj4.github.io/pyproj/stable/', None),
'xarray': ('https://xarray.pydata.org/en/stable/', None),
'pandas': ("https://pandas.pydata.org/pandas-docs/stable", None),
}
Expand Down Expand Up @@ -95,7 +96,7 @@
napoleon_google_docstring = False
napoleon_numpy_docstring = True
napoleon_use_param = False
napoleon_use_ivar = True
napoleon_use_ivar = False
napoleon_use_rtype = False
napoleon_preprocess_types = True
napoleon_type_aliases = {
Expand Down
68 changes: 50 additions & 18 deletions trajan/traj.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def animate(self):
@property
def tx(self):
"""
Trajectory x coordinates (usually longitude) - test.
Trajectory x coordinates (usually longitude).

See Also
--------
Expand Down Expand Up @@ -121,6 +121,10 @@ def ty(self):
def tlon(self):
"""
Retrieve the trajectories in geographic coordinates (longitudes).

See Also
--------
tx, tlat
"""
if self.crs.is_geographic:
return self.tx
Expand All @@ -137,6 +141,10 @@ def tlon(self):
def tlat(self) -> xr.DataArray:
"""
Retrieve the trajectories in geographic coordinates (latitudes).

See Also
--------
ty, tlon
"""
if self.crs.is_geographic:
return self.ty
Expand All @@ -155,14 +163,14 @@ def transform(self, to_crs, x, y):

Parameters
----------
to_crs : pyproj.CRS
to_crs : pyproj.crs.CRS

x, y : arrays
x, y : array-like
Coordinates in `self` CRS

Returns
-------
xn, yn : arrays
xn, yn : array-like
Coordinates in `to_crs`
"""
t = pyproj.Transformer.from_crs(self.crs, to_crs, always_xy=True)
Expand All @@ -174,23 +182,27 @@ def itransform(self, from_crs, x, y):

Parameters
----------
from_crs : `pyproj.CRS`
from_crs : pyproj.crs.CRS

x, y : arrays
Coordinates in `from_crs` CRS
x, y : array-like
Coordinates in from_crs CRS

Returns
-------
xn, yn : arrays
xn, yn : array-like
Coordinates in this datasets CRS
"""
t = pyproj.Transformer.from_crs(from_crs, self.crs, always_xy=True)
return t.transform(x, y)

@property
def crs(self) -> pyproj.CRS:
def crs(self) -> pyproj.crs.CRS:
"""
Retrieve the pyproj.CRS object from the CF-defined grid-mapping in the dataset.
Retrieve the pyproj.crs.CRS object from the CF-defined grid-mapping in the dataset.

Returns
-------
pyproj.crs.CRS
"""
if len(self.ds.cf.grid_mapping_names) == 0:
logger.debug(
Expand All @@ -208,23 +220,24 @@ def crs(self) -> pyproj.CRS:
else:
gm = self.ds.cf['grid_mapping']
logger.debug(f'Constructing CRS from grid_mapping: {gm}')
return pyproj.CRS.from_cf(gm.attrs)
return pyproj.crs.CRS.from_cf(gm.attrs)

def set_crs(self, crs) -> xr.Dataset:
"""
Returns a new dataset with the CF-supported grid-mapping / projection set to `crs`.

Parameters
----------
crs: pyproj.CRS
crs : pyproj.crs.CRS

Returns
-------
updated dataset

.. warning::
Dataset
Updated dataset

This does not transform the coordinates, make sure that `crs` is matching the data in the dataset.
Warning
-------
This does not transform the coordinates, make sure that `crs` is matching the data in the dataset.
"""

# TODO: Ideally this would be handled by cf-xarray or rio-xarray.
Expand Down Expand Up @@ -282,6 +295,19 @@ def assign_cf_attrs(self,
**kwargs) -> xr.Dataset:
"""
Return a new dataset with CF-standard and common attributes set.

Parameters
----------
*kwargs
Attribute names and values

Returns
-------
Dataset
Updated dataset with provided attributes, in addition to several CF standard attributes,
including Conventions, featureType, geospatial_lat_min etc.


"""
ds = self.ds.copy(deep=True)

Expand Down Expand Up @@ -332,7 +358,13 @@ def assign_cf_attrs(self,
return ds

def index_of_last(self):
"""Find index of last valid position along each trajectory."""
"""Find index of last valid position along each trajectory.

Returns
-------
array-like
Array of the index of the last valid position along each trajectory.
"""
return np.ma.notmasked_edges(np.ma.masked_invalid(self.ds.lon.values),
axis=1)[1][1]

Expand Down Expand Up @@ -612,7 +644,7 @@ def skill(self, other, method='liu-weissberg', **kwargs) -> xr.Dataset:
Returns
-------

skill : :class:`xarray.Dataset`
skill : Dataset
The skill-score in the same dimensions as this dataset.

Notes
Expand Down
Loading