Skip to content

Commit 568ae4c

Browse files
committed
opendrift_gui now takes --forcing as an optional commandline argument, for user defined file with list of forcing datasets
1 parent afbef0a commit 568ae4c

1 file changed

Lines changed: 28 additions & 12 deletions

File tree

opendrift/scripts/opendrift_gui.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99
import logging
1010
import os
11+
import argparse
1112
from datetime import datetime, timedelta
1213
import numpy as np
1314

@@ -101,7 +102,7 @@ class OpenDriftGUI(tk.Tk):
101102
'seed:m3_per_hour': {'default': 100}
102103
}
103104

104-
def __init__(self):
105+
def __init__(self, forcing_files):
105106

106107
tk.Tk.__init__(self)
107108

@@ -366,10 +367,7 @@ def __init__(self):
366367
##############
367368
self.set_model(list(self.opendrift_models)[0])
368369

369-
with open(files('opendrift.scripts').joinpath('data_sources.txt')) as fd:
370-
forcingfiles = fd.readlines()
371-
372-
for i, ff in enumerate(forcingfiles):
370+
for i, ff in enumerate(forcing_files):
373371
tk.Label(self.forcing, text=ff.strip(), wraplength=650, font=('Courier', 8)).grid(
374372
row=i, column=0, sticky=tk.W)
375373

@@ -787,8 +785,7 @@ def run_opendrift(self):
787785
nothing
788786
self.o.set_config(se, val)
789787

790-
forcing_file = str(files('opendrift.scripts').joinpath('data_sources.txt'))
791-
self.o.add_readers_from_file(forcing_file)
788+
self.o.add_readers_from_list(forcing_files)
792789

793790
self.o.seed_cone(lon=lon, lat=lat, radius=radius,
794791
time=start_time)#, #cone=cone,
@@ -881,15 +878,34 @@ def run_opendrift(self):
881878
command=lambda: self.handle_result(
882879
'copy_netcdf')).grid(row=81, column=1)
883880

884-
def main():
881+
def main(readers=None):
885882
OpenDriftGUI().mainloop()
886883

887884
if __name__ == '__main__':
888885

889-
# Add any argument to redirect output to terminal instead of GUI window.
890-
# TODO: must be a better way to pass arguments to Tkinter?
891-
if len(sys.argv) > 1:
886+
parser = argparse.ArgumentParser()
887+
parser.add_argument('-f', '--forcing', type=str, help='A file with URLs/names of forcing datasets, overriding built-in data_sources.txt')
888+
parser.add_argument('-t', '--terminal', action='store_true', help='redirect otput to terminal instead of GUI')
889+
890+
args = parser.parse_args()
891+
892+
forcing_files_ok = False
893+
if args.forcing is not None:
894+
try:
895+
with open(args.forcing) as fd:
896+
forcing_files = fd.readlines()
897+
forcing_files_ok = True
898+
except:
899+
print(f'Could not read {args.forcing}, using default OpenDrift forcing configuration instead.')
900+
if forcing_files_ok is False:
901+
with open(files('opendrift.scripts').joinpath('data_sources.txt')) as fd:
902+
forcing_files = fd.readlines()
903+
904+
## Add any argument to redirect output to terminal instead of GUI window.
905+
## TODO: must be a better way to pass arguments to Tkinter?
906+
if args.terminal is True:
892907
os.environ['OPENDRIFT_GUI_OUTPUT'] = 'terminal'
893908
else:
894909
os.environ['OPENDRIFT_GUI_OUTPUT'] = 'gui'
895-
OpenDriftGUI().mainloop()
910+
911+
OpenDriftGUI(forcing_files=forcing_files).mainloop()

0 commit comments

Comments
 (0)