Ejemplo n.º 1
0
    def test_1d(self):
        nc_file = self.temp_file_name(prefix='structured.1d.', suffix='.nc')

        field = StructuredField((1, 2, 4, 5), (4, ),
                                indexing='ij',
                                units=('m', ))
        data = np.arange(4.)
        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.assertTrue(os.path.isfile(nc_file))

        try:
            root = self.open_as_netcdf(nc_file)
        except Exception:
            raise AssertionError('%s: Could not open' % nc_file)
        else:
            self.assertDataVariableNames(root,
                                         ['mesh', 'Elevation', 'x', 'time'])
            self.assertDimensionsEqual(root, ['x', 'time'])
            self.assertDataVariableArrayEqual(root, 'x', [1, 2, 4, 5])

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

            self.assertDataVariableUnitsEqual(root, 'x', 'm')
        finally:
            root.close()
Ejemplo n.º 2
0
    def test_1d(self):
        vtk_file = self.vtk_files["test_1d"]

        field = StructuredField((1, 2, 4, 5), (4,), indexing="ij", units=("m",))
        data = np.arange(4.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.º 3
0
    def test_1d(self):
        vtk_file = self.vtk_files['test_1d']

        field = StructuredField((1, 2, 4, 5), (4, ), indexing='ij',
                                units=('m', ))
        data = np.arange(4.)
        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.º 4
0
    def test_1d(self):
        vtk_file = self.vtk_files["test_1d"]

        field = StructuredField((1, 2, 4, 5), (4, ),
                                indexing="ij",
                                units=("m", ))
        data = np.arange(4.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.º 5
0
    def test_2d(self):
        nc_file = self.temp_file_name(prefix='structured.2d.', suffix='.nc')

        field = StructuredField((1, 1, 4, 4, 5, 5), (2, 3, 2, 3, 2, 3), (3, 2),
                                indexing='ij',
                                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))

        try:
            root = self.open_as_netcdf(nc_file)
        except Exception:
            raise AssertionError('%s: Could not open' % nc_file)
        else:
            self.assertDataVariableNames(root, [
                'mesh', 'Temperature', 'Elevation', 'Velocity', 'Temp', 'x',
                'y', 'time'
            ])
            self.assertDimensionsEqual(root, ['x', 'y', 'time'])
            self.assertDataVariableArrayEqual(
                root, 'x', np.array([[2., 3.], [2., 3.], [2., 3.]]))
            self.assertDataVariableArrayEqual(
                root, 'y', np.array([[1., 1.], [4., 4.], [5., 5.]]))

            for name in ['Temperature', 'Elevation', 'Velocity', 'Temp']:
                self.assertDataVariableLongNameEqual(root, name, name)
            for (v, u) in dict(Temperature='C',
                               Elevation='meters',
                               Velocity='m/s',
                               Temp='F').items():
                self.assertDataVariableUnitsEqual(root, v, u)
        finally:
            root.close()
Ejemplo n.º 6
0
    def test_2d(self):
        vtk_file = self.vtk_files['test_2d']

        field = StructuredField((1, 4, 5, 1, 4, 5), (2, 3, 2, 3, 2, 3),
                                (3, 2), indexing='ij', 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, vtk_file, attrs=attrs, append=True)

        self.assertTrue(os.path.isfile(vtk_file))
Ejemplo n.º 7
0
    def test_2d(self):
        vtk_file = self.vtk_files["test_2d"]

        field = StructuredField(
            (1, 4, 5, 1, 4, 5),
            (2, 3, 2, 3, 2, 3),
            (3, 2),
            indexing="ij",
            units=("m", "km"),
        )

        data = np.arange(6.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 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 new_structured(self, **kwds):
        ndims = kwds.pop('ndims', 1)
        shape = np.random.random_integers(2, 101, ndims)
        node_count = np.prod(shape)

        coords = []
        for size in shape:
            coords.append(np.cumsum((1. - np.random.random(size))))

        if len(coords) > 1:
            args = np.meshgrid(*coords, indexing='ij')
        else:
            args = coords
        args.append(shape)

        return StructuredField(*args, **kwds)
Ejemplo n.º 9
0
    def test_2d(self):
        vtk_file = self.vtk_files["test_2d"]

        field = StructuredField(
            (1, 4, 5, 1, 4, 5),
            (2, 3, 2, 3, 2, 3),
            (3, 2),
            indexing="ij",
            units=("m", "km"),
        )

        data = np.arange(6.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 nc file", author="Eric")
        field_tofile(field, vtk_file, attrs=attrs, append=True)

        self.assertTrue(os.path.isfile(vtk_file))
Ejemplo n.º 10
0
def test_structured(tmpdir, ndims):
    shape = np.random.randint(1, 101, ndims)
    coords = [np.arange(dim) for dim in shape]

    field = StructuredField(*np.meshgrid(*coords),
                            shape,
                            indexing="ij",
                            units=["m"] * ndims)

    data = np.arange(field.get_point_count())

    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="pytest")
    with tmpdir.as_cwd():
        field_tofile(field, "structured.nc", attrs=attrs, append=True)

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