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 (137 lines) | stat: -rw-r--r-- 4,104 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
# -*- 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 os
from datetime import datetime

import numpy as np

from rsciio._docstrings import FILENAME_DOC, LAZY_UNSUPPORTED_DOC, RETURNS_DOC


def _cnv_time(timestr):
    try:
        if not isinstance(timestr, str):
            # for numpy < 2.0
            timestr = timestr.decode()
        t = datetime.strptime(timestr, "%H:%M:%S.%f")
        dt = t - datetime(t.year, t.month, t.day)
        r = float(dt.seconds) + float(dt.microseconds) * 1e-6
    except ValueError:
        r = float(timestr)
    return r


def _bad_file(filename):
    raise AssertionError("Cannot interpret as DENS heater log: %s" % filename)


def file_reader(filename, lazy=False):
    """
    Read a DENSsolutions DigiHeater logfile.

    Parameters
    ----------
    %s
    %s

    %s
    """
    if lazy is not False:
        raise NotImplementedError("Lazy loading is not supported.")

    with open(filename, "rt") as f:
        # Strip leading, empty lines
        line = str(f.readline())
        while line.strip() == "" and not f.closed:
            line = str(f.readline())
        try:
            date, version = line.split("\t")
        except ValueError:
            _bad_file(filename)
        if version.strip() != "Digiheater 3.1":
            _bad_file(filename)
        calib = str(f.readline()).split("\t")
        str(f.readline())  # delta_t
        header_line = str(f.readline())
        try:
            R0, a, b, c = [float(v.split("=")[1]) for v in calib]
            date0 = datetime.strptime(date, "%d/%m/'%y %H:%M")
            date = "%s" % date0.date()
            time = "%s" % date0.time()
        except ValueError:
            _bad_file(filename)
        original_metadata = dict(R0=R0, a=a, b=b, c=c, date=date0, version=version)

        if header_line.strip() != (
            "sample\ttime\tTset[C]\tTmeas[C]\tRheat[ohm]\tVheat[V]\t"
            "Iheat[mA]\tPheat [mW]\tc"
        ):
            _bad_file(filename)
        try:
            rawdata = np.loadtxt(
                f, converters={1: _cnv_time}, usecols=(1, 3), unpack=True
            )
        except ValueError:
            _bad_file(filename)

    times = rawdata[0]
    # Add a day worth of seconds to any values after a detected rollover
    # Hopefully unlikely that there is more than one, but we can handle it
    for rollover in 1 + np.where(np.diff(times) < 0)[0]:
        times[rollover:] += 60 * 60 * 24
    # Raw data is not necessarily grid aligned. Interpolate onto grid.
    offset, scale = np.polynomial.polynomial.polyfit(
        np.arange(times.size), times, deg=1
    )

    metadata = {
        "General": {
            "original_filename": os.path.split(filename)[1],
            "date": date,
            "time": time,
        },
        "Signal": {"signal_type": "", "quantity": "Temperature (Celsius)"},
    }

    axes = [
        {
            "size": times.size,
            "index_in_array": 0,
            "name": "Time",
            "scale": scale,
            "offset": offset,
            "units": "s",
            "navigate": False,
        }
    ]

    dictionary = {
        "data": rawdata[1],
        "axes": axes,
        "metadata": metadata,
        "original_metadata": {"DENS_header": original_metadata},
    }

    return [
        dictionary,
    ]


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