|
8 | 8 | an example |
9 | 9 | """ |
10 | 10 |
|
11 | | -import argparse |
12 | | -from vtools.datastore.read_ts import read_ts |
13 | 11 | import datetime as dtm |
| 12 | +import re |
| 13 | + |
| 14 | +import click |
| 15 | +from vtools.datastore.read_ts import read_ts |
14 | 16 | from vtools.functions.filter import cosine_lanczos |
15 | | -from vtools.data.vtime import hours, minutes |
| 17 | +from vtools.data.vtime import days, hours, minutes |
16 | 18 |
|
17 | 19 |
|
18 | 20 | def separate_species(ts, noise_thresh_min=40): |
@@ -105,67 +107,48 @@ def plot_result(ts, ts_semi, ts_diurnal, ts_sub_tide, station): |
105 | 107 |
|
106 | 108 |
|
107 | 109 | ################# command line application ##################### |
108 | | -def create_arg_parser(): |
109 | | - import textwrap |
110 | | - |
111 | | - parser = argparse.ArgumentParser( |
112 | | - formatter_class=argparse.RawDescriptionHelpFormatter, |
113 | | - epilog=textwrap.dedent( |
114 | | - """ |
115 | | - ============== Example ================== |
116 | | - > separate_species.py --stime=2009-03-12 --etime=2010-01-01 --label="San Francisco" --outprefix=sf --plot 9414290_gageheight.csv |
117 | | - """ |
118 | | - ), |
119 | | - description="""Script to filter a tide into its subtidal, diurnal and semidiurnal components (and residual noise""", |
120 | | - ) |
121 | | - parser.add_argument( |
122 | | - "--stime", |
123 | | - default=None, |
124 | | - required=False, |
125 | | - help="Start time in ISO-like format 2009-03-12T00:00:00. Time part and 'T' are optional.", |
126 | | - ) |
127 | | - parser.add_argument("--etime", default=None, required=False, help="End time.") |
128 | | - parser.add_argument( |
129 | | - "--plot", |
130 | | - default=False, |
131 | | - required=False, |
132 | | - action="store_true", |
133 | | - help="Plot time series in matplotlib", |
134 | | - ) |
135 | | - parser.add_argument( |
136 | | - "--label", default="Tide", required=False, help="Label for station in plots" |
137 | | - ) |
138 | | - parser.add_argument( |
139 | | - "--outprefix", |
140 | | - default=None, |
141 | | - help="Output file prefix (species name will be added). If omitted, the label will be used", |
142 | | - ) |
143 | | - parser.add_argument("infile", help="Input file.") |
144 | | - |
145 | | - return parser |
146 | | - |
147 | | - |
148 | | -def main(): |
149 | | - parser = create_arg_parser() |
150 | | - args = parser.parse_args() |
151 | | - if args.stime: |
| 110 | +@click.command( |
| 111 | + context_settings={"help_option_names": ["-h", "--help"]}, |
| 112 | + help=( |
| 113 | + "Script to filter a tide into its subtidal, diurnal and semidiurnal components " |
| 114 | + "(and residual noise).\n\n" |
| 115 | + "Example:\n" |
| 116 | + " separate_species.py --stime=2009-03-12 --etime=2010-01-01 " |
| 117 | + "--label=\"San Francisco\" --outprefix=sf --plot 9414290_gageheight.csv" |
| 118 | + ), |
| 119 | +) |
| 120 | +@click.option( |
| 121 | + "--stime", |
| 122 | + default=None, |
| 123 | + help="Start time in ISO-like format 2009-03-12T00:00:00. Time part and 'T' are optional.", |
| 124 | +) |
| 125 | +@click.option("--etime", default=None, help="End time.") |
| 126 | +@click.option("--plot", is_flag=True, help="Plot time series in matplotlib") |
| 127 | +@click.option("--label", default="Tide", help="Label for station in plots") |
| 128 | +@click.option( |
| 129 | + "--outprefix", |
| 130 | + default=None, |
| 131 | + help="Output file prefix (species name will be added). If omitted, the label will be used", |
| 132 | +) |
| 133 | +@click.argument("infile") |
| 134 | +def main(stime, etime, plot, label, outprefix, infile): |
| 135 | + if stime: |
152 | 136 | sdate = dtm.datetime( |
153 | | - *list(map(int, re.split(r"[^\d]", args.stime))) |
| 137 | + *list(map(int, re.split(r"[^\d]", stime))) |
154 | 138 | ) # convert start time string input to datetime |
155 | 139 | else: |
156 | 140 | sdate = None |
157 | | - if args.etime: |
| 141 | + if etime: |
158 | 142 | edate = dtm.datetime( |
159 | | - *list(map(int, re.split(r"[^\d]", args.etime))) |
| 143 | + *list(map(int, re.split(r"[^\d]", etime))) |
160 | 144 | ) # convert start time string input to datetime |
161 | 145 | else: |
162 | 146 | edate = None |
163 | | - data_file = args.infile |
164 | | - station_name = args.label |
165 | | - outprefix = args.outprefix |
| 147 | + data_file = infile |
| 148 | + station_name = label |
166 | 149 | if not outprefix: |
167 | 150 | outprefix = station_name.replace(" ", "_") |
168 | | - do_plot = args.plot |
| 151 | + do_plot = plot |
169 | 152 |
|
170 | 153 | ts = read_ts(data_file, None, None) |
171 | 154 | astart = max(ts.start, sdate - days(5)) if sdate else ts.start |
|
0 commit comments