-
Notifications
You must be signed in to change notification settings - Fork 303
/
t007_test.py
1689 lines (1389 loc) · 58.4 KB
/
t007_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Test export module
import sys
sys.path.append('..')
import copy
import glob
import os
import shutil
import numpy as np
import warnings
import flopy
pth = os.path.join('..', 'examples', 'data', 'mf2005_test')
namfiles = [namfile for namfile in os.listdir(pth) if namfile.endswith('.nam')]
# skip = ["MNW2-Fig28.nam", "testsfr2.nam", "testsfr2_tab.nam"]
skip = []
tpth = os.path.join('temp', 't007')
# make the directory if it does not exist
if not os.path.isdir(tpth):
os.makedirs(tpth)
npth = os.path.join('temp', 't007', 'netcdf')
# delete the directory if it exists
if os.path.isdir(npth):
shutil.rmtree(npth)
# make the directory
os.makedirs(npth)
spth = os.path.join('temp', 't007', 'shapefile')
# make the directory if it does not exist
if not os.path.isdir(spth):
os.makedirs(spth)
def remove_shp(shpname):
os.remove(shpname)
for ext in ['prj', 'shx', 'dbf']:
fname = shpname.replace('shp', ext)
if os.path.exists(fname):
os.remove(fname)
def export_mf6_netcdf(path):
print('in export_mf6_netcdf: {}'.format(path))
sim = flopy.mf6.modflow.mfsimulation.MFSimulation.load(sim_ws=path)
for name, model in sim.get_model_itr():
export_netcdf(model)
def export_mf2005_netcdf(namfile):
print('in export_mf2005_netcdf: {}'.format(namfile))
if namfile in skip:
return
m = flopy.modflow.Modflow.load(namfile, model_ws=pth, verbose=False)
if m.dis.lenuni == 0:
m.dis.lenuni = 1
# print('skipping...lenuni==0 (undefined)')
# return
# if sum(m.dis.laycbd) != 0:
if m.dis.botm.shape[0] != m.nlay:
print('skipping...botm.shape[0] != nlay')
return
assert m, 'Could not load namefile {}'.format(namfile)
msg = 'Could not load {} model'.format(namfile)
assert isinstance(m, flopy.modflow.Modflow), msg
export_netcdf(m)
def export_netcdf(m):
# Do not fail if netCDF4 not installed
try:
import netCDF4
import pyproj
except:
return
fnc = m.export(os.path.join(npth, m.name + '.nc'))
fnc.write()
fnc_name = os.path.join(npth, m.name + '.nc')
try:
fnc = m.export(fnc_name)
fnc.write()
except Exception as e:
msg = 'ncdf export fail for namfile {}:\n{} '.format(m.name, str(e))
raise Exception(msg)
try:
nc = netCDF4.Dataset(fnc_name, 'r')
except Exception as e:
msg = 'ncdf import fail for nc file {}:\n{}'.format(fnc_name, str(e))
raise Exception()
return
def export_shapefile(namfile):
print('in export_shapefile: {}'.format(namfile))
try:
import shapefile as shp
except:
return
m = flopy.modflow.Modflow.load(namfile, model_ws=pth, verbose=False)
assert m, 'Could not load namefile {}'.format(namfile)
msg = 'Could not load {} model'.format(namfile)
assert isinstance(m, flopy.modflow.Modflow), msg
fnc_name = os.path.join(spth, m.name + '.shp')
try:
fnc = m.export(fnc_name)
# fnc2 = m.export(fnc_name, package_names=None)
# fnc3 = m.export(fnc_name, package_names=['DIS'])
except Exception as e:
msg = 'shapefile export fail for namfile {}:\n{}'.format(namfile,
str(e))
raise Exception(msg)
try:
s = shp.Reader(fnc_name)
except Exception as e:
msg = 'shapefile import fail for {}:\n{}'.format(fnc_name, str(e))
raise Exception(msg)
msg = 'wrong number of records in shapefile {}:{:d}'.format(fnc_name,
s.numRecords)
assert s.numRecords == m.nrow * m.ncol, msg
return
def export_shapefile_modelgrid_override(namfile):
print('in export_modelgrid_override: {}'.format(namfile))
try:
import shapefile as shp
except:
return
from flopy.discretization import StructuredGrid
m = flopy.modflow.Modflow.load(namfile, model_ws=pth, verbose=False)
mg0 = m.modelgrid
modelgrid = StructuredGrid(mg0.delc * 0.3048, mg0.delr * 0.3048,
mg0.top, mg0.botm, mg0.idomain, mg0.lenuni,
mg0.epsg, mg0.proj4, xoff=mg0.xoffset,
yoff=mg0.yoffset, angrot=mg0.angrot)
assert m, 'Could not load namefile {}'.format(namfile)
assert isinstance(m, flopy.modflow.Modflow)
fnc_name = os.path.join(spth, m.name + '.shp')
try:
fnc = m.export(fnc_name, modelgrid=modelgrid)
# fnc2 = m.export(fnc_name, package_names=None)
# fnc3 = m.export(fnc_name, package_names=['DIS'])
except Exception as e:
msg = 'shapefile export fail for namfile {}:\n{}'.format(namfile,
str(e))
raise Exception(msg)
try:
s = shp.Reader(fnc_name)
except Exception as e:
msg = 'shapefile import fail for {}:{}'.format(fnc_name, str(e))
raise Exception(msg)
def test_output_helper_shapefile_export():
ws = os.path.join('..', 'examples', 'data', 'freyberg_multilayer_transient')
name = 'freyberg.nam'
ml = flopy.modflow.Modflow.load(name, model_ws=ws)
head = flopy.utils.HeadFile(os.path.join(ws, 'freyberg.hds'))
cbc = flopy.utils.CellBudgetFile(os.path.join(ws, "freyberg.cbc"))
flopy.export.utils.output_helper(os.path.join('temp', 'test.shp'), ml,
{'HDS': head, 'cbc': cbc},
mflay=1, kper=10)
def test_freyberg_export():
from flopy.discretization import StructuredGrid
namfile = 'freyberg.nam'
# steady state
model_ws = '../examples/data/freyberg'
m = flopy.modflow.Modflow.load(namfile, model_ws=model_ws,
check=False, verbose=False)
# test export at model, package and object levels
m.export('{}/model.shp'.format(spth))
m.wel.export('{}/wel.shp'.format(spth))
m.lpf.hk.export('{}/hk.shp'.format(spth))
m.riv.stress_period_data.export('{}/riv_spd.shp'.format(spth))
# transient
# (doesn't work at model level because the total size of
# the attribute fields exceeds the shapefile limit)
model_ws = '../examples/data/freyberg_multilayer_transient/'
m = flopy.modflow.Modflow.load(namfile, model_ws=model_ws, verbose=False,
load_only=['DIS', 'BAS6', 'NWT', 'OC',
'RCH',
'WEL',
'DRN',
'UPW'])
# test export without instantiating an sr
outshp = os.path.join(spth, namfile[:-4] + '_drn_sparse.shp')
m.drn.stress_period_data.export(outshp, sparse=True)
assert os.path.exists(outshp)
remove_shp(outshp)
m.modelgrid = StructuredGrid(delc=m.dis.delc.array,
delr=m.dis.delr.array,
epsg=5070)
# test export with an sr, regardless of whether or not wkt was found
m.drn.stress_period_data.export(outshp, sparse=True)
assert os.path.exists(outshp)
remove_shp(outshp)
m.modelgrid = StructuredGrid(delc=m.dis.delc.array,
delr=m.dis.delr.array,
epsg=3070)
# verify that attributes have same sr as parent
assert m.drn.stress_period_data.mg.epsg == m.modelgrid.epsg
assert m.drn.stress_period_data.mg.proj4 == m.modelgrid.proj4
assert m.drn.stress_period_data.mg.xoffset == m.modelgrid.xoffset
assert m.drn.stress_period_data.mg.yoffset == m.modelgrid.yoffset
assert m.drn.stress_period_data.mg.angrot == m.modelgrid.angrot
# if wkt text was fetched from spatialreference.org
if m.sr.wkt is not None:
# test default package export
outshp = os.path.join(spth, namfile[:-4] + '_dis.shp')
m.dis.export(outshp)
prjfile = outshp.replace('.shp', '.prj')
with open(prjfile) as src:
prjtxt = src.read()
assert prjtxt == m.sr.wkt
remove_shp(outshp)
# test default package export to higher level dir
outshp = os.path.join(spth, namfile[:-4] + '_dis.shp')
m.dis.export(outshp)
prjfile = outshp.replace('.shp', '.prj')
with open(prjfile) as src:
prjtxt = src.read()
assert prjtxt == m.sr.wkt
remove_shp(outshp)
# test sparse package export
outshp = os.path.join(spth, namfile[:-4] + '_drn_sparse.shp')
m.drn.stress_period_data.export(outshp,
sparse=True)
prjfile = outshp.replace('.shp', '.prj')
assert os.path.exists(prjfile)
with open(prjfile) as src:
prjtxt = src.read()
assert prjtxt == m.sr.wkt
remove_shp(outshp)
def test_export_output():
import os
import numpy as np
import flopy
# Do not fail if netCDF4 not installed
try:
import netCDF4
import pyproj
except:
return
model_ws = os.path.join("..", "examples", "data", "freyberg")
ml = flopy.modflow.Modflow.load("freyberg.nam", model_ws=model_ws)
hds_pth = os.path.join(model_ws, "freyberg.githds")
hds = flopy.utils.HeadFile(hds_pth)
out_pth = os.path.join(npth, "freyberg.out.nc")
nc = flopy.export.utils.output_helper(out_pth, ml,
{"freyberg.githds": hds})
var = nc.nc.variables.get("head")
arr = var[:]
ibound_mask = ml.bas6.ibound.array == 0
arr_mask = arr.mask[0]
assert np.array_equal(ibound_mask, arr_mask)
def test_write_shapefile():
from flopy.discretization import StructuredGrid
from flopy.export.shapefile_utils import shp2recarray
from flopy.export.shapefile_utils import write_grid_shapefile
sg = StructuredGrid(delr=np.ones(10) * 1.1,
# cell spacing along model rows
delc=np.ones(10) * 1.1,
# cell spacing along model columns
epsg=26715)
outshp = os.path.join(tpth, 'junk.shp')
write_grid_shapefile(outshp, sg, array_dict={})
# test that vertices aren't getting altered by writing shapefile
# check that pyshp reads integers
# this only check that row/column were recorded as "N"
# not how they will be cast by python or numpy
import shapefile as sf
sfobj = sf.Reader(outshp)
for f in sfobj.fields:
if f[0] == 'row' or f[0] == 'column':
assert f[1] == 'N'
recs = list(sfobj.records())
for r in recs[0]:
assert isinstance(r, int)
# check that row and column appear as integers in recarray
ra = shp2recarray(outshp)
assert np.issubdtype(ra.dtype['row'], np.integer)
assert np.issubdtype(ra.dtype['column'], np.integer)
try: # check that fiona reads integers
import fiona
with fiona.open(outshp) as src:
meta = src.meta
assert 'int' in meta['schema']['properties']['row']
assert 'int' in meta['schema']['properties']['column']
except:
pass
def test_shapefile_polygon_closed():
import os
import flopy
try:
import shapefile
except:
return
xll, yll = 468970, 3478635
xur, yur = 681010, 3716462
spacing = 2000
ncol = int((xur - xll) / spacing)
nrow = int((yur - yll) / spacing)
print(nrow, ncol)
m = flopy.modflow.Modflow("test.nam", proj4_str="EPSG:32614", xll=xll,
yll=yll)
flopy.modflow.ModflowDis(m, delr=spacing, delc=spacing, nrow=nrow,
ncol=ncol)
shp_file = os.path.join(spth, "test_polygon.shp")
m.dis.export(shp_file)
shp = shapefile.Reader(shp_file)
for shape in shp.iterShapes():
if len(shape.points) != 5:
raise AssertionError("Shapefile polygon is not closed!")
def test_export_array():
from flopy.export import utils
try:
from scipy.ndimage import rotate
except:
rotate = False
pass
namfile = 'freyberg.nam'
model_ws = '../examples/data/freyberg_multilayer_transient/'
m = flopy.modflow.Modflow.load(namfile, model_ws=model_ws, verbose=False,
load_only=['DIS', 'BAS6'])
m.modelgrid.set_coord_info(angrot=45)
nodata = -9999
utils.export_array(m.modelgrid, os.path.join(tpth, 'fb.asc'),
m.dis.top.array, nodata=nodata)
arr = np.loadtxt(os.path.join(tpth, 'fb.asc'), skiprows=6)
m.modelgrid.write_shapefile(os.path.join(tpth, 'grid.shp'))
# check bounds
with open(os.path.join(tpth, 'fb.asc')) as src:
for line in src:
if 'xllcorner' in line.lower():
val = float(line.strip().split()[-1])
# ascii grid origin will differ if it was unrotated
if rotate:
assert np.abs(val - m.modelgrid.extent[0]) < 1e-6
else:
assert np.abs(val - m.modelgrid.xoffset) < 1e-6
if 'yllcorner' in line.lower():
val = float(line.strip().split()[-1])
if rotate:
assert np.abs(val - m.modelgrid.extent[1]) < 1e-6
else:
assert np.abs(val - m.modelgrid.yoffset) < 1e-6
if 'cellsize' in line.lower():
val = float(line.strip().split()[-1])
rot_cellsize = np.cos(np.radians(m.modelgrid.angrot)) * \
m.modelgrid.delr[0] # * m.sr.length_multiplier
# assert np.abs(val - rot_cellsize) < 1e-6
break
rotate = False
rasterio = None
if rotate:
rotated = rotate(m.dis.top.array, m.modelgrid.angrot, cval=nodata)
if rotate:
assert rotated.shape == arr.shape
try:
# test GeoTIFF export
import rasterio
except:
pass
if rasterio is not None:
utils.export_array(m.modelgrid,
os.path.join(tpth, 'fb.tif'),
m.dis.top.array,
nodata=nodata)
with rasterio.open(os.path.join(tpth, 'fb.tif')) as src:
arr = src.read(1)
assert src.shape == (m.nrow, m.ncol)
# TODO: these tests currently fail -- fix is in progress
# assert np.abs(src.bounds[0] - m.modelgrid.extent[0]) < 1e-6
# assert np.abs(src.bounds[1] - m.modelgrid.extent[1]) < 1e-6
def test_mbase_modelgrid():
import numpy as np
import flopy
ml = flopy.modflow.Modflow(modelname="test", xll=500.0,
rotation=12.5, start_datetime="1/1/2016")
try:
print(ml.modelgrid.xcentergrid)
except:
pass
else:
raise Exception("should have failed")
dis = flopy.modflow.ModflowDis(ml, nrow=10, ncol=5, delr=np.arange(5))
assert ml.modelgrid.xoffset == 500
assert ml.modelgrid.yoffset == 0.0
assert ml.modelgrid.proj4 is None
ml.model_ws = tpth
ml.write_input()
ml1 = flopy.modflow.Modflow.load("test.nam", model_ws=ml.model_ws)
assert str(ml1.modelgrid) == str(ml.modelgrid)
assert ml1.start_datetime == ml.start_datetime
assert ml1.modelgrid.proj4 is None
def test_mt_modelgrid():
import numpy as np
import flopy
ml = flopy.modflow.Modflow(modelname="test", xll=500.0,
proj4_str='epsg:2193',
rotation=12.5, start_datetime="1/1/2016")
dis = flopy.modflow.ModflowDis(ml, nrow=10, ncol=5, delr=np.arange(5))
assert ml.modelgrid.xoffset == 500
assert ml.modelgrid.yoffset == 0.0
assert ml.modelgrid.epsg == 2193
assert ml.modelgrid.idomain is None
ml.model_ws = tpth
mt = flopy.mt3d.Mt3dms(modelname='test_mt', modflowmodel=ml,
model_ws=ml.model_ws, verbose=True)
assert mt.modelgrid.xoffset == ml.modelgrid.xoffset
assert mt.modelgrid.yoffset == ml.modelgrid.yoffset
assert mt.modelgrid.epsg == ml.modelgrid.epsg
assert mt.modelgrid.angrot == ml.modelgrid.angrot
assert np.array_equal(mt.modelgrid.idomain, ml.modelgrid.idomain)
# no modflowmodel
swt = flopy.seawat.Seawat(modelname='test_swt', modflowmodel=None,
mt3dmodel=None, model_ws=ml.model_ws,
verbose=True)
assert swt.modelgrid is swt.dis is swt.bas6 is None
# passing modflowmodel
swt = flopy.seawat.Seawat(modelname='test_swt', modflowmodel=ml,
mt3dmodel=mt, model_ws=ml.model_ws, verbose=True)
assert \
swt.modelgrid.xoffset == mt.modelgrid.xoffset == ml.modelgrid.xoffset
assert \
swt.modelgrid.yoffset == mt.modelgrid.yoffset == ml.modelgrid.yoffset
assert mt.modelgrid.epsg == ml.modelgrid.epsg == swt.modelgrid.epsg
assert mt.modelgrid.angrot == ml.modelgrid.angrot == swt.modelgrid.angrot
assert np.array_equal(mt.modelgrid.idomain, ml.modelgrid.idomain)
assert np.array_equal(swt.modelgrid.idomain, ml.modelgrid.idomain)
# bas and btn present
ibound = np.ones(ml.dis.botm.shape)
ibound[0][0:5] = 0
bas = flopy.modflow.ModflowBas(ml, ibound=ibound)
assert ml.modelgrid.idomain is not None
mt = flopy.mt3d.Mt3dms(modelname='test_mt', modflowmodel=ml,
model_ws=ml.model_ws, verbose=True)
btn = flopy.mt3d.Mt3dBtn(mt, icbund=ml.bas6.ibound.array)
# reload swt
swt = flopy.seawat.Seawat(modelname='test_swt', modflowmodel=ml,
mt3dmodel=mt, model_ws=ml.model_ws, verbose=True)
assert \
ml.modelgrid.xoffset == mt.modelgrid.xoffset == swt.modelgrid.xoffset
assert \
mt.modelgrid.yoffset == ml.modelgrid.yoffset == swt.modelgrid.yoffset
assert mt.modelgrid.epsg == ml.modelgrid.epsg == swt.modelgrid.epsg
assert mt.modelgrid.angrot == ml.modelgrid.angrot == swt.modelgrid.angrot
assert np.array_equal(mt.modelgrid.idomain, ml.modelgrid.idomain)
assert np.array_equal(swt.modelgrid.idomain, ml.modelgrid.idomain)
def test_free_format_flag():
import flopy
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)
bas = flopy.modflow.ModflowBas(ms, ifrefm=True)
assert ms.free_format_input == bas.ifrefm
ms.free_format_input = False
assert ms.free_format_input == bas.ifrefm
ms.free_format_input = True
bas.ifrefm = False
assert ms.free_format_input == bas.ifrefm
bas.ifrefm = True
assert ms.free_format_input == bas.ifrefm
ms.model_ws = tpth
ms.write_input()
ms1 = flopy.modflow.Modflow.load(ms.namefile, model_ws=ms.model_ws)
assert ms1.free_format_input == ms.free_format_input
assert ms1.free_format_input == ms1.bas6.ifrefm
ms1.free_format_input = False
assert ms1.free_format_input == ms1.bas6.ifrefm
bas.ifrefm = False
assert ms1.free_format_input == ms1.bas6.ifrefm
bas.ifrefm = True
assert ms1.free_format_input == ms1.bas6.ifrefm
def test_sr():
import flopy
m = flopy.modflow.Modflow("test", model_ws="./temp",
xll=12345, yll=12345,
proj4_str="test test test")
flopy.modflow.ModflowDis(m, 10, 10, 10)
m.sr.xll = 12345
m.sr.yll = 12345
m.write_input()
mm = flopy.modflow.Modflow.load("test.nam", model_ws="./temp")
if mm.sr.xul != 12345:
raise AssertionError()
if mm.sr.yul != 12355:
raise AssertionError()
if mm.sr.proj4_str != "test test test":
raise AssertionError()
def test_dis_sr():
import flopy
import numpy as np
delr = 640
delc = 640
nrow = np.ceil(59040. / delc).astype(int)
ncol = np.ceil(33128. / delr).astype(int)
nlay = 3
xul = 2746975.089
yul = 1171446.45
rotation = -39
bg = flopy.modflow.Modflow(modelname='base')
dis = flopy.modflow.ModflowDis(bg, nlay=nlay, nrow=nrow, ncol=ncol,
delr=delr, delc=delc, lenuni=1,
rotation=rotation, xul=xul, yul=yul,
proj4_str='epsg:2243')
if abs(dis.sr.xul - xul) > 0.01:
raise AssertionError()
if abs(dis.sr.yul - yul) > 0.01:
raise AssertionError()
def test_mg():
import flopy
from flopy.utils import geometry
Lx = 100.
Ly = 100.
nlay = 1
nrow = 51
ncol = 51
delr = Lx / ncol
delc = Ly / nrow
top = 0
botm = [-1]
ms = flopy.modflow.Modflow()
dis = flopy.modflow.ModflowDis(ms, nlay=nlay, nrow=nrow, ncol=ncol,
delr=delr, delc=delc, top=top, botm=botm)
bas = flopy.modflow.ModflowBas(ms, ifrefm=True)
# test instantiation of an empty basic Structured Grid
mg = flopy.discretization.StructuredGrid(dis.delc.array, dis.delr.array)
# test instantiation of Structured grid with offsets
mg = flopy.discretization.StructuredGrid(dis.delc.array, dis.delr.array,
xoff=1, yoff=1)
# txt = 'yul does not approximately equal 100 - ' + \
# '(xul, yul) = ({}, {})'.format( ms.sr.yul, ms.sr.yul)
assert abs(ms.modelgrid.extent[-1] - Ly) < 1e-3 # , txt
ms.modelgrid.set_coord_info(xoff=111, yoff=0)
assert ms.modelgrid.xoffset == 111
ms.modelgrid.set_coord_info()
xll, yll = 321., 123.
angrot = 20.
ms.modelgrid = flopy.discretization.StructuredGrid(delc=ms.dis.delc.array,
delr=ms.dis.delr.array,
xoff=xll, yoff=xll,
angrot=angrot,
lenuni=2)
# test that transform for arbitrary coordinates
# is working in same as transform for model grid
mg2 = flopy.discretization.StructuredGrid(delc=ms.dis.delc.array,
delr=ms.dis.delr.array,
lenuni=2)
x = mg2.xcellcenters[0]
y = mg2.ycellcenters[0]
mg2.set_coord_info(xoff=xll, yoff=yll, angrot=angrot)
xt, yt = geometry.transform(x, y, xll, yll, mg2.angrot_radians)
assert np.sum(xt - ms.modelgrid.xcellcenters[0]) < 1e-3
assert np.sum(yt - ms.modelgrid.ycellcenters[0]) < 1e-3
# test inverse transform
x0, y0 = 9.99, 2.49
x1, y1 = geometry.transform(x0, y0, xll, yll, angrot)
x2, y2 = geometry.transform(x1, y1, xll, yll, angrot, inverse=True)
assert np.abs(x2 - x0) < 1e-6
assert np.abs(y2 - y0) < 1e6
ms.start_datetime = "1-1-2016"
assert ms.start_datetime == "1-1-2016"
assert ms.dis.start_datetime == "1-1-2016"
ms.model_ws = tpth
ms.write_input()
ms1 = flopy.modflow.Modflow.load(ms.namefile, model_ws=ms.model_ws)
assert str(ms1.modelgrid) == str(ms.modelgrid)
assert ms1.start_datetime == ms.start_datetime
assert ms1.modelgrid.lenuni == ms.modelgrid.lenuni
# assert ms1.sr.lenuni != sr.lenuni
def test_epsgs():
import flopy.export.shapefile_utils as shp
# test setting a geographic (lat/lon) coordinate reference
# (also tests sr.crs parsing of geographic crs info)
delr = np.ones(10)
delc = np.ones(10)
sr = flopy.discretization.StructuredGrid(delr=delr, delc=delc)
sr.epsg = 102733
msg = 'sr.epsg is not 102733 ({})'.format(sr.epsg)
assert sr.epsg == 102733, msg
t_value = sr.__repr__()
msg = 'proj4_str:epsg:102733 not in sr.__repr__(): ({})'.format(t_value)
if not 'proj4_str:epsg:102733' in t_value:
raise AssertionError(msg)
sr.epsg = 4326 # WGS 84
crs = shp.CRS(epsg=4326)
assert crs.crs['proj'] == 'longlat'
t_value = crs.grid_mapping_attribs['grid_mapping_name']
msg = 'grid_mapping_name is not latitude_longitude: {}'.format(t_value)
assert t_value == 'latitude_longitude', msg
t_value = sr.__repr__()
msg = 'proj4_str:epsg:4326 not in sr.__repr__(): ({})'.format(t_value)
if not 'proj4_str:epsg:4326' in t_value:
raise AssertionError(msg)
def test_dynamic_xll_yll():
nlay, nrow, ncol = 1, 10, 5
delr, delc = 250, 500
xll, yll = 286.80, 29.03
# test scaling of length units
ms2 = flopy.modflow.Modflow()
dis = flopy.modflow.ModflowDis(ms2, nlay=nlay, nrow=nrow, ncol=ncol,
delr=delr,
delc=delc)
sr1 = flopy.utils.SpatialReference(delr=ms2.dis.delr.array,
delc=ms2.dis.delc.array, lenuni=2,
xll=xll, yll=yll, rotation=30)
xul, yul = sr1.xul, sr1.yul
sr1.length_multiplier = 1.0 / 3.281
msg = 'sr1.xll ({}) is not equal to {}'.format(sr1.xll, xll)
assert sr1.xll == xll, msg
msg = 'sr1.yll ({}) is not equal to {}'.format(sr1.yll, yll)
assert sr1.yll == yll, msg
sr2 = flopy.utils.SpatialReference(delr=ms2.dis.delr.array,
delc=ms2.dis.delc.array, lenuni=2,
xul=xul, yul=yul, rotation=30)
sr2.length_multiplier = 1.0 / 3.281
msg = 'sr2.xul ({}) is not equal to {}'.format(sr2.xul, xul)
assert sr2.xul == xul, msg
msg = 'sr2.yul ({}) is not equal to {}'.format(sr2.yul, yul)
assert sr2.yul == yul, msg
# test resetting of attributes
sr3 = flopy.utils.SpatialReference(delr=ms2.dis.delr.array,
delc=ms2.dis.delc.array, lenuni=2,
xll=xll, yll=yll, rotation=30)
# check that xul, yul and xll, yll are being recomputed
sr3.xll += 10.
sr3.yll += 21.
t_value = np.abs(sr3.xul - (xul + 10.))
msg = 'xul is not being recomputed correctly ({})'.format(t_value)
assert t_value < 1e-6, msg
t_value = np.abs(sr3.yul - (yul + 21.))
msg = 'yul is not being recomputed correctly ({})'.format(t_value)
assert t_value < 1e-6, msg
sr4 = flopy.utils.SpatialReference(delr=ms2.dis.delr.array,
delc=ms2.dis.delc.array, lenuni=2,
xul=xul, yul=yul, rotation=30)
assert sr4.origin_loc == 'ul'
sr4.xul += 10.
sr4.yul += 21.
t_value = np.abs(sr4.xll - (xll + 10.))
msg = 'xll is not being recomputed correctly ({})'.format(t_value)
assert t_value < 1e-6, msg
t_value = np.abs(sr4.yll - (yll + 21.))
msg = 'yll is not being recomputed correctly ({})'.format(t_value)
assert t_value < 1e-6, msg
sr4.rotation = 0.
# these shouldn't move because ul has priority
t_value = np.abs(sr4.xul - (xul + 10.))
msg = "rotation should not affect xul ({})".format(t_value)
assert t_value < 1e-6, msg
t_value = np.abs(sr4.yul - (yul + 21.))
msg = "rotation should not affect yul ({})".format(t_value)
assert t_value < 1e-6, msg
t_value = np.abs(sr4.xll - sr4.xul)
msg = "rotation should not affect xul and xll ({})".format(t_value)
assert t_value < 1e-6, msg
t_value = np.abs(sr4.yll - (sr4.yul - sr4.yedge[0]))
msg = "rotation should not affect yul and yll ({})".format(t_value)
assert t_value < 1e-6, msg
sr4.xll = 0.
sr4.yll = 10.
assert sr4.origin_loc == 'll', "origin_loc is not 'll'"
assert sr4.xul == 0., 'xul is not 0 ({})'.format(sr4.xul)
t_value = sr4.yedge[0] + 10.
msg = 'yul ({}) is not {}'.format(sr4.yul, t_value)
assert sr4.yul == t_value, msg
sr4.xul = xul
sr4.yul = yul
assert sr4.origin_loc == 'ul', "origin_loc is not 'ul'"
sr4.rotation = 30.
t_value = np.abs(sr4.xll - xll)
msg = "sr4.xll ({}) does not equal {}".format(sr4.xll, xll)
assert t_value < 1e-6, msg
t_value = np.abs(sr4.yll - yll)
msg = "sr4.yll ({}) does not equal {}".format(sr4.yll, yll)
assert t_value < 1e-6, msg
sr5 = flopy.utils.SpatialReference(delr=ms2.dis.delr.array,
delc=ms2.dis.delc.array, lenuni=2,
xll=xll, yll=yll,
rotation=0, epsg=26915)
sr5.lenuni = 1
assert sr5.length_multiplier == .3048, 'sr5 length multiplier is not .3048'
assert sr5.yul == sr5.yll + sr5.yedge[0] * sr5.length_multiplier
sr5.lenuni = 2
msg = 'sr5.length_multiplier ({}) is not 1.'.format(sr5.length_multiplier)
assert sr5.length_multiplier == 1., msg
t_value = sr5.yll + sr5.yedge[0]
msg = "sr4.yul ({}) does not equal {}".format(sr5.yul, t_value)
assert sr5.yul == t_value, msg
sr5.proj4_str = '+proj=utm +zone=16 +datum=NAD83 +units=us-ft +no_defs'
msg = "sr5 units ({}) is not 'feet'".format(sr5.units)
assert sr5.units == 'feet', msg
t_value = 1 / .3048
msg = "sr5 length_multiplier ({}) is not {}".format(sr5.length_multiplier,
t_value)
assert sr5.length_multiplier == t_value, msg
def test_namfile_readwrite():
nlay, nrow, ncol = 1, 30, 5
delr, delc = 250, 500
xll, yll = 272300, 5086000
fm = flopy.modflow
m = fm.Modflow(modelname='junk', model_ws=os.path.join('temp', 't007'))
dis = fm.ModflowDis(m, nlay=nlay, nrow=nrow, ncol=ncol, delr=delr,
delc=delc)
m.modelgrid = flopy.discretization.StructuredGrid(delc=m.dis.delc.array,
delr=m.dis.delr.array,
top=m.dis.top.array,
botm=m.dis.botm.array,
# lenuni=3,
# length_multiplier=.3048,
xoff=xll, yoff=yll,
angrot=30)
# test reading and writing of SR information to namfile
m.write_input()
m2 = fm.Modflow.load('junk.nam', model_ws=os.path.join('temp', 't007'))
t_value = abs(m2.modelgrid.xoffset - xll)
msg = 'm2.modelgrid.xoffset ({}) '.format(m2.modelgrid.xoffset) + \
'does not equal {}'.format(xll)
assert t_value < 1e-2, msg
t_value = abs(m2.modelgrid.yoffset - yll)
msg = 'm2.modelgrid.yoffset ({}) '.format(m2.modelgrid.yoffset) + \
'does not equal {}'.format(yll)
assert t_value < 1e-2
msg = 'm2.modelgrid.angrot ({}) '.format(m2.modelgrid.angrot) + \
'does not equal 30'
assert m2.modelgrid.angrot == 30, msg
model_ws = os.path.join("..", "examples", "data",
"freyberg_multilayer_transient")
ml = flopy.modflow.Modflow.load("freyberg.nam", model_ws=model_ws,
verbose=False,
check=False, exe_name="mfnwt")
assert ml.modelgrid.xoffset == ml.modelgrid._xul_to_xll(619653)
assert ml.modelgrid.yoffset == ml.modelgrid._yul_to_yll(3353277)
assert ml.modelgrid.angrot == 15.
def test_read_usgs_model_reference():
nlay, nrow, ncol = 1, 30, 5
delr, delc = 250, 500
# xll, yll = 272300, 5086000
model_ws = os.path.join('temp', 't007')
mrf = os.path.join(model_ws, 'usgs.model.reference')
shutil.copy('../examples/data/usgs.model.reference', mrf)
xul, yul = 0, 0
with open(mrf) as foo:
for line in foo:
if 'xul' in line.lower():
xul = float(line.strip().split()[1])
elif "yul" in line.lower():
yul = float(line.strip().split()[1])
else:
continue
fm = flopy.modflow
m = fm.Modflow(modelname='junk', model_ws=model_ws)
# feet and days
dis = fm.ModflowDis(m, nlay=nlay, nrow=nrow, ncol=ncol, delr=delr,
delc=delc, lenuni=1, itmuni=4)
m.write_input()
# test reading of SR information from usgs.model.reference
m2 = fm.Modflow.load('junk.nam', model_ws=os.path.join('temp', 't007'))
from flopy.discretization import StructuredGrid
mg = StructuredGrid(delr=dis.delr.array, delc=dis.delc.array)
mg.read_usgs_model_reference_file(mrf)
m2.modelgrid = mg
if abs(mg.xvertices[0, 0] - xul) > 0.01:
raise AssertionError()
if abs(mg.yvertices[0, 0] - yul) > 0.01:
raise AssertionError
assert m2.modelgrid.xoffset == mg.xoffset
assert m2.modelgrid.yoffset == mg.yoffset
assert m2.modelgrid.angrot == mg.angrot
assert m2.modelgrid.epsg == mg.epsg
# test reading non-default units from usgs.model.reference
shutil.copy(mrf, mrf + '_copy')
with open(mrf + '_copy') as src:
with open(mrf, 'w') as dst:
for line in src:
if 'epsg' in line:
line = line.replace("102733", '4326')
dst.write(line)
m2 = fm.Modflow.load('junk.nam', model_ws=os.path.join('temp', 't007'))
m2.modelgrid.read_usgs_model_reference_file(mrf)
assert m2.modelgrid.epsg == 4326
# have to delete this, otherwise it will mess up other tests
to_del = glob.glob(mrf + '*')
for f in to_del:
if os.path.exists(f):
os.remove(os.path.join(f))
assert True
def test_rotation():
m = flopy.modflow.Modflow(rotation=20.)
dis = flopy.modflow.ModflowDis(m, nlay=1, nrow=40, ncol=20,
delr=250.,
delc=250., top=10, botm=0)
xul, yul = 500000, 2934000
mg = flopy.discretization.StructuredGrid(delc=m.dis.delc.array,
delr=m.dis.delr.array)
mg._angrot = 45.
mg.set_coord_info(mg._xul_to_xll(xul), mg._yul_to_yll(yul),
angrot=45.)
xll, yll = mg.xoffset, mg.yoffset
assert np.abs(mg.xvertices[0, 0] - xul) < 1e-4
assert np.abs(mg.yvertices[0, 0] - yul) < 1e-4
mg2 = flopy.discretization.StructuredGrid(delc=m.dis.delc.array,
delr=m.dis.delr.array)
mg2._angrot = -45.
mg2.set_coord_info(mg2._xul_to_xll(xul), mg2._yul_to_yll(yul),
angrot=-45.)
xll2, yll2 = mg2.xoffset, mg2.yoffset
assert np.abs(mg2.xvertices[0, 0] - xul) < 1e-4
assert np.abs(mg2.yvertices[0, 0] - yul) < 1e-4
mg3 = flopy.discretization.StructuredGrid(delc=m.dis.delc.array,
delr=m.dis.delr.array,
xoff=xll2, yoff=yll2,
angrot=-45.)
assert np.abs(mg3.xvertices[0, 0] - xul) < 1e-4
assert np.abs(mg3.yvertices[0, 0] - yul) < 1e-4
mg4 = flopy.discretization.StructuredGrid(delc=m.dis.delc.array,
delr=m.dis.delr.array,
xoff=xll, yoff=yll, angrot=45.)
assert np.abs(mg4.xvertices[0, 0] - xul) < 1e-4
assert np.abs(mg4.yvertices[0, 0] - yul) < 1e-4
def test_sr_with_Map():
# Note that most of this is either deprecated, or has pending deprecation
import matplotlib.pyplot as plt
m = flopy.modflow.Modflow(rotation=20.)
dis = flopy.modflow.ModflowDis(m, nlay=1, nrow=40, ncol=20,
delr=250.,
delc=250., top=10, botm=0)
# transformation assigned by arguments
xul, yul, rotation = 500000., 2934000., 45.
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
modelmap = flopy.plot.ModelMap(model=m, xul=xul, yul=yul,