Ejemplo n.º 1
0
 def test_absolute_value(self):
     expression = 'abs(a)'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = xr.Variable(('x',), [-2.2, -3.4, 4.5 * -1])
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(('x',), [2.2, 3.4, 4.5])
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 2
0
 def test_square_root(self):
     expression = 'sqrt(a)'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = xr.Variable(('x',), [4, 9, math.pow(12, 2)])
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(('x',), [2, 3, 12])
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 3
0
 def test_replace_pi(self):
     expression = 'a*pi'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = xr.Variable(['x'], np.asarray([1, 2, 3, 4, 0.01]))
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(['x'], [3.14159265359, 6.28318530718, 9.42477796077, 12.5663706144, 0.0314159265359])
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 4
0
 def test_number_constants_with_scientific_notation(self):
     expression = 'a + 257e-3'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = xr.Variable(['x'], np.asarray([1, 2, 3, 4, 0.01]))
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(['x'], [1.257, 2.257, 3.257, 4.257, 0.267])
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 5
0
 def test_not_with_vertical_1D(self):
     expression = '~ a'  # the meaning is "not a"
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = get_vertical_one_dim_bool_variable()
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(['y'], np.asarray([t, f, t]))
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 6
0
 def test_inverse_tangent(self):
     expression = 'arctan(tan(a))'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = xr.Variable(('x',), [1, 0.5, 0])
     R._load_virtual_variable(ds, 'v')
     expected = ds['a']
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 7
0
 def test_extend_vertical_1D_variable_to_2D(self):
     vertical_variable = xr.Variable('y', [5, 6, 7])
     reference_variable = xr.Variable(('z', 'y', 'x'), [[[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], ], [[4, 5, 6, 7], [5, 6, 7, 8], [6, 7, 8, 9], ], ])
     extended = R._extend_1d_vertical_to_2d(vertical_variable, reference_variable)
     self.assertEqual((3, 4), extended.shape)
     self.assertEqual(('y', 'x'), extended.dims)
     expected = np.asarray([[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], ])
     ftu.assert_array_equals_with_index_error_message(self, expected, extended.data)
Ejemplo n.º 8
0
 def test_modulo(self):
     expression = 'a % 7'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = get_two_dim_int_variable()
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(['y', 'x'], np.asarray(
         [[3, 6, 2, 5], [6, 2, 5, 1], [2, 5, 1, 4]]))
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 9
0
 def test_power(self):
     expression = 'a ** 2'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = get_two_dim_int_variable()
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(['y', 'x'], np.asarray(
         [[100, 400, 900, 1600], [169, 529, 1089, 1849], [256, 676, 1296, 2116]]))
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 10
0
 def test_unary_negation(self):
     expression = '- a'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = get_two_dim_int_variable()
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(['y', 'x'], np.asarray(
         [[-10, -20, -30, -40], [-13, -23, -33, -43], [-16, -26, -36, -46]]))
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 11
0
 def test_or_with_vertical_1D(self):
     expression = 'a | b'  # the meaning is "a or b"
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = get_vertical_one_dim_bool_variable()
     ds['b'] = get_two_dim_bool_variable()
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(['y', 'x'], np.asarray([[t, f, f, t], [t, t, t, t], [t, f, f, f]]))
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 12
0
 def test_addition(self):
     expression = 'a + b'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = get_two_dim_int_variable()
     ds['b'] = get_vertical_one_dim_int_variable()
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(['y', 'x'], np.asarray(
         [[210, 220, 230, 240], [313, 323, 333, 343], [416, 426, 436, 446]]))
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 13
0
 def test_natural_logarithm_1_plus_x(self):
     expression = 'log1p(a)'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = xr.Variable(('x',), [2.2, 3.4, 4.5])
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(('x',), [math.log1p(2.2),
                                     math.log1p(3.4),
                                     math.log1p(4.5), ])
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 14
0
 def test_inverse_hyperbolic_tangent(self):
     expression = 'arctanh(a)'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = xr.Variable(('x',), [0.2, 0.4, 0.5])
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(('x',), [math.atanh(0.2),
                                     math.atanh(0.4),
                                     math.atanh(0.5), ])
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 15
0
 def test_inverse_hyperbolic_cosine(self):
     expression = 'arccosh(a)'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = xr.Variable(('x',), [2, 4, 5])
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(('x',), [math.acosh(2),
                                     math.acosh(4),
                                     math.acosh(5), ])
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 16
0
 def test_multiplication(self):
     expression = 'a * b'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = get_two_dim_int_variable()
     ds['b'] = get_vertical_one_dim_int_variable()
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(['y', 'x'], np.asarray(
         [[2000, 4000, 6000, 8000], [3900, 6900, 9900, 12900], [6400, 10400, 14400, 18400]]))
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 17
0
 def test_exponential_minus_one(self):
     expression = 'expm1(a)'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = xr.Variable(('x',), [2.2, 3.4, 4.5])
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(('x',), [math.expm1(2.2),
                                     math.expm1(3.4),
                                     math.expm1(4.5), ])
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 18
0
 def test_subtraction(self):
     expression = 'b - a'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = get_two_dim_int_variable()
     ds['b'] = get_vertical_one_dim_int_variable()
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(['y', 'x'], np.asarray(
         [[190, 180, 170, 160], [287, 277, 267, 257], [384, 374, 364, 354]]))
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 19
0
 def test_equal(self):
     expression = 'a == 5'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = get_three_dim_int_variable()
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(['z', 'y', 'x'], np.asarray(
         [[[f, f, f, f], [f, f, f, t], [f, f, t, f]],
          [[f, t, f, f], [t, f, f, f], [f, f, f, f]]]))
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 20
0
 def test_dont_replace_pi_if_it_part_of_a_word(self):
     expression = 'lpit + pit + lpi'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['lpit'] = xr.Variable(['x'], np.asarray([1, 2, 3, 4, 0.01]))
     ds['pit'] = xr.Variable(['x'], np.asarray([1, 2, 3, 4, 0.01]))
     ds['lpi'] = xr.Variable(['x'], np.asarray([1, 2, 3, 4, 0.01]))
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(['x'], [3, 6, 9, 12, 0.03])
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 21
0
 def test_arctan2(self):
     expression = 'arctan2(a, b)'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = xr.Variable(('x',), [0.2, 0.4, 0.5])
     ds['b'] = xr.Variable(('x',), [0.8, 0.7, 0.6])
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(('x',), [math.atan2(0.2, 0.8),
                                     math.atan2(0.4, 0.7),
                                     math.atan2(0.5, 0.6), ])
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 22
0
 def test_tangent(self):
     expression = 'tan(a)'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = get_one_dim_radians()
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(['x'], np.asarray(
         [0,
          1,
          6.95515277177,
          -1,
          1,
          6.95515277177]))
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 23
0
 def test_cosine(self):
     expression = 'cos(a)'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = get_one_dim_radians()
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(['x'], np.asarray(
         [1,
          0.70710678,
          0.142314838273,
          -0.70710678,
          -0.70710678,
          -0.142314838273]))
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 24
0
 def test_sinus(self):
     expression = 'sin(a)'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = get_one_dim_radians()
     R._load_virtual_variable(ds, 'v')
     expected = xr.Variable(['x'], np.asarray(
         [0,
          0.70710678,
          0.989821441881,
          0.70710678,
          -0.70710678,
          -0.989821441881]))
     tu.assert_array_equals_with_index_error_message(self, expected, ds['v'])
Ejemplo n.º 25
0
    def test_adding_1_three_dimensional_variable_and_1_vertical_one_dimensional_variable(self):
        ds = xr.Dataset()
        ds['a'] = create_three_dim_variable()
        ds['b'] = create_vertical_one_dim_variable()
        v_var = create_virtual_variable("a + b")
        ds["v_var"] = v_var

        self.fcdr_reader._load_virtual_variable(ds, 'v_var')

        self.assertTrue('v_var' in ds)
        virtual_loaded = ds['v_var']
        self.assertEqual(('z', 'y', 'x'), virtual_loaded.dims)
        self.assertEqual((4, 2, 3), virtual_loaded.shape)
        self.assertEqual(v_var.attrs, virtual_loaded.attrs)

        expected = np.asarray([[[6, 7, 8], [7.1, 8.1, 9.1]], [[16, 17, 18], [17.1, 18.1, 19.1]], [[26, 27, 28], [27.1, 28.1, 29.1]], [[36, 37, 38], [37.1, 38.1, 39.1]]])
        actual = virtual_loaded.values

        self.assertEqual(type(expected), type(actual))
        tu.assert_array_equals_with_index_error_message(self, expected, actual)
Ejemplo n.º 26
0
    def test_adding_1_three_dimensional_variable_and_1_one_dimensional_variable(self):
        ds = xr.Dataset()
        ds['a'] = create_three_dim_variable()
        ds['b'] = create_one_dim_variable()
        v_var = create_virtual_variable("a + b")
        ds["v_var"] = v_var

        self.fcdr_reader._load_virtual_variable(ds, 'v_var')

        self.assertTrue('v_var' in ds)
        virtual_loaded = ds['v_var']
        self.assertEqual(('z', 'y', 'x'), virtual_loaded.dims)
        self.assertEqual((4, 2, 3), virtual_loaded.shape)
        self.assertEqual(v_var.attrs, virtual_loaded.attrs)

        expected = np.asarray([[[2.3, 4.3, 6.3], [2.4, 4.4, 6.4]], [[12.3, 14.3, 16.3], [12.4, 14.4, 16.4]], [[22.3, 24.3, 26.3], [22.4, 24.4, 26.4]], [[32.3, 34.3, 36.3], [32.4, 34.4, 36.4]]])
        actual = virtual_loaded.values

        self.assertEqual(type(expected), type(actual))
        tu.assert_array_equals_with_index_error_message(self, expected, actual)
Ejemplo n.º 27
0
    def test_adding_1_three_dimensional_variable_and_1_two_dimensional_variable(self):
        ds = xr.Dataset()
        ds['a'] = create_three_dim_variable()
        ds['b'] = create_two_dim_variable()
        v_var = create_virtual_variable("a + b")
        ds["v_var"] = v_var

        self.fcdr_reader._load_virtual_variable(ds, 'v_var')

        self.assertTrue('v_var' in ds)
        virtual_loaded = ds['v_var']
        self.assertEqual(('z', 'y', 'x'), virtual_loaded.dims)
        self.assertEqual((4, 2, 3), virtual_loaded.shape)
        self.assertEqual(v_var.attrs, virtual_loaded.attrs)

        expected = np.asarray([[[2, 4, 6], [5.1, 7.1, 9.1]], [[12, 14, 16], [15.1, 17.1, 19.1]], [[22, 24, 26], [25.1, 27.1, 29.1]], [[32, 34, 36], [35.1, 37.1, 39.1]]])
        actual = virtual_loaded.values

        self.assertEqual(type(expected), type(actual))
        tu.assert_array_equals_with_index_error_message(self, expected, actual)
Ejemplo n.º 28
0
    def test_adding_2_three_dimensional_variables(self):
        ds = xr.Dataset()
        ds['a'] = create_three_dim_variable()
        ds['b'] = create_three_dim_variable()
        v_var = create_virtual_variable("a + b")
        ds["v_var"] = v_var

        self.fcdr_reader._load_virtual_variable(ds, 'v_var')

        self.assertTrue('v_var' in ds)
        virtual_loaded = ds['v_var']
        self.assertEqual(('z', 'y', 'x'), virtual_loaded.dims)
        self.assertEqual((4, 2, 3), virtual_loaded.shape)
        self.assertEqual(v_var.attrs, virtual_loaded.attrs)

        expected = np.asarray([[[2, 4, 6], [2.2, 4.2, 6.2]], [[22, 24, 26], [22.2, 24.2, 26.2]], [[42, 44, 46], [42.2, 44.2, 46.2]], [[62, 64, 66], [62.2, 64.2, 66.2]]])
        actual = ds['v_var'].values

        self.assertEqual(type(expected), type(actual))
        tu.assert_array_equals_with_index_error_message(self, expected, actual)
Ejemplo n.º 29
0
    def test_multiplying_1_three_dimensional_variable_and_1_scalar_variable(self):
        ds = xr.Dataset()
        ds['a'] = create_three_dim_variable()
        ds['b'] = create_scalar_variable(2.7)
        v_var = create_virtual_variable("a * b")
        ds["v_var"] = v_var

        self.fcdr_reader._load_virtual_variable(ds, 'v_var')

        self.assertTrue('v_var' in ds)
        virtual_loaded = ds['v_var']
        self.assertEqual(('z', 'y', 'x'), virtual_loaded.dims)
        self.assertEqual((4, 2, 3), virtual_loaded.shape)
        self.assertEqual(v_var.attrs, virtual_loaded.attrs)

        expected = np.asarray(
            [[[2.7, 5.4, 8.1], [2.97, 5.67, 8.37]], [[29.7, 32.4, 35.1], [29.97, 32.67, 35.37]], [[56.7, 59.4, 62.1], [56.97, 59.67, 62.37]], [[83.7, 86.4, 89.1], [83.97, 86.67, 89.37]]])
        actual = virtual_loaded.values

        self.assertEqual(type(expected), type(actual))
        tu.assert_array_equals_with_index_error_message(self, expected, actual)
Ejemplo n.º 30
0
 def test_division(self):
     expression = 'b / a'
     ds = xr.Dataset()
     ds['v'] = create_virtual_variable(expression)
     ds['a'] = get_two_dim_int_variable()
     ds['b'] = get_vertical_one_dim_int_variable()
     R._load_virtual_variable(ds, 'v')
     loaded_ = ds['v']
     # if two arrays of type integer are divided, the result data type can be integer or float
     # the expected preparation depends on returned datatype
     if np.issubdtype(loaded_.dtype, np.integer):
         expected = xr.Variable(['y', 'x'], np.asarray(
             [[20, 10, 6, 5],
              [23, 13, 9, 6],
              [25, 15, 11, 8]]))
     else:
         expected = xr.Variable(['y', 'x'], np.asarray(
             [[20, 10, 6.666666666666667, 5],
              [23.076923076923077, 13.043478260869565, 9.090909090909092, 6.976744186046512],
              [25, 15.384615384615385, 11.11111111111111, 8.695652173913043]]))
     tu.assert_array_equals_with_index_error_message(self, expected, loaded_)