Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Based on the MF USG transport documentation, concentrations are saved with the same format as heads with headers conc01, conc02,... concNN

Therefore, I think this should work:
hds = fp.utils.binaryfile.HeadUFile(r'..\pht_usg_gwf.con',text='CONC01', verbose = True)
but is does not:

EOFError                                  Traceback (most recent call last)
Cell In[17], line 1
----> 1 hds = fp.utils.binaryfile.HeadUFile(r'..\pht_usg_gwf.con',text='CONC01', verbose = True)

File ~\AppData\Local\miniforge3\envs\gwmodel_2025\Lib\site-packages\flopy\utils\binaryfile\__init__.py:732, in HeadUFile.__init__(self, filename, text, precision, verbose, **kwargs)
    730         raise Exception()
    731 self.header_dtype = BinaryHeader.set_dtype(bintype="Head", precision=precision)
--> 732 super().__init__(filename, precision, verbose, **kwargs)

File ~\AppData\Local\miniforge3\envs\gwmodel_2025\Lib\site-packages\flopy\utils\binaryfile\__init__.py:314, in BinaryLayerFile.__init__(self, filename, precision, verbose, **kwargs)
    313 def __init__(self, filename: Union[str, os.PathLike], precision, verbose, **kwargs):
--> 314     super().__init__(filename, precision, verbose, **kwargs)

File ~\AppData\Local\miniforge3\envs\gwmodel_2025\Lib\site-packages\flopy\utils\datafile.py:217, in LayerFile.__init__(self, filename, precision, verbose, **kwargs)
    214     raise Exception(f"LayerFile error: unrecognized kwargs: {args}")
    216 # read through the file and build the pointer index
--> 217 self._build_index()
    219 # now that we read the data and know nrow and ncol,
    220 # we can make a generic mg if needed
    221 if self.mg is None:

File ~\AppData\Local\miniforge3\envs\gwmodel_2025\Lib\site-packages\flopy\utils\binaryfile\__init__.py:342, in BinaryLayerFile._build_index(self)
    340 ipos = 0
    341 while ipos < self.totalbytes:
--> 342     header = self._get_header()
    343     self.recordarray.append(header)
    344     if self.text.upper() not in header["text"]:

File ~\AppData\Local\miniforge3\envs\gwmodel_2025\Lib\site-packages\flopy\utils\binaryfile\__init__.py:399, in BinaryLayerFile._get_header(self)
    394 def _get_header(self):
    395     """
    396     Read the file header
    397 
    398     """
--> 399     header = binaryread(self.file, self.header_dtype, (1,))
    400     return header[0]

File ~\AppData\Local\miniforge3\envs\gwmodel_2025\Lib\site-packages\flopy\utils\binaryfile\__init__.py:210, in binaryread(file, vartype, shape, charlen)
    208 result = np.fromfile(file, vartype, nval)
    209 if result.size < nval:
--> 210     raise EOFError
    211 if nval != 1:
    212     result = np.reshape(result, shape)

EOFError: 

Any one done this before?

You must be logged in to vote

Found the answer here: #374
here is a generalization of that solution that dumps the whole mess to a 4d dataframe

'''
process concentrations
'''
print('processing concentrations...')

def get_all_concentrations(ucnobj, nlay, ncpl):
    """
    Returns a 4D array of concentration data from a MODFLOW USG Transport binary output file.
    
    Dimensions: (ncomp, ntimes, nlay, nnodes)
    
    Parameters
    ----------
    ucnobj : flopy.utils.binaryfile.HeadUFile
        A MF USG Transport binary file object containing concentration data.
    Returns
    -------
    conc_data : ndarray
        4D array of shape (ncomp, ntimes, nlay, nnodes)
    text_labels : list
        List of component t…

Replies: 2 comments · 2 replies

Comment options

Out of curiosity, does flopy.utils.binaryfile.UcnFile() work? If no, and the file is small, post it somewhere to take a deeper look.

You must be logged in to vote
1 reply
@RyanConway91
Comment options

No it does not

Comment options

Found the answer here: #374
here is a generalization of that solution that dumps the whole mess to a 4d dataframe

'''
process concentrations
'''
print('processing concentrations...')

def get_all_concentrations(ucnobj, nlay, ncpl):
    """
    Returns a 4D array of concentration data from a MODFLOW USG Transport binary output file.
    
    Dimensions: (ncomp, ntimes, nlay, nnodes)
    
    Parameters
    ----------
    ucnobj : flopy.utils.binaryfile.HeadUFile
        A MF USG Transport binary file object containing concentration data.
    Returns
    -------
    conc_data : ndarray
        4D array of shape (ncomp, ntimes, nlay, nnodes)
    text_labels : list
        List of component text labels used for each component.
    times : list
        List of simulation times used for each timestep.
    """
    times = ucnobj.get_times()
    text_labels = sorted(set(ucnobj.recordarray['text']))
    ncomp = len(text_labels)
    ntimes = len(times)
    conc_data = np.zeros((ncomp, ntimes, nlay, ncpl), dtype=ucnobj.realtype)

    for icomp, text in enumerate(text_labels):
        for itime, totim in enumerate(times):
            indices = np.where(
                (ucnobj.recordarray["totim"] == totim) & 
                (ucnobj.recordarray["text"] == text)
            )[0]
            for idx in indices:
                ilay = ucnobj.recordarray["ilay"][idx] - 1
                ipos = ucnobj.iposarray[idx]
                ucnobj.file.seek(ipos, 0)
                conc_layer = bf.binaryread(ucnobj.file, ucnobj.realtype, shape=(ncpl,))
                conc_data[icomp, itime, ilay, :] = conc_layer

    return conc_data, text_labels, times

ucnobj = fp.utils.binaryfile.HeadUFile('..\pht_usg_gwf.con', text='conc')
conc_data, components, times = get_all_concentrations(ucnobj,nlay,ncpl)
You must be logged in to vote
1 reply
@mwtoews
Comment options

mwtoews Jul 27, 2025
Collaborator

Ah, setting text='conc' seems to be the fix.

Answer selected by RyanConway91
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.