From d89816389f06f37d6245a913488e2d13550e5661 Mon Sep 17 00:00:00 2001
From: Stephan Philips <s.g.j.philips@tudelft.nl>
Date: Fri, 30 Oct 2020 10:59:08 +0100
Subject: [PATCH] addin snapshot automatically

---
 core_tools/data/SQL/SQL_commands.py     | 13 +++++++------
 core_tools/data/SQL/SQL_database_mgr.py |  2 +-
 core_tools/data/ds/data_set.py          |  6 +++++-
 core_tools/data/gui/data_browser.py     |  9 ++++++---
 core_tools/data/measurement.py          |  6 +++---
 5 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/core_tools/data/SQL/SQL_commands.py b/core_tools/data/SQL/SQL_commands.py
index f61c5025..331da5ff 100644
--- a/core_tools/data/SQL/SQL_commands.py
+++ b/core_tools/data/SQL/SQL_commands.py
@@ -2,6 +2,7 @@ from core_tools.data.ds.data_set_raw import data_set_raw, m_param_raw
 from core_tools.data.SQL.buffer_writer import buffer_reader
 from core_tools.data.SQL.connector import sample_info
 from uuid import getnode as get_mac
+from psycopg2.extras import Json
 
 import time
 import json
@@ -115,9 +116,9 @@ class write_query_generator:
 			measurement_table_name (str) : name of the table that contains the raw measurement data
 			start_time (long) : time in unix seconds since the epoch
 			stop_time (long) : time in unix seconds since the epoch
-			metadata (JSON) : json string to be saved in the database
-			snapshot (JSON) : snapshot of the exprimental set up
-			tags (JSON) : list of tags that accompuarby the measurment if any
+			metadata (dict) : json string to be saved in the database
+			snapshot (dict) : snapshot of the exprimental set up
+			tags (dict) : list of tags that accompuarby the measurment if any
 			completed (bool) : tell that the measurement is completed.
 		
 		Not used atm:	
@@ -133,11 +134,11 @@ class write_query_generator:
 		if stop_time is not None:
 			statement += "UPDATE {} SET stop_time = to_timestamp('{}') WHERE uuid = {};".format(table_name, stop_time, meas_uuid)
 		if metadata is not None:
-			statement += "UPDATE {} SET metadata = '{}' WHERE uuid = {};".format(table_name, metadata, meas_uuid)
+			statement += "UPDATE {} SET metadata = {} WHERE uuid = {};".format(table_name, Json(metadata), meas_uuid)
 		if snapshot is not None:
-			statement += "UPDATE {} SET snapshot = '{}' WHERE uuid = {};".format(table_name, snapshot, meas_uuid)
+			statement += "UPDATE {} SET snapshot = {} WHERE uuid = {};".format(table_name, Json(snapshot), meas_uuid)
 		if tags is not None:
-			statement += "UPDATE {} SET tags = '{}' WHERE uuid = {};".format(table_name, tags, meas_uuid)
+			statement += "UPDATE {} SET tags = {} WHERE uuid = {};".format(table_name, Json(tags), meas_uuid)
 		if completed is not None:
 			statement += "UPDATE {} SET completed = '{}' WHERE uuid = {};".format(table_name, completed, meas_uuid)
 
diff --git a/core_tools/data/SQL/SQL_database_mgr.py b/core_tools/data/SQL/SQL_database_mgr.py
index 38053e0b..7b700f44 100644
--- a/core_tools/data/SQL/SQL_database_mgr.py
+++ b/core_tools/data/SQL/SQL_database_mgr.py
@@ -60,7 +60,7 @@ class SQL_database_manager(SQL_database_init):
 		ds.exp_uuid = exp_uuid
 		ds.running = True
 		ds.SQL_datatable = "_" + sample_info.set_up + "_" +sample_info.project + "_" +sample_info.sample +"_" + str(exp_uuid)
-		ds.SQL_datatable = ds.SQL_datatable.replace(" ", "_")
+		ds.SQL_datatable = ds.SQL_datatable.replace(" ", "_").replace('-', '_')
 		# todo -- add tags to the measurements
 		cur.execute(write_query_generator.fill_meas_info_in_measurement_table(
 			ds.exp_uuid,ds.SQL_datatable,
diff --git a/core_tools/data/ds/data_set.py b/core_tools/data/ds/data_set.py
index 7b808d87..2e5a2970 100644
--- a/core_tools/data/ds/data_set.py
+++ b/core_tools/data/ds/data_set.py
@@ -1,5 +1,7 @@
 from core_tools.data.ds.data_set_core import data_set_raw, data_set
 from core_tools.data.SQL.SQL_database_mgr import SQL_database_manager
+
+import json
 import qcodes as qc
 
 def load_by_id(exp_id):
@@ -32,7 +34,9 @@ def create_new_data_set(experiment_name, *m_params):
     ds = data_set_raw(exp_name=experiment_name)
 
     if qc.Station.default is not None:
-        ds.snapshot = qc.Station.default.snapshot()
+        snapshot = qc.Station.default.snapshot()
+        snapshot_json = json.dumps({'station': snapshot}, cls=qc.utils.helpers.NumpyJSONEncoder)
+        ds.snapshot = json.loads(snapshot_json)
 
     # intialize the buffers for the measurement
     for m_param in m_params:
diff --git a/core_tools/data/gui/data_browser.py b/core_tools/data/gui/data_browser.py
index f3a8b515..b6ba3961 100644
--- a/core_tools/data/gui/data_browser.py
+++ b/core_tools/data/gui/data_browser.py
@@ -217,7 +217,10 @@ class data_browser(data_browser_autogen.Ui_MainWindow):
         self.search_table_data_model.overwrite_data(data)
 
 if __name__ == '__main__':
-    from core_tools.data.SQL.connector import SQL_conn_info_local, SQL_conn_info_remote, sample_info, set_up_local_storage
-    set_up_local_storage('stephan', 'magicc', 'test', 'Intel Project', 'F006', 'SQ38328342')
-    db = data_browser()
+    # from core_tools.data.SQL.connector import SQL_conn_info_local, SQL_conn_info_remote, sample_info, set_up_local_storage
+    # set_up_local_storage('stephan', 'magicc', 'test', 'Intel Project', 'F006', 'SQ38328342')
+    # db = data_browser()
 
+    from core_tools.data.SQL.connector import set_up_local_storage
+    set_up_local_storage("xld_user", "XLDspin001", "vandersypen_data", "6dot", "XLD", "6D3S - SQ20-20-5-18-4")
+    db = data_browser()
\ No newline at end of file
diff --git a/core_tools/data/measurement.py b/core_tools/data/measurement.py
index ce78fd94..bcbc6e88 100644
--- a/core_tools/data/measurement.py
+++ b/core_tools/data/measurement.py
@@ -149,7 +149,7 @@ if __name__ == '__main__':
     from core_tools.data.SQL.connector import set_up_local_storage
 
     set_up_local_storage('stephan', 'magicc', 'test', 'Intel Project', 'F006', 'SQ38328342')
-
+    set_up_local_storage("xld_user", "XLDspin001", "vandersypen_data", "6dot", "XLD", "6D3S - SQ20-20-5-18-4")
     class MyCounter(qc.Parameter):
         def __init__(self, name):
             # only name is required
@@ -194,8 +194,8 @@ if __name__ == '__main__':
     d = dummy_multi_parameter_2dawg("name2")
     m1 = MyCounter('name3')
     m2 = dummy_multi_parameter_2dawg("name4")
-    m3 = construct_2D_scan_fast('P2', 10, 10, 'P5', 10, 10,50000, True, None, dig)
-    m4 = construct_1D_scan_fast("P2", 10,10,5000, True, None, dig)
+    # m3 = construct_2D_scan_fast('P2', 10, 10, 'P5', 10, 10,50000, True, None, dig)
+    # m4 = construct_1D_scan_fast("P2", 10,10,5000, True, None, dig)
 
     x = 100
     y = 100
-- 
GitLab