Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SITCOM-918: oscillations in m1m3 #105

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,336 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "34c30bfe-0ecc-45ad-977b-eff34fa3fdf4",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from glob import glob\n",
"import sys, time, os, asyncio\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"import numpy as np\n",
"from scipy.signal import find_peaks\n",
"from scipy.interpolate import interp1d\n",
"from astropy.time import Time, TimeDelta\n",
"from scipy.interpolate import UnivariateSpline\n",
"\n",
"\n",
"from lsst_efd_client import EfdClient\n",
"from lsst.ts.idl.enums import MTM1M3\n",
"\n",
"from lsst.summit.utils.efdUtils import calcNextDay, getEfdData\n",
"\n",
"import lsst.sitcom.vandv.m1m3.sitcomtn81.sitcomtn_81_identify_oscillations as sitcomtn81\n",
"\n",
"from tqdm import tqdm\n",
"\n",
"import logging\n",
"\n",
"summit_utils_logger = logging.getLogger(\"lsst.summit.utils\")\n",
"summit_utils_logger.setLevel(logging.ERROR)\n",
"\n",
"%matplotlib inline\n",
"%load_ext lab_black\n",
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "markdown",
"id": "d34a3dd1-3a9c-48b6-b042-8997384e9bf3",
"metadata": {},
"source": [
"# Characterizaiton of topple block \n",
"\n",
"\n",
"Timestamps of the topple block slews:\n",
"Dayobs, seqnum, eventnum, time (utc)\n",
"20231129, 73, 13, 1701269673.9407606\n",
"20231129, 90, 30, 1701298313.7859175\n",
"20231129, 97, 33, 1701299609.625537\n",
"20231129, 99, 38, 1701299684.265059\n",
"20231129, 100, 49, 1701299719.8852365\n",
"20231129, 100, 54, 1701299775.7053618\n",
"20231129, 107, 61, 1701302257.7229214\n",
"20231129, 114, 63, 1701303679.5524187\n",
"20231129, 120, 70, 1701305359.8571882\n",
"20231129, 365, 79, 1701316217.89929\n",
"20231129, 385, 80, 1701318835.4638815\n",
"20231129, 387, 81, 1701318835.4638815\n",
"20231129, 387, 82, 1701318878.6672893\n",
"20231129, 399, 84, 1701320922.0731387\n",
"20231129, 811, 101, 1701332665.7784734"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "55c9ab9a-9ccf-425e-99c9-d626abf1a973",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def add_timestamp(data):\n",
" \"\"\"\n",
" Adds a correct timestamp column in UTC format to the provided data if\n",
" not present.\n",
"\n",
" Parameters\n",
" ----------\n",
" data : DataFrame\n",
" The data to which the timestamp will be added.\n",
"\n",
" Returns\n",
" -------\n",
" DataFrame\n",
" The data with the added 'snd_timestamp_utc' column.\n",
" \"\"\"\n",
" if \"snd_timestamp_utc\" not in data.columns:\n",
" data[\"snd_timestamp_utc\"] = Time(\n",
" data[\"private_sndStamp\"], format=\"unix_tai\"\n",
" ).unix\n",
" return data\n",
"\n",
"\n",
"def get_data(events_frame, client, train=False):\n",
" table_dict = {\n",
" \"m1m3_hp_actuator\": \"lsst.sal.MTM1M3.hardpointActuatorData\",\n",
" }\n",
" query_dict = {}\n",
" for key in table_dict.keys():\n",
" query_dict[key] = []\n",
" for j, time in tqdm(enumerate(events_frame[\"times\"])):\n",
" event_time = Time(events_frame[\"times\"][j] - 2, format=\"unix\")\n",
"\n",
" for key in table_dict.keys():\n",
" hpcols = [\"private_sndStamp\"] + [f\"measuredForce2\"] # for i in range(6)]\n",
" query = getEfdData(\n",
" client=client,\n",
" topic=table_dict[key],\n",
" columns=hpcols,\n",
" begin=event_time,\n",
" end=event_time,\n",
" prePadding=60,\n",
" postPadding=60,\n",
" )\n",
" query[\"event_num\"] = j\n",
" query[\"seqNum\"] = events_frame[\"seq_num\"][j]\n",
" query[\"event_time\"] = events_frame[\"times\"][j] + 3\n",
" query = add_timestamp(query)\n",
"\n",
" # query[\"block\"] = block\n",
" query[\"oscillation_bool\"] = 0\n",
" query[\"delta_time\"] = (\n",
" query[\"snd_timestamp_utc\"] - events_frame[\"times\"][j] + 3\n",
" )\n",
" if train:\n",
" sel = query[\"delta_time\"] >= -5\n",
" sel &= query[\"delta_time\"] < 1\n",
" query.loc[sel, \"oscillation_bool\"] = 1\n",
"\n",
" query_dict[key].append(query)\n",
" for key in table_dict.keys():\n",
" query_dict[key] = pd.concat(query_dict[key])\n",
" sorted_keys = sorted(query_dict.keys())\n",
" initial_key = sorted_keys[0]\n",
" merged_df = query_dict[initial_key].sort_index()\n",
" # Define your tolerance for matching\n",
" tolerance = 0.03 # pd.Timedelta('0.03 seconds')\n",
"\n",
" # Iterate over the remaining DataFrames and merge them\n",
" for key in sorted_keys[1:]:\n",
" merged_df = pd.merge_asof(\n",
" merged_df,\n",
" query_dict[key].sort_index(),\n",
" left_on=\"snd_timestamp_utc\",\n",
" right_on=\"snd_timestamp_utc\",\n",
" tolerance=tolerance,\n",
" direction=\"nearest\",\n",
" suffixes=(\"\", \"_\" + key),\n",
" )\n",
" merged_df.reset_index(drop=True, inplace=True)\n",
"\n",
" return merged_df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "46b02dee-fd14-4646-a00b-dda499a7d033",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"days_with_events = [\n",
" i for i in days_with_events if (float(i) > 20231120) & (float(i) < 20231205)\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "00951deb-3242-4f4e-aa50-fd3549bcb39c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"days_with_events"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2b4afdd4-f48a-40b8-af77-8d651b55d50a",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"\n",
"day = 20231129\n",
"eventMaker = TMAEventMaker()\n",
"events = eventMaker.getEvents(int(day))\n",
"\n",
"events_frame = pd.read_csv(f\"./sitcomtn81_data/oscillation_events_20231129.csv\")\n",
"merged_df = get_data(events_frame, client)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "87ded02e-e64c-4f1b-9657-f8f7b28300cb",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"good = [13, 30, 33, 38, 49, 54, 61, 63, 70, 79, 80, 81, 82, 84, 101]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "04e78cfa-aacd-4760-beb9-51ca34f82400",
"metadata": {},
"outputs": [],
"source": [
"offset_list = [10, 2, 3.5, 2, 1, 1.5, 2, 4, 9, 4, 1, 1, 8.5, 7.5, -21]\n",
"offset_dict = {}\n",
"for l, k in enumerate(good):\n",
" offset_dict[k] = offset_list[l]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "751e78df-9683-40b7-882c-f8342ae0d7c1",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"l = 1\n",
"plt.figure(dpi=175, figsize=(10, 8 * l))\n",
"j = 0\n",
"ptp_list = []\n",
"offset = 2000\n",
"for i in np.unique(merged_df[\"event_num\"]): # [k : k + 30 * l]:\n",
" if i not in good:\n",
" continue\n",
" plt.title(f\"{day} slews with oscillations\")\n",
" subframe = merged_df.copy()\n",
" subframe = subframe[subframe[\"event_num\"] == i]\n",
" seq_val = np.unique(subframe[\"seqNum\"])\n",
" # block_num = np.unique(subframe[\"block\"])\n",
" if len(subframe) > 0:\n",
" ptp_range = (subframe[\"delta_time\"] - offset_dict[i] > 0) & (\n",
" subframe[\"delta_time\"] - offset_dict[i] < 4\n",
" )\n",
"\n",
" plt.plot(\n",
" subframe[\"delta_time\"][ptp_range] - offset_dict[i],\n",
" subframe[\"measuredForce2\"][ptp_range]\n",
" - subframe[\"measuredForce2\"][subframe[\"delta_time\"] < 0].mean()\n",
" - offset * j,\n",
" label=f\"{seq_val[0]}\",\n",
" )\n",
" ptp_range = (subframe[\"delta_time\"] - offset_dict[i] > 0) & (\n",
" subframe[\"delta_time\"] - offset_dict[i] < 4\n",
" )\n",
" ptp_list.append(np.ptp(subframe[\"measuredForce2\"][ptp_range]))\n",
" j += 1\n",
"if l == 1:\n",
" plt.legend(title=\"slew number\")\n",
"if l == 4:\n",
" plt.legend(fontsize=9.2, loc=7)\n",
"# plt.axvline(0, c=\"k\", ls=\"dashed\")\n",
"plt.ylabel(f\"$\\Delta$ HP 2 force [N] - {offset} N offset\")\n",
"plt.xlabel(\"Time [s]\")\n",
"plt.xlim(-10, 25)\n",
"for l in np.arange(0, 5, 1):\n",
" plt.axvline(l, ls=\"dashed\", c=\"k\")\n",
"# plt.savefig(f\"./sitcomtn81_data/plots/osc_hp2_{day}.png\")\n",
"\n",
"# plt.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "540fbb3a-0f24-4ff8-9f82-770eea878a82",
"metadata": {},
"outputs": [],
"source": [
"print(np.mean(ptp_list), np.std(ptp_list))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "56037911-95c9-4a66-ba6b-3e1666999437",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"seq_list = []\n",
"for row in good:\n",
" day_obs, seq_num, times = events_frame.loc[row, [\"day_obs\", \"seq_num\", \"times\"]]\n",
" print(f\"{day_obs}, {seq_num}, {row}, {times}\")\n",
" seq_list.append(seq_num)\n",
" events_frame.loc[row, [\"day_obs\", \"seq_num\", \"times\"]].to_csv(\n",
" f\"{datapath}/cleaned_events{day}.csv\"\n",
" )"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "LSST",
"language": "python",
"name": "lsst"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading