@main.command()
@click.argument("case_path", type=click.Path(exists=True, dir_okay=False, path_type=Path))
@click.argument("excluded_path", type=click.Path(exists=True, dir_okay=False, path_type=Path))
@click.option(
"-o", "--output", "output_path",
type=click.Path(dir_okay=False, path_type=Path),
required=True,
help="Path to write the reduced multi-sheet xlsx.",
)
@click.option(
"--summary-txt",
type=click.Path(dir_okay=False, path_type=Path),
default=None,
help="Optional path to also dump the summary diary as a .txt file.",
)
@click.option(
"--pf/--no-pf",
default=True,
help="Solve a DC PF on the full model before redistributing loads (default: --pf, matches Pf_flag=1).",
)
@click.option(
"--gentype-xlsx",
type=click.Path(exists=True, dir_okay=False, path_type=Path),
default=None,
help="If the case file is a MATPOWER .m that delegates Gen/Bus/etc. data to an xlsx, point to it here.",
)
def reduce(
case_path: Path,
excluded_path: Path,
output_path: Path,
summary_txt: Path | None,
pf: bool,
gentype_xlsx: Path | None,
) -> None:
"""Run network reduction on CASE_PATH using buses listed in EXCLUDED_PATH.
\b
Examples
simplenet reduce case9.m excluded.csv -o reduced.xlsx
simplenet reduce matlab2_WECC.xlsx excluded_550.csv -o result.xlsx
"""
case = _load_case(case_path)
if gentype_xlsx is not None:
sup = load_xlsx(gentype_xlsx)
if sup.bus.size:
case.bus = sup.bus
if sup.gen.size:
case.gen = sup.gen
if sup.branch.size:
case.branch = sup.branch
if sup.gencost is not None:
case.gencost = sup.gencost
if sup.gentype is not None:
case.gentype = sup.gentype
if sup.genfuel is not None:
case.genfuel = sup.genfuel
if sup.bus_name is not None:
case.bus_name = sup.bus_name
excluded = load_excluded_nodes(excluded_path)
click.echo(f"Loaded {case.n_bus()} buses, {case.n_branch()} branches, {case.n_gen()} gens", err=True)
click.echo(f"Excluding {excluded.size} buses", err=True)
result = reduce_network(case, excluded, pf_flag=pf)
dump_xlsx(result.reduced_case, output_path, summary=result.summary)
if summary_txt is not None:
summary_txt.write_text(result.summary)
click.echo(result.summary)