|
8 | 8 | import sys |
9 | 9 | import logging |
10 | 10 | import os |
| 11 | +import argparse |
11 | 12 | from datetime import datetime, timedelta |
12 | 13 | import numpy as np |
13 | 14 |
|
@@ -101,7 +102,7 @@ class OpenDriftGUI(tk.Tk): |
101 | 102 | 'seed:m3_per_hour': {'default': 100} |
102 | 103 | } |
103 | 104 |
|
104 | | - def __init__(self): |
| 105 | + def __init__(self, forcing_files): |
105 | 106 |
|
106 | 107 | tk.Tk.__init__(self) |
107 | 108 |
|
@@ -366,10 +367,7 @@ def __init__(self): |
366 | 367 | ############## |
367 | 368 | self.set_model(list(self.opendrift_models)[0]) |
368 | 369 |
|
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): |
373 | 371 | tk.Label(self.forcing, text=ff.strip(), wraplength=650, font=('Courier', 8)).grid( |
374 | 372 | row=i, column=0, sticky=tk.W) |
375 | 373 |
|
@@ -787,8 +785,7 @@ def run_opendrift(self): |
787 | 785 | nothing |
788 | 786 | self.o.set_config(se, val) |
789 | 787 |
|
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) |
792 | 789 |
|
793 | 790 | self.o.seed_cone(lon=lon, lat=lat, radius=radius, |
794 | 791 | time=start_time)#, #cone=cone, |
@@ -881,15 +878,34 @@ def run_opendrift(self): |
881 | 878 | command=lambda: self.handle_result( |
882 | 879 | 'copy_netcdf')).grid(row=81, column=1) |
883 | 880 |
|
884 | | -def main(): |
| 881 | +def main(readers=None): |
885 | 882 | OpenDriftGUI().mainloop() |
886 | 883 |
|
887 | 884 | if __name__ == '__main__': |
888 | 885 |
|
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: |
892 | 907 | os.environ['OPENDRIFT_GUI_OUTPUT'] = 'terminal' |
893 | 908 | else: |
894 | 909 | os.environ['OPENDRIFT_GUI_OUTPUT'] = 'gui' |
895 | | - OpenDriftGUI().mainloop() |
| 910 | + |
| 911 | + OpenDriftGUI(forcing_files=forcing_files).mainloop() |
0 commit comments