Skip to content

Commit ecc2022

Browse files
committed
Import data (*.DAT) files working good
1 parent e942c05 commit ecc2022

File tree

4 files changed

+147
-39
lines changed

4 files changed

+147
-39
lines changed

flo2d/flo2d.py

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,13 +1934,20 @@ def import_gds(self):
19341934
dir_name = os.path.dirname(fname)
19351935
s.setValue("FLO-2D/lastGdsDir", dir_name)
19361936
bname = os.path.basename(fname)
1937+
cadpts = False
1938+
fplain = False
19371939
if self.f2g.set_parser(fname):
19381940
topo = self.f2g.parser.dat_files["TOPO.DAT"]
1941+
# If topo does not exist, it could be a FLO-2D 2009 model. Check for CADPTS.DAT
19391942
if topo is None:
1940-
self.uc.bar_error("Could not find TOPO.DAT file! Importing GDS files aborted!", dur=3)
1941-
self.uc.log_info("Could not find TOPO.DAT file! Importing GDS files aborted!")
1942-
self.gutils.enable_geom_triggers()
1943-
return
1943+
cadpts = self.f2g.parser.dat_files["CADPTS.DAT"]
1944+
if cadpts is None:
1945+
self.uc.bar_error("Could not find TOPO.DAT or CADPTS.DAT file! Importing GDS files aborted!", dur=3)
1946+
self.uc.log_info("Could not find TOPO.DAT or CADPTS.DAT file! Importing GDS files aborted!")
1947+
self.gutils.enable_geom_triggers()
1948+
return
1949+
else:
1950+
cadpts = True
19441951
if bname not in self.f2g.parser.dat_files:
19451952
self.uc.bar_info("Import cancelled!")
19461953
self.uc.log_info("Import cancelled!")
@@ -1960,10 +1967,14 @@ def import_gds(self):
19601967

19611968
# Check if MANNINGS_N.DAT exist:
19621969
if not os.path.isfile(dir_name + r"\MANNINGS_N.DAT") or os.path.getsize(dir_name + r"\MANNINGS_N.DAT") == 0:
1963-
self.uc.bar_error("ERROR 241019.1821: file MANNINGS_N.DAT is missing or empty!")
1964-
self.uc.log_info("ERROR 241019.1821: file MANNINGS_N.DAT is missing or empty!")
1965-
self.gutils.enable_geom_triggers()
1966-
return
1970+
# If MANNINGS_N.DAT does not exist, it could be a FLO-2D 2009 model. Check for FPLAIN.DAT
1971+
if not os.path.isfile(dir_name + r"\FPLAIN.DAT") or os.path.getsize(dir_name + r"\FPLAIN.DAT") == 0:
1972+
self.uc.bar_error("ERROR 241019.1821: file MANNINGS_N.DAT or FPLAIN.DAT is missing or empty!")
1973+
self.uc.log_info("ERROR 241019.1821: file MANNINGS_N.DAT or FPLAIN.DAT is missing or empty!")
1974+
self.gutils.enable_geom_triggers()
1975+
return
1976+
else:
1977+
fplain = True
19671978

19681979
# Check if TOLER.DAT exist:
19691980
if not os.path.isfile(dir_name + r"\TOLER.DAT") or os.path.getsize(dir_name + r"\TOLER.DAT") == 0:
@@ -1972,7 +1983,16 @@ def import_gds(self):
19721983
self.gutils.enable_geom_triggers()
19731984
return
19741985

1986+
if cadpts and fplain:
1987+
sql = """INSERT OR REPLACE INTO cont (name, value, note) VALUES (?,?,?);"""
1988+
self.gutils.execute(sql, ("ENGINE", "FLO2009", "FLO-2D Engine"))
1989+
import_calls[1] = "import_cadpts_fplain"
1990+
else:
1991+
sql = """INSERT OR REPLACE INTO cont (name, value, note) VALUES (?,?,?);"""
1992+
self.gutils.execute(sql, ("ENGINE", "FLOPRO", "FLO-2D Engine"))
1993+
19751994
dlg_components = ComponentsDialog(self.con, self.iface, self.lyrs, "in")
1995+
dlg_components.export_engine_cbo.setEnabled(False)
19761996
ok = dlg_components.exec_()
19771997
if ok:
19781998
try:
@@ -2184,6 +2204,13 @@ def import_gds(self):
21842204
for table in tables:
21852205
self.gutils.clear_tables(table)
21862206

2207+
if "import_cadpts_fplain" in import_calls:
2208+
sql = """INSERT OR REPLACE INTO cont (name, value, note) VALUES (?,?,?);"""
2209+
self.gutils.execute(sql, ("ENGINE", "FLO2009", "FLO-2D Engine"))
2210+
else:
2211+
sql = """INSERT OR REPLACE INTO cont (name, value, note) VALUES (?,?,?);"""
2212+
self.gutils.execute(sql, ("ENGINE", "FLOPRO", "FLO-2D Engine"))
2213+
21872214
self.call_IO_methods(import_calls, True) # The strings list 'export_calls', contains the names of
21882215
# the methods in the class Flo2dGeoPackage to import (read) the
21892216
# FLO-2D .DAT files
@@ -3019,13 +3046,20 @@ def export_gds(self):
30193046
ok = dlg_components.exec_()
30203047
if ok:
30213048

3049+
QApplication.setOverrideCursor(Qt.WaitCursor)
3050+
30223051
engine_idx = dlg_components.export_engine_cbo.currentIndex()
30233052
sql = """INSERT OR REPLACE INTO cont (name, value, note) VALUES (?,?,?);"""
30243053
# idx equal 0 is the FLO-2D Pro and idx equal 1 is the FLO-2D 2009
30253054
if engine_idx == 0:
30263055
self.gutils.execute(sql, ("ENGINE", "FLOPRO", "FLO-2D Engine"))
30273056
else:
30283057
self.gutils.execute(sql, ("ENGINE", "FLO2009", "FLO-2D Engine"))
3058+
export_calls.remove("export_swmmflo")
3059+
export_calls.remove("export_swmmflort")
3060+
export_calls.remove("export_swmmoutf")
3061+
export_calls.remove("export_swmmflodropbox")
3062+
export_calls.remove("export_sdclogging")
30293063

30303064
if dlg_components.remove_files_chbox.isChecked():
30313065
for file in files:
@@ -3099,12 +3133,13 @@ def export_gds(self):
30993133
if "Rain" not in dlg_components.components:
31003134
export_calls.remove("export_rain")
31013135

3102-
if "Storm Drain" not in dlg_components.components:
3103-
export_calls.remove("export_swmmflo")
3104-
export_calls.remove("export_swmmflort")
3105-
export_calls.remove("export_swmmoutf")
3106-
export_calls.remove("export_swmmflodropbox")
3107-
export_calls.remove("export_sdclogging")
3136+
if engine_idx == 0:
3137+
if "Storm Drain" not in dlg_components.components:
3138+
export_calls.remove("export_swmmflo")
3139+
export_calls.remove("export_swmmflort")
3140+
export_calls.remove("export_swmmoutf")
3141+
export_calls.remove("export_swmmflodropbox")
3142+
export_calls.remove("export_sdclogging")
31083143

31093144
if "Spatial Shallow-n" not in dlg_components.components:
31103145
export_calls.remove("export_shallowNSpatial")

flo2d/flo2d_ie/flo2d_parser.py

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def __init__(self):
333333
["GRAPTIM"],
334334
]
335335
self.toler_rows = [
336-
["TOLGLOBAL", "DEPTOL", ""],
336+
["TOLGLOBAL", "DEPTOL", "WAVEMAX"],
337337
["COURCHAR_C", "COURANTFP", "COURANTC", "COURANTST"],
338338
["COURCHAR_T", "TIME_ACCEL"],
339339
]
@@ -376,26 +376,43 @@ def _calculate_cellsize(self):
376376
def calculate_cellsize(self):
377377
cell_size = 0
378378
topo = self.dat_files["TOPO.DAT"]
379-
if topo is None:
380-
return 0
381-
if not os.path.isfile(topo):
382-
return 0
383-
if not os.path.getsize(topo) > 0:
384-
return 0
385-
with open(topo) as top:
386-
first_coord = top.readline().split()[:2]
387-
first_x = float(first_coord[0])
388-
first_y = float(first_coord[1])
389-
top.seek(0)
390-
dx_coords = (abs(first_x - float(row.split()[0])) for row in top)
391-
try:
392-
size = min(dx for dx in dx_coords if dx > 0)
393-
except ValueError:
379+
# If TOPO.DAT does not exist, try to calculate the cell_size from CADPTS.DAT
380+
if not topo or not os.path.isfile(topo) or os.path.getsize(topo) <= 0:
381+
cadpts = self.dat_files["CADPTS.DAT"]
382+
# If CADPTS.DAT also does not exist, return 0.
383+
if not cadpts or not os.path.isfile(cadpts) or os.path.getsize(cadpts) <= 0:
384+
return 0
385+
else:
386+
with open(cadpts) as cad:
387+
first_coord = cad.readline().split()[1:3]
388+
first_x = float(first_coord[0])
389+
first_y = float(first_coord[1])
390+
cad.seek(0)
391+
dx_coords = (abs(first_x - float(row.split()[1])) for row in cad)
392+
try:
393+
size = min(dx for dx in dx_coords if dx > 0)
394+
except ValueError:
395+
cad.seek(0)
396+
dy_coords = (abs(first_y - float(row.split()[2])) for row in cad)
397+
size = min(dy for dy in dy_coords if dy > 0)
398+
cell_size += size
399+
return cell_size
400+
# If TOPO.DAT exists, calculate cell_size from this .DAT file
401+
else:
402+
with open(topo) as top:
403+
first_coord = top.readline().split()[:2]
404+
first_x = float(first_coord[0])
405+
first_y = float(first_coord[1])
394406
top.seek(0)
395-
dy_coords = (abs(first_y - float(row.split()[1])) for row in top)
396-
size = min(dy for dy in dy_coords if dy > 0)
397-
cell_size += size
398-
return cell_size
407+
dx_coords = (abs(first_x - float(row.split()[0])) for row in top)
408+
try:
409+
size = min(dx for dx in dx_coords if dx > 0)
410+
except ValueError:
411+
top.seek(0)
412+
dy_coords = (abs(first_y - float(row.split()[1])) for row in top)
413+
size = min(dy for dy in dy_coords if dy > 0)
414+
cell_size += size
415+
return cell_size
399416

400417
@staticmethod
401418
def single_parser(file1):

flo2d/flo2d_ie/flo2dgeopackage.py

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def set_parser(self, fpath, get_cell_size=True):
7676
self.cell_size = int(round(self.parser.calculate_cellsize()))
7777
if self.cell_size == 0:
7878
self.uc.show_info(
79-
"ERROR 060319.1604: Cell size is 0 - something went wrong!\nDoes TOPO.DAT file exist or is empty?"
79+
"ERROR 060319.1604: Cell size is 0 - something went wrong!\nDoes TOPO.DAT or CADPTS.DAT file exist or is empty?"
8080
)
8181
return False
8282
else:
@@ -114,7 +114,16 @@ def import_cont_toler_dat(self):
114114
if len(cont["ITIMTEP"]) > 1:
115115
cont["ITIMTEP"] = cont["ITIMTEP"][0]
116116
toler = self.parser.parse_toler()
117+
# Adjust some TOL values when importing from FLO-2D 2009 models
118+
if toler.get("COURANTST") is None:
119+
toler["COURANTST"] = 0.6
120+
if toler.get("COURCHAR_T") is None:
121+
toler["COURCHAR_T"] = "T"
122+
if toler.get("TIME_ACCEL") is None:
123+
toler["TIME_ACCEL"] = 0.1
117124
cont.update(toler)
125+
if cont.get("SWMM") is None:
126+
cont["SWMM"] = 0
118127
for option in cont:
119128
sql += [(option, cont[option], self.PARAMETER_DESCRIPTION[option])]
120129
sql += [("CELLSIZE", self.cell_size, self.PARAMETER_DESCRIPTION["CELLSIZE"])]
@@ -274,6 +283,46 @@ def import_mannings_n_topo_hdf5(self):
274283
QApplication.restoreOverrideCursor()
275284
self.uc.show_error("ERROR 040521.1154: importing Grid data from HDF5 file!\n", e)
276285

286+
def import_cadpts_fplain(self):
287+
if self.parsed_format == self.FORMAT_DAT:
288+
return self.import_cadpts_fplain_dat()
289+
elif self.parsed_format == self.FORMAT_HDF5:
290+
return self.import_cadpts_fplain_hdf5()
291+
292+
def import_cadpts_fplain_dat(self):
293+
try:
294+
sql = ["""INSERT INTO grid (fid, n_value, elevation, geom) VALUES""", 4]
295+
296+
self.clear_tables("grid")
297+
data = self.parser.parse_fplain_cadpts()
298+
299+
c = 0
300+
fid = slice(0, 1)
301+
man = slice(5, 6)
302+
elev = slice(6, 7)
303+
coords = slice(8, None)
304+
for row in data:
305+
if c < self.chunksize:
306+
geom = " ".join(row[coords])
307+
g = self.build_square(geom, self.cell_size)
308+
sql += [tuple(row[fid] + row[man] + row[elev] + [g])]
309+
c += 1
310+
else:
311+
self.batch_execute(sql)
312+
c = 0
313+
if len(sql) > 2:
314+
self.batch_execute(sql)
315+
else:
316+
pass
317+
318+
except Exception as e:
319+
QApplication.restoreOverrideCursor()
320+
self.uc.show_error("ERROR 040521.1154: importing CADPTS.DAT and FPLAIN.DAT!.\n", e)
321+
322+
def import_cadpts_fplain_hdf5(self):
323+
# TODO implement this on the hdf5 project
324+
pass
325+
277326
def import_inflow(self):
278327
cont_sql = ["""INSERT INTO cont (name, value, note) VALUES""", 3]
279328
inflow_sql = [
@@ -3705,6 +3754,9 @@ def export_cont_toler_dat(self, outdir):
37053754
if not self.is_flopro:
37063755
if o == "SWMM":
37073756
continue
3757+
# Set the super switch to 0
3758+
if o == "DEPRESSDEPTH":
3759+
options[o] = 0
37083760
if o not in options:
37093761
continue
37103762
val = options[o]
@@ -3756,6 +3808,10 @@ def export_cont_toler_dat(self, outdir):
37563808
for o in row:
37573809
if o not in options:
37583810
continue
3811+
# FLO-2D Pro does not require WAVEMAX on TOLER.DAT but FLO-2D 2009 does
3812+
if self.is_flopro:
3813+
if o == "WAVEMAX":
3814+
continue
37593815
val = options[o]
37603816
lst += rline.format(val) # Second line 'C' (Courant values) writes 1, 2, or 3 values depending
37613817
# if channels and/or streets are simulated
@@ -3907,8 +3963,8 @@ def export_mannings_n_topo_dat(self, outdir):
39073963
c.write(
39083964
cline.format(
39093965
fid,
3910-
"{0:.3f}".format(float(x)),
3911-
"{0:.3f}".format(float(y))
3966+
"{0:.4f}".format(float(x)),
3967+
"{0:.4f}".format(float(y))
39123968
)
39133969
)
39143970
cell_size = float(self.gutils.get_cont_par("CELLSIZE"))
@@ -3921,7 +3977,7 @@ def export_mannings_n_topo_dat(self, outdir):
39213977
s_grid if s_grid else 0,
39223978
w_grid if w_grid else 0,
39233979
"{0:.3f}".format(float(man)),
3924-
"{0:.2f}".format(float(elev))
3980+
"{0:.4f}".format(float(elev))
39253981
)
39263982
)
39273983

flo2d/gui/dlg_components.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def populate_components_dialog(self):
7979
if self.in_or_out == "in":
8080
self.setWindowTitle("FLO-2D Components to Import")
8181
self.components_note_lbl.setVisible(False)
82-
self.mannings_n_and_Topo_chbox.setVisible(False)
82+
self.mannings_n_and_Topo_chbox.setChecked(True)
8383

8484
if os.path.isfile(last_dir + r"\CHAN.DAT"):
8585
if os.path.getsize(last_dir + r"\CHAN.DAT") > 0:

0 commit comments

Comments
 (0)