From 48619fe690e1cd4974552c0e605ec897805641e4 Mon Sep 17 00:00:00 2001
From: unknown <asoalexandros@gmail.com>
Date: Tue, 1 Apr 2025 11:16:33 +0200
Subject: [PATCH] rescue data ADU done!

---
 hp4155/hp4155a.py        |   6 +-
 hp4155/rescue_data.ipynb | 175 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 180 insertions(+), 1 deletion(-)
 create mode 100644 hp4155/rescue_data.ipynb

diff --git a/hp4155/hp4155a.py b/hp4155/hp4155a.py
index a47efe5..16f416c 100644
--- a/hp4155/hp4155a.py
+++ b/hp4155/hp4155a.py
@@ -101,7 +101,11 @@ class HP4155a(object):
         self.inst.write("*WAI")
     
     def show_variables(self):
-        return self.inst.query(":DATA:CAT?") 
+        query = self.inst.query(":DATA:CAT?")
+        query = query.replace('\n',',')
+        var_list = query.split(',')
+        var_list.pop() #remove empty string
+        return var_list
 
      #-----------------------------------------------------------sweep functions---------------------------------------------------------------
     def smu_disable(self,number:int):
diff --git a/hp4155/rescue_data.ipynb b/hp4155/rescue_data.ipynb
new file mode 100644
index 0000000..75f432e
--- /dev/null
+++ b/hp4155/rescue_data.ipynb
@@ -0,0 +1,175 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "id": "643903f9-9dc6-4563-b389-9ffc6ed601d7",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import hp4155a\n",
+    "import pandas as pd\n",
+    "import numpy as np\n",
+    "import tkinter as tk\n",
+    "from datetime import datetime\n",
+    "from tkinter import filedialog"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "id": "82c9534e-4125-4c99-bca0-d763a3ec4f00",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "device =  hp4155a.HP4155a('GPIB0::17::INSTR')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "id": "e062d8a6-6b24-49e6-b88c-d59060ac46c1",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "variables = device.show_variables()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "id": "dd600907-ab15-440f-93fa-f0d3a1c19651",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "values = {}\n",
+    "for variable in variables:\n",
+    "    try:\n",
+    "        current_values = device.return_values(variable)\n",
+    "        if len(current_values)>1: # There are also some constants\n",
+    "            values[variable] = current_values\n",
+    "    except:\n",
+    "        print(f\"No Values for variable {variable}\")\n",
+    "\n",
+    "df = pd.DataFrame(values)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "id": "87309758-aea7-418d-8dfa-024f827330ce",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "del device"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "id": "62e77124-db55-4208-b008-d69379de6407",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "for col in df.columns.values.tolist():\n",
+    "    df.loc[abs(df[col]) > 1e30, col] = np.nan"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 8,
+   "id": "d64a276f-544c-436e-a948-0e3519e12e74",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# Prepare to save to file\n",
+    "def create_file(filename):\n",
+    "    root = tk.Tk()\n",
+    "    root.withdraw()\n",
+    "    root.lift() #show window above all other applications\n",
+    "\n",
+    "    root.attributes(\"-topmost\", True)#window stays above all other applications\n",
+    "\n",
+    "    file = filedialog.asksaveasfilename(defaultextension=\".txt\", filetypes=[(\"Text files\",\"*.txt\")],title = \"save results path\",initialfile =filename)\n",
+    "\n",
+    "    #check if the file path is correct(.txt)\n",
+    "    while file.endswith(\".txt\") == False:\n",
+    "        #open again filedialog with error message box\n",
+    "        tk.messagebox.showerror(message='invalid filename!')\n",
+    "        file = filedialog.asksaveasfilename(defaultextension=\".txt\", filetypes=[(\"Text files\",\"*.txt\")],title = \"save results path\",initialfile =filename)\n",
+    "    root.destroy()\n",
+    "    return file"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "id": "b1dfe94d-96ca-432c-b180-cf581dcf3c41",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "date = datetime.now().strftime('%d_%m_%Y_%H_%M_%S') "
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "id": "404b28c8-d14c-41cc-91da-097647338d32",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "filename = \"Rescue_Data_ADU_\"+date +\".txt\""
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 11,
+   "id": "268dd9f8-1092-41d5-aca5-668ecf5c38d6",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Data saved to file successfully\n"
+     ]
+    }
+   ],
+   "source": [
+    "file=create_file(filename)\n",
+    "df.to_csv(file,sep=\" \",mode='w',na_rep = 'NaN')\n",
+    "\n",
+    "print(\"Data saved to file successfully\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "00854758-b6e6-459c-8329-92bedc2c3f72",
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3 (ipykernel)",
+   "language": "python",
+   "name": "python3"
+  },
+  "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.4"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
-- 
GitLab