Skip to content

Commit

Permalink
Update for open class
Browse files Browse the repository at this point in the history
Changed the levels and variables attributes to properties.  These
were causing some slowdown of file opening and indexing. Now
levels and variables will be generated when referenced.
  • Loading branch information
EricEngle-NOAA committed Mar 24, 2024
1 parent 24e7df0 commit 626e28e
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions src/grib2io/_grib2io.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,27 @@ class `grib2io.open`, the file named `filename` is opened for reading (`mode
Attributes
----------
closed : bool
`True` is file handle is close; `False` otherwise.
current_message : int
Current position of the file in units of GRIB2 Messages.
levels : tuple
Tuple containing a unique list of wgrib2-formatted level/layer strings.
messages : int
Count of GRIB2 Messages contained in the file.
mode : str
File IO mode of opening the file.
name : str
Full path name of the GRIB2 file.
messages : int
Count of GRIB2 Messages contained in the file.
current_message : int
Current position of the file in units of GRIB2 Messages.
size : int
Size of the file in units of bytes.
closed : bool
`True` is file handle is close; `False` otherwise.
variables : tuple
Tuple containing a unique list of variable short names (i.e. GRIB2
abbreviation names).
levels : tuple
Tuple containing a unique list of wgrib2-formatted level/layer strings.
"""
__slots__ = ('_fileid', '_filehandle', '_hasindex', '_index', '_pos',
'closed', 'current_message', 'levels', 'messages', 'mode',
'name', 'size', 'variables')
__slots__ = ('_fileid', '_filehandle', '_hasindex', '_index', '_nodata',
'_pos', 'closed', 'current_message', 'messages', 'mode',
'name', 'size')
def __init__(self, filename: str, mode: str="r", **kwargs):
"""
Initialize GRIB2 File object instance.
Expand All @@ -109,6 +109,9 @@ def __init__(self, filename: str, mode: str="r", **kwargs):
# Manage keywords
if "_xarray_backend" not in kwargs:
kwargs["_xarray_backend"] = False
self._nodata = False
else:
self._nodata = kwargs["_xarray_backend"]
if mode in {'a','r','w'}:
mode = mode+'b'
if 'w' in mode: mode += '+'
Expand Down Expand Up @@ -139,15 +142,10 @@ def __init__(self, filename: str, mode: str="r", **kwargs):
self.current_message = 0
self.size = fstat.st_size
self.closed = self._filehandle.closed
self.levels = None
self.variables = None
self._fileid = hashlib.sha1((self.name+str(fstat.st_ino)+
str(self.size)).encode('ASCII')).hexdigest()
if 'r' in self.mode:
try:
self._build_index(no_data=kwargs['_xarray_backend'])
except(KeyError):
self._build_index()
self._build_index()
# FIX: Cannot perform reads on mode='a'
#if 'a' in self.mode and self.size > 0: self._build_index()

Expand Down Expand Up @@ -195,7 +193,7 @@ def __getitem__(self, key):
raise KeyError('Key must be an integer, slice, or GRIB2 variable shortName.')


def _build_index(self, no_data=False):
def _build_index(self):
"""Perform indexing of GRIB2 Messages."""
# Initialize index dictionary
if not self._hasindex:
Expand Down Expand Up @@ -321,7 +319,7 @@ def _build_index(self, no_data=False):
msg._msgnum = self.messages-1
msg._deflist = deflist
msg._coordlist = coordlist
if not no_data:
if not self._nodata:
msg._data = Grib2MessageOnDiskArray((msg.ny,msg.nx), 2,
TYPE_OF_VALUES_DTYPE[msg.typeOfValues],
self._filehandle,
Expand Down Expand Up @@ -356,10 +354,21 @@ def _build_index(self, no_data=False):
self._filehandle.seek(0)
break

# Index at end of _build_index()
if self._hasindex and not no_data:
self.variables = tuple(sorted(set([msg.shortName for msg in self._index['msg']])))
self.levels = tuple(sorted(set([msg.level for msg in self._index['msg']])))

@property
def levels(self):
if self._hasindex and not self._nodata:
return tuple(sorted(set([msg.level for msg in self._index['msg']])))
else:
return None


@property
def variables(self):
if self._hasindex and not self._nodata:
return tuple(sorted(set([msg.shortName for msg in self._index['msg']])))
else:
return None


def close(self):
Expand Down

0 comments on commit 626e28e

Please sign in to comment.