@@ -296,6 +296,78 @@ def get_headfile_precision(filename: Union[str, PathLike]):
296296 return result
297297
298298
299+ def get_concentration_file_type (filename : Union [str , PathLike ], precision ):
300+ """
301+ Method to check header and determine if the concentration file is a MT3D like
302+ file or a MF6 GWT like concentration file
303+
304+ Parameters
305+ ----------
306+ filename : str or PathLike
307+ Path of binary MODFLOW file to determine precision.
308+ precision : str
309+ double or single
310+
311+ Returns
312+ -------
313+ str
314+ Result will be ucn or head
315+
316+ """
317+ f = open (filename , "rb" )
318+ f .seek (0 , 2 )
319+ totalbytes = f .tell ()
320+ f .seek (0 , 0 ) # reset to beginning
321+ assert f .tell () == 0
322+ if totalbytes == 0 :
323+ raise ValueError (f"datafile error: file is empty: { filename } " )
324+
325+ floattype = "f4"
326+ if precision == "double" :
327+ floattype = "f8"
328+
329+ # first try mt3d ucn
330+ vartype = [
331+ ("ntrans" , "i4" ),
332+ ("kstp" , "i4" ),
333+ ("kper" , "i4" ),
334+ ("totim" , floattype ),
335+ ("text" , "S16" ),
336+ ]
337+ hdr = binaryread (f , vartype )
338+
339+ try :
340+ s = hdr [0 ][4 ].decode ()
341+ if not s .strip ().lower ().startswith ("c" ):
342+ success = False
343+ else :
344+ success = True
345+ result = "ucn"
346+ except ValueError :
347+ success = False
348+
349+ if not success :
350+ f .seek (0 )
351+ vartype = [
352+ ("kstp" , "<i4" ),
353+ ("kper" , "<i4" ),
354+ ("pertim" , floattype ),
355+ ("totim" , floattype ),
356+ ("text" , "S16" ),
357+ ]
358+ hdr = binaryread (f , vartype )
359+ s = hdr [0 ][4 ].decode ()
360+ if not s .strip ().lower ().startswith ("c" ):
361+ f .close ()
362+ raise ValueError (
363+ f"Could not determine the header type of the concentration { filename } "
364+ )
365+ else :
366+ result = "head"
367+
368+ return result
369+
370+
299371class BinaryLayerFile (LayerFile ):
300372 """
301373 The BinaryLayerFile class is a parent class from which concrete
@@ -699,7 +771,8 @@ def __init__(
699771 precision = get_headfile_precision (filename )
700772 if precision == "unknown" :
701773 raise ValueError (f"Error. Precision could not be determined for { filename } " )
702- self .header_dtype = BinaryHeader .set_dtype (bintype = "Ucn" , precision = precision )
774+ bintype = get_concentration_file_type (filename , precision )
775+ self .header_dtype = BinaryHeader .set_dtype (bintype = bintype , precision = precision )
703776 super ().__init__ (filename , precision , verbose , ** kwargs )
704777 return
705778
0 commit comments