can now read and write simple sfcf measurements

This commit is contained in:
Justus Kuhlmann 2024-06-19 12:55:42 +00:00
parent 5ec34ad98b
commit c3aa7577fb
7 changed files with 408 additions and 0 deletions

46
backlogger/io.py Normal file
View file

@ -0,0 +1,46 @@
from pyerrors.input import json as pj
import os
import datalad.api as dl
import sqlite3
def write_measurement(path, ensemble, measurement, uuid, code, parameters, parameter_file):
"""
Write a measurement to the backlog.
If the file for the measurement already exists, update the measurement.
Parameters
----------
path: str
The path to the backlogger folder.
ensemble: str
The ensemble of the measurement.
measurement: dict
Measurements to be captured in the backlogging system.
uuid: str
The uuid of the project.
"""
for corr in measurement.keys():
file = path + "/archive/" + ensemble + "/" + corr + '/' + uuid + '.json.gz'
if not os.path.exists(path + "/archive/" + ensemble + "/" + corr):
os.makedirs(path + "/archive/" + ensemble + "/" + corr)
conn = sqlite3.connect(path + '/backlogger.db')
c = conn.cursor()
if os.path.exists(file):
dl.unlock(file, dataset=path)
known_meas = pj.load_json_dict(file)
for key in measurement[corr].keys():
known_meas[key] = measurement[corr][key]
else:
known_meas = measurement[corr]
pj.dump_dict_to_json(measurement[corr], file)
for subkey in measurement[corr].keys():
meas_path = file + "::" + subkey
if not os.path.exists(file):
c.execute("INSERT INTO backlogs (name, ensemble, code, path, project, parameters, parameter_file, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, datetime('now'), datetime('now'))", (corr, ensemble, code, meas_path, uuid, parameters, parameter_file))
else:
c.execute("UPDATE backlogs SET updated_at=datetime('now') WHERE path=?", (file,))
conn.commit()
conn.close()
dl.save([path + '/backlogger.db', file], message="Add measurement to database", dataset=path)