Esempio n. 1
0
    def test_extract_albedo(self):
        """
        Test that extracting albedo data from a grid returns an array
        structure of the right dimensions, containing the right albedo
        data in the right order.
        """
        cell_00 = GridCell(1, 2, 0.1)
        cell_01 = GridCell(2, 4, 0.2)
        cell_10 = GridCell(3, 6, 0.3)
        cell_11 = GridCell(4, 8, 0.4)

        cells = [[cell_00, cell_01], [cell_10, cell_11]]

        grid = LatLongGrid(cells)
        albedo_data = grid.extract_datapoint("albedo")
        expected_albedo_data = [[0.1, 0.2], [0.3, 0.4]]

        self.assertEqual(len(albedo_data), 2)
        self.assertEqual(len(albedo_data[0]), 2)

        # Loop for comparison, because extract_datapoint may return its data
        # in a non-list format such as an array.
        for i in range(len(cells)):
            for j in range(len(cells[0])):
                self.assertEqual(albedo_data[i][j], expected_albedo_data[i][j])
Esempio n. 2
0
    def test_dimensions_small(self):
        """
        Test correct grid dimensions as returned by a grid's dimensions()
        method for various small grids.
        """
        cells = [[GridCell(1, 1, 0.1),
                  GridCell(2, 2, 0.2)],
                 [GridCell(3, 3, 0.3),
                  GridCell(4, 4, 0.4)]]

        # 2x2 grid example.
        grid = LatLongGrid(cells)
        dims = grid.dimensions().dims_by_count()
        dims_expected = (2, 2)
        self.assertEqual(dims, dims_expected)

        # Reduced grid example, with 1 dimension in latitude.
        grid = LatLongGrid(cells[1:])
        dims = grid.dimensions().dims_by_count()
        dims_expected = (1, 2)
        self.assertEqual(dims, dims_expected)

        # Reduced grid example, with 1 dimension in longitude.
        grid = LatLongGrid([[cells[0][0]], [cells[1][0]]])
        dims = grid.dimensions().dims_by_count()
        dims_expected = (2, 1)
        self.assertEqual(dims, dims_expected)
Esempio n. 3
0
    def test_out_of_bounds_albedo_error(self):
        """
        Test that attempting to create a grid cell with albedo greater than 1
        results in an error.
        """
        with self.assertRaises(ValueError):
            cell = GridCell(87, 45, 2)

        with self.assertRaises(ValueError):
            cell = GridCell(51.6, 25.2, 1.01)
Esempio n. 4
0
    def test_negative_albedo_error(self):
        """
        Test that attempting to create a grid cell with negative albedo
        results in an error.
        """
        with self.assertRaises(ValueError):
            cell = GridCell(37, 25, -1)

        with self.assertRaises(ValueError):
            cell = GridCell(70.6, 1.9, -0.01)
Esempio n. 5
0
    def test_out_of_bounds_humidity_error(self):
        """
        Test that attempting to create a grid cell with relative humidity
        above 100% results in an error.
        """
        with self.assertRaises(ValueError):
            cell = GridCell(26, 125, 0.22)

        with self.assertRaises(ValueError):
            cell = GridCell(83, 100.01, 0.4)
Esempio n. 6
0
    def test_negative_humidity_error(self):
        """
        Test that attempting to create a grid cell with negative relative
        humidity results in an error.
        """
        with self.assertRaises(ValueError):
            cell = GridCell(12, -62, 0.67)

        with self.assertRaises(ValueError):
            cell = GridCell(162, -0.01, 0.925)
Esempio n. 7
0
    def test_negative_temp_error(self):
        """
        Test that attempting to create a grid cell with negative temperature
        results in an error.
        """
        with self.assertRaises(ValueError):
            cell = GridCell(-50, 50, 0.1)

        with self.assertRaises(ValueError):
            cell = GridCell(-0.01, 25, 0.36)
Esempio n. 8
0
    def test_high_border_values(self):
        """
        Test that a grid cell can be created without error using high border
        values for all three variables, using 100 for humidity and 1 for
        albedo. Temperature has no high border.
        """
        cell = GridCell(987654321, 100, 1)

        self.assertEqual(cell.get_temperature(), 987654321)
        self.assertEqual(cell.get_relative_humidity(), 100)
        self.assertEqual(cell.get_albedo(), 1)
Esempio n. 9
0
    def test_low_border_values(self):
        """
        Test that a grid cell can be created without error using low border
        values for all three variables, namely 0 for temperature, humidity,
        and albedo.
        """
        cell = GridCell(0, 0, 0)

        self.assertEqual(cell.get_temperature(), 0)
        self.assertEqual(cell.get_relative_humidity(), 0)
        self.assertEqual(cell.get_albedo(), 0)
Esempio n. 10
0
    def _build_grid(self,
                    dimensions: Tuple[int, int],
                    temp_data: np.ndarray,
                    humidity: Optional[np.ndarray] = None,
                    albedo: Optional[np.ndarray] = None,
                    pressure: Optional[float] = None) -> 'LatLongGrid':
        grid_cells = []

        for j in range(dimensions[0]):
            # Holding row lists in memory prevents excess list lookups.
            temp_row = temp_data[j]
            r_hum_row = None if humidity is None else humidity[j]
            albedo_row = None if albedo is None else albedo[j]
            # Start creating a new list column for entry into the output
            # list.
            longitude_row = []

            for k in range(dimensions[1]):
                # Package the data from this grid cell into a GridCell
                # object.
                temp_val = temp_row[k]
                r_hum_val = None if humidity is None else r_hum_row[k]
                albedo_val = None if albedo is None else albedo_row[k]

                grid_cell_obj = GridCell(temp_val, r_hum_val, albedo_val)

                # Add new GridCell objects into the 2-D nested lists.
                longitude_row.append(grid_cell_obj)
            grid_cells.append(longitude_row)
        level_grid = LatLongGrid(grid_cells)
        level_grid.set_pressure(pressure)
        return level_grid
Esempio n. 11
0
    def test_iteration_order(self):
        """
        Test that iteration through a grid returns all grid cells and proceeds
        in left-to-right, top-to-bottom order.
        """
        cell_00 = GridCell(1, 1, 0.1)
        cell_01 = GridCell(2, 2, 0.2)
        cell_10 = GridCell(3, 3, 0.3)
        cell_11 = GridCell(4, 4, 0.4)

        cells = [[cell_00, cell_01], [cell_10, cell_11]]

        grid = LatLongGrid(cells)
        iter_order = [cell for cell in grid]
        expected_iter_order = [cell_00, cell_01, cell_10, cell_11]

        self.assertEqual(iter_order, expected_iter_order)
Esempio n. 12
0
    def test_valid_init_small(self):
        """
        Test error-free creation of a two-dimensional grid from an appropriate
        list of grid cells.
        """
        cells = [[GridCell(1, 1, 0.1)]]

        grid = LatLongGrid(cells)
        dims = grid.dimensions().dims_by_count()
        dims_expected = (1, 1)

        self.assertEqual(dims, dims_expected)
Esempio n. 13
0
    def test_invalid_temperature_change(self):
        """
        Test that an error is raised when a grid cell's temperature is changed
        to an invalid value after the cell is instantiated. Also ensures that
        the grid cell's temperature is unchanged after the attempt.
        """
        cell = GridCell(273.15, 65, 0.6)
        cell.set_temperature(274.15)

        with self.assertRaises(ValueError):
            cell.set_temperature(-1)

        self.assertEqual(cell.get_temperature(), 274.15)
        self.assertEqual(cell.get_temperature_change(), 1)
Esempio n. 14
0
    def test_set_coord_valid(self):
        """
        Test that the grid set_coord method uses coordinates that are
        consistent with get_coords, and that it updates the grid in the
        appropriate manner.
        """
        cell_00 = GridCell(1, 1, 0.1)
        cell_01 = GridCell(2, 2, 0.2)
        cell_10 = GridCell(3, 3, 0.3)
        cell_11 = GridCell(4, 4, 0.4)

        cell_00_new = GridCell(5, 5, 0.5)
        cell_10_new = GridCell(6, 6, 0.6)

        cells = [[cell_00, cell_01], [cell_10, cell_11]]

        grid = LatLongGrid(cells)
        grid.set_coord(0, 0, cell_00_new)
        grid.set_coord(1, 0, cell_10_new)

        cell_at_00 = grid.get_coord(0, 0)
        cell_at_01 = grid.get_coord(0, 1)
        cell_at_10 = grid.get_coord(1, 0)
        cell_at_11 = grid.get_coord(1, 1)

        self.assertEqual(cell_at_00, cell_00_new)
        self.assertEqual(cell_at_01, cell_01)
        self.assertEqual(cell_at_10, cell_10_new)
        self.assertEqual(cell_at_11, cell_11)
Esempio n. 15
0
    def test_get_cell_out_of_bounds_error(self):
        """
        Test that the grid get_coords method raises errors when any indices
        are given that are outside of the bounds of the arrays.
        """
        cell_00 = GridCell(1, 1, 0.1)
        cell_01 = GridCell(2, 2, 0.2)
        cell_10 = GridCell(3, 3, 0.3)
        cell_11 = GridCell(4, 4, 0.4)

        cells = [[cell_00, cell_01], [cell_10, cell_11]]

        grid = LatLongGrid(cells)

        with self.assertRaises(IndexError):
            grid.get_coord(-1, 0)
        with self.assertRaises(IndexError):
            grid.get_coord(0, -1)
        with self.assertRaises(IndexError):
            grid.get_coord(1, 2)
        with self.assertRaises(IndexError):
            grid.get_coord(2, 0)
Esempio n. 16
0
    def test_get_coords_small(self):
        """
        Test that the grid get_coord method returns the grid cell instance at
        the requested coordinates, relative to the appropriate points.
        """
        cell_00 = GridCell(1, 1, 0.1)
        cell_01 = GridCell(2, 2, 0.2)
        cell_10 = GridCell(3, 3, 0.3)
        cell_11 = GridCell(4, 4, 0.4)

        cells = [[cell_00, cell_01], [cell_10, cell_11]]

        grid = LatLongGrid(cells)
        cell_at_00 = grid.get_coord(0, 0)
        cell_at_01 = grid.get_coord(0, 1)
        cell_at_10 = grid.get_coord(1, 0)
        cell_at_11 = grid.get_coord(1, 1)

        self.assertEqual(cell_at_00, cell_00)
        self.assertEqual(cell_at_01, cell_01)
        self.assertEqual(cell_at_10, cell_10)
        self.assertEqual(cell_at_11, cell_11)
Esempio n. 17
0
    def test_invalid_humidity_change(self):
        """
        Test that an error is raised when a grid cell's relative humidity is
        changed to an invalid value after the cell is instantiated. Also
        ensures that the grid cell's humidity is unchanged after the attempt.
        """
        cell = GridCell(273.15, 65, 0.6)

        with self.assertRaises(ValueError):
            cell.set_relative_humidity(-0.01)

        with self.assertRaises(ValueError):
            cell.set_relative_humidity(100.1)

        self.assertEqual(cell.get_relative_humidity(), 65)
Esempio n. 18
0
    def test_dimensions_large(self):
        """
        Test correct grid dimensions as returned by the grid dimensions()
        method for one larger grid.
        """
        cells = []
        lat, lon = (172, 269)

        for i in range(lat):
            row = []
            for j in range(lon):
                row.append(GridCell(1, 1, 1))
            cells.append(row)

        grid = LatLongGrid(cells)
        dims = grid.dimensions().dims_by_count()
        dims_expected = (lat, lon)
        self.assertEqual(dims, dims_expected)
Esempio n. 19
0
    def test_valid_init(self):
        """
        Test error-free creation of a valid grid cell, and proper returns from
        getter methods.
        """
        temp = 1
        r_hum = 75
        albedo = 1

        cell = GridCell(temp, r_hum, albedo)

        self.assertEqual(cell.get_temperature(), temp)
        self.assertEqual(cell.get_relative_humidity(), r_hum)
        self.assertEqual(cell.get_albedo(), albedo)
        self.assertEqual(cell.get_temperature_change(), 0)
Esempio n. 20
0
    def test_extract_three_dimensional_datapoint(self):
        """
        Test conversion of a one-level of grids into a three-dimensional
        structure of temperature data extracted from the grid. Checks
        correct ordering of data in the output.
        """
        cell_000 = GridCell(1, 2, 0.1)
        cell_001 = GridCell(2, 4, 0.2)
        cell_010 = GridCell(3, 6, 0.3)
        cell_011 = GridCell(4, 8, 0.4)

        cell_100 = GridCell(5, 10, 0.5)
        cell_101 = GridCell(6, 12, 0.6)
        cell_110 = GridCell(7, 14, 0.7)
        cell_111 = GridCell(8, 16, 0.8)

        cells0 = [[cell_000, cell_001], [cell_010, cell_011]]
        cells1 = [[cell_100, cell_101], [cell_110, cell_111]]

        grid0 = LatLongGrid(cells0)
        grid1 = LatLongGrid(cells1)

        grid0_temp_expected = [[1, 2], [3, 4]]
        grid1_temp_expected = [[5, 6], [7, 8]]

        temp_data = extract_multidimensional_grid_variable([grid0, grid1],
                                                           "temperature")

        self.assertEqual(len(temp_data), 2)
        self.assertEqual(len(temp_data[0]), 2)
        self.assertEqual(len(temp_data[0][0]), 2)

        for i in range(len(temp_data)):
            for j in range(len(temp_data[0])):
                self.assertEqual(temp_data[0][i][j], grid0_temp_expected[i][j])
                self.assertEqual(temp_data[1][i][j], grid1_temp_expected[i][j])
Esempio n. 21
0
    def test_extract_temperature_change(self):
        """
        Test that extracting temperature change data from a grid returns an
        array structure of the right dimensions, containing the right delta
        temperature data in the right order.
        """
        cell_00 = GridCell(1, 2, 0.1)
        cell_01 = GridCell(2, 4, 0.2)
        cell_10 = GridCell(3, 6, 0.3)
        cell_11 = GridCell(4, 8, 0.4)

        cell_01.set_temperature(2.5)
        cell_10.set_temperature(4)
        cell_11.set_temperature(5.5)

        cells = [[cell_00, cell_01], [cell_10, cell_11]]

        grid = LatLongGrid(cells)
        delta_temp_data = grid.extract_datapoint("delta_t")
        expected_delta_temp_data = [[0, 0.5], [1, 1.5]]

        self.assertEqual(len(delta_temp_data), 2)
        self.assertEqual(len(delta_temp_data[0]), 2)

        # Loop for comparison, because extract_datapoint may return its data
        # in a non-list format such as an array.
        for i in range(len(cells)):
            for j in range(len(cells[0])):
                self.assertEqual(delta_temp_data[i][j],
                                 expected_delta_temp_data[i][j])
Esempio n. 22
0
    def test_temperature_change_tracks(self):
        """
        Test that changes to a grid cell's temperature are reflected properly
        in both its temperature value and temperature change value.
        """
        temp = 298
        r_hum = 37.25
        albedo = 0.5

        cell = GridCell(temp, r_hum, albedo)
        self.assertEqual(cell.get_temperature_change(), 0)

        cell.set_temperature(temp + 1)
        self.assertEqual(cell.get_temperature(), temp + 1)
        self.assertEqual(cell.get_temperature_change(), 1)

        cell.set_temperature(temp - 1)
        self.assertEqual(cell.get_temperature(), temp - 1)
        self.assertEqual(cell.get_temperature_change(), -1)