| 1 |
/* |
| 2 |
* (C) Copyright 2013 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 "atlas/field/FieldCreatorArraySpec.h" |
| 12 |
|
| 13 |
#include <algorithm> |
| 14 |
#include <sstream> |
| 15 |
|
| 16 |
#include "eckit/config/Parametrisation.h" |
| 17 |
|
| 18 |
#include "atlas/array/DataType.h" |
| 19 |
#include "atlas/field/detail/FieldImpl.h" |
| 20 |
#include "atlas/runtime/Exception.h" |
| 21 |
#include "atlas/runtime/Log.h" |
| 22 |
|
| 23 |
namespace atlas { |
| 24 |
namespace field { |
| 25 |
|
| 26 |
FieldImpl* FieldCreatorArraySpec::createField(const eckit::Parametrisation& params) const { |
| 27 |
std::vector<long> shape; |
| 28 |
if (!params.get("shape", shape)) { |
| 29 |
throw_Exception("Could not find parameter 'shape' in Parametrisation"); |
| 30 |
} |
| 31 |
|
| 32 |
std::vector<idx_t> s(shape.size()); |
| 33 |
|
| 34 |
bool fortran(false); |
| 35 |
params.get("fortran", fortran); |
| 36 |
if (fortran) { |
| 37 |
std::reverse_copy(shape.begin(), shape.end(), s.begin()); |
| 38 |
} |
| 39 |
else { |
| 40 |
s.assign(shape.begin(), shape.end()); |
| 41 |
} |
| 42 |
|
| 43 |
array::DataType datatype = array::DataType::create<double>(); |
| 44 |
std::string datatype_str; |
| 45 |
if (params.get("datatype", datatype_str)) { |
| 46 |
datatype = array::DataType(datatype_str); |
| 47 |
} |
| 48 |
else { |
| 49 |
array::DataType::kind_t kind(array::DataType::kind<double>()); |
| 50 |
params.get("kind", kind); |
| 51 |
if (!array::DataType::kind_valid(kind)) { |
| 52 |
std::stringstream msg; |
| 53 |
msg << "Could not create field. kind parameter unrecognized"; |
| 54 |
throw_Exception(msg.str()); |
| 55 |
} |
| 56 |
datatype = array::DataType(kind); |
| 57 |
} |
| 58 |
|
| 59 |
int alignment = 1; |
| 60 |
params.get("alignment", alignment); |
| 61 |
|
| 62 |
std::string name; |
| 63 |
params.get("name", name); |
| 64 |
Log::trace() << "Create field " << name << "\t shape=["; |
| 65 |
for (size_t i = 0; i < s.size(); ++i) { |
| 66 |
Log::trace() << s[i] << (i < s.size() - 1 ? "," : ""); |
| 67 |
} |
| 68 |
Log::trace() << "]" << std::endl; |
| 69 |
auto field = FieldImpl::create(name, datatype, array::ArraySpec(std::move(s), array::ArrayAlignment(alignment))); |
| 70 |
field->callbackOnDestruction([field]() { Log::trace() << "Destroy field " << field->name() << std::endl; }); |
| 71 |
return field; |
| 72 |
} |
| 73 |
|
| 74 |
namespace { |
| 75 |
static FieldCreatorBuilder<FieldCreatorArraySpec> __ArraySpec("ArraySpec"); |
| 76 |
} |
| 77 |
|
| 78 |
// ------------------------------------------------------------------ |
| 79 |
|
| 80 |
} // namespace field |
| 81 |
} // namespace atlas |