Debian Codegraph @

src/atlas/io/ArrayAdaptor.cc

raw
1 /*
2 * (C) Copyright 2020 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #include "ArrayAdaptor.h"
12
13 #include <cstring> // memcpy
14 #include <sstream>
15
16 #include "atlas/array/Array.h"
17
18 namespace atlas {
19 namespace array {
20
21 //---------------------------------------------------------------------------------------------------------------------
22
23 void interprete(const atlas::array::Array& a, atlas::io::ArrayReference& out) {
24 out = io::ArrayReference(a.data(), atlas::io::DataType(a.datatype().str()), a.shape());
25 }
26
27 //---------------------------------------------------------------------------------------------------------------------
28
29 void decode(const atlas::io::Metadata& metadata, const atlas::io::Data& data, atlas::array::Array& out) {
30 atlas::io::ArrayMetadata array(metadata);
31
32 if (array.datatype().str() != out.datatype().str()) {
33 std::stringstream err;
34 err << "Could not decode " << metadata.json() << " into Array with datatype " << out.datatype().str() << "."
35 << "Incompatible datatype!";
36 throw atlas::io::Exception(err.str(), Here());
37 }
38 if (array.rank() != out.rank()) {
39 std::stringstream err;
40 err << "Could not decode " << metadata.json() << " into Array with rank " << out.rank() << "."
41 << "Incompatible rank!";
42 throw atlas::io::Exception(err.str(), Here());
43 }
44
45 out.resize(array.shape());
46
47 ATLAS_IO_ASSERT(out.contiguous());
48
49 ::memcpy(out.data(), data, data.size());
50 }
51
52 //---------------------------------------------------------------------------------------------------------------------
53
54 } // namespace array
55 } // namespace atlas