#NEWLINE# def parsenamefile( namfilename , packages , verbose = True ) : #NEWLINE# #INDENT# '\n    Function to parse the nam file and return a dictionary with types,\n    names, units and handles\n\n    Parameters\n    ----------\n    namefilename : string\n        Name of the MODFLOW namefile to parse.\n    packages : dictionary\n        Dictionary of package objects as defined in the `mfnam_packages`\n        attribute of :class:`flopy.modflow.mf.Modflow`.\n    verbose : logical\n        Print messages to screen.  Default is True.\n\n    Returns\n    ----------\n    ext_unit_dict : dictionary\n        For each file listed in the name file, a\n        :class:`flopy.utils.mfreadnam.NamData` instance\n        is stored in the ext_unit_dict dictionary keyed by unit number\n    ' #NEWLINE# if ( not os . path . isfile ( namfilename ) ) : #NEWLINE# #INDENT# if ( namfilename [ ( - 4 ) : ] . lower ( ) != '.nam' ) : #NEWLINE# #INDENT# namfilename += '.nam' #NEWLINE# #UNINDENT# #UNINDENT# ext_unit_dict = dict ( ) #NEWLINE# if verbose : #NEWLINE# #INDENT# print ( 'Parsing the namefile --> {0:s}' . format ( namfilename ) ) #NEWLINE# print ( 'Setting filehandles:' ) #NEWLINE# #UNINDENT# if ( not os . path . isfile ( namfilename ) ) : #NEWLINE# #INDENT# dn = os . path . dirname ( namfilename ) #NEWLINE# s = 'Could not find {} in path {} with files \n {}' . format ( namfilename , dn , os . listdir ( dn ) ) #NEWLINE# raise Exception ( s ) #NEWLINE# #UNINDENT# indata = open ( namfilename , 'r' ) . readlines ( ) #NEWLINE# for line in indata : #NEWLINE# #INDENT# tmp = line . strip ( ) . split ( ) #NEWLINE# if ( len ( tmp ) == 0 ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( '#' not in tmp [ 0 ] ) : #NEWLINE# #INDENT# if testint ( tmp [ 1 ] ) : #NEWLINE# #INDENT# fname = os . path . join ( os . path . dirname ( namfilename ) , tmp [ 2 ] ) #NEWLINE# if ( not os . path . isfile ( fname ) ) : #NEWLINE# #INDENT# dn = os . path . dirname ( fname ) #NEWLINE# fls = os . listdir ( dn ) #NEWLINE# lownams = [ f . lower ( ) for f in fls ] #NEWLINE# bname = os . path . basename ( fname ) #NEWLINE# if ( bname . lower ( ) in lownams ) : #NEWLINE# #INDENT# idx = lownams . index ( bname . lower ( ) ) #NEWLINE# fname = os . path . join ( dn , fls [ idx ] ) #NEWLINE# #UNINDENT# #UNINDENT# openmode = 'r' #NEWLINE# if ( tmp [ 0 ] . upper ( ) == 'DATA(BINARY)' ) : #NEWLINE# #INDENT# openmode = 'rb' #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# filehandle = open ( fname , openmode ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# if verbose : #NEWLINE# #INDENT# print ( 'could not set filehandle for {0:s}' . format ( tmp [ 2 ] ) ) #NEWLINE# #UNINDENT# filehandle = None #NEWLINE# #UNINDENT# key = int ( tmp [ 1 ] ) #NEWLINE# if ( key == 0 ) : #NEWLINE# #INDENT# ftype = tmp [ 0 ] . lower ( ) #NEWLINE# if ( ftype in packages ) : #NEWLINE# #INDENT# key = packages [ ftype ] . unitnumber #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# key = tmp [ 0 ] #NEWLINE# #UNINDENT# #UNINDENT# ext_unit_dict [ namfilename ] = NamData ( tmp [ 0 ] . upper ( ) , fname , filehandle , packages ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return ext_unit_dict