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

Quick contourf #87

Merged
merged 11 commits into from
Apr 24, 2020
2 changes: 1 addition & 1 deletion .pep8speaks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ scanner:
linter: pycodestyle # Other option is flake8

pycodestyle: # Same as scanner.linter value. Other option is flake8
max-line-length: 100 # Default is 79 in PEP 8
max-line-length: 120 # Default is 79 in PEP 8
ignore: # Errors and warnings to ignore
- W504 # line break after binary operator
- E402 # module level import not at top of file
Expand Down
2 changes: 1 addition & 1 deletion meta.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% set name = "monet" %}
{% set version = "2.2.0" %}
{% set version = "2.2.1" %}

package:
name: monet
Expand Down
189 changes: 138 additions & 51 deletions monet/monet_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ def _rename_latlon(ds):
return ds


def _monet_to_latlon(da):
if isinstance(da, xr.DataArray):
dset = da.to_dataset()
dset['x'] = da.longitude[0, :].values
dset['y'] = da.latitude[:, 0].values
dset = dset.drop(['latitude', 'longitude'])
dset = dset.set_coords(['x', 'y'])
dset = dset.rename({'x': 'lon', 'y': 'lat'})
if isinstance(da, xr.DataArray):
return dset[da.name]
else:
return dset


def _dataset_to_monet(dset,
lat_name='latitude',
lon_name='longitude',
Expand Down Expand Up @@ -1008,106 +1022,179 @@ def _check_kwargs_and_set_defaults(**kwargs):
kwargs['filename'] = 'monet_xesmf_regrid_file.nc'
return kwargs

def quick_imshow(self, map_kws={}, center=True, **kwargs):
"""Creates a quick map view of a given data array.
def quick_imshow(self, map_kws={}, roll_dateline=False, **kwargs):
"""This function takes an xarray DataArray and quickly cerates a figure
using cartopy and the matplotlib imshow. Note that this should only be used for
regular grids.

Parameters
----------
map_kws : dict
kwargs for monet.plots.mapgen.draw_map.
**kwargs : dict
kwargs for xarray plotting.
map_kws : dictionary
kwargs for monet.plots.mapgen.draw_map
roll_dateline : bool
roll_dateline is meant to help with global datasets that the longitudes
range from 0 to 360 instead of -180 to 180. Otherwise a white line appears
at 0 degrees.
**kwargs :
kwargs for the xarray.DataArray.plot.imshow function

Returns
-------
matplotlib.axes
return of the axes handle for matplotlib.
axes

"""
from .plots.mapgen import draw_map
from .plots import _dynamic_fig_size
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import seaborn as sns
sns.set_context('notebook', font_scale=1.2)
# da = _dataset_to_monet(self._obj)
da = _rename_to_monet_latlon(self._obj)
da = _dataset_to_monet(self._obj)
da = _monet_to_latlon(da)
crs_p = ccrs.PlateCarree()
if 'crs' not in map_kws:
if ~center:
central_longitude = float(da.longitude.mean().values)
map_kws['crs'] = ccrs.PlateCarree(
central_longitude=central_longitude)
else:
map_kws['crs'] = ccrs.PlateCarree()
map_kws['crs'] = crs_p
if 'figsize' in kwargs:
map_kws['figsize'] = kwargs['figsize']
kwargs.pop('figsize', None)
else:
figsize = _dynamic_fig_size(da)
map_kws['figsize'] = figsize
if 'extent' in kwargs:
kwargs.pop('extent')
map_kws['extent'] = [da.longitude.min(), da.longitude.max(), da.latitude.min(), da.latitude.max()]
f, ax = draw_map(return_fig=True, **map_kws)
ax = da.plot.imshow(x='longitude', y='latitude', ax=ax,
transform=ccrs.PlateCarree(),
**kwargs)
if 'transform' not in kwargs:
transform = crs_p
else:
transform = kwargs['transform']
kwargs.pop('transform', None)
ax = draw_map(**map_kws)
try:
ax.axes.outline_patch.set_alpha(0)
except:
except AttributeError:
ax.outline_patch.set_alpha(0)
self._tight_layout()
if roll_dateline:
ax = da.roll(lon=int(len(da.lon) / 2), roll_coords=True).plot.imshow(ax=ax,
transform=transform,
**kwargs)
else:
ax = da.plot.imshow(ax=ax, transform=transform, **kwargs)
plt.tight_layout()
return ax

def quick_map(self, map_kws={}, center=True, **kwargs):
"""Creates a quick map view of a given data array.
def quick_map(self, map_kws={}, roll_dateline=False, **kwargs):
"""This function takes an xarray DataArray and quickly cerates a figure
using cartopy and the matplotlib pcolormesh

Parameters
----------
map_kws : dict
kwargs for monet.plots.mapgen.draw_map.
**kwargs : dict
kwargs for xarray plotting.
map_kws : dictionary
kwargs for monet.plots.mapgen.draw_map
roll_dateline : bool
roll_dateline is meant to help with global datasets that the longitudes
range from 0 to 360 instead of -180 to 180. Otherwise a white line appears
at 0 degrees.
**kwargs :
kwargs for the xarray.DataArray.plot.pcolormesh function

Returns
-------
matplotlib.axes
return of the axes handle for matplotlib.
axes

"""
from .plots.mapgen import draw_map
from .plots import _dynamic_fig_size
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import seaborn as sns
sns.set_context('notebook', font_scale=1.2)
sns.set_context('notebook')
da = _dataset_to_monet(self._obj)
crs_p = ccrs.PlateCarree()
if 'crs' not in map_kws:
if ~center:
central_longitude = float(da.longitude.mean().values)
map_kws['crs'] = ccrs.PlateCarree(
central_longitude=central_longitude)
else:
map_kws['crs'] = ccrs.PlateCarree()
map_kws['crs'] = crs_p
if 'figsize' in kwargs:
map_kws['figsize'] = kwargs['figsize']
kwargs.pop('figsize', None)
else:
figsize = _dynamic_fig_size(da)
map_kws['figsize'] = figsize
if 'extent' in kwargs:
kwargs.pop('extent')
map_kws['extent'] = [da.longitude.min(), da.longitude.max(), da.latitude.min(), da.latitude.max()]
f, ax = draw_map(return_fig=True, **map_kws)
ax = _rename_to_monet_latlon(da).plot(x='longitude',
y='latitude',
ax=ax,
transform=ccrs.PlateCarree(),
infer_intervals=True,
**kwargs)
if 'transform' not in kwargs:
transform = crs_p
else:
transform = kwargs['transform']
kwargs.pop('transform', None)
ax = draw_map(**map_kws)
try:
ax.axes.outline_patch.set_alpha(0)
except:
except AttributeError:
ax.outline_patch.set_alpha(0)
self._tight_layout()
if roll_dateline:
ax = da.roll(x=int(len(da.x) / 2), roll_coords=True).plot(x='longitude',
y='latitude',
ax=ax,
transform=crs_p,
**kwargs)
else:
ax = da.plot(x='longitude', y='latitude', ax=ax, transform=crs_p, **kwargs)
plt.tight_layout()
return ax

def quick_contourf(self, map_kws={}, roll_dateline=False, **kwargs):
"""This function takes an xarray DataArray and quickly cerates a figure
using cartopy and the matplotlib contourf

Parameters
----------
map_kws : dictionary
kwargs for monet.plots.mapgen.draw_map
roll_dateline : bool
roll_dateline is meant to help with global datasets that the longitudes
range from 0 to 360 instead of -180 to 180. Otherwise a white line appears
at 0 degrees.
**kwargs :
kwargs for the xarray.DataArray.plot.contourf function

Returns
-------
type
axes

"""
from monet.plots.mapgen import draw_map
from monet.plots import _dynamic_fig_size
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import seaborn as sns
sns.set_context('notebook')
da = _dataset_to_monet(self._obj)
crs_p = ccrs.PlateCarree()
if 'crs' not in map_kws:
map_kws['crs'] = crs_p
if 'figsize' in kwargs:
map_kws['figsize'] = kwargs['figsize']
kwargs.pop('figsize', None)
else:
figsize = _dynamic_fig_size(da)
map_kws['figsize'] = figsize
if 'transform' not in kwargs:
transform = crs_p
else:
transform = kwargs['transform']
kwargs.pop('transform', None)
ax = draw_map(**map_kws)
try:
ax.axes.outline_patch.set_alpha(0)
except AttributeError:
ax.outline_patch.set_alpha(0)
if roll_dateline:
ax1 = da.roll(x=int(len(da.x) / 2), roll_coords=True).plot.contourf(x='longitude',
y='latitude',
ax=ax,
transform=transform,
**kwargs)
else:
ax1 = da.plot.contourf(x='longitude', y='latitude', ax=ax, transform=transform, **kwargs)

plt.tight_layout()
return ax

def _tight_layout(self):
Expand Down Expand Up @@ -1743,7 +1830,7 @@ def stratify(self, levels, vertical, axis=1):
"""Short summary.

Parameters
----------
----------
levels : type
Description of parameter `levels`.
vertical : type
Expand Down
3 changes: 3 additions & 0 deletions monet/plots/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def _dynamic_fig_size(obj):
elif 'latitude' in obj.dims:
nx, ny = len(obj.longitude), len(obj.latitude)
scale = float(ny) / float(nx)
elif 'lat' in obj.dims:
nx, ny = len(obj.lon), len(obj.lat)
scale = float(ny) / float(nx)
figsize = (10, 10 * scale)
return figsize

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from distutils.core import setup

setup(name='monet',
version='2.2.0',
version='2.2.1',
url='https://github.com/noaa-oar-arl/MONET',
license='MIT',
include_package_data=True,
Expand Down