# -*- coding: utf-8 -*-
#
# Copyright 2016 Petras Jokubauskas
# Copyright 2016 The HyperSpy developers
#
# This library 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.
#
# This library 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 any project and source this library is coupled.
# If not, see <https://www.gnu.org/licenses/#GPL>.
#
# This python library subset provides read functionality of
#  Bruker bcf files.
# The basic reading capabilities of proprietary AidAim Software(tm)
#  SFS (Single File System) (used in bcf technology) is present in
#  the same library.

import codecs
import io
import logging
import xml.etree.ElementTree as ET
from ast import literal_eval
from datetime import datetime
from math import ceil
from os.path import basename, splitext
from struct import unpack as strct_unp
from zlib import decompress as unzip_block

import dask
import numpy as np

from rsciio._docstrings import FILENAME_DOC, LAZY_DOC, RETURNS_DOC
from rsciio.utils._date_time import msfiletime_to_unix
from rsciio.utils.xml import XmlToDict, sanitize_msxml_float

_logger = logging.getLogger(__name__)

warn_once = True

try:
    from rsciio.bruker import unbcf_fast

    fast_unbcf = True
    _logger.info("The fast cython based bcf unpacking library were found")
except ImportError:  # pragma: no cover
    fast_unbcf = False
    _logger.info(
        """unbcf_fast library is not present...
Falling back to slow python only backend."""
    )

# create dictionizer customized to Bruker Xml streams:
x2d = XmlToDict(dub_attr_pre_str="XmlClass", tags_to_flatten="ClassInstance")


class Container:
    pass


class SFSTreeItem:
    """Class to manage one internal sfs file.

    Reading, reading in chunks, reading and extracting, reading without
    extracting even if compression is present.

    Attributes:
    item_raw_string -- the bytes from sfs file table describing the file
    parent -- the item higher hierarchicaly in the sfs file tree

    Methods:
    read_piece, setup_compression_metadata, get_iter_and_properties,
    get_as_BytesIO_string
    """

    def __init__(self, item_raw_string, parent):
        self.sfs = parent
        (
            self._pointer_to_pointer_table,
            self.size,
            create_time,
            mod_time,
            some_time,
            self.permissions,
            self.parent,
            _,
            self.is_dir,
            _,
            name,
            _,
        ) = strct_unp("<iQQQQIi176s?3s256s32s", item_raw_string)
        self.create_time = msfiletime_to_unix(create_time)
        self.mod_time = msfiletime_to_unix(mod_time)
        self.some_time = msfiletime_to_unix(some_time)
        self.name = name.strip(b"\x00").decode("utf-8")
        self.size_in_chunks = self._calc_pointer_table_size()
        if self.is_dir == 0:
            self._fill_pointer_table()

    def _calc_pointer_table_size(self):
        n_chunks = ceil(self.size / self.sfs.usable_chunk)
        return n_chunks

    def _fill_pointer_table(self):
        """Parse the sfs and populate self.pointers table.

        self.pointer is the sfs pointer table containing addresses of
        every chunk of the file.

        The pointer table if the file is big can extend through many
        sfs chunks. Differently than files, the pointer table of file have no
        table of pointers to the chunks. Instead if pointer table is larger
        than sfs chunk, the chunk header contains next chunk number (address
        can be calculated using known chunk size and global offset) with
        continuation of file pointer table, thus it have to be read and filled
        consecutive.
        """
        # table size in number of chunks:
        n_of_chunks = ceil(self.size_in_chunks / (self.sfs.usable_chunk // 4))
        with open(self.sfs.filename, "rb") as fn:
            if n_of_chunks > 1:
                next_chunk = self._pointer_to_pointer_table
                temp_string = io.BytesIO()
                for dummy1 in range(n_of_chunks):
                    fn.seek(self.sfs.chunksize * next_chunk + 0x118)
                    next_chunk = strct_unp("<I", fn.read(4))[0]
                    fn.seek(28, 1)
                    temp_string.write(fn.read(self.sfs.usable_chunk))
                temp_string.seek(0)
                temp_table = temp_string.read()
                temp_string.close()
            else:
                fn.seek(self.sfs.chunksize * self._pointer_to_pointer_table + 0x138)
                temp_table = fn.read(self.sfs.usable_chunk)
            self.pointers = (
                np.frombuffer(
                    temp_table[: self.size_in_chunks * 4], dtype="uint32"
                ).astype(np.int64)
                * self.sfs.chunksize
                + 0x138
            )

    def read_piece(self, offset, length):
        """Read and returns raw byte string of the file without applying
        any decompression.

        Arguments:
        offset: seek value
        length: length of the data counting from the offset

        Returns:
        io.ByteIO object
        """
        data = io.BytesIO()
        # first block index:
        fb_idx = offset // self.sfs.usable_chunk
        # first block offset:
        fbo = offset % self.sfs.usable_chunk
        # last block index:
        lb_idx = (offset + length) // self.sfs.usable_chunk
        # last block cut off:
        lbco = (offset + length) % self.sfs.usable_chunk
        with open(self.sfs.filename, "rb") as fn:
            if fb_idx != lb_idx:
                fn.seek(self.pointers[fb_idx] + fbo)
                data.write(fn.read(self.sfs.usable_chunk - fbo))
                for i in self.pointers[fb_idx + 1 : lb_idx]:
                    fn.seek(i)
                    data.write(fn.read(self.sfs.usable_chunk))
                if lbco > 0:
                    fn.seek(self.pointers[lb_idx])
                    data.write(fn.read(lbco))
            else:
                fn.seek(self.pointers[fb_idx] + fbo)
                data.write(fn.read(length))
        data.seek(0)
        return data.read()

    def _iter_read_chunks(self, first=0):
        """Generate and return iterator for reading and returning
        sfs internal file in chunks.

        By default it creates iterator for whole file, however
        with kwargs 'first' and 'chunks' the range of chunks
        for iterator can be set.

        Keyword arguments:
        first -- the index of first chunk from which to read. (default 0)
        chunks -- the number of chunks to read. (default False)
        """
        last = self.size_in_chunks
        with open(self.sfs.filename, "rb") as fn:
            for idx in range(first, last - 1):
                fn.seek(self.pointers[idx])
                yield fn.read(self.sfs.usable_chunk)
            fn.seek(self.pointers[last - 1])
            last_stuff = self.size % self.sfs.usable_chunk
            if last_stuff != 0:
                yield fn.read(last_stuff)
            else:
                yield fn.read(self.sfs.usable_chunk)

    def setup_compression_metadata(self):
        """parse and setup the number of compression chunks

        and uncompressed chunk size as class attributes.

        Sets up attributes:
        self.uncompressed_blk_size, self.no_of_compr_blk

        """
        with open(self.sfs.filename, "rb") as fn:
            fn.seek(self.pointers[0])
            # AACS signature, uncompressed size, undef var, number of blocks
            aacs, uc_size, _, n_of_blocks = strct_unp("<IIII", fn.read(16))
        if aacs == 0x53434141:  # AACS as string
            self.uncompressed_blk_size = uc_size
            self.no_of_compr_blk = n_of_blocks
        else:
            raise ValueError(
                """The file is marked to be compressed,
but compression signature is missing in the header. Aborting...."""
            )

    def _iter_read_compr_chunks(self):
        """Generate and return reader and decompressor iterator
        for compressed with zlib compression sfs internal file.

        Returns:
        iterator of decompressed data chunks.
        """

        offset = 0x80  # the 1st compression block header
        for dummy1 in range(self.no_of_compr_blk):
            cpr_size = strct_unp("<I12x", self.read_piece(offset, 16))[0]
            # cpr_size, dum_size, dum_unkn, dum_size2 = strct_unp('<IIII',...
            # dum_unkn is probably some kind of checksum but
            # none of known (crc16, crc32, adler32) algorithm could match.
            # dum_size2 == cpr_size + 0x10 which have no use...
            # dum_size, which is decompressed size, also have no use...
            # as it is the same in file compression_header
            offset += 16
            raw_string = self.read_piece(offset, cpr_size)
            offset += cpr_size
            yield unzip_block(raw_string)

    def get_iter_and_properties(self):
        """Generate and return the iterator of data chunks and
        properties of such chunks such as size and count.

        Method detects if data is compressed and uses iterator with
        decompression involved, else uses simple iterator of chunks.

        Returns:
            (iterator, chunk_size, number_of_chunks)
        """
        if self.sfs.compression == "None":
            return self._iter_read_chunks(), self.sfs.usable_chunk, self.size_in_chunks
        elif self.sfs.compression == "zlib":
            return (
                self._iter_read_compr_chunks(),
                self.uncompressed_blk_size,
                self.no_of_compr_blk,
            )
        else:
            raise RuntimeError(
                "file",
                str(self.sfs.filename),
                " is compressed by not known and not",
                "implemented algorithm.\n Aborting...",
            )

    def get_as_BytesIO_string(self):
        """Get the whole file as io.BytesIO object (in memory!)."""
        data = io.BytesIO()
        data.write(b"".join(self.get_iter_and_properties()[0]))
        return data


class SFS_reader:
    """Class to read sfs file.
    SFS is AidAim software's(tm) single file system.
    The class provides basic reading capabilities of such container.
    It is capable to read compressed data in zlib, but
    SFS can contain other compression which is not implemented here.
    It is also not able to read encrypted sfs containers.

    This class can be used stand alone or inherited in construction of
    file readers using sfs technology.

    Attributes
    ----------
    filename

    """

    def __init__(self, filename):
        self.filename = filename
        # read the file header
        with open(filename, "rb") as fn:
            a = fn.read(8)
            if a != b"AAMVHFSS":
                raise TypeError("file '{0}' is not SFS container".format(filename))
            fn.seek(0x124)  # this looks to be version, as float value is always
            # nicely rounded and at older bcf versions (<1.9) it was 2.40,
            # at new (v2) - 2.60
            version, self.chunksize = strct_unp("<fI", fn.read(8))
            self.sfs_version = "{0:4.2f}".format(version)
            self.usable_chunk = self.chunksize - 32
            fn.seek(0x140)
            # the sfs tree and number of the items / files + directories in it,
            # and the number in chunks of whole sfs:
            self.tree_address, self.n_tree_items, self.sfs_n_of_chunks = strct_unp(
                "<III", fn.read(12)
            )
        self._setup_vfs()

    def _setup_vfs(self):
        """Setup the virtual file system tree represented as python dictionary
        with values populated with SFSTreeItem instances

        See also
        --------
        SFSTreeItem
        """
        with open(self.filename, "rb") as fn:
            # check if file tree do not exceed one chunk:
            n_file_tree_chunks = ceil(
                (self.n_tree_items * 0x200) / (self.chunksize - 0x20)
            )
            if n_file_tree_chunks == 1:
                # file tree do not exceed one chunk in bcf:
                fn.seek(self.chunksize * self.tree_address + 0x138)
                raw_tree = fn.read(0x200 * self.n_tree_items)
            else:
                temp_str = io.BytesIO()
                tree_address = self.tree_address
                tree_items_in_chunk = (self.chunksize - 0x20) // 0x200
                for i in range(n_file_tree_chunks):
                    # jump to tree/list address:
                    fn.seek(self.chunksize * tree_address + 0x118)
                    # next tree/list address:
                    tree_address = strct_unp("<I", fn.read(4))[0]
                    fn.seek(28, 1)
                    temp_str.write(fn.read(tree_items_in_chunk * 0x200))
                temp_str.seek(0)
                raw_tree = temp_str.read(self.n_tree_items * 0x200)
                temp_str.close()
            temp_item_list = [
                SFSTreeItem(raw_tree[i * 0x200 : (i + 1) * 0x200], self)
                for i in range(self.n_tree_items)
            ]
            # temp list with parents of items
            paths = [[h.parent] for h in temp_item_list]
        # checking the compression header which can be different per file:
        self._check_the_compresion(temp_item_list)
        if self.compression == "zlib":
            for c in temp_item_list:
                if not c.is_dir:
                    c.setup_compression_metadata()
        # convert the items to virtual file system tree
        dict_tree = self._flat_items_to_dict(paths, temp_item_list)
        # and finally set the Virtual file system:
        self.vfs = dict_tree["root"]

    def _flat_items_to_dict(self, paths, temp_item_list):
        """place items from flat list into dictionary tree
        of virtual file system
        """
        while not all(g[-1] == -1 for g in paths):
            for f in range(len(paths)):
                if paths[f][-1] != -1:
                    paths[f].extend(paths[paths[f][-1]])
        names = [j.name for j in temp_item_list]
        names.append("root")  # temp root item in dictionary
        for p in paths:
            for r in range(len(p)):
                p[r] = names[p[r]]
        for p in paths:
            p.reverse()
        root = {}
        for i in range(len(temp_item_list)):
            dir_pointer = root
            for j in paths[i]:
                if j in dir_pointer:
                    dir_pointer = dir_pointer[j]
                else:
                    dir_pointer[j] = {}
                    dir_pointer = dir_pointer[j]
            if temp_item_list[i].is_dir:
                dir_pointer[temp_item_list[i].name] = {}
            else:
                dir_pointer[temp_item_list[i].name] = temp_item_list[i]
        # return dict tree:
        return root

    def _check_the_compresion(self, temp_item_list):
        """parse, check and setup the self.compression"""
        with open(self.filename, "rb") as fn:
            # Find if there is compression:
            for c in temp_item_list:
                if not c.is_dir:
                    fn.seek(c.pointers[0])
                    if fn.read(4) == b"\x41\x41\x43\x53":  # string AACS
                        self.compression = "zlib"
                    else:
                        self.compression = "None"
                    # compression is global, can't be different per file in sfs
                    break

    def get_file(self, path):
        """Return the SFSTreeItem (aka internal file) object from
        sfs container.

        Parameters
        ----------
        path : str
            Internal file path in sfs file tree. Path accepts only
            standard - forward slash for directories.

        Returns
        -------
        object : SFSTreeItem
            SFSTreeItem, which can be read into byte stream, in chunks or
            whole using objects methods.

        Examples
        --------
        to get "file" object 'kitten.png' in folder 'catz' which
        resides in root directory of sfs, you would use:

        >>> instance_of_SFSReader.get_file('catz/kitten.png')

        See Also
        --------
        SFSTreeItem
        """
        item = self.vfs

        for i in path.split("/"):
            item = item[i]
        return item


class EDXSpectrum:
    def __init__(self, spectrum):
        """
        Wrap the objectified bruker EDS spectrum xml part
        to the python object, leaving all the xml and bruker clutter behind.

        Parameters
        ----------
        spectrum : etree xml object
            etree xml object, where spectrum.attrib['Type'] should
            be 'TRTSpectrum'.
        """
        TRTHeader = spectrum.find("./TRTHeaderedClass")
        hardware_header = TRTHeader.find(
            "./ClassInstance[@Type='TRTSpectrumHardwareHeader']"
        )
        detector_header = TRTHeader.find("./ClassInstance[@Type='TRTDetectorHeader']")
        esma_header = TRTHeader.find("./ClassInstance[@Type='TRTESMAHeader']")
        # what TRT means?
        # ESMA could stand for Electron Scanning Microscope Analysis
        spectrum_header = spectrum.find("./ClassInstance[@Type='TRTSpectrumHeader']")
        xrf_header = TRTHeader.find("./ClassInstance[@Type='TRTXrfHeader']")

        # map stuff from hardware xml branch:
        self.hardware_metadata = x2d.dictionarize(hardware_header)
        self.amplification = self.hardware_metadata["Amplification"]  # USED

        # map stuff from detector xml branch
        self.detector_metadata = x2d.dictionarize(detector_header)
        self.detector_type = self.detector_metadata["Type"]  # USED

        # decode silly hidden detector layer info:
        det_l_str = self.detector_metadata["DetLayers"]
        dec_det_l_str = codecs.decode(det_l_str.encode("ascii"), "base64")
        mini_xml = ET.fromstring(unzip_block(dec_det_l_str))
        self.detector_metadata["DetLayers"] = {}  # Overwrite with dict
        for i in list(mini_xml):
            self.detector_metadata["DetLayers"][i.tag] = dict(i.attrib)

        # map stuff from esma xml branch:
        if esma_header is not None:
            self.esma_metadata = x2d.dictionarize(esma_header)
        if xrf_header is not None:
            xrf_header_dict = x2d.dictionarize(xrf_header)
            self.esma_metadata = {
                "PrimaryEnergy": xrf_header_dict["Voltage"],
                "ElevationAngle": xrf_header_dict["ExcitationAngle"],
            }
        # USED:
        self.hv = self.esma_metadata.get("PrimaryEnergy", None)
        if self.hv is None:  # pragma: no cover
            # User has reported missing PrimaryEnergy in metadata in Quantax70
            # however, this wasn't possible to reproduce in smaller test files
            # could it be an acquisition setup issue?
            _logger.warning(
                "The beam energy could not be found in the file's metadata."
            )
        self.elev_angle = self.esma_metadata["ElevationAngle"]
        date_time = gen_iso_date_time(spectrum_header)
        if date_time is not None:
            self.date, self.time = date_time

        self.spectrum_metadata = x2d.dictionarize(spectrum_header)
        self.offset = self.spectrum_metadata["CalibAbs"]
        self.scale = self.spectrum_metadata["CalibLin"]

        # main data:
        self.data = np.fromstring(spectrum.find("./Channels").text, dtype="Q", sep=",")

    def last_non_zero_channel(self):
        """return index of last nonzero channel"""
        return self.data.nonzero()[0][-1]

    def energy_to_channel(self, energy, kV=True):
        """convert energy to channel index,
        optional kwarg 'kV' (default: True) should be set to False
        if given energy units is in V"""
        if not kV:
            en_temp = energy / 1000.0
        else:
            en_temp = energy
        return int(round((en_temp - self.offset) / self.scale))


class HyperHeader:
    """Wrap Bruker HyperMaping xml header into python object.

    Arguments:
    xml_str -- the uncompressed to be provided with extracted Header xml
    from bcf.
    indexes -- list of indexes of available datasets

    Methods:
    estimate_map_channels, estimate_map_depth

    If Bcf is version 2, the bcf can contain stacks
    of hypermaps - thus header part  can contain multiply sum eds spectras
    and it's metadata per hypermap slice which can be selected using index.
    Bcf can record number of images from different single dimensional value
    detectors (BSE, SEI, ARGUS, etc...). images representing signals are
    internally ordered and right signal image can be accessed using image
    index (do not confuse with dataset index).
    """

    def __init__(self, xml_str, indexes, instrument=None):
        root = ET.fromstring(xml_str)
        root = root.find("./ClassInstance[@Type='TRTSpectrumDatabase']")
        try:
            self.name = str(root.attrib["Name"])
        except KeyError:
            self.name = "Undefined"
            _logger.info("hypermap have no name. Giving it 'Undefined' name")
        hd = root.find("./Header")
        self.date, self.time = gen_iso_date_time(hd)
        self.version = int(literal_eval(hd.find("./FileVersion").text))
        # fill the sem and stage attributes:
        self._set_microscope(root)
        self._set_mode(instrument)
        self._set_images(root)
        self.elements = {}
        self._set_elements(root)
        self.line_counter = literal_eval(root.find("./LineCounter").text)
        self.channel_count = int(root.find("./ChCount").text)
        self.mapping_count = int(root.find("./DetectorCount").text)
        # self.channel_factors = {}
        self.spectra_data = {}
        self._set_sum_edx(root, indexes)

    def _set_microscope(self, root):
        """set microscope metadata from objectified xml part (TRTSEMData,
        TRTSEMStageData, TRTDSPConfiguration).

        BCF can contain basic parameters of SEM column, and optionally
        the stage. This metadata can be not fully or at all available to
        Esprit and saved into bcf file as it depends from license and
        the link and implementation state between the microscope's
        software and Bruker system.
        """

        semData = root.find("./ClassInstance[@Type='TRTSEMData']")
        self.sem_metadata = x2d.dictionarize(semData)
        # parse values for use in hspy metadata:
        self.hv = self.sem_metadata.get("HV", None)  # in kV
        # image/hypermap resolution in um/pixel:
        if "DX" in self.sem_metadata:
            self.units = "µm"
        else:
            self.units = "pix"
        self.x_res = self.sem_metadata.get("DX", 1.0)
        self.y_res = self.sem_metadata.get("DY", 1.0)
        # stage position:
        semStageData = root.find("./ClassInstance[@Type='TRTSEMStageData']")
        self.stage_metadata = x2d.dictionarize(semStageData)
        # DSP configuration (always present, part of Bruker system):
        DSPConf = root.find("./ClassInstance[@Type='TRTDSPConfiguration']")
        self.dsp_metadata = x2d.dictionarize(DSPConf)

    def _set_mode(self, instrument=None):
        if instrument is not None:
            self.mode = instrument
        else:
            self.mode = guess_mode(self.hv)

    def get_acq_instrument_dict(self, detector=False, **kwargs):
        """return python dictionary with acquisition instrument
        mandatory data
        """
        acq_inst = {}
        if self.hv is not None:
            acq_inst["beam_energy"] = self.hv
        if "Mag" in self.sem_metadata:
            acq_inst["magnification"] = self.sem_metadata["Mag"]
        if detector:
            eds_metadata = self.get_spectra_metadata(**kwargs)
            det = gen_detector_node(eds_metadata)
            det["EDS"]["real_time"] = self.calc_real_time()
            acq_inst["Detector"] = det
            # In case of XRF, the primary energy is only defined in
            # the spectrum metadata
            if eds_metadata.hv is not None:
                acq_inst["beam_energy"] = eds_metadata.hv

        return acq_inst

    def _parse_image(self, xml_node, overview=False):
        """parse image from bruker xml image node."""
        if overview:
            overlay_node = xml_node.find(
                "./ChildClassInstances"
                "/ClassInstance["
                # "@Type='TRTRectangleOverlayElement' and "
                "@Name='Map']/TRTSolidOverlayElement/"
                "TRTBasicLineOverlayElement/TRTOverlayElement"
            )
            if overlay_node is not None:
                # Currently not tested
                # waiting on small test files to be provided by users
                # See https://github.com/hyperspy/rosettasciio/pull/383
                overlay_dict = x2d.dictionarize(overlay_node)
                over_rect = overlay_dict["TRTOverlayElement"]["Rect"]
                x1, x2 = over_rect["Left"] * self.x_res, over_rect["Right"] * self.x_res
                y1, y2 = over_rect["Top"] * self.y_res, over_rect["Bottom"] * self.y_res
                width, height = x2 - x1, y2 - y1
                md_over_dict = {
                    "class": "Rectangles",
                    "offsets": ([x1 + width / 2, y1 + height / 2],),
                    "widths": (width,),
                    "heights": (height,),
                    "color": "yellow",
                    "linewidth": 2,
                    "facecolor": "none",
                }
        image = Container()
        image.width = int(xml_node.find("./Width").text)  # in pixels
        image.height = int(xml_node.find("./Height").text)  # in pixels
        # in bytes ('u1','u2','u4')
        image.dtype = "u" + xml_node.find("./ItemSize").text
        image.plane_count = int(xml_node.find("./PlaneCount").text)
        image.images = []
        for i in range(image.plane_count):
            img = xml_node.find("./Plane" + str(i))
            raw = codecs.decode((img.find("./Data").text).encode("ascii"), "base64")
            array1 = np.frombuffer(raw, dtype=image.dtype)
            if any(array1):
                item = self.gen_hspy_item_dict_basic()
                data = array1.reshape((image.height, image.width))
                desc = img.find("./Description")
                item["data"] = data
                item["axes"][0]["size"] = image.height
                item["axes"][1]["size"] = image.width
                item["metadata"]["Signal"] = {}
                item["metadata"]["General"] = {}
                if desc is not None:
                    item["metadata"]["General"]["title"] = str(desc.text)
                if overview and (overlay_node is not None):
                    item["metadata"]["Markers"] = {"overview": md_over_dict}
                image.images.append(item)
        return image

    def _set_images(self, root):
        """Wrap objectified xml part with image to class attributes
        for self.image.
        """
        image_nodes = root.findall("./ClassInstance[@Type='TRTImageData']")
        for n in image_nodes:
            if not (n.get("Name")):
                image_node = n
        self.image = self._parse_image(image_node)
        if self.version == 2:
            overview_node = root.findall(
                "./ClassInstance[@Type='TRTContainerClass']"
                "/ChildClassInstances"
                "/ClassInstance["
                # "@Type='TRTContainerClass' and "
                "@Name='OverviewImages']"
                "/ChildClassInstances"
                "/ClassInstance[@Type='TRTImageData']"
            )
            if len(overview_node) > 0:  # in case there is no image
                self.overview = self._parse_image(overview_node[0], overview=True)

    def _set_elements(self, root):
        """wrap objectified xml part with selection of elements to
        self.elements list
        """
        try:
            elements = root.find(
                "./ClassInstance[@Type='TRTContainerClass']"
                "/ChildClassInstances"
                "/ClassInstance[@Type='TRTElementInformationList']"
                "/ClassInstance[@Type='TRTSpectrumRegionList']"
                "/ChildClassInstances"
            )
            for j in elements.findall("./ClassInstance[@Type='TRTSpectrumRegion']"):
                tmp_d = x2d.dictionarize(j)
                # In case no information on the specific selected X-ray line is
                # available, assume it is a 'Ka' line, reflecting the fact that element
                # tables in Bruker Esprit (1.9 and 2.1) store a single K line for Li-Al
                self.elements[tmp_d["XmlClassName"]] = {
                    "line": tmp_d.get("Line", "Ka"),
                    "energy": tmp_d.get("Energy"),
                }
        except AttributeError:
            _logger.info("no element selection present in the spectra..")

    def _set_sum_edx(self, root, indexes):
        for i in indexes:
            spec_node = root.find("./SpectrumData{0}/ClassInstance".format(str(i)))
            self.spectra_data[i] = EDXSpectrum(spec_node)

    def get_consistent_min_channels(self, index=0):
        """Estimate consistent minimal size of energy axis by comparing energy
        at last recorded channel vs electron beam potential and return channel
        number corresponding to least energy. This method is safe to use with
        sliced datasets (consistent between slices) which were acquired using
        the same electron potential.

        Parameters
        ----------
        index : int
            Index of the map if multiple hypermaps are present in the same bcf.

        Returns
        -------
        optimal channel number
        """
        eds_max_energy = self.spectra_data[index].amplification / 1000  # in kV
        if self.hv and self.hv > 0 and self.hv < eds_max_energy:
            return self.spectra_data[index].energy_to_channel(self.hv)
        if self.hv is None:
            logging.warn(
                "bcf header contains no node for electron beam "
                "voltage or such node is absent.\n"
                "Using full range of recorded channels."
            )
        return self.spectra_data[index].data.shape[0]

    def estimate_map_depth(self, index=0, downsample=1, for_numpy=False):
        """Estimate minimal dtype of array using cumulative spectra
        of the all pixels so that no data would be truncated.

        The method estimates the value from sum eds spectra, dividing
        the maximum  energy pulse value from raster x and y and to be on the
        safe side multiplying by 2.

        Parameters
        ----------
        index : int
            Index of the hypermap if multiply hypermaps are present in the
            same bcf. (default 0)
        downsample : int
            Downsample factor. (default 1)
        for_numpy : bool
            If False produce unsigned, otherwise signed types: if hypermap
            will be loaded using the pure python function where numpy's inplace
            integer addition will be used, the dtype should be signed;
            If cython implementation will be used (default), then any returned
            dtypes can be safely unsigned. (default False)

        Returns
        -------
        depth : numpy.dtype
            numpy dtype large enough to use in final hypermap numpy array.

        """
        sum_eds = self.spectra_data[index].data
        # the most intensive peak is Bruker reference peak at 0kV:
        roof = (
            np.max(sum_eds)
            // self.image.width
            // self.image.height
            * 2
            * downsample
            * downsample
        )
        # this complicated nonsense below is due to numpy regression in adding
        # integer inplace to unsigned integer array. (python integers is
        # signed)
        if roof > 0xFF:
            if roof > 0xFFFF:
                if for_numpy and (downsample > 1):
                    if roof > 0xEFFFFFFF:
                        depth = np.int64
                    else:
                        depth = np.int32
                else:
                    depth = np.uint32
            else:
                if for_numpy and (downsample > 1):
                    if roof > 0xEFFF:
                        depth = np.int32
                    else:
                        depth = np.int16
                else:
                    depth = np.uint16
        else:
            if for_numpy and (downsample > 1):
                if roof > 0xEF:
                    depth = np.int16
                else:
                    depth = np.int8
            else:
                depth = np.uint8
        return depth

    def get_spectra_metadata(self, index=0):
        """return objectified xml with spectra metadata
        Arguments:
        index -- index of hypermap/spectra (default 0)
        """
        return self.spectra_data[index]

    def calc_real_time(self):
        """calculate and return real time for whole hypermap
        in seconds
        """
        line_cnt_sum = np.sum(self.line_counter, dtype=float)
        line_avg = self.dsp_metadata["LineAverage"]
        pix_avg = self.dsp_metadata["PixelAverage"]
        pix_time = self.dsp_metadata["PixelTime"]
        width = self.image.width
        real_time = line_cnt_sum * line_avg * pix_avg * pix_time * width * 1e-6
        return float(real_time)

    def gen_hspy_item_dict_basic(self):
        i = {
            "axes": [
                {
                    "name": "height",
                    "offset": 0,
                    "scale": self.y_res,
                    "units": self.units,
                },
                {
                    "name": "width",
                    "offset": 0,
                    "scale": self.x_res,
                    "units": self.units,
                },
            ],
            "metadata": {
                "Acquisition_instrument": {self.mode: self.get_acq_instrument_dict()},
                "Sample": {"name": self.name},
            },
            "original_metadata": {
                "Microscope": self.sem_metadata,
                "DSP Configuration": self.dsp_metadata,
                "Stage": self.stage_metadata,
            },
            "mapping": get_mapping(self.mode),
        }
        return i


class BCF_reader(SFS_reader):
    """Class to read bcf (Bruker hypermapping) file.

    Inherits SFS_reader and all its attributes and methods.

    Attributes:
    filename

    Methods:
    check_index_valid, parse_hypermap

    The class instantiates HyperHeader class as self.header attribute
    where all metadata, sum eds spectras, (SEM) images are stored.
    """

    def __init__(self, filename, instrument=None):
        SFS_reader.__init__(self, filename)
        header_file = self.get_file("EDSDatabase/HeaderData")
        self.available_indexes = []
        # get list of presented indexes from file tree of binary sfs container
        # while looking for file names containing the hypercube data:
        for i in self.vfs["EDSDatabase"].keys():
            if "SpectrumData" in i:
                self.available_indexes.append(int(i[-1]))
        self.def_index = min(self.available_indexes)
        header_bytes = header_file.get_as_BytesIO_string().getvalue()
        sanitized_bytes = sanitize_msxml_float(header_bytes)
        self.header = HyperHeader(
            sanitized_bytes, self.available_indexes, instrument=instrument
        )
        self.hypermap = {}

    def check_index_valid(self, index):
        """check and return if index is valid"""
        if not isinstance(index, int):
            raise TypeError("provided index should be integer")
        if index not in self.available_indexes:
            raise IndexError(
                "requisted index is not in the list of available indexes. "
                "Available maps are under indexes: {0}".format(
                    str(self.available_indexes)
                )
            )
        return index

    def parse_hypermap(self, index=None, downsample=1, cutoff_at_kV=None, lazy=False):
        """Unpack the Delphi/Bruker binary spectral map and return
        numpy array in memory efficient way.

        Pure python/numpy implementation -- slow, or
        cython/memoryview/numpy implementation if compiled and present
        (fast) is used.

        Parameters
        ----------
        index : None or int
            The index of hypermap in bcf if there is more than one
            hyper map in file.
        downsample : int
            Downsampling factor. Differently than block_reduce from
            skimage.measure, the parser populates reduced array by suming
            results of pixels, thus having lower memory requiriments. Default
            is 1.
        cutoff_at_kV : None, float, int or str
            Value or method to truncate the array at energy in kV. Helps reducing size of the returned
            array. Default value is None (does not truncate). Numerical value should be in kV.
            Two methods for automatic cutoff is available:
              "zealous" - truncates to the last non zero channel (should not be used for stacks).
              "auto" - truncates to hv of electron microscope (good for stacks if hv is consistent).
        lazy : bool
            It True, returns dask.array otherwise a numpy.array. Default is
            False.

        Returns
        -------
        result : numpy.ndarray or dask.array.array
            Bruker hypermap, with (y,x,E) shape.
        """
        if index is None:
            index = self.def_index

        if type(cutoff_at_kV) in (int, float):
            eds = self.header.spectra_data[index]
            n_channels = eds.energy_to_channel(cutoff_at_kV)
        elif cutoff_at_kV == "zealous":
            n_channels = self.header.spectra_data[index].last_non_zero_channel() + 1
        elif cutoff_at_kV == "auto":
            n_channels = self.header.get_consistent_min_channels(index=index)
        else:  # None
            n_channels = self.header.spectra_data[index].data.size

        shape = (
            ceil(self.header.image.height / downsample),
            ceil(self.header.image.width / downsample),
            n_channels,
        )
        sfs_file = SFS_reader(self.filename)
        vrt_file_hand = sfs_file.get_file("EDSDatabase/SpectrumData" + str(index))
        if fast_unbcf:
            parse_func = unbcf_fast.parse_to_numpy
            dtype = self.header.estimate_map_depth(
                index=index, downsample=downsample, for_numpy=False
            )
        else:
            parse_func = py_parse_hypermap
            dtype = self.header.estimate_map_depth(
                index=index, downsample=downsample, for_numpy=True
            )
        if lazy:
            import dask.array as da

            value = dask.delayed(parse_func)(
                vrt_file_hand, shape, dtype, downsample=downsample
            )
            result = da.from_delayed(value, shape=shape, dtype=dtype)
        else:
            result = parse_func(vrt_file_hand, shape, dtype, downsample=downsample)
        return result

    def add_filename_to_general(self, item):
        """hypy helper method"""
        item["metadata"]["General"]["original_filename"] = basename(self.filename)


def spx_reader(filename, lazy=False):
    with open(filename, "br") as fn:
        xml_str = fn.read()
    root = ET.fromstring(xml_str)
    sp_node = root.find("./ClassInstance[@Type='TRTSpectrum']")
    try:
        name = str(sp_node.attrib["Name"])
    except KeyError:
        name = "Undefinded"
        _logger.info("spectra have no name. Giving it 'Undefined' name")
    spectrum = EDXSpectrum(sp_node)
    mode = guess_mode(spectrum.hv)
    results_xml = sp_node.find("./ClassInstance[@Type='TRTResult']")
    elements_xml = sp_node.find("./ClassInstance[@Type='TRTPSEElementList']")
    hy_spec = {
        "data": spectrum.data,
        "axes": [
            {
                "name": "Energy",
                "size": len(spectrum.data),
                "offset": spectrum.offset,
                "scale": spectrum.scale,
                "units": "keV",
                "navigate": False,
            }
        ],
        "metadata":
        # where is no way to determine what kind of instrument was used:
        # TEM or SEM
        {
            "Acquisition_instrument": {
                mode: {
                    "Detector": gen_detector_node(spectrum),
                    "beam_energy": spectrum.hv,
                }
            },
            "General": {
                "original_filename": basename(filename),
                "title": "EDX",
                "date": spectrum.date,
                "time": spectrum.time,
            },
            "Sample": {"name": name},
            "Signal": {
                "signal_type": "EDS_%s" % mode,
                "quantity": "X-rays (Counts)",
            },
        },
        "original_metadata": {
            "Hardware": spectrum.hardware_metadata,
            "Detector": spectrum.detector_metadata,
            "Analysis": spectrum.esma_metadata,
            "Spectrum": spectrum.spectrum_metadata,
        },
    }
    if results_xml is not None:
        hy_spec["original_metadata"]["Results"] = x2d.dictionarize(results_xml)
    if elements_xml is not None:
        elem = x2d.dictionarize(elements_xml)["ChildClassInstances"]
        hy_spec["original_metadata"]["Selected_elements"] = elem
        hy_spec["metadata"]["Sample"]["elements"] = elem["XmlClassName"]
    return [hy_spec]


# dict of nibbles to struct notation for reading:
st = {1: "B", 2: "B", 4: "H", 8: "I", 16: "Q"}


def py_parse_hypermap(virtual_file, shape, dtype, downsample=1):
    """Unpack the Delphi/Bruker binary spectral map and return
    numpy array in memory efficient way using pure python implementation.
    (Slow!)

    The function is long and complicated due to complexity of Delphi packed
    array.
    Whole parsing is placed in one function to reduce overhead of
    python function calls. For cleaner parsing logic, please, see
    fast cython implementation at rsciio/bruker/unbcf_fast.pyx

    The method is only meant to be used if for some
    reason c (generated with cython) version of the parser is not compiled.

    Parameters
    ----------
    virtual_file -- virtual file handle returned by SFS_reader instance
        or by object inheriting it (e.g. BCF_reader instance)
    shape -- numpy shape
    dtype -- numpy dtype
    downsample -- downsample factor

    note!: downsample, shape and dtype are interconnected and needs
    to be properly calculated otherwise wrong output or segfault
    is expected

    Returns
    -------
    numpy array of bruker hypermap, with (y, x, E) shape.
    """
    iter_data, size_chnk = virtual_file.get_iter_and_properties()[:2]
    dwn_factor = downsample
    max_chan = shape[2]
    buffer1 = next(iter_data)
    height, width = strct_unp("<ii", buffer1[:8])
    # hyper map as very flat array:
    vfa = np.zeros(shape[0] * shape[1] * shape[2], dtype=dtype)
    offset = 0x1A0
    size = size_chnk
    for line_cnt in range(height):
        if (offset + 4) >= size:
            size = size_chnk + size - offset
            buffer1 = buffer1[offset:] + next(iter_data)
            offset = 0
        line_head = strct_unp("<i", buffer1[offset : offset + 4])[0]
        offset += 4
        for dummy1 in range(line_head):
            if (offset + 22) >= size:
                size = size_chnk + size - offset
                buffer1 = buffer1[offset:] + next(iter_data)
                offset = 0
            # the pixel header contains such information:
            # x index of pixel (uint32);
            # number of channels for whole mapping (unit16);
            # number of channels for pixel (uint16);
            # dummy placehollder (same value in every known bcf) (32bit);
            # flag distinguishing packing data type (16bit):
            #    0 - 16bit packed pulses, 1 - 12bit packed pulses,
            #    >1 - instructively packed spectra;
            # value which sometimes shows the size of packed data (uint16);
            # number of pulses if pulse data are present (uint16) or
            #      additional pulses to the instructively packed data;
            # packed data size (32bit) (without additional pulses) \
            #       next header is after that amount of bytes;
            (
                x_pix,
                chan1,
                chan2,
                dummy1,
                flag,
                dummy_size1,
                n_of_pulses,
                data_size2,
            ) = strct_unp("<IHHIHHHI", buffer1[offset : offset + 22])
            pix_idx = (x_pix // dwn_factor) + (
                ceil(width / dwn_factor) * (line_cnt // dwn_factor)
            )
            offset += 22
            if (offset + data_size2) >= size:
                buffer1 = buffer1[offset:] + next(iter_data)
                size = size_chnk + size - offset
                offset = 0
            if flag == 0:
                data1 = buffer1[offset : offset + data_size2]
                arr16 = np.frombuffer(data1, dtype=np.uint16)
                pixel = np.bincount(arr16, minlength=chan1 - 1)
                offset += data_size2
            elif flag == 1:  # and (chan1 != chan2)
                # Unpack packed 12-bit data to 16-bit uints:
                data1 = buffer1[offset : offset + data_size2]
                switched_i2 = np.frombuffer(data1, dtype="<u2").copy().byteswap(True)
                data2 = (
                    np.frombuffer(switched_i2.tobytes(), dtype=np.uint8)
                    .copy()
                    .repeat(2)
                )
                mask = np.ones_like(data2, dtype=bool)
                mask[0::6] = mask[5::6] = False
                # Reinterpret expanded as 16-bit:
                # string representation of array after switch will have
                # always BE independently from endianness of machine
                exp16 = np.frombuffer(
                    data2[mask].tobytes(), dtype=">u2", count=n_of_pulses
                ).copy()
                exp16[0::2] >>= 4  # Shift every second short by 4
                exp16 &= np.uint16(0x0FFF)  # Mask all shorts to 12bit
                pixel = np.bincount(exp16, minlength=chan1 - 1)
                offset += data_size2
            else:  # flag > 1
                # Unpack instructively packed data to pixel channels:
                pixel = []
                the_end = offset + data_size2 - 4
                while offset < the_end:
                    # this would work on py3
                    # size_p, channels = buffer1[offset:offset + 2]
                    # this is needed on py2:
                    size_p, channels = strct_unp("<BB", buffer1[offset : offset + 2])
                    offset += 2
                    if size_p == 0:
                        pixel += channels * [0]
                    else:
                        gain = strct_unp(
                            "<" + st[size_p * 2], buffer1[offset : offset + size_p]
                        )[0]
                        offset += size_p
                        if size_p == 1:
                            # special case with nibble switching
                            length = ceil(channels / 2)
                            # valid py3 code
                            # a = list(buffer1[offset:offset + length])
                            # this have to be used on py2:
                            a = strct_unp(
                                "<" + "B" * length, buffer1[offset : offset + length]
                            )
                            g = []
                            for i in a:
                                g += (i & 0x0F) + gain, (i >> 4) + gain
                            pixel += g[:channels]
                        else:
                            length = int(channels * size_p / 2)
                            temp = strct_unp(
                                "<" + channels * st[size_p],
                                buffer1[offset : offset + length],
                            )
                            pixel += [l_ + gain for l_ in temp]
                        offset += length
                if chan2 < chan1:
                    rest = chan1 - chan2
                    pixel += rest * [0]
                # additional data size:
                if n_of_pulses > 0:
                    add_s = strct_unp("<I", buffer1[offset : offset + 4])[0]
                    offset += 4
                    if (offset + add_s) >= size:
                        buffer1 = buffer1[offset:] + next(iter_data)
                        size = size_chnk + size - offset
                        offset = 0
                    # the additional pulses:
                    add_pulses = strct_unp(
                        "<" + "H" * n_of_pulses, buffer1[offset : offset + add_s]
                    )
                    offset += add_s
                    for i in add_pulses:
                        pixel[i] += 1
                else:
                    offset += 4
            # if no downsampling is needed, or if it is first
            # pixel encountered with downsampling on, then
            # use assignment, which is ~4 times faster, than inplace add
            if max_chan < chan1:  # if pixel have more channels than we need
                chan1 = max_chan
            if dwn_factor == 1:
                vfa[max_chan * pix_idx : chan1 + max_chan * pix_idx] = pixel[:chan1]
            else:
                vfa[max_chan * pix_idx : chan1 + max_chan * pix_idx] += pixel[:chan1]
    vfa.resize((ceil(height / dwn_factor), ceil(width / dwn_factor), max_chan))
    # check if array is signed, and convert to unsigned
    if str(vfa.dtype)[0] == "i":
        new_dtype = "".join(["u", str(vfa.dtype)])
        vfa.dtype = new_dtype
    return vfa


def file_reader(
    filename,
    lazy=False,
    select_type=None,
    index=None,
    downsample=1,
    cutoff_at_kV=None,
    instrument=None,
):
    """
    Read a Bruker ``.bcf`` or ``.spx`` file.

    Parameters
    ----------
    %s
    %s
    select_type : ``'spectrum_image'``, ``'image'`` or None, default=None
        If specified, only the corresponding type of data, either spectrum
        image or image, is returned.
        By default (None), all data are loaded.
    index : int, None or str, default=None
        Allow to select the index of the dataset in the ``.bcf`` file, which
        can contain several datasets. The default value (``None``) results in
        loading the first dataset. When set to ``'all'``, all available datasets
        will be loaded and returned as separate signals.
    downsample : int, default=1
        The downsampling ratio of a hyperspectral array (height and width only),
        can be an integer >=1, where value of 1 results in no downsampling.
        The underlying method of downsampling is unchangeable: sum. Differently
        than :py:func:``skimage.measure.block_reduce``, it is memory efficient
        as it doesn't create intermediate arrays.
    cutoff_at_kV : int, float, ``'zealous'``, ``'auto'`` or None, default=None
        It can be used either to crop or enlarge the energy (or number of channels)
        range at max values. It can be used to conserve memory or enlarge
        the range if needed to mach the size of other file. The default value
        (``None``) does not influence the size. Numerical values should be in kV.
        ``'zealous'`` truncates to the last non zero channel (this option
        should not be used for stacks, as low beam current EDS can have a different
        last non zero channel per slice). ``'auto'`` truncates channels to SEM/TEM
        acceleration voltage or energy at last channel, depending which is smaller.
        In case the hv info is not there or hv is off (0 kV) then it falls back to
        the full channel range.
    instrument : str or None, default=None
        Can be either ``'TEM'`` or ``'SEM'``.

    %s

    Examples
    --------
    Example of loading reduced (downsampled, and with energy range cropped)
    "spectrum only" data from ``bcf`` (original shape: 80 keV EDS range (4096 channels),
    100x75 pixels; SEM acceleration voltage: 20kV):

    >>> file_reader("sample80kv.bcf", select_type='spectrum_image',
                    downsample=2, cutoff_at_kV=10)

    Load the same file with limiting array size to SEM acceleration voltage:

    >>> file_reader("sample80kv.bcf", cutoff_at_kV='auto')

    The loaded array energy dimension can by forced to be larger than the data
    recorded by setting the 'cutoff_at_kV' kwarg to higher value:

    >>> file_reader("sample80kv.bcf", cutoff_at_kV=60)

    Loading without setting ``cutoff_at_kV`` value would return data with all 4096
    channels. Note that setting ``downsample`` higher than 1 currently locks out using SEM
    images for navigation in the plotting.
    """
    ext = splitext(filename)[1][1:].lower()
    if ext == "bcf":
        to_return = bcf_reader(
            filename=filename,
            lazy=lazy,
            select_type=select_type,
            index=index,
            downsample=downsample,
            cutoff_at_kV=cutoff_at_kV,
            instrument=instrument,
        )
    elif ext == "spx":
        to_return = spx_reader(
            filename=filename,
            lazy=lazy,
        )
    else:
        raise ValueError(f"'{ext}' is not a supported extension for the bruker reader.")

    return to_return


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


def bcf_reader(
    filename,
    lazy=False,
    select_type=None,
    index=None,  # noqa
    downsample=1,
    cutoff_at_kV=None,
    instrument=None,
):
    """
    Reads a bruker ``.bcf`` file and loads the data into the appropriate class,
    then wraps it into a list of dictionaries typically used by the
    :py:func:`hyperspy.api.load` function.

    Parameters
    ----------
    select_type : str or None, default=None
        One of: spectrum_image, image. If none specified, then function
        loads everything, else if specified, loads either just sem imagery,
        or just hyper spectral mapping data.
    index : int, None or str, default=None
        Index of dataset in bcf v2 can be None integer and 'all';
        None will select first available mapping if more than one.
        'all' will return all maps if more than one present;
        integer will return only selected map.
    downsample : int, default=1
        the downsample ratio of hyperspectral array (downsampling
        height and width only), can be integer from 1 to inf, where '1' means
        no downsampling will be applied.
    cutoff_at_kV : int, float or None, default=None
        if set (can be int of float >= 0) can be used either, to
        crop or enlarge energy range at max values.
    instrument : str or None, default=None
        Can be either 'TEM' or 'SEM'.
    """

    # objectified bcf file:
    obj_bcf = BCF_reader(filename, instrument=instrument)
    if select_type == "image":
        return bcf_images(obj_bcf)
    elif select_type == "spectrum_image":
        return bcf_hyperspectra(
            obj_bcf,
            index=index,
            downsample=downsample,
            cutoff_at_kV=cutoff_at_kV,
            lazy=lazy,
        )
    else:
        return bcf_images(obj_bcf) + bcf_hyperspectra(
            obj_bcf,
            index=index,
            downsample=downsample,
            cutoff_at_kV=cutoff_at_kV,
            lazy=lazy,
        )


def bcf_images(obj_bcf):
    """Returns list of dict with sem images and metadata."""
    images_list = []
    for img in obj_bcf.header.image.images:
        obj_bcf.add_filename_to_general(img)
        images_list.append(img)
    if hasattr(obj_bcf.header, "overview"):
        for img2 in obj_bcf.header.overview.images:
            obj_bcf.add_filename_to_general(img2)
            images_list.append(img2)
    return images_list


def bcf_hyperspectra(
    obj_bcf,
    index=None,
    downsample=None,
    cutoff_at_kV=None,
    lazy=False,  # noqa
):
    """Returns list of dict with eds hyperspectra and metadata."""
    global warn_once
    if (fast_unbcf is False) and warn_once:
        _logger.warning(
            """unbcf_fast library is not present...
Parsing BCF with Python-only backend, which is slow... please wait.
If parsing is uncomfortably slow, first install cython, then reinstall RosettaSciIO.
For more information, check the 'Installing RosettaSciIO' section in the documentation."""
        )
        warn_once = False
    if index is None:
        indexes = [obj_bcf.def_index]
    elif index == "all":
        indexes = obj_bcf.available_indexes
    else:
        indexes = [obj_bcf.check_index_valid(index)]
    hyperspectra = []
    mode = obj_bcf.header.mode
    mapping = get_mapping(mode)
    for index in indexes:
        hypermap = obj_bcf.parse_hypermap(
            index=index, downsample=downsample, cutoff_at_kV=cutoff_at_kV, lazy=lazy
        )
        eds_metadata = obj_bcf.header.get_spectra_metadata(index=index)
        hyperspectra.append(
            {
                "data": hypermap,
                "axes": [
                    {
                        "name": "height",
                        "size": hypermap.shape[0],
                        "offset": 0,
                        "scale": obj_bcf.header.y_res * downsample,
                        "units": obj_bcf.header.units,
                        "navigate": True,
                    },
                    {
                        "name": "width",
                        "size": hypermap.shape[1],
                        "offset": 0,
                        "scale": obj_bcf.header.y_res * downsample,
                        "units": obj_bcf.header.units,
                        "navigate": True,
                    },
                    {
                        "name": "Energy",
                        "size": hypermap.shape[2],
                        "offset": eds_metadata.offset,
                        "scale": eds_metadata.scale,
                        "units": "keV",
                        "navigate": False,
                    },
                ],
                "metadata":
                # where is no way to determine what kind of instrument was used:
                # TEM or SEM
                {
                    "Acquisition_instrument": {
                        mode: obj_bcf.header.get_acq_instrument_dict(
                            detector=True, index=index
                        )
                    },
                    "General": {
                        "original_filename": basename(obj_bcf.filename),
                        "title": "EDX",
                        "date": obj_bcf.header.date,
                        "time": obj_bcf.header.time,
                    },
                    "Sample": {
                        "name": obj_bcf.header.name,
                        "elements": sorted(list(obj_bcf.header.elements)),
                        "xray_lines": sorted(gen_elem_list(obj_bcf.header.elements)),
                    },
                    "Signal": {
                        "signal_type": "EDS_%s" % mode,
                        "quantity": "X-rays (Counts)",
                    },
                },
                "original_metadata": {
                    "Hardware": eds_metadata.hardware_metadata,
                    "Detector": eds_metadata.detector_metadata,
                    "Analysis": eds_metadata.esma_metadata,
                    "Spectrum": eds_metadata.spectrum_metadata,
                    "DSP Configuration": obj_bcf.header.dsp_metadata,
                    "Line counter": obj_bcf.header.line_counter,
                    "Stage": obj_bcf.header.stage_metadata,
                    "Microscope": obj_bcf.header.sem_metadata,
                },
                "mapping": mapping,
            }
        )
    return hyperspectra


def gen_elem_list(the_dict):
    return ["_".join([i, parse_line(the_dict[i]["line"])]) for i in the_dict]


def parse_line(line_string):
    """standardize line description.

    Bruker saves line description in all caps
    and omits the type if only one exists instead of
    using alfa"""
    if len(line_string) == 1:
        line_string = line_string + "a"
    elif len(line_string) > 2:
        line_string = line_string[:2]
    return line_string.capitalize()


def get_mapping(mode):
    return {
        "Stage.Rotation": ("Acquisition_instrument.%s.Stage.rotation" % mode, None),
        "Stage.Tilt": ("Acquisition_instrument.%s.Stage.tilt_alpha" % mode, None),
        "Stage.X": ("Acquisition_instrument.%s.Stage.x" % mode, None),
        "Stage.Y": ("Acquisition_instrument.%s.Stage.y" % mode, None),
        "Stage.Z": ("Acquisition_instrument.%s.Stage.z" % mode, None),
    }


def guess_mode(hv):
    """there is no way to determine what kind of instrument
    was used from metadata: TEM or SEM.
    However simple guess can be made using the acceleration
    voltage, assuming that SEM is <= 30kV or TEM is >30kV"""
    if hv is not None and hv > 30.0:
        mode = "TEM"
    else:
        mode = "SEM"

    if hv is not None:
        _logger.info(
            "Guessing that the acquisition instrument is %s " % mode
            + "because the beam energy is %i keV. If this is wrong, " % hv
            + "please provide the right instrument using the 'instrument' "
            + "keyword."
        )
    return mode


def gen_detector_node(spectrum):
    eds_dict = {
        "EDS": {
            "elevation_angle": spectrum.elev_angle,
            "detector_type": spectrum.detector_type,
        }
    }
    if "AzimutAngle" in spectrum.esma_metadata:
        eds_dict["EDS"]["azimuth_angle"] = spectrum.esma_metadata["AzimutAngle"]
    if "RealTime" in spectrum.hardware_metadata:
        eds_dict["EDS"]["real_time"] = spectrum.hardware_metadata["RealTime"] / 1000
        eds_dict["EDS"]["live_time"] = spectrum.hardware_metadata["LifeTime"] / 1000
    return eds_dict


def gen_iso_date_time(node):
    date_xml = node.find("./Date")
    time_xml = node.find("./Time")
    if date_xml is not None:
        dt = datetime.strptime(
            " ".join([date_xml.text, time_xml.text]), "%d.%m.%Y %H:%M:%S"
        )
        date = dt.date().isoformat()
        time = dt.time().isoformat()
        return date, time
    else:
        return None
