def _import_variable(c_variable): # Import variable data. data = _import_array(c_variable.data_type, c_variable.num_elements, c_variable.data) num_dimensions = c_variable.num_dimensions if num_dimensions == 0: variable = Variable(numpy.asscalar(data)) else: data = data.reshape( [c_variable.dimension[i] for i in range(num_dimensions)]) dimension = [ _get_py_dimension_type(c_variable.dimension_type[i]) for i in range(num_dimensions) ] variable = Variable(data, dimension) # Import variable attributes. if c_variable.unit != _ffi.NULL: variable.unit = _decode_string(_ffi.string(c_variable.unit)) if c_variable.data_type != _lib.harp_type_string: variable.valid_min = _import_scalar(c_variable.data_type, c_variable.valid_min) variable.valid_max = _import_scalar(c_variable.data_type, c_variable.valid_max) if c_variable.description: variable.description = _decode_string( _ffi.string(c_variable.description)) return variable
def _import_product(c_product): product = Product() # Import product attributes. if c_product.source_product: product.source_product = _decode_string(_ffi.string(c_product.source_product)) if c_product.history: product.history = _decode_string(_ffi.string(c_product.history)) # Import variables. for i in range(c_product.num_variables): c_variable_ptr = c_product.variable[i] variable = _import_variable(c_variable_ptr[0]) setattr(product, _decode_string(_ffi.string(c_variable_ptr[0].name)), variable) return product
def _import_product(c_product): product = Product() # Import product attributes. if c_product.source_product: product.source_product = _decode_string( _ffi.string(c_product.source_product)) if c_product.history: product.history = _decode_string(_ffi.string(c_product.history)) # Import variables. for i in range(c_product.num_variables): c_variable_ptr = c_product.variable[i] variable = _import_variable(c_variable_ptr[0]) setattr(product, _decode_string(_ffi.string(c_variable_ptr[0].name)), variable) return product
def __init__(self, errno=None, strerror=None): if errno is None: errno = _lib.harp_errno if strerror is None: strerror = _decode_string(_ffi.string(_lib.harp_errno_to_string(errno))) super(CLibraryError, self).__init__(errno, strerror) self.errno = errno self.strerror = strerror
def __init__(self, errno=None, strerror=None): if errno is None: errno = _lib.harp_errno if strerror is None: strerror = _decode_string( _ffi.string(_lib.harp_errno_to_string(errno))) super(CLibraryError, self).__init__(errno, strerror) self.errno = errno self.strerror = strerror
def _import_array(c_data_type, c_num_elements, c_data): if c_data_type == _lib.harp_type_string: data = numpy.empty((c_num_elements,), dtype=numpy.object) for i in range(c_num_elements): # NB. The _ffi.string() method returns a copy of the C string. data[i] = _decode_string(_ffi.string(c_data.string_data[i])) return data # NB. The _ffi.buffer() method, as well as the numpy.frombuffer() method, provide a view on the C array; neither # method incurs a copy. c_data_buffer = _ffi.buffer(c_data.ptr, c_num_elements * _lib.harp_get_size_for_type(c_data_type)) return numpy.copy(numpy.frombuffer(c_data_buffer, dtype=_get_py_data_type(c_data_type)))
def _import_array(c_data_type, c_num_elements, c_data): if c_data_type == _lib.harp_type_string: data = numpy.empty((c_num_elements, ), dtype=numpy.object) for i in range(c_num_elements): # NB. The _ffi.string() method returns a copy of the C string. data[i] = _decode_string(_ffi.string(c_data.string_data[i])) return data # NB. The _ffi.buffer() method, as well as the numpy.frombuffer() method, provide a view on the C array; neither # method incurs a copy. c_data_buffer = _ffi.buffer( c_data.ptr, c_num_elements * _lib.harp_get_size_for_type(c_data_type)) return numpy.copy( numpy.frombuffer(c_data_buffer, dtype=_get_py_data_type(c_data_type)))
def _import_variable(c_variable): # Import variable data. data = _import_array(c_variable.data_type, c_variable.num_elements, c_variable.data) num_dimensions = c_variable.num_dimensions if num_dimensions == 0: variable = Variable(numpy.asscalar(data)) else: data = data.reshape([c_variable.dimension[i] for i in range(num_dimensions)]) dimension = [_get_py_dimension_type(c_variable.dimension_type[i]) for i in range(num_dimensions)] variable = Variable(data, dimension) # Import variable attributes. if c_variable.unit != _ffi.NULL: variable.unit = _decode_string(_ffi.string(c_variable.unit)) if c_variable.data_type != _lib.harp_type_string: variable.valid_min = _import_scalar(c_variable.data_type, c_variable.valid_min) variable.valid_max = _import_scalar(c_variable.data_type, c_variable.valid_max) if c_variable.description: variable.description = _decode_string(_ffi.string(c_variable.description)) return variable
def version(): """Return the version of the HARP C library.""" return _decode_string(_ffi.string(_lib.libharp_version))