11"""
2- Functions for reading and Tecmag .tnt data files
2+ Functions for reading Tecmag .tnt data files.
33"""
4- __developer_info__ = """
4+ __developer_doc__ = """
55Tecmag .tnt file format information
6- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7-
6+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
87The Tecmag .tnt file format is documented with C pseudo-code in the file
98"A1 - TNMR File Format.doc" distributed with the TNMR software.
109
11- This file is based on the pytnt module available at
12- https://www.github.com/chatcannon/pytnt
13- Please inform upstream if you find a bug or want a new feature .
10+ This file is based on the
11+ `pytnt module < https://www.github.com/chatcannon/pytnt>`_.
12+ Please inform upstream if you find a bug or to request additional features .
1413
1514"""
1615
1716import io
1817import re
19- from collections import OrderedDict
2018
2119import numpy as np
2220
2321from . import fileiobase
2422
2523
26- tntMagic_re = re .compile (b"^TNT1\.\d\d\d$" )
24+ TNTMAGIC_RE = re .compile (b"^TNT1\.\d\d\d$" )
2725
28- tntMagic = np .dtype ('a8' )
29- tntTLV = np .dtype ([('tag' , 'a4' ), ('bool' , '<u4' ), ('length' , '<u4' )])
26+ TNTMAGIC = np .dtype ('a8' )
27+ TNTTLV = np .dtype ([('tag' , 'a4' ), ('bool' , '<u4' ), ('length' , '<u4' )])
3028
31- tntTMAG = np .dtype ([
29+ TNTTMAG = np .dtype ([
3230 ('npts' , '<i4' , 4 ),
3331 ('actual_npts' , '<i4' , 4 ),
3432 ('acq_points' , '<i4' ),
109107 ('sequence' , 'a32' ),
110108 ('lock_solvent' , 'a16' ),
111109 ('lock_nucleus' , 'a16' )
112- ])
110+ ])
113111
114112
115- tntGridAndAxis = np .dtype ([
113+ TNTGRIDANDAXIS = np .dtype ([
116114 ('majorTickInc' , '<f8' , 12 ),
117115 ('minorIntNum' , '<i2' , 12 ),
118116 ('labelPrecision' , '<i2' , 12 ),
128126])
129127
130128
131- tntTMG2 = np .dtype ([
129+ TNTTMG2 = np .dtype ([
132130 ('real_flag' , '<u4' ),
133131 ('imag_flag' , '<u4' ),
134132 ('magn_flag' , '<u4' ),
155153 ('ampCtl' , '<f8' ),
156154 ('offset' , '<i4' ),
157155
158- ('axis_set' , tntGridAndAxis ),
156+ ('axis_set' , TNTGRIDANDAXIS ),
159157
160158 ('display_units' , '<i2' , 4 ),
161159 ('ref_point' , '<i4' , 4 ),
235233
236234def read (filename ):
237235 """
238- Read a .tnt data file
236+ Read a Tecmag .tnt data file.
239237
240238 Parameters
241239 ----------
@@ -250,60 +248,62 @@ def read(filename):
250248 Array of NMR data.
251249
252250 """
253- tnt_sections = OrderedDict ()
251+ tnt_sections = dict ()
254252
255253 with open (filename , 'rb' ) as tntfile :
256254
257- tntmagic = np .fromstring (tntfile .read (tntMagic .itemsize ),
258- tntMagic , count = 1 )[0 ]
255+ tntmagic = np .fromstring (tntfile .read (TNTMAGIC .itemsize ),
256+ TNTMAGIC , count = 1 )[0 ]
259257
260- if not tntMagic_re .match (tntmagic ):
261- raise ValueError ("Invalid magic number (is '%s' really a TNMR file?): %s" % (filename , tntmagic ))
258+ if not TNTMAGIC_RE .match (tntmagic ):
259+ err = ("Invalid magic number (is '%s' really TNMR file?): %s" %
260+ (filename , tntmagic ))
261+ raise ValueError (err )
262262
263263 ##Read in the section headers
264- tnthdrbytes = tntfile .read (tntTLV .itemsize )
265- while (tntTLV .itemsize == len (tnthdrbytes )):
266- TLV = np .fromstring (tnthdrbytes , tntTLV )[0 ]
267- data_length = TLV ['length' ]
264+ tnthdrbytes = tntfile .read (TNTTLV .itemsize )
265+ while (TNTTLV .itemsize == len (tnthdrbytes )):
266+ tlv = np .fromstring (tnthdrbytes , TNTTLV )[0 ]
267+ data_length = tlv ['length' ]
268268 hdrdict = {'offset' : tntfile .tell (),
269269 'length' : data_length ,
270- 'bool' : bool (TLV ['bool' ])}
270+ 'bool' : bool (tlv ['bool' ])}
271271 if data_length <= 4096 :
272272 hdrdict ['data' ] = tntfile .read (data_length )
273273 assert (len (hdrdict ['data' ]) == data_length )
274274 else :
275275 tntfile .seek (data_length , io .SEEK_CUR )
276- tnt_sections [TLV ['tag' ]] = hdrdict
277- tnthdrbytes = tntfile .read (tntTLV .itemsize )
276+ tnt_sections [tlv ['tag' ]] = hdrdict
277+ tnthdrbytes = tntfile .read (TNTTLV .itemsize )
278278
279- assert (tnt_sections ['TMAG' ]['length' ] == tntTMAG .itemsize )
280- TMAG = np .fromstring (tnt_sections ['TMAG' ]['data' ], tntTMAG , count = 1 )[0 ]
279+ assert (tnt_sections ['TMAG' ]['length' ] == TNTTMAG .itemsize )
280+ tmag = np .fromstring (tnt_sections ['TMAG' ]['data' ], TNTTMAG , count = 1 )[0 ]
281281
282282 assert (tnt_sections ['DATA' ]['length' ] ==
283- TMAG ['actual_npts' ].prod () * 8 )
283+ tmag ['actual_npts' ].prod () * 8 )
284284 ## For some reason we can't set offset and shape together
285285 #DATA = np.memmap(tntfilename,np.dtype('<c8'), mode='r',
286286 # offset=self.tnt_sections['DATA']['offset'],
287287 # shape=self.TMAG['actual_npts'].tolist(),order='F')
288- DATA = np .memmap (filename , np .dtype ('<c8' ), mode = 'c' ,
288+ data = np .memmap (filename , np .dtype ('<c8' ), mode = 'c' ,
289289 offset = tnt_sections ['DATA' ]['offset' ],
290- shape = TMAG ['actual_npts' ].prod ())
291- DATA = np .reshape (DATA , TMAG ['actual_npts' ], order = 'F' )
290+ shape = tmag ['actual_npts' ].prod ())
291+ data = np .reshape (data , tmag ['actual_npts' ], order = 'F' )
292292
293- assert (tnt_sections ['TMG2' ]['length' ] == tntTMG2 .itemsize )
294- TMG2 = np .fromstring (tnt_sections ['TMG2' ]['data' ], tntTMG2 , count = 1 )[0 ]
293+ assert (tnt_sections ['TMG2' ]['length' ] == TNTTMG2 .itemsize )
294+ tmg2 = np .fromstring (tnt_sections ['TMG2' ]['data' ], TNTTMG2 , count = 1 )[0 ]
295295
296296 dic = dict ()
297- for name in tntTMAG .names :
297+ for name in TNTTMAG .names :
298298 if not name .startswith ('space' ):
299- dic [name ] = TMAG [name ]
300- for name in tntTMG2 .names :
299+ dic [name ] = tmag [name ]
300+ for name in TNTTMG2 .names :
301301 if name not in ['Boolean_space' , 'unused' , 'space' , 'axis_set' ]:
302- dic [name ] = TMG2 [name ]
303- for name in tntGridAndAxis .names :
304- dic [name ] = TMG2 ['axis_set' ][name ]
302+ dic [name ] = tmg2 [name ]
303+ for name in TNTGRIDANDAXIS .names :
304+ dic [name ] = tmg2 ['axis_set' ][name ]
305305
306- return dic , DATA
306+ return dic , data
307307
308308
309309def guess_udic (dic , data ):
0 commit comments