66import matplotlib .pyplot as plt
77from matplotlib .backends .backend_tkagg import FigureCanvasTkAgg
88from matplotlib .figure import Figure
9+ from aeolis .constants import DEFAULT_CONFIG
910
1011try :
1112 import netCDF4
1213 HAVE_NETCDF = True
1314except ImportError :
1415 HAVE_NETCDF = False
1516
16- # Default configuration file path
17- configfile = r'C:\Users\svries\Documents\GitHub\OE_aeolis-python\aeolis\examples\2D\Barchan_dune\aeolis.txt'
18-
1917# Function to prompt the user to select a configuration file
2018def prompt_file ():
2119 file_path = filedialog .askopenfilename (
22- initialdir = os .path . dirname ( configfile ),
20+ initialdir = os .getcwd ( ),
2321 title = "Select config file" ,
2422 filetypes = (("Text files" , "*.txt" ), ("All files" , "*.*" ))
2523 )
26- return file_path if file_path else configfile
24+ return file_path
2725
2826# Prompt the user to select a configuration file or use the default
29- configfile = prompt_file ()
27+ selected_file = prompt_file ()
28+
3029# Read the configuration file into a dictionary
31- dic = aeolis .inout .read_configfile (configfile )
30+ if selected_file :
31+ # User selected a file
32+ configfile = selected_file
33+ dic = aeolis .inout .read_configfile (configfile )
34+ else :
35+ # User canceled - load empty fields with defaults
36+ configfile = "No file selected"
37+ # Use the default configuration from constants
38+ dic = DEFAULT_CONFIG .copy ()
3239
3340class AeolisGUI :
3441 def __init__ (self , root , dic ):
@@ -42,6 +49,17 @@ def __init__(self, root, dic):
4249
4350 self .create_widgets ()
4451
52+ def get_config_dir (self ):
53+ """Get the directory of the config file, or current directory if no file selected"""
54+ global configfile
55+ if configfile and configfile != "No file selected" and os .path .exists (configfile ):
56+ return os .path .dirname (configfile )
57+ elif configfile and configfile != "No file selected" and os .path .dirname (configfile ):
58+ # configfile might be a path even if file doesn't exist yet
59+ return os .path .dirname (configfile )
60+ else :
61+ return os .getcwd ()
62+
4563 def create_widgets (self ):
4664 # Create a tab control widget
4765 tab_control = ttk .Notebook (self .root )
@@ -61,7 +79,8 @@ def create_label_entry(self, tab, text, value, row):
6179 label = ttk .Label (tab , text = text )
6280 label .grid (row = row , column = 0 , sticky = W )
6381 entry = ttk .Entry (tab )
64- entry .insert (0 , str (value ))
82+ # Convert None to empty string for cleaner display
83+ entry .insert (0 , '' if value is None else str (value ))
6584 entry .grid (row = row , column = 1 , sticky = W )
6685 return entry
6786
@@ -127,7 +146,9 @@ def create_domain_tab(self, tab_control):
127146 label = ttk .Label (params_frame , text = f"{ field } :" )
128147 label .grid (row = i , column = 0 , sticky = W , pady = 2 )
129148 entry = ttk .Entry (params_frame , width = 35 )
130- entry .insert (0 , str (self .dic .get (field , '' )))
149+ value = self .dic .get (field , '' )
150+ # Convert None to empty string for cleaner display
151+ entry .insert (0 , '' if value is None else str (value ))
131152 entry .grid (row = i , column = 1 , sticky = W , pady = 2 , padx = (0 , 5 ))
132153 self .entries [field ] = entry
133154
@@ -175,7 +196,7 @@ def create_domain_tab(self, tab_control):
175196 def browse_file (self , entry_widget ):
176197 """Open file dialog to select a file and update the entry widget"""
177198 # Get initial directory from config file location
178- initial_dir = os . path . dirname ( configfile )
199+ initial_dir = self . get_config_dir ( )
179200
180201 # Get current value to determine initial directory
181202 current_value = entry_widget .get ()
@@ -198,15 +219,15 @@ def browse_file(self, entry_widget):
198219 # Update entry if a file was selected
199220 if file_path :
200221 # Try to make path relative to config file directory for portability
201- config_dir = os . path . dirname ( configfile )
222+ config_dir = self . get_config_dir ( )
202223 try :
203224 rel_path = os .path .relpath (file_path , config_dir )
204225 # Use relative path if it doesn't go up too many levels
205226 parent_dir = os .pardir + os .sep + os .pardir + os .sep
206227 if not rel_path .startswith (parent_dir ):
207228 file_path = rel_path
208- except ValueError :
209- # Different drives on Windows, keep absolute path
229+ except ( ValueError , TypeError ) :
230+ # Different drives on Windows or invalid path , keep absolute path
210231 pass
211232
212233 entry_widget .delete (0 , END )
@@ -215,7 +236,7 @@ def browse_file(self, entry_widget):
215236 def browse_nc_file (self ):
216237 """Open file dialog to select a NetCDF file"""
217238 # Get initial directory from config file location
218- initial_dir = os . path . dirname ( configfile )
239+ initial_dir = self . get_config_dir ( )
219240
220241 # Get current value to determine initial directory
221242 current_value = self .nc_file_entry .get ()
@@ -238,15 +259,15 @@ def browse_nc_file(self):
238259 # Update entry if a file was selected
239260 if file_path :
240261 # Try to make path relative to config file directory for portability
241- config_dir = os . path . dirname ( configfile )
262+ config_dir = self . get_config_dir ( )
242263 try :
243264 rel_path = os .path .relpath (file_path , config_dir )
244265 # Use relative path if it doesn't go up too many levels
245266 parent_dir = os .pardir + os .sep + os .pardir + os .sep
246267 if not rel_path .startswith (parent_dir ):
247268 file_path = rel_path
248- except ValueError :
249- # Different drives on Windows, keep absolute path
269+ except ( ValueError , TypeError ) :
270+ # Different drives on Windows or invalid path , keep absolute path
250271 pass
251272
252273 self .nc_file_entry .delete (0 , END )
@@ -261,7 +282,7 @@ def load_new_config(self):
261282
262283 # Open file dialog
263284 file_path = filedialog .askopenfilename (
264- initialdir = os . path . dirname ( configfile ),
285+ initialdir = self . get_config_dir ( ),
265286 title = "Select config file" ,
266287 filetypes = (("Text files" , "*.txt" ), ("All files" , "*.*" ))
267288 )
@@ -296,7 +317,7 @@ def browse_save_location(self):
296317 """Browse for save location for config file"""
297318 # Open file dialog for saving
298319 file_path = filedialog .asksaveasfilename (
299- initialdir = os . path . dirname ( configfile ),
320+ initialdir = self . get_config_dir ( ),
300321 title = "Save config file as" ,
301322 defaultextension = ".txt" ,
302323 filetypes = (("Text files" , "*.txt" ), ("All files" , "*.*" ))
@@ -1273,7 +1294,7 @@ def plot_data(self, file_key, title):
12731294 return
12741295
12751296 # Get the directory of the config file to resolve relative paths
1276- config_dir = os . path . dirname ( configfile )
1297+ config_dir = self . get_config_dir ( )
12771298
12781299 # Load the data file
12791300 if not os .path .isabs (data_file ):
@@ -1372,7 +1393,7 @@ def plot_combined(self):
13721393 return
13731394
13741395 # Get the directory of the config file to resolve relative paths
1375- config_dir = os . path . dirname ( configfile )
1396+ config_dir = self . get_config_dir ( )
13761397
13771398 # Load the bed file
13781399 if not os .path .isabs (bed_file ):
@@ -1482,7 +1503,7 @@ def plot_nc_bed_level(self):
14821503 return
14831504
14841505 # Get the directory of the config file to resolve relative paths
1485- config_dir = os . path . dirname ( configfile )
1506+ config_dir = self . get_config_dir ( )
14861507
14871508 # Load the NC file
14881509 if not os .path .isabs (nc_file ):
@@ -1591,7 +1612,7 @@ def plot_nc_wind(self):
15911612 if not nc_file :
15921613 messagebox .showwarning ("Warning" , "No NetCDF file specified!" )
15931614 return
1594- config_dir = os . path . dirname ( configfile )
1615+ config_dir = self . get_config_dir ( )
15951616 nc_file_path = os .path .join (config_dir , nc_file ) if not os .path .isabs (nc_file ) else nc_file
15961617 if not os .path .exists (nc_file_path ):
15971618 messagebox .showerror ("Error" , f"NetCDF file not found: { nc_file_path } " )
@@ -1719,7 +1740,7 @@ def enable_overlay_vegetation(self):
17191740 if not nc_file :
17201741 messagebox .showwarning ("Warning" , "No NetCDF file specified!" )
17211742 return
1722- config_dir = os . path . dirname ( configfile )
1743+ config_dir = self . get_config_dir ( )
17231744 nc_file_path = os .path .join (config_dir , nc_file ) if not os .path .isabs (nc_file ) else nc_file
17241745 if not os .path .exists (nc_file_path ):
17251746 messagebox .showerror ("Error" , f"NetCDF file not found: { nc_file_path } " )
0 commit comments