Line data Source code
1 : // SPDX-License-Identifier: BSD-3-Clause 2 : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp) 3 : #pragma once 4 : 5 : #include <tuple> 6 : #include <variant> 7 : 8 : #include "pybind11.h" 9 : 10 : #include "scipp/core/dtype.h" 11 : #include "scipp/core/time_point.h" 12 : #include "scipp/units/unit.h" 13 : 14 : struct DefaultUnit {}; 15 : 16 : using ProtoUnit = 17 : std::variant<std::string, scipp::units::Unit, pybind11::none, DefaultUnit>; 18 : 19 : std::tuple<scipp::units::Unit, int64_t> 20 : get_time_unit(std::optional<scipp::units::Unit> value_unit, 21 : std::optional<scipp::units::Unit> dtype_unit, 22 : scipp::units::Unit sc_unit); 23 : 24 : std::tuple<scipp::units::Unit, int64_t> 25 : get_time_unit(const pybind11::buffer &value, const pybind11::object &dtype, 26 : scipp::units::Unit unit); 27 : 28 : template <class T> 29 : std::tuple<scipp::units::Unit, scipp::units::Unit> 30 39088 : common_unit(const pybind11::object &, const scipp::units::Unit unit) { 31 : // In the general case, values and variances do not encode units themselves. 32 39088 : return std::tuple{unit, unit}; 33 : } 34 : 35 : template <> 36 : std::tuple<scipp::units::Unit, scipp::units::Unit> 37 : common_unit<scipp::core::time_point>(const pybind11::object &values, 38 : const scipp::units::Unit unit); 39 : 40 : /// Format a time unit as an ASCII string. 41 : /// Only time units are supported! 42 : // TODO Can be removed if / when the units library supports this. 43 : std::string to_numpy_time_string(scipp::units::Unit unit); 44 : std::string to_numpy_time_string(const ProtoUnit &unit); 45 : 46 : scipp::units::Unit 47 : unit_or_default(const ProtoUnit &unit, 48 : const scipp::core::DType type = scipp::core::dtype<void>);