Exemplo n.º 1
0
    def setUp(self):
        x_points = np.arange(-10, 11, 5)
        y_points = np.arange(-5, 6, 5)
        y, x = np.meshgrid(y_points, x_points)
        x = Coord(
            x, Metadata(name='lat', standard_name='latitude', units='degrees'))
        y = Coord(
            y, Metadata(name='lon', standard_name='longitude',
                        units='degrees'))
        data = np.reshape(np.arange(15) + 1.0, (5, 3))
        self.coords = CoordList([x, y])

        ug1 = UngriddedData(
            data,
            Metadata(standard_name='rainfall_flux',
                     long_name="TOTAL RAINFALL RATE: LS+CONV KG/M2/S",
                     units="kg m-2 s-1",
                     missing_value=-999), self.coords)
        ug2 = UngriddedData(
            data * 0.1,
            Metadata(standard_name='snowfall_flux',
                     long_name="TOTAL SNOWFALL RATE: LS+CONV KG/M2/S",
                     units="kg m-2 s-1",
                     missing_value=-999), self.coords)
        self.ungridded_data_list = UngriddedDataList([ug1, ug2])
Exemplo n.º 2
0
    def test_horizontal_constraint_in_2d(self):
        from cis.data_io.Coord import Coord, CoordList
        from cis.data_io.ungridded_data import Metadata
        ug_data = mock.make_regular_2d_ungridded_data()
        ug_data_points = ug_data.as_data_frame(time_index=False, name='vals').dropna(axis=1)
        sample_points = UngriddedData(np.array([0.0]), Metadata(),
                                      CoordList([Coord(np.array([7.5]), Metadata(standard_name='latitude')),
                                                 Coord(np.array([-2.5]), Metadata(standard_name='longitude'))]))
        sample_points_view = sample_points.as_data_frame(time_index=False, name='vals').dropna(axis=1)
        # sample_point = HyperPoint(lat=7.5, lon=-2.5)
        # sample_points = HyperPointList([sample_point])
        coord_map = None

        # One degree near 0, 0 is about 110km in latitude and longitude, so 300km should keep us to within 3 degrees
        #  in each direction
        constraint = SepConstraintKdtree(h_sep=400)

        index = HaversineDistanceKDTreeIndex()
        index.index_data(sample_points, ug_data_points, coord_map, leafsize=2)
        constraint.haversine_distance_kd_tree_index = index

        # This should leave us with 4 points
        ref_vals = np.array([10, 11, 13, 14])

        indices = constraint.haversine_distance_kd_tree_index.find_points_within_distance_sample(sample_points_view, 400)

        new_vals = ug_data.data.flat[indices]

        eq_(ref_vals.size, len(new_vals[0]))
        assert (np.equal(ref_vals, new_vals).all())
Exemplo n.º 3
0
    def test_get_all_points_returns_points(self):
        x_points = np.arange(-10, 11, 5)
        y_points = np.arange(-5, 6, 5)
        y, x = np.meshgrid(y_points, x_points)

        x = Coord(x, Metadata(standard_name='latitude', units='degrees'))
        y = Coord(y, Metadata(standard_name='longitude', units='degrees'))
        data = np.reshape(np.arange(15) + 1.0, (5, 3))

        coords = CoordList([x, y])
        ug = UngriddedData(data, Metadata(standard_name='rain', long_name="TOTAL RAINFALL RATE: LS+CONV KG/M2/S",
                                          units="kg m-2 s-1", missing_value=-999), coords)
        points = ug.get_all_points()
        num_points = len([p for p in points])
        assert(num_points == 15)
Exemplo n.º 4
0
    def test_can_create_ungridded_data(self):
        x_points = np.arange(-10, 11, 5)
        y_points = np.arange(-5, 6, 5)
        y, x = np.meshgrid(y_points, x_points)

        x = Coord(x, Metadata(standard_name='latitude', units='degrees'))
        y = Coord(y, Metadata(standard_name='longitude', units='degrees'))
        data = np.reshape(np.arange(15) + 1.0, (5, 3))

        coords = CoordList([x, y])
        ug = UngriddedData(data, Metadata(standard_name='rain', long_name="TOTAL RAINFALL RATE: LS+CONV KG/M2/S",
                                          units="kg m-2 s-1", missing_value=-999), coords)
        standard_coords = ug.coords().find_standard_coords()
        assert(standard_coords == [x, y, None, None, None])
        assert(ug.data.size == 15)
Exemplo n.º 5
0
    def create_data_object(self, filenames, variable):
        from cis.exceptions import InvalidVariableError, CISError
        import numpy as np

        try:
            data_obj = load_multiple_hysplit(filenames, [variable])
        except ValueError:
            raise InvalidVariableError(variable + " does not exist in " +
                                       str(filenames))
        except EOFError as e:
            raise CISError(e)
        except IOError as e:
            raise CISError(e)  # TODO

        coords = self._create_coord_list(filenames, data_obj)

        # WRITE STANDARD NAME GUESSER HERE
        if variable == "PRESSURE":
            variable = "air_pressure"
        elif variable == "RELHUMID":
            variable = "relative_humidity"

        objM = Metadata(name=variable,
                        standard_name=variable,
                        long_name=variable,
                        shape=(len(data_obj[variable]), ),
                        missing_value=-99999.0)
        #objM.standard_name = None

        #print((len(data_obj[variable]),))
        return UngriddedData(data_obj[variable], objM, coords)
Exemplo n.º 6
0
    def create_coords(self, filenames, variable=None):
        """
        Reads the coordinates and data if required from the files
        :param filenames: List of filenames to read coordinates from
        :param variable: load a variable for the data
        :return: Coordinates
        """
        data_variables, variable_selector = self._load_data(
            filenames, variable)

        dim_coords = self._create_coordinates_list(data_variables,
                                                   variable_selector)

        if variable is None:
            return UngriddedCoordinates(dim_coords)
        else:
            aux_coord_name = variable_selector.find_auxiliary_coordinate(
                variable)
            if aux_coord_name is not None:
                all_coords = self._add_aux_coordinate(
                    dim_coords, filenames[0], aux_coord_name,
                    dim_coords.get_coord(standard_name='time').data.size)
            else:
                all_coords = dim_coords
            return UngriddedData(data_variables[variable],
                                 get_metadata(data_variables[variable][0]),
                                 all_coords)
Exemplo n.º 7
0
    def create_coords(self, filenames, usr_variable=None):
        from cis.data_io.netcdf import read_many_files_individually, get_metadata
        from cis.data_io.Coord import Coord, CoordList
        from cis.data_io.ungridded_data import UngriddedCoordinates, UngriddedData
        from cis.exceptions import InvalidVariableError

        variables = [("longitude", "x"), ("latitude", "y")]

        # if usr_variable is not None:
        #     variables.append((usr_variable, ''))

        logging.info("Listing coordinates: " + str(variables))

        coords = CoordList()
        var_data = read_many_files_individually(filenames,
                                                [v[0] for v in variables])
        for var, (name, axis) in zip(var_data.values(), variables):
            try:
                coords.append(Coord(var, get_metadata(var[0]), axis=axis))
            except InvalidVariableError:
                pass

        # Note - We don't need to convert this time coord as it should have been written in our
        #  'standard' time unit

        if usr_variable is None:
            res = UngriddedCoordinates(coords)
        else:
            usr_var_data = read_many_files_individually(
                filenames, usr_variable)[usr_variable]
            res = UngriddedData(usr_var_data, get_metadata(usr_var_data[0]),
                                coords)

        return res
Exemplo n.º 8
0
 def create_data_object(self, filenames, variable):
     data_dict = {}  #initialise data dictionary
     inData = netCDF4.Dataset(filenames[0])  #open netCDF file
     data_dict['longitude'] = np.array(
         inData.variables['lon'])  #extract longitudes
     data_dict['latitude'] = np.array(
         inData.variables['lat'])  #extract latitudes
     origTimes = np.array(inData.variables['time_counter'])  #extract times
     #Convert time to days since
     niceDateTime = cf_units.num2date(origTimes,
                                      'seconds since 1999-01-01 00:00:00',
                                      'gregorian')
     data_dict['time'] = cf_units.date2num(
         niceDateTime, 'days since 1600-01-01 00:00:00', 'gregorian')
     data_dict[variable] = np.array(
         inData.variables[variable])  #extract requested variable
     inData.close()  #close netCDF file
     coords = self._create_coord_list(filenames, data_dict)
     return UngriddedData(
         data_dict[variable],
         Metadata(name=variable,
                  long_name=variable,
                  shape=(len(data_dict), ),
                  missing_value=-999.0,
                  units="1"), coords)
Exemplo n.º 9
0
def stack_data_list(data_list, var_name=None, units=None):
    """
    Stacks a list of Ungridded data objects with the same data variable, but different coordinates into a single
    UngriddedData object, with accompanying lat, lon and time data.


    It assumes the coordinates have the same *sets* of coordinates, but that the coordinates themselves are different.

    :param data_list: list of UngriddedData objects to be merged (stacked vertically)
    :param string var_name: Name of final data variable
    :param string units: Units of final data variable
    :return: A merged UngriddedData object
    """
    import numpy as np
    from cis.data_io.ungridded_data import UngriddedData, Metadata
    from cis.data_io.Coord import Coord

    coords = []
    all_data = np.hstack((d.data_flattened for d in data_list))

    # Create mask (not all datasets might be masked)
    mask = np.hstack((d.data.mask.ravel() if isinstance(
        d.data, np.ma.MaskedArray) else np.zeros(d.shape, np.bool)
                      for d in data_list))
    all_data = np.ma.MaskedArray(all_data, mask)

    for c in data_list[0].coords():
        coord_data = np.hstack((d.coord(c).data_flattened for d in data_list))
        coords.append(Coord(coord_data, c.metadata, axis=c.axis))

    return UngriddedData(
        all_data,
        Metadata(name=var_name or data_list[0].name(),
                 units=units or data_list[0].units), coords)
Exemplo n.º 10
0
    def test_wrapping_of_pres_points_on_hybrid_pressure_coordinates_on_0_360_grid(
            self):
        cube = make_from_cube(
            mock.make_mock_cube(time_dim_length=3,
                                hybrid_pr_len=10,
                                lon_dim_length=36,
                                lon_range=(0., 350.)))

        # Ensure the longitude coord is circular
        cube.coord(standard_name='longitude').circular = True

        sample_points = UngriddedData.from_points_array([
            HyperPoint(lat=0.0,
                       lon=355.0,
                       pres=1482280045.0,
                       t=dt.datetime(1984, 8, 28, 0, 0, 0)),
            HyperPoint(lat=5.0,
                       lon=2.5,
                       pres=1879350048.0,
                       t=dt.datetime(1984, 8, 28, 0, 0, 0))
        ])
        col = GriddedUngriddedCollocator(extrapolate=False)
        new_data = col.collocate(sample_points, cube, None, 'lin')[0]
        eq_(new_data.data[0], 2701.0011131725005)
        eq_(new_data.data[1], 3266.1930161260775)
Exemplo n.º 11
0
    def test_collocation_of_pres_points_on_hybrid_pressure_coordinates(self):
        cube = make_from_cube(
            mock.make_mock_cube(time_dim_length=3, hybrid_pr_len=10))

        sample_points = UngriddedData.from_points_array([
            HyperPoint(lat=0.0,
                       lon=0.0,
                       pres=111100040.5,
                       t=dt.datetime(1984, 8, 28, 0, 0, 0)),
            HyperPoint(lat=0.0,
                       lon=0.0,
                       pres=113625040.5,
                       t=dt.datetime(1984, 8, 28, 12, 0, 0)),
            HyperPoint(lat=5.0,
                       lon=2.5,
                       pres=177125044.5,
                       t=dt.datetime(1984, 8, 28, 0, 0, 0)),
            HyperPoint(lat=-4.0,
                       lon=-4.0,
                       pres=166600039.0,
                       t=dt.datetime(1984, 8, 27))
        ])
        col = GriddedUngriddedCollocator()
        new_data = col.collocate(sample_points, cube, None, 'lin')[0]
        # Exactly on the lat, lon, time points, interpolated over pressure
        assert_almost_equal(new_data.data[0], 221.5, decimal=5)
        # Exactly on the lat, lon, points, interpolated over time and pressure
        assert_almost_equal(new_data.data[1], 226.5, decimal=7)
        # Exactly on the lat, time points, interpolated over longitude and pressure
        assert_almost_equal(new_data.data[2], 330.5, decimal=7)
        # Outside of the pressure bounds - extrapolation off
        assert np.ma.is_masked(new_data.data[3])
Exemplo n.º 12
0
    def test_collocation_of_alt_pres_points_on_hybrid_altitude_coordinates(
            self):
        cube = make_from_cube(
            mock.make_mock_cube(time_dim_length=3, hybrid_ht_len=10))

        sample_points = UngriddedData.from_points_array([
            HyperPoint(lat=0.0,
                       lon=0.0,
                       alt=5550.0,
                       pres=10000.0,
                       t=dt.datetime(1984, 8, 28)),
            HyperPoint(lat=4.0,
                       lon=4.0,
                       alt=6000.0,
                       pres=1000.0,
                       t=dt.datetime(1984, 8, 28)),
            HyperPoint(lat=-4.0,
                       lon=-4.0,
                       alt=6500.0,
                       pres=100.0,
                       t=dt.datetime(1984, 8, 27))
        ])

        col = GriddedUngriddedCollocator(fill_value=np.NAN)
        new_data = col.collocate(sample_points, cube, None, 'lin')[0]
        assert_almost_equal(new_data.data[0], 222.4814815, decimal=7)
        assert_almost_equal(new_data.data[1], 321.0467626, decimal=7)
        # Test that points outside the cell are returned as masked, rather than extrapolated by default
        assert np.ma.is_masked(new_data.data[2])
Exemplo n.º 13
0
    def test_missing_data_for_missing_sample(self):
        data = make_from_cube(mock.make_mock_cube())
        data.name = lambda: 'Name'
        data.var_name = 'var_name'
        data._standard_name = 'y_wind'
        sample = UngriddedData.from_points_array([
            HyperPoint(lat=1.0,
                       lon=1.0,
                       alt=12.0,
                       t=dt.datetime(1984, 8, 29, 8, 34)),
            HyperPoint(lat=3.0,
                       lon=3.0,
                       alt=7.0,
                       t=dt.datetime(1984, 8, 29, 8, 34)),
            HyperPoint(lat=-1.0,
                       lon=-1.0,
                       alt=5.0,
                       t=dt.datetime(1984, 8, 29, 8, 34))
        ])
        constraint = None

        sample_mask = [False, True, False]
        sample.data = np.ma.array([0, 0, 0], mask=sample_mask)

        col = GriddedUngriddedCollocator(missing_data_for_missing_sample=True)
        output = col.collocate(sample, data, constraint, 'nn')

        assert len(output) == 1
        assert isinstance(output, UngriddedDataList)
        assert np.array_equal(output[0].data.mask, sample_mask)
Exemplo n.º 14
0
    def test_wrapping_of_alt_points_on_hybrid_height_coordinates_on_0_360_grid(
            self):
        cube = make_from_cube(
            mock.make_mock_cube(time_dim_length=3,
                                hybrid_ht_len=10,
                                lon_dim_length=36,
                                lon_range=(0., 350.)))

        # Shift the cube around so that the dim which isn't hybrid (time) is at the front. This breaks the fix we used
        #  for air pressure...
        cube.transpose([2, 0, 1, 3])
        # Ensure the longitude coord is circular
        cube.coord(standard_name='longitude').circular = True

        sample_points = UngriddedData.from_points_array([
            HyperPoint(lat=4.0,
                       lon=355.0,
                       alt=11438.0,
                       t=dt.datetime(1984, 8, 28)),
            HyperPoint(lat=0.0,
                       lon=2.0,
                       alt=10082.0,
                       t=dt.datetime(1984, 8, 28))
        ])
        col = GriddedUngriddedCollocator(extrapolate=False)
        new_data = col.collocate(sample_points, cube, None, 'lin')[0]
        eq_(new_data.data[0], 3563.0)
        eq_(new_data.data[1], 2185.0)
Exemplo n.º 15
0
    def test_collocation_of_alt_points_on_hybrid_pressure_and_altitude_coordinates(
            self):
        """
            Kernel should use the auxilliary altitude dimension when altitude is present in the coordinates
        """
        cube = make_from_cube(
            mock.make_mock_cube(time_dim_length=3,
                                hybrid_pr_len=10,
                                geopotential_height=True))

        sample_points = UngriddedData.from_points_array(
            # This point actually lies outside the lower bounds for altitude at this point in space
            [
                HyperPoint(lat=1.0,
                           lon=1.0,
                           alt=10,
                           t=dt.datetime(1984, 8, 28, 8, 34)),
                # This point lies in the middle of the altitude bounds at this point
                HyperPoint(lat=4.0,
                           lon=4.0,
                           alt=354,
                           t=dt.datetime(1984, 8, 28, 8, 34)),
                # This point lies outside the upper bounds for altitude at this point
                HyperPoint(lat=-4.0,
                           lon=-4.0,
                           alt=1000,
                           t=dt.datetime(1984, 8, 27, 2, 18, 52))
            ])
        col = GriddedUngriddedCollocator(extrapolate=True)
        new_data = col.collocate(sample_points, cube, None, 'nn')[0]
        eq_(new_data.data[0], float(cube[2, 1, 1, 0].data))
        eq_(new_data.data[1], float(cube[3, 2, 1, 4].data))
        eq_(new_data.data[2], float(cube[1, 0, 0, 9].data))
Exemplo n.º 16
0
    def test_collocation_of_pres_points_on_hybrid_pressure_coordinates_and_altitude_coordinates(
            self):
        """
            When only pressure coordinate is present this should be used for the collocation
        """
        cube = make_from_cube(
            mock.make_mock_cube(time_dim_length=3, hybrid_pr_len=10))

        sample_points = UngriddedData.from_points_array(
            # This point actually lies outside the lower bounds for altitude at this point in space
            [
                HyperPoint(lat=1.0,
                           lon=1.0,
                           pres=1100000.0,
                           t=dt.datetime(1984, 8, 28, 8, 34)),
                # This point lies in the middle of the altitude bounds at this point
                HyperPoint(lat=4.0,
                           lon=4.0,
                           pres=184600000.0,
                           t=dt.datetime(1984, 8, 28, 8, 34)),
                # This point lies outside the upper bounds for altitude at this point
                HyperPoint(lat=-4.0,
                           lon=-4.0,
                           pres=63100049.0,
                           t=dt.datetime(1984, 8, 27, 2, 18, 52))
            ])
        col = GriddedUngriddedCollocator(extrapolate=True)
        new_data = col.collocate(sample_points, cube, None, 'nn')[0]
        eq_(new_data.data[0], float(cube[2, 1, 1, 0].data))
        eq_(new_data.data[1], float(cube[3, 2, 1, 4].data))
        eq_(new_data.data[2], float(cube[1, 0, 0, 9].data))
Exemplo n.º 17
0
    def test_gridded_ungridded_lin(self):
        data = make_from_cube(mock.make_mock_cube())
        data.name = lambda: 'Name'
        data.var_name = 'var_name'
        data._standard_name = 'y_wind'
        sample = UngriddedData.from_points_array([
            HyperPoint(lat=1.0,
                       lon=1.0,
                       alt=12.0,
                       t=dt.datetime(1984, 8, 29, 8, 34)),
            HyperPoint(lat=3.0,
                       lon=3.0,
                       alt=7.0,
                       t=dt.datetime(1984, 8, 29, 8, 34)),
            HyperPoint(lat=-1.0,
                       lon=-1.0,
                       alt=5.0,
                       t=dt.datetime(1984, 8, 29, 8, 34))
        ])
        constraint = None

        col = GriddedUngriddedCollocator()
        output = col.collocate(sample, data, constraint, 'lin')

        expected_result = np.array([8.8, 10.4, 7.2])
        assert len(output) == 1
        assert isinstance(output, UngriddedDataList)
        assert np.allclose(output[0].data, expected_result)
Exemplo n.º 18
0
    def test_can_add_history(self):
        x_points = np.arange(-10, 11, 5)
        y_points = np.arange(-5, 6, 5)
        y, x = np.meshgrid(y_points, x_points)

        x = Coord(x, Metadata(standard_name='latitude', units='degrees'))
        y = Coord(y, Metadata(standard_name='longitude', units='degrees'))
        data = np.reshape(np.arange(15) + 1.0, (5, 3))

        coords = CoordList([x, y])
        ug = UngriddedData(data, Metadata(standard_name='rain', long_name="TOTAL RAINFALL RATE: LS+CONV KG/M2/S",
                                          units="kg m-2 s-1", missing_value=-999), coords)

        new_history = 'This is a new history entry.'
        ug.add_history(new_history)
        assert(ug.metadata.history.find(new_history) >= 0)
Exemplo n.º 19
0
    def test_extrapolation_of_pres_points_on_hybrid_pressure_coordinates_multi_var(
            self):
        cube_list = [
            make_from_cube(
                mock.make_mock_cube(time_dim_length=3, hybrid_pr_len=10))
        ]
        cube_list.append(
            make_from_cube(
                mock.make_mock_cube(time_dim_length=3,
                                    hybrid_pr_len=10,
                                    data_offset=100)))

        sample_points = UngriddedData.from_points_array(
            # Point interpolated in the horizontal and then extrapolated past the top vertical layer (by one layer)
            [
                HyperPoint(lat=-4.0,
                           lon=-4.0,
                           pres=68400050.0,
                           t=dt.datetime(1984, 8, 27))
            ])

        col = GriddedUngriddedCollocator(extrapolate=True)
        new_data = col.collocate(sample_points, cube_list, None, 'lin')
        assert_almost_equal(new_data[0].data[0], 125.0, decimal=7)
        assert_almost_equal(new_data[1].data[0], 225.0, decimal=7)
Exemplo n.º 20
0
    def test_negative_lon_points_on_hybrid_altitude_coordinates_with_0_360_grid(
            self):
        cube = make_from_cube(
            mock.make_mock_cube(time_dim_length=3,
                                hybrid_ht_len=10,
                                lon_dim_length=36,
                                lon_range=(0., 350.)))

        sample_points = UngriddedData.from_points_array([
            HyperPoint(lat=1.0,
                       lon=111.0,
                       alt=5000.0,
                       t=dt.datetime(1984, 8, 28, 8, 34)),
            HyperPoint(lat=4.0,
                       lon=141.0,
                       alt=12000.0,
                       t=dt.datetime(1984, 8, 28, 8, 34)),
            HyperPoint(lat=-4.0,
                       lon=-14.0,
                       alt=10000.0,
                       t=dt.datetime(1984, 8, 27, 2, 18, 52))
        ])
        col = GriddedUngriddedCollocator(extrapolate=True)
        new_data = col.collocate(sample_points, cube, None, 'nn')[0]
        eq_(new_data.data[0], 2501.0)  # float(cube[2,11,1,0].data))
        eq_(new_data.data[1], 3675.0)  # float(cube[3,14,1,4].data))
        eq_(new_data.data[2], 2139.0)  # float(cube[1,35,0,8].data))
Exemplo n.º 21
0
    def test_coordinates_exactly_between_points_in_col_ungridded_to_ungridded_in_2d(
            self):
        """
            This works out the edge case where the points are exactly in the middle or two or more datapoints.
                The nn_horizontal algorithm will start with the first point as the nearest and iterates through the
                points finding any points which are closer than the current closest. If two distances were exactly
                the same  you would expect the first point to be chosen. This doesn't seem to always be the case but is
                probably down to floating points errors in the haversine calculation as these test points are pretty
                close together. This test is only really for documenting the behaviour for equidistant points.
        """
        from cis.collocation.col_implementations import GeneralUngriddedCollocator, nn_horizontal, SepConstraintKdtree

        ug_data = mock.make_regular_2d_ungridded_data()
        sample_points = UngriddedData.from_points_array([
            HyperPoint(2.5, 2.5),
            HyperPoint(-2.5, 2.5),
            HyperPoint(2.5, -2.5),
            HyperPoint(-2.5, -2.5)
        ])
        col = GeneralUngriddedCollocator()
        new_data = col.collocate(sample_points, ug_data, SepConstraintKdtree(),
                                 nn_horizontal())[0]
        eq_(new_data.data[0], 11.0)
        eq_(new_data.data[1], 5.0)
        eq_(new_data.data[2], 10.0)
        eq_(new_data.data[3], 4.0)
Exemplo n.º 22
0
 def test_already_collocated_in_col_gridded_to_ungridded_in_2d(self):
     cube = make_from_cube(mock.make_square_5x3_2d_cube())
     # This point already exists on the cube with value 5 - which shouldn't be a problem
     sample_points = UngriddedData.from_points_array([HyperPoint(0.0, 0.0)])
     col = GriddedUngriddedCollocator()
     new_data = col.collocate(sample_points, cube, None, 'nn')[0]
     eq_(new_data.data[0], 8.0)
Exemplo n.º 23
0
    def test_hybrid_alt_Coord_order_doesnt_matter(self):
        from cis.test.util.mock import make_mock_cube
        from cis.data_io.ungridded_data import UngriddedData
        from cis.data_io.hyperpoint import HyperPoint
        import datetime as dt
        cube = make_mock_cube(time_dim_length=3, hybrid_ht_len=10)

        cube.transpose()

        sample_points = UngriddedData.from_points_array(
            # This point actually lies outside the lower bounds for altitude at this point in space
            [
                HyperPoint(lat=1.0,
                           lon=1.0,
                           alt=5000.0,
                           t=dt.datetime(1984, 8, 28, 8, 34)),
                # This point lies in the middle of the altitude bounds at this point
                HyperPoint(lat=4.0,
                           lon=4.0,
                           alt=6000.0,
                           t=dt.datetime(1984, 8, 28, 8, 34)),
                # This point lies outside the upper bounds for altitude at this point
                HyperPoint(lat=-4.0,
                           lon=-4.0,
                           alt=6500.0,
                           t=dt.datetime(1984, 8, 27, 2, 18, 52))
            ])

        interpolator = GriddedUngriddedInterpolator(cube, sample_points, 'nn')
        values = interpolator(cube, fill_value=None)
        wanted = np.asarray([221.0, 345.0, 100.0])
        assert_array_almost_equal(values, wanted)
Exemplo n.º 24
0
 def test_already_collocated_in_col_ungridded_to_ungridded_in_2d(self):
     ug_data = mock.make_regular_2d_ungridded_data()
     # This point already exists on the cube with value 5 - which shouldn't be a problem
     sample_points = UngriddedData.from_points_array([HyperPoint(0.0, 0.0)])
     col = GeneralUngriddedCollocator(fill_value=-999)
     new_data = col.collocate(sample_points, ug_data, SepConstraintKdtree(), nn_horizontal_only())[0]
     eq_(new_data.data[0], 8.0)
Exemplo n.º 25
0
    def test_hybrid_Coord(self):
        from cis.test.util.mock import make_mock_cube
        from cis.data_io.ungridded_data import UngriddedData
        from cis.data_io.hyperpoint import HyperPoint
        import datetime as dt
        cube = make_mock_cube(time_dim_length=3, hybrid_pr_len=10)

        sample_points = UngriddedData.from_points_array([
            HyperPoint(lat=0.0,
                       lon=0.0,
                       pres=111100040.5,
                       alt=5000,
                       t=dt.datetime(1984, 8, 28, 0, 0, 0)),
            HyperPoint(lat=0.0,
                       lon=0.0,
                       pres=113625040.5,
                       alt=4000,
                       t=dt.datetime(1984, 8, 28, 12, 0, 0)),
            HyperPoint(lat=5.0,
                       lon=2.5,
                       pres=177125044.5,
                       alt=3000,
                       t=dt.datetime(1984, 8, 28, 0, 0, 0)),
            HyperPoint(lat=-4.0,
                       lon=-4.0,
                       pres=166600039.0,
                       alt=3500,
                       t=dt.datetime(1984, 8, 27))
        ])

        interpolator = GriddedUngriddedInterpolator(cube, sample_points, 'lin')
        values = interpolator(cube)
        wanted = np.ma.masked_invalid(
            np.ma.array([221.5, 226.5, 330.5, np.nan]))
        assert_array_almost_equal(values, wanted)
Exemplo n.º 26
0
    def test_GIVEN_multiple_ungridded_data_with_missing_data_WHEN_call_as_data_frame_THEN_returns_valid_data_frame(
            self):
        d = np.reshape(np.arange(15) + 10.0, (5, 3))

        data = np.ma.masked_array(d, np.zeros(d.shape, dtype=bool))
        data.mask[1, 2] = True

        ug3 = UngriddedData(
            data,
            Metadata(name='hail',
                     long_name="TOTAL HAIL RATE: LS+CONV KG/M2/S",
                     units="kg m-2 s-1",
                     missing_value=-999), self.coords)

        self.ungridded_data_list.append(ug3)

        df = self.ungridded_data_list.as_data_frame()

        assert_that(df['rainfall_flux'][5] == 6)
        assert_almost_equal(df['snowfall_flux'][5], 0.6)
        assert_that(df['latitude'][13] == 10)
        assert_that(df['longitude'][0] == -5)
        assert_almost_equal(df['TOTAL HAIL RATE: LS+CONV KG/M2/S'][1], 11.0)
        assert_that(
            np.isnan(
                df['TOTAL HAIL RATE: LS+CONV KG/M2/S'][np.ravel_multi_index(
                    [1, 2], (5, 3))]))

        self.ungridded_data_list.pop()
Exemplo n.º 27
0
 def test_already_collocated_in_col_gridded_to_ungridded_in_2d(self):
     cube = make_from_cube(mock.make_square_5x3_2d_cube())
     # This point already exists on the cube with value 5 - which shouldn't be a problem
     sample_points = UngriddedData.from_points_array([HyperPoint(0.0, 0.0)])
     col = GriddedUngriddedCollocator()
     new_data = col.collocate(sample_points, cube, None, 'nn')[0]
     eq_(new_data.data[0], 8.0)
Exemplo n.º 28
0
    def test_collocation_of_alt_points_on_hybrid_altitude_and_pressure_coordinates(
            self):
        cube = make_from_cube(
            mock.make_mock_cube(time_dim_length=3,
                                hybrid_pr_len=10,
                                geopotential_height=True))

        sample_points = UngriddedData.from_points_array(
            # Test point with both pressure and altitude should interpolate over the altitude only (since that is also
            #  present in the data cube)
            [
                HyperPoint(lat=0.0,
                           lon=0.0,
                           alt=234.5,
                           t=dt.datetime(1984, 8, 28, 0, 0, 0)),
                HyperPoint(lat=5.0,
                           lon=5.0,
                           alt=355.5,
                           t=dt.datetime(1984, 8, 28, 0, 0))
            ])

        col = GriddedUngriddedCollocator(fill_value=np.NAN)
        new_data = col.collocate(sample_points, cube, None, 'lin')[0]
        assert_almost_equal(new_data.data[0], 225.5, decimal=7)
        assert_almost_equal(new_data.data[1], 346.5, decimal=7)
Exemplo n.º 29
0
    def test_basic_col_in_4d(self):
        from cis.collocation.col_implementations import GeneralUngriddedCollocator, nn_altitude, SepConstraintKdtree
        import datetime as dt

        ug_data = mock.make_regular_4d_ungridded_data()
        sample_points = HyperPointList()
        sample_points.append(
            HyperPoint(lat=1.0,
                       lon=1.0,
                       alt=12.0,
                       t=dt.datetime(1984, 8, 29, 8, 34)))
        sample_points.append(
            HyperPoint(lat=4.0,
                       lon=4.0,
                       alt=34.0,
                       t=dt.datetime(1984, 9, 2, 1, 23)))
        sample_points.append(
            HyperPoint(lat=-4.0,
                       lon=-4.0,
                       alt=89.0,
                       t=dt.datetime(1984, 9, 4, 15, 54)))
        sample_points = UngriddedData.from_points_array(sample_points)
        col = GeneralUngriddedCollocator()
        new_data = col.collocate(sample_points, ug_data, SepConstraintKdtree(),
                                 nn_altitude())[0]
        eq_(new_data.data[0], 6.0)
        eq_(new_data.data[1], 16.0)
        eq_(new_data.data[2], 46.0)
Exemplo n.º 30
0
    def test_coordinates_outside_grid_in_col_ungridded_to_ungridded_in_2d(
            self):
        from cis.collocation.col_implementations import GeneralUngriddedCollocator, nn_pressure, SepConstraintKdtree
        import datetime as dt

        ug_data = mock.make_regular_4d_ungridded_data()
        sample_points = HyperPointList()
        sample_points.append(
            HyperPoint(lat=0.0,
                       lon=0.0,
                       pres=0.1,
                       t=dt.datetime(1984, 8, 29, 8, 34)))
        sample_points.append(
            HyperPoint(lat=0.0,
                       lon=0.0,
                       pres=91.0,
                       t=dt.datetime(1984, 9, 2, 1, 23)))
        sample_points.append(
            HyperPoint(lat=0.0,
                       lon=0.0,
                       pres=890.0,
                       t=dt.datetime(1984, 9, 4, 15, 54)))
        sample_points = UngriddedData.from_points_array(sample_points)
        col = GeneralUngriddedCollocator()
        new_data = col.collocate(sample_points, ug_data, SepConstraintKdtree(),
                                 nn_pressure())[0]
        eq_(new_data.data[0], 1.0)
        eq_(new_data.data[1], 46.0)
        eq_(new_data.data[2], 46.0)
Exemplo n.º 31
0
    def create_coords(self, filenames, variable=None):
        """
        Reads the coordinates and data if required from the files
        :param filenames: List of filenames to read coordinates from
        :param variable: load a variable for the data
        :return: Coordinates
        """
        from cis.data_io.netcdf import read_many_files_individually
        from cis.data_io.Coord import Coord, CoordList
        from cis.exceptions import InvalidVariableError

        variables = [("longitude", "x"), ("latitude", "y"), ("altitude", "z"), ("time", "t"), ("air_pressure", "p")]

        dim_coords = CoordList()
        for v in variables:
            try:
                var_data = read_many_files_individually(filenames, v[0])[v[0]]
                dim_coords.append(Coord(var_data, get_metadata(var_data[0]), axis=v[1]))
            except InvalidVariableError:
                pass

        if variable is None:
            return UngriddedCoordinates(dim_coords)
        else:
            all_coords = self._add_aux_coordinate(dim_coords, filenames[0], 'DP_MID',
                                                  dim_coords.get_coord(standard_name='time').data.size)

            usr_var_data = read_many_files_individually(filenames, variable)[variable]
            return UngriddedData(usr_var_data, get_metadata(usr_var_data[0]), all_coords)
    def test_list_gridded_ungridded_box_moments(self):
        data1 = make_from_cube(mock.make_mock_cube())
        data1.name = lambda: 'Name1'
        data1.var_name = 'var_name1'
        data1._standard_name = 'y_wind'
        data2 = make_from_cube(mock.make_mock_cube(data_offset=3))
        data2.name = lambda: 'Name1'
        data2.var_name = 'var_name2'
        data2._standard_name = 'x_wind'
        data_list = GriddedDataList([data1, data2])
        sample = UngriddedData.from_points_array(
            [HyperPoint(lat=1.0, lon=1.0, alt=12.0, t=dt.datetime(1984, 8, 29, 8, 34)),
             HyperPoint(lat=3.0, lon=3.0, alt=7.0, t=dt.datetime(1984, 8, 29, 8, 34)),
             HyperPoint(lat=-1.0, lon=-1.0, alt=5.0, t=dt.datetime(1984, 8, 29, 8, 34))])
        constraint = SepConstraintKdtree('500km')
        kernel = moments()

        col = GeneralUngriddedCollocator()
        output = col.collocate(sample, data_list, constraint, kernel)

        expected_result = np.array([28.0/3, 10.0, 20.0/3])
        expected_stddev = np.array([1.52752523, 1.82574186, 1.52752523])
        expected_n = np.array([3, 4, 3])
        assert len(output) == 6
        assert isinstance(output, UngriddedDataList)
        assert np.allclose(output[0].data, expected_result)
        assert np.allclose(output[1].data, expected_stddev)
        assert np.allclose(output[2].data, expected_n)
        assert np.allclose(output[3].data, expected_result + 3)
        assert np.allclose(output[4].data, expected_stddev)
        assert np.allclose(output[5].data, expected_n)
Exemplo n.º 33
0
    def test_collocation_of_pres_alt_points_on_hybrid_pressure_coordinates_multi_var(self):
        cube_list = [make_from_cube(mock.make_mock_cube(time_dim_length=3, hybrid_pr_len=10))]
        cube_list.append(make_from_cube(mock.make_mock_cube(time_dim_length=3,
                                                            hybrid_pr_len=10,
                                                            data_offset=100)))

        sample_points = UngriddedData.from_points_array(
            [HyperPoint(lat=0.0, lon=0.0, pres=111100040.5, alt=5000, t=dt.datetime(1984, 8, 28, 0, 0, 0)),
             HyperPoint(lat=0.0, lon=0.0, pres=113625040.5, alt=4000, t=dt.datetime(1984, 8, 28, 12, 0, 0)),
             HyperPoint(lat=5.0, lon=2.5, pres=177125044.5, alt=3000, t=dt.datetime(1984, 8, 28, 0, 0, 0)),
             HyperPoint(lat=-4.0, lon=-4.0, pres=166600039.0, alt=3500, t=dt.datetime(1984, 8, 27))])
        col = GriddedUngriddedCollocator()
        outlist = col.collocate(sample_points, cube_list, None, 'lin')
        # First data set:
        new_data = outlist[0]
        # Exactly on the lat, lon, time points, interpolated over pressure
        assert_almost_equal(new_data.data[0], 221.5, decimal=5)
        # Exactly on the lat, lon, points, interpolated over time and pressure
        assert_almost_equal(new_data.data[1], 226.5, decimal=7)
        # Exactly on the lat, time points, interpolated over longitude and pressure
        assert_almost_equal(new_data.data[2], 330.5, decimal=7)
        # Outside of the pressure bounds - extrapolation off
        assert np.ma.is_masked(new_data.data[3])

        # Second dataset:
        new_data = outlist[1]
        # Exactly on the lat, lon, time points, interpolated over pressure
        assert_almost_equal(new_data.data[0], 321.5, decimal=5)
        # Exactly on the lat, lon, points, interpolated over time and pressure
        assert_almost_equal(new_data.data[1], 326.5, decimal=7)
        # Exactly on the lat, time points, interpolated over longitude and pressure
        assert_almost_equal(new_data.data[2], 430.5, decimal=7)
        # Outside of the pressure bounds - extrapolation off
        assert np.ma.is_masked(new_data.data[3])
Exemplo n.º 34
0
    def test_missing_data_for_missing_sample_with_no_extrapolation(self):
        cube = make_from_cube(
            mock.make_mock_cube(time_dim_length=3, hybrid_ht_len=10))

        sample_points = UngriddedData.from_points_array([
            HyperPoint(lat=0.0,
                       lon=0.0,
                       alt=5550.0,
                       t=dt.datetime(1984, 8, 28)),
            HyperPoint(lat=4.0,
                       lon=4.0,
                       alt=6000.0,
                       t=dt.datetime(1984, 8, 28)),
            HyperPoint(lat=-4.0,
                       lon=-4.0,
                       alt=6500.0,
                       t=dt.datetime(1984, 8, 27))
        ])

        sample_mask = [False, True, False]
        sample_points.data = np.ma.array([0, 0, 0], mask=sample_mask)

        col = GriddedUngriddedCollocator(fill_value=np.NAN,
                                         missing_data_for_missing_sample=True)
        new_data = col.collocate(sample_points, cube, None, 'lin')[0]
        assert_almost_equal(new_data.data[0], 222.4814815, decimal=7)
        # This point should be masked because of the sampling
        assert np.ma.is_masked(new_data.data[1])
        # And this one because of the extrapolation
        assert np.ma.is_masked(new_data.data[2])
Exemplo n.º 35
0
    def test_GIVEN_missing_coord_values_WHEN_coords_data_THEN_missing_values_removed(self):
        x_points = np.arange(-10, 11, 5)
        y_points = np.arange(-5, 6, 5)
        y, x = np.meshgrid(y_points, x_points)
        y = np.ma.masked_array(y, np.zeros(y.shape, dtype=bool))
        y.mask[1, 2] = True

        x = Coord(x, Metadata(standard_name='latitude', units='degrees'))
        y = Coord(y, Metadata(standard_name='longitude', units='degrees'))
        coords = CoordList([x, y])

        data = np.reshape(np.arange(15) + 1.0, (5, 3))

        ug = UngriddedData(None, Metadata(), coords, lambda x: data)
        coords = ug.coords()
        for coord in coords:
            assert_that(len(coord.data), is_(14))
Exemplo n.º 36
0
Arquivo: CCI.py Projeto: bjlittle/cis
    def create_data_object(self, filenames, variable):
        from cis.data_io.netcdf import get_metadata, read_many_files_individually

        coords = self._create_coord_list(filenames)
        var = read_many_files_individually(filenames, [variable])
        metadata = get_metadata(var[variable][0])

        return UngriddedData(var[variable], metadata, coords)
Exemplo n.º 37
0
 def test_already_collocated_in_col_ungridded_to_ungridded_in_2d(self):
     ug_data = mock.make_regular_2d_ungridded_data()
     # This point already exists on the cube with value 5 - which shouldn't be a problem
     sample_points = UngriddedData.from_points_array([HyperPoint(0.0, 0.0)])
     col = GeneralUngriddedCollocator(fill_value=-999)
     new_data = col.collocate(sample_points, ug_data, SepConstraintKdtree(),
                              nn_horizontal_only())[0]
     eq_(new_data.data[0], 8.0)
Exemplo n.º 38
0
    def create_coords(self, filenames, usr_variable=None):
        from cis.data_io.netcdf import read_many_files_individually, get_metadata, get_netcdf_file_variables
        from cis.data_io.Coord import Coord, CoordList
        from cis.data_io.ungridded_data import UngriddedCoordinates, UngriddedData
        from cis.exceptions import InvalidVariableError

        # We have to read it once first to find out which variables are in there. We assume the set of coordinates in
        # all the files are the same
        file_variables = get_netcdf_file_variables(filenames[0])

        def get_axis_std_name(var):
            axis=None
            lvar = var.lower()
            if lvar == 'longitude':
                axis = 'x', 'longitude'
            if lvar == 'latitude':
                axis = 'y', 'latitude'
            if lvar == 'G_ALT' or lvar == 'altitude' or lvar == 'pressure_altitude':
                axis = 'z', 'altitude'
            if lvar == 'time':
                axis = 't', 'time'
            if lvar == 'p' or lvar == 'pressure' or lvar == 'static_pressure':
                axis = 'p', 'air_pressure'
            return axis

        all_coord_variables = [(v, get_axis_std_name(v)) for v in file_variables if get_axis_std_name(v) is not None]
        # Get rid of any duplicates
        coord_variables = []
        for v in all_coord_variables:
            if v is None or v[1][1] not in [x[1][1] for x in coord_variables]:
                coord_variables.append(v)

        all_variables = coord_variables.copy()
        if usr_variable is not None:
            all_variables.append((usr_variable, ''))

        logging.info("Listing coordinates: " + str(all_variables))

        coords = CoordList()
        var_data = read_many_files_individually(filenames, [v[0] for v in all_variables])
        for name, axis_std_name in coord_variables:
            try:
                meta = get_metadata(var_data[name][0])
                if meta.standard_name is None:
                    meta.standard_name = axis_std_name[1]
                coords.append(Coord(var_data[name], meta, axis=axis_std_name[0]))
            except InvalidVariableError:
                pass

        # Note - We don't need to convert this time coord as it should have been written in our
        #  'standard' time unit

        if usr_variable is None:
            res = UngriddedCoordinates(coords)
        else:
            res = UngriddedData(var_data[usr_variable], get_metadata(var_data[usr_variable][0]), coords)

        return res
Exemplo n.º 39
0
    def test_already_collocated_in_col_ungridded_to_ungridded_in_2d(self):
        from cis.collocation.col_implementations import GeneralUngriddedCollocator, nn_horizontal, DummyConstraint

        ug_data = mock.make_regular_2d_ungridded_data()
        # This point already exists on the cube with value 5 - which shouldn't be a problem
        sample_points = UngriddedData.from_points_array([HyperPoint(0.0, 0.0)])
        col = GeneralUngriddedCollocator()
        new_data = col.collocate(sample_points, ug_data, DummyConstraint(), nn_horizontal())[0]
        eq_(new_data.data[0], 8.0)
Exemplo n.º 40
0
    def test_already_collocated_in_col_gridded_to_ungridded_in_2d(self):
        from cis.collocation.col_implementations import GeneralUngriddedCollocator, nn_gridded

        cube = gridded_data.make_from_cube(mock.make_square_5x3_2d_cube())
        # This point already exists on the cube with value 5 - which shouldn't be a problem
        sample_points = UngriddedData.from_points_array([HyperPoint(0.0, 0.0)])
        col = GeneralUngriddedCollocator()
        new_data = col.collocate(sample_points, cube, None, nn_gridded())[0]
        eq_(new_data.data[0], 8.0)
Exemplo n.º 41
0
    def test_GIVEN_missing_coord_values_WHEN_coords_data_THEN_missing_values_removed(
            self):
        x_points = np.arange(-10, 11, 5)
        y_points = np.arange(-5, 6, 5)
        y, x = np.meshgrid(y_points, x_points)
        y = np.ma.masked_array(y, np.zeros(y.shape, dtype=bool))
        y.mask[1, 2] = True

        x = Coord(x, Metadata(standard_name='latitude', units='degrees'))
        y = Coord(y, Metadata(standard_name='longitude', units='degrees'))
        coords = CoordList([x, y])

        data = np.reshape(np.arange(15) + 1.0, (5, 3))

        ug = UngriddedData(None, Metadata(), coords, lambda x: data)
        coords = ug.coords()
        for coord in coords:
            assert_that(len(coord.data), is_(14))
Exemplo n.º 42
0
    def test_alt_extrapolation(self):
        cube = make_from_cube(mock.make_mock_cube(time_dim_length=3, hybrid_ht_len=10))

        sample_points = UngriddedData.from_points_array(
            [HyperPoint(lat=-4.0, lon=-4.0, alt=6382.8, t=dt.datetime(1984, 8, 27))])

        col = GriddedUngriddedCollocator(fill_value=np.NAN, extrapolate=True)
        new_data = col.collocate(sample_points, cube, None, 'lin')[0]
        assert_almost_equal(new_data.data[0], 126.0, decimal=7)
Exemplo n.º 43
0
    def test_extrapolation_of_pres_points_on_hybrid_pressure_coordinates(self):
        cube = make_from_cube(mock.make_mock_cube(time_dim_length=3, hybrid_pr_len=10))

        sample_points = UngriddedData.from_points_array(
            # Point interpolated in the horizontal and then extrapolated past the top vertical layer (by one layer)
            [HyperPoint(lat=-4.0, lon=-4.0, pres=68400050.0, t=dt.datetime(1984, 8, 27))])
        col = GriddedUngriddedCollocator(extrapolate=True)
        new_data = col.collocate(sample_points, cube, None, 'lin')[0]
        assert_almost_equal(new_data.data[0], 125.0, decimal=7)
Exemplo n.º 44
0
 def test_basic_col_gridded_to_ungridded_using_li_in_2d(self):
     cube = make_from_cube(mock.make_square_5x3_2d_cube())
     sample_points = UngriddedData.from_points_array(
         [HyperPoint(1.0, 1.0), HyperPoint(4.0, 4.0), HyperPoint(-4.0, -4.0)])
     col = GriddedUngriddedCollocator()
     new_data = col.collocate(sample_points, cube, None, 'lin')[0]
     assert_almost_equal(new_data.data[0], 8.8)
     assert_almost_equal(new_data.data[1], 11.2)
     assert_almost_equal(new_data.data[2], 4.8)
Exemplo n.º 45
0
 def test_basic_col_in_2d(self):
     # lat: -10 to 10 step 5; lon -5 to 5 step 5
     ug_data = mock.make_regular_2d_ungridded_data()
     sample_points = UngriddedData.from_points_array(
         [HyperPoint(lat=1.0, lon=1.0), HyperPoint(lat=4.0, lon=4.0), HyperPoint(lat=-4.0, lon=-4.0)])
     col = GeneralUngriddedCollocator(fill_value=-999)
     new_data = col.collocate(sample_points, ug_data, SepConstraintKdtree(), nn_horizontal_only())[0]
     eq_(new_data.data[0], 8.0)
     eq_(new_data.data[1], 12.0)
     eq_(new_data.data[2], 4.0)
Exemplo n.º 46
0
    def test_basic_col_with_incompatible_points_throws_a_TypeError(self):
        from cis.collocation.col_implementations import GeneralUngriddedCollocator, nn_pressure, SepConstraintKdtree

        ug_data = mock.make_regular_4d_ungridded_data()
        # Make sample points with no time dimension specified
        sample_points = UngriddedData.from_points_array(
            [HyperPoint(1.0, 1.0), HyperPoint(4.0, 4.0), HyperPoint(-4.0, -4.0)])
        col = GeneralUngriddedCollocator()
        with self.assertRaises(AttributeError):
            new_data = col.collocate(sample_points, ug_data, SepConstraintKdtree(), nn_pressure())[0]
Exemplo n.º 47
0
 def test_coordinates_outside_grid_in_col_gridded_to_ungridded_in_2d(self):
     cube = make_from_cube(mock.make_square_5x3_2d_cube())
     sample_points = UngriddedData.from_points_array(
         [HyperPoint(5.5, 5.5), HyperPoint(-5.5, 5.5), HyperPoint(5.5, -5.5), HyperPoint(-5.5, -5.5)])
     col = GriddedUngriddedCollocator(extrapolate=True)
     new_data = col.collocate(sample_points, cube, None, 'nn')[0]
     eq_(new_data.data[0], 12.0)
     eq_(new_data.data[1], 6.0)
     eq_(new_data.data[2], 10.0)
     eq_(new_data.data[3], 4.0)
Exemplo n.º 48
0
    def test_collocation_of_pres_points_on_hybrid_altitude_coordinates(self):
        cube = make_from_cube(mock.make_mock_cube(time_dim_length=3, hybrid_ht_len=10))

        sample_points = UngriddedData.from_points_array(
            [HyperPoint(lat=-4.0, lon=-4.0, pres=100.0, t=dt.datetime(1984, 8, 27))])

        col = GriddedUngriddedCollocator(fill_value=np.NAN)

        # Since there is no corresponding pressure field in the source data a ValueError should be raised
        assert_raises(ValueError, col.collocate, sample_points, cube, None, 'lin')
Exemplo n.º 49
0
    def test_already_collocated_in_col_ungridded_to_ungridded_in_2d(self):
        from cis.collocation.col_implementations import GeneralUngriddedCollocator, nn_pressure, SepConstraintKdtree
        import datetime as dt

        ug_data = mock.make_regular_4d_ungridded_data()
        sample_points = UngriddedData.from_points_array(
            [HyperPoint(lat=0.0, lon=0.0, pres=80.0, t=dt.datetime(1984, 9, 4, 15, 54))])
        col = GeneralUngriddedCollocator()
        new_data = col.collocate(sample_points, ug_data, SepConstraintKdtree(), nn_pressure())[0]
        eq_(new_data.data[0], 41.0)
Exemplo n.º 50
0
    def test_collocation_of_alt_points_on_hybrid_altitude_coordinates_on_0_360_grid(self):
        cube = make_from_cube(mock.make_mock_cube(time_dim_length=3, hybrid_ht_len=10, lon_dim_length=36,
                                                  lon_range=(0., 350.)))

        sample_points = UngriddedData.from_points_array(
            [HyperPoint(lat=1.0, lon=111.0, alt=5000.0, t=dt.datetime(1984, 8, 28, 8, 34)),
             HyperPoint(lat=4.0, lon=141.0, alt=12000.0, t=dt.datetime(1984, 8, 28, 8, 34))])
        col = GriddedUngriddedCollocator(extrapolate=True)
        new_data = col.collocate(sample_points, cube, None, 'nn')[0]
        eq_(new_data.data[0], 2501.0)  # float(cube[2,11,1,0].data))
        eq_(new_data.data[1], 3675.0)  # float(cube[3,14,1,4].data))
Exemplo n.º 51
0
 def test_coordinates_outside_grid_in_col_ungridded_to_ungridded_in_2d(self):
     ug_data = mock.make_regular_2d_ungridded_data()
     sample_points = UngriddedData.from_points_array(
         [HyperPoint(5.5, 5.5), HyperPoint(-5.5, 5.5), HyperPoint(5.5, -5.5),
          HyperPoint(-5.5, -5.5)])
     col = GeneralUngriddedCollocator(fill_value=-999)
     new_data = col.collocate(sample_points, ug_data, SepConstraintKdtree(), nn_horizontal_only())[0]
     eq_(new_data.data[0], 12.0)
     eq_(new_data.data[1], 6.0)
     eq_(new_data.data[2], 10.0)
     eq_(new_data.data[3], 4.0)
Exemplo n.º 52
0
    def test_basic_col_gridded_to_ungridded_using_li_in_2d(self):
        from cis.collocation.col_implementations import GeneralUngriddedCollocator, li

        cube = gridded_data.make_from_cube(mock.make_square_5x3_2d_cube())
        sample_points = UngriddedData.from_points_array(
            [HyperPoint(1.0, 1.0), HyperPoint(4.0, 4.0), HyperPoint(-4.0, -4.0)])
        col = GeneralUngriddedCollocator()
        new_data = col.collocate(sample_points, cube, None, li())[0]
        assert_almost_equal(new_data.data[0], 8.8)
        assert_almost_equal(new_data.data[1], 11.2)
        assert_almost_equal(new_data.data[2], 4.8)
Exemplo n.º 53
0
    def test_basic_col_in_2d(self):
        from cis.collocation.col_implementations import GeneralUngriddedCollocator, nn_horizontal, SepConstraintKdtree

        ug_data = mock.make_regular_2d_ungridded_data()
        sample_points = UngriddedData.from_points_array(
            [HyperPoint(lat=1.0, lon=1.0), HyperPoint(lat=4.0, lon=4.0), HyperPoint(lat=-4.0, lon=-4.0)])
        col = GeneralUngriddedCollocator()
        new_data = col.collocate(sample_points, ug_data, SepConstraintKdtree(), nn_horizontal())[0]
        eq_(new_data.data[0], 8.0)
        eq_(new_data.data[1], 12.0)
        eq_(new_data.data[2], 4.0)
Exemplo n.º 54
0
    def test_basic_col_gridded_to_ungridded_in_2d_with_time(self):
        cube = make_from_cube(mock.make_square_5x3_2d_cube_with_time())

        sample_points = [HyperPoint(lat=1.0, lon=1.0, t=dt.datetime(1984, 8, 28, 8, 34)),
                         HyperPoint(lat=4.0, lon=4.0, t=dt.datetime(1984, 8, 31, 1, 23)),
                         HyperPoint(lat=-4.0, lon=-4.0, t=dt.datetime(1984, 9, 2, 15, 54))]
        sample_points = UngriddedData.from_points_array(sample_points)
        col = GriddedUngriddedCollocator(extrapolate=True)
        new_data = col.collocate(sample_points, cube, None, 'nn')[0]
        eq_(new_data.data[0], 51.0)
        eq_(new_data.data[1], 82.0)
        eq_(new_data.data[2], 28.0)
Exemplo n.º 55
0
    def test_alt_extrapolation(self):
        from cis.collocation.col_implementations import GeneralUngriddedCollocator, li
        import datetime as dt

        cube = gridded_data.make_from_cube(mock.make_mock_cube(time_dim_length=3, hybrid_ht_len=10))

        sample_points = UngriddedData.from_points_array(
            [HyperPoint(lat=-4.0, lon=-4.0, alt=6382.8, t=dt.datetime(1984, 8, 27))])

        col = GeneralUngriddedCollocator(fill_value=np.NAN)
        new_data = col.collocate(sample_points, cube, None, li(extrapolate=True))[0]
        assert_almost_equal(new_data.data[0], 126.0, decimal=7)
Exemplo n.º 56
0
 def test_negative_lon_points_in_2d_dont_matter(self):
     """
         This is exactly the same test as above, except we ommit the point with negative longitude, this makes the
         collocator wrap the longitude coordinate and gives a slightly different interpolation result...
     """
     cube = make_from_cube(mock.make_square_5x3_2d_cube())
     sample_points = UngriddedData.from_points_array(
         [HyperPoint(1.0, 1.0), HyperPoint(4.0, 4.0)])
     col = GriddedUngriddedCollocator()
     new_data = col.collocate(sample_points, cube, None, 'lin')[0]
     assert_almost_equal(new_data.data[0], 8.8)
     assert_almost_equal(new_data.data[1], 11.2)
Exemplo n.º 57
0
    def test_collocation_of_alt_pres_points_on_hybrid_altitude_and_pressure_coordinates(self):
        cube = make_from_cube(mock.make_mock_cube(time_dim_length=3, hybrid_pr_len=10,
                                                  geopotential_height=True))

        sample_points = UngriddedData.from_points_array(
            # Test point with both pressure and altitude should interpolate over the altitude only (since that is also
            #  present in the data cube)
            [HyperPoint(lat=0.0, lon=0.0, alt=234.5, pres=1000, t=dt.datetime(1984, 8, 28, 0, 0, 0))])

        col = GriddedUngriddedCollocator(fill_value=np.NAN)
        new_data = col.collocate(sample_points, cube, None, 'lin')[0]
        assert_almost_equal(new_data.data[0], 225.5, decimal=7)
Exemplo n.º 58
0
    def test_basic_col_in_4d(self):
        from cis.collocation.col_implementations import GeneralUngriddedCollocator, mean, SepConstraintKdtree
        import datetime as dt

        ug_data = mock.make_regular_4d_ungridded_data()
        # Note - This isn't actually used for averaging
        sample_points = UngriddedData.from_points_array(
            [HyperPoint(lat=1.0, lon=1.0, alt=12.0, t=dt.datetime(1984, 8, 29, 8, 34))])

        col = GeneralUngriddedCollocator()
        new_data = col.collocate(sample_points, ug_data, SepConstraintKdtree(), mean())[0]
        eq_(new_data.data[0], 25.5)
Exemplo n.º 59
0
    def test_negative_lon_points_on_hybrid_pressure_coordinates_dont_matter(self):
        cube = make_from_cube(mock.make_mock_cube(time_dim_length=3, hybrid_pr_len=10))

        sample_points = UngriddedData.from_points_array(
            [HyperPoint(lat=0.0, lon=0.0, pres=111100040.5, t=dt.datetime(1984, 8, 28, 0, 0, 0)),
             HyperPoint(lat=5.0, lon=2.5, pres=177125044.5, t=dt.datetime(1984, 8, 28, 0, 0, 0))])
        col = GriddedUngriddedCollocator()
        new_data = col.collocate(sample_points, cube, None, 'lin')[0]
        # Exactly on the lat, lon, time points, interpolated over pressure
        assert_almost_equal(new_data.data[0], 221.5, decimal=5)
        # Exactly on the lat, time points, interpolated over latitude and pressure
        assert_almost_equal(new_data.data[1], 330.5, decimal=7)
Exemplo n.º 60
0
    def test_basic_col_gridded_to_ungridded_in_2d(self):
        cube = make_from_cube(mock.make_square_5x3_2d_cube())

        sample_points = UngriddedData.from_points_array(
            [HyperPoint(lat=1.0, lon=1.0),
             HyperPoint(lat=4.0, lon=4.0),
             HyperPoint(lat=-4.0, lon=-4.0)])
        col = GriddedUngriddedCollocator()
        new_data = col.collocate(sample_points, cube, None, 'nn')[0]
        eq_(new_data.data[0], 8.0)  # float(cube[2,1].data))
        eq_(new_data.data[1], 12.0)  # float(cube[3,2].data))
        eq_(new_data.data[2], 4.0)  # float(cube[1,0].data))