Skip to content

Commit 66fc459

Browse files
authored
Merge pull request #1149 from PCMDI/lee1043_improve_docstrings_20240926
Improve doc and enable more functions in API doc
2 parents e4f5aa2 + 241ea0d commit 66fc459

File tree

9 files changed

+391
-20
lines changed

9 files changed

+391
-20
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Release Notes and History
109109

110110
| <div style="width:300%">[Versions]</div> | Update summary |
111111
| ------------- | ------------------------------------- |
112+
| [v3.6.1] | Technical update, additional QC repair functions
112113
| [v3.6] | New capability (regional application of precip variability) and technical update
113114
| [v3.5.2] | Technical update, QC tools, new modes for modes of variability metrics (PSA1, PSA2)
114115
| [v3.5.1] | Technical update
@@ -147,6 +148,7 @@ Release Notes and History
147148

148149

149150
[Versions]: https://github.com/PCMDI/pcmdi_metrics/releases
151+
[v3.6.1]: https://github.com/PCMDI/pcmdi_metrics/releases/tag/v3.6.1
150152
[v3.6]: https://github.com/PCMDI/pcmdi_metrics/releases/tag/v3.6
151153
[v3.5.2]: https://github.com/PCMDI/pcmdi_metrics/releases/tag/v3.5.2
152154
[v3.5.1]: https://github.com/PCMDI/pcmdi_metrics/releases/tag/v3.5.1

docs/api.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ APIs for Developers
66

77
.. currentmodule:: pcmdi_metrics
88

9-
Below is a list of APIs available in `pcmdi_metrics (> v3.6)` for developers.
9+
Below is a list of APIs available in `pcmdi_metrics (> v3.6.1)` for developers.
10+
11+
Data load
12+
~~~~~~~~~
13+
.. autosummary::
14+
:toctree: generated/
15+
16+
io.xcdat_open
1017

1118

1219
Land-sea mask
@@ -29,7 +36,7 @@ Grid and regrid
2936

3037

3138
Custom calendars
32-
~~~~~~~~~~~~~~~
39+
~~~~~~~~~~~~~~~~
3340
.. autosummary::
3441
:toctree: generated/
3542

@@ -67,13 +74,14 @@ Retrieve data from xarray Dataset
6774
io.select_subset
6875

6976

70-
Quality control (QC)
71-
~~~~~~~~~~~~~~~~~~~~
77+
Quality control (QC) and repair
78+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7279
.. autosummary::
7380
:toctree: generated/
7481

7582
utils.check_daily_time_axis
7683
utils.check_monthly_time_axis
84+
utils.regenerate_time_axis
7785

7886

7987
Miscellaneous tools

pcmdi_metrics/io/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .xcdat_dataset_io import ( # noqa # isort:skip
88
da_to_ds,
99
get_axis_list,
10+
get_calendar,
1011
get_data_list,
1112
get_grid,
1213
get_latitude_bounds_key,

pcmdi_metrics/io/xcdat_dataset_io.py

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,8 @@ def get_time_bounds_key(ds: Union[xr.Dataset, xr.DataArray]) -> str:
172172
str
173173
The key for the time bounds.
174174
"""
175-
176-
lat_key = get_time_key(ds)
177-
return ds[lat_key].attrs["bounds"]
175+
time_key = get_time_key(ds)
176+
return ds[time_key].attrs["bounds"]
178177

179178

180179
def get_latitude_bounds_key(ds: Union[xr.Dataset, xr.DataArray]) -> str:
@@ -456,3 +455,63 @@ def get_grid(
456455
lat_bnds_key = get_latitude_bounds_key(d)
457456
lon_bnds_key = get_longitude_bounds_key(d)
458457
return d[[lat_key, lon_key, lat_bnds_key, lon_bnds_key]]
458+
459+
460+
def get_calendar(d: Union[xr.Dataset, xr.DataArray]) -> str:
461+
"""
462+
Get the calendar type from an xarray Dataset or DataArray.
463+
464+
Parameters
465+
----------
466+
d : xarray.Dataset or xarray.DataArray
467+
The input xarray object containing a time dimension.
468+
469+
Returns
470+
-------
471+
str
472+
The calendar type as a string (e.g., 'proleptic_gregorian', 'noleap', '360_day').
473+
474+
Raises
475+
------
476+
ValueError
477+
If no time dimension is found in the input.
478+
AttributeError
479+
If the calendar information cannot be extracted from the time dimension.
480+
481+
Notes
482+
-----
483+
This function first attempts to retrieve the calendar from the time dimension's
484+
encoding. If that fails, it tries to get the calendar from the time dimension's
485+
datetime accessor. If both methods fail, it raises an AttributeError.
486+
487+
Examples
488+
--------
489+
>>> import xarray as xr
490+
>>> import pandas as pd
491+
>>> dates = xr.cftime_range('2000-01-01', periods=365)
492+
>>> ds = xr.Dataset({'time': dates, 'data': ('time', range(365))})
493+
>>> get_calendar(ds)
494+
'standard'
495+
496+
See Also
497+
--------
498+
get_time : Function to extract the time dimension from a Dataset or DataArray.
499+
"""
500+
time = get_time(d)
501+
502+
if time is None:
503+
raise ValueError("No time dimension found in the input.")
504+
505+
try:
506+
calendar = time.encoding.get("calendar")
507+
if calendar is not None:
508+
return calendar
509+
except AttributeError:
510+
pass
511+
512+
try:
513+
return time.dt.calendar
514+
except AttributeError:
515+
raise AttributeError(
516+
"Unable to determine calendar type from the time dimension."
517+
)

pcmdi_metrics/io/xcdat_openxml.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,27 @@
1111
def xcdat_open(
1212
infile: Union[str, list], data_var: str = None, decode_times: bool = True
1313
) -> xr.Dataset:
14-
"""Open input file (netCDF, or xml generated by cdscan)
14+
"""
15+
Open input file (netCDF, or xml generated by cdscan)
1516
1617
Parameters
1718
----------
1819
infile : Union[str, list]
19-
list of string, or string, for path of file(s) to open using xcdat
20+
list of string, or string, for path of file(s) to open using xcdat.
2021
data_var : str, optional
21-
key of the non-bounds data variable to keep in the Dataset, alongside any existing bounds data variables, by default None, which loads all data variables
22+
key of the non-bounds data variable to keep in the Dataset, alongside any existing bounds data variables.
23+
By default None, which loads all data variables.
2224
decode_times : bool, optional
23-
If True, attempt to decode times encoded in the standard NetCDF datetime format into cftime.datetime objects. Otherwise, leave them encoded as numbers. This keyword may not be supported by all the backends, by default True
25+
If True, attempt to decode times encoded in the standard NetCDF datetime format into cftime.datetime objects.
26+
Otherwise, leave them encoded as numbers. This keyword may not be supported by all the backends, by default True.
2427
2528
Returns
2629
-------
2730
xr.Dataset
2831
xarray dataset opened via xcdat
2932
30-
Usage
31-
-----
33+
Examples
34+
--------
3235
>>> from pcmdi_metrics.io import xcdat_open
3336
# Open a single netCDF file
3437
>>> ds = xcdat_open('mydata.nc')

pcmdi_metrics/utils/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
generate_calendar_months,
66
subset_timesteps_in_custom_season,
77
)
8+
from .dates import (
9+
extract_date_components,
10+
find_overlapping_dates,
11+
regenerate_time_axis,
12+
replace_date_pattern,
13+
)
814
from .grid import (
915
calculate_area_weights,
1016
calculate_grid_area,

0 commit comments

Comments
 (0)