|
| 1 | +# cwmscli/load/timeseries_ids.py |
| 2 | +import json |
| 3 | +import logging |
| 4 | +from datetime import datetime |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +import click |
| 8 | +import pandas as pd |
| 9 | +from cwms import JSON |
| 10 | + |
| 11 | + |
| 12 | +def _load_timeseries_data( |
| 13 | + source_cda: str, |
| 14 | + source_office: str, |
| 15 | + target_cda: str, |
| 16 | + target_api_key: Optional[str], |
| 17 | + verbose: int, |
| 18 | + dry_run: bool, |
| 19 | + ts_id: str, |
| 20 | + ts_group: str, |
| 21 | + ts_group_category_id: str, |
| 22 | + ts_group_category_office_id: str, |
| 23 | + begin: Optional[datetime] = None, |
| 24 | + end: Optional[datetime] = None, |
| 25 | +): |
| 26 | + import cwms |
| 27 | + |
| 28 | + if verbose: |
| 29 | + click.echo( |
| 30 | + f"Loading timeseries data from source CDA '{source_cda}' (office '{source_office}') " |
| 31 | + f"to target CDA '{target_cda}'." |
| 32 | + ) |
| 33 | + cwms.init_session(api_root=source_cda, api_key=None) |
| 34 | + # User has a ts_id |
| 35 | + if ts_id and not ts_group: |
| 36 | + ts_data = [ |
| 37 | + cwms.get_timeseries( |
| 38 | + ts_id=ts_id, office_id=source_office, begin=begin, end=end |
| 39 | + ) |
| 40 | + ] |
| 41 | + # only grab time_ids for locations that are in the target database |
| 42 | + try: |
| 43 | + if dry_run: |
| 44 | + click.echo("Dry run enabled. No changes will be made.") |
| 45 | + logging.debug(f"Would store {ts_data} for {ts_id}({source_office})") |
| 46 | + else: |
| 47 | + cwms.init_session(api_root=target_cda, api_key=target_api_key) |
| 48 | + cwms.store_timeseries( |
| 49 | + data=ts_data.json, |
| 50 | + store_rule="REPLACE_ALL", |
| 51 | + override_protection=False, |
| 52 | + ) |
| 53 | + except Exception as e: |
| 54 | + click.echo(f"Error storing timeseries ({ts_id}) data: {e}", err=True) |
| 55 | + # User did not have a ts_id but has a ts_group |
| 56 | + if ts_group: |
| 57 | + ts_ids = cwms.get_timeseries_group( |
| 58 | + group_id=ts_group, |
| 59 | + category_id=ts_group_category_id, |
| 60 | + office_id=source_office, |
| 61 | + category_office_id=ts_group_category_office_id, |
| 62 | + ) |
| 63 | + logging.info( |
| 64 | + f"Found {len(ts_ids.json.get('assigned-time-series', []))} timeseries in group {ts_group}." |
| 65 | + ) |
| 66 | + logging.info(f"Storing TSID from begin: {begin} to end: {end}") |
| 67 | + for ts in ts_ids.json.get("assigned-time-series", []): |
| 68 | + try: |
| 69 | + if dry_run: |
| 70 | + click.echo( |
| 71 | + f"Would store timeseries data for {ts['timeseries-id']}({ts['office-id']})" |
| 72 | + ) |
| 73 | + else: |
| 74 | + cwms.init_session(api_root=source_cda, api_key=None) |
| 75 | + ts_data = cwms.get_timeseries( |
| 76 | + ts_id=ts["timeseries-id"], |
| 77 | + office_id=ts["office-id"], |
| 78 | + begin=begin, |
| 79 | + end=end, |
| 80 | + ) |
| 81 | + cwms.init_session(api_root=target_cda, api_key=target_api_key) |
| 82 | + # Convert the TS Values to a format CDA expects |
| 83 | + ts_cda_format_values = [ |
| 84 | + [ |
| 85 | + pd.to_datetime(value["date-time"]).isoformat(), |
| 86 | + value["value"], |
| 87 | + value["quality-code"], |
| 88 | + ] |
| 89 | + for value in ts_data.json.get("values", []) |
| 90 | + ] |
| 91 | + # Build a payload dict with formatted values instead of copying the response object |
| 92 | + ts_data_copy = ts_data.json.copy() |
| 93 | + ts_data_copy["values"] = ts_cda_format_values |
| 94 | + cwms.store_timeseries( |
| 95 | + data=ts_data_copy, |
| 96 | + store_rule="REPLACE_ALL", |
| 97 | + override_protection=False, |
| 98 | + ) |
| 99 | + click.echo( |
| 100 | + f"Wrote {len(ts_data_copy.get('values', []))} values to {ts['timeseries-id']}." |
| 101 | + ) |
| 102 | + except Exception as e: |
| 103 | + logging.warning( |
| 104 | + f"Error storing timeseries ({ts['timeseries-id']}) data: {e}", |
| 105 | + exc_info=True, |
| 106 | + ) |
| 107 | + |
| 108 | + if verbose: |
| 109 | + click.echo("Timeseries ID copy operation completed.") |
0 commit comments