ffmtools 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ffmgen/gui.py ADDED
@@ -0,0 +1,315 @@
1
+ """tkinter MDU builder (3Di subgrid).
2
+
3
+ Each tab is one parameter group (= one sheet of mdu_prep.xlsx).
4
+ * 'On' checkbox = column 1 of the sheet (checked -> active line, unchecked -> written with '#').
5
+ * The start file (default: mdu_prep.xlsx in the current folder) is loaded at start;
6
+ otherwise the built-in template is used.
7
+ * Load reads an .xlsx (one sheet per group) or an existing .mdu; 'Load .mdu...' picks an .mdu
8
+ file and loads it directly. Save xlsx writes the tabs back.
9
+ * Generate writes <name>.mdu and _run_<name>.bat.
10
+ * EndDate / EndTime (tab 'time'): on Preview/Generate, NTimesteps = (End - Start) / Dt is
11
+ calculated and written; if it does not divide evenly it is rounded up (ceil).
12
+ """
13
+ import contextlib
14
+ import io
15
+ import os
16
+ import tkinter as tk
17
+ from collections import OrderedDict
18
+ from tkinter import ttk, filedialog, messagebox
19
+ from tkinter.scrolledtext import ScrolledText
20
+
21
+ from .core import (DEFAULT_NAME, EXE_DEFAULT, OMP_DEFAULT, check_groups, default_data,
22
+ name_from_path, read_any, to_mdu_text, write_mdu, write_xlsx)
23
+ from .timecalc import ensure_end_rows, update_ntimesteps
24
+
25
+ DEFAULT_XLSX = 'mdu_prep.xlsx' # auto-loaded at start if it exists
26
+
27
+ __all__ = ['ScrollFrame', 'MduGuiTk', 'App', 'run']
28
+
29
+
30
+ class ScrollFrame(ttk.Frame):
31
+ """Frame with a vertical + horizontal scrollbar; put widgets in .inner."""
32
+
33
+ def __init__(self, master, **kw):
34
+ super().__init__(master, **kw)
35
+ self.canvas = tk.Canvas(self, highlightthickness=0)
36
+ vsb = ttk.Scrollbar(self, orient='vertical', command=self.canvas.yview)
37
+ hsb = ttk.Scrollbar(self, orient='horizontal', command=self.canvas.xview)
38
+ self.canvas.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)
39
+ self.inner = ttk.Frame(self.canvas)
40
+ self.canvas.create_window((0, 0), window=self.inner, anchor='nw')
41
+ self.inner.bind('<Configure>', self._update_region)
42
+ self.canvas.bind('<Configure>', self._update_region)
43
+ self.canvas.grid(row=0, column=0, sticky='nsew')
44
+ vsb.grid(row=0, column=1, sticky='ns')
45
+ hsb.grid(row=1, column=0, sticky='ew')
46
+ self.rowconfigure(0, weight=1)
47
+ self.columnconfigure(0, weight=1)
48
+ # mouse wheel only while the pointer is over this frame
49
+ self.canvas.bind('<Enter>', lambda e: self.canvas.bind_all('<MouseWheel>', self._wheel))
50
+ self.canvas.bind('<Leave>', lambda e: self.canvas.unbind_all('<MouseWheel>'))
51
+
52
+ def _update_region(self, _=None):
53
+ # scroll region never smaller than the visible canvas and always anchored at (0, 0);
54
+ # otherwise (Windows Tk) short pages like 'grid' slide down and leave a gap under the tab bar
55
+ w = max(self.inner.winfo_reqwidth(), self.canvas.winfo_width())
56
+ h = max(self.inner.winfo_reqheight(), self.canvas.winfo_height())
57
+ self.canvas.configure(scrollregion=(0, 0, w, h))
58
+ if self.inner.winfo_reqheight() <= self.canvas.winfo_height():
59
+ self.canvas.yview_moveto(0)
60
+ if self.inner.winfo_reqwidth() <= self.canvas.winfo_width():
61
+ self.canvas.xview_moveto(0)
62
+
63
+ def _wheel(self, e):
64
+ if self.inner.winfo_reqheight() <= self.canvas.winfo_height():
65
+ return # everything visible -> nothing to scroll
66
+ self.canvas.yview_scroll(int(-e.delta / 120) or (-1 if e.delta > 0 else 1), 'units')
67
+
68
+
69
+ class MduGuiTk:
70
+ """Notebook of parameter-group tabs, editable row by row."""
71
+ W_KEY, W_VAL, W_COM = 30, 45, 70 # entry widths (characters)
72
+ W_INFO = [70, 25, 30]
73
+
74
+ def __init__(self, notebook):
75
+ self.nb = notebook
76
+ self.rows = OrderedDict() # group -> list of row vars
77
+ self.frames = {} # group -> rows frame
78
+
79
+ # ---- one row of widgets; returns list of tk variables ----
80
+ def _row(self, parent, group, vals, r):
81
+ if group == 'info':
82
+ vals = (list(vals) + ['', '', ''])[:3]
83
+ vs = [tk.StringVar(value=str(v)) for v in vals]
84
+ for c, (v, wd) in enumerate(zip(vs, self.W_INFO)):
85
+ ttk.Entry(parent, textvariable=v, width=wd).grid(row=r, column=c, sticky='ew', padx=1, pady=1)
86
+ return vs
87
+ on, key, val, com = vals
88
+ vs = [tk.BooleanVar(value=bool(on)), tk.StringVar(value=str(key)),
89
+ tk.StringVar(value=str(val)), tk.StringVar(value=str(com))]
90
+ ttk.Checkbutton(parent, variable=vs[0]).grid(row=r, column=0, padx=(4, 2))
91
+ for c, (v, wd) in enumerate(zip(vs[1:], (self.W_KEY, self.W_VAL, self.W_COM)), start=1):
92
+ ttk.Entry(parent, textvariable=v, width=wd).grid(row=r, column=c, sticky='ew', padx=1, pady=1)
93
+ return vs
94
+
95
+ def _page(self, group, rows):
96
+ page = ttk.Frame(self.nb)
97
+ sf = ScrollFrame(page)
98
+ sf.pack(fill='both', expand=True)
99
+ body = sf.inner
100
+ if group == 'info':
101
+ ttk.Label(body, text="Header lines, written as '# ...' at the top of the .mdu",
102
+ font=('', 9, 'italic')).grid(row=0, column=0, columnspan=3, sticky='w', pady=(4, 2))
103
+ else:
104
+ for c, t in enumerate(['On', 'Parameter', 'Value', 'Description']):
105
+ ttk.Label(body, text=t, font=('', 9, 'bold')).grid(row=0, column=c, sticky='w', pady=(4, 2))
106
+ self.rows[group] = [self._row(body, group, r, i + 1) for i, r in enumerate(rows)]
107
+ self.frames[group] = body
108
+
109
+ def _add():
110
+ new = ['', '', ''] if group == 'info' else [True, '', '', '']
111
+ self.rows[group].append(self._row(body, group, new, len(self.rows[group]) + 1))
112
+ btn.grid_configure(row=len(self.rows[group]) + 1)
113
+ sf.canvas.update_idletasks()
114
+ sf.canvas.yview_moveto(1.0)
115
+
116
+ btn = ttk.Button(body, text='+ add row', command=_add)
117
+ btn.grid(row=len(rows) + 1, column=0, columnspan=2, sticky='w', pady=4)
118
+ return page
119
+
120
+ def set_data(self, data, keep_tab=False):
121
+ idx = self.nb.index('current') if (keep_tab and self.nb.tabs()) else 0
122
+ for t in self.nb.tabs():
123
+ self.nb.forget(t)
124
+ self.rows, self.frames = OrderedDict(), {}
125
+ for g, rows in data.items():
126
+ self.nb.add(self._page(g, rows), text=g)
127
+ if self.nb.tabs():
128
+ self.nb.select(min(idx, len(self.nb.tabs()) - 1))
129
+
130
+ def get_data(self):
131
+ return OrderedDict((g, [[v.get() for v in r] for r in rows])
132
+ for g, rows in self.rows.items())
133
+
134
+
135
+ class App(tk.Tk):
136
+ """Main window: load/save bar, parameter tabs, output options and log."""
137
+
138
+ def __init__(self, src=None, out_dir='.'):
139
+ super().__init__()
140
+ self.title('3Di subgrid - MDU builder (ffmgen)')
141
+ self.geometry('1250x820')
142
+ pad = dict(padx=4, pady=2)
143
+ start_file = src or DEFAULT_XLSX
144
+
145
+ # ---- top: load / save ----
146
+ top = ttk.Frame(self)
147
+ top.pack(fill='x', **pad)
148
+ self.src_path = tk.StringVar(value=start_file)
149
+ self.save_path = tk.StringVar(value='mdu_custom.xlsx')
150
+ ttk.Label(top, text='Load from').grid(row=0, column=0, sticky='w')
151
+ ttk.Entry(top, textvariable=self.src_path, width=70).grid(row=0, column=1, sticky='ew', **pad)
152
+ ttk.Button(top, text='...', width=3, command=self.browse_src).grid(row=0, column=2)
153
+ ttk.Button(top, text='Load', command=self.on_load).grid(row=0, column=3, **pad)
154
+ ttk.Button(top, text='Load .mdu...', command=self.on_load_mdu).grid(row=0, column=4, **pad)
155
+ ttk.Button(top, text='Built-in defaults', command=self.on_reset).grid(row=0, column=5, **pad)
156
+ ttk.Label(top, text='Save as').grid(row=1, column=0, sticky='w')
157
+ ttk.Entry(top, textvariable=self.save_path, width=70).grid(row=1, column=1, sticky='ew', **pad)
158
+ ttk.Button(top, text='...', width=3, command=self.browse_save).grid(row=1, column=2)
159
+ ttk.Button(top, text='Save xlsx', command=self.on_save).grid(row=1, column=3, **pad)
160
+ top.columnconfigure(1, weight=1)
161
+
162
+ # ---- middle: tabs (resizable against the log) ----
163
+ pw = ttk.PanedWindow(self, orient='vertical')
164
+ pw.pack(fill='both', expand=True, **pad)
165
+ nb = ttk.Notebook(pw)
166
+ self.gui = MduGuiTk(nb)
167
+ pw.add(nb, weight=4)
168
+
169
+ # ---- output options + log ----
170
+ bottom = ttk.Frame(pw)
171
+ pw.add(bottom, weight=1)
172
+ opt = ttk.LabelFrame(bottom, text='Output')
173
+ opt.pack(fill='x')
174
+ self.out_dir = tk.StringVar(value=out_dir)
175
+ self.out_name = tk.StringVar()
176
+ self.exe = tk.StringVar(value=EXE_DEFAULT)
177
+ self.threads = tk.IntVar(value=OMP_DEFAULT)
178
+ self.write_bat = tk.BooleanVar(value=True)
179
+ ttk.Label(opt, text='Output dir').grid(row=0, column=0, sticky='w', **pad)
180
+ ttk.Entry(opt, textvariable=self.out_dir, width=55).grid(row=0, column=1, sticky='ew', **pad)
181
+ ttk.Button(opt, text='...', width=3, command=self.browse_dir).grid(row=0, column=2)
182
+ ttk.Label(opt, text='MDU name').grid(row=0, column=3, sticky='w', **pad)
183
+ ttk.Entry(opt, textvariable=self.out_name, width=30).grid(row=0, column=4, sticky='w', **pad)
184
+ ttk.Label(opt, text='Executable').grid(row=1, column=0, sticky='w', **pad)
185
+ ttk.Entry(opt, textvariable=self.exe, width=55).grid(row=1, column=1, sticky='ew', **pad)
186
+ ttk.Label(opt, text='OMP threads').grid(row=1, column=3, sticky='w', **pad)
187
+ ttk.Spinbox(opt, from_=1, to=256, textvariable=self.threads, width=6).grid(row=1, column=4, sticky='w', **pad)
188
+ ttk.Checkbutton(opt, text='Also write _run_<name>.bat', variable=self.write_bat).grid(
189
+ row=1, column=5, sticky='w', **pad)
190
+ btns = ttk.Frame(opt)
191
+ btns.grid(row=2, column=0, columnspan=6, sticky='w', **pad)
192
+ ttk.Button(btns, text='Preview', command=self.on_preview).pack(side='left', padx=(0, 6))
193
+ ttk.Button(btns, text='Generate .mdu', command=self.on_generate).pack(side='left')
194
+ opt.columnconfigure(1, weight=1)
195
+
196
+ self.log = ScrolledText(bottom, height=10, font=('Consolas', 9), wrap='none')
197
+ self.log.pack(fill='both', expand=True, pady=(4, 0))
198
+
199
+ # ---- initial values: start file if present, else built-in template ----
200
+ with self.logging(clear=True):
201
+ if os.path.exists(start_file):
202
+ try:
203
+ data, name = read_any(start_file), name_from_path(start_file)
204
+ print('Loaded ' + os.path.abspath(start_file))
205
+ except Exception as e:
206
+ print('Could not read', start_file, '->', e, '- using built-in defaults.')
207
+ data, name = default_data(), DEFAULT_NAME
208
+ else:
209
+ data, name = default_data(), DEFAULT_NAME
210
+ print(start_file + ' not found - using built-in defaults.')
211
+ self.gui.set_data(ensure_end_rows(data))
212
+ self.out_name.set(name)
213
+ check_groups(self.gui.rows.keys())
214
+
215
+ # ---- print() -> log widget ----
216
+ @contextlib.contextmanager
217
+ def logging(self, clear=True):
218
+ if clear:
219
+ self.log.delete('1.0', 'end')
220
+ buf = io.StringIO()
221
+ try:
222
+ with contextlib.redirect_stdout(buf):
223
+ yield
224
+ except Exception as e: # never lose an error silently
225
+ buf.write('ERROR: %s\n' % e)
226
+ finally:
227
+ self.log.insert('end', buf.getvalue())
228
+ self.log.see('end' if not clear else '1.0')
229
+
230
+ # ---- file dialogs ----
231
+ def browse_src(self):
232
+ p = filedialog.askopenfilename(filetypes=[('xlsx / mdu', ('*.xlsx', '*.mdu')), ('Excel', '*.xlsx'),
233
+ ('MDU', '*.mdu'), ('All files', '*.*')])
234
+ if p:
235
+ self.src_path.set(p)
236
+
237
+ def browse_save(self):
238
+ p = filedialog.asksaveasfilename(defaultextension='.xlsx', filetypes=[('Excel', '*.xlsx')])
239
+ if p:
240
+ self.save_path.set(p)
241
+
242
+ def browse_dir(self):
243
+ p = filedialog.askdirectory()
244
+ if p:
245
+ self.out_dir.set(p)
246
+
247
+ # ---- actions ----
248
+ def on_load(self):
249
+ with self.logging():
250
+ p = self.src_path.get().strip()
251
+ try:
252
+ data = read_any(p)
253
+ except Exception as e:
254
+ print('Could not load', p, '->', e)
255
+ return
256
+ self.gui.set_data(ensure_end_rows(data))
257
+ self.out_name.set(name_from_path(p))
258
+ print('Loaded', os.path.abspath(p))
259
+ check_groups(data.keys())
260
+
261
+ def on_load_mdu(self):
262
+ p = filedialog.askopenfilename(title='Load .mdu', filetypes=[('MDU files', '*.mdu'), ('All files', '*.*')])
263
+ if p:
264
+ self.src_path.set(p)
265
+ self.on_load()
266
+
267
+ def on_reset(self):
268
+ with self.logging():
269
+ self.gui.set_data(ensure_end_rows(default_data()))
270
+ self.out_name.set(DEFAULT_NAME)
271
+ print('Reset to built-in defaults.')
272
+
273
+ def on_save(self):
274
+ with self.logging():
275
+ p = self.save_path.get().strip()
276
+ try:
277
+ write_xlsx(self.gui.get_data(), p)
278
+ print('Saved', os.path.abspath(p))
279
+ except Exception as e:
280
+ print('Could not save ->', e)
281
+
282
+ def on_preview(self):
283
+ with self.logging():
284
+ data = self.gui.get_data()
285
+ try:
286
+ if update_ntimesteps(data) is not None:
287
+ self.gui.set_data(data, keep_tab=True)
288
+ except Exception as e:
289
+ print('WARNING: NTimesteps not calculated ->', e)
290
+ print(to_mdu_text(data))
291
+
292
+ def on_generate(self):
293
+ with self.logging():
294
+ data = self.gui.get_data()
295
+ check_groups(data.keys())
296
+ try:
297
+ if update_ntimesteps(data) is not None:
298
+ self.gui.set_data(data, keep_tab=True)
299
+ except Exception as e:
300
+ print('ERROR: cannot calculate NTimesteps from EndDate/EndTime ->', e)
301
+ print('Nothing written. Fix the [time] tab and try again.')
302
+ messagebox.showerror('ffmgen', 'Cannot calculate NTimesteps:\n%s' % e)
303
+ return
304
+ try:
305
+ n_thr = int(self.threads.get())
306
+ except (tk.TclError, ValueError):
307
+ n_thr = OMP_DEFAULT
308
+ write_mdu(data, out_dir=self.out_dir.get().strip() or '.',
309
+ name=self.out_name.get().strip() or 'model',
310
+ write_bat=self.write_bat.get(), threads=n_thr, exe=self.exe.get().strip())
311
+
312
+
313
+ def run(src=None, out_dir='.'):
314
+ """Open the GUI. ``src``: .xlsx/.mdu to load at start (default: ./mdu_prep.xlsx)."""
315
+ App(src=src, out_dir=out_dir).mainloop()
@@ -0,0 +1,138 @@
1
+ # Generated on 16:01:35, 27-02-2014
2
+ # Deltares, 3Di - subgrid Version 0.1.1.1254, Feb 25 2014, 20:37:59
3
+
4
+ [model]
5
+ AutoStart = 0 # 0: NoAutoStart, 1: AutoStart, 2: AutoStartStop
6
+ FileFormatVersion = 2.09999990463257 # File format version. Do not edit this.
7
+
8
+ [geometry]
9
+ BathymetryFile = subgrid/FinalDEM2D_2.tif # *.binary or *.asc file with pixel data
10
+ #DikeFile = polygons/Stamford3Di_network_v4_2.pol # *.pol file with dike data
11
+ #MaxInterception = 0. # Default constant thickness of interception layer (m)
12
+ MaxInterceptionFile = subgrid/intception_45_5.asc # Pixel level *asc file with interception layer thickness (m)
13
+ NetworkFile = # 1d Channel network file
14
+ #NetworkPolyFile = one_d/network_manilla_manilla.pli #
15
+ CrossSectionFile = #
16
+ CrossSectionDefinitionFile = #
17
+ StructureFile = # Structure file
18
+ LeveeFile = # Levee file
19
+
20
+ [grid]
21
+ GridAdminFile = # *.grd file
22
+ TableDataFile = l # *.tbl file
23
+ GridSpace = 20. # Min grid size (m)
24
+ kmax = 3 # Max refinement level
25
+ BathDelta = 1. # Refinement threshold (max bath difference)
26
+ BathMax = 45.9 # Max sensible bathymetry level (m)
27
+ GuessDams = 0 # 1: use bath info to find dams, 0: ignore bath info
28
+
29
+ [initialization]
30
+ WaterLevelIni = -0.18629 # Initial water level (m)
31
+ #WaterLevelFile = # Initial water level file
32
+ WaterLevelIniFile = # initial water level file
33
+ WaterLevelIniType = 0 # 0: max, 1: avg
34
+ #InterceptionIni = #
35
+ InterceptionIniFile = # Initial interception file
36
+ InterceptionIniType = 2 # Initial interception type: 0-average, 1-max
37
+ FloodIniFile = # Flood fill file name
38
+ RestartFile = # *.nc restart file used to start the simulation.
39
+
40
+ [defaults]
41
+ FloodWaterLevel = 1. # Flood fill water level (m)
42
+ FloodLevelAbsolute = 1 # 1/0: absolute/relative flood water level
43
+ BathymetryIncrement = -0.5 # Increment for bathymetry lifting (m)
44
+ BathIncAbsolute = 0 # 1/0 : absolute/relative bathymetry lifting
45
+ InfiltrationRateNew = 5. # Infiltration rate (mm/day) for interactive changing
46
+ RainfallCloudAmount = 5.d-2 # Rainfall amount (m) in interactive cloud rain
47
+ RainfallCloudDiameter = 1000. # Rainfall cloud diameter (m) in interactive cloud rain
48
+
49
+ [numerics]
50
+ IntegrationMethod = 0 # Time integration method: 0=Euler implicit, 1=Carlson implicit, 2=Silecki explicit.
51
+ MaxNonlinIterations = 20 # Maximum number of nonlinear iterations in single time step.
52
+ NumPredCor = 1 # Number of 'predictor-corrector'-steps in single time step (npc).
53
+ ConvergenceEps = 1.d-5 # Minimal residual for convergence of water level solver.
54
+ FloodingThreshold = 1.d-2 # Flooding threshold (m)
55
+ OpenLinkCheck = 0 # 0: lowest bath point, 1: wet path
56
+ Advection = 0 # 1/0 Advection on/off
57
+ PricemanSlot = 0 # 1/0 Priceman slots on/off
58
+
59
+ [physics]
60
+ FrictType = 4 # 1: Chezy, 4: Manning
61
+ FrictCoefFile = subgrid/manning55_35donw.asc # *.asc file with space varying friction coefficient
62
+ #FrictCoef = 2.6d-2 # Constant friction coefficient
63
+
64
+ [hydrology]
65
+ InfiltrationRateFile = # Pixel level *asc file with infiltration rates (mm/day)
66
+ NumLayers = 0 #
67
+ #GroundLayerElevation = -10.0 # Elevation/position of the bottom of the ground layer (should be below bathymetry).
68
+ #GroundLayerElevationAbsolute = 1 # Bottom Layer position measured from 1: datum, 0: relative to node's lowest bath point
69
+ #GroundWaterLevelIni = -9999.0 # Initial constant ground water level (m)
70
+ #GroundWaterLevelIniFile = # Initial ground water level file
71
+ #RiverResistanceFile = # River sediment resistance file (d)
72
+ #ConstantRiverResistance = 1 #
73
+ #HydraulicConductivityFile = # Hydraulic conductivity file (m/d)
74
+ #HydraulicConductivityFile_X = # File of Hydraulic conductivity in x direction (m/d)
75
+ #HydraulicConductivityFile_Y = # File of Hydraulic conductivity in x direction (m/d)
76
+ #HydraulicConductivity_X = 0.0001 # Permeability in x direction (m/s)
77
+ #HydraulicConductivity_Y = 0.0001 # Permeability in y direction (m/s)
78
+ #MoistureContent = 1 # 0: wilting point, 1: equilibrium
79
+ #SoilTypeFile = # Soil type file name
80
+ #CropTypeFile = # Crop type file name
81
+ #ConstantSeepage = 0 #
82
+
83
+ [time]
84
+ Dt = 4. # Surface flow timestep (s)
85
+ StartDate = 2011-12-23 # Reference date (yyyy-mm-dd)
86
+ StartTime = 06:00:00 # Initial time (hh:mm:ss)
87
+ #EndDate = 2011-12-23 # End date (yyyy-mm-dd); self-defined, used to compute NTimesteps, not used in 3Di
88
+ #EndTime = 11:50:00 # End time (hh:mm:ss); self-defined, used to compute NTimesteps, not used in 3Di
89
+ NTimesteps = 5250 # Number of timesteps
90
+
91
+ [external forcing]
92
+ ExtForceFile = boundaries/boundary_v5.ext # *.ext
93
+ #ExtForceFile = boundaries/boundary_v6.ext # *.ext
94
+ ManholeFile = # *.dat file with manholes
95
+ ConstantRainAmount = 0. # Constant rainfall amount (mm/day)
96
+ RainfallFile = # Rainfall timeseries file name
97
+ Evaporation = 0 # 1/0 Evaporation on/off
98
+
99
+ [output]
100
+ HisOutput1Dpoints = 1 # 1/0: include all 1D grid points in his file as well.
101
+ MapInterval = 60. # Interval (s) between successive snapshots in map file.
102
+ HisInterval = 60. # Interval (s) for timeseries in his file.
103
+ RstInterval = 0. # Interval (s) between writing *.nc restart files.
104
+ #ObsFile = Obs_stationsStamford_v3.xyn # *.xyn file with coordinates+name of observation stations.
105
+ CrsFile = polygons/OutflowStamfCanal.pol # *.pol file with crossections.
106
+
107
+ [display]
108
+ RedrawEvery = 3 # Plot every RedrawEvery timesteps
109
+ SaveHardCopy = 0 # 1/0: save/don't save hard copy
110
+ showGrid = 0 # 1/0: show/don't show grid
111
+ showLinks = 0 # 1/0: show/dont't show open flow links
112
+ ShowNodNumbers = 0 # 1/0: show/dont't show node/link numbers
113
+ Show1DNetwork = 0 # 1/0: show/dont't show 1D network
114
+ ShowStructures = 0 # Display structures in GUI. 0: no, 1: icon, 2: icon+label.
115
+ ShowNetworkCRS = 0 # Display network cross sections in GUI. 0: no, 1: icon, 2: icon+label.
116
+ Show1DNodNum = 0 # 1/0: show/dont't show 1D node/link numbers
117
+ showInterception = 0 # 0: Do not show, 1: Show intercepted water.
118
+ ShowUZslice = 0 # 0: do not show, 1: show UZ in side view
119
+ sliceUZcolor = 0 # 0: single color, 1: moisture content dependent
120
+ showChanSelect = #
121
+ showChanMinY = 0. #
122
+ showChanMaxY = 0. #
123
+
124
+ [colors]
125
+ LandColorMapFile = gui/land.rgb # *.rgb or *.zrgb land color map file
126
+ LandColorType = 0 # 0: spec. min/max height, 1: auto, 2: absolute from zrgb-file
127
+ cmax = 45.6591987609863 # Maximum colored bathymetry value (m)
128
+ cmin = -2.85542011260986 # Minimum colored bathymetry value (m)
129
+ WaterColorMapFile = gui/water.rgb # Water color map file
130
+ WaterColorType = 0 # 0: spec. max depth, 1: auto
131
+ hmax = 2. # Maximum colored water depth (m)
132
+ hmin = 0. # Minimum plotted water depth (m)
133
+ InterceptionColorMapFile = # Intercepted water color map file
134
+ simax = 0.5 # Maximum colored intercepted water depth (m)
135
+ simin = 0. # Minimum plotted intercepted water depth (m)
136
+ gwmax = 6.98999977111816 # Maximum colored ground water level (m)
137
+ gwmin = 0. # Minimum colored ground water level (m)
138
+
ffmgen/timecalc.py ADDED
@@ -0,0 +1,107 @@
1
+ """EndDate / EndTime handling for the [time] group.
2
+
3
+ EndDate and EndTime are self-defined parameters (always written commented out,
4
+ 3Di ignores them). They are used to compute
5
+ ``NTimesteps = (End - Start) / Dt``, rounded up if not an integer.
6
+ """
7
+ import math
8
+
9
+ import pandas as pd
10
+
11
+ __all__ = ['END_DATE_COM', 'END_TIME_COM', 'ensure_end_rows', 'update_ntimesteps']
12
+
13
+ END_DATE_COM = 'End date (yyyy-mm-dd); self-defined, used to compute NTimesteps, not used in 3Di'
14
+ END_TIME_COM = 'End time (hh:mm:ss); self-defined, used to compute NTimesteps, not used in 3Di'
15
+
16
+
17
+ def _time_rows(data):
18
+ """{lower-case parameter name: row} of the [time] group (rows are the same list objects)."""
19
+ return {str(r[1]).strip().lower(): r for r in data.get('time', []) if str(r[1]).strip()}
20
+
21
+
22
+ def _num(s):
23
+ """'4.' / '1.d-2' / '5' -> float (Fortran 'd' exponent allowed)."""
24
+ return float(str(s).strip().lower().replace('d', 'e'))
25
+
26
+
27
+ def _parse_datetime(date_s, time_s):
28
+ """'2011-12-23' + '06:00:00' -> Timestamp."""
29
+ date_s, time_s = str(date_s).strip(), str(time_s).strip() or '00:00:00'
30
+ day = pd.to_datetime(date_s).normalize()
31
+ try:
32
+ tod = pd.to_timedelta(time_s)
33
+ except (ValueError, TypeError):
34
+ t = pd.to_datetime(time_s) # e.g. '1900-01-01 06:00:00' from Excel
35
+ tod = t - t.normalize()
36
+ return day + tod
37
+
38
+
39
+ def ensure_end_rows(data):
40
+ """Add EndDate/EndTime rows to [time] if missing (e.g. old xlsx/mdu).
41
+
42
+ Values are back-calculated from StartDate/StartTime + NTimesteps*Dt, so
43
+ the simulation length is unchanged. Modifies and returns ``data``.
44
+ """
45
+ if 'time' not in data:
46
+ return data
47
+ rows = data['time']
48
+ tr = _time_rows(data)
49
+ if 'enddate' in tr or 'endtime' in tr:
50
+ return data
51
+ end_d = end_t = ''
52
+ try:
53
+ start = _parse_datetime(tr['startdate'][2], tr['starttime'][2])
54
+ end = start + pd.Timedelta(seconds=_num(tr['ntimesteps'][2]) * _num(tr['dt'][2]))
55
+ end_d, end_t = end.strftime('%Y-%m-%d'), end.strftime('%H:%M:%S')
56
+ except Exception:
57
+ pass
58
+ keys = [str(r[1]).strip().lower() for r in rows]
59
+ idx = [keys.index(k) for k in ('startdate', 'starttime') if k in keys]
60
+ pos = max(idx) + 1 if idx else len(rows)
61
+ rows[pos:pos] = [[False, 'EndDate', end_d, END_DATE_COM],
62
+ [False, 'EndTime', end_t, END_TIME_COM]]
63
+ return data
64
+
65
+
66
+ def update_ntimesteps(data):
67
+ """Set NTimesteps = (EndDate EndTime - StartDate StartTime) / Dt, rounded up.
68
+
69
+ Modifies ``data`` in place. Returns NTimesteps, or None if EndDate and
70
+ EndTime are both empty. Raises ValueError on bad input.
71
+ """
72
+ tr = _time_rows(data)
73
+ end_d = str(tr['enddate'][2]).strip() if 'enddate' in tr else ''
74
+ end_t = str(tr['endtime'][2]).strip() if 'endtime' in tr else ''
75
+ if not end_d and not end_t:
76
+ print('EndDate/EndTime empty -> NTimesteps left unchanged.')
77
+ return None
78
+ for k in ('dt', 'startdate', 'starttime'):
79
+ if k not in tr:
80
+ raise ValueError('[time] %s is missing' % k)
81
+ start = _parse_datetime(tr['startdate'][2], tr['starttime'][2])
82
+ end = _parse_datetime(end_d or tr['startdate'][2], end_t)
83
+ dt = _num(tr['dt'][2])
84
+ if dt <= 0:
85
+ raise ValueError('Dt must be > 0')
86
+ total = (end - start).total_seconds()
87
+ if total <= 0:
88
+ raise ValueError('End (%s) must be after Start (%s)' % (end, start))
89
+ ratio = total / dt
90
+ n = int(round(ratio)) if abs(ratio - round(ratio)) < 1e-9 * max(1.0, ratio) else math.ceil(ratio)
91
+
92
+ if 'ntimesteps' in tr:
93
+ tr['ntimesteps'][0] = True
94
+ tr['ntimesteps'][2] = str(n)
95
+ else:
96
+ rows = data['time']
97
+ keys = [str(r[1]).strip().lower() for r in rows]
98
+ pos = keys.index('endtime') + 1 if 'endtime' in keys else len(rows)
99
+ rows.insert(pos, [True, 'NTimesteps', str(n), 'Number of timesteps'])
100
+
101
+ msg = 'NTimesteps = {0} ({1} -> {2}, {3:g} s / Dt {4:g} s'.format(
102
+ n, start.strftime('%Y-%m-%d %H:%M:%S'), end.strftime('%Y-%m-%d %H:%M:%S'), total, dt)
103
+ if n * dt != total:
104
+ msg += '; not divisible, rounded up -> model ends at {0}'.format(
105
+ (start + pd.Timedelta(seconds=n * dt)).strftime('%Y-%m-%d %H:%M:%S'))
106
+ print(msg + ')')
107
+ return n
@@ -0,0 +1,106 @@
1
+ Metadata-Version: 2.4
2
+ Name: ffmtools
3
+ Version: 0.1.0
4
+ Summary: Flood/flow modelling tools for 3Di subgrid: ffmgen (build .mdu files) and ffmclr (clean run outputs).
5
+ Author-email: aaronchh <aaronhsu219@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/AaronOET/ffmtools
8
+ Project-URL: Changelog, https://github.com/AaronOET/ffmtools/blob/main/CHANGELOG.md
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.9
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: pandas>=1.3
15
+ Requires-Dist: openpyxl>=3.0
16
+ Provides-Extra: test
17
+ Requires-Dist: pytest; extra == "test"
18
+ Dynamic: license-file
19
+
20
+ # ffmtools
21
+
22
+ ## ffmgen
23
+
24
+ Builds 3Di subgrid `.mdu` input files (plus a `_run_<name>.bat` launcher) from an
25
+ `mdu_prep.xlsx`-style workbook or an existing `.mdu`. Packaged from
26
+ `prototype/Singapore_003/mdugen_gui_v3_tk.py`.
27
+
28
+ ### Install
29
+
30
+ ```bat
31
+ cd D:\GITHUB_OET\ffmtools
32
+ pip install -e .
33
+ ```
34
+
35
+ ### GUI
36
+
37
+ ```bat
38
+ ffmgen :: loads .\mdu_prep.xlsx if present, else the built-in template
39
+ ffmgen gui STAMFORD_basis.mdu
40
+ python -m ffmgen
41
+ ```
42
+
43
+ Each tab is one parameter group (= one sheet of the workbook). The **On** checkbox
44
+ decides whether a line is active or written commented out with `#`.
45
+ `EndDate` / `EndTime` in the *time* tab are used on Preview/Generate to compute
46
+ `NTimesteps = (End - Start) / Dt` (rounded up if it does not divide evenly).
47
+
48
+ ### Command line
49
+
50
+ ```bat
51
+ ffmgen generate mdu_prep.xlsx -o runs -n STAMFORD_basis -t 4
52
+ ffmgen generate STAMFORD_basis.mdu --dry-run
53
+ ffmgen convert STAMFORD_basis.mdu mdu_prep.xlsx
54
+ ffmgen template STAMFORD_template.mdu
55
+ ```
56
+
57
+ ### Python
58
+
59
+ ```python
60
+ import ffmgen
61
+
62
+ data = ffmgen.ensure_end_rows(ffmgen.read_any("mdu_prep.xlsx"))
63
+ ffmgen.update_ntimesteps(data)
64
+ ffmgen.write_mdu(data, out_dir="runs", name="STAMFORD_basis", threads=4)
65
+ ```
66
+
67
+ ### Layout
68
+
69
+ | module | contents |
70
+ |---|---|
71
+ | `ffmgen.core` | parse/read/write `.mdu` and `.xlsx`, `to_mdu_text`, `write_mdu` |
72
+ | `ffmgen.timecalc` | `ensure_end_rows`, `update_ntimesteps` |
73
+ | `ffmgen.gui` | tkinter GUI (`App`, `run`) |
74
+ | `ffmgen.cli` | `ffmgen` console command |
75
+ | `ffmgen/templates/STAMFORD_basis.mdu` | built-in template |
76
+
77
+ ## ffmclr
78
+
79
+ Removes the files a 3Di (subgrid) run leaves in a model folder. Packaged from
80
+ `prototype/Singapore_003/clean_3di_outputs.py`. Dry run by default.
81
+
82
+ ```bat
83
+ ffmclr :: list what would be removed in the current folder
84
+ ffmclr D:\models\STAMFORD --yes :: delete
85
+ ffmclr --yes --keep-table :: keep the TableDataFile (faster restart)
86
+ ffmclr --move-to _old_run --yes :: move into <model>\_old_run instead of deleting
87
+ ffmclr -r :: also scan sub-folders
88
+ ```
89
+
90
+ Removed: `*_his.nc`, `*_map.nc`, `*_rst.nc`, `*.rst`, `*.dia`, `*_numlimdt.xyz`,
91
+ `*_timings.txt`, `.test`, and the `TableDataFile` named in the `.mdu`.
92
+ Never removed: anything referenced as input in an `.mdu`/`.ext`, and input
93
+ extensions (`.mdu .ini .ext .pli .pol .xyn .tim .tif .asc .rgb ...`).
94
+
95
+ ```python
96
+ import ffmclr
97
+ res = ffmclr.clean("D:/models/STAMFORD", dry_run=False, move_to="_old_run")
98
+ print(len(res.removed), res.failed)
99
+ ```
100
+
101
+ ## Tests
102
+
103
+ ```bat
104
+ pip install -e .[test]
105
+ pytest
106
+ ```