Beispiel #1
0
def test_io_sections_property():
    ds = DataStore(
        {
            'st': (['x', 'time'], np.ones((100, 5))),
            'ast': (['x', 'time'], np.ones((100, 5))),
            'probe1Temperature': (['time'], range(5)),
            'probe2Temperature': (['time'], range(5))
        },
        coords={
            'x': ('x', range(100), {
                'units': 'm'
            }),
            'time': range(5)
        })

    sections = {
        'probe1Temperature': [slice(7.5, 17.),
                              slice(70., 80.)],  # cold bath
        'probe2Temperature': [slice(24., 34.),
                              slice(85., 95.)],  # warm bath
    }
    ds['x'].attrs['units'] = 'm'

    ds.sections = sections

    # Create a temporary file to write data to.
    # 'with' method is used so the file is closed by tempfile
    # and free to be overwritten.
    with tempfile.NamedTemporaryFile('w') as tmp:
        temppath = tmp.name

    # Write the datastore to the temp file
    ds.to_netcdf(path=temppath)

    try:
        ds2 = open_datastore(temppath)
    except ValueError as e:
        if str(e) != 'cannot guess the engine, try passing one explicitly':
            raise
        else:
            warnings.warn('Could not guess engine, defaulted to netcdf4')
            ds2 = open_datastore(temppath, engine='netcdf4')

    assert ds.sections == ds2.sections

    # Close the datastore so the temp file can be removed
    ds2.close()
    ds2 = None

    # Remove the temp file once the test is done
    if os.path.exists(temppath):
        os.remove(temppath)

    pass
def test_to_mf_netcdf_open_mf_datastore():
    filepath = data_dir_single_ended
    ds = read_silixa_files(directory=filepath, file_ext='*.xml')

    with tempfile.TemporaryDirectory() as tmpdirname:
        print('created temporary directory', tmpdirname)

        # work around the effects of deafault encoding.
        path = os.path.join(tmpdirname, 'ds_merged.nc')
        ds.to_netcdf(path)
        ds.close()
        time.sleep(2)  # to ensure all is written on Windows and file released
        ds1 = open_datastore(path, load_in_memory=True)

        # Test saving
        ds1 = ds1.chunk({'time': 1})
        ds1.to_mf_netcdf(folder_path=tmpdirname,
                         filename_preamble='file_',
                         filename_extension='.nc')
        correct_val = float(ds1.st.sum())
        ds1.close()
        time.sleep(2)  # to ensure all is written on Windows and file released

        # Test loading
        path = os.path.join(tmpdirname, 'file_*.nc')
        ds2 = open_mf_datastore(path=path, load_in_memory=True)
        test_val = float(ds1.st.sum())

        np.testing.assert_equal(correct_val, test_val)
        ds2.close()

    pass
def test_io_sections_property():
    ds = DataStore({
        'st':    (['x', 'time'], np.ones((5, 5))),
        'ast':   (['x', 'time'], np.ones((5, 5))),
        'probe1Temperature':  (['time'], range(5)),
        'probe2Temperature':  (['time'], range(5))
        },
        coords={
            'x':    range(5),
            'time': range(5)})

    sections = {
        'probe1Temperature': [slice(7.5, 17.), slice(70., 80.)],  # cold bath
        'probe2Temperature': [slice(24., 34.), slice(85., 95.)],  # warm bath
        }

    ds.sections = sections
    with tempfile.NamedTemporaryFile() as tmp:
        ds.to_netcdf(path=tmp.name)

        ds2 = open_datastore(tmp.name)

        assert ds.sections == ds2.sections

    pass
def test_io_sections_property():
    ds = DataStore(
        {
            'st': (['x', 'time'], np.ones((100, 5))),
            'ast': (['x', 'time'], np.ones((100, 5))),
            'probe1Temperature': (['time'], range(5)),
            'probe2Temperature': (['time'], range(5))
        },
        coords={
            'x': ('x', range(100), {
                'units': 'm'
            }),
            'time': range(5)
        })

    sections = {
        'probe1Temperature': [slice(7.5, 17.),
                              slice(70., 80.)],  # cold bath
        'probe2Temperature': [slice(24., 34.),
                              slice(85., 95.)],  # warm bath
    }
    ds['x'].attrs['units'] = 'm'

    ds.sections = sections

    # Create a temporary file to write data to.
    # 'with' method is used so the file is closed by tempfile
    # and free to be overwritten.
    with tempfile.NamedTemporaryFile('w') as tmp:
        temppath = tmp.name

    # Write the datastore to the temp file
    ds.to_netcdf(path=temppath)

    ds2 = open_datastore(temppath)

    assert ds.sections == ds2.sections

    # Close the datastore so the temp file can be removed
    ds2.close()
    ds2 = None

    # Remove the temp file once the test is done
    if os.path.exists(temppath):
        os.remove(temppath)

    pass