From 849133ba6c6f0b50e9d3f5ab75eaa7163debe00c Mon Sep 17 00:00:00 2001
From: stedif <stefano.difrischia@inaf.it>
Date: Fri, 10 Sep 2021 17:35:37 +0200
Subject: [PATCH] L2SS-235: add jupyter notebooks for both cases

---
 .../PCC_archive_all_attributes.ipynb          | 329 ++++++++++++++++++
 jupyter-notebooks/PCC_archive_attribute.ipynb |  46 +--
 2 files changed, 354 insertions(+), 21 deletions(-)
 create mode 100644 jupyter-notebooks/PCC_archive_all_attributes.ipynb

diff --git a/jupyter-notebooks/PCC_archive_all_attributes.ipynb b/jupyter-notebooks/PCC_archive_all_attributes.ipynb
new file mode 100644
index 000000000..df4b304d9
--- /dev/null
+++ b/jupyter-notebooks/PCC_archive_all_attributes.ipynb
@@ -0,0 +1,329 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "id": "3191bdf1",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import sys, time\n",
+    "import numpy as np\n",
+    "sys.path.append('/hosthome/tango/devices')\n",
+    "from toolkit.archiver import Archiver,Retriever\n",
+    "from toolkit.archiver_base import *\n",
+    "from matplotlib import pyplot as plt"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "id": "e2d12232",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "True\n"
+     ]
+    }
+   ],
+   "source": [
+    "from common.lofar_environment import isProduction\n",
+    "print(isProduction())"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "id": "81e08b9f",
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[]"
+      ]
+     },
+     "execution_count": 3,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "archiver = Archiver()\n",
+    "archiver.get_subscriber_attributes()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "id": "884ff1ff",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "OFF\n"
+     ]
+    }
+   ],
+   "source": [
+    "device_name = 'LTS/PCC/1'\n",
+    "d=DeviceProxy(device_name) \n",
+    "state = str(d.state())\n",
+    "print(state)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "id": "0f6e65b0",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Attribute lts/pcc/1/ant_mask_rw added to archiving list!\n",
+      "Attribute lts/pcc/1/clk_enable_pwr_r added to archiving list!\n",
+      "Attribute lts/pcc/1/clk_i2c_status_r added to archiving list!\n",
+      "Attribute lts/pcc/1/clk_pll_error_r added to archiving list!\n",
+      "Attribute lts/pcc/1/clk_pll_locked_r added to archiving list!\n",
+      "Attribute lts/pcc/1/clk_monitor_rate_rw added to archiving list!\n",
+      "Attribute lts/pcc/1/clk_translator_busy_r added to archiving list!\n",
+      "Attribute lts/pcc/1/hba_element_beamformer_delays_r added to archiving list!\n",
+      "Attribute lts/pcc/1/hba_element_beamformer_delays_rw added to archiving list!\n",
+      "Attribute lts/pcc/1/hba_element_led_r added to archiving list!\n",
+      "Attribute lts/pcc/1/hba_element_led_rw added to archiving list!\n",
+      "Attribute lts/pcc/1/hba_element_lna_pwr_r added to archiving list!\n",
+      "Attribute lts/pcc/1/hba_element_lna_pwr_rw added to archiving list!\n",
+      "Attribute lts/pcc/1/hba_element_pwr_r added to archiving list!\n",
+      "Attribute lts/pcc/1/hba_element_pwr_rw added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_adc_lock_r added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_attenuator_r added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_attenuator_rw added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_band_r added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_band_rw added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_i2c_status_r added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_id_r added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_led0_r added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_led0_rw added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_led1_r added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_led1_rw added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_mask_rw added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_monitor_rate_rw added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_pwr_dig_r added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_temperature_r added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_translator_busy_r added to archiving list!\n",
+      "Attribute lts/pcc/1/rcu_version_r added to archiving list!\n",
+      "Device is now in ON state\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Start the device\n",
+    "if state == \"OFF\":\n",
+    "    if isProduction():\n",
+    "        archiver.add_attributes_to_archiver(device_name,global_archive_period=1000)\n",
+    "    else:\n",
+    "        archiver.remove_attributes_by_device(device_name)\n",
+    "    time.sleep(1)\n",
+    "    d.initialise()\n",
+    "    time.sleep(1)\n",
+    "state = str(d.state())\n",
+    "if state == \"STANDBY\":\n",
+    "    d.on()\n",
+    "state = str(d.state())\n",
+    "if state == \"ON\":\n",
+    "    print(\"Device is now in ON state\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "id": "8efd3dc1",
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "('tango://databaseds:10000/lts/pcc/1/ant_mask_rw',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/clk_enable_pwr_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/clk_i2c_status_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/clk_pll_error_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/clk_pll_locked_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/clk_monitor_rate_rw',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/clk_translator_busy_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/hba_element_beamformer_delays_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/hba_element_beamformer_delays_rw',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/hba_element_led_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/hba_element_led_rw',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/hba_element_lna_pwr_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/hba_element_lna_pwr_rw',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/hba_element_pwr_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/hba_element_pwr_rw',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_adc_lock_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_attenuator_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_attenuator_rw',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_band_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_band_rw',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_i2c_status_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_id_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_led0_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_led0_rw',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_led1_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_led1_rw',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_mask_rw',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_monitor_rate_rw',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_pwr_dig_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_temperature_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_translator_busy_r',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_version_r')"
+      ]
+     },
+     "execution_count": 6,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "archiver.get_subscriber_attributes()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "id": "a1222d19",
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "{'tango://databaseds:10000/lts/pcc/1/clk_enable_pwr_r': 'Read value for attribute CLK_Enable_PWR_R has not been updated',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/clk_i2c_status_r': 'Read value for attribute CLK_I2C_STATUS_R has not been updated',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/clk_pll_error_r': 'Read value for attribute CLK_PLL_error_R has not been updated',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/clk_pll_locked_r': 'Read value for attribute CLK_PLL_locked_R has not been updated',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/clk_translator_busy_r': 'Read value for attribute CLK_translator_busy_R has not been updated',\n",
+       " 'tango://databaseds:10000/lts/pcc/1/rcu_version_r': 'Storing Error: mysql_stmt_bind_param() failed, err=Buffer type is not supported'}"
+      ]
+     },
+     "execution_count": 7,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# Archiver managing methods\n",
+    "archiver.get_subscriber_errors()\n",
+    "\n",
+    "#e = archiver.get_attribute_errors('lts/pcc/1/rcu_temperature_r')\n",
+    "#print(e)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 12,
+   "id": "174bbcdb",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "1586.0 events/period  -> Number of archiving events per minute\n"
+     ]
+    }
+   ],
+   "source": [
+    "l = archiver.get_subscriber_load()\n",
+    "print(l,\" -> Number of archiving events per minute\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "f060b0b6",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "#archiver.update_archiving_attribute('lts/pcc/1/rcu_pwr_dig_r')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "f626d029",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# Turn off the device\n",
+    "d.off()\n",
+    "\n",
+    "# Leave commented by default\n",
+    "archiver.remove_attributes_by_device(device_name)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "13c3b97d",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# Initialise the retriever object and print the archived attributes in the database\n",
+    "retriever = Retriever()\n",
+    "#retriever.get_all_archived_attributes()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "f176c20e",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# Retrieve records in the last n hours (works even with decimals)\n",
+    "\n",
+    "# Use alternatively one of the following two methods to retrieve data (last n hours or interval)\n",
+    "records= retriever.get_attribute_value_by_hours(attr_fq_name,hours=0.1)\n",
+    "#records = retriever.get_attribute_value_by_interval(attr_fq_name,'2021-09-01 16:00:00', '2021-09-01 16:03:00')\n",
+    "\n",
+    "if not records:\n",
+    "    print('Empty result!')\n",
+    "else:\n",
+    "    # Convert DB Array records into Python lists\n",
+    "    data = build_array_from_record(records,records[0].dim_x_r)\n",
+    "    # Extract only the value from the array \n",
+    "    array_values = get_values_from_record(data)\n",
+    "\n",
+    "#records\n",
+    "#data\n",
+    "#array_values"
+   ]
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "StationControl",
+   "language": "python",
+   "name": "stationcontrol"
+  },
+  "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.7.3"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/jupyter-notebooks/PCC_archive_attribute.ipynb b/jupyter-notebooks/PCC_archive_attribute.ipynb
index 67875c1bc..6c744387f 100644
--- a/jupyter-notebooks/PCC_archive_attribute.ipynb
+++ b/jupyter-notebooks/PCC_archive_attribute.ipynb
@@ -3,7 +3,7 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "7a417be6",
+   "id": "42e7f25a",
    "metadata": {},
    "outputs": [],
    "source": [
@@ -18,7 +18,7 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "a3b8ab54",
+   "id": "1f025912",
    "metadata": {},
    "outputs": [],
    "source": [
@@ -29,7 +29,7 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "b0f8a926",
+   "id": "e0656e2d",
    "metadata": {},
    "outputs": [],
    "source": [
@@ -37,7 +37,7 @@
     "device_name = 'LTS/PCC/1'\n",
     "d=DeviceProxy(device_name) \n",
     "state = str(d.state())\n",
-    "print(state)\n",
+    "print(device_name,'is',state)\n",
     "\n",
     "archiver = Archiver()\n",
     "\n",
@@ -49,7 +49,7 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "4fdfe60f",
+   "id": "153d9420",
    "metadata": {},
    "outputs": [],
    "source": [
@@ -62,15 +62,18 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "6756d93c",
+   "id": "2ebb00f8",
    "metadata": {},
    "outputs": [],
    "source": [
     "# Start the device\n",
-    "#d.set_timeout_millis(5000)\n",
     "if state == \"OFF\":\n",
-    "    # Workaround to avoid device timeout error\n",
-    "    d.command_inout_asynch(cmd_name='Initialise')\n",
+    "    if isProduction():\n",
+    "        archiver.check_attribute_in_archiving_list(attr_fq_name)\n",
+    "    else:\n",
+    "        archiver.remove_attribute_from_archiver(attr_fq_name)\n",
+    "    time.sleep(1)\n",
+    "    d.initialise()\n",
     "    time.sleep(1)\n",
     "state = str(d.state())\n",
     "if state == \"STANDBY\":\n",
@@ -83,18 +86,18 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "ab496017",
+   "id": "75163627",
    "metadata": {},
    "outputs": [],
    "source": [
     "# Modify attribute archiving features\n",
-    "archiver.update_archiving_attribute(attr_fq_name,polling_period=1000,event_period=1000,strategy='RUN')"
+    "archiver.update_archiving_attribute(attr_fq_name,polling_period=1000,event_period=5000,strategy='RUN')"
    ]
   },
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "0d3a7cbe",
+   "id": "7814715e",
    "metadata": {},
    "outputs": [],
    "source": [
@@ -112,7 +115,7 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "266ad6c6",
+   "id": "52a27abb",
    "metadata": {},
    "outputs": [],
    "source": [
@@ -126,7 +129,7 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "ec567395",
+   "id": "c064e337",
    "metadata": {},
    "outputs": [],
    "source": [
@@ -137,7 +140,7 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "e629a396",
+   "id": "d199916c",
    "metadata": {},
    "outputs": [],
    "source": [
@@ -149,7 +152,7 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "b79dc679",
+   "id": "80e2a560",
    "metadata": {},
    "outputs": [],
    "source": [
@@ -175,7 +178,7 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "0638cfd3",
+   "id": "64c8e060",
    "metadata": {},
    "outputs": [],
    "source": [
@@ -191,7 +194,7 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "d72904fa",
+   "id": "59a0c05c",
    "metadata": {},
    "outputs": [],
    "source": [
@@ -223,20 +226,21 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "824b3d83",
+   "id": "a0e8dcab",
    "metadata": {},
    "outputs": [],
    "source": [
     "# Turn off the device\n",
     "d.off()\n",
     "# Remove attribute from archiving list\n",
-    "#archiver.remove_attribute_from_archiver(attr_fq_name)"
+    "#archiver.remove_attribute_from_archiver(attr_fq_name)\n",
+    "#archiver.remove_attributes_by_device(device_name)"
    ]
   },
   {
    "cell_type": "code",
    "execution_count": null,
-   "id": "8e9fd9a5",
+   "id": "32ab34a9",
    "metadata": {},
    "outputs": [],
    "source": []
-- 
GitLab