Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cwmscli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def cli(


cli.add_command(usgs_group, name="usgs")
cli.add_command(commands_cwms.shefcritimport)
cli.add_command(commands_cwms.shef_group)
cli.add_command(commands_cwms.csv2cwms_cmd)
cli.add_command(commands_cwms.update_cli_cmd)
cli.add_command(commands_cwms.blob_group)
Expand Down
82 changes: 78 additions & 4 deletions cwmscli/commands/commands_cwms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,23 @@
from cwmscli.utils.version import get_cwms_cli_version


@click.command(
"shefcritimport",
# region SHEF
# ================================================================================
# SHEF
# ================================================================================
@click.group(
"shef",
help="Manage SHEF file imports (crit files, .in configuration files)",
)
def shef_group():
pass


# ================================================================================
# Import Crit File
# ================================================================================
@shef_group.command(
"import_crit",
help="Import SHEF crit file into timeseries group for SHEF file processing",
)
@click.option(
Expand All @@ -42,9 +57,10 @@
)
@common_api_options
@api_key_loc_option
@click.option("--dry-run", is_flag=True, help="Show request; do not send.")
@requires(reqs.cwms)
def shefcritimport(filename, office, api_root, api_key, api_key_loc):
from cwmscli.commands.shef_critfile_import import import_shef_critfile
def shef_import_crit(filename, office, api_root, api_key, api_key_loc, dry_run):
from cwmscli.commands.shef.import_critfile import import_shef_critfile
from cwmscli.utils import get_api_key

api_key = get_api_key(api_key, api_key_loc)
Expand All @@ -53,9 +69,67 @@ def shefcritimport(filename, office, api_root, api_key, api_key_loc):
office_id=office,
api_root=api_root,
api_key=api_key,
dry_run=dry_run,
)


# ================================================================================
# Import .in File
# ================================================================================
@shef_group.command(
"import_infile",
help="Import SHEF .in file into timeseries group for SHEF file processing",
)
@click.option(
"-f",
"--filename",
required=True,
type=str,
help="filename of SHEF .in file to be processed",
)
@click.option(
"-g",
"--group-name",
required=True,
type=str,
help="CWMS timeseries group name",
)
@click.option(
"--category",
type=str,
default="SHEF Export",
show_default=True,
help="Timeseries category ID",
)
@common_api_options
@api_key_loc_option
@click.option(
"--dry-run",
is_flag=True,
help="Parse the .in file and print the JSON payload without posting to the API.",
)
@requires(reqs.cwms)
def shef_import_infile(
filename, group_name, category, office, api_root, api_key, api_key_loc, dry_run
):
from cwmscli.commands.shef.import_infile import import_shef_infile
from cwmscli.utils import get_api_key

api_key = get_api_key(api_key, api_key_loc)
import_shef_infile(
in_file=filename,
group_name=group_name,
office_id=office,
api_root=api_root,
api_key=api_key,
category_id=category,
dry_run=dry_run,
)


# endregion


@click.command("csv2cwms", help="Store CSV TimeSeries data to CWMS using a config file")
@common_api_options
@click.option(
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def import_shef_critfile(
group_office_id: str = "CWMS",
category_office_id: str = "CWMS",
replace_assigned_ts: bool = False,
dry_run: bool = False,
) -> None:
"""
Processes a .crit file and saves the information to the SHEF Data Acquisition time series group.
Expand All @@ -34,19 +35,41 @@ def import_shef_critfile(
The specified office group associated with the timeseries data. Defaults to "CWMS".
replace_assigned_ts : bool, optional
Specifies whether to unassign all existing time series before assigning new time series specified in the content body. Default is False.
dry_run : bool, optional
Parse the .crit file and print the JSON payload without posting to the API. Default is False.

Returns
-------
None
"""

api_key = "apikey " + api_key
cwms.api.init_session(api_root=api_root, api_key=api_key)
logging.info(f"CDA connection: {api_root}")
if not dry_run:
api_key = "apikey " + api_key
cwms.api.init_session(api_root=api_root, api_key=api_key)
logging.info(f"CDA connection: {api_root}")

# Parse the file and get the parsed data
parsed_data = parse_crit_file(file_path)
logging.info("CRIT file has been parsed")
logging.info(f"Found {len(parsed_data)} timeseries entries:")
for data in parsed_data:
logging.info(f" {data['Timeseries ID']} --> alias={data['Alias']}")

if not parsed_data:
logging.error("No timeseries entries found in the CRIT file")
return

if dry_run:
logging.info(
f"\n--- DRY RUN: The following timeseries entries will be added to {group_id} ---"
)
for data in parsed_data:
logging.info(
f" timeseries-id: {data['Timeseries ID']}, alias-id: {data['Alias']}"
)
logging.info(f"--- Dry run complete. Nothing was posted to the API. ---\n")
return

# df = pd.DataFrame()
logging.info(f"Saving Timeseries IDs to group: {group_id}")
for data in parsed_data:
Expand Down
Loading
Loading