modflowpy / flopy

A Python package to create, run, and post-process MODFLOW-based models.
https://flopy.readthedocs.io
Other
517 stars 313 forks source link

problem loading WEL file with "nwt" version, Windows, and python 2.7 #248

Closed hwreeves-USGS closed 7 years ago

hwreeves-USGS commented 7 years ago

in pakbase.py, there is a section of code that tests if the version is 'nwt' and the package is "WEL". It uses f.tell() and f.seek() to check if there is a 'specify' keyword for NWT input.

With Windows and Python 2.7; the f.tell(), f.seek() does not return to the same line and I am getting a load error for a simple well file. Brian tested MacOS python 3.6 and 2.7, and Windows python 3.6 and 2.7 - only 2.7 has trouble. Evidently it might be associated with using the f = open(file) as an iterator.

I'm not sure if there is a straightforward approach that would take care of the issue for Windows and 2.7 or if that techniques is used in other places in the code.

Note that if I change the if statement so I don't check for "nwt" and skip this section, then the files I have are read in fine and the model runs.

Here is the block of code: ``if "nwt" in model.version.lower() and 'flopy.modflow.mfwel.modflowwel'.lower() in str(pack_type).lower():

        specify = False
        ipos = f.tell()
        line = f.readline()
        # test for specify keyword if a NWT well file - This is a temporary hack
        if 'specify' in line.lower():
            specify = True
            t = line.strip().split()
            phiramp = np.float32(t[1])
            try:
                phiramp_unit = np.int32(t[2])
            except:
                phiramp_unit = 2
            options.append('specify {} {} '.format(phiramp, phiramp_unit))
        else:
            f.seek(ipos)
    elif 'flopy.modflow.mfchd.modflowchd'.lower() in str(
            pack_type).lower():
        partype = ['shead', 'ehead']``
jdhughes-usgs commented 7 years ago

I can't reproduce the problem on python 2.7. Could you send the files you are having a problem with.

jdhughes-usgs commented 7 years ago

Added conditional to test if python version is less than 3 and if windows os - if this is the case using io.open instead of open. This change should close the issue.