Esempio n. 1
0
 def test_move(self):
     data = np.random.random_sample((50, 49, 31))
     test = PFData(data)
     move = test.moveDataArray()
     self.assertTrue(
         np.array_equal(data, move),
         'Data obtained from PFData::moveDataArray must match given data')
     self.assertIsNone(
         test.viewDataArray(),
         'Calling PFData::moveDataArray must invalidate the internal data pointer'
     )
Esempio n. 2
0
    def _pfb_to_array(self, file_path):
        from parflowio.pyParflowio import PFData

        array = None
        if file_path:
            full_path = get_absolute_path(file_path)
            # FIXME do something with selector inside parflow-io
            pfb_data = PFData(full_path)
            pfb_data.loadHeader()
            pfb_data.loadData()
            array = pfb_data.moveDataArray()

        return array
Esempio n. 3
0
def pfread(pfbfile):
    """
    Read a pfb file and return data as an ndarray
    :param pfbfile: path to pfb file
    :return: An ndarray of ndim=3, with shape (nz, ny, nx)
    """
    if not os.path.exists(pfbfile):
        raise RuntimeError(f'{pfbfile} not found')

    pfb_data = PFData(pfbfile)
    pfb_data.loadHeader()
    pfb_data.loadData()
    arr = pfb_data.moveDataArray()
    pfb_data.close()
    assert arr.ndim == 3, 'Only 3D arrays are supported'
    return arr
Esempio n. 4
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.moveDataArray()
        pfdata.close()
        del pfdata
    else:
        raise ValueError('can not read file type ' + ext)

    return res_arr
Esempio n. 5
0
def read_array_pfb(file_name):
    from parflowio.pyParflowio import PFData
    data = PFData(file_name)
    data.loadHeader()
    data.loadData()
    return data.moveDataArray()