better io for measurement management
This commit is contained in:
parent
fe8f63465a
commit
36496a4fb8
1 changed files with 39 additions and 8 deletions
|
@ -1,57 +0,0 @@
|
|||
from pyerrors.input import json as pj
|
||||
import os
|
||||
import datalad.api as dl
|
||||
import sqlite3
|
||||
from .input.sfcf import get_specs
|
||||
from .input.sfcf import read_param
|
||||
|
||||
|
||||
def write_measurement(path, ensemble, measurement, uuid, code, 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.
|
||||
"""
|
||||
parameters = read_param(path, uuid, parameter_file)
|
||||
print(parameters)
|
||||
dl.unlock(path + '/backlogger.db', dataset=path)
|
||||
conn = sqlite3.connect(path + '/backlogger.db')
|
||||
c = conn.cursor()
|
||||
files = []
|
||||
for corr in measurement.keys():
|
||||
file = path + "/archive/" + ensemble + "/" + corr + '/' + uuid + '.json.gz'
|
||||
files.append(file)
|
||||
if not os.path.exists(path + "/archive/" + ensemble + "/" + corr):
|
||||
os.makedirs(path + "/archive/" + ensemble + "/" + corr)
|
||||
|
||||
for subkey in measurement[corr].keys():
|
||||
meas_path = file + "::" + subkey
|
||||
|
||||
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]
|
||||
# this should be only set if something changed.
|
||||
else:
|
||||
known_meas = measurement[corr]
|
||||
|
||||
if c.execute("SELECT * FROM backlogs WHERE path = ?", (meas_path,)).fetchone() is not None:
|
||||
c.execute("UPDATE backlogs SET name = ?, ensemble = ?, code = ?, project = ?, parameters = ?, parameter_file = ?, updated_at = datetime('now') WHERE path = ?", (meas_path, ))
|
||||
else:
|
||||
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, get_specs(corr + "/" + subkey, parameters), parameter_file))
|
||||
conn.commit()
|
||||
pj.dump_dict_to_json(measurement[corr], file)
|
||||
files.append(path + '/backlogger.db')
|
||||
conn.close()
|
||||
dl.save(files, message="Add measurement to database", dataset=path)
|
Loading…
Add table
Add a link
Reference in a new issue