예제 #1
0
    def test_get_buffered_geometry(self):
        proj4 = '+proj=aea +lat_1=20 +lat_2=60 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs'
        buffer_crs_list = [None, CoordinateReferenceSystem(proj4=proj4)]
        poly = make_poly((36, 44), (-104, -95))

        for buffer_crs in buffer_crs_list:
            gvar = GeometryVariable(value=poly, name='geoms', dimensions='dim', crs=WGS84())
            self.assertEqual(gvar.crs, WGS84())
            if buffer_crs is None:
                buffer_value = 1
            else:
                buffer_value = 10

            ret = SpatialSubsetOperation._get_buffered_geometry_(gvar, buffer_value, buffer_crs=buffer_crs)
            ref = ret.get_value()[0]

            if buffer_crs is None:
                self.assertEqual(ref.bounds, (-105.0, 35.0, -94.0, 45.0))
            else:
                self.assertNumpyAllClose(np.array(ref.bounds), np.array(
                    (-104.00013263459613, 35.9999147913708, -94.99986736540386, 44.00008450528758)))
            self.assertEqual(gvar.crs, ret.crs)

            # check deepcopy
            ret.get_value()[0] = make_poly((1, 2), (3, 4))
            ref_buffered = ret.get_value()[0]
            ref_original = gvar.get_value()[0]
            with self.assertRaises(AssertionError):
                self.assertNumpyAllClose(np.array(ref_buffered.bounds), np.array(ref_original.bounds))
예제 #2
0
    def test_system_regridding_crs(self):
        """Test with coordinate systems."""

        dest_crs = WGS84()

        grid_spherical = self.get_gridxy_global(resolution=10.0, wrapped=False, crs=Spherical())
        self.assertEqual(grid_spherical.crs, Spherical())
        coords = grid_spherical.get_value_stacked()
        data_value = self.get_exact_field_value(coords[1], coords[0])
        desired = data_value.copy()
        data_var = Variable(name='data_src', value=data_value, dimensions=grid_spherical.dimensions)
        source = Field(grid=grid_spherical, is_data=data_var, crs=grid_spherical.crs)
        self.assertEqual(source.crs, Spherical())

        destination = deepcopy(source)
        destination.update_crs(dest_crs)

        source_expanded = deepcopy(source.grid)
        source_expanded.expand()
        diff = np.abs(destination.y.get_value() - source_expanded.y.get_value())
        self.assertAlmostEqual(diff.max(), 0.19231511439)

        for output_crs in [None, WGS84()]:
            ops = OcgOperations(dataset=source, regrid_destination=destination, output_crs=output_crs)
            ret = ops.execute()

            actual = ret.get_element(variable_name=data_var.name)
            if output_crs is None:
                self.assertEqual(actual.parent.crs, Spherical())
            else:
                self.assertEqual(actual.parent.crs, WGS84())
            actual = actual.get_value()
            diff = np.abs(actual - desired)
            self.assertTrue(diff.max() < 1e-5)
예제 #3
0
    def test_prepare_geometry(self):
        for geometry_record in self.get_subset_geometries():
            for ss, k in self:
                gvar = GeometryVariable(value=geometry_record['geom'],
                                        dimensions='dim',
                                        crs=WGS84())
                self.assertIsNotNone(gvar.crs)
                prepared = ss._prepare_geometry_(gvar)
                self.assertNotEqual(gvar.get_value()[0].bounds,
                                    prepared.get_value()[0].bounds)
                self.assertFalse(
                    np.may_share_memory(gvar.get_value(),
                                        prepared.get_value()))
                try:
                    self.assertEqual(prepared.crs, ss.field.crs)
                except AssertionError:
                    # Rotated pole on the fields become spherical.
                    self.assertEqual(prepared.crs, CFSpherical())
                    self.assertIsInstance(ss.field.crs, CFRotatedPole)

        # Test nebraska against an unwrapped dataset.
        nebraska = GeometryVariable(value=self.nebraska['geom'],
                                    dimensions='d',
                                    crs=WGS84())
        field = self.test_data.get_rd('cancm4_tas').get()
        ss = SpatialSubsetOperation(field)
        prepared = ss._prepare_geometry_(nebraska)
        self.assertEqual(prepared.wrapped_state, WrappedState.UNWRAPPED)
예제 #4
0
파일: test_geom.py 프로젝트: NCPP/ocgis
    def test_get_unioned(self):
        # TODO: Test with an n-dimensional mask.

        ancillary = Variable('ancillary')
        pa = self.get_geometryvariable(parent=ancillary.parent, crs=WGS84())

        self.assertIn(ancillary.name, pa.parent)

        unioned = pa.get_unioned()
        new_uid = Variable('flower',
                           value=[100],
                           dimensions=unioned.dimensions)
        unioned.set_ugid(new_uid)

        # Parent should be removed from the unioned variable.
        self.assertNotIn(ancillary.name, unioned.parent)
        self.assertIn(ancillary.name, pa.parent)

        self.assertEqual(unioned.crs, WGS84())
        self.assertEqual(unioned.shape, (1, ))
        desired = MultiPoint([[1.0, 2.0], [3.0, 4.0]])
        self.assertEqual(unioned.get_value()[0], desired)
        self.assertEqual(len(unioned.dimensions[0]), 1)
        self.assertIsNone(unioned.get_mask())
        self.assertEqual(unioned.ugid.get_value()[0], 100)
        self.assertNotEqual(id(unioned), id(pa))
예제 #5
0
    def test_init(self):
        # Test empty.
        gvar = GeometryVariable()
        self.assertEqual(gvar.dtype, object)

        gvar = self.get_geometryvariable()
        self.assertIsInstance(gvar.get_masked_value(), MaskedArray)
        self.assertEqual(gvar.ndim, 1)

        # Test passing a "crs".
        gvar = self.get_geometryvariable(crs=WGS84(),
                                         name='my_geom',
                                         dimensions='ngeom')
        self.assertEqual(gvar.crs, WGS84())

        # Test using lines.
        line1 = LineString([(0, 0), (1, 1)])
        line2 = LineString([(1, 1), (2, 2)])
        gvar = GeometryVariable(value=[line1, line2], dimensions='two')
        self.assertTrue(gvar.get_value()[1].almost_equals(line2))
        self.assertEqual(gvar.geom_type, line1.geom_type)
        lines = MultiLineString([line1, line2])
        lines2 = [lines, lines]
        for actual in [lines, lines2, lines]:
            gvar2 = GeometryVariable(value=actual, dimensions='ngeom')
            self.assertTrue(gvar2.get_value()[0].almost_equals(lines))
            self.assertEqual(gvar2.geom_type, lines.geom_type)
            self.assertTrue(gvar2.shape[0] > 0)
            self.assertIsNone(gvar2.get_mask())
예제 #6
0
파일: test_crs.py 프로젝트: wk1984/ocgis
    def test_system_writing_to_netcdf(self):
        """Test coordinate system is retrievable from netCDF format."""

        path = self.get_temporary_file_path('crs.nc')
        with nc.Dataset(path, 'w') as rootgrp:
            WGS84().write_to_rootgrp(rootgrp)
        rd = RequestDataset(path)
        infield = rd.get()
        actual = infield.first()
        self.assertEqual(actual, WGS84())
        actual_crs = AbstractProj4CRS.load_from_metadata(rd.metadata)
        self.assertEqual(actual_crs, WGS84())
예제 #7
0
    def test_get_spatial_subset_wrap(self):
        """Test subsetting with wrap set to a boolean value."""

        rd = self.test_data.get_rd('cancm4_tas')
        self.assertEqual(rd.get().wrapped_state, WrappedState.UNWRAPPED)
        ss = SpatialSubsetOperation(rd.get(), wrap=True)
        ret = ss.get_spatial_subset('intersects', self.nebraska['geom'], geom_crs=WGS84())
        self.assertEqual(ret.wrapped_state, WrappedState.WRAPPED)
        self.assertAlmostEqual(ret.grid.get_value_stacked()[1].mean(), -99.84375)

        # Test with wrap false.
        ss = SpatialSubsetOperation(rd.get(), wrap=False)
        ret = ss.get_spatial_subset('intersects', self.nebraska['geom'], geom_crs=WGS84())
        self.assertEqual(ret.wrapped_state, WrappedState.UNWRAPPED)
        self.assertAlmostEqual(ret.grid.get_value_stacked()[1].mean(), 260.15625)
예제 #8
0
 def test_update_crs(self):
     f = self.fixture(crs=Spherical())
     to_crs = WGS84()
     orig_diff = np.sum(np.diff(f.y.get_value()))
     f.update_crs(to_crs)
     actual_diff = np.sum(np.diff(f.y.get_value()))
     self.assertGreater(np.abs(orig_diff - actual_diff), 0.01)
예제 #9
0
파일: test_field.py 프로젝트: huard/ocgis
    def test_init(self):
        field = self.get_ocgfield()
        self.assertIsInstance(field, Field)

        # Test unique identifier.
        field = Field(uid=4)
        self.assertEqual(field.uid, 4)

        # Test with a coordinate system and geometry.
        desired_crs = WGS84()
        geom = GeometryVariable(name='geom',
                                value=[Point(1, 2)],
                                dimensions='geom')
        field = Field(crs=desired_crs, geom=geom)
        self.assertEqual(field.crs, desired_crs)

        # Test dimension names are automatically added to dimension map.
        g = GeometryVariable(name='geom',
                             value=[Point(1, 2)],
                             dimensions='the_geom_dim')
        f = Field(geom=g)
        actual = f.dimension_map.get_dimension(DimensionMapKey.GEOM)
        self.assertEqual(actual, ['the_geom_dim'])

        # Test dimension map does not have any entries at initialization.
        actual = Field()
        desired = actual.dimension_map.as_dict()
        self.assertEqual(len(desired), 0)
예제 #10
0
    def test_get_spatial_subset_rotated_pole(self):
        """Test input has rotated pole with now output CRS."""

        rd = self.rd_rotated_pole
        ss = SpatialSubsetOperation(rd.get())
        ret = ss.get_spatial_subset('intersects', self.germany['geom'], geom_crs=WGS84())
        self.assertEqual(ret.crs, rd.get().crs)
        self.assertAlmostEqual(ret.grid.get_value_stacked().mean(), -2.1699999954751132)
예제 #11
0
    def test_get_spatial_subset_output_crs(self):
        """Test subsetting with an output CRS."""

        # test with default crs converting to north american lambert
        proj4 = '+proj=aea +lat_1=20 +lat_2=60 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs'
        output_crs = CoordinateReferenceSystem(proj4=proj4)
        rd = self.test_data.get_rd('cancm4_tas')
        ss = SpatialSubsetOperation(rd.get(), output_crs=output_crs)
        ret = ss.get_spatial_subset('intersects', self.nebraska['geom'], geom_crs=WGS84())
        self.assertEqual(ret.crs, output_crs)
        self.assertAlmostEqual(ret.grid.get_value_stacked().mean(), -23341.955070198124)

        # test with an input rotated pole coordinate system
        rd = self.rd_rotated_pole
        ss = SpatialSubsetOperation(rd.get(), output_crs=env.DEFAULT_COORDSYS)
        ret = ss.get_spatial_subset('intersects', self.germany['geom'], geom_crs=WGS84())
        self.assertEqual(ret.crs, env.DEFAULT_COORDSYS)
예제 #12
0
파일: test_geom.py 프로젝트: NCPP/ocgis
    def test_get_mask_from_intersects(self):
        poly = wkt.loads(
            'POLYGON((-98.26574367088608142 40.19952531645570559,-98.71764240506330168 39.54825949367089066,-99.26257911392406186 39.16281645569620906,-99.43536392405064817 38.64446202531645724,-98.78409810126584034 38.33876582278481493,-98.23916139240508016 37.71408227848101546,-97.77397151898735217 37.67420886075949937,-97.62776898734178133 38.15268987341772799,-98.39865506329114453 38.52484177215190186,-98.23916139240508016 39.33560126582278826,-97.73409810126582897 39.58813291139241386,-97.52143987341773368 40.27927215189873777,-97.52143987341773368 40.27927215189873777,-98.26574367088608142 40.19952531645570559))'
        )
        desired_mask = np.array([[True, True, False, True],
                                 [True, False, True, True],
                                 [True, True, False, True]])

        dist = OcgDist()
        xdim = dist.create_dimension('x', 4, dist=True)
        ydim = dist.create_dimension('y', 3)
        dist.create_dimension('bounds', 2)
        dist.update_dimension_bounds()

        if MPI_RANK == 0:
            x = self.get_variable_x()
            y = self.get_variable_y()
            grid = Grid(x=x, y=y, abstraction='point', crs=WGS84())
            pa = get_geometry_variable(grid)
        else:
            pa = None
        pa = variable_scatter(pa, dist)

        vm.create_subcomm_by_emptyable('test_get_mask_from_intersects',
                                       pa,
                                       is_current=True)
        if vm.is_null:
            self.assertTrue(pa.is_empty)
            return

        usi = [False]
        if env.USE_SPATIAL_INDEX:
            usi.append(True)

        keywords = dict(use_spatial_index=usi)
        for k in self.iter_product_keywords(keywords):
            ret = pa.get_mask_from_intersects(
                poly, use_spatial_index=k.use_spatial_index)
            desired_mask_local = desired_mask[slice(*ydim.bounds_local),
                                              slice(*xdim.bounds_local)]
            if MPI_RANK > 1:
                self.assertIsNone(ret)
            else:
                self.assertNumpyAll(desired_mask_local, ret)

            # This does not test a parallel operation.
            if MPI_RANK == 0:
                # Test pre-masked values in geometry are okay for intersects operation.
                value = [Point(1, 1), Point(2, 2), Point(3, 3)]
                value = np.ma.array(value,
                                    mask=[False, True, False],
                                    dtype=object)
                pa2 = GeometryVariable(value=value, dimensions='ngeom')
                b = box(0, 0, 5, 5)
                res = pa2.get_mask_from_intersects(
                    b, use_spatial_index=k.use_spatial_index)
                self.assertNumpyAll(res, value.mask)
예제 #13
0
파일: test_geom.py 프로젝트: NCPP/ocgis
 def test_update_crs(self):
     from_crs = WGS84()
     pa = self.get_geometryvariable(crs=from_crs, name='g', dimensions='gg')
     to_crs = CoordinateReferenceSystem(epsg=2136)
     pa.update_crs(to_crs)
     self.assertEqual(pa.crs, to_crs)
     v0 = [1629871.494956261, -967769.9070825744]
     v1 = [2358072.3857447207, -239270.87548993886]
     np.testing.assert_almost_equal(pa.get_value()[0], v0, decimal=3)
     np.testing.assert_almost_equal(pa.get_value()[1], v1, decimal=3)
예제 #14
0
파일: test_geom.py 프로젝트: NCPP/ocgis
 def test_deepcopy(self):
     gvar = GeometryVariable(value=Point(1, 2),
                             crs=Spherical(),
                             dimensions='d')
     self.assertEqual(gvar.crs, Spherical())
     d = gvar.deepcopy()
     d.crs = WGS84()
     self.assertEqual(gvar.crs, Spherical())
     self.assertFalse(np.may_share_memory(gvar.get_value(), d.get_value()))
     self.assertIsNotNone(d.crs)
예제 #15
0
파일: test_crs.py 프로젝트: wk1984/ocgis
    def test_transform_coordinates(self):
        desired_min_maxes = [[-0.9330127018922193, 0.93301270189221941],
                             [-0.93301270189221941, 0.93301270189221941],
                             [-0.96592582628906831, 0.96592582628906831]]

        keywords = {
            'wrapped': [False, True],
            'angular_units': [OcgisUnits.DEGREES, OcgisUnits.RADIANS],
            'other_crs': [Cartesian(), WGS84()]
        }

        for k in self.iter_product_keywords(keywords):
            spherical = Spherical(angular_units=k.angular_units)
            tp = Tripole(spherical=spherical)

            grid = create_gridxy_global(resolution=30.0, wrapped=k.wrapped)

            if not k.wrapped:
                x_value = grid.x.get_value()
                select = x_value > 180.
                x_value[select] -= 360.

            grid.expand()

            x = grid.x.get_value()
            y = grid.y.get_value()

            if k.angular_units == OcgisUnits.RADIANS:
                x *= ConversionFactor.DEG_TO_RAD
                y *= ConversionFactor.DEG_TO_RAD

            z = np.ones(x.shape, dtype=x.dtype)

            desired = (x, y, z)
            try:
                as_cart = tp.transform_coordinates(k.other_crs, x, y, z)
            except CRSNotEquivalenError:
                self.assertNotEqual(k.other_crs, Cartesian())
                continue
            x_cart, y_cart, z_cart = as_cart

            for idx, ii in enumerate(as_cart):
                actual_min_max = [ii.min(), ii.max()]
                self.assertNumpyAllClose(np.array(actual_min_max),
                                         np.array(desired_min_maxes[idx]))

            actual = tp.transform_coordinates(k.other_crs,
                                              x_cart,
                                              y_cart,
                                              z_cart,
                                              inverse=True)

            for a, d in zip(actual, desired):
                are = np.abs(a - d)
                self.assertLessEqual(are.max(), 1e-6)
예제 #16
0
    def test_get_spatial_subset(self):

        ctr_test = 0
        for ss, k in self:
            for geometry_record in self.get_subset_geometries():
                for operation in ['intersects', 'clip', 'foo']:
                    # if ctr_test != 18:
                    #     ctr_test += 1
                    #     continue

                    if MPI_RANK == 0:
                        output_path = self.get_temporary_file_path(
                            'file-{}.nc'.format(ctr_test))
                    else:
                        output_path = None
                    output_path = MPI_COMM.bcast(output_path)

                    ctr_test += 1

                    use_geometry = deepcopy(geometry_record['geom'])
                    use_ss = deepcopy(ss)
                    try:
                        ret = use_ss.get_spatial_subset(operation,
                                                        use_geometry,
                                                        use_spatial_index=True,
                                                        buffer_value=None,
                                                        buffer_crs=None,
                                                        geom_crs=WGS84())
                    except ValueError:
                        # 'foo' is not a valid type of subset operation.
                        if operation == 'foo':
                            continue
                        else:
                            raise
                    except EmptySubsetError:
                        try:
                            self.assertEqual(
                                list(k.target.keys())[0], 'lambert')
                            self.assertEqual(
                                geometry_record['properties']['DESC'],
                                'Germany')
                        except AssertionError:
                            self.assertEqual(
                                list(k.target.keys())[0], 'rotated_pole')
                            self.assertEqual(
                                geometry_record['properties']['DESC'],
                                'Nebraska')
                        continue
                    else:
                        self.assertIsInstance(ret, Field)
                        with vm.scoped_by_emptyable('write', ret):
                            if not vm.is_null:
                                ret.write(output_path)

        self.assertGreater(ctr_test, 5)
예제 #17
0
    def test_get_spatial_subset_circular_geometries(self):
        """Test circular geometries. They were causing wrapping errors."""

        geoms = TestGeom.get_geometry_dictionaries()
        rd = self.test_data.get_rd('cancm4_tas')
        buffered = [element['geom'].buffer(rd.get().grid.resolution * 2) for element in geoms]
        for buff in buffered:
            ss = SpatialSubsetOperation(rd.get(), wrap=False)
            gvar = GeometryVariable(value=buff, name='geom', dimensions='dim', crs=WGS84())
            ret = ss.get_spatial_subset('intersects', gvar)
            self.assertTrue(np.all(ret.grid.x.get_value() >= 0))
예제 #18
0
    def test_get_spatial_subset_rotated_pole(self):
        """Test a spatial subset with rotated pole data."""

        rd = self.fixture_rd_rotated_pole(
            **{KeywordArgument.ROTATED_POLE_PRIORITY: True})
        ss = SpatialSubsetOperation(rd.get())
        ret = ss.get_spatial_subset('intersects',
                                    self.germany['geom'],
                                    geom_crs=WGS84())
        self.assertEqual(ret.crs, rd.get().crs)
        self.assertAlmostEqual(ret.grid.get_value_stacked().mean(),
                               -2.1699999954751132)
예제 #19
0
    def test_init(self):
        # Test a field is always the parent of a grid.
        grid = self.get_gridxy()
        self.assertIsInstance(grid.parent, Field)

        crs = WGS84()
        grid = self.get_gridxy(crs=crs)
        self.assertIsInstance(grid, Grid)
        self.assertIn('x', grid.parent)
        self.assertIn('y', grid.parent)
        self.assertEqual(grid.crs, crs)
        self.assertEqual([dim.name for dim in grid.dimensions],
                         ['ydim', 'xdim'])
        self.assertEqual(grid.shape, (4, 3))
        self.assertTrue(grid.is_vectorized)
        self.assertEqual(grid.x.ndim, 1)
        self.assertEqual(grid.y.ndim, 1)

        # Test with different variable names.
        x = Variable(name='col', value=[1], dimensions='col')
        y = Variable(name='row', value=[2], dimensions='row')
        grid = Grid(x, y)
        assert_equal(grid.x.get_value(), [1])
        assert_equal(grid.y.get_value(), [2])

        # Test point and polygon representations.
        grid = self.get_gridxy(crs=WGS84())
        grid.set_extrapolated_bounds('x_bounds', 'y_bounds', 'bounds')
        targets = ['get_point', 'get_polygon']
        targets = [getattr(grid, t)() for t in targets]
        for t in targets:
            self.assertIsInstance(t, GeometryVariable)
        self.assertTrue(grid.is_vectorized)
        sub = grid[1, 1]
        targets = ['get_point', 'get_polygon']
        targets = [getattr(sub, t)() for t in targets]
        for t in targets:
            self.assertEqual(t.shape, (1, 1))
            self.assertIsInstance(t, GeometryVariable)
        self.assertTrue(grid.is_vectorized)
예제 #20
0
    def test_crs(self):
        """Test overloading by geometry and grid."""

        field = Field()
        self.assertIsNone(field.crs)

        geom = GeometryVariable(name='geom', value=[Point(1, 2)], dimensions='g', crs=Spherical())
        field = Field(geom=geom)
        self.assertEqual(field.crs, geom.crs)

        grid = self.get_gridxy_global(crs=Spherical())
        field = Field(grid=grid)
        self.assertEqual(field.crs, grid.crs)

        grid = self.get_gridxy_global(crs=Spherical())
        # Grid and field coordinate systems do not match.
        with self.assertRaises(ValueError):
            Field(grid=grid, crs=WGS84())

        geom = GeometryVariable(name='geom', value=[Point(1, 2)], dimensions='g', crs=Spherical())
        with self.assertRaises(ValueError):
            Field(geom=geom, crs=WGS84())

        geom = GeometryVariable(name='geom', value=[Point(1, 2)], dimensions='g')
        grid = self.get_gridxy_global()
        field = Field(geom=geom, grid=grid, crs=WGS84())
        self.assertEqual(field.crs, WGS84())
        self.assertEqual(field.geom.crs, WGS84())
        self.assertEqual(field.grid.crs, WGS84())

        g = self.get_gridxy_global()
        f = Field(grid=g, crs=Spherical())
        self.assertIn('standard_name', f.grid.x.attrs)
        self.assertIn('standard_name', f.grid.y.attrs)
예제 #21
0
파일: test_geom.py 프로젝트: NCPP/ocgis
    def test_init(self):
        # Test empty.
        gvar = GeometryVariable()
        self.assertEqual(gvar.dtype, object)

        gvar = self.get_geometryvariable()
        self.assertIsInstance(gvar.get_masked_value(), MaskedArray)
        self.assertEqual(gvar.ndim, 1)

        # The geometry variable should set itself as the representative geometry on its parent field if that parent does
        # not have a representative geometry set.
        self.assertIsNotNone(gvar.parent.geom)

        # Test with a parent that already has a geometry.
        field = Field()
        field.set_geom(GeometryVariable(name='empty'))
        gvar = self.get_geometryvariable(parent=field)
        self.assertEqual(field.geom.name, 'empty')
        self.assertIn(gvar.name, field)

        # Test passing a "crs".
        gvar = self.get_geometryvariable(crs=WGS84(),
                                         name='my_geom',
                                         dimensions='ngeom')
        self.assertEqual(gvar.crs, WGS84())

        # Test using lines.
        line1 = LineString([(0, 0), (1, 1)])
        line2 = LineString([(1, 1), (2, 2)])
        gvar = GeometryVariable(value=[line1, line2], dimensions='two')
        self.assertTrue(gvar.get_value()[1].almost_equals(line2))
        self.assertEqual(gvar.geom_type, line1.geom_type)
        lines = MultiLineString([line1, line2])
        lines2 = [lines, lines]
        for actual in [lines, lines2, lines]:
            gvar2 = GeometryVariable(value=actual, dimensions='ngeom')
            self.assertTrue(gvar2.get_value()[0].almost_equals(lines))
            self.assertEqual(gvar2.geom_type, lines.geom_type)
            self.assertTrue(gvar2.shape[0] > 0)
            self.assertIsNone(gvar2.get_mask())
예제 #22
0
파일: test_geom.py 프로젝트: NCPP/ocgis
    def test_system_spatial_averaging_from_file(self):
        rd_nc = self.test_data.get_rd('cancm4_tas')

        rd_shp = RequestDataset(self.path_state_boundaries)
        field_shp = rd_shp.get()

        actual = field_shp.dimension_map.get_variable(DMK.GEOM)
        self.assertIsNotNone(actual)
        actual = field_shp.dimension_map.get_dimension(DMK.GEOM)
        self.assertEqual(len(actual), 1)

        self.assertEqual(field_shp.crs, WGS84())

        try:
            index_geom = np.where(
                field_shp['STATE_NAME'].get_value() == 'Nebraska')[0][0]
        except IndexError:
            # Not found on rank.
            polygon_field = None
        else:
            polygon_field = field_shp.get_field_slice({'geom': index_geom})
        polygon_field = MPI_COMM.gather(polygon_field)
        if MPI_RANK == 0:
            for p in polygon_field:
                if p is not None:
                    polygon_field = p
                    break
        polygon_field = MPI_COMM.bcast(polygon_field)
        polygon_field.unwrap()
        polygon = polygon_field.geom.get_value()[0]

        field_nc = rd_nc.get()
        sub_field_nc = field_nc.get_field_slice({'time': slice(0, 10)})
        self.assertEqual(sub_field_nc['tas']._dimensions,
                         field_nc['tas']._dimensions)
        sub = sub_field_nc.grid.get_intersects(polygon)

        # When split across two processes, there are floating point summing differences.
        desired = {1: 2734.5195, 2: 2740.4014}
        with vm.scoped_by_emptyable('grid intersects', sub):
            if not vm.is_null:
                abstraction_geometry = sub.get_abstraction_geometry()
                sub.parent.add_variable(abstraction_geometry, force=True)
                unioned = abstraction_geometry.get_unioned(
                    spatial_average='tas')
                if unioned is not None:
                    tas = unioned.parent['tas']
                    self.assertFalse(tas.is_empty)
                    self.assertAlmostEqual(tas.get_value().sum(),
                                           desired[vm.size],
                                           places=4)
예제 #23
0
    def test_get_spatial_subset_rotated_pole(self):
        """Test a spatial subset with rotated pole data."""

        try:
            rd = self.fixture_rd_rotated_pole(**{KeywordArgument.ROTATED_POLE_PRIORITY: True})
            ss = SpatialSubsetOperation(rd.get())
            ret = ss.get_spatial_subset('intersects', self.germany['geom'], geom_crs=WGS84())
            self.assertEqual(ret.crs, rd.get().crs)
            self.assertAlmostEqual(ret.grid.get_value_stacked().mean(), -2.1699999954751132)
        except RuntimeError as e:
            if "HDF error" in str(e):
                raise SkipTest('HDF sometimes has trouble reading the dataset')
            else:
                raise
예제 #24
0
파일: test_geom.py 프로젝트: NCPP/ocgis
    def test_crs(self):
        crs = WGS84()
        gvar = GeometryVariable(crs=crs, name='var')
        self.assertEqual(gvar.crs, crs)
        self.assertIn(crs.name, gvar.parent)
        gvar.crs = None
        self.assertIsNone(gvar.crs)
        self.assertEqual(len(gvar.parent), 1)
        self.assertNotIn(crs.name, gvar.parent)

        # Test coordinate system is maintained.
        gvar = GeometryVariable(crs=crs, name='var')
        vc = VariableCollection(variables=gvar)
        self.assertIn(crs.name, vc)
예제 #25
0
    def test_as_field(self):
        """Test iteration returned as field objects."""

        select_ugid = [16, 17, 51]
        desired_data_variables = ('UGID', 'STATE_FIPS', 'ID', 'STATE_NAME', 'STATE_ABBR')
        sci = GeomCabinetIterator(key='state_boundaries', select_uid=select_ugid, as_field=True)

        for _ in range(2):
            ugids = []
            for field in sci:
                self.assertIsInstance(field, Field)
                self.assertEqual(field.geom.ugid.shape, (1,))
                self.assertEqual(get_variable_names(field.data_variables), desired_data_variables)
                self.assertEqual(field.crs, WGS84())
                ugids.append(field.geom.ugid.get_value()[0])
예제 #26
0
파일: test_vector.py 프로젝트: wk1984/ocgis
    def test_system_merge_geometries_across_shapefiles(self):
        geoms_to_union = []
        state_names = ('Nebraska', 'South Dakota', 'North Dakota')
        gci = GeomCabinetIterator(path=self.path_state_boundaries)
        for row in gci:
            if row['properties']['STATE_NAME'] in state_names:
                geoms_to_union.append(row['geom'])
        self.assertEqual(len(geoms_to_union), 3)
        unioned = cascaded_union(geoms_to_union)

        grid = create_gridxy_global()
        field = create_exact_field(grid, 'data', crs=WGS84())
        original_shape = field.grid.shape
        ops = OcgOperations(dataset=field, geom=unioned)
        ret = ops.execute()
        actual_shape = ret.get_element().grid.shape
        self.assertNotEqual(actual_shape, original_shape)
예제 #27
0
    def test_system_user_geometry_identifer_added(self):
        """Test geometry identifier is added for linked dataset geometry formats."""

        field = self.get_field(crs=WGS84())
        subset_geom = Point(field.grid.x.get_value()[0], field.grid.y.get_value()[0]).buffer(0.1)
        ops = OcgOperations(dataset=field, geom=subset_geom, output_format=constants.OutputFormatName.CSV_SHAPEFILE)
        ret = ops.execute()

        ugid_name = HeaderName.ID_SELECTION_GEOMETRY
        csv_field = RequestDataset(ret).get()

        self.assertIn(ugid_name, list(csv_field.keys()))
        shp_path = os.path.join(ops.dir_output, ops.prefix, 'shp', ops.prefix + '_gid.shp')
        shp_field = RequestDataset(shp_path).get()
        self.assertIn(ugid_name, list(shp_field.keys()))

        shp_path = os.path.join(ops.dir_output, ops.prefix, 'shp', ops.prefix + '_ugid.shp')
        shp_field = RequestDataset(shp_path).get()
        self.assertIn(ugid_name, list(shp_field.keys()))
예제 #28
0
    def test_check_fields_for_regridding(self):
        from ESMF import RegridMethod
        from ocgis.regrid.base import check_fields_for_regridding

        source = self.get_ofield()
        destination = deepcopy(source)

        # Test non-spherical coordinate systems.
        new_source = deepcopy(source)
        new_source.update_crs(WGS84())
        with self.assertRaises(RegriddingError):
            check_fields_for_regridding(new_source, destination)

        # Change coordinate systems to spherical.
        source.update_crs(Spherical())
        destination.update_crs(Spherical())

        # Test with different parameters of the sphere.
        new_source = deepcopy(source)
        new_source.update_crs(Spherical(semi_major_axis=100))
        with self.assertRaises(RegriddingError):
            check_fields_for_regridding(new_source, destination)

        # Test corners not available on all inputs #####################################################################

        # Test corners not on one of the sources
        new_source = deepcopy(source)

        new_source.grid.x.set_bounds(None)
        new_source.grid.y.set_bounds(None)

        self.assertFalse(new_source.grid.has_bounds)

        for regrid_method in ['auto', RegridMethod.CONSERVE]:
            if regrid_method == RegridMethod.CONSERVE:
                with self.assertRaises(CornersInconsistentError):
                    check_fields_for_regridding(new_source,
                                                destination,
                                                regrid_method=regrid_method)
            else:
                check_fields_for_regridding(new_source,
                                            destination,
                                            regrid_method=regrid_method)
예제 #29
0
    def test_get_intersects(self):
        subset = box(100.7, 39.71, 102.30, 42.30)
        desired_manual = [[[40.0, 40.0], [41.0, 41.0], [42.0, 42.0]],
                          [[101.0, 102.0], [101.0, 102.0], [101.0, 102.0]]]
        desired_manual = np.array(desired_manual)

        grid = self.get_gridxy(crs=WGS84())
        # self.write_fiona_htmp(grid, 'grid')
        # self.write_fiona_htmp(GeometryVariable(value=subset), 'subset')
        sub, sub_slc = grid.get_intersects(subset, return_slice=True)

        self.assertFalse(sub.has_allocated_point)
        self.assertFalse(sub.has_allocated_polygon)
        self.assertFalse(sub.has_allocated_abstraction_geometry)
        # self.write_fiona_htmp(sub, 'sub')
        self.assertEqual(sub_slc, (slice(0, 3, None), slice(0, 2, None)))
        self.assertNumpyAll(sub.get_value_stacked(), desired_manual)
        point = sub.get_point()
        self.assertEqual(point.crs, grid.crs)

        # Test masks are updated.
        grid = self.get_gridxy(with_xy_bounds=True, with_parent=True)
        for t in ['xbounds', 'ybounds']:
            self.assertIn(t, grid.parent)
        subset = 'Polygon ((100.81193771626298883 42.17577854671281301, 101.13166089965399408 42.21211072664360842, 101.34965397923876651 41.18754325259516236, 103.68944636678200766 41.34013840830451159, 103.63858131487890546 41.22387543252595776, 100.77560553633219342 41.08581314878893664, 100.81193771626298883 42.17577854671281301))'
        subset = wkt.loads(subset)
        sub = grid.get_intersects(subset, cascade=True)
        self.assertTrue(sub.get_mask().any())
        self.assertTrue(sub.get_abstraction_geometry().get_mask().any())
        mask_slice = {'ydim': slice(1, 2), 'xdim': slice(1, 3)}

        sub_member_variables = get_variable_names(sub.get_member_variables())
        for v in list(sub.parent.values()):
            if v.name in sub_member_variables and not isinstance(
                    v, GeometryVariable) and v.name != sub.mask_variable.name:
                self.assertIsNone(v.get_mask())
            else:
                self.assertTrue(v.get_mask().any())
                self.assertFalse(v.get_mask().all())
                actual = v[mask_slice].get_mask()
                self.assertTrue(np.all(actual))
예제 #30
0
    def test_keyword_dataset_esmf(self):
        """Test with operations on an ESMF Field."""

        from ocgis.regrid.base import get_esmf_field_from_ocgis_field

        efield = get_esmf_field_from_ocgis_field(self.get_field(nrlz=0, nlevel=0))
        output_format = OutputFormat.iter_possible()
        for kk in output_format:
            # Geojson may only be written with a spherical coordinate system.
            if kk == constants.OutputFormatName.GEOJSON:
                output_crs = WGS84()
            else:
                output_crs = None
            try:
                ops = OcgOperations(dataset=efield, output_format=kk, prefix=kk, output_crs=output_crs)
            except DefinitionValidationError:
                self.assertEqual(kk, constants.OutputFormatName.METADATA_JSON)
                continue
            ret = ops.execute()
            self.assertIsNotNone(ret)
        efield.destroy()