本文整理汇总了Python中pygrib.open函数的典型用法代码示例。如果您正苦于以下问题:Python open函数的具体用法?Python open怎么用?Python open使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了open函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, center=None, radius=None, forecast_date=None, gfs_data_file=None, hrrr_data_file=None, keep_files=False):
"""
Construct a wind model centered at a given Latitude/Longitude with a radius defined in miles
:param center:
:param radius:
:param forecast_date:
"""
if gfs_data_file is None and hrrr_data_file is None and (center is None or radius is None or forecast_date is None):
raise Exception("Invalid center or radius for wind model")
self.forecast_date = forecast_date
self.gfs_data_file = gfs_data_file
self.gfs_height_map = None
self.gfs_latlons = None
self.hrrr_data_file = hrrr_data_file
self.hrrr_height_map = None
self.hrrr_latlons = None
if gfs_data_file is not None:
self.gfs_height_map, self.gfs_latlons = self._parse_grbs(pygrib.open(gfs_data_file))
if hrrr_data_file is not None:
self.hrrr_height_map, self.hrrr_latlons = self._parse_grbs(pygrib.open(hrrr_data_file))
if gfs_data_file is None and hrrr_data_file is None:
lat_radius = change_in_latitude_miles(radius)
lon_radius = max(change_in_longitude_miles(center[0] - lat_radius, radius), change_in_longitude_miles(center[0] + lat_radius, radius))
self.NW_bound = [center[0] + lat_radius, center[1] - lon_radius]
self.SE_bound = [center[0] - lat_radius, center[1] + lon_radius]
self._load_gfs(keep_files)
示例2: __init__
def __init__(self,source,target):
def lon_lat_to_cartesian(lon,lat,R=1):
"""
calculates lon, lat coordinates of a point on a sphere with
radius R
"""
lon_r=np.radians(lon)
lat_r=np.radians(lat)
x=R*np.cos(lat_r)*np.cos(lon_r)
y=R*np.cos(lat_r)*np.sin(lon_r)
z=R*np.sin(lat_r)
return x,y,z
"""
read data from files
"""
self.source=source
self.target=target
self.D1=pygrib.open(self.source)
self.latSOURCE,self.lonSOURCE=self.D1[8].latlons()
self.D2=pygrib.open(self.target)
self.latTARGET,self.lonTARGET=self.D2[1].latlons()
"""
flatten grid coordinates into cartesian x,y,z
"""
self.xs,self.ys,self.zs=lon_lat_to_cartesian(self.lonSOURCE.flatten(),self.latSOURCE.flatten())
self.xt,self.yt,self.zt=lon_lat_to_cartesian(self.lonTARGET.flatten(),self.latTARGET.flatten())
示例3: plot_algorithms
def plot_algorithms(file1,file2):
file1=pygrib.open(file1)
temp_var_file1=file1[8].values
file2=pygrib.open(file2)
temp_var_file2=file2[3].values
file1.close()
file2.close()
plt.figure(figsize=(10,5))
plt.subplot(121)
plt.pcolormesh(temp_var_file1)
plt.xlim([0, temp_var_file1.shape[0]])
plt.ylim([0, temp_var_file1.shape[1]])
plt.colorbar()
plt.title("from file1")
plt.subplot(122)
plt.pcolormesh(temp_var_file2)
plt.colorbar()
plt.xlim([0, temp_var_file2.shape[0]])
plt.ylim([0, temp_var_file2.shape[1]])
plt.title("from file2");
plt.show()
示例4: pcpn
def pcpn(grids, valid, iarchive):
"""Attempt to use MRMS or stage IV pcpn here"""
floor = datetime.datetime(2014, 11, 1)
floor = floor.replace(tzinfo=pytz.timezone("UTC"))
if valid < floor:
# Use stageIV
ts = (valid + datetime.timedelta(minutes=60)).replace(minute=0)
gribfn = ts.strftime(("/mesonet/ARCHIVE/data/%Y/%m/%d/stage4/ST4."
"%Y%m%d%H.01h.grib"))
if not os.path.isfile(gribfn):
return
grbs = pygrib.open(gribfn)
grib = grbs[1]
lats, lons = grib.latlons()
vals = grib.values
nn = NearestNDInterpolator((lons.flatten(), lats.flatten()),
vals.flatten())
grids['pcpn'] = nn(XI, YI)
return
fn = None
i = 0
while i < 10:
ts = valid - datetime.timedelta(minutes=i)
if ts.minute % 2 == 0:
testfn = mrms_util.fetch('PrecipRate', ts)
if testfn is not None:
fn = testfn
break
i += 1
if fn is None:
print("Warning, no PrecipRate data found!")
return
fp = gzip.GzipFile(fn, 'rb')
(_, tmpfn) = tempfile.mkstemp()
tmpfp = open(tmpfn, 'wb')
tmpfp.write(fp.read())
tmpfp.close()
grbs = pygrib.open(tmpfn)
values = grbs[1]['values']
# just set -3 (no coverage) to 0 for now
values = np.where(values < 0, 0, values)
map(os.unlink, [fn, tmpfn])
# 3500, 7000, starts in upper left
top = int((55. - reference.IA_NORTH) * 100.)
bottom = int((55. - reference.IA_SOUTH) * 100.)
right = int((reference.IA_EAST - -130.) * 100.) - 1
left = int((reference.IA_WEST - -130.) * 100.)
# two minute accumulation is in mm/hr / 60 * 5
# stage IV is mm/hr
grids['pcpn'] = np.flipud(values[top:bottom, left:right]) / 12.0
示例5: compute
def compute(valid):
''' Get me files '''
prob = None
for hr in range(-15,0):
ts = valid + datetime.timedelta(hours=hr)
fn = ts.strftime("hrrr.ref.%Y%m%d%H00.grib2")
if not os.path.isfile(fn):
continue
grbs = pygrib.open(fn)
gs = grbs.select(level=1000,forecastTime=(-1 * hr * 60))
ref = generic_filter(gs[0]['values'], np.max, size=10)
if prob is None:
lats, lons = gs[0].latlons()
prob = np.zeros( np.shape(ref) )
prob = np.where(ref > 29, prob+1, prob)
prob = np.ma.array(prob / 15. * 100.)
prob.mask = np.ma.where(prob < 1, True, False)
m = MapPlot(sector='iowa',
title='HRRR Composite Forecast 4 PM 20 May 2014 30+ dbZ Reflectivity',
subtitle='frequency of previous 15 model runs all valid at %s, ~15km smoothed' % (valid.astimezone(pytz.timezone("America/Chicago")).strftime("%-d %b %Y %I:%M %p %Z"),))
m.pcolormesh(lons, lats, prob, np.arange(0,101,10), units='%',
clip_on=False)
m.map.drawcounties()
m.postprocess(filename='test.ps')
m.close()
示例6: read_grib
def read_grib(filename, dataset, dt):
'''
read dataset in grib file filename
with date and hour specified by datetime object dt
'''
localFilename = copyToLocal(filename)
hour = dt.hour
date = int(dt.strftime('%Y%m%d'))
try:
g = pygrib.open(localFilename)
except IOError:
raise Exception('Could not open grib file %s' % (localFilename))
data = None
grblist = []
for grb in g:
grblist.append(grb)
if (grb.name == dataset) and (grb.hour == hour) and (grb.dataDate == date):
data = grb.values
g.close()
if data == None:
print 'List of datasets in grib file:'
for grb in grblist:
print grb
# for j in sorted(grb.keys()): print '->', j
# print '===', '"%s"' % (grb.name), grb.dataDate, type(grb.dataDate), grb.hour
raise Exception('Could not read "%s" at %s in file %s' % (dataset, dt, localFilename))
return data
示例7: gettigge
def gettigge(center,idate,var,lev,ftyp='cntl'):
YYYYMM=idate.strftime('%Y%m')
YYYYMMDDHH=idate.strftime('%Y%m%d%H')
varname = var+str(lev)
fname=FNAMEBASE.format(center=center.lower(),
ftyp=ftyp,
varname=varname,
YYYYMM=YYYYMM,
YYYYMMDDHH=YYYYMMDDHH)
print fname
#
shortname=var
if var=='z': shortname='gh'
grbs = pygrib.open(fname)
grb = grbs.select(shortName=shortname,level=lev)
xn = grb[0]['Ni']
yn = grb[0]['Nj']
tn = len(grb)
lat,lon = grb[0].latlons()
out = np.empty((tn,yn,xn))
tyme = np.empty(tn,dtype=np.object)
for t, gr in enumerate(grb):
out[t,...] = gr.values
tyme[t] = gr.validDate
grbs.close()
dims = ['tyme','lat','lon']
grid = McGrid(out, lon=lon[0,:], lat=lat[::-1,0], lev=lev, tyme=tyme, dims=dims)
out = McField(out,name=var,grid=grid)
return out
示例8: do
def do():
now = sts
while now < ets:
fn = now.strftime("/mesonet/ARCHIVE/data/%Y/%m/%d/model/hrrr/%H/hrrr.t%Hz.3kmf00.grib2")
if not os.path.isfile(fn):
print fn
now += interval
continue
grbs = pygrib.open(fn)
try:
gs = grbs.select(name='2 metre temperature')
except:
print fn
now += interval
continue
g = gs[0]['values']
if now == sts:
lats,lons = gs[0].latlons()
maxinterval = np.zeros(np.shape(g))
current = np.zeros(np.shape(g))
print np.max(g), np.min(g)
current = np.where(g < 273, current + 1, 0)
maxinterval = np.where(current > maxinterval, current, maxinterval)
now += interval
np.save('maxinterval.npy', np.array(maxinterval))
np.save('lats.npy', lats)
np.save('lons.npy', lons)
示例9: doday
def doday():
"""
Create a plot of precipitation stage4 estimates for some day
"""
sts = mx.DateTime.DateTime(2013,5,25,12)
ets = mx.DateTime.DateTime(2013,5,31,12)
interval = mx.DateTime.RelativeDateTime(days=1)
now = sts
total = None
while now < ets:
fp = "/mesonet/ARCHIVE/data/%s/stage4/ST4.%s.24h.grib" % (
now.strftime("%Y/%m/%d"),
now.strftime("%Y%m%d%H") )
if os.path.isfile(fp):
lts = now
grbs = pygrib.open(fp)
if total is None:
g = grbs[1]
total = g["values"]
lats, lons = g.latlons()
else:
total += grbs[1]["values"]
grbs.close()
now += interval
m = MapPlot(sector='iowa', title='NOAA Stage IV & Iowa ASOS Precipitation',
subtitle='25-30 May 2013')
m.pcolormesh(lons, lats, total / 25.4, numpy.arange(0,14.1,1), latlon=True,
units='inch')
m.drawcounties()
m.plot_values(dlons, dlats, dvals, '%.02f')
m.postprocess(filename='test.svg')
import iemplot
iemplot.makefeature('test')
示例10: readfile
def readfile(infile):
'''Read the GRIB file and return the regional (SCS) sst data'''
grbs=pygrib.open(infile)
grb=grbs.select(name='Temperature')[0]
sst, lats, lons = grb.data(lat1=14,lat2=30,lon1=106,lon2=126)
grbs.close()
return sst
示例11: ncepgfs
def ncepgfs(lonStart=3, lonEnd=4, latStart=41, latEnd=42, \
m=None, name='ncepgfs', contour=None):
"""
Plot latest NCEP GFS field from \
http://nomad1.ncep.noaa.gov/pub/gfs_master/
"""
base = '/media/SOLabNFS/SERVERS/media/hyrax/data/auxdata/model/ncep/gfs/'
outdir = '/home/mag/'
fn = 'gfs20101218/gfs.t18z.master.grbf00'
# fn = 'gfs20101218/gfs.t18z.master.grbf03'
refDate = datetime.datetime(1978,1,1,0,0,0)
wantedDate = datetime.datetime(2010,12,18,17,39,0)
# Get the file date
grbs = pygrib.open(base + fn)
grb = grbs.message(1)
fileDate = grb.analDate
filetime = fileDate.strftime("%Y%m%d_%H%M")
print "File time: ", filetime
print "File date: ", fileDate
lats, lons = grb.latlons()
u10 = grbs.message(9)['values']
v10 = grbs.message(9)['values']
# u = grbs.message(3)['values']
# v = grbs.message(4)['values']
w = sqrt(u10**2+v10**2)
示例12: run
def run(ts, routes):
""" Run for a given UTC timestamp """
fn = ts.strftime(("/mesonet/ARCHIVE/data/%Y/%m/%d/model/rtma/%H/"
"rtma.t%Hz.awp2p5f000.grib2"))
if not os.path.isfile(fn):
print 'wind_power.py missing', fn
return
grb = pygrib.open(fn)
try:
u = grb.select(name='10 metre U wind component')[0]
v = grb.select(name='10 metre V wind component')[0]
except:
print('Missing u/v wind for wind_power.py\nFN: %s' % (fn,))
return
mag = (u['values']**2 + v['values']**2)**.5
mag = (mag * 1.35)**3 * 0.002641
# 0.002641
lats, lons = u.latlons()
lts = ts.astimezone(pytz.timezone("America/Chicago"))
pqstr = ("plot %s %s00 midwest/rtma_wind_power.png "
"midwest/rtma_wind_power_%s00.png png"
) % (routes, ts.strftime("%Y%m%d%H"), ts.strftime("%H"))
m = MapPlot(sector='midwest',
title=(r'Wind Power Potential :: '
'(speed_mps_10m * 1.35)$^3$ * 0.002641'),
subtitle=('valid: %s based on NOAA Realtime '
'Mesoscale Analysis'
) % (lts.strftime("%d %b %Y %I %p")))
m.pcolormesh(lons, lats, mag, numpy.array(levels), units='MW')
m.postprocess(pqstr=pqstr)
示例13: create_sub
def create_sub(full_grib, data_dir='.'):
''' Extracts a subset of GRIB records from full_grib and
saves them in a file named the same as full_grib, but in
the data_dir folder.
'''
#names=['Temperature', 'Soil temperature', 'Total precipitation', 'Wind speed (gust)', 'Water equivalent of accumulated snow depth']
name, ext = os.path.splitext(full_grib)
out_file = '{2}/{0}_sub{1}'.format(name, ext, data_dir)
grbout = open(out_file, 'wb')
grbs = pygrib.open(full_grib)
surface_names=['t', 'acpcp', 'gust', 'sdwe']
for name in surface_names:
print 'finding {0}'.format(name)
grbs.seek(0)
for grb in grbs.select(shortName=name, typeOfLevel='surface'):
grbout.write(grb.tostring())
agl_names=['10u', '10v']
grbs.seek(0)
for name in agl_names:
print 'finding {0}'.format(name)
for grb in grbs.select(shortName=name, typeOfLevel='heightAboveGround'):
grbout.write(grb.tostring())
grbout.close()
return out_file
示例14: containsMessages
def containsMessages(filename):
grbs = pg.open(filename)
count = grbs.messages
#print "messages: %s" % (count)
if count:
return grbs
return None
示例15: run
def run(ts, routes):
""" Run for a given UTC timestamp """
fn = ts.strftime("/mesonet/ARCHIVE/data/%Y/%m/%d/model/rtma/%H/rtma.t%Hz.awp2p5f000.grib2")
if not os.path.isfile(fn):
print "wind_power.py missing", fn
return
grb = pygrib.open(fn)
u = grb.select(name="10 metre U wind component")[0]
v = grb.select(name="10 metre V wind component")[0]
mag = (u["values"] ** 2 + v["values"] ** 2) ** 0.5
mag = (mag * 1.35) ** 3 * 0.002641
# 0.002641
lats, lons = u.latlons()
lts = ts.astimezone(pytz.timezone("America/Chicago"))
pqstr = "plot %s %s00 midwest/rtma_wind_power.png midwest/rtma_wind_power_%s00.png png" % (
routes,
ts.strftime("%Y%m%d%H"),
ts.strftime("%H"),
)
m = MapPlot(
sector="midwest",
title=r"Wind Power Potential :: (speed_mps_10m * 1.35)$^3$ * 0.002641",
subtitle="valid: %s based on NOAA Realtime Mesoscale Analysis" % (lts.strftime("%d %b %Y %I %p")),
)
m.pcolormesh(lons, lats, mag, numpy.array(levels), units="MW")
m.postprocess(pqstr=pqstr)