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

ImagePlot fails on some [0, 360] data #1915

Open
dopplershift opened this issue Jun 16, 2021 · 4 comments
Open

ImagePlot fails on some [0, 360] data #1915

dopplershift opened this issue Jun 16, 2021 · 4 comments
Labels
Area: Plots Pertains to producing plots Type: Bug Something is not working like it should
Milestone

Comments

@dopplershift
Copy link
Member

dopplershift commented Jun 16, 2021

This example:

from metpy.plots import ImagePlot, MapPanel, PanelContainer
from metpy.units import units
import xarray as xr

ds_z500 = xr.open_dataset('/Users/rmay/Downloads/gfs.t00z.pgrb2.0p25.f024', engine='cfgrib', filter_by_keys={'typeOfLevel': 'isobaricInhPa', 'level': 500})

img = ImagePlot()
img.data = ds_z500
img.field = 't'
img.colormap = 'plasma'

panel = MapPanel()
panel.area = 'us'
panel.title = 'GFS 500mb Temp Forecast Example'
panel.plots = [img]

pc = PanelContainer()
pc.size = (10,8)
pc.panels = [panel]
pc.show()

produces the following with 1.0 and main:
image

Taken from a Stack Overflow question. This pretty clearly relates to this code that wraps longitude:

@property
def plotdata(self):
"""Return the data for plotting.
The data array, x coordinates, and y coordinates.
"""
x = self.griddata.metpy.x
y = self.griddata.metpy.y
# At least currently imshow with cartopy does not like this
if 'degree' in x.units:
x = x.data
x[x > 180] -= 360
return x, y, self.griddata

because without it the same code gives:
image

The code is pretty clearly incorrect because it results in an x array where x[0] is 0 and x[-1] is -0.25, since this full dataset has a longitude range of [0, 360].

@dopplershift dopplershift added Type: Bug Something is not working like it should Area: Plots Pertains to producing plots labels Jun 16, 2021
@dopplershift dopplershift added this to the 1.1.0 milestone Jun 16, 2021
@dopplershift
Copy link
Member Author

Of course it's not as simple as removing that bogus wrapping code, as that breaks a the test_latlon test:

from datetime import datetime

from metpy.cbook import get_test_data
from metpy.plots import ContourPlot, ImagePlot, MapPanel, PanelContainer
from metpy.units import units
import xarray as xr

data = xr.open_dataset(get_test_data('irma_gfs_example.nc', as_file_obj=False))

img = ImagePlot()
img.data = data
img.field = 'Temperature_isobaric'
img.level = 500 * units.hPa
img.time = datetime(2017, 9, 5, 15, 0, 0)
#img.colorbar = None

contour = ContourPlot()
contour.data = data
contour.field = 'Geopotential_height_isobaric'
contour.level = img.level
contour.time = img.time

panel = MapPanel()
#panel.projection = 'lcc'
panel.area = 'global'
panel.plots = [img, contour]

pc = PanelContainer(size=(8,5))
pc.panel = panel
pc.draw()

produces
image

so that's lovely.

@dopplershift
Copy link
Member Author

Everything works fine if I change the extent of the image data (either with extent manually or in the data) from [250, 315] to [-110, -45 ]. It also behaves more as expected if the range is [179, 315].

I've so far traced this into CartoPy's image warping code. It seems to be an issue where we're using the fact that PlateCarree can (sort of) handle longitudes in [0, 360], but when you transform points back into PlateCarree, they're in [-180, 180]. I'll continue digging into CartoPy to try to figure out how to make it not completely broken (seeing as NCEP produces data that are natively [0, 360]).

In the meanwhile, though, we need to unbreak the original example with MetPy, but also keep the test case working. This probably means writing some code to properly "roll" an image from [0, 360] to [-180, 180].

@eliteuser26
Copy link
Contributor

eliteuser26 commented Aug 10, 2021

I had to face this issue in the past where I had to wrestle with plotting with longitudes from -180 to 180 degrees. When plotting on a map with this set of longitudes it would warp the lines and wrap around the globe when taken from platecarree. The only solution that I found that prevented these problems was to convert the longitudes to 0 to 360 degrees and it works over the poles too. I tried both ways and looked at the results. It was quite convincing.

@dopplershift dopplershift modified the milestones: 1.2.0, 1.3.0 Jan 14, 2022
@dopplershift dopplershift modified the milestones: 1.3.0, May 2022 Apr 6, 2022
@dopplershift
Copy link
Member Author

Jotting down a thought here:

  • Add a method to our xarray accessor(s) to use CartoPy's add_cyclic_point (or implement our own for dependency avoidance)
  • Use that to avoid any need for hacks to deal with this

@dopplershift dopplershift modified the milestones: May 2022, July 2022 Jun 1, 2022
@dopplershift dopplershift modified the milestones: 1.4.1, April 2023 Mar 16, 2023
@dopplershift dopplershift modified the milestones: April 2023, July 2023 Apr 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Plots Pertains to producing plots Type: Bug Something is not working like it should
Projects
Status: No status
Development

No branches or pull requests

2 participants