Ejemplo n.º 1
0
def test_3d():
    grid = RasterField((2, 3, 4), (1., 1., 1.), (0., 0., 0.))
    point_data = np.arange(grid.get_point_count())
    grid.add_field('var_0', point_data, centering='point')

    with cd_temp() as _:
        tofile('test-3d', grid)

        assert_true(os.path.isfile('test-3d.bov'))
        assert_true(os.path.isfile('test-3d.dat'))

        data = np.fromfile('test-3d.dat', dtype=int)
        assert_array_equal(point_data, data)

        with open('test-3d.bov', 'r') as fp:
            header = yaml.load(fp)

    assert_dict_equal(
        header, {
            'VARIABLE': 'var_0',
            'BRICK_SIZE': '2.0 3.0 4.0',
            'DATA_FILE': 'test-3d.dat',
            'DATA_SIZE': '2 3 4',
            'BRICK_ORIGIN': '0.0 0.0 0.0',
            'DATA_FORMAT': 'INT',
            'DATA_ENDIAN': sys.byteorder.upper(),
        })
Ejemplo n.º 2
0
    def test_2d_cell_fields(self):
        # Create a field that looks like this,
        #
        #    (0) --- (1) --- (2)
        #     |       |       |
        #     |   0   |   1   |
        #     |       |       |
        #    (3) --- (4) --- (5)

        g = RasterField((2, 3), (1, 2), (0, 0), indexing="ij")

        data = np.arange(6)
        g.add_field("point_var_1", data, centering="point")
        g.add_field("point_var_2", data * 2, centering="point")

        data = np.arange(2)
        g.add_field("cell_var_1", data, centering="zonal")
        g.add_field("cell_var_2", data * 2, centering="zonal")

        fields = g.get_cell_fields()
        self.assertEqual(len(fields), 2)
        self.assertTrue("cell_var_1" in fields)
        self.assertTrue("cell_var_2" in fields)

        self.assert_field_values(g, "cell_var_1", [0, 1])
        self.assert_field_values(g, "cell_var_2", [0, 2])
Ejemplo n.º 3
0
def test_3d(tmpdir):
    grid = RasterField((2, 3, 4), (1., 1., 1.), (0., 0., 0.))
    point_data = np.arange(grid.get_point_count())
    grid.add_field("var_0", point_data, centering="point")

    with tmpdir.as_cwd():
        tofile("test-3d", grid)

        assert os.path.isfile("test-3d.bov")
        assert os.path.isfile("test-3d.dat")

        data = np.fromfile("test-3d.dat", dtype=int)
        assert np.all(point_data == data)

        with open("test-3d.bov", "r") as fp:
            header = yaml.load(fp)

    assert header == {
        "VARIABLE": "var_0",
        "BRICK_SIZE": "2.0 3.0 4.0",
        "DATA_FILE": "test-3d.dat",
        "DATA_SIZE": "2 3 4",
        "BRICK_ORIGIN": "0.0 0.0 0.0",
        "DATA_FORMAT": "INT",
        "DATA_ENDIAN": sys.byteorder.upper(),
    }
Ejemplo n.º 4
0
    def test_2d(self):
        nc_file = self.temp_file_name(prefix='raster.2d.', suffix='.nc')
        field = RasterField((3, 2), (2., 1), (0, 0.5), units=('m', 'km'))

        data = np.arange(6.)

        field.add_field('Temperature', data * 10, centering='point', units='C')
        field.add_field('Elevation', data, centering='point', units='meters')
        field.add_field('Velocity', data * 100, centering='point', units='m/s')
        field.add_field('Temp', data * 2, centering='point', units='F')

        attrs = dict(description='Example nc file', author='Eric')
        field_tofile(field, nc_file, attrs=attrs, append=True)

        self.assertTrue(os.path.isfile(nc_file))

        root = self.open_as_netcdf(nc_file)
        self.assertDataVariableNames(root, [
            'mesh', 'Temperature', 'Elevation', 'Velocity', 'Temp', 'x', 'y',
            'time'
        ])
        self.assertDimensionsEqual(root, ['x', 'y', 'time'])
        self.assertDataVariableArrayEqual(root, 'x', [.5, 1.5])
        self.assertDataVariableArrayEqual(root, 'y', [0., 2., 4.])

        for name in ['Temperature', 'Elevation', 'Velocity', 'Temp']:
            self.assertDataVariableLongNameEqual(root, name, name)

        for (name, units) in [('Temperature', 'C'), ('Elevation', 'meters'),
                              ('Velocity', 'm/s'), ('Temp', 'F')]:
            self.assertDataVariableUnitsEqual(root, name, units)

        root.close()
Ejemplo n.º 5
0
    def test_1d(self):
        vtk_file = self.vtk_files['test_1d']
        field = RasterField((12, ), (1, ), (-1, ), indexing='ij', units=('m', ))
        data = np.arange(12.)
        field.add_field('Elevation', data, centering='point')

        attrs = dict(description='Example 1D nc file', author='Eric')
        field_tofile(field, vtk_file, attrs=attrs, append=True)

        self.assertTrue(os.path.isfile(vtk_file))
Ejemplo n.º 6
0
    def test_0d(self):
        vtk_file = self.vtk_files["test_0d"]
        field = RasterField((1, ), (0, ), (0, ), indexing="ij", units=("m", ))

        attrs = dict(description="Example 0D nc file", author="Eric")
        for i in range(10):
            field.add_field("Elevation", i * 10.0, centering="point")
            field_tofile(field, vtk_file, attrs=attrs, append=True)

        self.assertTrue(os.path.isfile(vtk_file))
Ejemplo n.º 7
0
    def test_1d(self):
        vtk_file = self.vtk_files["test_1d"]
        field = RasterField((12,), (1,), (-1,), indexing="ij", units=("m",))
        data = np.arange(12.0)
        field.add_field("Elevation", data, centering="point")

        attrs = dict(description="Example 1D nc file", author="Eric")
        field_tofile(field, vtk_file, attrs=attrs, append=True)

        self.assertTrue(os.path.isfile(vtk_file))
Ejemplo n.º 8
0
    def test_0d(self):
        vtk_file = self.vtk_files["test_0d"]
        field = RasterField((1,), (0,), (0,), indexing="ij", units=("m",))

        attrs = dict(description="Example 0D nc file", author="Eric")
        for i in range(10):
            field.add_field("Elevation", i * 10.0, centering="point")
            field_tofile(field, vtk_file, attrs=attrs, append=True)

        self.assertTrue(os.path.isfile(vtk_file))
Ejemplo n.º 9
0
    def test_2d_point_fields(self):
        # Create a field that looks like this,
        #
        #    (0) --- (1) --- (2)
        #     |       |       |
        #     |   0   |   1   |
        #     |       |       |
        #    (3) --- (4) --- (5)

        g = RasterField((2, 3), (1, 2), (0, 0), indexing="ij")

        data = np.arange(6)
        g.add_field("point_var_1", data, centering="point")
        g.add_field("point_var_2", data * 2, centering="point")

        data = np.arange(2)
        g.add_field("cell_var", data, centering="zonal")

        fields = g.get_point_fields()
        self.assertEqual(len(fields), 2)
        self.assertTrue("point_var_1" in fields)
        self.assertTrue("point_var_2" in fields)

        self.assert_field_values(g, "point_var_1", [0, 1, 2, 3, 4, 5])
        self.assert_field_values(g, "point_var_2", [0, 2, 4, 6, 8, 10])
Ejemplo n.º 10
0
    def test_2d_point_fields (self):
        # Create a field that looks like this,
        #
        #    (0) --- (1) --- (2)
        #     |       |       |
        #     |   0   |   1   |
        #     |       |       |
        #    (3) --- (4) --- (5)

        g = RasterField ((2,3), (1,2), (0, 0), indexing='ij')

        data = np.arange (6)
        g.add_field ('point_var_1', data, centering='point')
        g.add_field ('point_var_2', data*2, centering='point')

        data = np.arange (2)
        g.add_field ('cell_var', data, centering='zonal')

        fields = g.get_point_fields ()
        self.assertEqual (len (fields), 2)
        self.assertTrue (fields.has_key ('point_var_1'))
        self.assertTrue (fields.has_key ('point_var_2'))

        self.assert_field_values (g, 'point_var_1', [0, 1, 2, 3, 4, 5])
        self.assert_field_values (g, 'point_var_2', [0, 2, 4, 6, 8, 10])
Ejemplo n.º 11
0
def test_2d_constant_shape(tmpdir):
    # Create field and add some data to it that we will write to a
    # NetCDF file database.

    # nc_file = 'Elevation_time_series_0000.nc'
    nc_file = "2d_elevation_time_series.nc"

    data = np.arange(6.0)

    field = RasterField((2, 3), (1.0, 1.0), (0.0, 0.0), indexing="ij")
    field.add_field("Elevation", data, centering="point")

    # Create database of 'Elevation' values. Data are written to the
    # NetCDF file Elevation_time_series.nc.
    with tmpdir.as_cwd():
        db = Database()
        db.open(nc_file, "Elevation")
        db.write(field)

        assert os.path.isfile(nc_file)

        # Append data to the NetCDF file.
        data *= 2.0
        db.write(field)

        db.close()

        try:
            root = open_nc_file(nc_file)
        except Exception:
            raise AssertionError("%s: Could not open" % nc_file)
        else:
            assert list(root.dimensions.keys()) == ["y", "x", "time"]
            assert list(root.variables) == ["mesh", "y", "x", "Elevation", "time"]

            assert len(root.dimensions["x"]) == 3
            assert len(root.dimensions["y"]) == 2
            assert len(root.dimensions["time"]) == 2

            assert root.variables["Elevation"].shape == (2, 2, 3)

            assert root.variables["Elevation"][0].data == approx(
                np.arange(6.0).reshape(2, 3)
            )
            assert root.variables["Elevation"][1].data == approx(
                np.arange(6.0).reshape((2, 3)) * 2.0
            )

            assert root.variables["y"] == approx([0.0, 1.0])
            assert root.variables["x"] == approx([0.0, 1.0, 2.0])

            assert root.variables["Elevation"].long_name == "Elevation"
            assert root.variables["Elevation"].units == "-"

            root.close()
Ejemplo n.º 12
0
    def test_0d(self):
        vtk_file = self.vtk_files['test_0d']
        field = RasterField((1, ), (0, ), (0, ), indexing='ij',
                            units=('m', ))

        attrs = dict(description='Example 0D nc file', author='Eric')
        for i in range(10):
            field.add_field('Elevation', i * 10., centering='point')
            field_tofile(field, vtk_file, attrs=attrs, append=True)

        self.assertTrue(os.path.isfile(vtk_file))
Ejemplo n.º 13
0
def test_2d_constant_shape(tmpdir):
    # Create field and add some data to it that we will write to a
    # NetCDF file database.

    # nc_file = 'Elevation_time_series_0000.nc'
    nc_file = "2d_elevation_time_series.nc"

    data = np.arange(6.0)

    field = RasterField((2, 3), (1.0, 1.0), (0.0, 0.0), indexing="ij")
    field.add_field("Elevation", data, centering="point")

    # Create database of 'Elevation' values. Data are written to the
    # NetCDF file Elevation_time_series.nc.
    with tmpdir.as_cwd():
        db = Database()
        db.open(nc_file, "Elevation")
        db.write(field)

        assert os.path.isfile(nc_file)

        # Append data to the NetCDF file.
        data *= 2.0
        db.write(field)

        db.close()

        try:
            root = open_nc_file(nc_file)
        except Exception:
            raise AssertionError("%s: Could not open" % nc_file)
        else:
            assert list(root.dimensions.keys()) == ["y", "x", "time"]
            assert list(
                root.variables) == ["mesh", "y", "x", "Elevation", "time"]

            assert len(root.dimensions["x"]) == 3
            assert len(root.dimensions["y"]) == 2
            assert len(root.dimensions["time"]) == 2

            assert root.variables["Elevation"].shape == (2, 2, 3)

            assert root.variables["Elevation"][0].data == approx(
                np.arange(6.0).reshape(2, 3))
            assert root.variables["Elevation"][1].data == approx(
                np.arange(6.0).reshape((2, 3)) * 2.0)

            assert root.variables["y"] == approx([0.0, 1.0])
            assert root.variables["x"] == approx([0.0, 1.0, 2.0])

            assert root.variables["Elevation"].long_name == "Elevation"
            assert root.variables["Elevation"].units == "-"

            root.close()
Ejemplo n.º 14
0
def test_2d_constant_shape():
    #Create field and add some data to it that we will write to a
    # NetCDF file database.

    #nc_file = 'Elevation_time_series_0000.nc'
    nc_file = os.path.join(_TMP_DIR, '2d_elevation_time_series.nc')

    data = np.arange(6.)

    field = RasterField((2, 3), (1., 1.), (0., 0.), indexing='ij')
    field.add_field('Elevation', data, centering='point')

    #Create database of 'Elevation' values. Data are written to the
    # NetCDF file Elevation_time_series.nc.

    db = Database()
    db.open(nc_file, 'Elevation')
    db.write(field)

    assert_true(os.path.isfile(nc_file))

    # Append data to the NetCDF file.
    data *= 2.
    db.write(field)

    db.close()

    try:
        root = open_nc_file(nc_file)
    except Exception:
        raise AssertionError('%s: Could not open' % nc_file)
    else:
        assert_items_equal(['x', 'y', 'time'], root.dimensions.keys())
        assert_items_equal(['Elevation', 'x', 'y', 'time', 'mesh'],
                           root.variables)

        assert_equal(3, len(root.dimensions['x']))
        assert_equal(2, len(root.dimensions['y']))
        assert_equal(2, len(root.dimensions['time']))

        assert_tuple_equal((2, 2, 3), root.variables['Elevation'].shape)

        assert_array_equal(
            np.arange(6.).reshape(2, 3), root.variables['Elevation'][0])
        assert_array_equal(
            np.arange(6.).reshape((2, 3)) * 2., root.variables['Elevation'][1])

        assert_array_equal([0., 1.], root.variables['y'])
        assert_array_equal([0., 1., 2.], root.variables['x'])

        assert_equal('Elevation', root.variables['Elevation'].long_name)
        assert_equal('-', root.variables['Elevation'].units)

        root.close()
Ejemplo n.º 15
0
def test_clobber(tmpdir):
    grid = RasterField((12,), (1.,), (0.,))
    var_0_data = np.arange(grid.get_point_count())
    grid.add_field("var_0", var_0_data, centering="point")

    for fname in ["test-1d.bov", "test-1d.dat"]:
        with tmpdir.as_cwd():
            with open(fname, "w") as fp:
                fp.write("empty_file")
            with pytest.raises(FileExists):
                tofile("test-1d", grid, no_clobber=True)
            tofile("test-1d", grid)
Ejemplo n.º 16
0
def test_clobber():
    grid = RasterField((12, ), (1., ), (0., ))
    var_0_data = np.arange(grid.get_point_count())
    grid.add_field('var_0', var_0_data, centering='point')

    for fname in ['test-1d.bov', 'test-1d.dat']:
        with cd_temp() as _:
            with open(fname, 'w') as fp:
                fp.write('empty_file')
            with assert_raises(FileExists):
                tofile('test-1d', grid, no_clobber=True)
            tofile('test-1d', grid)
Ejemplo n.º 17
0
    def test_1d(self):
        vtk_file = self.vtk_files["test_1d"]
        field = RasterField((12, ), (1, ), (-1, ),
                            indexing="ij",
                            units=("m", ))
        data = np.arange(12.0)
        field.add_field("Elevation", data, centering="point")

        attrs = dict(description="Example 1D nc file", author="Eric")
        field_tofile(field, vtk_file, attrs=attrs, append=True)

        self.assertTrue(os.path.isfile(vtk_file))
Ejemplo n.º 18
0
    def test_raster_field_1d(self):
        g = RasterField((6,), (1.5,), (0.5,), indexing="ij")
        self.assert_point_count(g, 6)
        self.assert_cell_count(g, 5)

        point_data = np.arange(6.0)
        g.add_field("Point Data", point_data, centering="point")

        cell_data = np.arange(5.0) * 10.0
        g.add_field("Cell Data", cell_data, centering="zonal")

        self.assert_field_values(g, "Cell Data", [0.0, 10.0, 20.0, 30.0, 40.0])
        self.assert_field_values(g, "Point Data", [0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
Ejemplo n.º 19
0
    def test_raster_field_3d(self):
        g = RasterField((4, 3, 2), (1.5, 1.0, 3), (0.5, 0, -0.5), indexing="ij")
        self.assert_point_count(g, 24)
        self.assert_cell_count(g, 6)

        g.add_field("Point Data", g.get_x(), centering="point")

        cell_data = np.arange(6.0) * 10.0
        g.add_field("Cell Data", cell_data, centering="zonal")

        g.get_field("Cell Data")
        self.assert_field_values(g, "Cell Data", [0.0, 10.0, 20.0, 30.0, 40.0, 50.0])

        self.assert_field_values(
            g,
            "Point Data",
            [
                -0.5,
                2.5,
                -0.5,
                2.5,
                -0.5,
                2.5,
                -0.5,
                2.5,
                -0.5,
                2.5,
                -0.5,
                2.5,
                -0.5,
                2.5,
                -0.5,
                2.5,
                -0.5,
                2.5,
                -0.5,
                2.5,
                -0.5,
                2.5,
                -0.5,
                2.5,
            ],
        )

        self.assert_shape(g, (4, 3, 2))

        x = g.get_x()
        data = g.get_field("Point Data")

        self.assertEqual(x.size, data.size)
Ejemplo n.º 20
0
def test_2d_changing_shape():
    #Create field and add some data to it that we will write to a
    # NetCDF file database.

    nc_file = os.path.join(_TMP_DIR, 'Temperature_time_series.nc')

    data = np.arange(6.)

    field = RasterField((3, 2), (1., 1.), (0., 0.))
    field.add_field('Temperature', data, centering='point')

    db = Database()
    db.open(nc_file, 'Temperature')
    db.write(field)

    assert_true(os.path.isfile(nc_file))

    # Create a new field and write the data to the database. Since
    # the size of the field has changed, the data will be written
    # to a new file, Elevation_time_series_0000.nc.

    field = RasterField((3, 3), (1., 1.), (0., 0.))
    data = np.arange(9.)
    field.add_field('Temperature', data, centering='point')

    db.write(field)
    assert_true(
        os.path.isfile(
            os.path.join(_TMP_DIR, 'Temperature_time_series_0000.nc')))

    db.close()

    try:
        nc_file = os.path.join(_TMP_DIR, 'Temperature_time_series_0000.nc')
        root = open_nc_file(nc_file)
    except Exception:
        raise AssertionError('%s: Could not open' % nc_file)
    else:
        assert_items_equal(['x', 'y', 'time'], root.dimensions.keys())
        assert_items_equal(['Temperature', 'x', 'y', 'time', 'mesh'],
                           root.variables)

        assert_equal(3, len(root.dimensions['x']))
        assert_equal(3, len(root.dimensions['y']))
        assert_equal(1, len(root.dimensions['time']))

        assert_tuple_equal((1, 3, 3), root.variables['Temperature'].shape)

        assert_array_equal(
            np.arange(9.).reshape((3, 3)), root.variables['Temperature'][0])

        assert_array_equal([0., 1., 2.], root.variables['x'])
        assert_array_equal([0., 1., 2.], root.variables['y'])

        assert_equal('Temperature', root.variables['Temperature'].long_name)
        assert_equal('-', root.variables['Temperature'].units)

        root.close()
Ejemplo n.º 21
0
    def test_3d(self):
        vtk_file = self.vtk_files['test_3d']

        field = RasterField((2, 3, 4), (1, 2, 3), (-1, 0, 1),
                            indexing='ij', units=('mm', 'm', 'km'))

        data = np.arange(24.)
        field.add_field('Temperature', data * 10, centering='point', units='C')
        field.add_field('Elevation', data, centering='point', units='meters')
        field.add_field('Velocity', data * 100, centering='point', units='m/s')
        field.add_field('Temp', data * 2, centering='point', units='F')

        attrs = dict(description='Example 3D nc file', author='Eric')
        field_tofile(field, vtk_file, attrs=attrs, append=True)

        self.assertTrue(os.path.isfile(vtk_file))
Ejemplo n.º 22
0
def test_2d_changing_shape(tmpdir):
    # Create field and add some data to it that we will write to a
    # NetCDF file database.

    nc_file = "Temperature_time_series.nc"

    data = np.arange(6.0)

    field = RasterField((3, 2), (1.0, 1.0), (0.0, 0.0))
    field.add_field("Temperature", data, centering="point")

    with tmpdir.as_cwd():
        db = Database()
        db.open(nc_file, "Temperature")
        db.write(field)

        assert os.path.isfile(nc_file)

        # Create a new field and write the data to the database. Since
        # the size of the field has changed, the data will be written
        # to a new file, Elevation_time_series_0000.nc.

        field = RasterField((3, 3), (1.0, 1.0), (0.0, 0.0))
        data = np.arange(9.0)
        field.add_field("Temperature", data, centering="point")

        db.write(field)
        assert os.path.isfile("Temperature_time_series_0000.nc")

        db.close()

        nc_file = "Temperature_time_series_0000.nc"
        try:
            root = open_nc_file(nc_file)
        except Exception:
            raise AssertionError("%s: Could not open" % nc_file)
        else:
            assert list(root.dimensions.keys()) == ["y", "x", "time"]
            assert list(
                root.variables) == ["mesh", "y", "x", "Temperature", "time"]

            assert len(root.dimensions["x"]) == 3
            assert len(root.dimensions["y"]) == 3
            assert len(root.dimensions["time"]) == 1

            assert root.variables["Temperature"].shape == (1, 3, 3)
            assert root.variables["Temperature"][0].data == approx(
                np.arange(9.0).reshape((3, 3)))

            assert root.variables["x"] == approx([0.0, 1.0, 2.0])
            assert root.variables["y"] == approx([0.0, 1.0, 2.0])

            assert root.variables["Temperature"].long_name == "Temperature"
            assert root.variables["Temperature"].units == "-"

            root.close()
Ejemplo n.º 23
0
    def test_3d(self):
        vtk_file = self.vtk_files["test_3d"]

        field = RasterField((2, 3, 4), (1, 2, 3), (-1, 0, 1),
                            indexing="ij",
                            units=("mm", "m", "km"))

        data = np.arange(24.0)
        field.add_field("Temperature", data * 10, centering="point", units="C")
        field.add_field("Elevation", data, centering="point", units="meters")
        field.add_field("Velocity", data * 100, centering="point", units="m/s")
        field.add_field("Temp", data * 2, centering="point", units="F")

        attrs = dict(description="Example 3D nc file", author="Eric")
        field_tofile(field, vtk_file, attrs=attrs, append=True)

        self.assertTrue(os.path.isfile(vtk_file))
Ejemplo n.º 24
0
    def test_3d(self):
        nc_file = self.temp_file_name(prefix='raster.3d.', suffix='.nc')

        field = RasterField((2, 3, 4), (1, 2, 3), (-1, 0, 1),
                            indexing='ij',
                            units=('mm', 'm', 'km'))

        data = np.arange(24.)
        field.add_field('Temperature', data * 10, centering='point', units='C')
        field.add_field('Elevation', data, centering='point', units='meters')
        field.add_field('Velocity', data * 100, centering='point', units='m/s')
        field.add_field('Temp', data * 2, centering='point', units='F')

        attrs = dict(description='Example 3D nc file', author='Eric')
        field_tofile(field, nc_file, attrs=attrs, append=True)

        self.assertIsFile(nc_file)
Ejemplo n.º 25
0
    def new_raster(self, **kwds):
        import random
        ndims = kwds.pop('ndims', 1)
        shape = np.random.random_integers(2, 101, ndims)
        spacing = (1. - np.random.random(ndims)) * 100.
        origin = (np.random.random(ndims) - .5) * 100.

        return RasterField(shape, spacing, origin, **kwds)
Ejemplo n.º 26
0
def test_all_fields(tmpdir):
    grid = RasterField((12,), (1.,), (0.,))
    var_0_data = np.arange(grid.get_point_count())
    var_1_data = np.arange(grid.get_point_count()) * 10
    grid.add_field("var_0", var_0_data, centering="point")
    grid.add_field("var_1", var_1_data, centering="point")

    with tmpdir.as_cwd():
        tofile("test-1d", grid)

        assert os.path.isfile("test-1d_var_0.bov")
        assert os.path.isfile("test-1d_var_0.dat")
        assert os.path.isfile("test-1d_var_1.bov")
        assert os.path.isfile("test-1d_var_1.dat")

        data = np.fromfile("test-1d_var_0.dat", dtype=int)
        assert np.all(var_0_data == data)
        data = np.fromfile("test-1d_var_1.dat", dtype=int)
        assert np.all(var_1_data == data)
Ejemplo n.º 27
0
def test_all_fields():
    grid = RasterField((12, ), (1., ), (0., ))
    var_0_data = np.arange(grid.get_point_count())
    var_1_data = np.arange(grid.get_point_count()) * 10
    grid.add_field('var_0', var_0_data, centering='point')
    grid.add_field('var_1', var_1_data, centering='point')

    with cd_temp() as _:
        tofile('test-1d', grid)

        assert_true(os.path.isfile('test-1d_var_0.bov'))
        assert_true(os.path.isfile('test-1d_var_0.dat'))
        assert_true(os.path.isfile('test-1d_var_1.bov'))
        assert_true(os.path.isfile('test-1d_var_1.dat'))

        data = np.fromfile('test-1d_var_0.dat', dtype=int)
        assert_array_equal(var_0_data, data)
        data = np.fromfile('test-1d_var_1.dat', dtype=int)
        assert_array_equal(var_1_data, data)
Ejemplo n.º 28
0
    def test_3d(self):
        vtk_file = self.vtk_files["test_3d"]

        field = RasterField(
            (2, 3, 4), (1, 2, 3), (-1, 0, 1), indexing="ij", units=("mm", "m", "km")
        )

        data = np.arange(24.0)
        field.add_field("Temperature", data * 10, centering="point", units="C")
        field.add_field("Elevation", data, centering="point", units="meters")
        field.add_field("Velocity", data * 100, centering="point", units="m/s")
        field.add_field("Temp", data * 2, centering="point", units="F")

        attrs = dict(description="Example 3D nc file", author="Eric")
        field_tofile(field, vtk_file, attrs=attrs, append=True)

        self.assertTrue(os.path.isfile(vtk_file))
Ejemplo n.º 29
0
def test_options(tmpdir):
    grid = RasterField((12,), (1.,), (0.,))
    var_0_data = np.arange(grid.get_point_count())
    grid.add_field("var_0", var_0_data, centering="point")

    with tmpdir.as_cwd():
        tofile("test-1d", grid, options={"foo": "bar"})

        with open("test-1d.bov", "r") as fp:
            header = yaml.load(fp)

    assert header == {
        "VARIABLE": "var_0",
        "BRICK_SIZE": "12.0 1.0 1.0",
        "DATA_FILE": "test-1d.dat",
        "DATA_SIZE": "12 1 1",
        "BRICK_ORIGIN": "0.0 1.0 1.0",
        "DATA_FORMAT": "INT",
        "DATA_ENDIAN": sys.byteorder.upper(),
        "foo": "bar",
    }
Ejemplo n.º 30
0
    def test_1d(self):
        nc_file = self.temp_file_name(prefix='raster.1d.', suffix='.nc')
        field = RasterField((12, ), (1, ), (-1, ), units=('m', ))
        data = np.arange(12.)
        field.add_field('Elevation', data, centering='point')

        attrs = dict(description='Example 1D nc file', author='Eric')
        field_tofile(field, nc_file, attrs=attrs, append=True)

        self.assertIsFile(nc_file)

        root = self.open_as_netcdf(nc_file)
        self.assertDataVariableNames(root, ['mesh', 'Elevation', 'x', 'time'])
        self.assertDimensionsEqual(root, ['x', 'time'])
        self.assertDataVariableArrayEqual(root, 'x', np.arange(12.) - 1.)

        self.assertDataVariableLongNameEqual(root, 'Elevation', 'Elevation')
        self.assertDataVariableUnitsEqual(root, 'Elevation', '-')

        self.assertDataVariableUnitsEqual(root, 'x', 'm')
        root.close()
Ejemplo n.º 31
0
def test_options():
    grid = RasterField((12, ), (1., ), (0., ))
    var_0_data = np.arange(grid.get_point_count())
    grid.add_field('var_0', var_0_data, centering='point')

    with cd_temp() as _:
        tofile('test-1d', grid, options={'foo': 'bar'})

        with open('test-1d.bov', 'r') as fp:
            header = yaml.load(fp)

    assert_dict_equal(
        header, {
            'VARIABLE': 'var_0',
            'BRICK_SIZE': '12.0 1.0 1.0',
            'DATA_FILE': 'test-1d.dat',
            'DATA_SIZE': '12 1 1',
            'BRICK_ORIGIN': '0.0 1.0 1.0',
            'DATA_FORMAT': 'INT',
            'DATA_ENDIAN': sys.byteorder.upper(),
            'foo': 'bar',
        })
Ejemplo n.º 32
0
def test_raster(tmpdir, ndims):
    if ndims == 0:
        shape = (1, )
        ndims = 1
    else:
        shape = np.random.randint(1, 101, ndims)

    spacing = np.ones(ndims, dtype=float)
    origin = np.zeros(ndims, dtype=float)
    units = ["m"] * ndims

    field = RasterField(shape, spacing, origin, units=units)

    attrs = {
        "description":
        "Example {ndims}D nc file".format(ndims=ndims, author="pytest")
    }
    data = np.arange(field.get_point_count())

    with tmpdir.as_cwd():
        for i in range(10):
            field.add_field("Elevation",
                            data * i,
                            centering="point",
                            units="m")
            field_tofile(field, "raster.nc", attrs=attrs, append=True)

        assert os.path.isfile("raster.nc")

        ds = xarray.open_dataset("raster.nc")

        assert {"mesh", "Elevation", "time"}.issubset(ds.variables)
        assert "time" in ds.dims

        assert ds["Elevation"].long_name == "Elevation"
        assert ds["Elevation"].units == "m"
Ejemplo n.º 33
0
def test_bov_database(tmpdir):
    data = np.arange(6.)
    field = RasterField((3, 2), (1., 1.), (0., 0.))
    field.add_field("Elevation", data, centering="point")

    with tmpdir.as_cwd():
        db = Database()
        db.open("Bov_database.bov", "Elevation")

        # Write the field to the database. Since BOV files only
        # store one variable, append the variable name to the file name.

        db.write(field)
        assert os.path.isfile("Bov_database_0000.bov")

        data *= 2.
        db.write(field)
        assert os.path.isfile("Bov_database_0001.bov")

        data *= 2.
        db.write(field)
        assert os.path.isfile("Bov_database_0002.bov")

        db.close()
Ejemplo n.º 34
0
    def test_0d(self):
        nc_file = self.temp_file_name(prefix='raster.0d.', suffix='.nc')
        field = RasterField((1, ), (0, ), (0, ), units=('m', ))

        attrs = dict(description='Example 0D nc file', author='Eric')
        for i in range(10):
            field.add_field('Elevation', i * 10., centering='point')
            field_tofile(field, nc_file, attrs=attrs, append=True)

        self.assertIsFile(nc_file)

        root = self.open_as_netcdf(nc_file)

        self.assertDataVariableNames(root, ['mesh', 'Elevation', 'time'])
        self.assertDimensionsEqual(root, ['time'])

        self.assertDataVariableLongNameEqual(root, 'Elevation', 'Elevation')
        self.assertDataVariableUnitsEqual(root, 'Elevation', '-')

        self.assertDataVariableArrayEqual(root, 'Elevation',
                                          10. * np.arange(10))
        self.assertDataVariableArrayEqual(root, 'time', range(10))

        root.close()
Ejemplo n.º 35
0
def test_2d_changing_shape(tmpdir):
    # Create field and add some data to it that we will write to a
    # NetCDF file database.

    nc_file = "Temperature_time_series.nc"

    data = np.arange(6.0)

    field = RasterField((3, 2), (1.0, 1.0), (0.0, 0.0))
    field.add_field("Temperature", data, centering="point")

    with tmpdir.as_cwd():
        db = Database()
        db.open(nc_file, "Temperature")
        db.write(field)

        assert os.path.isfile(nc_file)

        # Create a new field and write the data to the database. Since
        # the size of the field has changed, the data will be written
        # to a new file, Elevation_time_series_0000.nc.

        field = RasterField((3, 3), (1.0, 1.0), (0.0, 0.0))
        data = np.arange(9.0)
        field.add_field("Temperature", data, centering="point")

        db.write(field)
        assert os.path.isfile("Temperature_time_series_0000.nc")

        db.close()

        nc_file = "Temperature_time_series_0000.nc"
        try:
            root = open_nc_file(nc_file)
        except Exception:
            raise AssertionError("%s: Could not open" % nc_file)
        else:
            assert list(root.dimensions.keys()) == ["y", "x", "time"]
            assert list(root.variables) == ["mesh", "y", "x", "Temperature", "time"]

            assert len(root.dimensions["x"]) == 3
            assert len(root.dimensions["y"]) == 3
            assert len(root.dimensions["time"]) == 1

            assert root.variables["Temperature"].shape == (1, 3, 3)
            assert root.variables["Temperature"][0].data == approx(
                np.arange(9.0).reshape((3, 3))
            )

            assert root.variables["x"] == approx([0.0, 1.0, 2.0])
            assert root.variables["y"] == approx([0.0, 1.0, 2.0])

            assert root.variables["Temperature"].long_name == "Temperature"
            assert root.variables["Temperature"].units == "-"

            root.close()
Ejemplo n.º 36
0
    def test_raster_field_1d(self):
        g = RasterField((6, ), (1.5, ), (0.5, ), indexing="ij")
        self.assert_point_count(g, 6)
        self.assert_cell_count(g, 5)

        point_data = np.arange(6.0)
        g.add_field("Point Data", point_data, centering="point")

        cell_data = np.arange(5.0) * 10.0
        g.add_field("Cell Data", cell_data, centering="zonal")

        self.assert_field_values(g, "Cell Data", [0.0, 10.0, 20.0, 30.0, 40.0])
        self.assert_field_values(g, "Point Data",
                                 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
Ejemplo n.º 37
0
    def test_raster_field_1d (self):
        g = RasterField ((6, ), (1.5, ), (0.5, ), indexing='ij')
        self.assert_point_count (g, 6)
        self.assert_cell_count (g, 5)

        point_data = np.arange (6.)
        g.add_field ('Point Data', point_data, centering='point')

        cell_data = np.arange (5.)*10.
        g.add_field ('Cell Data', cell_data, centering='zonal')

        self.assert_field_values (g, 'Cell Data',
                                  [0., 10., 20., 30., 40.])
        self.assert_field_values (g, 'Point Data',
                                  [0., 1., 2., 3., 4., 5.])
Ejemplo n.º 38
0
    def test_2d(self):
        # Create a field that looks like this,
        #
        #    (0) --- (1) --- (2)
        #     |       |       |
        #     |   0   |   1   |
        #     |       |       |
        #    (3) --- (4) --- (5)

        g = RasterField((2, 3), (1, 2), (0, 0), indexing="ij")

        self.assert_cell_count(g, 2)
        self.assert_point_count(g, 6)

        data = np.arange(6)
        g.add_field("var0", data, centering="point")

        self.assert_field_values(g, "var0", [0, 1, 2, 3, 4, 5])

        data = np.arange(6)
        data.shape = (2, 3)
        g.add_field("var0", data, centering="point")
        self.assert_field_values(g, "var0", [0, 1, 2, 3, 4, 5])

        # If the size or shape doesn't match, it's an error.
        with self.assertRaises(DimensionError):
            data = np.arange(2)
            g.add_field("bad var", data, centering="point")

        # DimensionError: (3, 2) != (2, 3)
        with self.assertRaises(DimensionError):
            data = np.ones((3, 2))
            g.add_field("bad var", data, centering="point")

        data = np.arange(2)
        g.add_field("var1", data, centering="zonal")
        self.assert_field_values(g, "var1", [0.0, 1.0])

        # DimensionError: (2, 1) != (1, 2)
        with self.assertRaises(DimensionError):
            data = np.ones((2, 1))
            g.add_field("bad var", data, centering="zonal")

        # DimensionError: 3 != 2
        with self.assertRaises(DimensionError):
            data = np.arange(3)
            g.add_field("bad var", data, centering="zonal")

        # DimensionError: 1 != 2
        with self.assertRaises(DimensionError):
            data = np.array(3)
            g.add_field("bad var", data, centering="zonal")
Ejemplo n.º 39
0
    def test_2d(self):
        # Create a field that looks like this,
        #
        #    (0) --- (1) --- (2)
        #     |       |       |
        #     |   0   |   1   |
        #     |       |       |
        #    (3) --- (4) --- (5)

        g = RasterField((2, 3), (1, 2), (0, 0), indexing="ij")

        self.assert_cell_count(g, 2)
        self.assert_point_count(g, 6)

        data = np.arange(6)
        g.add_field("var0", data, centering="point")

        self.assert_field_values(g, "var0", [0, 1, 2, 3, 4, 5])

        data = np.arange(6)
        data.shape = (2, 3)
        g.add_field("var0", data, centering="point")
        self.assert_field_values(g, "var0", [0, 1, 2, 3, 4, 5])

        # If the size or shape doesn't match, it's an error.
        with self.assertRaises(DimensionError):
            data = np.arange(2)
            g.add_field("bad var", data, centering="point")

        # DimensionError: (3, 2) != (2, 3)
        with self.assertRaises(DimensionError):
            data = np.ones((3, 2))
            g.add_field("bad var", data, centering="point")

        data = np.arange(2)
        g.add_field("var1", data, centering="zonal")
        self.assert_field_values(g, "var1", [0.0, 1.0])

        # DimensionError: (2, 1) != (1, 2)
        with self.assertRaises(DimensionError):
            data = np.ones((2, 1))
            g.add_field("bad var", data, centering="zonal")

        # DimensionError: 3 != 2
        with self.assertRaises(DimensionError):
            data = np.arange(3)
            g.add_field("bad var", data, centering="zonal")

        # DimensionError: 1 != 2
        with self.assertRaises(DimensionError):
            data = np.array(3)
            g.add_field("bad var", data, centering="zonal")