diff --git a/corrlib/input/__init__.py b/corrlib/input/__init__.py index f917e2a..50074df 100644 --- a/corrlib/input/__init__.py +++ b/corrlib/input/__init__.py @@ -3,3 +3,5 @@ Import functions for different codes. """ from . import sfcf +from . import openQCD +from . import implementations diff --git a/corrlib/input/implementations.py b/corrlib/input/implementations.py new file mode 100644 index 0000000..2a14d3e --- /dev/null +++ b/corrlib/input/implementations.py @@ -0,0 +1,2 @@ + +codes = ['sfcf', 'openQCD'] diff --git a/corrlib/input/openQCD.py b/corrlib/input/openQCD.py new file mode 100644 index 0000000..8f9c185 --- /dev/null +++ b/corrlib/input/openQCD.py @@ -0,0 +1,66 @@ +import pyerrors.input.openQCD as input +import datalad.api as dl +import os +import fnmatch +from typing import Any + + +def read_param(path: str, project: str, file_in_project: str) -> dict[str, Any]: + file = path + "/projects/" + project + '/' + file_in_project + dl.get(file, dataset=path + "/projects/" + project) + with open(file, 'r') as fp: + lines = fp.readlines() + fp.close() + param = {} + param['rw_fcts'] = [] + param['rand'] = {} + + for line in lines: + if line.startswith('#'): + continue + if line.startswith('\n'): + continue + + if line.startswith("nrw"): + l = line.strip() + num_rw = l.split('#').split()[1] + + return param + +# reweighting factors (len nrw) + +# void read_rw_parms(int irw) +# On process 0, this program scans stdin for a line starting with the +# string "[Reweighting factor ]" (after any number of blanks), where +# is the integer value passed through the argument. An error occurs +# if no such line or more than one is found. The lines +# +# rwfact +# im0 +# nsrc +# irp +# mu [] +# np [] +# isp [] + + +def read_rwms(path: str, project: str, dir_in_project: str, prefix, postfix="ms1", version='2.0', names=None, files=None): + + directory = path + "/projects/" + project + '/' + dir_in_project + if files is None: + files = [] + for root, ds, fs in os.walk(directory): + for f in fs: + if fnmatch.fnmatch(f, prefix + "*" + postfix + ".dat"): + files.append(f) + dl.get([directory + "/" + f for f in files], dataset=path + "/projects/" + project) + kwargs = {} + if names is not None: + kwargs['names'] = names + if files is not None: + kwargs['files'] = files + rwms = input.read_rwms(directory, prefix, version, **kwargs) + print(rwms) + rw_dict: dict[str, Any] = {} + rw_dict[''] = + return rw_dict \ No newline at end of file diff --git a/corrlib/toml.py b/corrlib/toml.py index 007c8e1..88b62a9 100644 --- a/corrlib/toml.py +++ b/corrlib/toml.py @@ -10,11 +10,12 @@ the import of projects via TOML. import tomllib as toml import shutil -from .input import sfcf +from .input import sfcf, openQCD from .main import import_project, update_aliases from .meas_io import write_measurement import datalad.api as dl import os +from .input.implementations import codes as known_codes def check_project_data(d: dict) -> None: @@ -30,8 +31,12 @@ def check_project_data(d: dict) -> None: return -def check_measurement_data(measurements: dict) -> None: - var_names: list[str] = ["path", "ensemble", "param_file", "version", "prefix", "cfg_seperator", "names"] +def check_measurement_data(measurements: dict, code: str) -> None: + var_names: list[str] = [] + if code == "sfcf": + var_names = ["path", "ensemble", "param_file", "version", "prefix", "cfg_seperator", "names"] + elif code == "openQCD": + var_names = ["path", "ensemble", "measurement", "prefix", "param_file"] for mname, md in measurements.items(): for var_name in var_names: if var_name not in md.keys(): @@ -56,8 +61,10 @@ def import_toml(path: str, file: str, copy_file: bool=True) -> None: toml_dict = toml.load(fp) check_project_data(toml_dict) project: dict = toml_dict['project'] + if project['code'] not in known_codes: + raise ValueError('Code' + project['code'] + 'has no import implementation!') measurements: dict = toml_dict['measurements'] - check_measurement_data(measurements) + check_measurement_data(measurements, project['code']) aliases = project.get('aliases', None) uuid = project.get('uuid', None) if uuid is not None: @@ -70,15 +77,21 @@ def import_toml(path: str, file: str, copy_file: bool=True) -> None: for mname, md in measurements.items(): print("Import measurement: " + mname) ensemble = md['ensemble'] - param = sfcf.read_param(path, uuid, md['param_file']) - if 'names' in md.keys(): - measurement = sfcf.read_data(path, uuid, md['path'], md['prefix'], param, - version=md['version'], cfg_seperator=md['cfg_seperator'], sep='/', names=md['names']) - else: - measurement = sfcf.read_data(path, uuid, md['path'], md['prefix'], param, - version=md['version'], cfg_seperator=md['cfg_seperator'], sep='/') + if project['code'] == 'sfcf': + param = sfcf.read_param(path, uuid, md['param_file']) + if 'names' in md.keys(): + measurement = sfcf.read_data(path, uuid, md['path'], md['prefix'], param, + version=md['version'], cfg_seperator=md['cfg_seperator'], sep='/', names=md['names']) + else: + measurement = sfcf.read_data(path, uuid, md['path'], md['prefix'], param, + version=md['version'], cfg_seperator=md['cfg_seperator'], sep='/') + print(mname + " imported.") + elif project['code'] == 'openQCD': + param = openQCD.read_param(path, uuid, md['param_file']) + param['type'] = md['measurement'] + measurement = openQCD.read_rwms(path, uuid, md['path'], md["prefix"], version=md["version"], names=md['names'], files=md['files']) write_measurement(path, ensemble, measurement, uuid, project['code'], md['param_file']) - print(mname + " imported.") + if not os.path.exists(os.path.join(path, "toml_imports", uuid)): os.makedirs(os.path.join(path, "toml_imports", uuid)) if copy_file: