def test_bad_georeference(self):
        ok = global_pp()

        bad = global_pp()
        bad.coord('latitude').coord_system = None
        bad.coord('longitude').coord_system = None
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)

        bad = global_pp()
        bad.coord('latitude').coord_system = None
        bad.coord('longitude').coord_system = GeogCS(6371000)
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)

        bad = global_pp()
        bad.coord('latitude').coord_system = GeogCS(6370000)
        bad.coord('longitude').coord_system = GeogCS(6371000)
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)
    def test_bad_georeference(self):
        ok = global_pp()

        bad = global_pp()
        bad.coord('latitude').coord_system = None
        bad.coord('longitude').coord_system = None
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)

        bad = global_pp()
        bad.coord('latitude').coord_system = None
        bad.coord('longitude').coord_system = GeogCS(6371000)
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)

        bad = global_pp()
        bad.coord('latitude').coord_system = GeogCS(6370000)
        bad.coord('longitude').coord_system = GeogCS(6371000)
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)
    def test_bad_units(self):
        ok = global_pp()

        bad = global_pp()
        bad.coord('longitude').units = 'radians'
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)

        bad = Cube(np.arange(12, dtype=np.float32).reshape(3, 4))
        cs = OSGB()
        y_coord = DimCoord(range(3),
                           'projection_y_coordinate',
                           units='m',
                           coord_system=cs)
        x_coord = DimCoord(range(4),
                           'projection_x_coordinate',
                           units='km',
                           coord_system=cs)
        bad.add_dim_coord(y_coord, 0)
        bad.add_dim_coord(x_coord, 1)
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)
Esempio n. 4
0
 def setUp(self):
     self.func_setup = (
         'iris.analysis._regrid.'
         '_regrid_weighted_curvilinear_to_rectilinear__prepare')
     self.func_operate = (
         'iris.analysis._regrid.'
         '_regrid_weighted_curvilinear_to_rectilinear__perform')
     # Define a test source grid and target grid, basically the same.
     self.src_grid = global_pp()
     self.tgt_grid = global_pp()
     # Modify the names so we can tell them apart.
     self.src_grid.rename('src_grid')
     self.tgt_grid.rename('TARGET_GRID')
     # Replace the source-grid x and y coords with equivalent 2d versions.
     x_coord = self.src_grid.coord('longitude')
     y_coord = self.src_grid.coord('latitude')
     nx, = x_coord.shape
     ny, = y_coord.shape
     xx, yy = np.meshgrid(x_coord.points, y_coord.points)
     self.src_grid.remove_coord(x_coord)
     self.src_grid.remove_coord(y_coord)
     x_coord_2d = AuxCoord(xx,
                           standard_name=x_coord.standard_name,
                           units=x_coord.units,
                           coord_system=x_coord.coord_system)
     y_coord_2d = AuxCoord(yy,
                           standard_name=y_coord.standard_name,
                           units=y_coord.units,
                           coord_system=y_coord.coord_system)
     self.src_grid.add_aux_coord(x_coord_2d, (0, 1))
     self.src_grid.add_aux_coord(y_coord_2d, (0, 1))
     self.weights = np.ones(self.src_grid.shape, self.src_grid.dtype)
     # Define an actual, dummy cube for the internal partial result, so we
     # can do a cubelist merge on it, which is too complicated to mock out.
     self.dummy_slice_result = Cube([1])
 def setUp(self):
     self.func_setup = (
         'iris.experimental.regrid.'
         '_regrid_weighted_curvilinear_to_rectilinear__prepare')
     self.func_operate = (
         'iris.experimental.regrid.'
         '_regrid_weighted_curvilinear_to_rectilinear__perform')
     # Define a test source grid and target grid, basically the same.
     self.src_grid = global_pp()
     self.tgt_grid = global_pp()
     # Modify the names so we can tell them apart.
     self.src_grid.rename('src_grid')
     self.tgt_grid.rename('TARGET_GRID')
     # Replace the source-grid x and y coords with equivalent 2d versions.
     x_coord = self.src_grid.coord('longitude')
     y_coord = self.src_grid.coord('latitude')
     nx, = x_coord.shape
     ny, = y_coord.shape
     xx, yy = np.meshgrid(x_coord.points, y_coord.points)
     self.src_grid.remove_coord(x_coord)
     self.src_grid.remove_coord(y_coord)
     x_coord_2d = AuxCoord(xx,
                           standard_name=x_coord.standard_name,
                           units=x_coord.units,
                           coord_system=x_coord.coord_system)
     y_coord_2d = AuxCoord(yy,
                           standard_name=y_coord.standard_name,
                           units=y_coord.units,
                           coord_system=y_coord.coord_system)
     self.src_grid.add_aux_coord(x_coord_2d, (0, 1))
     self.src_grid.add_aux_coord(y_coord_2d, (0, 1))
     self.weights = np.ones(self.src_grid.shape, self.src_grid.dtype)
     # Define an actual, dummy cube for the internal partial result, so we
     # can do a cubelist merge on it, which is too complicated to mock out.
     self.dummy_slice_result = Cube([1])
    def test_non_rectilinear_src(self):
        ok = global_pp()

        # Lat and/or lon missing
        bad = global_pp()
        bad.remove_coord('latitude')
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)

        bad = global_pp()
        bad.remove_coord('longitude')
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)

        bad = global_pp()
        bad.remove_coord('latitude')
        bad.remove_coord('longitude')
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)

        # Lat/lon not a DimCoord
        def demote_coord(coord_name):
            bad = global_pp()
            coord = bad.coord(coord_name)
            dims = bad.coord_dims(coord)
            bad.remove_coord(coord_name)
            aux_coord = AuxCoord.from_coord(coord)
            bad.add_aux_coord(aux_coord, dims)
            with self.assertRaises(ValueError):
                regrid(bad, ok)
            with self.assertRaises(ValueError):
                regrid(ok, bad)

        demote_coord('latitude')
        demote_coord('longitude')

        # Lat/lon share a single dimension
        bad = global_pp()
        lat = bad.coord('latitude')
        bad = bad[0, :lat.shape[0]]
        bad.remove_coord('latitude')
        bad.add_aux_coord(lat, 0)
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)
    def test_non_rectilinear_src(self):
        ok = global_pp()

        # Lat and/or lon missing
        bad = global_pp()
        bad.remove_coord('latitude')
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)

        bad = global_pp()
        bad.remove_coord('longitude')
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)

        bad = global_pp()
        bad.remove_coord('latitude')
        bad.remove_coord('longitude')
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)

        # Lat/lon not a DimCoord
        def demote_coord(coord_name):
            bad = global_pp()
            coord = bad.coord(coord_name)
            dims = bad.coord_dims(coord)
            bad.remove_coord(coord_name)
            aux_coord = AuxCoord.from_coord(coord)
            bad.add_aux_coord(aux_coord, dims)
            with self.assertRaises(ValueError):
                regrid(bad, ok)
            with self.assertRaises(ValueError):
                regrid(ok, bad)
        demote_coord('latitude')
        demote_coord('longitude')

        # Lat/lon share a single dimension
        bad = global_pp()
        lat = bad.coord('latitude')
        bad = bad[0, :lat.shape[0]]
        bad.remove_coord('latitude')
        bad.add_aux_coord(lat, 0)
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)
Esempio n. 8
0
    def test_geog_cs_circular(self):
        cube = stock.global_pp()
        assert cube.coord('longitude').circular

        result = _xy_range(cube)
        np.testing.assert_array_almost_equal(
            result, ((0, 360), (-90, 90)), decimal=0)
 def test_subsample(self):
     src = global_pp()
     grid = src[::2, ::3]
     result = regrid(src, grid)
     qplt.pcolormesh(result)
     qplt.plt.gca().coastlines()
     self.check_graphic()
Esempio n. 10
0
    def test_geog_cs_circular(self):
        cube = stock.global_pp()
        assert cube.coord('longitude').circular

        result = _xy_range(cube)
        np.testing.assert_array_almost_equal(result, ((0, 360), (-90, 90)),
                                             decimal=0)
Esempio n. 11
0
 def _grid_subset(self):
     # The destination grid points are entirely contained within the
     # src grid points.
     grid = global_pp()[:4, :5]
     grid.coord('longitude').points = np.linspace(-3.182, -3.06, 5)
     grid.coord('latitude').points = np.linspace(52.372, 52.44, 4)
     return grid
 def test_subsample(self):
     src = global_pp()
     grid = src[::2, ::3]
     result = regrid(src, grid)
     qplt.pcolormesh(result)
     qplt.plt.gca().coastlines()
     self.check_graphic()
Esempio n. 13
0
 def _grid_subset(self):
     # The destination grid points are entirely contained within the
     # src grid points.
     grid = global_pp()[:4, :5]
     grid.coord('longitude').points = np.linspace(-3.182, -3.06, 5)
     grid.coord('latitude').points = np.linspace(52.372, 52.44, 4)
     return grid
 def test_grid_no_overlap(self):
     # The destination grid points are NOT contained within the
     # src grid points.
     grid = global_pp()[:4, :4]
     grid.coord('longitude').points = np.linspace(-3.3, -3.2, 4)
     grid.coord('latitude').points = np.linspace(52.377, 52.43, 4)
     result = regrid(self.src, grid)
     self.assertCMLApproxData(result, RESULT_DIR + ('no_overlap.cml',))
Esempio n. 15
0
 def setUp(self):
     self.src = global_pp()
     # Subsample and shift the target grid so that we can see a visual
     # difference between regridding scheme methods.
     grid = self.src[1::2, 1::3]
     grid.coord('latitude').points = grid.coord('latitude').points + 1
     grid.coord('longitude').points = grid.coord('longitude').points + 1
     self.grid = grid
Esempio n. 16
0
    def test_geog_cs_regional(self):
        cube = stock.global_pp()
        cube = cube[10:20, 20:30]
        assert not cube.coord('longitude').circular

        result = _xy_range(cube)
        np.testing.assert_array_almost_equal(
            result, ((75, 108.75), (42.5, 65)), decimal=0)
Esempio n. 17
0
 def setUp(self):
     self.src = global_pp()
     # Subsample and shift the target grid so that we can see a visual
     # difference between regridding scheme methods.
     grid = self.src[1::2, 1::3]
     grid.coord('latitude').points = grid.coord('latitude').points + 1
     grid.coord('longitude').points = grid.coord('longitude').points + 1
     self.grid = grid
 def test_grid_no_overlap(self):
     # The destination grid points are NOT contained within the
     # src grid points.
     grid = global_pp()[:4, :4]
     grid.coord('longitude').points = np.linspace(-3.3, -3.2, 4)
     grid.coord('latitude').points = np.linspace(52.377, 52.43, 4)
     result = regrid(self.src, grid)
     self.assertCMLApproxData(result, RESULT_DIR + ('no_overlap.cml', ))
Esempio n. 19
0
    def setUp(self):
        src = global_pp()[::10, ::10]
        level_height = AuxCoord(0, long_name='level_height', units='m',
                                attributes={'positive': 'up'})
        sigma = AuxCoord(1, long_name='sigma')
        surface_altitude = AuxCoord((src.data - src.data.min()) * 50,
                                    'surface_altitude', units='m')
        src.add_aux_coord(level_height)
        src.add_aux_coord(sigma)
        src.add_aux_coord(surface_altitude, [0, 1])
        hybrid_height = HybridHeightFactory(level_height, sigma,
                                            surface_altitude)
        src.add_aux_factory(hybrid_height)
        self.src = src

        grid = global_pp()[:4, :4]
        grid.coord('longitude').points = grid.coord('longitude').points - 5
        self.grid = grid
    def setUp(self):
        src = global_pp()[::10, ::10]
        level_height = AuxCoord(0, long_name='level_height', units='m',
                                attributes={'positive': 'up'})
        sigma = AuxCoord(1, long_name='sigma')
        surface_altitude = AuxCoord((src.data - src.data.min()) * 50,
                                    'surface_altitude', units='m')
        src.add_aux_coord(level_height)
        src.add_aux_coord(sigma)
        src.add_aux_coord(surface_altitude, [0, 1])
        hybrid_height = HybridHeightFactory(level_height, sigma,
                                            surface_altitude)
        src.add_aux_factory(hybrid_height)
        self.src = src

        grid = global_pp()[:4, :4]
        grid.coord('longitude').points = grid.coord('longitude').points - 5
        self.grid = grid
 def test_single_point(self):
     src = self.src[0, 0]
     grid = global_pp()[:1, :1]
     # These coordinate values have been derived by converting the
     # rotated coordinates of src[1, 1] into lat/lon by using cs2cs.
     grid.coord('longitude').points = -3.144870
     grid.coord('latitude').points = 52.406444
     result = regrid(src, grid)
     self.assertEqual(src.data[1, 1], result.data)
 def test_single_point(self):
     src = self.src[0, 0]
     grid = global_pp()[:1, :1]
     # These coordinate values have been derived by converting the
     # rotated coordinates of src[1, 1] into lat/lon by using cs2cs.
     grid.coord('longitude').points = -3.144870
     grid.coord('latitude').points = 52.406444
     result = regrid(src, grid)
     self.assertEqual(src.data[1, 1], result.data)
Esempio n. 23
0
    def test_geog_cs_regional(self):
        cube = stock.global_pp()
        cube = cube[10:20, 20:30]
        assert not cube.coord("longitude").circular

        result = _xy_range(cube)
        np.testing.assert_array_almost_equal(
            result, ((75, 108.75), (42.5, 65)), decimal=0
        )
Esempio n. 24
0
 def setUp(self):
     self.src_grid = global_pp()
     y = self.src_grid.coord('latitude')
     x = self.src_grid.coord('longitude')
     self.src_grid.remove_coord('latitude')
     self.src_grid.remove_coord('longitude')
     self.src_grid.add_aux_coord(y, 0)
     self.src_grid.add_aux_coord(x, 1)
     weights = np.ones(self.src_grid.shape, self.src_grid.dtype)
     self.regridder = Regridder(self.src_grid, self.src_grid, weights)
 def setUp(self):
     self.src_grid = global_pp()
     y = self.src_grid.coord('latitude')
     x = self.src_grid.coord('longitude')
     self.src_grid.remove_coord('latitude')
     self.src_grid.remove_coord('longitude')
     self.src_grid.add_aux_coord(y, 0)
     self.src_grid.add_aux_coord(x, 1)
     weights = np.ones(self.src_grid.shape, self.src_grid.dtype)
     self.regridder = Regridder(self.src_grid, self.src_grid, weights)
 def test_non_cube(self):
     array = np.zeros((3, 4))
     cube = global_pp()
     with self.assertRaises(TypeError):
         regrid(array, cube)
     with self.assertRaises(TypeError):
         regrid(cube, array)
     with self.assertRaises(TypeError):
         regrid(42, cube)
     with self.assertRaises(TypeError):
         regrid(cube, 42)
Esempio n. 27
0
 def test_grid_partial_overlap(self):
     # The destination grid points are partially contained within the
     # src grid points.
     grid = global_pp()[:4, :4]
     grid.coord('longitude').points = np.linspace(-3.3, -3.06, 4)
     grid.coord('latitude').points = np.linspace(52.377, 52.43, 4)
     for method in self.methods:
         regridder = Regridder(self.src, grid, method, self.mode)
         result = regridder(self.src)
         cml = RESULT_DIR + ('{}_partial_overlap.cml'.format(method),)
         self.assertCMLApproxData(result, cml)
Esempio n. 28
0
 def test_grid_partial_overlap(self):
     # The destination grid points are partially contained within the
     # src grid points.
     grid = global_pp()[:4, :4]
     grid.coord('longitude').points = np.linspace(-3.3, -3.06, 4)
     grid.coord('latitude').points = np.linspace(52.377, 52.43, 4)
     for method in self.methods:
         regridder = Regridder(self.src, grid, method, self.mode)
         result = regridder(self.src)
         cml = RESULT_DIR + ('{}_partial_overlap.cml'.format(method), )
         self.assertCMLApproxData(result, cml)
 def demote_coord(coord_name):
     bad = global_pp()
     coord = bad.coord(coord_name)
     dims = bad.coord_dims(coord)
     bad.remove_coord(coord_name)
     aux_coord = AuxCoord.from_coord(coord)
     bad.add_aux_coord(aux_coord, dims)
     with self.assertRaises(ValueError):
         regrid(bad, ok)
     with self.assertRaises(ValueError):
         regrid(ok, bad)
 def test_non_cube(self):
     array = np.zeros((3, 4))
     cube = global_pp()
     with self.assertRaises(TypeError):
         regrid(array, cube)
     with self.assertRaises(TypeError):
         regrid(cube, array)
     with self.assertRaises(TypeError):
         regrid(42, cube)
     with self.assertRaises(TypeError):
         regrid(cube, 42)
Esempio n. 31
0
 def setUp(self):
     self.func = ('iris.experimental.regrid.'
                  'regrid_weighted_curvilinear_to_rectilinear')
     self.ok = global_pp()
     y = self.ok.coord('latitude')
     x = self.ok.coord('longitude')
     self.ok.remove_coord('latitude')
     self.ok.remove_coord('longitude')
     self.ok.add_aux_coord(y, 0)
     self.ok.add_aux_coord(x, 1)
     self.weights = np.ones(self.ok.shape, self.ok.dtype)
 def demote_coord(coord_name):
     bad = global_pp()
     coord = bad.coord(coord_name)
     dims = bad.coord_dims(coord)
     bad.remove_coord(coord_name)
     aux_coord = AuxCoord.from_coord(coord)
     bad.add_aux_coord(aux_coord, dims)
     with self.assertRaises(ValueError):
         regrid(bad, ok)
     with self.assertRaises(ValueError):
         regrid(ok, bad)
Esempio n. 33
0
 def setUp(self):
     self.src = global_pp()
     _ = self.src.data
     # Cast up to float64, to work around numpy<=1.8 bug with means of
     # arrays of 32bit floats.
     self.src.data = self.src.data.astype(np.float64)
     # Subsample and shift the target grid so that we can see a visual
     # difference between regridding scheme methods.
     grid = self.src[1::2, 1::3]
     grid.coord("latitude").points = grid.coord("latitude").points + 1
     grid.coord("longitude").points = grid.coord("longitude").points + 1
     self.grid = grid
Esempio n. 34
0
    def setUp(self):
        path = tests.get_data_path(
            ('NetCDF', 'unstructured_grid', 'theta_nodal_xios.nc'))
        self.src = iris.load_cube(path, 'Potential Temperature')

        src_lat = self.src.coord('latitude')
        src_lon = self.src.coord('longitude')
        src_lat.coord_system = src_lon.coord_system = GeogCS(6370000)
        src_lat.convert_units(Unit('degrees'))
        src_lon.convert_units(Unit('degrees'))

        self.global_grid = global_pp()
    def setUp(self):
        path = tests.get_data_path(
            ("NetCDF", "unstructured_grid", "theta_nodal_xios.nc"))
        self.src = iris.load_cube(path, "Potential Temperature")

        src_lat = self.src.coord("latitude")
        src_lon = self.src.coord("longitude")
        src_lat.coord_system = src_lon.coord_system = GeogCS(6370000)
        src_lat.convert_units(Unit("degrees"))
        src_lon.convert_units(Unit("degrees"))

        self.global_grid = global_pp()
Esempio n. 36
0
    def setUp(self):
        path = tests.get_data_path(('NetCDF', 'unstructured_grid',
                                   'theta_nodal_xios.nc'))
        self.src = iris.load_cube(path, 'Potential Temperature')

        src_lat = self.src.coord('latitude')
        src_lon = self.src.coord('longitude')
        src_lat.coord_system = src_lon.coord_system = GeogCS(6370000)
        src_lat.convert_units(Unit('degrees'))
        src_lon.convert_units(Unit('degrees'))

        self.global_grid = global_pp()
Esempio n. 37
0
 def setUp(self):
     self.src = global_pp()
     _ = self.src.data
     # Cast up to float64, to work around numpy<=1.8 bug with means of
     # arrays of 32bit floats.
     self.src.data = self.src.data.astype(np.float64)
     # Subsample and shift the target grid so that we can see a visual
     # difference between regridding scheme methods.
     grid = self.src[1::2, 1::3]
     grid.coord('latitude').points = grid.coord('latitude').points + 1
     grid.coord('longitude').points = grid.coord('longitude').points + 1
     self.grid = grid
    def test_bad_units(self):
        ok = global_pp()

        bad = global_pp()
        bad.coord('longitude').units = 'radians'
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)

        bad = Cube(np.arange(12, dtype=np.float32).reshape(3, 4))
        cs = OSGB()
        y_coord = DimCoord(range(3), 'projection_y_coordinate', units='m',
                           coord_system=cs)
        x_coord = DimCoord(range(4), 'projection_x_coordinate', units='km',
                           coord_system=cs)
        bad.add_dim_coord(y_coord, 0)
        bad.add_dim_coord(x_coord, 1)
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)
Esempio n. 39
0
File: azeq.py Progetto: pp-mo/azeq
def simpletest(do_savefig=True, do_showfig=True, savefig_file='./puffer.png'):
    figure = make_puffersphere_figure()
    axes = make_puffersphere_axes()
    axes.stock_img()
    data = istk.global_pp()
    axes.coastlines()
    qplt.contour(data)
    draw_gridlines()
    #axes.coastlines()
    if do_savefig:
        save_figure_for_puffersphere(figure=plt.gcf(), filename=savefig_file)
    if do_showfig:
        plt.show()
Esempio n. 40
0
    # Suck it back in
    readback = rasterio.open(file_path)
    data = readback.read(1)
    print 'readback:'
    print '  crs=', readback.crs
    print '  width=', readback.width
    print '  height=', readback.height
    print '  data-shape=', data.shape
    print '  data-dtype=', data.dtype
    print '  data-min=', np.min(data)
    print '  data-max=', np.max(data)


if __name__ == '__main__':
    import iris.tests.stock as istk
    test_cube = istk.global_pp()

    print 'Original float file version:'
    raster = RasterioContent(test_cube)
    raster.save('tmp1.tif')
    file_report('tmp1.tif')

    # Let's rescale to 0..255, just for fun
    datamin = np.min(test_cube.data)
    datamax = np.max(test_cube.data)
    newmin = 25.
    newmax = 215.
    ratio = (newmax - newmin) / (datamax - datamin)
    test_cube.data = newmin + ratio * (test_cube.data - datamin)
    # Let's also make it into bytes
    test_cube.data = test_cube.data.astype(np.uint8)