def test_nans_filled_failure(self):
     fill_value = 1e+20
     dtype = np.dtype('int16')
     emsg = 'Fill value of .* invalid for array result .*'
     with self.assertRaisesRegexp(ValueError, emsg):
         convert_nans_array(self.array,
                            nans_replacement=fill_value,
                            result_dtype=dtype)
Пример #2
0
 def test_nans_filled_failure(self):
     fill_value = 1e+20
     dtype = np.dtype('int16')
     emsg = 'Fill value of .* invalid for array result .*'
     with self.assertRaisesRegexp(ValueError, emsg):
         convert_nans_array(self.array,
                            nans_replacement=fill_value,
                            result_dtype=dtype)
 def test_nans_filled(self):
     fill_value = 666.0
     result = convert_nans_array(self.array, nans_replacement=fill_value)
     self.assertNotIsInstance(result, ma.MaskedArray)
     self.assertIs(result, self.array)
     expected = np.array([[1.0, fill_value], [3.0, 4.0]])
     self.assertArrayEqual(result, expected)
Пример #4
0
 def test_nans_filled(self):
     fill_value = 666.0
     result = convert_nans_array(self.array,
                                 nans_replacement=fill_value)
     self.assertNotIsInstance(result, ma.MaskedArray)
     self.assertIs(result, self.array)
     expected = np.array([[1.0, fill_value],
                          [3.0, 4.0]])
     self.assertArrayEqual(result, expected)
 def test_result_dtype_cast_float_to_int(self):
     dtype = np.int
     result = convert_nans_array(self.array,
                                 nans_replacement=ma.masked,
                                 result_dtype=dtype)
     self.assertIsInstance(result, ma.MaskedArray)
     expected = ma.masked_array([[1, 2], [3, 4]], dtype=dtype)
     expected[0, 1] = ma.masked
     self.assertArrayEqual(result, expected)
     self.assertIsNot(result, self.array)
 def test_filled_result_dtype_cast_float_to_int(self):
     fill_value = 666
     dtype = np.int
     result = convert_nans_array(self.array,
                                 nans_replacement=fill_value,
                                 result_dtype=dtype)
     self.assertNotIsInstance(result, ma.MaskedArray)
     expected = np.array([[1, fill_value], [3, 4]], dtype=dtype)
     self.assertArrayEqual(result, expected)
     self.assertIsNot(result, self.array)
 def test_nans_masked(self):
     result = convert_nans_array(self.array, nans_replacement=ma.masked)
     self.assertIsInstance(result, ma.MaskedArray)
     self.assertArrayEqual(result.mask, [[False, True], [False, False]])
     dummy = 666.0
     result[0, 1] = dummy
     self.assertArrayEqual(result.data, [[1.0, dummy], [3.0, 4.0]])
     self.assertIsNot(result, self.array)
     # Check that fill value is the "standard" one for the type.
     expected = ma.masked_array(self.array[0], dtype=self.array.dtype)
     self.assertEqual(result.fill_value, expected.fill_value)
Пример #8
0
 def test_result_dtype_cast_float_to_int(self):
     dtype = np.int
     result = convert_nans_array(self.array,
                                 nans_replacement=ma.masked,
                                 result_dtype=dtype)
     self.assertIsInstance(result, ma.MaskedArray)
     expected = ma.masked_array([[1, 2],
                                 [3, 4]],
                                dtype=dtype)
     expected[0, 1] = ma.masked
     self.assertArrayEqual(result, expected)
     self.assertIsNot(result, self.array)
Пример #9
0
 def test_filled_result_dtype_cast_float_to_int(self):
     fill_value = 666
     dtype = np.int
     result = convert_nans_array(self.array,
                                 nans_replacement=fill_value,
                                 result_dtype=dtype)
     self.assertNotIsInstance(result, ma.MaskedArray)
     expected = np.array([[1, fill_value],
                          [3, 4]],
                         dtype=dtype)
     self.assertArrayEqual(result, expected)
     self.assertIsNot(result, self.array)
Пример #10
0
    def __init__(self, grib_message, grib_fh=None):
        """Store the grib message and compute our extra keys."""
        self.grib_message = grib_message
        self.realised_dtype = np.array([0.]).dtype

        if self.edition != 1:
            emsg = 'GRIB edition {} is not supported by {!r}.'
            raise TranslationError(
                emsg.format(self.edition,
                            type(self).__name__))

        deferred = grib_fh is not None

        # Store the file pointer and message length from the current
        # grib message before it's changed by calls to the grib-api.
        if deferred:
            # Note that, the grib-api has already read this message and
            # advanced the file pointer to the end of the message.
            offset = grib_fh.tell()
            message_length = gribapi.grib_get_long(grib_message, 'totalLength')

        # Initialise the key-extension dictionary.
        # NOTE: this attribute *must* exist, or the the __getattr__ overload
        # can hit an infinite loop.
        self.extra_keys = {}
        self._confirm_in_scope()
        self._compute_extra_keys()

        # Calculate the data payload shape.
        shape = (gribapi.grib_get_long(grib_message, 'numberOfValues'), )

        if not self.gridType.startswith('reduced'):
            ni, nj = self.Ni, self.Nj
            j_fast = gribapi.grib_get_long(grib_message,
                                           'jPointsAreConsecutive')
            shape = (nj, ni) if j_fast == 0 else (ni, nj)

        if deferred:
            # Wrap the reference to the data payload within the data proxy
            # in order to support deferred data loading.
            # The byte offset requires to be reset back to the first byte
            # of this message. The file pointer offset is always at the end
            # of the current message due to the grib-api reading the message.
            proxy = GribDataProxy(shape, self.realised_dtype, grib_fh.name,
                                  offset - message_length)
            self._data = as_lazy_data(proxy)
        else:
            values_array = _message_values(grib_message, shape)
            # mask where the values are nan
            self.data = convert_nans_array(values_array,
                                           nans_replacement=ma.masked)
Пример #11
0
    def __init__(self, grib_message, grib_fh=None):
        """Store the grib message and compute our extra keys."""
        self.grib_message = grib_message
        self.realised_dtype = np.array([0.]).dtype

        if self.edition != 1:
            emsg = 'GRIB edition {} is not supported by {!r}.'
            raise TranslationError(emsg.format(self.edition,
                                               type(self).__name__))

        deferred = grib_fh is not None

        # Store the file pointer and message length from the current
        # grib message before it's changed by calls to the grib-api.
        if deferred:
            # Note that, the grib-api has already read this message and
            # advanced the file pointer to the end of the message.
            offset = grib_fh.tell()
            message_length = gribapi.grib_get_long(grib_message, 'totalLength')

        # Initialise the key-extension dictionary.
        # NOTE: this attribute *must* exist, or the the __getattr__ overload
        # can hit an infinite loop.
        self.extra_keys = {}
        self._confirm_in_scope()
        self._compute_extra_keys()

        # Calculate the data payload shape.
        shape = (gribapi.grib_get_long(grib_message, 'numberOfValues'),)

        if not self.gridType.startswith('reduced'):
            ni, nj = self.Ni, self.Nj
            j_fast = gribapi.grib_get_long(grib_message,
                                           'jPointsAreConsecutive')
            shape = (nj, ni) if j_fast == 0 else (ni, nj)

        if deferred:
            # Wrap the reference to the data payload within the data proxy
            # in order to support deferred data loading.
            # The byte offset requires to be reset back to the first byte
            # of this message. The file pointer offset is always at the end
            # of the current message due to the grib-api reading the message.
            proxy = GribDataProxy(shape, self.realised_dtype, grib_fh.name,
                                  offset - message_length)
            self._data = as_lazy_data(proxy)
        else:
            values_array = _message_values(grib_message, shape)
            # mask where the values are nan
            self.data = convert_nans_array(values_array,
                                           nans_replacement=ma.masked)
Пример #12
0
 def test_nans_masked(self):
     result = convert_nans_array(self.array,
                                 nans_replacement=ma.masked)
     self.assertIsInstance(result, ma.MaskedArray)
     self.assertArrayEqual(result.mask, [[False, True],
                                         [False, False]])
     dummy = 666.0
     result[0, 1] = dummy
     self.assertArrayEqual(result.data, [[1.0, dummy],
                                         [3.0, 4.0]])
     self.assertIsNot(result, self.array)
     # Check that fill value is the "standard" one for the type.
     expected = ma.masked_array(self.array[0], dtype=self.array.dtype)
     self.assertEqual(result.fill_value, expected.fill_value)
Пример #13
0
 def test_nans_none_failure(self):
     emsg = 'Array contains unexpected NaNs'
     with self.assertRaisesRegexp(ValueError, emsg):
         convert_nans_array(self.array)
Пример #14
0
 def test_pass_thru_masked_array_float(self):
     array = ma.arange(10, dtype=np.float)
     result = convert_nans_array(array)
     self.assertIsInstance(result, ma.MaskedArray)
     self.assertIs(result, array)
Пример #15
0
 def test_pass_thru_array_integer(self):
     array = np.arange(10)
     result = convert_nans_array(array)
     self.assertNotIsInstance(result, ma.MaskedArray)
     self.assertIs(result, array)
Пример #16
0
 def test_no_nans(self):
     array = np.array([[1.0, 2.0],
                       [3.0, 4.0]])
     result = convert_nans_array(array)
     self.assertNotIsInstance(result, ma.MaskedArray)
     self.assertIs(result, array)
Пример #17
0
 def test_pass_thru_array_integer(self):
     array = np.arange(10)
     result = convert_nans_array(array)
     self.assertNotIsInstance(result, ma.MaskedArray)
     self.assertIs(result, array)
Пример #18
0
 def test_pass_thru_masked_array_float(self):
     array = ma.arange(10, dtype=np.float)
     result = convert_nans_array(array)
     self.assertIsInstance(result, ma.MaskedArray)
     self.assertIs(result, array)
Пример #19
0
 def test_no_nans(self):
     array = np.array([[1.0, 2.0], [3.0, 4.0]])
     result = convert_nans_array(array)
     self.assertNotIsInstance(result, ma.MaskedArray)
     self.assertIs(result, array)
Пример #20
0
 def test_nans_none_failure(self):
     emsg = 'Array contains unexpected NaNs'
     with self.assertRaisesRegexp(ValueError, emsg):
         convert_nans_array(self.array)