0

所以我正在做一个研究项目,我在一个目录中有大约 6500 个 HRRR .grib2 文件,我需要做的只是打开每个 grib2 文件并将消息打印到 .txt 中,以便以后解析值。别担心,我已经将通常较大的 .grib2 文件从大约 150-300mb 减少到平均 4.5mb,所以我不太担心内存错误。

这是我目前正在使用的代码:

import pygrib, sys
import numpy as np
import os

station_lat=41.31190
station_lon=-95.90185
print("INPUT LAT LON: ",station_lat,station_lon)

path = "/Volumes/harddrive/HRRRFILES"
gribfiles = os.listdir("/Volumes/harddrive/HRRRFILES")

for file in gribfiles:
    if file.endswith(".grib2"): #iterate files with .grib2 appendage
        grbs = pygrib.open(gribfiles) 
        for grb in grbs:
            lats, lons = grb.latlons()
            print("\n",grb.messagenumber,grb) # For Debugging Purposes

            a = abs(lats-station_lat)+abs(lons-station_lon)

            lat_counter,lon_counter = np.unravel_index(a.argmin(),a.shape)
            data=grb.values[lat_counter,lon_counter]

            print(lat_counter,lon_counter,lats[lat_counter,lon_counter],lons[lat_counter,lon_counter],data)

虽然当我运行它时,我收到以下错误:

INPUT LAT LON:  41.3119 -95.90185
Traceback (most recent call last):
  File "example1.py", line 15, in <module>
    grbs = pygrib.open(gribfiles) 
  File "pygrib.pyx", line 393, in pygrib.open.__cinit__
TypeError: expected bytes, list found

一些帮助将不胜感激。

4

0 回答 0