Data not fitted to Basemap

313 Views Asked by At

When I try to plot data using Basemap from a Grib file, the map is not fitted to the data being plotted. I posted the code used below and and a link to the output image below that. I think the projection type might be the issue. I also tried making the projection cylindrical while keeping the rest of the code the same, but that didn't work either (though it looked a little better) and I posted a link to the output image for that as well. Maybe this helps to visualize what projection will properly fit the data? Any ideas as to what you think might have gone wrong would be appreciated.

from pprint import pprint
import pygrib
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from ncepgrib2 import Grib2Decode


file = 'hrrr.t00z.wrfsfcf00.grib2'
gr = pygrib.open(file)


msg = gr[32] #Temperature values in Kelvin
#print(Grib2Decode(msg.tostring(),gribmsg=True))#Similar to printing all info for netCDF file
Temp = msg.values

lat, lon = msg.latlons()

m=Basemap(projection='lcc',llcrnrlon=lon.min(), \
  urcrnrlon=lon.max(),llcrnrlat=lat.min(),urcrnrlat=lat.max(), \
  lat_0 = float(msg['latitudeOfFirstGridPointInDegrees']),lon_0 =float(msg['longitudeOfFirstGridPointInDegrees']) ,resolution='c')

x,y = m(lon,lat)


                                                                                                                                                                                                                                                                                                                                                                                          #m = Basemap(width=11297120.0,height=8959788.0,
#           resolution='c',projection='lcc',\
#            lat_ts=40,lat_0=lat_0,lon_0=lon_0)



fig = plt.figure(figsize = (18.6,10.5))
cs = m.pcolormesh(x,y,(Temp-273.15)*9/5 +32,cmap = plt.cm.jet)
m.drawcoastlines()
m.drawstates()
m.drawcountries()
plt.colorbar(cs,orientation='vertical')
plt.title('Temperature F')
plt.savefig('plot')
plt.show()                                                                                           

Image of output using lcc projection Image of output using cylindrical projection Link to HRRR data containing Grib file. All are in the exact same format, projection, etc... so you could download any one of them and get the same results (just different Temperature values)

1

There are 1 best solutions below

8
On BEST ANSWER

There are different ways to solve your problem:

  1. Setting xlim and ylim which is the easiest way for you:
plt.xlim([min_lon, max_lon])
plt.ylim([min_lat, max_lat])
  1. Use cfgrib+xarray to plot directly from the xarray object