Sunday, October 1, 2017

Valid numpy arrays produces IndexError:

Leave a Comment

I am working on gfs weather data to plot certain parameters using python and matplotlib. Since it is a grib2 file, I am using wgrib2 for extracting data from file (even though I am aware of pygrib). These extracted variables (lat, lon and temp) are convered in to numpy arrary successfully. I am attaching these lat, lon and data values for your review. Portion of the code I am using for plotting is:-

lat = # load from attached lat file lon = # load from attached lon file data = # load data from data file plt.figure() m = Basemap(projection='mill', lat_ts=10, llcrnrlon=lon.min(), urcrnrlon=lon.max(), llcrnrlat=lat.min(), urcrnrlat=lat.max(),             resolution='c') x, y = m(lat, lon) cs = m.contourf(x, y, data) m.drawcoastlines() m.fillcontinents() m.drawmapboundary() plt.show() 

Now, when I use matplotlib countorf function for plotting, it produces the following error:-

File "wgrib2.py", line 145, in <module>     cs = m.contourf(x, y, data)   File "/usr/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py", line 521, in with_transform     return plotfunc(self,x,y,data,*args,**kwargs)   File "/usr/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py", line 3644, in contourf     xx = x[x.shape[0]/2,:] IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices 

Can someone help me to solve this issue?

Here is the minimal example as requested (actual files are uploaded in above link):-

#!/usr/bin/python2 # -*- coding: utf-8 -*- from __future__ import print_function, unicode_literals, division import os import subprocess import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap  lat = np.loadtxt("lat.txt") lon = np.loadtxt("lon.txt") data = np.loadtxt("data.txt") plt.figure() m = Basemap(projection='mill', lat_ts=10, llcrnrlon=lon.min(), urcrnrlon=lon.max(), llcrnrlat=lat.min(), urcrnrlat=lat.max(),             resolution='c') x, y = m(lat, lon) cs = m.contourf(x, y, data) m.drawcoastlines() m.fillcontinents() m.drawmapboundary() plt.show() 

Edit 1

Uploaded files again.

It appears to be an issue with dimension of the data. The output generate by wgrib2 to numpy array is a single dimension with value of (259920,) for lat, lon and data. I have checked with pygrib and it produces the shape of (720, 361) which leads to 720 x 361 = 259920. Therefore, it seems that I have an issue with the data being converted in to numpy.

1 Answers

Answers 1

Your code does not produce an error on Python 3.5 with basemap 1.1.0, so this should be fixed if you upgrade your basemap. If you are actually running Python 2 (which is suggested by your minimal example), then it may be sufficient to remove division from your __future__ imports.

In particular, this MCVE does not produce an error:

from __future__ import print_function, unicode_literals, division import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap  lat = np.linspace(-90, 90, 180) lon = np.linspace(-180, 180, 360) lat, lon = np.meshgrid(lat, lon) data = np.random.randn(360, 180) plt.figure() m = Basemap(projection='mill', lat_ts=10, llcrnrlon=lon.min(), urcrnrlon=lon.max(), llcrnrlat=lat.min(), urcrnrlat=lat.max(),             resolution='c') x, y = m(lon, lat) cs = m.contourf(x, y, data) m.drawcoastlines() m.fillcontinents() m.drawmapboundary() plt.show() 

If for some reason that doesn't work and you want to patch the issue erroring right here, you can go into the basemap source code and change xx = x[x.shape[0]/2,:] to xx = x[int(x.shape[0]/2),:]. What I heard from Ben suggests that if you do this, you're likely to continue to run in to other compatibility problems (possibly right after you make that fix), but it might get this code working for now.

Keep in mind that basemap does not support Python 3, and is deprecated. The core developer of Basemap (Ben Root) is urging users to stop using it, because it's been abandoned and will not be ported to Python 3, even when Python 2 stops receiving security updates in 2020. He suggests using cartopy instead. All this was said by Ben Root at Scipy 2017.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment