コード例 #1
0
def test_llc_facets_2d_to_compact(llc_mds_datadirs):
    from xmitgcm.utils import llc_facets_2d_to_compact, get_extra_metadata
    from xmitgcm.utils import rebuild_llc_facets, read_raw_data
    from xmitgcm.utils import write_to_binary
    from xmitgcm import open_mdsdataset

    dirname, expected = llc_mds_datadirs

    # open dataset
    ds = open_mdsdataset(dirname,
                         iters=expected['test_iternum'],
                         geometry=expected['geometry'])

    nt, nfaces, ny, nx = expected['shape']
    md = get_extra_metadata(domain=expected['geometry'], nx=nx)
    # split in facets
    facets = rebuild_llc_facets(ds['XC'], md)
    flatdata = llc_facets_2d_to_compact(facets, md)
    # compare with raw data
    raw = read_raw_data(dirname + '/XC.data', np.dtype('>f'), (nfaces, ny, nx))
    flatraw = raw.flatten()

    assert len(flatdata) == len(flatraw)
    assert flatdata.min() == flatraw.min()
    assert flatdata.max() == flatraw.max()

    # write new file
    write_to_binary(flatdata, 'tmp.bin', dtype=np.dtype('f'))
    md5new = file_md5_checksum('tmp.bin')
    md5old = file_md5_checksum(dirname + '/XC.data')
    assert md5new == md5old
    os.remove('tmp.bin')
コード例 #2
0
def test_write_to_binary(dtype):
    from xmitgcm.utils import write_to_binary
    import sys

    data = np.arange(2)
    # write
    write_to_binary(data, 'tmp.bin', dtype=dtype)
    # read
    if dtype == np.dtype('f'):
        tmp = np.fromfile('tmp.bin', '>f')
    elif dtype == np.dtype('d'):
        tmp = np.fromfile('tmp.bin', '>d')
    # check
    assert len(data) == len(tmp)
    assert data[0] == tmp[0]
    assert data[1] == tmp[1]
    os.remove('tmp.bin')