1- # code by M.Hauser
2-
31import warnings
42
53import cartopy .crs as ccrs
6- import cartopy .util as cutil
74import matplotlib .pyplot as plt
85import numpy as np
96import shapely .geometry as sgeom
@@ -127,14 +124,29 @@ def _is_monotonic(coord, axis=0):
127124# =============================================================================
128125
129126
130- def cyclic_dataarray (da , coord = "lon" ):
131- """
132- Add a cyclic coordinate point to a DataArray along a specified named dimension.
127+ def cyclic_dataarray (obj , coord = "lon" ):
128+ """Add a cyclic coordinate point to a DataArray or Dataset along a dimension.
129+
130+ Parameters
131+ ----------
132+ obj : xr.Dataset | xr.DataArray
133+ Object to add the cyclic data point to.
134+ coord : str, default: "lon"
135+ Name of the
133136
137+ Returns
138+ -------
139+ obj_cyclic : xr.Dataset | xr.DataArray
140+ The same as `obj` with a cyclic data point added.
141+
142+ Examples
143+ --------
134144 >>> import xarray as xr
135- >>> data = xr.DataArray([[1, 2, 3], [4, 5, 6]],
136- ... coords={'x': [1, 2], 'y': range(3)},
137- ... dims=['x', 'y'])
145+ >>> data = xr.DataArray(
146+ ... [[1, 2, 3], [4, 5, 6]],
147+ ... coords={'x': [1, 2], 'y': range(3)},
148+ ... dims=['x', 'y']
149+ ... )
138150 >>> data_cyclic = cyclic_dataarray(data, 'y')
139151 >>> data_cyclic
140152 <xarray.DataArray (x: 2, y: 4)>
@@ -144,38 +156,9 @@ def cyclic_dataarray(da, coord="lon"):
144156 * x (x) int64 1 2
145157 * y (y) int64 0 1 2 3
146158
147- Notes
148- -----
149- After: https://github.com/darothen/plot-all-in-ncfile/blob/master/plot_util.py
150-
151159 """
152- import xarray as xr
153160
154- assert isinstance (da , xr .DataArray )
155-
156- lon_idx = da .dims .index (coord )
157- cyclic_data , cyclic_coord = cutil .add_cyclic_point (
158- da .values , coord = da .coords [coord ], axis = lon_idx
159- )
160-
161- # Copy and add the cyclic coordinate and data
162- new_coords = dict (da .coords )
163- new_coords [coord ] = cyclic_coord
164- new_values = cyclic_data
165-
166- new_da = xr .DataArray (new_values , dims = da .dims , coords = new_coords )
167-
168- # Copy the attributes for the re-constructed data and coords
169- for att , val in da .attrs .items ():
170- new_da .attrs [att ] = val
171- for c in da .coords :
172- for att in da .coords [c ].attrs :
173- new_da .coords [c ].attrs [att ] = da .coords [c ].attrs [att ]
174-
175- return new_da
176-
177-
178- # =============================================================================
161+ return obj .pad ({coord : (0 , 1 )}, mode = "wrap" )
179162
180163
181164def ylabel_map (s , labelpad = None , size = None , weight = None , y = 0.5 , ax = None , ** kwargs ):
0 commit comments