Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9246bfc

Browse files
authored
Merge pull request #249 from kaustubhmote/bruker_read_data_bugfix
Bugfix: Reading Floating Point Datasets (Topspin > 4.0)
2 parents 2500a5f + 363724c commit 9246bfc

1 file changed

Lines changed: 64 additions & 11 deletions

File tree

nmrglue/fileio/bruker.py

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -426,22 +426,24 @@ def read(dir=".", bin_file=None, acqus_files=None, pprog_file=None, shape=None,
426426
else:
427427
isfloat = False
428428

429-
# read the binary file
430-
f = os.path.join(dir, bin_file)
431-
_, data = read_binary(f, shape=shape, cplex=cplex, big=big,
432-
isfloat=isfloat)
433-
434429
try:
435430
if dic['acqus']['FnTYPE'] == 2: # non-uniformly sampled data
436431
try:
437432
dic['nuslist'] = read_nuslist(dir)
438433
except FileNotFoundError:
439434
warn("NUS data detected, but nuslist was not found")
435+
shape = (-1, shape[-1])
440436
except KeyError:
441437
# old datasets do not have the FnTYPE parameter in acqus files.
442438
# also fails silently when acqus file is absent.
443439
pass
444440

441+
# read the binary file
442+
f = os.path.join(dir, bin_file)
443+
_, data = read_binary(f, shape=shape, cplex=cplex, big=big,
444+
isfloat=isfloat)
445+
446+
445447
return dic, data
446448

447449

@@ -1052,19 +1054,25 @@ def guess_shape(dic):
10521054
except KeyError:
10531055
dtypa = 0 # default value, int32 data
10541056

1057+
if dtypa == 0:
1058+
bytesize = 4
1059+
elif dtypa == 2:
1060+
bytesize = 8
1061+
else:
1062+
raise ValueError(f'DTYPA ({dtypa}) is inconsistent with expected values of 0 or 2')
1063+
10551064
# last (direct) dimension is given by "TD" parameter in acqus file
10561065
# rounded up to nearest (1024/(bytes per point))
10571066
# next-to-last dimension may be given by "TD" in acqu2s. In 3D+ data
10581067
# this is often the sum of the indirect dimensions
1059-
if dtypa == 2:
1060-
shape = [0, 0, td2, int(np.ceil(td0 / 128.) * 128.)]
1061-
else:
1062-
shape = [0, 0, td2, int(np.ceil(td0 / 256.) * 256.)]
1068+
1069+
pointsize = 1024.0 / bytesize
1070+
shape = [0, 0, td2, int(np.ceil(td0 / pointsize) * pointsize)]
10631071

10641072
# additional dimension given by data size
10651073
if shape[2] != 0 and shape[3] != 0:
1066-
shape[1] = fsize // (shape[3] * shape[2] * 4)
1067-
shape[0] = fsize // (shape[3] * shape[2] * shape[1] * 4)
1074+
shape[1] = fsize // (shape[3] * shape[2] * bytesize)
1075+
shape[0] = fsize // (shape[3] * shape[2] * shape[1] * bytesize)
10681076

10691077
# if there in no pulse program parameters in dictionary return current
10701078
# shape after removing zeros
@@ -2724,3 +2732,48 @@ def read_vdlist(dirc, fname='vdlist'):
27242732
vdlist = [float(i) for i in vdlist]
27252733

27262734
return vdlist
2735+
2736+
def guess_topspin_version(dic):
2737+
"""
2738+
Guess the version of topspin on which the data was acquired
2739+
2740+
Parameters
2741+
----------
2742+
dic : dict
2743+
dictionary associated with the data
2744+
2745+
Returns
2746+
-------
2747+
tuple
2748+
(version, tag, instrument)
2749+
2750+
"""
2751+
version = dic["acqus"]["_coreheader"][0]
2752+
version = version.split("##TITLE= Parameter file, ")[1]
2753+
version = (
2754+
version.replace(" ", "")
2755+
.replace("\t", "")
2756+
.replace("Version", "")
2757+
.replace("TopSpin", "topspin.")
2758+
.replace("TOPSPIN", "topspin.")
2759+
)
2760+
if "XWIN-NMR" in version:
2761+
version = version.replace("XWIN-NMR", "xwin-nmr.").split(".")
2762+
else:
2763+
version = version.replace(" ", "").replace("pl", ".pl.").split(".")
2764+
2765+
parsed_version = []
2766+
for i in version:
2767+
try:
2768+
parsed_version.append(int(i))
2769+
except (TypeError, ValueError):
2770+
parsed_version.append(i)
2771+
2772+
version = f"{parsed_version[1]}.{parsed_version[2]}"
2773+
instrument = parsed_version[0]
2774+
try:
2775+
tag = ''.join(str(i) for i in parsed_version[3:])
2776+
except IndexError:
2777+
tag = ''
2778+
2779+
return version, tag, instrument

0 commit comments

Comments
 (0)