Skip to content

Commit 16f34be

Browse files
converted separate species to click
1 parent 4d0d082 commit 16f34be

File tree

3 files changed

+56
-56
lines changed

3 files changed

+56
-56
lines changed

pyproject.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ dependencies = [
3131
"scikit-learn",
3232
"scipy",
3333
"statsmodels>=0.13",
34-
"pint"
34+
"pint",
35+
"click"
3536
]
3637

3738
[project.optional-dependencies]
@@ -66,6 +67,7 @@ exclude = ["docs"]
6667

6768
[tool.pytest.ini_options]
6869
collect_ignore = ["setup.py"]
70+
testpaths = ["tests"]
6971

7072
[tool.setuptools.packages.find]
7173
include = ["vtools*"]
@@ -76,4 +78,6 @@ version_file="vtools/_version.py"
7678
version_scheme = "post-release"
7779
local_scheme = "dirty-tag"
7880

79-
[project.scripts]
81+
[project.scripts]
82+
vtl = "vtools.__main__:cli"
83+
# separate_species = "vtools.functions.separate_species:main" # suppressed, for now use "vtl separate_species"

vtools/__main__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import click
2+
from vtools.functions.separate_species import main as separate_main
3+
4+
5+
@click.group(help="vtools CLI tools for time series processing.")
6+
@click.help_option("-h", "--help") # Add the help option at the group level
7+
def cli():
8+
"""Main entry point for vtools commands."""
9+
pass
10+
11+
12+
# Register the commands
13+
cli.add_command(separate_main, "separate_species")

vtools/functions/separate_species.py

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
an example
99
"""
1010

11-
import argparse
12-
from vtools.datastore.read_ts import read_ts
1311
import datetime as dtm
12+
import re
13+
14+
import click
15+
from vtools.datastore.read_ts import read_ts
1416
from vtools.functions.filter import cosine_lanczos
15-
from vtools.data.vtime import hours, minutes
17+
from vtools.data.vtime import days, hours, minutes
1618

1719

1820
def separate_species(ts, noise_thresh_min=40):
@@ -105,67 +107,48 @@ def plot_result(ts, ts_semi, ts_diurnal, ts_sub_tide, station):
105107

106108

107109
################# 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:
152136
sdate = dtm.datetime(
153-
*list(map(int, re.split(r"[^\d]", args.stime)))
137+
*list(map(int, re.split(r"[^\d]", stime)))
154138
) # convert start time string input to datetime
155139
else:
156140
sdate = None
157-
if args.etime:
141+
if etime:
158142
edate = dtm.datetime(
159-
*list(map(int, re.split(r"[^\d]", args.etime)))
143+
*list(map(int, re.split(r"[^\d]", etime)))
160144
) # convert start time string input to datetime
161145
else:
162146
edate = None
163-
data_file = args.infile
164-
station_name = args.label
165-
outprefix = args.outprefix
147+
data_file = infile
148+
station_name = label
166149
if not outprefix:
167150
outprefix = station_name.replace(" ", "_")
168-
do_plot = args.plot
151+
do_plot = plot
169152

170153
ts = read_ts(data_file, None, None)
171154
astart = max(ts.start, sdate - days(5)) if sdate else ts.start

0 commit comments

Comments
 (0)