From d4056f73ab5f1f752ca20c15dea424c4ed687d92 Mon Sep 17 00:00:00 2001 From: "Charles Graham, SWT" Date: Tue, 24 Mar 2026 16:44:17 +0000 Subject: [PATCH] Add TS shift example from class --- examples/08_cwms_ts_shift_examples.ipynb | 77 ++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 examples/08_cwms_ts_shift_examples.ipynb diff --git a/examples/08_cwms_ts_shift_examples.ipynb b/examples/08_cwms_ts_shift_examples.ipynb new file mode 100644 index 0000000..f3c7771 --- /dev/null +++ b/examples/08_cwms_ts_shift_examples.ipynb @@ -0,0 +1,77 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "d9b66ebe", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import cwms\n", + "import os\n", + "import logging\n", + "\n", + "\n", + "TS_NAME = \"KEYS.Elev.Inst.1Hour.0.Ccp-Rev\"\n", + "OFFICE = os.getenv(\"OFFICE\", \"SWT\")\n", + "API_KEY_DEV = os.getenv(\"CDA_API_KEY_DEV\")\n", + "API_ROOT_DEV = os.getenv(\"CDA_API_ROOT_DEV\")\n", + "\n", + "logging.basicConfig(\n", + " level=logging.DEBUG, format=\"%(asctime)s - %(levelname)s - %(message)s\"\n", + ")\n", + "\n", + "\n", + "if not API_ROOT_DEV:\n", + " logging.error(\n", + " \"CDA_API_KEY environment variable is not set. Please set it to your API key.\"\n", + " )\n", + "\n", + "if not API_KEY_DEV:\n", + " logging.error(\n", + " \"CDA_API_ROOT environment variable is not set. Please set it to your API root URL.\"\n", + " )\n", + "\n", + "logging.debug(\"Starting Script!\")\n", + "\n", + "\n", + "logging.info(f\"Getting timeseries data for {TS_NAME} from office {OFFICE}\")\n", + "\n", + "cwms.init_session()\n", + "ts = cwms.get_timeseries(ts_id=TS_NAME, office_id=OFFICE)\n", + "shifted_df = ts.df.copy()\n", + "\n", + "if isinstance(shifted_df.index, pd.DatetimeIndex):\n", + " shifted_df.index = shifted_df.index + pd.Timedelta(hours=10)\n", + "else:\n", + " shifted_df[\"date-time\"] = pd.to_datetime(shifted_df[\"date-time\"]) + pd.Timedelta(\n", + " hours=10\n", + " )\n", + "\n", + "ts_json = cwms.timeseries_df_to_json(\n", + " data=shifted_df,\n", + " office_id=OFFICE,\n", + " ts_id=TS_NAME,\n", + " units=ts.json.get(\"units\"),\n", + ")\n", + "cwms.init_session(api_root=API_ROOT_DEV, api_key=API_KEY_DEV)\n", + "cwms.store_timeseries(data=ts_json)\n", + "\n", + "\n", + "logging.debug(\"Script Done\")\n" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}