Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions pyerrors/input/sfcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
sep = "/"


def read_sfcf(path, prefix, name, quarks='.*', corr_type="bi", noffset=0, wf=0, wf2=0, version="1.0c", cfg_separator="n", silent=False, **kwargs):
def read_sfcf(path, prefix, name, quarks='.*', corr_type="bi", noffset=0, wf=0, wf2=0, version="1.0c", cfg_separator="n", cfg_func=None, silent=False, **kwargs):
"""Read sfcf files from given folder structure.

Parameters
Expand Down Expand Up @@ -71,11 +71,11 @@ def read_sfcf(path, prefix, name, quarks='.*', corr_type="bi", noffset=0, wf=0,
"""
ret = read_sfcf_multi(path, prefix, [name], quarks_list=[quarks], corr_type_list=[corr_type],
noffset_list=[noffset], wf_list=[wf], wf2_list=[wf2], version=version,
cfg_separator=cfg_separator, silent=silent, **kwargs)
cfg_separator=cfg_separator, cfg_func=cfg_func, silent=silent, **kwargs)
return ret[name][quarks][str(noffset)][str(wf)][str(wf2)]


def read_sfcf_multi(path, prefix, name_list, quarks_list=['.*'], corr_type_list=['bi'], noffset_list=[0], wf_list=[0], wf2_list=[0], version="1.0c", cfg_separator="n", silent=False, keyed_out=False, **kwargs):
def read_sfcf_multi(path, prefix, name_list, quarks_list=['.*'], corr_type_list=['bi'], noffset_list=[0], wf_list=[0], wf2_list=[0], version="1.0c", cfg_separator="n", cfg_func=None, silent=False, keyed_out=False, **kwargs):
"""Read sfcf files from given folder structure.

Parameters
Expand Down Expand Up @@ -245,6 +245,16 @@ def read_sfcf_multi(path, prefix, name_list, quarks_list=['.*'], corr_type_list=
for key in needed_keys:
internal_ret_dict[key] = []

def _default_idl_func(cfg_string, cfg_sep):
return int(cfg_string.split(cfg_sep)[-1])

if cfg_func is None:
print("Default idl function in use.")
cfg_func = _default_idl_func
cfg_func_args = [cfg_separator]
else:
cfg_func_args = kwargs.get("cfg_func_args", [])

if not appended:
for i, item in enumerate(ls):
rep_path = path + '/' + item
Expand All @@ -268,7 +278,7 @@ def read_sfcf_multi(path, prefix, name_list, quarks_list=['.*'], corr_type_list=
for cfg in sub_ls:
try:
if compact:
rep_idl.append(int(cfg.split(cfg_separator)[-1]))
rep_idl.append(cfg_func(cfg, *cfg_func_args))
else:
rep_idl.append(int(cfg[3:]))
except Exception:
Expand Down Expand Up @@ -351,7 +361,7 @@ def read_sfcf_multi(path, prefix, name_list, quarks_list=['.*'], corr_type_list=
for rep, file in enumerate(name_ls):
rep_idl = []
filename = path + '/' + file
T, rep_idl, rep_data = _read_append_rep(filename, pattern, intern[name]['b2b'], cfg_separator, im, intern[name]['single'])
T, rep_idl, rep_data = _read_append_rep(filename, pattern, intern[name]['b2b'], im, intern[name]['single'], cfg_func, cfg_func_args)
if rep == 0:
intern[name]['T'] = T
for t in range(intern[name]['T']):
Expand Down Expand Up @@ -581,12 +591,7 @@ def _read_compact_rep(path, rep, sub_ls, intern, needed_keys, im):
return return_vals


def _read_chunk(chunk, gauge_line, cfg_sep, start_read, T, corr_line, b2b, pattern, im, single):
try:
idl = int(chunk[gauge_line].split(cfg_sep)[-1])
except Exception:
raise Exception("Couldn't parse idl from directory, problem with chunk around line ", gauge_line)

def _read_chunk_data(chunk, start_read, T, corr_line, b2b, pattern, im, single):
found_pat = ""
data = []
for li in chunk[corr_line + 1:corr_line + 6 + b2b]:
Expand All @@ -595,10 +600,10 @@ def _read_chunk(chunk, gauge_line, cfg_sep, start_read, T, corr_line, b2b, patte
for t, line in enumerate(chunk[start_read:start_read + T]):
floats = list(map(float, line.split()))
data.append(floats[im + 1 - single])
return idl, data
return data


def _read_append_rep(filename, pattern, b2b, cfg_separator, im, single):
def _read_append_rep(filename, pattern, b2b, im, single, idl_func, cfg_func_args):
with open(filename, 'r') as fp:
content = fp.readlines()
data_starts = []
Expand Down Expand Up @@ -634,7 +639,11 @@ def _read_append_rep(filename, pattern, b2b, cfg_separator, im, single):
start = data_starts[cnfg]
stop = start + data_starts[1]
chunk = content[start:stop]
idl, data = _read_chunk(chunk, gauge_line, cfg_separator, start_read, T, corr_line, b2b, pattern, im, single)
try:
idl = idl_func(chunk[gauge_line], *cfg_func_args)
except Exception:
raise Exception("Couldn't parse idl from file", filename, ", problem with chunk of lines", start + 1, "to", stop + 1)
data = _read_chunk_data(chunk, start_read, T, corr_line, b2b, pattern, im, single)
rep_idl.append(idl)
rep_data.append(data)

Expand Down
Loading
Loading