| 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 |
/// @author Slavko Brdar |
| 12 |
/// @author Willem Deconinck |
| 13 |
/// @date September 2024 |
| 14 |
|
| 15 |
#include <cmath> |
| 16 |
#include <sstream> |
| 17 |
|
| 18 |
#include "atlas/array/ArrayDataStore.h" |
| 19 |
#include "atlas/array/DataType.h" |
| 20 |
#include "atlas/array/Range.h" |
| 21 |
#include "atlas/field/MultiFieldCreatorArray.h" |
| 22 |
#include "atlas/field/detail/MultiFieldImpl.h" |
| 23 |
#include "atlas/grid/Grid.h" |
| 24 |
#include "atlas/runtime/Exception.h" |
| 25 |
#include "atlas/runtime/Log.h" |
| 26 |
|
| 27 |
namespace atlas { |
| 28 |
namespace field { |
| 29 |
|
| 30 |
MultiFieldCreatorArray::MultiFieldCreatorArray() = default; |
| 31 |
|
| 32 |
MultiFieldCreatorArray::MultiFieldCreatorArray(const eckit::Configuration&) {} |
| 33 |
|
| 34 |
MultiFieldCreatorArray::~MultiFieldCreatorArray() = default; |
| 35 |
|
| 36 |
MultiFieldImpl* MultiFieldCreatorArray::create(const eckit::Configuration& config) const { |
| 37 |
array::DataType datatype = array::DataType::create<double>(); |
| 38 |
std::string datatype_str; |
| 39 |
if (config.get("datatype", datatype_str)) { |
| 40 |
datatype = array::DataType(datatype_str); |
| 41 |
} |
| 42 |
else { |
| 43 |
array::DataType::kind_t kind(array::DataType::kind<double>()); |
| 44 |
config.get("kind", kind); |
| 45 |
if (!array::DataType::kind_valid(kind)) { |
| 46 |
std::stringstream msg; |
| 47 |
msg << "Could not create field. kind parameter unrecognized"; |
| 48 |
throw_Exception(msg.str()); |
| 49 |
} |
| 50 |
datatype = array::DataType(kind); |
| 51 |
} |
| 52 |
std::vector<int> shape; |
| 53 |
config.get("shape", shape); |
| 54 |
const auto fields = config.getSubConfigurations("fields"); |
| 55 |
int nflds = 0; |
| 56 |
for (size_t i = 0; i < fields.size(); ++i) { |
| 57 |
long nvar = 1; |
| 58 |
fields[i].get("nvar", nvar); |
| 59 |
nflds += nvar; |
| 60 |
} |
| 61 |
std::vector<std::string> var_names; |
| 62 |
var_names.resize(nflds); |
| 63 |
for (size_t i = 0, cnt = 0; i < fields.size(); ++i) { |
| 64 |
std::string name; |
| 65 |
fields[i].get("name", name); |
| 66 |
long nvar = 1; |
| 67 |
fields[i].get("nvar", nvar); |
| 68 |
if (nvar > 1) { |
| 69 |
for (int ivar = 0; ivar < nvar; ivar++) { |
| 70 |
std::stringstream ss; |
| 71 |
ss << name << "_" << ivar; |
| 72 |
var_names[cnt++] = ss.str(); |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
else { |
| 77 |
var_names[cnt++] = name; |
| 78 |
} |
| 79 |
} |
| 80 |
return create(datatype, shape, var_names); |
| 81 |
} |
| 82 |
|
| 83 |
MultiFieldImpl* MultiFieldCreatorArray::create(const array::DataType datatype, const array::ArrayShape& shape, const std::vector<std::string>& var_names) const { |
| 84 |
const int dim = shape.size(); |
| 85 |
const int nvar = var_names.size(); |
| 86 |
ATLAS_ASSERT(nvar > 0 && dim > 2, "MultiField must have at least one field name."); |
| 87 |
|
| 88 |
int varidx = -1; |
| 89 |
for (int i = 0; i < dim; i++) { |
| 90 |
if (shape[i] == -1) { |
| 91 |
varidx = i; |
| 92 |
break; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
array::ArrayShape multiarray_shape = shape; |
| 97 |
multiarray_shape[varidx] = nvar; |
| 98 |
|
| 99 |
MultiFieldImpl* multifield = new MultiFieldImpl{array::ArraySpec{datatype, multiarray_shape}}; |
| 100 |
auto& multiarray = multifield->array(); |
| 101 |
|
| 102 |
array::ArrayShape field_shape; |
| 103 |
field_shape.resize(dim - 1); |
| 104 |
array::ArrayStrides field_strides; |
| 105 |
field_strides.resize(dim - 1); |
| 106 |
for (int i = 0, j = 0; i < dim; i++) { |
| 107 |
if (i != varidx) { |
| 108 |
field_shape[j] = multiarray.shape()[i]; |
| 109 |
field_strides[j] = multiarray.strides()[i]; |
| 110 |
++j; |
| 111 |
} |
| 112 |
} |
| 113 |
array::ArraySpec field_array_spec(field_shape, field_strides); |
| 114 |
|
| 115 |
for (size_t ifield = 0; ifield < nvar; ++ifield) { |
| 116 |
idx_t start_index = multiarray.strides()[varidx] * ifield; |
| 117 |
Field field; |
| 118 |
if (datatype.kind() == array::DataType::KIND_REAL64) { |
| 119 |
double* slice_begin = multiarray.data<double>() + start_index; |
| 120 |
field = Field(var_names[ifield], slice_begin, field_array_spec); |
| 121 |
} |
| 122 |
else if (datatype.kind() == array::DataType::KIND_REAL32) { |
| 123 |
float* slice_begin = multiarray.data<float>() + start_index; |
| 124 |
field = Field(var_names[ifield], slice_begin, field_array_spec); |
| 125 |
} |
| 126 |
else if (datatype.kind() == array::DataType::KIND_INT32) { |
| 127 |
int* slice_begin = multiarray.data<int>() + start_index; |
| 128 |
field = Field(var_names[ifield], slice_begin, field_array_spec); |
| 129 |
} |
| 130 |
else if (datatype.kind() == array::DataType::KIND_INT64) { |
| 131 |
long* slice_begin = multiarray.data<long>() + start_index; |
| 132 |
field = Field(var_names[ifield], slice_begin, field_array_spec); |
| 133 |
} |
| 134 |
else { |
| 135 |
ATLAS_NOTIMPLEMENTED; |
| 136 |
} |
| 137 |
multifield->add(field); |
| 138 |
} |
| 139 |
Log::debug() << "Creating multifield of " << datatype.str() << " type" << std::endl; |
| 140 |
return multifield; |
| 141 |
} |
| 142 |
|
| 143 |
// ------------------------------------------------------------------ |
| 144 |
|
| 145 |
namespace { |
| 146 |
static MultiFieldCreatorBuilder<MultiFieldCreatorArray> __MultiFieldCreatorArray("MultiFieldCreatorArray"); |
| 147 |
} |
| 148 |
|
| 149 |
} // namespace field |
| 150 |
} // namespace atlas |