Skip to content

Commit

Permalink
Rename eccodes.py to bindings.py to ease comparison with new eccodes-…
Browse files Browse the repository at this point in the history
…python pacakge.
  • Loading branch information
alexamici committed Jan 31, 2019
1 parent 9cb3c38 commit 66c24bd
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 309 deletions.
4 changes: 2 additions & 2 deletions cfgrib/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def cfgrib_cli():

@cfgrib_cli.command('selfcheck')
def selfcheck():
from . import eccodes
from . import bindings

print("Found: ecCodes v%s." % eccodes.codes_get_api_version())
print("Found: ecCodes v%s." % bindings.codes_get_api_version())
print("Your system is ready.")


Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions cfgrib/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import numpy as np

from . import __version__
from . import bindings
from . import cfmessage
from . import eccodes
from . import messages

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -237,7 +237,7 @@ def build_array(self):
for header_indexes, offset in self.offsets.items():
# NOTE: fill a single field as found in the message
message = self.stream.message_from_file(file, offset=offset[0])
values = message.message_get('values', eccodes.CODES_TYPE_DOUBLE)
values = message.message_get('values', bindings.CODES_TYPE_DOUBLE)
array.__getitem__(header_indexes).flat[:] = values
array[array == self.missing_value] = np.nan
return array
Expand All @@ -259,7 +259,7 @@ def __getitem__(self, item):
continue
# NOTE: fill a single field as found in the message
message = self.stream.message_from_file(file, offset=offset[0])
values = message.message_get('values', eccodes.CODES_TYPE_DOUBLE)
values = message.message_get('values', bindings.CODES_TYPE_DOUBLE)
array_field.__getitem__(tuple(array_field_indexes)).flat[:] = values

array = array_field[(Ellipsis,) + item[-self.geo_ndim:]]
Expand Down Expand Up @@ -466,7 +466,7 @@ def build_dataset_components(
attributes_namespace = {
'cfgrib_version': __version__,
'cfgrib_open_kwargs': json.dumps(encoding),
'eccodes_version': eccodes.codes_get_api_version(),
'eccodes_version': bindings.codes_get_api_version(),
'timestamp': timestamp or datetime.datetime.now().isoformat().partition('.')[0]
}
history_in = '{timestamp} GRIB to CDM+CF via ' \
Expand Down
52 changes: 26 additions & 26 deletions cfgrib/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

import attr

from . import eccodes
from . import bindings


LOG = logging.getLogger(__name__)
Expand All @@ -52,34 +52,34 @@ class Message(collections.MutableMapping):
errors = attr.attrib(default='ignore', validator=attr.validators.in_(['ignore', 'strict']))

@classmethod
def from_file(cls, file, offset=None, product_kind=eccodes.CODES_PRODUCT_GRIB, **kwargs):
def from_file(cls, file, offset=None, product_kind=bindings.CODES_PRODUCT_GRIB, **kwargs):
# type: (T.IO[bytes], int, int, T.Any) -> Message
if offset is not None:
file.seek(offset)
codes_id = eccodes.codes_handle_new_from_file(file, product_kind)
codes_id = bindings.codes_handle_new_from_file(file, product_kind)
return cls(codes_id=codes_id, **kwargs)

@classmethod
def from_sample_name(cls, sample_name, product_kind=eccodes.CODES_PRODUCT_GRIB, **kwargs):
codes_id = eccodes.codes_new_from_samples(sample_name.encode('ASCII'), product_kind)
def from_sample_name(cls, sample_name, product_kind=bindings.CODES_PRODUCT_GRIB, **kwargs):
codes_id = bindings.codes_new_from_samples(sample_name.encode('ASCII'), product_kind)
return cls(codes_id=codes_id, **kwargs)

@classmethod
def from_message(cls, message, **kwargs):
codes_id = eccodes.codes_handle_clone(message.codes_id)
codes_id = bindings.codes_handle_clone(message.codes_id)
return cls(codes_id=codes_id, **kwargs)

def __del__(self):
eccodes.codes_handle_delete(self.codes_id)
bindings.codes_handle_delete(self.codes_id)

def message_get(self, item, key_type=None, size=None, length=None, default=_MARKER):
# type: (str, int, int, int, T.Any) -> T.Any
"""Get value of a given key as its native or specified type."""
key = item.encode(self.encoding)
try:
values = eccodes.codes_get_array(self.codes_id, key, key_type, size, length)
except eccodes.EcCodesError as ex:
if ex.code == eccodes.lib.GRIB_NOT_FOUND:
values = bindings.codes_get_array(self.codes_id, key, key_type, size, length)
except bindings.EcCodesError as ex:
if ex.code == bindings.lib.GRIB_NOT_FOUND:
if default is _MARKER:
raise KeyError(item)
else:
Expand All @@ -97,22 +97,22 @@ def message_set(self, item, value):
key = item.encode(self.encoding)
set_array = isinstance(value, T.Sequence) and not isinstance(value, (str, bytes))
if set_array:
eccodes.codes_set_array(self.codes_id, key, value)
bindings.codes_set_array(self.codes_id, key, value)
else:
if isinstance(value, str):
value = value.encode(self.encoding)
eccodes.codes_set(self.codes_id, key, value)
bindings.codes_set(self.codes_id, key, value)

def message_iterkeys(self, namespace=None):
# type: (str) -> T.Generator[str, None, None]
if namespace is not None:
bnamespace = namespace.encode(self.encoding) # type: T.Optional[bytes]
else:
bnamespace = None
iterator = eccodes.codes_keys_iterator_new(self.codes_id, namespace=bnamespace)
while eccodes.codes_keys_iterator_next(iterator):
yield eccodes.codes_keys_iterator_get_name(iterator).decode(self.encoding)
eccodes.codes_keys_iterator_delete(iterator)
iterator = bindings.codes_keys_iterator_new(self.codes_id, namespace=bnamespace)
while bindings.codes_keys_iterator_next(iterator):
yield bindings.codes_keys_iterator_get_name(iterator).decode(self.encoding)
bindings.codes_keys_iterator_delete(iterator)

def __getitem__(self, item):
# type: (str) -> T.Any
Expand All @@ -122,8 +122,8 @@ def __setitem__(self, item, value):
# type: (str, T.Any) -> None
try:
return self.message_set(item, value)
except eccodes.EcCodesError as ex:
if self.errors == 'ignore' and ex.code == eccodes.lib.GRIB_READ_ONLY:
except bindings.EcCodesError as ex:
if self.errors == 'ignore' and ex.code == bindings.lib.GRIB_READ_ONLY:
# Very noisy error when trying to set computed keys
pass
else:
Expand All @@ -142,7 +142,7 @@ def __len__(self):
return sum(1 for _ in self)

def write(self, file):
eccodes.codes_write(self.codes_id, file)
bindings.codes_write(self.codes_id, file)


@attr.attrs()
Expand Down Expand Up @@ -182,15 +182,15 @@ def make_message_schema(message, schema_keys, log=LOG):
for key in schema_keys:
bkey = key.encode(message.encoding)
try:
key_type = eccodes.codes_get_native_type(message.codes_id, bkey)
except eccodes.EcCodesError as ex:
if ex.code != eccodes.lib.GRIB_NOT_FOUND: # pragma: no cover
key_type = bindings.codes_get_native_type(message.codes_id, bkey)
except bindings.EcCodesError as ex:
if ex.code != bindings.lib.GRIB_NOT_FOUND: # pragma: no cover
log.exception("key %r failed", key)
schema[key] = ()
continue
size = eccodes.codes_get_size(message.codes_id, bkey)
if key_type == eccodes.CODES_TYPE_STRING:
length = eccodes.codes_get_length(message.codes_id, bkey)
size = bindings.codes_get_size(message.codes_id, bkey)
if key_type == bindings.CODES_TYPE_STRING:
length = bindings.codes_get_length(message.codes_id, bkey)
schema[key] = (key_type, size, length)
else:
schema[key] = (key_type, size)
Expand Down Expand Up @@ -272,7 +272,7 @@ def from_filestream(cls, filestream, index_keys):
if isinstance(value, list):
value = tuple(value)
header_values.append(value)
offset = message.message_get('offset', eccodes.CODES_TYPE_LONG)
offset = message.message_get('offset', bindings.CODES_TYPE_LONG)
offsets.setdefault(tuple(header_values), []).append(offset)
return cls(filestream=filestream, index_keys=index_keys, offsets=list(offsets.items()))

Expand Down
Loading

0 comments on commit 66c24bd

Please sign in to comment.