dopplershift

GRIB Plotting

Mar 21st, 2022
1,622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. from datetime import datetime
  2. from urllib.request import urlopen
  3.  
  4. import cartopy.crs as ccrs
  5. import matplotlib.pyplot as plt
  6. import numpy as np
  7. import pygrib
  8. import xarray as xr
  9.  
  10. # Download the GRIB file from the webserver and save to a local file
  11. fname = 'forecast.bin'
  12. now = datetime.utcnow()
  13. with open(fname, 'wb') as cache_file:
  14.     fobj = urlopen(f'https://nomads.ncep.noaa.gov/pub/data/nccf/com/hrrr/prod/hrrr.{now:%Y%m%d}/conus/hrrr.t00z.wrfnatf00.grib2')
  15.     cache_file.write(fobj.read())
  16.  
  17. # Open grib and get the first message
  18. grb = pygrib.open(fname)
  19. msg = grb[1]
  20.  
  21. # Set up the projection information using information from the GRIB message
  22. globe = ccrs.Globe(semimajor_axis=msg['radius'])
  23. data_proj = ccrs.LambertConformal(globe=globe, central_latitude=msg['LaDInDegrees'],
  24.                                   central_longitude=msg['LoVInDegrees'], standard_parallels=(msg['Latin1InDegrees'], msg['Latin2InDegrees']))
  25.  
  26. # Transform the corner point lat/lon to an x/y in the projected coordinates
  27. x0, y0 = data_proj.transform_point(msg['longitudeOfFirstGridPointInDegrees'],
  28.                                    msg['latitudeOfFirstGridPointInDegrees'],
  29.                                    src_crs=ccrs.PlateCarree())
  30.  
  31. # Generate the grid x/y from that corner using the number of point and the spacing
  32. x = x0 + np.arange(msg['Nx']) * msg['DxInMetres']
  33. y = y0 + np.arange(msg['Ny']) * msg['DyInMetres']
  34.  
  35. # Pull valid time out to use as a coordinate
  36. t = msg.validDate
  37.  
  38. # Grab lat/lons from pygrib
  39. lats, lons = msg.latlons()
  40.  
  41. # Create an xarray DataArray from the coordinates and information in the message
  42. data = xr.DataArray(data=msg.values, dims=["y", "x"],
  43.                     coords=dict(x=x, y=y, time=t, latitude=(('y', 'x'), lats), longitude=(('y', 'x'), lons)),
  44.                     attrs=dict(parameter=msg['parameter'], unit=msg['units']))
  45.  
  46. # Convert a lon/lat into the x/y coordinates
  47. lat_pt, lon_pt = (40, -105)
  48. x_pt, y_pt = data_proj.transform_point(lon_pt, lat_pt, ccrs.PlateCarree())
  49.  
  50. # Use that point to select
  51. my_value = data.sel(x=x_pt, y=y_pt, method='nearest')
  52.  
  53. # Plot on a map to verify we got this right
  54. fig = plt.figure()
  55. ax = fig.add_subplot(1, 1, 1, projection=data_proj)
  56.  
  57. # Plot the data
  58. ax.pcolormesh(data.x, data.y, data)
  59.  
  60. # Plot original point in lon/lat space
  61. ax.plot(lon_pt, lat_pt, 'bs', markersize=20, transform=ccrs.PlateCarree())
  62.  
  63. # Plot point after transforming manually
  64. ax.plot(x_pt, y_pt, 'ro')
  65.  
  66. ax.coastlines()
Advertisement
Add Comment
Please, Sign In to add comment