As the error message says, the problem is on Line 14:
print f.variables['WWSWHGT_P0_L1_GLL0']
a KeyError means that you tried to access an element that is not in a
dictionary. In this case "f.variables" is the dictionary and "'
WWSWHGT_P0_L1_GLL0'" is the element.
Did your data and script come of the same place? You can't just throw any
basemap script at any grid file.
On Thu, Jan 9, 2014 at 4:52 AM, Rolling Six <surfersh...@hotmail.com> wrote:
> Hi im new to Python and basemap i am trying to follow the script below that
> was posted in another thread here
> <http://matplotlib.1069221.n5.nabble.com/Plotting-NOAA-data-td19588.html>
> but im getting this error
>
> alec@alec-imedia-S2870:~$ python Desktop/test.py
> /usr/lib/pymodules/python2.7/mpl_toolkits/__init__.py:2: UserWarning:
> Module
> dap was already imported from None, but /usr/lib/python2.7/dist-packages is
> being added to sys.path
> __import__('pkg_resources').declare_namespace(__name__)
> a
> fatal: environment variable not set
> b
> ['VGRD_P0_L1_GLL0', 'WVDIR_P0_L1_GLL0', 'lon_0', 'PERSW_P0_L1_GLL0',
> 'WIND_P0_L1_GLL0', 'PERPW_P0_L1_GLL0', 'WDIR_P0_L1_GLL0', 'forecast_time0',
> 'DIRPW_P0_L1_GLL0', 'WVPER_P0_L1_GLL0', 'DIRSW_P0_L1_GLL0',
> 'HTSGW_P0_L1_GLL0', 'UGRD_P0_L1_GLL0', 'lat_0']
> Traceback (most recent call last):
> File "Desktop/test.py", line 14, in <module>
> datavar = f.variables['WWSWHGT_P0_L1_GLL0']
> KeyError: 'WWSWHGT_P0_L1_GLL0'
>
> The script im trying to run below
>
> import Nio
> from mpl_toolkits.basemap import Basemap
> import matplotlib.pyplot as plt
> import numpy as np
> f = Nio.open_file('akw.t00z.grib.grib2')
> print f.variables.keys()
> lons = f.variables['lon_0'][:]
> # flip latitudes so data goes S-->N
> lats = f.variables['lat_0'][::-1]
> times = f.variables['forecast_time0'][:]
> datavar = f.variables['WWSWHGT_P0_L1_GLL0']
> ntime = 10
> data = datavar[ntime,::-1]
> print f.variables['WWSWHGT_P0_L1_GLL0']
> print data.min(), data.max()
> m = Basemap(projection='cyl',llcrnrlat=lats[0],llcrnrlon=lons[0],\
> urcrnrlat=lats[-1],urcrnrlon=lons[-1],resolution='l')
> x, y = m(*np.meshgrid(lons, lats))
> levels = np.arange(0,9.1,0.5)
> m.contourf(x,y,data,levels)
> m.drawcoastlines()
> m.fillcontinents()
> m.drawparallels(np.arange(40,81,10),labels=[1,0,0,0])
> m.drawmeridians(np.arange(150,241,10),labels=[0,0,0,1])
> m.drawparallels(np.arange(40,81,10),labels=[1,0,0,0])
> m.drawmeridians(np.arange(150,241,10),labels=[0,0,0,1])
> plt.title(datavar.long_name+' %s hr fcst'%(times[ntime]),fontsize=12)
> plt.colorbar(orientation='horizontal',shrink=0.9,format="%g")
> plt.show()
>
>
>
>
>
>
>
> --
> View this message in context:
> http://matplotlib.1069221.n5.nabble.com/Plotting-NOAA-grib2-data-in-basemap-tp42698.html
> Sent from the matplotlib - users mailing list archive at Nabble.com.
>
>
> ------------------------------------------------------------------------------
> CenturyLink Cloud: The Leader in Enterprise Cloud Services.
> Learn Why More Businesses Are Choosing CenturyLink Cloud For
> Critical Workloads, Development Environments & Everything In Between.
> Get a Quote or Start a Free Trial Today.
>
> http://pubads.g.doubleclick.net/gampad/clk?id=119420431&iu=/4140/ostg.clktrk
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
------------------------------------------------------------------------------
CenturyLink Cloud: The Leader in Enterprise Cloud Services.
Learn Why More Businesses Are Choosing CenturyLink Cloud For
Critical Workloads, Development Environments & Everything In Between.
Get a Quote or Start a Free Trial Today.
http://pubads.g.doubleclick.net/gampad/clk?id=119420431&iu=/4140/ostg.clktrk
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users