File: _api.py

package info (click to toggle)
python-rosettasciio 0.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144,644 kB
  • sloc: python: 36,638; xml: 2,582; makefile: 20; ansic: 4
file content (847 lines) | stat: -rw-r--r-- 28,684 bytes parent folder | download
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
# -*- coding: utf-8 -*-
# Copyright 2007-2023 The HyperSpy developers
#
# This file is part of RosettaSciIO.
#
# RosettaSciIO is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RosettaSciIO is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with RosettaSciIO. If not, see <https://www.gnu.org/licenses/#GPL>.

import logging
import os
import struct
import warnings
import xml.etree.ElementTree as ET
from collections import OrderedDict
from glob import glob

import numpy as np
from dateutil import parser

from rsciio._docstrings import FILENAME_DOC, LAZY_DOC, RETURNS_DOC
from rsciio.utils.tools import DTBox, sarray2dict

_logger = logging.getLogger(__name__)


data_types = {
    "1": "<u1",
    "2": "<u2",
    "3": "<u4",
    "4": "<i1",
    "5": "<i2",
    "6": "<i4",
    "7": "<f4",
    "8": "<f8",
    "9": "<c8",
    "10": "<c16",
}

XY_TAG_ID = 16706  # header contains XY calibration


def readLELongLong(file):
    """Read 8 bytes as *little endian* integer in file"""
    read_bytes = file.read(8)
    return struct.unpack("<Q", read_bytes)[0]


def readLELong(file):
    """Read 4 bytes as *little endian* integer in file"""
    read_bytes = file.read(4)
    return struct.unpack("<L", read_bytes)[0]


def readLEShort(file):
    """Read 2 bytes as *little endian* integer in file"""
    read_bytes = file.read(2)
    return struct.unpack("<H", read_bytes)[0]


def dimension_array_dtype(n, DescriptionLength, UnitsLength):
    dt_list = [
        ("Dim-%s_DimensionSize" % n, ("<u4")),
        ("Dim-%s_CalibrationOffset" % n, "<f8"),
        ("Dim-%s_CalibrationDelta" % n, "<f8"),
        ("Dim-%s_CalibrationElement" % n, "<u4"),
        ("Dim-%s_DescriptionLength" % n, "<u4"),
        ("Dim-%s_Description" % n, (bytes, DescriptionLength)),
        ("Dim-%s_UnitsLength" % n, "<u4"),
        ("Dim-%s_Units" % n, (bytes, UnitsLength)),
    ]
    return dt_list


def get_lengths(file):
    file.seek(24, 1)
    description_length = readLELong(file)
    file.seek(description_length, 1)
    unit_length = readLELong(file)
    file.seek(unit_length, 1)
    return description_length, unit_length


def get_header_dtype_list(file):
    # Read the first part of the header
    header_list1 = [
        ("ByteOrder", "<u2"),
        ("SeriesID", "<u2"),
        ("SeriesVersion", "<u2"),
        ("DataTypeID", "<u4"),
        ("TagTypeID", "<u4"),
        ("TotalNumberElements", "<u4"),
        ("ValidNumberElements", "<u4"),
    ]
    header1 = np.fromfile(file, dtype=np.dtype(header_list1), count=1)
    # Depending on the SeriesVersion, the OffsetArrayOffset is 4 or 8 bytes
    if header1["SeriesVersion"] <= 528:
        OffsetArrayOffset_dtype = "<u4"
        beginning_dimension_array_section = 30
    else:
        OffsetArrayOffset_dtype = "<u8"
        beginning_dimension_array_section = 34

    # Once we know the type of the OffsetArrayOffset, we can continue reading
    # the 2nd part of the header
    file.seek(22)
    header_list2 = [
        ("OffsetArrayOffset", OffsetArrayOffset_dtype),
        ("NumberDimensions", "<u4"),
    ]
    header2 = np.fromfile(file, dtype=np.dtype(header_list2), count=1)
    header_list = header_list1 + header_list2
    # Go to the beginning of the dimension array section
    file.seek(beginning_dimension_array_section)
    for n in range(1, header2["NumberDimensions"][0] + 1):
        description_length, unit_length = get_lengths(file)
        header_list += dimension_array_dtype(n, description_length, unit_length)
    file.seek(0)
    return header_list


def get_data_dtype_list(file, offset, record_by):
    if record_by == "spectrum":
        file.seek(offset + 20)
        data_type = readLEShort(file)
        array_size = readLELong(file)
        header = [
            ("CalibrationOffset", ("<f8")),
            ("CalibrationDelta", "<f8"),
            ("CalibrationElement", "<u4"),
            ("DataType", "<u2"),
            ("ArrayLength", "<u4"),
            ("Array", (data_types[str(data_type)], array_size)),
        ]
        shape = array_size
    elif record_by == "image":  # Untested
        file.seek(offset + 40)
        data_type = readLEShort(file)
        array_size_x = readLELong(file)
        array_size_y = readLELong(file)
        header = [
            ("CalibrationOffsetX", ("<f8")),
            ("CalibrationDeltaX", "<f8"),
            ("CalibrationElementX", "<u4"),
            ("CalibrationOffsetY", ("<f8")),
            ("CalibrationDeltaY", "<f8"),
            ("CalibrationElementY", "<u4"),
            ("DataType", "<u2"),
            ("ArraySizeX", "<u4"),
            ("ArraySizeY", "<u4"),
            ("Array", (data_types[str(data_type)], (array_size_x, array_size_y))),
        ]
        shape = (array_size_x, array_size_y)
    return header, shape


def get_data_tag_dtype_list(data_type_id):
    if data_type_id == XY_TAG_ID:
        header = [
            ("TagTypeID", ("<u2")),
            ("Unknown", ("<u2")),  # Not in Boothroyd description. = 0
            ("Time", "<u4"),  # The precision is one second...
            ("PositionX", "<f8"),
            ("PositionY", "<f8"),
        ]
    else:  # elif data_type_id == ?????, 16722?
        header = [
            ("TagTypeID", ("<u2")),
            # Not in Boothroyd description. = 0. Not tested.
            ("Unknown", ("<u2")),
            ("Time", "<u4"),  # The precision is one second...
        ]
    return header


def log_struct_array_values(struct_array):
    for key in struct_array.dtype.names:
        if (
            not isinstance(struct_array[key], np.ndarray)
            or np.array(struct_array[key].shape).sum() == 1
        ):
            _logger.info("%s : %s", key, struct_array[key])
        else:
            _logger.info("%s : Array", key)


def guess_record_by(record_by_id):
    if record_by_id == 16672:
        return "spectrum"
    else:
        return "image"


def parse_ExperimentalDescription(et, dictree):
    dictree.add_node(et.tag)
    dictree = dictree[et.tag]
    for data in et.find("Root").findall("Data"):
        label = data.find("Label").text
        value = data.find("Value").text
        units = data.find("Unit").text
        item = label if not units else label + "_%s" % units
        try:
            # try to coerce value to decimal representation
            value = float(value) if units else value
        except ValueError:
            _logger.warning(
                f"Expected decimal value for {label}, " f"but received {value} instead"
            )
        dictree[item] = value


def parse_TrueImageHeaderInfo(et, dictree):
    dictree.add_node(et.tag)
    dictree = dictree[et.tag]
    et = ET.fromstring(et.text)
    for data in et.findall(b"Data"):
        dictree[data.find(b"Index").text] = float(data.find(b"Value").text)


def emixml2dtb(et, dictree):
    if et.tag == "ExperimentalDescription":
        parse_ExperimentalDescription(et, dictree)
        return
    elif et.tag == "TrueImageHeaderInfo":
        parse_TrueImageHeaderInfo(et, dictree)
        return
    if et.text:
        dictree[et.tag] = et.text
        return
    else:
        dictree.add_node(et.tag)
        for child in et:
            emixml2dtb(child, dictree[et.tag])


def emi_reader(filename, lazy=False, only_valid_data=True, dump_xml=False):
    # TODO: recover the tags from the emi file. It is easy: just look for
    # <ObjectInfo> and </ObjectInfo>. It is standard xml :)
    # xml chunks are identified using UUID, if we can find how these UUID are
    # generated then, it will possible to match to the corresponding ser file
    # and add the detector information in the metadata
    objects = get_xml_info_from_emi(filename)
    orig_fname = filename
    filename = os.path.splitext(filename)[0]
    if dump_xml:
        for i, obj in enumerate(objects):
            with open(filename + "-object-%s.xml" % i, "w") as f:
                f.write(obj)

    ser_files = sorted(glob(filename + "_[0-9].ser"))
    sers = []
    for f in ser_files:
        _logger.info("Opening %s", f)
        try:
            sers.extend(ser_reader(f, objects, lazy, only_valid_data))
        except IOError:  # Probably a single spectrum that we don't support
            continue

        index = int(os.path.splitext(f)[0].split("_")[-1]) - 1
        op = DTBox(sers[-1]["original_metadata"], box_dots=True)

        # defend against condition where more ser files are present than object
        # metadata defined in emi
        if index < len(objects):
            emixml2dtb(ET.fromstring(objects[index]), op)
        else:
            _logger.warning(
                f"{orig_fname} did not contain any metadata for "
                f"{f}, so only .ser header information was read"
            )
        sers[-1]["original_metadata"] = op.to_dict()
    return sers


def file_reader(filename, lazy=False, only_valid_data=True):
    """
    Read sets of ``.ser`` and ``.emi`` files from the FEI/ThermoFisher software TIA
    (TEM Imaging & Analysis).

    Parameters
    ----------
    %s
    %s
    only_valid_data : bool, Default=True
        For cases, where acquisition of series or linescan data stopped before
        the end. If `True`, load only the acquired data. If `False`, the empty
        data are filled with zeros.

    %s
    """
    ext = os.path.splitext(filename)[1][1:]
    if ext.lower() == "ser":
        to_return = ser_reader(
            filename, objects=None, lazy=lazy, only_valid_data=only_valid_data
        )
    elif ext.lower() == "emi":
        to_return = emi_reader(filename, lazy, only_valid_data)
    else:
        raise ValueError(f"'{ext}' is not a supported extension for the TIA reader.")

    return to_return


file_reader.__doc__ %= (FILENAME_DOC, LAZY_DOC, RETURNS_DOC)


def load_ser_file(filename):
    _logger.info("Opening the file: %s", filename)
    with open(filename, "rb") as f:
        header = np.fromfile(f, dtype=np.dtype(get_header_dtype_list(f)), count=1)
        _logger.info("Header info:")
        log_struct_array_values(header[0])

        if header["ValidNumberElements"] == 0:
            raise IOError(
                "The file does not contains valid data. "
                "If it is a single spectrum, the data is contained in the  "
                ".emi file but HyperSpy cannot currently extract this "
                "information."
            )

        # Read the first element of data offsets
        f.seek(header["OffsetArrayOffset"][0])
        # OffsetArrayOffset can contain 4 or 8 bytes integer depending if the
        # data have been acquired using a 32 or 64 bits platform.
        if header["SeriesVersion"] <= 528:
            data_offset = readLELong(f)
            data_offset_array = np.fromfile(
                f, dtype="<u4", count=header["ValidNumberElements"][0]
            )
        else:
            data_offset = readLELongLong(f)
            data_offset_array = np.fromfile(
                f, dtype="<u8", count=header["ValidNumberElements"][0]
            )
        data_dtype_list, shape = get_data_dtype_list(
            f, data_offset, guess_record_by(header["DataTypeID"])
        )
        tag_dtype_list = get_data_tag_dtype_list(header["TagTypeID"])
        f.seek(data_offset)
        data = np.empty(
            header["ValidNumberElements"][0],
            dtype=np.dtype(data_dtype_list + tag_dtype_list),
        )
        for i, offset in enumerate(data_offset_array):
            data[i] = np.fromfile(
                f, dtype=np.dtype(data_dtype_list + tag_dtype_list), count=1
            )
            f.seek(offset)
        _logger.info("Data info:")
        log_struct_array_values(data[0])
    return header, data


def get_xml_info_from_emi(emi_file):
    with open(emi_file, "rb") as f:
        tx = f.read()
    objects = []
    i_start = 0
    while i_start != -1:
        i_start += 1
        i_start = tx.find(b"<ObjectInfo>", i_start)
        i_end = tx.find(b"</ObjectInfo>", i_start)
        objects.append(tx[i_start : i_end + 13].decode("utf-8"))
    return objects[:-1]


def get_calibration_from_position(position):
    """Compute the size, scale and offset of a linear axis from coordinates.

    This function assumes rastering on a regular grid for the full size of
    each dimension before rastering over another one. Fox example: a11, a12,
    a13, a21, a22, a23 for a 2x3 grid.

    Parameters
    ----------
    position: numpy array.
        Position coordinates of the axis. Normally as in PositionX/Y of the
        ser file.

    Returns
    -------
    axis_attr: dictionary with `size`, `scale`, `offeset` keys.

    """
    offset = position[0]
    for i, x in enumerate(position):
        if x != position[0]:
            break
    if i == len(position) - 1:
        # No scanning over this axis
        scale = 0
        size = 0
    else:
        scale = x - position[0]
        if i == 1:  # Rastering over this dimension first
            for j, x in enumerate(position[1:]):
                if x == position[0]:
                    break
            size = j + 1
        else:  # Second rastering dimension
            size = len(position) / i
    axis_attr = {"size": size, "scale": scale, "offset": offset}
    return axis_attr


def get_axes_from_position(header, data):
    array_shape = []
    axes = []
    array_size = int(header["ValidNumberElements"])
    if data[b"TagTypeID"][0] == XY_TAG_ID:
        xcal = get_calibration_from_position(data[b"PositionX"])
        ycal = get_calibration_from_position(data[b"PositionY"])
        if xcal[b"size"] == 0 and ycal[b"size"] != 0:
            # Vertical line scan
            axes.append(
                {
                    "name": "x",
                    "units": "meters",
                    "index_in_array": 0,
                }
            )
            axes[-1].update(xcal)
            array_shape.append(axes[-1]["size"])

        elif xcal[b"size"] != 0 and ycal[b"size"] == 0:
            # Horizontal line scan
            axes.append(
                {
                    "name": "y",
                    "units": "meters",
                    "index_in_array": 0,
                }
            )
            axes[-1].update(ycal)
            array_shape.append(axes[-1]["size"])

        elif xcal[b"size"] * ycal[b"size"] == array_size:
            # Signal2D
            axes.append(
                {
                    "name": "y",
                    "units": "meters",
                    "index_in_array": 0,
                }
            )
            axes[-1].update(ycal)
            array_shape.append(axes[-1]["size"])
            axes.append(
                {
                    "name": "x",
                    "units": "meters",
                    "index_in_array": 1,
                }
            )
            axes[-1].update(xcal)
            array_shape.append(axes[-1]["size"])
        elif xcal[b"size"] == ycal[b"size"] == array_size:
            # Oblique line scan
            scale = np.sqrt(xcal["scale"] ** 2 + ycal["scale"] ** 2)
            axes.append(
                {
                    "name": "x",
                    "units": "meters",
                    "index_in_array": 0,
                    "offset": 0,
                    "scale": scale,
                    "size": xcal["size"],
                }
            )
            array_shape.append(axes[-1]["size"])
        else:
            raise IOError
    else:
        array_shape = [header["ValidNumberElements"]]
        axes.append(
            {
                "name": "Unknown dimension",
                "offset": 0,
                "scale": 1,
                "units": "",
                "size": header["ValidNumberElements"],
                "index_in_array": 0,
            }
        )
    return array_shape, axes


def convert_xml_to_dict(xml_object):
    op = DTBox(box_dots=True)
    emixml2dtb(ET.fromstring(xml_object), op)
    return op


def ser_reader(filename, objects=None, lazy=False, only_valid_data=True):
    """
    Reads the information from the file and returns it in the HyperSpy
    required format.
    """
    header, data = load_ser_file(filename)
    record_by = guess_record_by(header["DataTypeID"])
    ndim = int(header["NumberDimensions"][0])
    date, time = None, None
    if objects is not None:
        objects_dict = convert_xml_to_dict(objects[0])
        try:
            acq_date = objects_dict.ObjectInfo.AcquireDate
            date, time = _get_date_time(acq_date)
        except AttributeError:
            _logger.warning(
                f"AcquireDate not found in metadata of {filename};"
                " Not setting metadata date or time"
            )
    if (
        "PositionY" in data.dtype.names
        and len(data["PositionY"]) > 1
        and (data["PositionY"][0] == data["PositionY"][1])
    ):
        # The spatial dimensions are stored in F order i.e. X, Y, ...
        order = "F"
    else:
        # The spatial dimensions are stored in C order i.e. ..., Y, X
        order = "C"
    if ndim == 0 and header["ValidNumberElements"] != 0:
        # The calibration of the axes are not stored in the header.
        # We try to guess from the position coordinates.
        array_shape, axes = get_axes_from_position(header=header, data=data)
    else:
        axes = []
        array_shape = [
            None,
        ] * int(ndim)
        spatial_axes = ["x", "y"][:ndim]
        for i in range(ndim):
            idim = 1 + i if order == "C" else ndim - i
            if (
                record_by == "spectrum"
                or header["Dim-%i_DimensionSize" % (i + 1)][0] != 1
            ):
                units = (
                    header["Dim-%i_Units" % (idim)][0].decode("utf-8")
                    if header["Dim-%i_UnitsLength" % (idim)] > 0
                    else None
                )
                if units == "meters":
                    name = spatial_axes.pop() if order == "F" else spatial_axes.pop(-1)
                else:
                    name = None
                axes.append(
                    {
                        "offset": header["Dim-%i_CalibrationOffset" % idim][0],
                        "scale": header["Dim-%i_CalibrationDelta" % idim][0],
                        "units": units,
                        "size": header["Dim-%i_DimensionSize" % idim][0],
                        "name": name,
                        "navigate": True,
                    }
                )
                array_shape[i] = header["Dim-%i_DimensionSize" % idim][0]

        # Deal with issue when TotalNumberElements does not equal
        # ValidNumberElements for ndim==1.
        if (
            ndim == 1
            and (header["TotalNumberElements"] != header["ValidNumberElements"][0])
            and only_valid_data
        ):
            if header["ValidNumberElements"][0] == 1:
                # no need for navigation dimension
                array_shape = []
                axes = []
            else:
                array_shape[0] = header["ValidNumberElements"][0]
                axes[0]["size"] = header["ValidNumberElements"][0]

    # Spectral dimension
    if record_by == "spectrum":
        axes.append(
            {
                "offset": data["CalibrationOffset"][0],
                "scale": data["CalibrationDelta"][0],
                "size": data["ArrayLength"][0],
                "index_in_array": header["NumberDimensions"][0],
                "navigate": False,
            }
        )

        # FEI seems to use the international system of units (SI) for the
        # energy scale (eV).
        axes[-1]["units"] = "eV"
        axes[-1]["name"] = "Energy"

        array_shape.append(data["ArrayLength"][0])

    elif record_by == "image":
        if objects is not None:
            units = _guess_units_from_mode(objects_dict, header)
        else:
            units = "meters"
        # Y axis
        axes.append(
            {
                "name": "y",
                "offset": data["CalibrationOffsetY"][0]
                - data["CalibrationElementY"][0] * data["CalibrationDeltaY"][0],
                "scale": data["CalibrationDeltaY"][0],
                "units": units,
                "size": data["ArraySizeY"][0],
                "navigate": False,
            }
        )
        array_shape.append(data["ArraySizeY"][0])
        # X axis
        axes.append(
            {
                "name": "x",
                "offset": data["CalibrationOffsetX"][0]
                - data["CalibrationElementX"][0] * data["CalibrationDeltaX"][0],
                "scale": data["CalibrationDeltaX"][0],
                "size": data["ArraySizeX"][0],
                "units": units,
                "navigate": False,
            }
        )
        array_shape.append(data["ArraySizeX"][0])

    # FEI seems to use the international system of units (SI) for the
    # spatial scale. However, we prefer to work in nm
    for axis in axes:
        if axis["units"] == "meters":
            axis["units"] = "nm"
            axis["scale"] *= 10**9
        elif axis["units"] == "1/meters":
            axis["units"] = "1 / nm"
            axis["scale"] /= 10**9

    # Remove Nones from array_shape caused by squeezing size 1 dimensions
    array_shape = [dim for dim in array_shape if dim is not None]
    if lazy:
        from dask import delayed
        from dask.array import from_delayed

        val = delayed(load_only_data, pure=True)(
            filename, array_shape, record_by, len(axes), only_valid_data=only_valid_data
        )
        dc = from_delayed(val, shape=array_shape, dtype=data["Array"].dtype)
    else:
        dc = load_only_data(
            filename,
            array_shape,
            record_by,
            len(axes),
            data=data,
            header=header,
            only_valid_data=only_valid_data,
        )

    original_metadata = OrderedDict()
    header_parameters = sarray2dict(header)
    sarray2dict(data, header_parameters)
    # We remove the Array key to save memory avoiding duplication
    del header_parameters["Array"]
    original_metadata["ser_header_parameters"] = header_parameters
    metadata = {
        "General": {
            "original_filename": os.path.split(filename)[1],
        },
        "Signal": {
            "signal_type": "",
        },
    }
    if date is not None and time is not None:
        metadata["General"]["date"] = date
        metadata["General"]["time"] = time
    dictionary = {
        "data": dc,
        "metadata": metadata,
        "axes": axes,
        "original_metadata": original_metadata,
        "mapping": mapping,
    }
    return [
        dictionary,
    ]


def load_only_data(
    filename,
    array_shape,
    record_by,
    num_axes,
    data=None,
    header=None,
    only_valid_data=True,
):
    if data is None:
        header, data = load_ser_file(filename)
    # If the acquisition stops before finishing the job, the stored file will
    # report the requested size even though no values are recorded. Therefore
    # if the shapes of the retrieved array does not match that of the data
    # dimensions we must fill the rest with zeros or (better) nans if the
    # dtype is float
    if np.prod(array_shape) != np.prod(data["Array"].shape):
        if int(header["NumberDimensions"][0]) == 1 and only_valid_data:
            # No need to fill with zeros if `TotalNumberElements !=
            # ValidNumberElements` for series data.
            # The valid data is always `0:ValidNumberElements`
            dc = data["Array"][0 : header["ValidNumberElements"][0], ...]
            array_shape[0] = header["ValidNumberElements"][0]
        else:
            # Maps will need to be filled with zeros or nans
            dc = np.zeros(np.prod(array_shape), dtype=data["Array"].dtype)
            if dc.dtype is np.dtype("f") or dc.dtype is np.dtype("f8"):
                dc[:] = np.nan
            dc[: data["Array"].ravel().shape[0]] = data["Array"].ravel()
    else:
        dc = data["Array"]

    dc = dc.reshape(array_shape)
    if record_by == "image":
        dc = dc[..., ::-1, :]
    if num_axes != len(dc.shape):
        dc = dc.squeeze()
    if num_axes != len(dc.shape):
        raise IOError("Please report this issue to the HyperSpy developers.")
    return dc


def _guess_units_from_mode(objects_dict, header):
    # in case the xml file doesn't contain the "Mode" or the header doesn't
    # contain 'Dim-1_UnitsLength', return "meters" as default, which will be
    # OK most of the time
    warn_str = (
        "The navigation axes units could not be determined. "
        "Setting them to `nm`, but this may be wrong."
    )
    try:
        mode = objects_dict.ObjectInfo.ExperimentalDescription.Mode
        isCamera = "CameraNamePath" in objects_dict.ObjectInfo.AcquireInfo.keys()
    except AttributeError:  # in case the xml chunk doesn't contain the Mode
        warnings.warn(warn_str)
        return "meters"  # Most of the time, the unit will be meters!
    if "Dim-1_UnitsLength" in header.dtype.fields:
        # assuming that for an image stack, the UnitsLength of the "3rd"
        # dimension is 0
        isImageStack = header["Dim-1_UnitsLength"][0] == 0
        # Workaround: if this is not an image stack and not a STEM image, then
        # we assume that it should be a diffraction
        isDiffractionScan = header["Dim-1_DimensionSize"][0] > 1 and not isImageStack
    else:
        warnings.warn(warn_str)
        return "meters"  # Most of the time, the unit will be meters!

    _logger.info(objects_dict.ObjectInfo.AcquireInfo)
    _logger.info("mode: %s", mode)
    _logger.info("isCamera: %s", isCamera)
    _logger.info("isImageStack: %s", isImageStack)
    _logger.info("isImageStack: %s", isDiffractionScan)
    if "STEM" in mode:
        # data recorded in STEM with a camera, so we assume, it's a diffraction
        # in case we can't make use the detector is a camera, use a workaround
        if isCamera or isDiffractionScan:
            return "1/meters"
        else:
            return "meters"
    elif "Diffraction" in mode:
        return "1/meters"
    else:
        return "meters"


def _get_simplified_mode(mode):
    if "STEM" in mode:
        return "STEM"
    else:
        return "TEM"


def _get_date_time(value):
    dt = parser.parse(value)
    return dt.date().isoformat(), dt.time().isoformat()


def _get_microscope_name(value):
    return value.replace("Microscope ", "")


mapping = {
    "ObjectInfo.ExperimentalDescription.High_tension_kV": (
        "Acquisition_instrument.TEM.beam_energy",
        None,
    ),
    "ObjectInfo.ExperimentalDescription.Microscope": (
        "Acquisition_instrument.TEM.microscope",
        _get_microscope_name,
    ),
    "ObjectInfo.ExperimentalDescription.Mode": (
        "Acquisition_instrument.TEM.acquisition_mode",
        _get_simplified_mode,
    ),
    "ObjectInfo.ExperimentalDescription.Camera length_m": (
        "Acquisition_instrument.TEM.camera_length",
        lambda x: x * 1e3,
    ),
    "ObjectInfo.ExperimentalDescription.Magnification_x": (
        "Acquisition_instrument.TEM.magnification",
        None,
    ),
    "ObjectInfo.AcquireInfo.CameraNamePath": (
        "Acquisition_instrument.TEM.Detector.Camera.Name",
        None,
    ),
    "ObjectInfo.AcquireInfo.DwellTimePath": (
        "Acquisition_instrument.TEM.Detector.Camera.exposure",
        None,
    ),
    "ObjectInfo.ExperimentalDescription.Stage_A_deg": (
        "Acquisition_instrument.TEM.Stage.tilt_alpha",
        None,
    ),
    "ObjectInfo.ExperimentalDescription.Stage_B_deg": (
        "Acquisition_instrument.TEM.Stage.tilt_beta",
        None,
    ),
    "ObjectInfo.ExperimentalDescription.Stage_X_um": (
        "Acquisition_instrument.TEM.Stage.x",
        lambda x: x * 1e-3,
    ),
    "ObjectInfo.ExperimentalDescription.Stage_Y_um": (
        "Acquisition_instrument.TEM.Stage.y",
        lambda x: x * 1e-3,
    ),
    "ObjectInfo.ExperimentalDescription.Stage_Z_um": (
        "Acquisition_instrument.TEM.Stage.z",
        lambda x: x * 1e-3,
    ),
    "ObjectInfo.ExperimentalDescription.User": ("General.authors", None),
}