Пример #1
0
    def test_comparisons(self):
        """See if the grids can compare themselves"""

        args = dict(nxny=(3, 3), dxdy=(1, 1), x0y0=(0, 0), proj=wgs84)
        g1 = Grid(**args)
        self.assertEqual(g1.center_grid, g1.corner_grid)
        self.assertTrue(g1.center_grid.almost_equal(g1.center_grid))

        g2 = Grid(**args)
        self.assertEqual(g1, g2)
        self.assertTrue(g1.almost_equal(g2))

        args['dxdy'] = (1. + 1e-6, 1. + 1e-6)
        g2 = Grid(**args)
        self.assertNotEqual(g1, g2)
        self.assertTrue(g1.almost_equal(g2))

        # serialization
        d = g1.to_dict()
        rg = Grid.from_dict(d)
        self.assertEqual(g1, rg)
        d = g2.to_dict()
        rg = Grid.from_dict(d)
        self.assertEqual(g2, rg)
        self.assertNotEqual(g1, rg)
        self.assertTrue(g1.almost_equal(rg))
        g1.to_json('test.json')
        rg = Grid.from_json('test.json')
        os.remove('test.json')
        self.assertEqual(g1, rg)
        g2.to_json('test.json')
        rg = Grid.from_json('test.json')
        os.remove('test.json')
        self.assertEqual(g2, rg)
        self.assertNotEqual(g1, rg)
        self.assertTrue(g1.almost_equal(rg))

        args['proj'] = gis.check_crs('+init=epsg:26915')
        g2 = Grid(**args)
        self.assertNotEqual(g1, g2)
        self.assertFalse(g1.almost_equal(g2))

        # New instance, same proj
        args['proj'] = gis.check_crs('+init=epsg:26915')
        g1 = Grid(**args)
        self.assertEqual(g1, g2)
        self.assertTrue(g1.almost_equal(g2))
        # serialization
        d = g1.to_dict()
        rg = Grid.from_dict(d)
        self.assertEqual(g1, rg)
        self.assertTrue(g1.almost_equal(rg))
        g1.to_json('test.json')
        rg = Grid.from_json('test.json')
        os.remove('test.json')
        self.assertEqual(g1, rg)
        self.assertTrue(g1.almost_equal(rg))
Пример #2
0
    def test_constructor(self):

        # It should work exact same for any projection
        projs = [wgs84, gis.check_crs('+init=epsg:26915')]

        for proj in projs:
            args = dict(nxny=(3, 3), dxdy=(1, 1), x0y0=(0, 0), proj=proj)
            g = Grid(**args)
            self.assertTrue(isinstance(g, Grid))
            self.assertEqual(g.center_grid, g.corner_grid)

            # serialization
            d = g.to_dict()
            rg = Grid.from_dict(d)
            self.assertEqual(g, rg)
            g.to_json('test.json')
            rg = Grid.from_json('test.json')
            os.remove('test.json')
            self.assertEqual(g, rg)

            oargs = dict(nxny=(3, 3), dxdy=(1, 1), x0y0=(0, 0), proj=proj)
            og = Grid(**oargs)
            self.assertEqual(g, og)

            # very simple test
            exp_i, exp_j = np.meshgrid(np.arange(3), np.arange(3))
            i, j = g.ij_coordinates
            assert_allclose(i, exp_i)
            assert_allclose(j, exp_j)

            i, j = g.xy_coordinates
            assert_allclose(i, exp_i)
            assert_allclose(j, exp_j)

            if proj == projs[0]:
                i, j = g.ll_coordinates
                assert_allclose(i, exp_i)
                assert_allclose(j, exp_j)

            args['proj'] = 'dummy'
            self.assertRaises(ValueError, Grid, **args)

            args['proj'] = proj
            args['nxny'] = (1, -1)
            self.assertRaises(ValueError, Grid, **args)
            args['nxny'] = (3, 3)

            args['dxdy'] = (1, -1)
            args['ll_corner'] = args['x0y0']
            del args['x0y0']
            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always')
                self.assertRaises(ValueError, Grid, **args)
                self.assertEqual(len(w), 1)
            args['x0y0'] = args['ll_corner']
            del args['ll_corner']

            # Center VS corner - multiple times because it was a bug
            assert_allclose(g.center_grid.xy_coordinates, g.xy_coordinates)
            assert_allclose(g.center_grid.center_grid.xy_coordinates,
                            g.xy_coordinates)
            assert_allclose(g.corner_grid.corner_grid.xy_coordinates,
                            g.corner_grid.xy_coordinates)

            ex = g.corner_grid.extent
            assert_allclose([-0.5, 2.5, -0.5, 2.5], ex)
            assert_allclose(g.center_grid.extent, g.corner_grid.extent)

            args['x0y0'] = (0, 0)

            g = Grid(**args)
            self.assertTrue(isinstance(g, Grid))

            oargs = dict(nxny=(3, 3), dxdy=(1, -1), x0y0=(0, 0), proj=proj)
            og = Grid(**oargs)
            self.assertEqual(g, og)

            # serialization
            d = og.to_dict()
            rg = Grid.from_dict(d)
            self.assertEqual(og, rg)

            # The simple test should work here too
            i, j = g.ij_coordinates
            assert_allclose(i, exp_i)
            assert_allclose(j, exp_j)

            # But the lonlats are the other way around:
            exp_x, exp_y = np.meshgrid(np.arange(3), -np.arange(3))
            x, y = g.xy_coordinates
            assert_allclose(x, exp_x)
            assert_allclose(y, exp_y)

            if proj == projs[0]:
                i, j = g.ll_coordinates
                assert_allclose(i, exp_x)
                assert_allclose(j, exp_y)

            # Center VS corner - multiple times because it was a bug
            assert_allclose(g.center_grid.xy_coordinates, g.xy_coordinates)
            assert_allclose(g.center_grid.center_grid.xy_coordinates,
                            g.xy_coordinates)
            assert_allclose(g.corner_grid.corner_grid.xy_coordinates,
                            g.corner_grid.xy_coordinates)

            ex = g.corner_grid.extent
            assert_allclose([-0.5, 2.5, -2.5, 0.5], ex)
            assert_allclose(g.center_grid.extent, g.corner_grid.extent)

            # The equivalents
            g = g.corner_grid
            i, j = g.ij_coordinates
            assert_allclose(i, exp_i)
            assert_allclose(j, exp_j)

            exp_x, exp_y = np.meshgrid(np.arange(3) - 0.5, -np.arange(3) + 0.5)
            x, y = g.xy_coordinates
            assert_allclose(x, exp_x)
            assert_allclose(y, exp_y)

            args = dict(nxny=(3, 2), dxdy=(1, 1), x0y0=(0, 0))
            g = Grid(**args)
            self.assertTrue(isinstance(g, Grid))
            self.assertTrue(g.xy_coordinates[0].shape == (2, 3))
            self.assertTrue(g.xy_coordinates[1].shape == (2, 3))