pygrib code

mail@pastecode.io avatar
unknown
python
2 years ago
1.9 kB
6
Indexable
Never
import matplotlib.pyplot as plt
import xarray as xr
import requests
import time
import pygrib
import metpy
import numpy as np

datestr = time.strftime('%Y%m%d')

#REQUEST FOR GFS GRIB FILE
gfs_url = 'https://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_0p25_1hr.pl?file=gfs.t00z.pgrb2.0p25.f001&lev_surface=on&var_TMP=on&leftlon=0&rightlon=360&toplat=90&bottomlat=-90&dir=%2Fgfs.'+datestr+'%2F00%2Fatmos'
r = requests.get(gfs_url, allow_redirects=True)
open('gfs.txt', 'wb').write(r.content)

#REQUEST FOR HRRR GRIB FILE
hrrr_url = 'https://nomads.ncep.noaa.gov/cgi-bin/filter_hrrr_2d.pl?file=hrrr.t00z.wrfsfcf01.grib2&lev_surface=on&var_TMP=on&leftlon=0&rightlon=360&toplat=90&bottomlat=-90&dir=%2Fhrrr.'+datestr+'%2Fconus'
r = requests.get(hrrr_url, allow_redirects=True)
open('hrrr.txt', 'wb').write(r.content)

#XARRAY LOAD GRIBS INTO DATASETS
ds = xr.load_dataset("hrrr.grib2", engine="cfgrib",filter_by_keys={'typeOfLevel': 'surface'})
gfs_ds = xr.load_dataset("gfs.grib2", engine="cfgrib",filter_by_keys={'typeOfLevel': 'surface'})

#THIS IS THE LINE GIVING TROUBLE. the error that i'm getting is "AttributeError: module 'pygrib' has no attribute 'open'"
gr = pygrib.open("hrrr.grib2")

#LOAD IN NETCDF DOWNSCALE RATIOS
prism_ds = xr.load_dataset('/Users/clamalo/documents/us/us_prism_0.125_1_area.nc')

air = prism_ds.pratio

#SET UP PLOT PARAMETERS
f, ((ax1, ax2)) = plt.subplots(2, figsize=(160, 192))

#FIRST PLOT (DOWNSCALE RATIOS)
air.plot(ax=ax1, cbar_kwargs={"label": "downscale ratio"},cmap='coolwarm',vmin=0, vmax=2)
ax1.set_title("downscale ratio")
#ax1.margins(x=0, y=-0.25)
ax1.set_xlabel("x grid number")
ax1.set_ylabel("y grid number")

#SECOND PLOT (HRRR AIR TEMP)
air = ds.t
airc = air - 273.15
airc.plot(ax=ax2, cbar_kwargs={"label": "°C"}, cmap='RdBu')
ax2.set_title("HRRR degrees celsius")
ax2.set_xlabel("x grid number")
ax2.set_ylabel("y grid number")

#PLOT DATA
plt.tight_layout()
plt.savefig("test.png")