

def test_util2d():
    ml = flopy.modflow.Modflow()
    u2d = Util2d(ml, (10, 10), np.float32, 10.0, 'test')
    a1 = u2d.array
    a2 = (np.ones((10, 10), dtype=np.float32) * 10.0)
    assert np.array_equal(a1, a2)
    fname_ascii = os.path.join(out_dir, 'test_a.dat')
    fname_bin = os.path.join(out_dir, 'test_b.dat')
    np.savetxt(fname_ascii, a1, fmt='%15.6E')
    u2d.write_bin(a1.shape, fname_bin, a1, bintype='head')
    dis = flopy.modflow.ModflowDis(ml, 2, 10, 10)
    lpf = flopy.modflow.ModflowLpf(ml, hk=[fname_ascii, fname_bin])
    ml.lpf.hk[1].fmtin = '(BINARY)'
    assert np.array_equal(lpf.hk[0].array, a1)
    assert np.array_equal(lpf.hk[1].array, a1)
    ml = flopy.modflow.Modflow(model_ws='temp', external_path='ref')
    u2d = Util2d(ml, (10, 10), np.float32, 10.0, 'test')
    fname_ascii = os.path.join(out_dir, 'test_a.dat')
    fname_bin = os.path.join(out_dir, 'test_b.dat')
    np.savetxt(fname_ascii, a1, fmt='%15.6E')
    u2d.write_bin(a1.shape, fname_bin, a1, bintype='head')
    dis = flopy.modflow.ModflowDis(ml, 2, 10, 10)
    lpf = flopy.modflow.ModflowLpf(ml, hk=[fname_ascii, fname_bin])
    ml.lpf.hk[1].fmtin = '(BINARY)'
    assert np.array_equal(lpf.hk[0].array, a1)
    assert np.array_equal(lpf.hk[1].array, a1)
    fname = os.path.join(out_dir, 'test.bin')
    u2d.write_bin((10, 10), fname, u2d.array)
    a3 = u2d.load_bin((10, 10), fname, u2d.dtype)[1]
    assert np.array_equal(a3, a1)
    fname = os.path.join(out_dir, 'text.dat')
    u2d.write_txt((10, 10), fname, u2d.array)
    a4 = u2d.load_txt((10, 10), fname, u2d.dtype, '(FREE)')
    assert np.array_equal(a1, a4)
    data = np.arange(100).reshape(10, 10)
    u2d_arange = Util2d(ml, (10, 10), np.float32, data, 'test')
    u2d_arange.write_txt((10, 10), fname, u2d_arange.array, python_format=[7, '{0:10.4E}'])
    a4a = u2d.load_txt((10, 10), fname, np.float32, '(7E10.6)')
    assert np.array_equal(u2d_arange.array, a4a)
    a5 = u2d.array
    a5 += 1
    assert (not np.array_equal(a5, u2d.array))
    new_2d = (u2d * 2)
    assert np.array_equal(new_2d.array, (u2d.array * 2))
    u2d.cnstnt = 2.0
    a6 = u2d.array
    assert (not np.array_equal(a1, a6))
    u2d.write_txt((10, 10), fname, u2d.array)
    a7 = u2d.load_txt((10, 10), fname, u2d.dtype, '(FREE)')
    assert np.array_equal(u2d.array, a7)
    return

dataset/ETHPy150Open modflowpy/flopy/autotest/t004_test.py test_util2d/original