-
Notifications
You must be signed in to change notification settings - Fork 0
/
grib.py
510 lines (487 loc) · 24 KB
/
grib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
import pygrib
import numpy as np
#import cPickle as pickle
import pickle
from operator import itemgetter
from os.path import splitext
from datetime import datetime
from datetime import timedelta
import aa
class File(aa.File) :
def __init__(self, filePath) :
super(File, self).__init__()
fileName = splitext(filePath)[0]
rawFile = pygrib.open(filePath)
# read the first line of the file
gribLine = rawFile.readline()
firstInstant = datetime(gribLine.year, gribLine.month, gribLine.day,
gribLine.hour, gribLine.minute, gribLine.second)
################
# SURVEY SHAPE #
################
# sometimes there are several types of level
# 2D data is followed by 3D data e.g. jra25
variablesLevels = {} # variable - level type - level
variablesEnsembleSize = {}
variablesMetaData = {}
# loop through the variables and levels of the first time step
# default : grib has a time axis
timeDimension = True
while datetime(gribLine.year, gribLine.month, gribLine.day,
gribLine.hour, gribLine.minute, gribLine.second)\
== firstInstant :
# is it the first time this variable is met ?
if gribLine.shortName not in variablesLevels :
# create a dictionary for that variable
# that will contain different level types
variablesLevels[gribLine.shortName] = {}
variablesMetaData[gribLine.shortName] = {}
variablesMetaData[gribLine.shortName]['shortName'] = gribLine.shortName
variablesMetaData[gribLine.shortName]['units'] = gribLine.units
variablesMetaData[gribLine.shortName]['name'] = gribLine.name
# is this the first time this type of level is met ?
if gribLine.typeOfLevel not in \
variablesLevels[gribLine.shortName] :
# create a list that will contain the level labels
variablesLevels[gribLine.shortName][gribLine.typeOfLevel] = [gribLine.level]
# set the ensemble member counter to 1
variablesEnsembleSize[gribLine.shortName] = 1
# level type already exists :
else :
# is this the second time this level label is met ?
if variablesLevels[gribLine.shortName][gribLine.typeOfLevel][-1] == gribLine.level :
# increment one ensemble member
variablesEnsembleSize[gribLine.shortName] += 1
# assume that it is square : levType*lev*mbr (easy to fix if needed)
else :
# append the level label to the variable / level type
variablesLevels[gribLine.shortName][gribLine.typeOfLevel]\
.append(gribLine.level)
# set the ensemble member counter to 1
variablesEnsembleSize[gribLine.shortName] = 1
# move to the next line
gribLine = rawFile.readline()
if gribLine == None :
timeDimension = False
break
if timeDimension :
#############
# TIME AXIS #
#############
# "seek/tell" index starts with 1
# but we've moved on the next instant at the end of the while loop
# hence the minus one
linesPerInstant = rawFile.tell() - 1
# determine the interval between two samples
secondInstant = datetime(gribLine.year, gribLine.month, gribLine.day,
gribLine.hour, gribLine.minute, gribLine.second)
timeStep = secondInstant - firstInstant
# go to the end of the file
rawFile.seek(0, 2)
lastIndex = rawFile.tell()
# this index points at the last message
# e.g. f.message(lastIndex) returns the last message
# indices start at 1 meaning that lastIndex is also the
# number of messages in the file
# for consistency checks
gribLine = rawFile.message(lastIndex)
lastInstant = datetime(gribLine.year, gribLine.month, gribLine.day,
gribLine.hour, gribLine.minute, gribLine.second)
if timeStep.days >= 28 :
#if gribLine.stepType == 'avgfc' and firstInstant.month == 12 :
if firstInstant.month == 12 :
#forecasted = firstInstant + aa.timedelta(hours = int(gribLine.stepRange))
self.axes['time'] = aa.TimeAxis(
np.array([aa.datetime(firstInstant.year + 1 + (1 + timeIndex-1)//12,
(1 + timeIndex-1)%12+1, 1)
- aa.datetime(firstInstant.year + 1, 1, 1)
+ aa.datetime(firstInstant.year, firstInstant.month, firstInstant.day, firstInstant.hour)
for timeIndex in range(int(lastIndex/linesPerInstant))]), None)
else :
self.axes['time'] = aa.TimeAxis(
np.array([aa.datetime(firstInstant.year + (firstInstant.month + timeIndex-1)//12,
(firstInstant.month + timeIndex-1)%12+1, 1)
for timeIndex in range(int(lastIndex/linesPerInstant))]), None)
"""
# attempt at reading "regular monthly means" i.e. average of all XX UTC values
try :
forecasted = firstInstant + aa.timedelta(hours = int(gribLine.stepRange))
self.axes['time'] = aa.TimeAxis(
np.array([aa.datetime(forecasted.year + (forecasted.month + timeIndex-1)//12,
(forecasted.month + timeIndex-1)%12+1, 1) - aa.timedelta(hours = int(gribLine.stepRange))
for timeIndex in range(int(lastIndex/linesPerInstant))]), None)
except (RuntimeError, ValueError) as error :
self.axes['time'] = aa.TimeAxis(
np.array([aa.datetime(firstInstant.year + (firstInstant.month + timeIndex-1)//12,
(firstInstant.month + timeIndex-1)%12+1, 1, firstInstant.hour)
for timeIndex in range(int(lastIndex/linesPerInstant))]), None)
"""
else :
self.axes['time'] = aa.TimeAxis(
np.array([firstInstant + timeIndex*timeStep
for timeIndex in range(int(lastIndex/linesPerInstant))]), None)
if lastInstant != self.dts[-1] or \
lastIndex % linesPerInstant != 0 :
print("Error in time axis")
raise Exception
############
# VERTICAL #
############
# find the longest vertical axis
maxLevelNumber = 0
for variableName, levelKinds in variablesLevels.items() :
for levelType, levels in levelKinds.items() :
# does levels look like a proper axis ?
if len(levels) > 1 :
variablesLevels[variableName][levelType] \
= aa.Vertical(np.array(levels), levelType)
# is levels longer than the previous longest axis ?
if len(levels) > maxLevelNumber :
maxLevelNumber = len(levels)
mainLevels = aa.Vertical(np.array(levels), levelType)
# find the longest ensemble
self.axes['level'] = mainLevels
# the longest vertical axis gets to be the file's vertical axis
############
# ENSEMBLE #
############
maxEnsembleSize = 1
for variableName, ensembleSize in variablesEnsembleSize.items() :
if ensembleSize > maxEnsembleSize :
maxEnsembleSize = ensembleSize
if maxEnsembleSize > 1 :
self.axes['member'] = aa.Axis(np.arange(maxEnsembleSize))
###################
# HORIZONTAL AXES #
###################
rawFile.rewind()
# maybe it is necessary to rewind after this, methinks not
gribLine = rawFile.readline()
# assumes the first grib messages has spatial dimensions
lats, lons = gribLine.latlons()
# why the first and not the last ? in case there is no time dimension and gribLine is None
if lats.shape[1] > 1 :
if lats[0, 0] == lats[0, 1] :
self.axes['latitude'] = aa.Meridian(lats[:, 0], 'degrees')
self.axes['longitude'] = aa.Parallel(lons[0, :], 'degrees')
else :
self.axes['latitude'] = aa.Meridian(lats[0, :], 'degrees')
self.axes['longitude'] = aa.Parallel(lons[:, 0], 'degrees')
else :
if lats[0, 0] == lats[1, 0] :
self.axes['latitude'] = aa.Meridian([lats[0, 0]], 'degrees')
self.axes['longitude'] = aa.Parallel(lons[:, 0], 'degrees')
else :
self.axes['latitude'] = aa.Meridian(lats[:, 0], 'degrees')
self.axes['longitude'] = aa.Parallel([lons[0, 0]], 'degrees')
#############
# VARIABLES #
#############
self.variables = {}
for variableName, levelKinds in variablesLevels.items() :
for levelType, verticalAxis in levelKinds.items() :
conditions = {'shortName' : variableName,
'typeOfLevel' : levelType}
axes = aa.Axes()
if timeDimension :
axes['time'] = self.axes['time']
else :
conditions['time'] = firstInstant
# do we need to add a suffix to the variable's name ?
if len(levelKinds) > 1 :
variableLabel = variableName + '_' + levelType
else :
variableLabel = variableName
# does this variable have a vertical extension ?
# it may not be the file's vertical axis
if len(verticalAxis) > 1 :
axes['level'] = verticalAxis
# in case of homonyms, only the variable with the main
# vertical axis gets to keep the original shortname
if verticalAxis.units == mainLevels.units :
variableLabel = variableName
else :
# flat level i.e. 2D data
# the condition is a list to be iterable
conditions['level'] = verticalAxis
# is this variable an ensemble ?
if variablesEnsembleSize[variableName] > 1 :
axes['member'] = aa.Axis(np.arange(variablesEnsembleSize[variableName]))
else :
conditions['member'] = [0]
axes['latitude'] = self.axes['latitude']
axes['longitude'] = self.axes['longitude']
self.variables[variableLabel] = \
Variable(axes, variablesMetaData[variableName],
conditions, fileName)
##################
# PICKLE & INDEX #
##################
rawFile.close()
pickleFile = open(fileName+'.p', 'wb')
#import pdb ; pdb.set_trace()
pickle.dump(self, pickleFile)
pickleFile.close()
gribIndex = pygrib.index(filePath,
'shortName', 'level', 'typeOfLevel',
'year', 'month', 'day', 'hour')
gribIndex.write(fileName+'.idx')
gribIndex.close()
class Variable(aa.Variable) :
def __init__(self, axes, metadata, conditions,
fileName, full_axes = None) :
super(Variable, self).__init__()
self.axes = axes
if full_axes == None :
self.full_axes = axes.copy()
else :
self.full_axes = full_axes
self.metadata = metadata
self.conditions = conditions
self.fileName = fileName
@property
def shape(self) :
if "_data" not in self.__dict__ :
dimensions = []
for axis in list(self.axes.values()) :
dimensions.append(len(axis))
return tuple(dimensions)
else :
return super(Variable, self).shape
def __getitem__(self, item) :
# if the variable is still in pure grib mode
if "_data" not in self.__dict__ :
conditions = {}
# make item iterable, even when it's a singleton
if not isinstance(item, tuple) :
if not isinstance(item, list) :
if isinstance(item, np.ndarray) :
# don't bother if request is an array
self._get_data()
return super(Variable, self).__getitem__(item)
else :
item = (item,)
# loop through axes in their correct order
# and match axis with a sub-item
for axisIndex, axisName in enumerate(self.axes) :
# there may be more axes than sub-items
# do not overshoot
if axisIndex < len(item) :
# if it's a single index slice
if not isinstance(item[axisIndex], slice) :
conditions[axisName] = \
self.axes[axisName].data[item[axisIndex]]
else :
# it's a slice
# if it's a ':' slice, do nothing
if item[axisIndex] != slice(None) :
conditions[axisName] = \
(self.axes[axisName][item[axisIndex]].min(),
self.axes[axisName][item[axisIndex]].max())
return self(**conditions)
# if _data already exists (as a numpy array), follow standard protocol
else :
return super(Variable, self).__getitem__(item)
def __init__(self, axes, metadata, conditions,
fileName, full_axes = None) :
super(Variable, self).__init__()
self.axes = axes
if full_axes == None :
self.full_axes = axes.copy()
else :
self.full_axes = full_axes
self.metadata = metadata
self.conditions = conditions
self.fileName = fileName
@property
def shape(self) :
if "_data" not in self.__dict__ :
dimensions = []
for axis in list(self.axes.values()) :
dimensions.append(len(axis))
return tuple(dimensions)
else :
return super(Variable, self).shape
def __getitem__(self, item) :
# if the variable is still in pure grib mode
if "_data" not in self.__dict__ :
conditions = {}
# make item iterable, even when it's a singleton
if not isinstance(item, tuple) :
if not isinstance(item, list) :
item = (item,)
# loop through axes in their correct order
# and match axis with a sub-item
for axisIndex, axisName in enumerate(self.axes) :
# there may be more axes than sub-items
# do not overshoot
if axisIndex < len(item) :
# if it's a single index slice
if not isinstance(item[axisIndex], slice) :
conditions[axisName] = \
self.axes[axisName].data[item[axisIndex]]
else :
# it's a slice
# if it's a ':' slice, do nothing
if item[axisIndex] != slice(None) :
conditions[axisName] = \
(self.axes[axisName][item[axisIndex]].min(),
self.axes[axisName][item[axisIndex]].max())
return self(**conditions)
# if _data already exists (as a numpy array), follow standard protocol
else :
return super(Variable, self).__getitem__(item)
def extract_data(self, **kwargs) :
"Extract a subset via its axes"
# if the variable is still in pure grib mode
if "_data" not in self.__dict__ :
# conditions and axes of the output variable
newConditions = self.conditions.copy()
newMetadata = self.metadata.copy()
newAxes = self.axes.copy()
for axisName, condition in kwargs.items() :
# lat/lon get a special treatment within grib messages (array)
if axisName in ['latitude', 'longitude'] :
# there may already be restrictions on lat/lon from former calls
# refer to the complete axes to define the new slice
item, newAxis = self.full_axes[axisName](condition)
newConditions[axisName] = item
# time and level slices need to be made explicit
else :
# given the condition, call axis for a new version
item, newAxis = self.axes[axisName](condition)
# to what datetimes and pressures
# do the conditions correspond ? slice former axis
newConditions[axisName] = \
self.axes[axisName].data[item]
# make sure newConditions is still iterable though
if not isinstance(newConditions[axisName], list) :
if not isinstance(newConditions[axisName], np.ndarray) :
newConditions[axisName] = \
[newConditions[axisName]]
# if item is scalar, there will be no need for an axis
if newAxis == None :
del newAxes[axisName]
newMetadata[axisName] = condition
# otherwise, load newAxis in the new variable's axes
else :
newAxes[axisName] = newAxis
return Variable(newAxes, newMetadata,
newConditions, self.fileName, self.full_axes.copy())
# if _data already exists (as a numpy array), follow standard protocol
else :
return super(Variable, self).extract_data(**kwargs)
def _get_data(self) :
if '_data' not in self.__dict__ :
# dummy conditions to play with
newConditions = self.conditions.copy()
# scalar conditions only (input for the gribIndex)
subConditions = self.conditions.copy()
#########################
# TIME & LEVEL & MEMBER #
#########################
if 'time' not in self.conditions :
newConditions['time'] = self.axes['time'].data
else :
# gribIndex won't want lists of datetimes
# but rather individual year/month/day/hour
del subConditions['time']
# make sure time condition is iterable
if not isinstance(newConditions['time'], list) :
if not isinstance(newConditions['time'], np.ndarray) :
newConditions['time'] = [newConditions['time']]
# if data is 2D, it will have already have a level condition
# idem if it's 3D and has already been sliced
# if not, that means the user wants all available levels
if 'level' not in self.conditions :
newConditions['level'] = self.axes['level'].data
# same reasoning with ensemble members
if 'member' not in self.conditions :
newConditions['member'] = self.axes['member'].data
else :
# gribIndex won't want lists of ensemble members
del subConditions['member']
########################
# LATITUDE & LONGITUDE #
########################
### MASK ###
# mask is used to slice the netcdf array contained in gribMessages
mask = []
if 'latitude' in self.conditions :
del subConditions['latitude']
mask.append(self.conditions['latitude'])
else :
mask.append(slice(None))
twistedLongitudes = False
if 'longitude' in self.conditions :
del subConditions['longitude']
# twisted longitudes...
if type(self.conditions['longitude']) == tuple :
twistedLongitudes = True
secondMask = mask[:]
mask.append(self.conditions['longitude'][0])
#slice1 = slice(0, -mask[-1].start)
slice1 = slice(0, mask[-1].stop - mask[-1].start)
secondMask.append(self.conditions['longitude'][1])
slice2 = slice(-secondMask[-1].stop, None)
else :
mask.append(self.conditions['longitude'])
else :
mask.append(slice(None))
mask = tuple(mask)
### HORIZONTAL SHAPE ###
# shape of the output array : (time, level, horizontalShape)
horizontalShape = []
#if hasattr(self, 'lats') :
if 'latitude' in self.axes :
horizontalShape.append(len(self.lats))
#if hasattr(self, 'lons') :
if 'longitude' in self.axes :
horizontalShape.append(len(self.lons))
horizontalShape = tuple(horizontalShape)
#####################
# GET GRIB MESSAGES #
#####################
shape = ()
for axisName, axis in self.axes.items() :
shape = shape + (len(axis),)
# build the output numpy array
self._data = np.empty(shape, dtype=float)
# flatten time and level and ensemble dimensions
# that's in case there's neither of either
self._data.shape = (-1,) + horizontalShape
# load the grib index
gribIndex = pygrib.index(self.fileName+'.idx')
lineIndex = 0
for instant in newConditions['time'] :
subConditions['year'] = instant.year
subConditions['month'] = instant.month
subConditions['day'] = instant.day
subConditions['hour'] = instant.hour
for level in newConditions['level'] :
subConditions['level'] = \
np.asscalar(np.array(level))
# converts numpy types to standard types
# standard types are converted to numpy
# normally, there should be as many lines
# that answer our query as there are ensemble members
gribLines = gribIndex(**subConditions)
# catching a bug involving cfsr gribs confusing u & v winds
if gribLines[0].shortName != subConditions['shortName'] :
gribLines = gribIndex(**subConditions)
assert gribLines[0].shortName == subConditions['shortName']
for member in newConditions['member'] :
if twistedLongitudes :
self._data[tuple([lineIndex, Ellipsis, slice1])] = \
gribLines[member].values[mask]
self._data[tuple([lineIndex, Ellipsis, slice2])] = \
gribLines[member].values[tuple(secondMask)]
else :
self._data[lineIndex] = gribLines[member].values[mask]
lineIndex += 1
gribIndex.close()
self._data.shape = shape
return self._data
def _set_data(self, newValue) :
self._data = newValue
data = property(_get_data, _set_data)