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])
    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)
Beispiel #3
0
    def test_horizontal_constraint_for_same_3d_grids_returns_original_data(self):
        # Create sample and data cubes that include a time coordinate with the dimensions in reverse of normal order.
        sample_cube = gridded_data.make_from_cube(
            mock.make_mock_cube(lat_dim_length=5, lon_dim_length=3, time_dim_length=2, dim_order=["time", "lon", "lat"])
        )
        data_cube = gridded_data.make_from_cube(
            mock.make_mock_cube(lat_dim_length=5, lon_dim_length=3, time_dim_length=2, dim_order=["time", "lon", "lat"])
        )

        data_points = data_cube.get_non_masked_points()

        sample_points = sample_cube.get_all_points()
        coord_map = make_coord_map(sample_cube, data_cube)

        # Make separation constraint small enough to include only the corresponding point in the data cube.
        constraint = SepConstraintKdtree(h_sep=400)

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

        for idx, sample_point in enumerate(sample_points):
            out_points = constraint.constrain_points(sample_point, data_points)
            # Two times for each spatial position.
            assert len(out_points) == 2
            assert data_points[idx].val[0] in [p.val[0] for p in out_points]
    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)
Beispiel #5
0
 def setUp(self):
     """
         Create the dummy objects necessary for testing
     """
     self.ug = make_regular_2d_ungridded_data()
     self.ug_1 = make_regular_2d_ungridded_data()
     self.gd = make_from_cube(make_mock_cube())
     self.gd_large = make_from_cube(make_mock_cube(50, 30))
Beispiel #6
0
 def setUp(self):
     """
         Create the dummy objects necessary for testing
     """
     self.ug = make_regular_2d_ungridded_data()
     self.ug_1 = make_regular_2d_ungridded_data()
     self.gd = make_from_cube(make_mock_cube())
     self.gd_large = make_from_cube(make_mock_cube(50, 30))
Beispiel #7
0
 def _make_two_gridded(self):
     data1 = make_from_cube(mock.make_mock_cube())
     data2 = make_from_cube(mock.make_mock_cube(data_offset=10))
     data1.var_name = 'var1'
     data2._var_name = 'var2'
     data1.filenames = ['filename1']
     data2.filenames = ['filename2']
     self.data = [data1, data2]
     self.data = GriddedDataList([data1, data2])
    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])
Beispiel #9
0
    def test_taylor_diagram_gridded(self):
        from cis.test.util.mock import make_mock_cube
        from cis.data_io.gridded_data import GriddedDataList

        d = GriddedDataList([make_mock_cube(), make_mock_cube(data_offset=2)])
        d[0].var_name = 'snow'
        d[1].var_name = 'rain'

        d.plot(how='taylor')

        self.check_graphic()
Beispiel #10
0
    def test_iris_comparative_scatter(self):
        from cis.test.util.mock import make_mock_cube
        from cis.data_io.gridded_data import GriddedDataList

        d = GriddedDataList([make_mock_cube(), make_mock_cube(data_offset=2)])
        d[0].var_name = 'snow'
        d[1].var_name = 'rain'

        d.plot(how='comparativescatter')

        self.check_graphic()
Beispiel #11
0
    def test_gridded_gridded_bin_when_sample_has_dimension_data_doesnt(self):
        # JASCIS-204
        from cis.data_io.gridded_data import make_from_cube
        sample = make_from_cube(make_mock_cube(time_dim_length=7, dim_order=['lat', 'lon', 'time']))
        data = make_from_cube(make_mock_cube(lat_dim_length=11, lon_dim_length=13,
                                             time_dim_length=0, dim_order=['time', 'lon', 'lat']))

        col = GeneralGriddedCollocator()
        constraint = BinningCubeCellConstraint()
        kernel = mean()
        out_cube = col.collocate(points=sample, data=data, constraint=constraint, kernel=kernel)
        assert out_cube[0].shape == (5, 3)
Beispiel #12
0
    def test_iris_multiple_scatter(self):
        from cis.test.util.mock import make_mock_cube
        from cis.data_io.gridded_data import GriddedDataList

        # This only works with one dimensional gridded data
        d = GriddedDataList([make_mock_cube(lat_dim_length=0), make_mock_cube(lat_dim_length=0, data_offset=2)])
        d[0].var_name = 'snow'
        d[1].var_name = 'rain'

        # Will default to line plots
        d.plot()

        self.check_graphic()
    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)
    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))
Beispiel #15
0
 def test_gridded_write_units(self):
     data = make_from_cube(make_mock_cube())
     data.var_name = 'rain'
     data.units = 'ppm'
     data.save_data(tmp_file)
     self.d = Dataset(tmp_file)
     assert self.d.variables['rain'].units == 'ppm'
    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)
    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)
    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)
    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])
    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))
    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)
    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])
    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])
    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))
 def test_gridded_write_no_time_has_no_unlimited_dimension(self):
     data = make_from_cube(make_mock_cube())
     data.var_name = 'rain'
     data.save_data(tmp_file)
     self.d = Dataset(tmp_file)
     for d in self.d.dimensions.values():
         assert not d.isunlimited()
    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)
Beispiel #27
0
 def test_gridded_list_write_time_as_unlimited_dimension(self):
     data = GriddedDataList(
         [make_from_cube(make_mock_cube(time_dim_length=7))])
     data[0].var_name = 'rain'
     data.save_data(tmp_file)
     self.d = Dataset(tmp_file)
     assert self.d.dimensions['time'].isunlimited()
    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)
Beispiel #29
0
 def test_gridded_list_write_no_time_has_no_unlimited_dimension(self):
     data = GriddedDataList([make_from_cube(make_mock_cube())])
     data[0].var_name = 'rain'
     data.save_data(tmp_file)
     self.d = Dataset(tmp_file)
     for d in self.d.dimensions.values():
         assert not d.isunlimited()
    def test_GIVEN_grid_contains_single_points_WHEN_collapse_THEN_stddev_undefined(self):
        cube = make_from_cube(mock.make_mock_cube(2, 2))
        cube.data = numpy.ma.masked_invalid([[float('Nan'), 1], [float('Nan'), float('Nan')]])
        kernel = aggregation_kernels['moments']
        result = cube.collapsed(['y'], how=kernel)

        assert_that(result[1].data.mask.all())
    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)
Beispiel #32
0
 def test_get_axis_gridded(self):
     from cis.plotting.plot import get_axis
     from cis.test.util.mock import make_mock_cube
     from cis.data_io.gridded_data import make_from_cube
     d = make_from_cube(make_mock_cube())
     assert get_axis(d, "x").name() == 'longitude'
     assert get_axis(d, "y").name() == 'latitude'
 def test_gridded_write_units(self):
     data = make_from_cube(make_mock_cube())
     data.var_name = 'rain'
     data.units = 'ppm'
     data.save_data(tmp_file)
     self.d = Dataset(tmp_file)
     assert self.d.variables['rain'].units == 'ppm'
Beispiel #34
0
 def test_get_axis_gridded(self):
     from cis.plotting.plot import get_axis
     from cis.test.util.mock import make_mock_cube
     from cis.data_io.gridded_data import make_from_cube
     d = make_from_cube(make_mock_cube())
     assert get_axis(d, "x").name() == 'longitude'
     assert get_axis(d, "y").name() == 'latitude'
Beispiel #35
0
    def test_heatmap(self):
        from cis.test.util.mock import make_mock_cube
        from cis.data_io.gridded_data import GriddedData

        d = GriddedData.make_from_cube(make_mock_cube())
        d.plot()

        self.check_graphic()
Beispiel #36
0
    def test_data_with_no_standard_name(self):
        sample_cube = make_mock_cube()
        data_points = make_dummy_1d_ungridded_data()

        col = GeneralGriddedCollocator(fill_value=-999.9)
        con = CubeCellConstraint()

        out_cube = col.collocate(points=sample_cube, data=data_points, constraint=con, kernel=SlowMean())[0]
Beispiel #37
0
    def test_GIVEN_grid_contains_single_points_WHEN_collapse_THEN_stddev_undefined(self):
        grid = {'y': AggregationGrid(-10, 10, float('Nan'), False)}
        cube = mock.make_mock_cube(2, 2)
        cube.data = numpy.ma.masked_invalid([[float('Nan'), 1], [float('Nan'), float('Nan')]])
        kernel = aggregation_kernels['moments']
        agg = Aggregator(cube, grid)
        result = agg.aggregate_gridded(kernel)

        assert_that(result[1].data.mask.all())
    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)
Beispiel #39
0
    def test_contour_over_bluemarble(self):
        from cis.test.util.mock import make_mock_cube
        from cis.data_io.gridded_data import GriddedData

        d = GriddedData.make_from_cube(make_mock_cube())
        ax = d.plot(how='contour')
        ax.bluemarble()

        self.check_graphic()
    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')
Beispiel #41
0
def single_masked_point_results_in_single_value_in_cell_using_kernel_and_con(con, kernel):
    sample_cube = make_mock_cube()
    data_point = make_dummy_ungridded_data_single_point(0.5, 0.5, 1.2, mask=True)
    col = GeneralGriddedCollocator(fill_value=-999.9)
    out_cube = col.collocate(points=sample_cube, data=data_point, constraint=con, kernel=kernel)[0]
    expected_result = numpy.array([[-999.9, -999.9, -999.9],
                                   [-999.9, -999.9, -999.9],
                                   [-999.9, -999.9, -999.9],
                                   [-999.9, -999.9, -999.9],
                                   [-999.9, -999.9, -999.9]])
    assert_arrays_equal(out_cube.data.filled(), expected_result)
Beispiel #42
0
def point_on_a_lat_lon_boundary_appears_in_highest_cell(con, kernel):
    sample_cube = make_mock_cube()
    data_point = make_dummy_ungridded_data_single_point(2.5, 2.5, 1.2)
    col = GeneralGriddedCollocator(fill_value=-999.9)
    out_cube = col.collocate(points=sample_cube, data=data_point, constraint=con, kernel=kernel)[0]
    expected_result = numpy.array([[-999.9, -999.9, -999.9],
                                   [-999.9, -999.9, -999.9],
                                   [-999.9, -999.9, -999.9],
                                   [-999.9, -999.9, 1.2],
                                   [-999.9, -999.9, -999.9]])
    assert_arrays_almost_equal(out_cube.data.filled(), expected_result)
Beispiel #43
0
def single_point_on_grid_corner_is_counted_once(con, kernel):
    sample_cube = make_mock_cube()
    data_point = make_dummy_ungridded_data_single_point(10, 5, 1.2)
    col = GeneralGriddedCollocator(fill_value=-999.9)
    out_cube = col.collocate(points=sample_cube, data=data_point, constraint=con, kernel=kernel)[0]
    expected_result = numpy.array([[-999.9, -999.9, -999.9],
                                   [-999.9, -999.9, -999.9],
                                   [-999.9, -999.9, -999.9],
                                   [-999.9, -999.9, -999.9],
                                   [-999.9, -999.9, 1.2]])
    assert_arrays_equal(out_cube.data.filled(), expected_result)
Beispiel #44
0
def single_point_outside_grid_and_one_inside_excludes_outside_using_binned_only(con, kernel):
    sample_cube = make_mock_cube()
    data_point = make_dummy_ungridded_data_single_point([0.5, 99], [0.5, 99], [1.2, 5])
    col = GeneralGriddedCollocator(fill_value=-999.9)
    out_cube = col.collocate(points=sample_cube, data=data_point, constraint=con, kernel=kernel)[0]
    expected_result = numpy.array([[-999.9, -999.9, -999.9],
                                   [-999.9, -999.9, -999.9],
                                   [-999.9, 1.2, -999.9],
                                   [-999.9, -999.9, -999.9],
                                   [-999.9, -999.9, -999.9]])
    assert_arrays_equal(out_cube.data.filled(), expected_result)
Beispiel #45
0
def two_points_in_a_cell_results_in_mean_value_in_cell(con, kernel):
    sample_cube = make_mock_cube()
    data_point = make_dummy_ungridded_data_two_points_with_different_values(0.5, 0.5, 1.2, 1.4)
    col = GeneralGriddedCollocator(fill_value=-999.9)
    out_cube = col.collocate(points=sample_cube, data=data_point, constraint=con, kernel=kernel)[0]
    expected_result = numpy.array([[-999.9, -999.9, -999.9],
                                   [-999.9, -999.9, -999.9],
                                   [-999.9, 1.3, -999.9],
                                   [-999.9, -999.9, -999.9],
                                   [-999.9, -999.9, -999.9]])
    assert_arrays_almost_equal(out_cube.data.filled(), expected_result)
    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))
    def test_GIVEN_gridded_data_WHEN_full_collapse_THEN_calculations_correct(self):
        cube = make_from_cube(mock.make_mock_cube())
        kernel = aggregation_kernels['moments']
        result = cube.collapsed(['y'], how=kernel)

        expected_means = numpy.array([7, 8, 9])
        expected_std_dev = numpy.array(3 * [numpy.sqrt(22.5)])
        expected_no = numpy.array([5, 5, 5])
        assert_that(len(result), is_(3))
        assert_that(numpy.allclose(result[0].data, expected_means))
        assert_that(numpy.allclose(result[1].data, expected_std_dev))
        assert_that(numpy.array_equal(result[2].data, expected_no))
Beispiel #48
0
def test_set_longitude_bounds_wrap_at_360():
    gd = gridded_data.make_from_cube(mock.make_mock_cube())
    gd.set_longitude_range(0.0)
    assert (np.min(gd.coords(standard_name='longitude')[0].points) >= 0.0)
    assert (np.max(gd.coords(standard_name='longitude')[0].points) < 360.0)
    long_coord = gd.coord('longitude')
    assert (long_coord.points[0] == 0.0)
    assert (long_coord.points[1] == 5.0)
    assert (long_coord.points[2] == 355.0)
    assert ((long_coord.bounds[0] == np.array([-2.5, 2.5])).all())
    assert ((long_coord.bounds[1] == np.array([2.5, 7.5])).all())
    assert ((long_coord.bounds[2] == np.array([352.5, 357.5])).all())
Beispiel #49
0
def multiple_points_inside_grid_and_outside(con, kernel):
    sample_cube = make_mock_cube()
    data_point = make_dummy_ungridded_data_single_point([0.5, 99, 0.6, 3.0, -9], [0.5, 99, 0.6, 0.5, -3],
                                                        [1.2, 5, 3.4, 5, 8])
    col = GeneralGriddedCollocator(fill_value=-999.9)
    out_cube = col.collocate(points=sample_cube, data=data_point, constraint=con, kernel=kernel)[0]
    expected_result = numpy.array([[8, -999.9, -999.9],
                                   [-999.9, -999.9, -999.9],
                                   [-999.9, 2.3, -999.9],
                                   [-999.9, 5, -999.9],
                                   [-999.9, -999.9, -999.9]])
    assert_arrays_equal(out_cube.data.filled(), expected_result)
    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)
    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)
Beispiel #52
0
 def test_gridded_write_attributes(self):
     data = make_from_cube(make_mock_cube())
     data.var_name = 'rain'
     attrs = {
         'attr_name': 'attr_val',
         'standard_name': 'convective_rainfall_amount',
         'long_name': 'lg_val',
         'units': 'units'
     }
     data.add_attributes(attrs)
     data.save_data(tmp_file)
     self.d = Dataset(tmp_file)
     for key, val in attrs.items():
         assert getattr(self.d.variables['rain'], key) == val