-
Notifications
You must be signed in to change notification settings - Fork 3
/
ser.py
1121 lines (872 loc) · 41.9 KB
/
ser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This module provides an interface to the SER file format written by FEI and Therm0 Fischer's program TIA.
It reads STEM and TEM images and other datasets.
It is based on information provided by Dr Chris Boothroyd and work by Peter Ercius' original serReader code in Matlab.
Note
----
General users:
Use the simplified ser.serReader() function to load the data and meta
data as a python dictionary.
Advanced users and developers:
Access the file internals through the ser.fileSER() class.
"""
import xml.etree.ElementTree as ET
from pathlib import Path
import os # TODO: Remove os and use pathlib instead.
import numpy as np
class NotSERError(Exception):
"""Exception if a file is not in SER file format.
"""
pass
class fileSER:
""" Class to represent SER files (read only).
Attributes
----------
_file_hdl : file
The open file as a raw stream.
_emi : dict
A dictionary of metadata from the EMI file accompanying the SER file.
filename : str
A string of the file name of the SER file.
head : dict
Header information for the SER file as a dictionary. Provides direct access to data offsets and other
internal file information.
Note
----
For most users, we suggest using the ser.serReader() function to
load the full data set into memory. Otherwise, this class provides
low level access to the SER file data and metadata and internals.
Examples
--------
Read data from a single image into memory using the low level API.
>>> import matplotlib.pyplot as plt
>>> import ncempy.io as nio
>>> with nio.ser.fileSER('filename.ser') as ser1:
>>> data, metadata = ser1.getDataset(0)
SER files are internally structured such that each image in a series is a
different data set. Thus, time series data should be read as the
following:
>>> with ser.fileSER('filename_1.ser') as ser1:
>>> image0, metadata0 = ser1.getDataset(0)
>>> image1, metadata1 = ser1.getDataset(1)
"""
_dictByteOrder = {0x4949: 'little endian'}
'''dict : Information on byte order.'''
_dictSeriesVersion = {0x0210: '< TIA 4.7.3', 0x0220: '>= TIA 4.7.3'}
'''dict : Information on file format version.'''
_dictDataTypeID = {0x4120: '1D datasets', 0x4122: '2D images'}
'''dict : Information on data type.'''
_dictTagTypeID = {0x4152: 'time only', 0x4142: 'time and 2D position'}
'''dict : Information on tag type.'''
_dictDataType = {1: ' <u1', 2: '<u2', 3: '<u4', 4: '<i1', 5: '<i2', 6: '<i4', 7: '<f4', 8: '<f8', 9: '<c8',
10: '<c16'}
'''dict : Information on data format.'''
def __init__(self, filename, verbose=False):
"""Init opening the file and reading in the header.
Parameters
----------
filename : str
Name of the SER file.
verbose : bool, optional
True to get extensive output while reading the file.
"""
# necessary declarations, if something fails
self._file_hdl = None
self._emi = None
self.filename = filename
self.head = None
# check filename type
if isinstance(self.filename, str):
pass
elif isinstance(self.filename, Path):
self.filename = str(self.filename)
else:
raise TypeError('Filename is supposed to be a string or pathlib.Path')
# try opening the file
try:
self._file_hdl = open(filename, 'rb')
except IOError:
print('Error reading file: "{}"'.format(filename))
raise
except:
raise
# read header
self.head = self.readHeader(verbose)
# read emi file if exists
self._read_emi()
def __del__(self):
""" Close the file stream in destructor.
"""
if not self._file_hdl.closed:
self._file_hdl.close()
def __enter__(self):
""" Implement python's with statement
"""
return self
def __exit__(self, exception_type, exception_value, traceback):
"""Implement python's with statement
and close the file via __del__()
"""
self.__del__()
return None
def __str__(self):
return 'ncempy SER data set'
def readHeader(self, verbose=False):
"""Read and return the SER files header.
Parameters
----------
verbose: bool
True to get extensive output while reading the file.
Returns
-------
: dict
The header of the SER file as dict.
"""
# prepare empty dict to be populated while reading
head = {}
# go back to beginning of file
self._file_hdl.seek(0, 0)
# read 3 int16
data = np.fromfile(self._file_hdl, dtype='<i2', count=3)
# ByteOrder (only little Endian expected)
if not data[0] in self._dictByteOrder:
raise RuntimeError('Only little Endian implemented for SER files')
head['ByteOrder'] = data[0]
if verbose:
print('ByteOrder:\t"{:#06x}",\t{}'.format(data[0], self._dictByteOrder[data[0]]))
# SeriesID, check whether TIA Series Data File
if not data[1] == 0x0197:
raise NotSERError('This is not a TIA Series Data File (SER)')
head['SeriesID'] = data[1]
if verbose:
print('SeriesID:\t"{:#06x},\tTIA Series Data File'.format(data[1]))
# SeriesVersion
if not data[2] in self._dictSeriesVersion:
raise RuntimeError('Unknown TIA version: "{:#06x}"'.format(data[2]))
head['SeriesVersion'] = data[2]
if verbose:
print('SeriesVersion:\t"{:#06x}",\t{}'.format(data[2], self._dictSeriesVersion[data[2]]))
# version dependent file format for below
if head['SeriesVersion'] == 0x0210:
offset_dtype = '<i4'
else:
# head['SeriesVersion']==0x220:
offset_dtype = '<i8'
# read 4 int32
data = np.fromfile(self._file_hdl, dtype='<i4', count=4)
# DataTypeID
if not data[0] in self._dictDataTypeID:
raise RuntimeError('Unknown DataTypeID: "{:#06x}"'.format(data[0]))
head['DataTypeID'] = data[0]
if verbose:
print('DataTypeID:\t"{:#06x}",\t{}'.format(data[0], self._dictDataTypeID[data[0]]))
# TagTypeID
if not data[1] in self._dictTagTypeID:
raise RuntimeError('Unknown TagTypeID: "{:#06x}"'.format(data[1]))
head['TagTypeID'] = data[1]
if verbose:
print('TagTypeID:\t"{:#06x}",\t{}'.format(data[1], self._dictTagTypeID[data[1]]))
# TotalNumberElements
if not data[2] >= 0:
raise RuntimeError('Negative total number of elements: {}'.format(data[2]))
head['TotalNumberElements'] = data[2]
if verbose:
print('TotalNumberElements:\t{}'.format(data[2]))
# ValidNumberElements
if not data[3] >= 0:
raise RuntimeError('Negative valid number of elements: {}'.format(data[3]))
head['ValidNumberElements'] = data[3]
if verbose:
print('ValidNumberElements:\t{}'.format(data[3]))
# OffsetArrayOffset, sensitive to SeriesVersion
data = np.fromfile(self._file_hdl, dtype=offset_dtype, count=1)
head['OffsetArrayOffset'] = data[0]
if verbose:
print('OffsetArrayOffset:\t{}'.format(data[0]))
# NumberDimensions
data = np.fromfile(self._file_hdl, dtype='<i4', count=1)
if not data[0] >= 0:
raise RuntimeError('Negative number of dimensions')
head['NumberDimensions'] = data[0]
if verbose:
print('NumberDimensions:\t{}'.format(data[0]))
# Dimensions array
dimensions = []
for i in range(head['NumberDimensions']):
if verbose:
print('reading Dimension {}'.format(i))
this_dim = {}
# DimensionSize
data = np.fromfile(self._file_hdl, dtype='<i4', count=1)
this_dim['DimensionSize'] = data[0]
if verbose:
print('DimensionSize:\t{}'.format(data[0]))
data = np.fromfile(self._file_hdl, dtype='<f8', count=2)
# CalibrationOffset
this_dim['CalibrationOffset'] = data[0]
if verbose:
print('CalibrationOffset:\t{}'.format(data[0]))
# CalibrationDelta
this_dim['CalibrationDelta'] = data[1]
if verbose:
print('CalibrationDelta:\t{}'.format(data[1]))
data = np.fromfile(self._file_hdl, dtype='<i4', count=2)
# CalibrationElement
this_dim['CalibrationElement'] = data[0]
if verbose:
print('CalibrationElement:\t{}'.format(data[0]))
# DescriptionLength
n = data[1]
# Description
data = np.fromfile(self._file_hdl, dtype='<i1', count=n)
data = ''.join(map(chr, data))
this_dim['Description'] = data
if verbose:
print('Description:\t{}'.format(data))
# UnitsLength
data = np.fromfile(self._file_hdl, dtype='<i4', count=1)
n = data[0]
# Units
data = np.fromfile(self._file_hdl, dtype='<i1', count=n)
data = ''.join(map(chr, data))
this_dim['Units'] = data
if verbose:
print('Units:\t{}'.format(data))
dimensions.append(this_dim)
# save dimensions array as tuple of dicts in head dict
head['Dimensions'] = tuple(dimensions)
# Offset array
self._file_hdl.seek(head['OffsetArrayOffset'], 0)
# DataOffsetArray
data = np.fromfile(self._file_hdl, dtype=offset_dtype, count=head['ValidNumberElements'])
head['DataOffsetArray'] = data.tolist()
if verbose:
print('reading in DataOffsetArray')
# TagOffsetArray
data = np.fromfile(self._file_hdl, dtype=offset_dtype, count=head['ValidNumberElements'])
head['TagOffsetArray'] = data.tolist()
if verbose:
print('reading in TagOffsetArray')
return head
def _checkIndex(self, i):
""" Check index i for sanity, otherwise raise Exception.
Parameters
----------
i: int
Index.
"""
# check type
if not isinstance(i, int):
raise TypeError('index supposed to be integer')
# check whether in range
if i < 0 or i >= self.head['ValidNumberElements']:
raise IndexError('Index out of range accessing element {} of {} valid elements'.format(i + 1, self.head[
'ValidNumberElements']))
return
def getDataset(self, index, verbose=False):
""" Retrieve data and meta data for one image or spectra
from the file.
Parameters
----------
index: int
Index of dataset.
verbose: bool, optional
True to get extensive output while reading the file.
Returns
-------
dataset: tuple, 2 elements in form (data metadata)
Tuple contains data as np.ndarray and metadata
(pixel size, etc.) as a dict.
"""
# check index, will raise Exceptions if not
try:
self._checkIndex(index)
except:
raise
if verbose:
print('Getting dataset {} of {}.'.format(index, self.head['ValidNumberElements']))
# go to dataset in file
self._file_hdl.seek(self.head['DataOffsetArray'][index], 0)
# read meta
meta = {}
# number of calibrations depends on DataTypeID
if self.head['DataTypeID'] == 0x4120:
n = 1
elif self.head['DataTypeID'] == 0x4122:
n = 2
else:
raise RuntimeError('Unknown DataTypeID')
# read in the calibrations
cals = []
for i in range(n):
if verbose:
print('Reading calibration {}'.format(i))
this_cal = {}
data = np.fromfile(self._file_hdl, dtype='<f8', count=2)
# CalibrationOffset
this_cal['CalibrationOffset'] = data[0]
if verbose:
print('CalibrationOffset:\t{}'.format(data[0]))
# CalibrationDelta
this_cal['CalibrationDelta'] = data[1]
if verbose:
print('CalibrationDelta:\t{}'.format(data[1]))
data = np.fromfile(self._file_hdl, dtype='<i4', count=1)
# CalibrationElement
this_cal['CalibrationElement'] = data[0]
if verbose:
print('CalibrationElement:\t{}'.format(data[0]))
cals.append(this_cal)
meta['Calibration'] = tuple(cals)
data = np.fromfile(self._file_hdl, dtype='<i2', count=1)
# DataType
meta['DataType'] = data[0]
if not data[0] in self._dictDataType:
raise RuntimeError('Unknown DataType: "{}"'.format(data[0]))
if verbose:
print('DataType:\t{},\t{}'.format(data[0], self._dictDataType[data[0]]))
dataset = None # in case something goes wrong
if self.head['DataTypeID'] == 0x4120:
# 1D data element
data = np.fromfile(self._file_hdl, dtype='<i4', count=1)
# ArrayLength
data = data.tolist()
meta['ArrayShape'] = data
if verbose:
print('ArrayShape:\t{}'.format(data))
dataset = np.fromfile(self._file_hdl, dtype=self._dictDataType[meta['DataType']],
count=meta['ArrayShape'][0])
elif self.head['DataTypeID'] == 0x4122:
# 2D data element
data = np.fromfile(self._file_hdl, dtype='<i4', count=2)
# ArrayShape
data = data.tolist()
meta['ArrayShape'] = data
if verbose:
print('ArrayShape:\t{}'.format(data))
# dataset
dataset = np.fromfile(self._file_hdl, dtype=self._dictDataType[meta['DataType']],
count=meta['ArrayShape'][0] * meta['ArrayShape'][1])
dataset = dataset.reshape(meta['ArrayShape'][::-1]) # needs to be reversed for little endian data
dataset = np.flipud(dataset)
return dataset, meta
def _getTag(self, index, verbose=False):
"""Retrieve tag from data file.
Parameters
----------
index: int
Index of tag.
verbose: bool
True to get extensive output while reading the file.
Returns
-------
tag: dict
Tag as a python dictionary.
"""
# check index, will raise Exceptions if not
try:
self._checkIndex(index)
except:
raise
if verbose:
print('Getting tag {} of {}.'.format(index, self.head['ValidNumberElements']))
tag = {}
try:
# bad tagoffsets occurred pointing to the end of the file
# go to dataset in file
self._file_hdl.seek(self.head['TagOffsetArray'][index], 0)
data = np.fromfile(self._file_hdl, dtype='<i4', count=2)
# TagTypeID
tag['TagTypeID'] = data[0]
# only proceed if TagTypeID is the same like in the file header (bad TagOffsetArray issue)
if tag['TagTypeID'] == self.head['TagTypeID']:
if verbose:
print('TagTypeID:\t"{:#06x}",\t{}'.format(data[0], self._dictTagTypeID[data[0]]))
# Time
tag['Time'] = data[1]
if verbose:
print('Time:\t{}'.format(data[1]))
# check for position
if tag['TagTypeID'] == 0x4142:
data = np.fromfile(self._file_hdl, dtype='<f8', count=2)
# PositionX
tag['PositionX'] = data[0]
if verbose:
print('PositionX:\t{}'.format(data[0]))
# PositionY
tag['PositionY'] = data[1]
if verbose:
print('PositionY:\t{}'.format(data[1]))
else:
# otherwise raise to get to default tag
raise
except:
tag['TagTypeID'] = 0
tag['Time'] = 0
tag['PositionX'] = np.nan
tag['PositionY'] = np.nan
return tag
def _createDim(self, size, offset, delta, element):
"""Create dimension labels for conversion to EMD
from information in the SER file.
Parameters
----------
size: int
Number of elements.
offset: float
Value at indicated element.
delta: float
Difference between elements.
element: int
Indicates the element of value offset.
Returns
-------
dim: np.ndarray
Dimension vector as array.
"""
# if element is out off range, map it back into defined
if element >= size:
element = size - 1
offset = offset - (element - (size - 1)) * delta
dim = np.array(range(size)).astype('f8')
dim = dim * delta
dim += (offset - dim[element])
# some weird shifting, positionx is +0.5, positiony is -0.5
# doing this during saving
# dim += 0.5*delta
return dim
def _read_emi(self):
# Generate emi file string
# and test for file existence.
emi_file = self.filename[:-6] + '.emi'
if not os.path.exists(emi_file):
self._emi = None
else:
self._emi = read_emi(emi_file)
def writeEMD(self, filename):
""" Write SER data to an EMD file.
Parameters
----------
filename: str
Name of the EMD file.
"""
# Todo: Update this to be much simpler. Can write this in a couple of lines now using the fileEMD class
from ncempy.io import emd
# create the EMD file and set version attributes
try:
f = emd.fileEMD(filename)
except:
raise IOError('Cannot write to file "{}"!'.format(filename))
# create EMD group
grp = f.file_hdl['data'].create_group(os.path.basename(self._file_hdl.name))
grp.attrs['emd_group_type'] = 1
# use first dataset to layout memory
data, first_meta = self.getDataset(0)
first_tag = self._getTag(0)
if self.head['DataTypeID'] == 0x4122:
# 2D datasets
self.head['ExperimentType'] = 'image' # text indicator of the experiment type
if first_tag['TagTypeID'] == 0x4142:
# 2D mapping
dset = grp.create_dataset('data', (self.head['Dimensions'][1]['DimensionSize'],
self.head['Dimensions'][0]['DimensionSize'],
first_meta['ArrayShape'][1], first_meta['ArrayShape'][0]),
dtype=self._dictDataType[first_meta['DataType']])
# collect time
time = np.zeros((self.head['Dimensions'][0]['DimensionSize'],
self.head['Dimensions'][1]['DimensionSize']), dtype='i4')
# create mapping dims for checking
map_xdim = self._createDim(self.head['Dimensions'][0]['DimensionSize'],
self.head['Dimensions'][0]['CalibrationOffset'],
self.head['Dimensions'][0]['CalibrationDelta'],
self.head['Dimensions'][0]['CalibrationElement'])
map_ydim = self._createDim(self.head['Dimensions'][1]['DimensionSize'],
self.head['Dimensions'][1]['CalibrationOffset'],
self.head['Dimensions'][1]['CalibrationDelta'],
self.head['Dimensions'][1]['CalibrationElement'])
# weird direction depend half pixel shifting
map_xdim += 0.5 * self.head['Dimensions'][0]['CalibrationDelta']
map_ydim -= 0.5 * self.head['Dimensions'][1]['CalibrationDelta']
for y in range(self.head['Dimensions'][0]['DimensionSize']):
for x in range(self.head['Dimensions'][1]['DimensionSize']):
index = int(x + y * self.head['Dimensions'][0]['DimensionSize'])
print('converting dataset {} of {}, items ({}, {})'.format(index + 1,
self.head['ValidNumberElements'],
x, y))
# retrieve dataset and put into buffer
data, meta = self.getDataset(index)
dset[y, x, :, :] = data[:, :]
# get tag data per image
tag = self._getTag(index)
time[y, x] = tag['Time']
assert (np.abs(tag['PositionX'] - map_xdim[x]) < np.abs(tag['PositionX'] * 1e-8))
assert (np.abs(tag['PositionY'] - map_ydim[y]) < np.abs(tag['PositionY'] * 1e-8))
del data, meta, tag
# create dimension datasets
dims = []
dims_time = []
# Position Y
assert self.head['Dimensions'][1]['Description'] == 'Position'
dims.append((map_ydim, self.head['Dimensions'][1]['Description'],
'[{}]'.format(self.head['Dimensions'][1]['Units'])))
dims_time.append((map_ydim, self.head['Dimensions'][1]['Description'],
'[{}]'.format(self.head['Dimensions'][1]['Units'])))
# Position X
assert self.head['Dimensions'][0]['Description'] == 'Position'
dims.append((map_xdim, self.head['Dimensions'][0]['Description'],
'[{}]'.format(self.head['Dimensions'][0]['Units'])))
dims_time.append((map_xdim, self.head['Dimensions'][0]['Description'],
'[{}]'.format(self.head['Dimensions'][0]['Units'])))
dim = self._createDim(first_meta['ArrayShape'][1], first_meta['Calibration'][1]['CalibrationOffset'],
first_meta['Calibration'][1]['CalibrationDelta'],
first_meta['Calibration'][1]['CalibrationElement'])
dims.append((dim, 'y', '[m]'))
dim = self._createDim(first_meta['ArrayShape'][0], first_meta['Calibration'][0]['CalibrationOffset'],
first_meta['Calibration'][0]['CalibrationDelta'],
first_meta['Calibration'][0]['CalibrationElement'])
dims.append((dim, 'x', '[m]'))
# write dimensions
for ii in range(len(dims)):
f.write_dim('dim{:d}'.format(ii + 1), dims[ii], grp)
# write out time as additional dataset
_ = f.put_emdgroup('timestamp', time, dims_time, parent=grp)
else:
# 1 entry series to single image
if self.head['ValidNumberElements'] == 1:
# get image
data, meta = self.getDataset(0)
tag = self._getTag(0)
# create dimensions
dims = []
dim = self._createDim(first_meta['ArrayShape'][1],
first_meta['Calibration'][1]['CalibrationOffset'],
first_meta['Calibration'][1]['CalibrationDelta'],
first_meta['Calibration'][1]['CalibrationElement'])
dims.append((dim, 'y', '[m]'))
dim = self._createDim(first_meta['ArrayShape'][0],
first_meta['Calibration'][0]['CalibrationOffset'],
first_meta['Calibration'][0]['CalibrationDelta'],
first_meta['Calibration'][0]['CalibrationElement'])
dims.append((dim, 'x', '[m]'))
dset = grp.create_dataset('data', (first_meta['ArrayShape'][1],
first_meta['ArrayShape'][0]),
dtype=self._dictDataType[first_meta['DataType']])
dset[:, :] = data[:, :]
for i in range(len(dims)):
f.write_dim('dim{:d}'.format(i + 1), dims[i], grp)
dset.attrs['timestamp'] = tag['Time']
else:
# simple series
dset = grp.create_dataset('data', (self.head['ValidNumberElements'],
first_meta['ArrayShape'][1], first_meta['ArrayShape'][0]),
dtype=self._dictDataType[first_meta['DataType']])
# collect time
time = np.zeros(self.head['ValidNumberElements'], dtype='i4')
for i in range(self.head['ValidNumberElements']):
print('converting dataset {} of {}'.format(i + 1, self.head['ValidNumberElements']))
# retrieve dataset and put into buffer
data, meta = self.getDataset(i)
dset[i, :, :] = data[:, :]
# get tag data per image
tag = self._getTag(i)
time[i] = tag['Time']
# create dimension data sets
dims = []
# first SER dimension is number
assert self.head['Dimensions'][0]['Description'] == 'Number'
dim = self._createDim(self.head['Dimensions'][0]['DimensionSize'],
self.head['Dimensions'][0]['CalibrationOffset'],
self.head['Dimensions'][0]['CalibrationDelta'],
self.head['Dimensions'][0]['CalibrationElement'])
dims.append((dim[0:self.head['ValidNumberElements']],
self.head['Dimensions'][0]['Description'],
'[{}]'.format(self.head['Dimensions'][0]['Units'])))
dim = self._createDim(first_meta['ArrayShape'][1],
first_meta['Calibration'][1]['CalibrationOffset'],
first_meta['Calibration'][1]['CalibrationDelta'],
first_meta['Calibration'][1]['CalibrationElement'])
dims.append((dim, 'y', '[m]'))
dim = self._createDim(first_meta['ArrayShape'][0],
first_meta['Calibration'][0]['CalibrationOffset'],
first_meta['Calibration'][0]['CalibrationDelta'],
first_meta['Calibration'][0]['CalibrationElement'])
dims.append((dim, 'x', '[m]'))
# write dimensions
for i in range(len(dims)):
f.write_dim('dim{:d}'.format(i + 1), dims[i], grp)
# write out time as additional dim vector
f.write_dim('dim1_time', (time, 'timestamp', '[s]'), grp)
elif self.head['DataTypeID'] == 0x4120:
# 1D datasets; spectra
self.head['ExperimentType'] = 'spectrum' # text indicator of the experiment type
if first_tag['TagTypeID'] == 0x4142:
# 2D mapping
dset = grp.create_dataset('data', (self.head['Dimensions'][1]['DimensionSize'],
self.head['Dimensions'][0]['DimensionSize'],
first_meta['ArrayShape'][0]),
dtype=self._dictDataType[first_meta['DataType']])
time = np.zeros((self.head['Dimensions'][0]['DimensionSize'],
self.head['Dimensions'][1]['DimensionSize']), dtype='i4')
# create mapping dims for checking
map_xdim = self._createDim(self.head['Dimensions'][0]['DimensionSize'],
self.head['Dimensions'][0]['CalibrationOffset'],
self.head['Dimensions'][0]['CalibrationDelta'],
self.head['Dimensions'][0]['CalibrationElement'])
map_ydim = self._createDim(self.head['Dimensions'][1]['DimensionSize'],
self.head['Dimensions'][1]['CalibrationOffset'],
self.head['Dimensions'][1]['CalibrationDelta'],
self.head['Dimensions'][1]['CalibrationElement'])
# weird direction depend half pixel shifting
map_xdim += 0.5 * self.head['Dimensions'][0]['CalibrationDelta']
map_ydim -= 0.5 * self.head['Dimensions'][1]['CalibrationDelta']
for y in range(self.head['Dimensions'][0]['DimensionSize']):
for x in range(self.head['Dimensions'][1]['DimensionSize']):
index = int(x + y * self.head['Dimensions'][0]['DimensionSize'])
print('converting dataset {} of {}, items ({}, {})'.format(index + 1,
self.head['ValidNumberElements'],
x, y))
# retrieve dataset and put into buffer
data, meta = self.getDataset(index)
dset[y, x, :] = np.copy(data[:])
# get tag data per image
tag = self._getTag(index)
time[y, x] = tag['Time']
assert (np.abs(tag['PositionX'] - map_xdim[x]) < np.abs(tag['PositionX'] * 1e-8))
assert (np.abs(tag['PositionY'] - map_ydim[y]) < np.abs(tag['PositionY'] * 1e-8))
del data, meta, tag
# create dimension datasets
dims = []
dims_time = []
# Position Y
assert self.head['Dimensions'][1]['Description'] == 'Position'
dims.append((map_ydim, self.head['Dimensions'][1]['Description'],
'[{}]'.format(self.head['Dimensions'][1]['Units'])))
dims_time.append((map_ydim, self.head['Dimensions'][1]['Description'],
'[{}]'.format(self.head['Dimensions'][1]['Units'])))
# Position X
assert self.head['Dimensions'][0]['Description'] == 'Position'
dims.append((map_xdim, self.head['Dimensions'][0]['Description'],
'[{}]'.format(self.head['Dimensions'][0]['Units'])))
dims_time.append((map_xdim, self.head['Dimensions'][0]['Description'],
'[{}]'.format(self.head['Dimensions'][0]['Units'])))
dim = self._createDim(first_meta['ArrayShape'][0], first_meta['Calibration'][0]['CalibrationOffset'],
first_meta['Calibration'][0]['CalibrationDelta'],
first_meta['Calibration'][0]['CalibrationElement'])
dims.append((dim, 'E', '[m_eV]'))
# write dimensions
for i in range(len(dims)):
f.write_dim('dim{:d}'.format(i + 1), dims[i], grp)
# write out time as additional dataset
_ = f.put_emdgroup('timestamp', time, dims_time, parent=grp)
else:
# simple series
dset = grp.create_dataset('data', (self.head['ValidNumberElements'], first_meta['ArrayShape'][0]),
dtype=self._dictDataType[first_meta['DataType']])
# collect time
time = np.zeros(self.head['ValidNumberElements'], dtype='i4')
for i in range(self.head['ValidNumberElements']):
print('converting dataset {} of {}'.format(i + 1, self.head['ValidNumberElements']))
# retrieve dataset and put into buffer
data, meta = self.getDataset(i)
dset[i, :] = data[:]
# get tag data per image
tag = self._getTag(i)
time[i] = tag['Time']
# create dimension datasets
dims = []
# first SER dimension is number
assert self.head['Dimensions'][0]['Description'] == 'Number'
dim = self._createDim(self.head['Dimensions'][0]['DimensionSize'],
self.head['Dimensions'][0]['CalibrationOffset'],
self.head['Dimensions'][0]['CalibrationDelta'],
self.head['Dimensions'][0]['CalibrationElement'])
dims.append((dim[0:self.head['ValidNumberElements']], self.head['Dimensions'][0]['Description'],
'[{}]'.format(self.head['Dimensions'][0]['Units'])))
dim = self._createDim(first_meta['ArrayShape'][0], first_meta['Calibration'][0]['CalibrationOffset'],
first_meta['Calibration'][0]['CalibrationDelta'],
first_meta['Calibration'][0]['CalibrationElement'])
dims.append((dim, 'E', '[m_eV]'))
# write dimensions
for i in range(len(dims)):
f.write_dim('dim{:d}'.format(i + 1), dims[i], grp)
# write out time as additional dim vector
f.write_dim('dim1_time', (time, 'timestamp', '[s]'), grp)
else:
raise RuntimeError('Unknown DataTypeID')
# put meta information from _emi to Microscope group, if available
if self._emi:
for key in self._emi:
if not self._emi[key] is None:
f.microscope.attrs[key] = self._emi[key]
# write comment into Comment group
f.put_comment('Converted SER file "{}" to EMD using the openNCEM tools.'.format(self._file_hdl.name))
def read_emi(filename):
"""Read the meta data from an emi file.
Parameters
----------
filename: str or pathlib.Path
Path to the emi file.
Returns
-------
: dict
Dictionary of experimental metadata stored in the EMI file.
"""
# check filename type
if isinstance(filename, str):
pass
elif isinstance(filename, Path):
filename = str(filename)
else:
raise TypeError('Filename is supposed to be a string or pathlib.Path')
# try opening the file
try:
# open file for reading bytes, as binary and text are intermixed
with open(filename, 'rb') as f_emi:
emi_data = f_emi.read()
except IOError:
print('Error reading file: "{}"'.format(filename))
raise
except:
raise
# dict to store _emi stuff
_emi = {}
# need anything readable from <ObjectInfo> to </ObjectInfo>
# collect = False
# data = b''
# for line in f_emi:
# if b'<ObjectInfo>' in line:
# collect = True
# if collect:
# data += line.strip()
# if b'</ObjectInfo>' in line:
# collect = False
# close the file
# f_emi.close()
metaStart = emi_data.find(b'<ObjectInfo>')
metaEnd = emi_data.find(b'</ObjectInfo>') # need to add len('</ObjectInfo>') = 13 to encompass this final tag
root = ET.fromstring(emi_data[metaStart:metaEnd + 13])
# strip of binary stuff still around
# data = data.decode('ascii', errors='ignore')
# matchObj = re.search('<ObjectInfo>(.+?)</ObjectInfo', data)
# try:
# data = matchObj.group(1)
# except:
# raise RuntimeError('Could not find _emi metadata in specified file.')
# parse metadata as xml
# root = ET.fromstring('<_emi>' + data + '</_emi>')
# single items
_emi['Uuid'] = root.findtext('Uuid')
_emi['AcquireDate'] = root.findtext('AcquireDate')
_emi['Manufacturer'] = root.findtext('Manufacturer')
_emi['DetectorPixelHeight'] = root.findtext('DetectorPixelHeight')
_emi['DetectorPixelWidth'] = root.findtext('DetectorPixelWidth')
# Microscope Conditions
grp = root.find('ExperimentalConditions/MicroscopeConditions')
for elem in grp:
_emi[elem.tag] = _parseEntry_emi(elem.text)
# Experimental Description
grp = root.find('ExperimentalDescription/Root')
for elem in grp:
_emi['{} [{}]'.format(elem.findtext('Label'), elem.findtext('Unit'))] = _parseEntry_emi(
elem.findtext('Value'))
# AcquireInfo
grp = root.find('AcquireInfo')
for elem in grp:
_emi[elem.tag] = _parseEntry_emi(elem.text)
# DetectorRange
grp = root.find('DetectorRange')