Beispiel #1
0
    def test_dist_nldas_file(self):
        test = PFData(('NLDAS.APCP.000001_to_000024.pfb'))
        test.distFile(P=2,
                      Q=2,
                      R=1,
                      outFile=('NLDAS.APCP.000001_to_000024.pfb.tmp'))

        out_file = PFData(('NLDAS.APCP.000001_to_000024.pfb.tmp'))
        dist_file = open(('NLDAS.APCP.000001_to_000024.pfb.tmp.dist'), 'r')
        dist_lines = dist_file.readlines()
        [
            self.assertEqual(int(line.rstrip('\n')), val) for line, val in zip(
                dist_lines, [0, 84772, 165448, 246124, 322960])
        ]
        self.assertEqual(0, out_file.loadHeader(),
                         'should load distributed file header')
        self.assertEqual(0, out_file.loadData(),
                         'should load distributed data')
        self.assertIsNone(
            np.testing.assert_array_equal(test.getDataAsArray(),
                                          out_file.getDataAsArray()),
            'should find matching data values in original and distributed files'
        )
        test.close()
        out_file.close()
        dist_file.close()
        os.remove(('NLDAS.APCP.000001_to_000024.pfb.tmp'))
        os.remove(('NLDAS.APCP.000001_to_000024.pfb.tmp.dist'))
Beispiel #2
0
    def test_dist_file(self):
        test = PFData(('press.init.pfb'))
        test.distFile(P=2, Q=2, R=1, outFile=('press.init.pfb.tmp'))

        out_file = PFData(('press.init.pfb.tmp'))
        dist_file = open(('press.init.pfb.tmp.dist'), 'r')
        dist_lines = dist_file.readlines()
        [
            self.assertEqual(int(line.rstrip('\n')), val) for line, val in zip(
                dist_lines, [0, 176500, 344536, 512572, 672608])
        ]
        self.assertEqual(0, out_file.loadHeader(),
                         'should load distributed file header')
        self.assertEqual(0, out_file.loadData(),
                         'should load distributed data')
        self.assertIsNone(
            np.testing.assert_array_equal(test.getDataAsArray(),
                                          out_file.getDataAsArray()),
            'should find matching data values in original and distributed files'
        )
        test.close()
        out_file.close()
        dist_file.close()
        os.remove(('press.init.pfb.tmp'))
        os.remove(('press.init.pfb.tmp.dist'))
Beispiel #3
0
 def test_create_from_data(self):
     data = np.random.random_sample((50, 49, 31))
     test = PFData(data)
     self.assertEqual(31, test.getNX())
     self.assertEqual(49, test.getNY())
     self.assertEqual(50, test.getNZ())
     self.assertEqual(1, test.getP())
     self.assertEqual(1, test.getQ())
     self.assertEqual(1, test.getR())
     self.assertEqual(0, test.getX())
     self.assertEqual(0, test.getY())
     self.assertEqual(0, test.getZ())
     test.writeFile(('test_write_raw.pfb'))
     test_read = PFData(('test_write_raw.pfb'))
     test_read.loadHeader()
     test_read.loadData()
     self.assertEqual(0, test_read.getX())
     self.assertEqual(0, test_read.getY())
     self.assertEqual(0, test_read.getZ())
     self.assertEqual(31, test_read.getNX())
     self.assertEqual(49, test_read.getNY())
     self.assertEqual(50, test_read.getNZ())
     self.assertEqual(1, test_read.getP())
     self.assertEqual(1, test_read.getQ())
     self.assertEqual(1, test_read.getR())
     test_data = test_read.getDataAsArray()
     self.assertIsNone(
         np.testing.assert_array_equal(data, test_data),
         'Data written to array should exist in '
         'written PFB file.')
     del data
     test.close()
     test_read.close()
     os.remove(('test_write_raw.pfb'))
Beispiel #4
0
    def test_compare(self):
        test1 = PFData(('press.init.pfb'))
        test1.loadHeader()
        test1.loadData()

        test2 = PFData(('press.init.pfb'))
        test2.loadHeader()
        test2.loadData()

        self.assertEqual(PFData.differenceType_none,
                         test1.compare(test2)[0],
                         "test1 and test2 are the same")

        test1.setX(test1.getX() + 1.0)
        self.assertEqual(PFData.differenceType_x,
                         test1.compare(test2)[0], "The x values differ")
        test1.setX(test1.getX() - 1.0)

        arr = test1.getDataAsArray()
        arr[1][2][3] += 1.0
        ret, zyx = test1.compare(test2)
        self.assertEqual(PFData.differenceType_data, ret,
                         "The data values differ")
        self.assertEqual((1, 2, 3), zyx,
                         "The differing data's coordinates are correct")
        arr[1][2][3] -= 1.0

        test1.close()
        test2.close()
Beispiel #5
0
    def test_read_write_data(self):
        test = PFData(('press.init.pfb'))
        retval = test.loadHeader()
        self.assertEqual(0, retval, 'should load header of file that exists')
        retval = test.loadData()
        self.assertEqual(0, retval, 'should load data from valid file')
        retval = test.writeFile(('press.init.pfb.tmp'))
        self.assertEqual(0, retval,
                         'should write data from previously loaded file')

        data2 = PFData(('press.init.pfb.tmp'))
        data2.loadHeader()
        data2.loadData()
        self.assertIsNone(
            np.testing.assert_array_equal(
                test.getDataAsArray(), data2.getDataAsArray(),
                'should read back same values we wrote'))
        in_file_hash = calculate_sha1_hash(('press.init.pfb'))
        out_file_hash = calculate_sha1_hash(('press.init.pfb.tmp'))

        # This assertion (that the files are identical) is failing in Python and in C++
        # because the original test input file was written by a tool that incorrectly set the value
        self.assertNotEqual(
            in_file_hash, out_file_hash,
            'sha1 hash of input and output files should not match')

        same, byte_diff = byte_compare_files(('press.init.pfb'),
                                             ('press.init.pfb.tmp'))

        self.assertFalse(
            same, 'press.init.pfb should differ from version just written')
        self.assertEqual(92, byte_diff, 'first byte difference at byte 92')

        test.close()
        data2.close()
        os.remove(('press.init.pfb.tmp'))
Beispiel #6
0
def load_patch_matrix_from_pfb_file(file_name, layer=None):
    from parflowio.pyParflowio import PFData
    data = PFData(file_name)
    data.loadHeader()
    data.loadData()
    data_array = data.getDataAsArray()

    if data_array.ndim == 3:
        nlayer, nrows, ncols = data_array.shape
        if layer:
            nlayer = layer
        return data_array[nlayer - 1, :, :]

    elif data_array.ndim == 2:
        return data_array

    else:
        print(f'invalid PFB file: {file_name}')

    return
Beispiel #7
0
 def test_validate_cell_values(self):
     test = PFData(('press.init.pfb'))
     retval = test.loadHeader()
     self.assertEqual(0, retval, 'should load header of file that exists')
     retval = test.loadData()
     self.assertEqual(0, retval, 'should load data from valid file')
     data = test.getDataAsArray()
     self.assertIsNotNone(
         data, 'data from object should be available as python object')
     self.assertSequenceEqual((50, 41, 41), data.shape)
     self.assertAlmostEqual(98.003604098773, test(0, 0, 0), 12,
                            'valid data in cell (0,0,0)')
     self.assertAlmostEqual(97.36460429313328, test(40, 0, 0), 12,
                            'data in cell (40,0,0)')
     self.assertAlmostEqual(98.0043134691891, test(0, 1, 0), 12,
                            'data in cell (0, 1, 0)')
     self.assertAlmostEqual(98.00901307022781, test(1, 0, 0), 12,
                            'data in cell (1, 0, 0)')
     self.assertAlmostEqual(92.61370155558751, test(21, 1, 2), 12,
                            'data in cell (21, 1, 2)')
     self.assertAlmostEqual(7.98008728357588, test(0, 1, 45), 12,
                            'data in cell (0, 1, 45)')
     self.assertAlmostEqual(97.30205516102234, test(22, 1, 0), 12,
                            'valid data in cell (22,1,0)')
     self.assertEqual(test(0, 0, 0), data[0, 0, 0],
                      'data array and c array match values at (0,0,0)')
     self.assertEqual(test(40, 0, 0), data[0, 0, 40],
                      'data array and c array match values at (40,0,0)')
     self.assertEqual(test(0, 1, 0), data[0, 1, 0],
                      'data array and c array match values at (0,1,0)')
     self.assertEqual(test(1, 0, 0), data[0, 0, 1],
                      'data array and c array match values at (1,0,0)')
     self.assertEqual(test(21, 1, 2), data[2, 1, 21],
                      'data array and c array match values at (21,1,2)')
     self.assertEqual(test(0, 1, 45), data[45, 1, 0],
                      'data array and c array match values at (0,1,45)')
     self.assertEqual(test(22, 1, 0), data[0, 1, 22],
                      'data array and c array match values at (22,1,0)')
     test.close()
Beispiel #8
0
def read_file(infile):
    """read an input file and return a 3d numpy array

    Parameters
    ----------
    infile : str
        file to open (.pfb, .sa, .tif, .tiff)

    Returns
    -------
    res_arr : ndarray
        a 3d numpy array with data from file in (z,y,x) format with y axis 0 at bottom

    """
    infile_path = Path(infile)
    # get extension
    ext = infile_path.suffix
    file_string_path = os.fspath(infile_path)
    if ext in ['.tif', '.tiff']:
        res_arr = gdal.Open(file_string_path).ReadAsArray()
        if len(res_arr.shape) == 2:
            res_arr = res_arr[np.newaxis, ...]
        # flip y axis so tiff aligns with PFB native alignment
        res_arr = np.flip(res_arr, axis=1)
    elif ext == '.sa':  # parflow ascii file
        with open(file_string_path, 'r') as fi:
            header = fi.readline()
        nx, ny, nz = [int(x) for x in header.strip().split(' ')]
        arr = pd.read_csv(file_string_path, skiprows=1, header=None).values
        res_arr = np.reshape(arr, (nz, ny, nx))[:, :, :]
    elif ext == '.pfb':  # parflow binary file
        pfdata = PFData(file_string_path)
        pfdata.loadHeader()
        pfdata.loadData()
        res_arr = pfdata.getDataAsArray()
    else:
        raise ValueError('can not read file type ' + ext)

    return res_arr
Beispiel #9
0
 def test_manipulate_data(self):
     test = PFData(('press.init.pfb'))
     retval = test.loadHeader()
     self.assertEqual(0, retval, 'should load header of file that exists')
     retval = test.loadData()
     self.assertEqual(0, retval, 'should load data from valid file')
     test_data = test.getDataAsArray()
     self.assertSequenceEqual(
         (50, 41, 41), test_data.shape,
         'test file array should have shape (50,41,41)')
     self.assertAlmostEqual(98.003604098773, test(0, 0, 0), 12,
                            'valid data in cell (0,0,0)')
     test_data[0, 0, 0] = 1
     test_data[0, 0, 40] = 1
     test_data[2, 1, 21] = 1
     self.assertEqual(1, test(0, 0, 0),
                      'data update affects underlying array')
     self.assertEqual(1, test(40, 0, 0),
                      'data update affects underlying array')
     self.assertEqual(1, test(21, 1, 2),
                      'data update affects underlying array')
     retval = test.writeFile(('press.init.pfb.tmp'))
     self.assertEqual(0, retval,
                      'able to write updated data to output file')
     test_read = PFData(('press.init.pfb.tmp'))
     test_read.loadHeader()
     test_read.loadData()
     self.assertEqual(1, test_read(0, 0, 0),
                      'updates to data written to file can be read back')
     self.assertEqual(1, test_read(40, 0, 0),
                      'updates to data written to file can be read back')
     self.assertEqual(1, test_read(21, 1, 2),
                      'updates to data written to file can be read back')
     test.close()
     test_read.close()
     os.remove(('press.init.pfb.tmp'))
control2 = st.button(" Animate Results ")
if control2 == True:
    N = 100
    time = np.zeros(
        [N + 1])  # time array, we will probably want to swap with a date
    outflow = np.zeros([N + 1])  # array to load in the meterological forcing
    sat = np.zeros([N + 1, 300, 20])

    for icount in range(0, N):
        base = (base_dir + "/dunne_over/Dunne.out.satur.{:05d}.pfb")
        filename = base.format(icount)
        data_obj = PFData(filename)
        data_obj.loadHeader()
        data_obj.loadData()
        data_arr = data_obj.getDataAsArray()
        data_obj.close()
        sat[icount, :, :] = np.where(data_arr[:, 0, :] <= 0.0, 0.0,
                                     data_arr[:, 0, :])

    fig, ax = plt.subplots()
    image = st.pyplot(plt)
    for i in range(N):
        ax.cla()
        ax.imshow(sat[i, :, :],
                  vmin=0.1,
                  vmax=1.0,
                  origin='lower',
                  aspect=0.015,
                  cmap='Blues',
                  interpolation='none')  #,extent=[0,100,0,1])
Beispiel #11
0
    sandtank.ICPressure.GeomNames = 'domain'
    sandtank.Geom.domain.ICPressure.Value = 30.0
    sandtank.Geom.domain.ICPressure.RefGeom = 'domain'
    sandtank.Geom.domain.ICPressure.RefPatch = 'z_lower'
else:
    fname_ic = f'./sandtank.out.press.{"{:0>5}".format(StartNumber)}.pfb'
    print(f'Initial Conditions: {fname_ic}')
    sandtank.ICPressure.Type = 'PFBFile'
    sandtank.ICPressure.GeomNames = 'domain'
    sandtank.Geom.domain.ICPressure.FileName = fname_ic
    sandtank.dist(fname_ic)

# -----------------------------------------------------------------------------
# Run and Unload the n ParFlow output files
# -----------------------------------------------------------------------------

sandtank.dist('SandTank_Indicator.pfb')

sandtank.run()

pressure = np.zeros(RunLength + 1)

for i in range(RunLength + 1):
    press_out = PFData(
        get_absolute_path(f'sandtank_flux.out.press.{"{:0>5}".format(i)}.pfb'))
    press_out.loadHeader()
    press_out.loadData()
    press_out_data = press_out.getDataAsArray()
    pressure[i] = press_out_data[0, 0, 50]
    print(f'timestep: {i}; pressure: {pressure[i]}')
rich_fbx.Solver = 'Richards'
rich_fbx.Solver.MaxIter = 50000

rich_fbx.Solver.Nonlinear.MaxIter = 100
rich_fbx.Solver.Nonlinear.ResidualTol = 1e-6
rich_fbx.Solver.Nonlinear.EtaChoice = 'EtaConstant'
rich_fbx.Solver.Nonlinear.EtaValue = 1e-2
rich_fbx.Solver.Nonlinear.UseJacobian = True

rich_fbx.Solver.Nonlinear.DerivativeEpsilon = 1e-12

rich_fbx.Solver.Linear.KrylovDimension = 100

rich_fbx.Solver.Linear.Preconditioner = 'PFMG'

#-----------------------------------------------------------------------------
# Run and Unload the ParFlow output files
#-----------------------------------------------------------------------------

rich_fbx.run()

# From pythonio Tutorial:
FBx_press_out = PFData(get_absolute_path('richards_FBx.out.press.00010.pfb'))
FBx_press_out.loadHeader()
FBx_press_out.loadData()
FBx_press_out_data = FBx_press_out.getDataAsArray()

print(f'Dimensions of output file: {FBx_press_out_data.shape}')
print(FBx_press_out_data)