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

Instantiation of SpatialReference from mbase #92

Closed langevin-usgs closed 8 years ago

langevin-usgs commented 8 years ago

Jeremy,

The sr object is now instantiated from mbase, which is good, but since we don't have delc at this point, the constructor for SpatialReference sets delc to 1.

def __init__(self, delr=1.0, delc=1.0, lenuni=1, xul=None, yul=None,
             rotation=0.0, proj4_str="EPSG:4326"):
    self.delc = np.atleast_1d(np.array(delc))
    self.delr = np.atleast_1d(np.array(delr))

    self.lenuni = lenuni
    self.proj4_str = proj4_str
    self._reset()
    self.set_spatialreference(xul, yul, rotation)

delc is used to calculate yul so that the lower left corner of the grid is at 0, 0. Not sure how to fix this, but really, sr shouldn't be constructed until after dis has been added. Any thoughts?

You can see the problem with the following lines. Note that the upper left corner is (0, 1). The default when xul and yul are not specified should result in the upper left corner at (0, 100).

Lx = 100.
Ly = 100.
nlay = 1
nrow = 51
ncol = 51
delr = Lx / ncol
delc = Ly / nrow
top = 0
botm = [-1]
ms = flopy.modflow.Modflow(rotation=20.)
dis = flopy.modflow.ModflowDis(ms, nlay=nlay, nrow=nrow, ncol=ncol, delr=delr,
                               delc=delc, top=top, botm=botm)
print('yul: ', ms.sr.yul)
ms.plot()

image

jtwhite79 commented 8 years ago

I tried to overcome this problem by making the SR more dynamic and by protecting access to SR in the BaseModel. But you are right, if you access the SR before a DIS has been loaded, its junk. But we load the SR from name file header and we have to declare something about the SR in the BaseModel constructor -right?

I know this is a problem - I seem to remember telling Joe about this when he wanted to move the SR to the model level. I still think it belongs attached to the DIS since the DIS is what describes the spatial information and the DIS contains the critical information we need (nrow, ncol, delr, delc). But that would mean rolling back the code

jtwhite79 commented 8 years ago

Added some hacks to fix this problem.

jtwhite79 commented 8 years ago

My earlier hacks broke some other things, so I moved SR and start_datetime back to ModflowDis. They both still function as if they are attached to BaseModel, but if you try to access them before a DIS has been set, they appear as None. They are both still stored in the namfile comments. Let me know if this causes problems..I think it is a good compromise.

langevin-usgs commented 8 years ago

This has been working so I'm closing.