Skip to content

Commit

Permalink
Add atlas::io support for std::vector<std::array>
Browse files Browse the repository at this point in the history
  • Loading branch information
wdeconinck committed Oct 9, 2023
1 parent 128ab3e commit a14961f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions atlas_io/src/atlas_io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ ecbuild_add_library( TARGET atlas_io
types/array/ArrayReference.h
types/array/adaptors/StdArrayAdaptor.h
types/array/adaptors/StdVectorAdaptor.h
types/array/adaptors/StdVectorOfStdArrayAdaptor.h
types/string.h
types/scalar.h
types/scalar.cc
Expand Down
1 change: 1 addition & 0 deletions atlas_io/src/atlas_io/types/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
#include "atlas_io/types/array/ArrayReference.h"
#include "atlas_io/types/array/adaptors/StdArrayAdaptor.h"
#include "atlas_io/types/array/adaptors/StdVectorAdaptor.h"
#include "atlas_io/types/array/adaptors/StdVectorOfStdArrayAdaptor.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* (C) Copyright 2020 ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation
* nor does it submit to any jurisdiction.
*/

#pragma once

#include <vector>
#include <array>

#include "atlas_io/Data.h"
#include "atlas_io/Exceptions.h"
#include "atlas_io/Metadata.h"
#include "atlas_io/types/array/ArrayMetadata.h"
#include "atlas_io/types/array/ArrayReference.h"

namespace std {

//---------------------------------------------------------------------------------------------------------------------

template <typename T, size_t N>
void interprete(const std::vector<std::array<T, N>>& vector_of_array, atlas::io::ArrayReference& out) {
using atlas::io::ArrayReference;
out = ArrayReference{vector_of_array.front().data(), {vector_of_array.size(),N}};
}

//---------------------------------------------------------------------------------------------------------------------

template <typename T, size_t N>
void decode(const atlas::io::Metadata& m, const atlas::io::Data& encoded, std::vector<std::array<T, N>>& out) {
atlas::io::ArrayMetadata array(m);
if (array.datatype().kind() != atlas::io::ArrayMetadata::DataType::kind<T>()) {
std::stringstream err;
err << "Could not decode " << m.json() << " into std::vector<" << atlas::io::demangle<T>() << ">. "
<< "Incompatible datatype!";
throw atlas::io::Exception(err.str(), Here());
}
if (array.rank() != 2) {
std::stringstream err;
err << "Could not decode " << m.json() << " into std::vector<std::array<" << atlas::io::demangle<T>() << "," << N << ">>. "
<< "Incompatible rank!";
throw atlas::io::Exception(err.str(), Here());
}
if (array.shape(1) != N) {
std::stringstream err;
err << "Could not decode " << m.json() << " into std::vector<std::array<" << atlas::io::demangle<T>() << "," << N << ">>. "
<< "Incompatible size!";
throw atlas::io::Exception(err.str(), Here());
}
const std::array<T,N>* data = static_cast<const std::array<T,N>*>(encoded.data());
// std::copy(data, data + array.shape(0), out.begin());
out.assign(data, data + array.shape(0));

}

//---------------------------------------------------------------------------------------------------------------------








} // end namespace std

0 comments on commit a14961f

Please sign in to comment.