Skip to content

Adding cyclic point feature #82

Closed
Closed
@mickaellalande

Description

@mickaellalande

I will address the same issue as for Cartopy (SciTools/cartopy#1402). I would find extremely useful to have an automatic behavior of adding a cyclic point when needed, or at least add an option when doing a plot.

Here is an example of what I mean (data: ens_avg_snow.zip):

import xarray as xr
import proplot as plot

ens_avg_snow = xr.open_dataarray('ens_avg_snow.nc')

f, axs = plot.subplots(proj='cyl', width=8)
m = axs[0].contourf(ens_avg_snow, cmap='BuRd')
f.colorbar(m)
axs.format(
    geogridlinewidth=0.5, geogridcolor='gray8', geogridalpha=0.5, labels=True, 
    coast=True, ocean=True, oceancolor='gray3', latlim=(20,90),
)

proplot_cyclic_point

As you can see, there is a white line at 0° of longitude. I usually use the add_cyclic_point function of cartopy.utils but it is not straightforward for DataArrays because it doesn't keep the coordinates and attributes. I recently found some very useful code here: https://github.com/darothen/plot-all-in-ncfile/blob/master/plot_util.py that allows to do it easily with the function cyclic_dataarray and there is also a check_cyclic that could be used for automatically checking it.

Here is the function that allows adding a cyclic point (from the link above):

from cartopy.util import add_cyclic_point

# https://github.com/darothen/plot-all-in-ncfile/blob/master/plot_util.py
def cyclic_dataarray(da, coord='lon'):
    """ Add a cyclic coordinate point to a DataArray along a specified
    named coordinate dimension.
    """
    assert isinstance(da, xr.DataArray)

    lon_idx = da.dims.index(coord)
    cyclic_data, cyclic_coord = add_cyclic_point(da.values,
                                                 coord=da.coords[coord],
                                                 axis=lon_idx)

    # Copy and add the cyclic coordinate and data
    new_coords = dict(da.coords)
    new_coords[coord] = cyclic_coord
    new_values = cyclic_data

    new_da = xr.DataArray(new_values, dims=da.dims, coords=new_coords)

    # Copy the attributes for the re-constructed data and coords
    for att, val in da.attrs.items():
        new_da.attrs[att] = val
    for c in da.coords:
        for att in da.coords[c].attrs:
            new_da.coords[c].attrs[att] = da.coords[c].attrs[att]

    return new_da

Thus ens_avg_snow_cyclic = cyclic_dataarray(ens_avg_snow) allows to make back the previous plot without the white line. Incorporating this directly into ProPlot would be nice so that we can just add an option like for example add_cyclic=True (just need to be careful about the dimension to do the cyclic or have another way to pass it).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions