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

Strange behavior with data from grib file #4549

Closed
JFMass opened this issue Jun 22, 2015 · 4 comments
Closed

Strange behavior with data from grib file #4549

JFMass opened this issue Jun 22, 2015 · 4 comments
Milestone

Comments

@JFMass
Copy link

JFMass commented Jun 22, 2015

Hi

I've been using matplotlib and basemaps with pygrib to plot meteorological data from grib files.

However, when using a particular grib, I get some strange behavior where a large horizontal strip of value 80 appears on the map. Digging into the problem I can tell the problem is not related to the data inside the grib file because grads plots perfectly. I could can also confirm that the problem doesn't seem to come from pyplot either because checking individual grid values show normal value where the map shows 80. I could identify that the plotted value is near 80 by playing with levels.

The only thing left would be with the contour and contourf functions.

preciptest

The data I am using 10km polar steregraphic projection rainfall data. Interestingly, 15km resolution files plot perfectly without any strip into the image.

Anyone can tell what is wrong?


#Retrieve gridded data from pygrib
grbs = pygrib.open('CMC_RDPA_APCP-006-final_SFC_0_ps10km_2015061906_000.grib2')
handle = grbs[1]
pltvar = handle.values
lat, lon = handle.latlons()

#troubleshooting to confirm data in grib is good where map show max values
print lat[365,765], lon[365,765], pltvar[365,765]
#46.0384291906 -70.7820353751 7.7229
#just as expected

#plot them with mpl/basemap
fig = plt.figure(figsize=(4.5,2.5),dpi=1024,frameon=False)
m=Basemap(epsg=3857,llcrnrlon=-80.15625,llcrnrlat=42.032974332441405,urcrnrlon=-54.84375,urcrnrlat=51.6180165487737,resolution='i')
x, y = m(lon, lat)
m.drawcountries()
m.drawcoastlines()
cs = m.contourf(x,y,pltvar,levels=(2,5,10,15,20,30,40,50,75,100))
plt.colorbar()
plt.savefig('preciptest.png',dpi=1024,transparent=True)
plt.close()

@tacaswell tacaswell added this to the proposed next point release milestone Jul 5, 2015
@tacaswell
Copy link
Member

Do the axes limits matter at all? There are some known bugs with some projections.

attn @pelson @WeatherGod @ianthomas23

@pelson
Copy link
Member

pelson commented Jul 5, 2015

It's hard to see with the map we have, but this is an exceedingly common problem with plotting geometries which touch/cross the dateline and/or poles.

Are you getting any warnings with the contourf call? I'm expecting you to get something like a non-monotonic warning?

The general solution to this is not to work in lat/lons - doing so can result in a loss of coordinate topology (due to the modulus 360). In practice, I don't know how well basemap deals with that. @WeatherGod might be able to help with that.

This kind of problem is the bread and butter of a package called cartopy (which I originally wrote), which deals with the geometry/contour topology problems that basemap's approach cannot. It might be also worth looking at that as an option.

Either way, this is not really a matplotlib issue, more of a https://github.com/matplotlib/basemap issue.

@efiring
Copy link
Member

efiring commented Jul 5, 2015

@JFMass, Try plotting without basemap: just make a simple call to contourf with lon and lat as your x and y. Check min and max for lon and lat; are the lons all negative, consistent with your choice of negative corner longitudes? Are the lons and lats monotonic? Have you made a simple plot of lon versus index?
In any case, I agree with @pelson that this is not a matplotlib problem, and I suspect it is fundamentally not a basemap problem either; there is something about your data that doesn't match your expectations, or basemap's requirements. It is almost certainly related to the longitude grid.

@efiring efiring closed this as completed Jul 5, 2015
@JFMass
Copy link
Author

JFMass commented Jul 14, 2015

Thanks for the help, couln'd solve the bug but I could work around by regridding with wgrib and then opening new grib.

Problem solved that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants