2 * Copyright (C) 2012-2021 Euclid Science Ground Segment
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 3.0 of the License, or (at your option)
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21#include "NdArray/NdArray.h"
23#include <ElementsKernel/Exception.h>
30using boost::endian::little_uint16_t;
31using boost::endian::little_uint32_t;
34NdArray<T> readNpy(std::istream& input) {
37 std::vector<size_t> shape;
38 std::vector<std::string> attr_names;
40 readNpyHeader(input, dtype, shape, attr_names, n_elements);
41 if (dtype != NpyDtype<T>::str)
42 throw Elements::Exception() << "Can not cast " << dtype << " into " << typeid(T).name();
44 if (!attr_names.empty()) {
45 n_elements *= attr_names.size();
48 std::vector<T> data(n_elements);
49 input.read(reinterpret_cast<char*>(&data[0]), sizeof(T) * data.size());
50 return {shape, attr_names, std::move(data)};
53} // end of namespace NdArray
54} // end of namespace Euclid