コード例 #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(),
        })
コード例 #2
0
def test_model_load():
    del_component_instances(['air_port'])
    with cd_temp() as _:
        os.mkdir('air')
        model = Model.load(AIR_PORT_CONTENTS)

        assert_equal(model.components, ['air_port'])
コード例 #3
0
def test_bad_grid():
    grid = RectilinearField((np.arange(12), ))
    var_0_data = np.arange(grid.get_point_count())
    grid.add_field('var_0', var_0_data, centering='point')

    with cd_temp() as _:
        with assert_raises(TypeError):
            tofile('test-1d', grid)
コード例 #4
0
def test_model_from_file():
    del_component_instances(['air_port'])
    with cd_temp() as _:
        os.mkdir('air')
        with open('components.yml', 'w') as fp:
            fp.write(AIR_PORT_CONTENTS)
        model = Model.from_file('components.yml')

        assert_equal(model.components, ['air_port'])
コード例 #5
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)
コード例 #6
0
def test_time_funcs():
    with cd_temp() as _:
        with open('params.yml', 'w') as fp:
            yaml.dump({'foo': 2, 'bar': 3.}, fp)
        c = ConstantScalars()
        c.initialize('params.yml')

    assert_almost_equal(c.get_start_time(), 0.)
    assert_almost_equal(c.get_current_time(), 0.)

    c.update_until(12.5)
    assert_almost_equal(c.get_current_time(), 12.5)
コード例 #7
0
def test_initialize():
    with cd_temp() as _:
        with open('params.yml', 'w') as fp:
            yaml.dump({'foo': 2, 'bar': 3.}, fp)
        c = ConstantScalars()
        c.initialize('params.yml')

    assert_list_equal(c.get_output_var_names(), ['foo', 'bar'])
    assert_list_equal(c.get_input_var_names(), [])

    assert_array_almost_equal(c.get_value('foo'), np.array(2.))
    assert_array_almost_equal(c.get_value('bar'), np.array(3.))
    with assert_raises(KeyError):
        c.get_value('baz')
コード例 #8
0
def test_grid_funcs():
    with cd_temp() as _:
        with open('params.yml', 'w') as fp:
            yaml.dump({'foo': 2, 'bar': 3.}, fp)
        c = ConstantScalars()
        c.initialize('params.yml')

    assert_equal(c.get_var_grid('foo'), 0)

    assert_array_almost_equal(c.get_grid_shape(0), (1, ))
    assert_array_almost_equal(c.get_grid_spacing(0), (1, ))
    assert_array_almost_equal(c.get_grid_origin(0), (0, ))

    with assert_raises(KeyError):
        c.get_var_grid('baz')
コード例 #9
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)
コード例 #10
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',
        })