2016-09-29 1 views
0

일부 grib 파일에 액세스해야합니다. 나는 파이그 리브 (pygrib)를 사용하여 어떻게하는지 알아 냈다. 그러나 내가 어떻게하는지 알아 낸 유일한 방법은 근면하게 느리다.파이 그 리브로 한 번에 많은 그리브 메시지에 액세스하십시오.

나는 34 시간의 3hrly 데이터를 보유하고 있으며, 연간 36 개 파일 (10 일마다 하나씩)로 구성됩니다. 총 약 1000 개의 파일

각 파일에는 ~ 80 개의 "메시지"(10 일 동안 하루에 8 개의 값)가 있습니다. (공간 데이터이므로 (x, y) 차원을 갖습니다).

grbfile = pygrib.index(filename, 'shortName', 'typeOfLevel', 'level') 
var1 = grbfile.select(typeOfLevel='pressureFromGroundLayer', level=180, shortName='unknown') 
for it in np.arange(len(var1)): 
    var_values, lat1, lon1 = var1[it].data() 
    if (it==0): 
     tot_var = np.expand_dims(var_values,axis=0) 
    else: 
     tot_var = np.append(tot_var, np.expand_dims(var_values,axis=0),axis=0) 

및 1000 개 파일 각각에 대해이 작업을 반복 :

내가 쓰는 내 모든 데이터를 읽을 수 있습니다.

더 빠른 방법이 있습니까? 한 번에 모든 grib 파일 당 ~ 80 개의 레이어를로드하는 것과 비슷합니까?

var_values, lat1, lon1 = var1[:].data() 

답변

1

정확하게 이해하면 각 파일의 모든 80 개 메시지의 데이터가 하나의 배열에 쌓여 있어야합니다.

내가 그 배열이 매우 큰 얻을 것이다, 당신을 경고해야하고, NumPy와는 던져 발생할 수 MemoryError (전에 나 한테 일어난) 그리드의 크기에 따라 등

말했다되고 그건

을 수행 할 수 있습니다 다음과 같이 입력하십시오 :

# substitute with a list of your file names 
# glob is a builtin library that can help accomplish this 
files = list_of_files 

grib = pygrib.open(files[0]) # start with the first one 

# grib message numbering starts at 1 
data, lats, lons = grib.message(1).data() 

# while np.expand_dims works, the following is shorter 
# syntax wise and will accomplish the same thing 
data = data[None,...] # add an empty dimension as axis 0 

for m in xrange(2, grib.messages + 1): 
    data = np.vstack((data, grib.message(m).values[None,...])) 

grib.close() # good practice 

# now data has all the values from each message in the first file stacked up 
# time to stack the rest on there 
for file_ in files[1:]: # all except the first file which we've done 
    grib = pygrib.open(file_) 
    for msg in grib: 
     data = np.vstack((data, msg.values[None,...])) 

    grib.close() 
print data.shape # should be (80 * len(files), nlats, nlons) 

이것은 약간의 속도를 얻을 수 있습니다. pygrib.open 개체는 생성자처럼 작동하므로 pygrib.indexselect() 메서드와 같은 개체 목록을 작성하는 대신 각 pygrib.gribmessage 개체를 전달합니다. 특정 파일에 모든 메시지가 필요한 경우 여기에 액세스하는 방법입니다.

희망 하시겠습니까?

관련 문제