Skip to content

Commit a4b81e4

Browse files
convert combine_consume to click
1 parent 2ec47ca commit a4b81e4

3 files changed

Lines changed: 138 additions & 107 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ convert_mesh = "schimpy.convert_mesh:convert_mesh_cli"
8989
convert_polygons = "schimpy.convert_polygons:convert_polygons_cli"
9090
convert_linestrings = "schimpy.convert_linestrings:convert_linestrings_cli"
9191
convert_points = "schimpy.convert_points:convert_points_cli"
92-
combine_consume = "schimpy.combine_consume:main"
92+
combine_consume = "schimpy.combine_consume:combine_consume_cli"
9393
prepare_schism = "schimpy.prepare_schism:main"
9494
hotstart_inventory = "schimpy.hotstart_inventory:main"
9595
create_vgrid_lsc2 = "schimpy.create_vgrid_lsc2:main"

schimpy/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from schimpy.convert_polygons import convert_polygons_cli
77
from schimpy.convert_linestrings import convert_linestrings_cli
88
from schimpy.convert_points import convert_points_cli
9+
from schimpy.combine_consume import combine_consume
910

10-
# from schimpy.combine_consume import combine_consume
1111
# from schimpy.prepare_schism import prepare_schism
1212
# from schimpy.hotstart_inventory import hotstart_inventory
1313
# from schimpy.create_vgrid_lsc2 import create_vgrid_lsc2
@@ -39,7 +39,7 @@ def cli():
3939
cli.add_command(convert_polygons_cli, "convert_polygons")
4040
cli.add_command(convert_linestrings_cli, "convert_linestrings")
4141
cli.add_command(convert_points_cli, "convert_points")
42-
# cli.add_command(combine_consume, "combine_consume")
42+
cli.add_command(combine_consume_cli, "combine_consume")
4343
# cli.add_command(prepare_schism, "prepare_schism")
4444
# cli.add_command(hotstart_inventory, "hotstart_inventory")
4545
# cli.add_command(create_vgrid_lsc2, "create_vgrid_lsc2")

schimpy/combine_consume.py

Lines changed: 135 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import time
99
from shutil import copyfile
1010
import datetime as dtm
11+
import click
1112

1213
FBASE_5_6 = ["schout.nc"]
1314

@@ -33,8 +34,8 @@ def do_combine_hotstart(wdir, step):
3334

3435

3536
def split(alist, n):
36-
k, m = divmod(len(a), n)
37-
return (a[i * k + min(i, m) : (i + 1) * k + min(i + 1, m)] for i in xrange(n))
37+
k, m = divmod(len(alist), n)
38+
return (alist[i * k + min(i, m) : (i + 1) * k + min(i + 1, m)] for i in range(n))
3839

3940

4041
def appears_done(wdir, fb, exten, firstblock, lastblock):
@@ -330,104 +331,38 @@ def detect_min_max_index(wdir, sample_fbase):
330331
return ndxmin, ndxmax
331332

332333

333-
def create_arg_parser():
334-
"""Create argument parser for"""
335-
import argparse
336-
337-
parser = argparse.ArgumentParser()
338-
parser.add_argument(
339-
"--start", type=str, help="start date of simulation in format like yyyy-mm-dd"
340-
)
341-
parser.add_argument(
342-
"--dir",
343-
default=".",
344-
type=str,
345-
help="directory in which output will be processed",
346-
)
347-
parser.add_argument(
348-
"--fbase",
349-
type=str,
350-
default=["schout.nc"],
351-
nargs="+",
352-
help="File base name. This will either be 'schout.nc' or a list of files like 'elev.61,hvel.64,salt.63'. ",
353-
)
354-
parser.add_argument(
355-
"--hotstart", action="store_true", help="Combine hotstart in addition to fbase"
356-
)
357-
parser.add_argument(
358-
"--hotstart_only",
359-
action="store_true",
360-
help="Only combine hotstart -- avoids file search errors when nothing left to combine",
361-
)
362-
parser.add_argument(
363-
"--consume",
364-
action="store_true",
365-
help="Delete combined files or unrequested files",
366-
)
367-
parser.add_argument(
368-
"--assume_done",
369-
action="store_true",
370-
help="Assume that the simulation is finished, so incomplete blocks can be deleted",
371-
)
372-
parser.add_argument(
373-
"--combiner", default=f"{combine_exe}", help="Executable for combine_output."
374-
)
375-
parser.add_argument(
376-
"--hot_combiner",
377-
default=f"{combine_hotstart_exe}",
378-
help="Executable for combine_output.",
379-
)
380-
parser.add_argument(
381-
"--sndx", default=None, type=int, help="First index to consider for processing."
382-
)
383-
parser.add_argument(
384-
"--endx", default=None, type=int, help="Last index to consider for processing."
385-
)
386-
parser.add_argument(
387-
"--sndx_hot",
388-
default=None,
389-
type=int,
390-
help="First index to consider for processing.",
391-
)
392-
parser.add_argument(
393-
"--endx_hot",
394-
default=None,
395-
type=int,
396-
help="Last index to consider for processing.",
397-
)
398-
399-
parser.add_argument(
400-
"--datefile",
401-
type=str,
402-
help="File containing list of dates. Each line can have a single date or comma-separated pair indicating block start and end. Blank lines are ignored and # is comment character that can comment the entire line or be placed at the side of a line after the date(s). ",
403-
)
404-
parser.add_argument(
405-
"--blocks_per_day",
406-
type=int,
407-
default=1,
408-
help="Blocks used to store 1 day worth of output.",
409-
)
410-
return parser
411-
412-
413-
def combine_consume(is_test=False):
414-
parser = create_arg_parser()
415-
args = parser.parse_args()
416-
combine_exe = args.combiner
417-
combine_hotstart_exe = args.hot_combiner
334+
def combine_consume(
335+
start,
336+
dir,
337+
fbase,
338+
hotstart,
339+
hotstart_only,
340+
consume,
341+
assume_done,
342+
combiner,
343+
hot_combiner,
344+
sndx,
345+
endx,
346+
sndx_hot,
347+
endx_hot,
348+
datefile,
349+
blocks_per_day,
350+
is_test=False,
351+
):
352+
combine_exe = combiner
353+
combine_hotstart_exe = hot_combiner
418354

419-
wdir = args.dir
420-
fbase = args.fbase
421-
blocks_per_day = args.blocks_per_day
422-
start = dtm.datetime.strptime(args.start, "%Y-%m-%d")
423-
consume = args.consume
355+
wdir = dir
356+
blocks_per_day = blocks_per_day
357+
start = dtm.datetime.strptime(start, "%Y-%m-%d")
358+
consume = consume
424359

425-
hotstart = args.hotstart or args.hotstart_only
426-
hotstart_only = args.hotstart_only
427-
datefile = args.datefile.strip()
428-
sndx = args.sndx
429-
endx = args.endx
430-
assume_done = args.assume_done
360+
hotstart = hotstart or hotstart_only
361+
hotstart_only = hotstart_only
362+
datefile = datefile.strip() if datefile else None
363+
sndx = sndx
364+
endx = endx
365+
assume_done = assume_done
431366

432367
if is_test:
433368
setup(wdir, fbase)
@@ -451,7 +386,6 @@ def combine_consume(is_test=False):
451386
wanted.update(range(b[0], b[1] + 1))
452387
u = range(ndxmin, ndxmax + 1)
453388
unwanted = [ii for ii in u if not ii in wanted]
454-
unwanted = [ii for ii in u if not ii in wanted]
455389
wanted = list(wanted)
456390
wanted.sort()
457391
unwanted.sort()
@@ -464,11 +398,108 @@ def combine_consume(is_test=False):
464398
combine_hotstart(wdir, combine_hotstart_exe, consume=consume)
465399

466400

467-
def main():
468-
combine_consume()
401+
@click.command()
402+
@click.option(
403+
"--start",
404+
required=True,
405+
type=str,
406+
help="start date of simulation in format like yyyy-mm-dd",
407+
)
408+
@click.option(
409+
"--dir",
410+
"dir",
411+
default=".",
412+
type=click.Path(),
413+
help="directory in which output will be processed",
414+
)
415+
@click.option(
416+
"--fbase",
417+
multiple=True,
418+
default=["schout.nc"],
419+
help="File base name. This will either be 'schout.nc' or a list of files like 'elev.61,hvel.64,salt.63'. ",
420+
)
421+
@click.option("--hotstart", is_flag=True, help="Combine hotstart in addition to fbase")
422+
@click.option(
423+
"--hotstart_only",
424+
is_flag=True,
425+
help="Only combine hotstart -- avoids file search errors when nothing left to combine",
426+
)
427+
@click.option(
428+
"--consume", is_flag=True, help="Delete combined files or unrequested files"
429+
)
430+
@click.option(
431+
"--assume_done",
432+
is_flag=True,
433+
help="Assume that the simulation is finished, so incomplete blocks can be deleted",
434+
)
435+
@click.option(
436+
"--combiner", default=f"{combine_exe}", help="Executable for combine_output."
437+
)
438+
@click.option(
439+
"--hot_combiner",
440+
default=f"{combine_hotstart_exe}",
441+
help="Executable for combine_output.",
442+
)
443+
@click.option(
444+
"--sndx", default=None, type=int, help="First index to consider for processing."
445+
)
446+
@click.option(
447+
"--endx", default=None, type=int, help="Last index to consider for processing."
448+
)
449+
@click.option(
450+
"--sndx_hot", default=None, type=int, help="First index to consider for processing."
451+
)
452+
@click.option(
453+
"--endx_hot", default=None, type=int, help="Last index to consider for processing."
454+
)
455+
@click.option(
456+
"--datefile",
457+
type=str,
458+
help="File containing list of dates. Each line can have a single date or comma-separated pair indicating block start and end. Blank lines are ignored and # is comment character that can comment the entire line or be placed at the side of a line after the date(s). ",
459+
)
460+
@click.option(
461+
"--blocks_per_day",
462+
type=int,
463+
default=1,
464+
help="Blocks used to store 1 day worth of output.",
465+
)
466+
def combine_consume_cli(
467+
start,
468+
dir,
469+
fbase,
470+
hotstart,
471+
hotstart_only,
472+
consume,
473+
assume_done,
474+
combiner,
475+
hot_combiner,
476+
sndx,
477+
endx,
478+
sndx_hot,
479+
endx_hot,
480+
datefile,
481+
blocks_per_day,
482+
):
483+
combine_consume(
484+
start,
485+
dir,
486+
list(fbase),
487+
hotstart,
488+
hotstart_only,
489+
consume,
490+
assume_done,
491+
combiner,
492+
hot_combiner,
493+
sndx,
494+
endx,
495+
sndx_hot,
496+
endx_hot,
497+
datefile,
498+
blocks_per_day,
499+
)
469500

470501

471502
if __name__ == "__main__":
472-
wdir = "./test_archive"
473-
fbase = FBASE_5_6
474-
combine_consume()
503+
# wdir = "./test_archive"
504+
# fbase = FBASE_5_6
505+
combine_consume_cli()

0 commit comments

Comments
 (0)