"""Andor."""


# --- import --------------------------------------------------------------------------------------


import os
import pathlib
import time

import numpy as np

from ._data import Data
from .. import exceptions as wt_exceptions
from ..kit import _timestamp as timestamp


# --- define --------------------------------------------------------------------------------------


__all__ = ["from_Solis"]


# --- from function -------------------------------------------------------------------------------


def from_Solis(filepath, name=None, parent=None, verbose=True) -> Data:
    """Create a data object from Andor Solis software (ascii exports).

    Parameters
    ----------
    filepath : path-like
        Path to file (should be .asc format).
        Can be either a local or remote file (http/ftp).
        Can be compressed with gz/bz2, decompression based on file name.
    name : string (optional)
        Name to give to the created data object. If None, filename is used.
        Default is None.
    parent : WrightTools.Collection (optional)
        Collection to place new data object within. Default is None.
    verbose : boolean (optional)
        Toggle talkback. Default is True.

    Returns
    -------
    data
        New data object.
    """
    # parse filepath
    filestr = os.fspath(filepath)
    filepath = pathlib.Path(filepath)

    if not ".asc" in filepath.suffixes:
        wt_exceptions.WrongFileTypeWarning.warn(filepath, ".asc")
    # parse name
    if not name:
        name = filepath.name.split(".")[0]
    # create data
    ds = np.DataSource(None)
    f = ds.open(filestr, "rt")
    axis0 = []
    arr = []
    attrs = {}
    for i in range(2):
        line = f.readline().strip()[:-1]
        line = [float(x) for x in line.split(",")]  # TODO: robust to space, tab, comma
        axis0.append(line.pop(0))
        arr.append(line)
    axis0_increasing = axis0[1] - axis0[0] > 0

    def get_frames(f, arr, axis0, increasing):
        axis0_written = False
        while True:
            line = f.readline().strip()[:-1]
            if len(line) == 0:
                break
            else:
                line = [float(x) for x in line.split(",")]
                # signature of new frames is restart of axis0
                if not axis0_written and ((line[0] - axis0[-1] > 0) != increasing):
                    axis0_written = True
                if axis0_written:
                    line.pop(0)
                else:
                    axis0.append(line.pop(0))
                arr.append(line)
        return arr, axis0

    arr, axis0 = get_frames(f, arr, axis0, axis0_increasing)
    nframes = len(arr) // len(axis0)

    i = 0
    while i < 3:
        line = f.readline().strip()
        if len(line) == 0:
            i += 1
        else:
            try:
                key, val = line.split(":", 1)
            except ValueError:
                pass
            else:
                attrs[key.strip()] = val.strip()

    f.close()
    created = attrs["Date and Time"]  # is this UTC?
    created = time.strptime(created, "%a %b %d %H:%M:%S %Y")
    created = timestamp.TimeStamp(time.mktime(created)).RFC3339

    kwargs = {"name": name, "kind": "Solis", "source": filestr, "created": created}
    if parent is None:
        data = Data(**kwargs)
    else:
        data = parent.create_data(**kwargs)

    axis0 = np.array(axis0)
    if float(attrs["Grating Groove Density (l/mm)"]) == 0:
        xname = "xindex"
        xunits = None
    else:
        xname = "wm"
        xunits = "nm"
    axes = [xname, "yindex"]

    if nframes == 1:
        arr = np.array(arr)
        data.create_variable(name=xname, values=axis0[:, None], units=xunits)
        data.create_variable(name="yindex", values=np.arange(arr.shape[-1])[None, :], units=None)
    else:
        arr = np.array(arr).reshape(nframes, len(axis0), len(arr[0]))
        data.create_variable(name="frame", values=np.arange(nframes)[:, None, None], units=None)
        data.create_variable(name=xname, values=axis0[None, :, None], units=xunits)
        data.create_variable(
            name="yindex", values=np.arange(arr.shape[-1])[None, None, :], units=None
        )
        axes = ["frame"] + axes

    data.transform(*axes)
    print(f"arr.shape {arr.shape}")
    print(f"nframes {nframes}")
    arr /= float(attrs["Exposure Time (secs)"])
    # signal has units of Hz because time normalized
    data.create_channel(name="signal", values=arr, signed=False, units="Hz")

    for key, val in attrs.items():
        data.attrs[key] = val

    # finish
    if verbose:
        print("data created at {0}".format(data.fullpath))
        print("  axes: {0}".format(data.axis_names))
        print("  shape: {0}".format(data.shape))
    return data

@@ -1675,43 +1675,17 @@
  %7B%7D%0A
-    for i in range(2):%0A    
+%0A
     line
  = f
@@ -1672,32 +1672,33 @@
 s = %7B%7D%0A%0A    line
+0
  = f.readline().
@@ -1710,32 +1710,29 @@
 ()%5B:-1%5D%0A    
-   
- 
 line
+0
  = %5Bfloat(x)
@@ -1737,32 +1737,33 @@
 x) for x in line
+0
 .split(%22,%22)%5D  # 
@@ -1796,20 +1796,16 @@
 , comma%0A
-    
     axis
@@ -1809,32 +1809,33 @@
 xis0.append(line
+0
 .pop(0))%0A       
@@ -1827,28 +1827,24 @@
 pop(0))%0A    
- 
-   
 arr.append(l
@@ -1850,56 +1850,10 @@
 line
-)%0A    axis0_increasing = axis0%5B1%5D - axis0%5B0%5D %3E 
 0
+)
 %0A%0A  
@@ -1886,20 +1886,8 @@
 xis0
-, increasing
 ):%0A 
@@ -2213,17 +2213,16 @@
 ten and 
-(
 (line%5B0%5D
@@ -2226,38 +2226,19 @@
 %5B0%5D 
--
+==
  axis0%5B
--1%5D %3E 0) != increasing
+0%5D
 ):%0A 
@@ -2516,26 +2516,8 @@
 xis0
-, axis0_increasing
 )%0A  
@@ -4135,76 +4135,8 @@
 es)%0A
-    print(f%22arr.shape %7Barr.shape%7D%22)%0A    print(f%22nframes %7Bnframes%7D%22)%0A
     

Update _solis.py