| 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 Willem Deconinck |
| 12 |
/// @author Slavko Brdar |
| 13 |
/// @date June 2024 |
| 14 |
|
| 15 |
#include <cmath> |
| 16 |
#include <sstream> |
| 17 |
|
| 18 |
#include "eckit/config/Parametrisation.h" |
| 19 |
|
| 20 |
#include "atlas/array/ArrayDataStore.h" |
| 21 |
#include "atlas/array/DataType.h" |
| 22 |
#include "atlas/field/MultiFieldCreatorIFS.h" |
| 23 |
#include "atlas/field/detail/MultiFieldImpl.h" |
| 24 |
#include "atlas/grid/Grid.h" |
| 25 |
#include "atlas/runtime/Exception.h" |
| 26 |
#include "atlas/runtime/Log.h" |
| 27 |
|
| 28 |
namespace atlas { |
| 29 |
namespace field { |
| 30 |
|
| 31 |
MultiFieldCreatorIFS::MultiFieldCreatorIFS() = default; |
| 32 |
|
| 33 |
MultiFieldCreatorIFS::MultiFieldCreatorIFS(const eckit::Configuration& config) {} |
| 34 |
|
| 35 |
MultiFieldCreatorIFS::~MultiFieldCreatorIFS() = default; |
| 36 |
|
| 37 |
MultiFieldImpl* MultiFieldCreatorIFS::create(const array::DataType datatype, const array::ArrayShape& shape, |
| 38 |
const std::vector<std::string>& var_names) const { |
| 39 |
ATLAS_NOTIMPLEMENTED; |
| 40 |
return nullptr; |
| 41 |
} |
| 42 |
|
| 43 |
MultiFieldImpl* MultiFieldCreatorIFS::create(const eckit::Configuration& config) const { |
| 44 |
long nproma; |
| 45 |
config.get("nproma", nproma); |
| 46 |
long nlev; |
| 47 |
config.get("nlev", nlev); |
| 48 |
long nblk = 0; |
| 49 |
if (config.has("nblk")) { |
| 50 |
config.get("nblk", nblk); |
| 51 |
} |
| 52 |
else if (config.has("ngptot")) { |
| 53 |
long ngptot; |
| 54 |
config.get("ngptot", ngptot); |
| 55 |
nblk = std::ceil(static_cast<double>(ngptot) / static_cast<double>(nproma)); |
| 56 |
} |
| 57 |
else { |
| 58 |
ATLAS_THROW_EXCEPTION("Configuration not found: ngptot or nblk"); |
| 59 |
} |
| 60 |
array::DataType datatype = array::DataType::create<double>(); |
| 61 |
std::string datatype_str; |
| 62 |
if (config.get("datatype", datatype_str)) { |
| 63 |
datatype = array::DataType(datatype_str); |
| 64 |
} |
| 65 |
else { |
| 66 |
array::DataType::kind_t kind(array::DataType::kind<double>()); |
| 67 |
config.get("kind", kind); |
| 68 |
if (!array::DataType::kind_valid(kind)) { |
| 69 |
std::stringstream msg; |
| 70 |
msg << "Could not create field. kind parameter unrecognized"; |
| 71 |
throw_Exception(msg.str()); |
| 72 |
} |
| 73 |
datatype = array::DataType(kind); |
| 74 |
} |
| 75 |
|
| 76 |
auto fields = config.getSubConfigurations("fields"); |
| 77 |
long nfld = 0; |
| 78 |
for (const auto& field_params : fields) { |
| 79 |
long nvar = 1; |
| 80 |
field_params.get("nvar", nvar); |
| 81 |
nfld += nvar; |
| 82 |
} |
| 83 |
array::ArrayShape multiarray_shape = ( (nlev > 0 and nfld > 0) ? array::make_shape(nblk, nfld, nlev, nproma) : |
| 84 |
( (nlev > 0) ? array::make_shape(nblk, nlev, nproma) : ( (nfld > 0) ? array::make_shape(nblk, nfld, nproma) : |
| 85 |
array::make_shape(nblk, nproma) ) ) ); |
| 86 |
|
| 87 |
MultiFieldImpl* multifield = new MultiFieldImpl{array::ArraySpec{datatype, multiarray_shape}}; |
| 88 |
auto& multiarray = multifield->array(); |
| 89 |
|
| 90 |
size_t multiarray_field_idx = 0; |
| 91 |
for (size_t i = 0; i < fields.size(); ++i) { |
| 92 |
std::string name; |
| 93 |
fields[i].get("name", name); |
| 94 |
Field field; |
| 95 |
size_t field_vars = 1; |
| 96 |
if (fields[i].get("nvar", field_vars)) { |
| 97 |
array::ArrayShape field_shape = |
| 98 |
( (nlev > 0 and field_vars > 0) ? |
| 99 |
array::make_shape(multiarray.shape(0), field_vars, multiarray.shape(2), multiarray.shape(3)) : |
| 100 |
( nlev > 0 ? |
| 101 |
array::make_shape(multiarray.shape(0), multiarray.shape(1), multiarray.shape(2)) : |
| 102 |
( field_vars > 0 ? |
| 103 |
array::make_shape(multiarray.shape(0), field_vars, multiarray.shape(2)) : |
| 104 |
array::make_shape(multiarray.shape(0), multiarray.shape(1)) ) ) ); |
| 105 |
array::ArrayShape multiarray_shape = |
| 106 |
( (nlev > 0 and field_vars > 0) ? array::make_shape(nblk, field_vars, nlev, nproma) : |
| 107 |
( (nlev > 0) ? array::make_shape(nblk, nlev, nproma) : ( (field_vars > 0) ? |
| 108 |
array::make_shape(nblk, field_vars, nproma) : array::make_shape(nblk, nproma) ) ) ); |
| 109 |
auto field_strides = multiarray.strides(); |
| 110 |
auto field_array_spec = array::ArraySpec(field_shape, field_strides); |
| 111 |
|
| 112 |
constexpr auto all = array::Range::all(); |
| 113 |
const auto range = array::Range(multiarray_field_idx, multiarray_field_idx + field_vars); |
| 114 |
if (datatype.kind() == array::DataType::KIND_REAL64) { |
| 115 |
if (nlev > 0 and field_vars > 0) { |
| 116 |
auto slice = array::make_view<double, 4>(multiarray).slice(all, range, all, all); |
| 117 |
field = Field(name, slice.data(), field_array_spec); |
| 118 |
} |
| 119 |
else if (nlev > 0) { |
| 120 |
auto slice = array::make_view<double, 3>(multiarray).slice(all, all, all); |
| 121 |
field = Field(name, slice.data(), field_array_spec); |
| 122 |
} |
| 123 |
else if (field_vars > 0) { |
| 124 |
auto slice = array::make_view<double, 3>(multiarray).slice(all, range, all); |
| 125 |
field = Field(name, slice.data(), field_array_spec); |
| 126 |
} |
| 127 |
else { |
| 128 |
auto slice = array::make_view<double, 2>(multiarray).slice(all, all); |
| 129 |
field = Field(name, slice.data(), field_array_spec); |
| 130 |
} |
| 131 |
} |
| 132 |
else if (datatype.kind() == array::DataType::KIND_REAL32) { |
| 133 |
if (nlev > 0 and field_vars > 0) { |
| 134 |
auto slice = array::make_view<float, 4>(multiarray).slice(all, range, all, all); |
| 135 |
field = Field(name, slice.data(), field_array_spec); |
| 136 |
} |
| 137 |
else if (nlev > 0) { |
| 138 |
auto slice = array::make_view<float, 3>(multiarray).slice(all, all, all); |
| 139 |
field = Field(name, slice.data(), field_array_spec); |
| 140 |
} |
| 141 |
else if (field_vars > 0) { |
| 142 |
auto slice = array::make_view<float, 3>(multiarray).slice(all, range, all); |
| 143 |
field = Field(name, slice.data(), field_array_spec); |
| 144 |
} |
| 145 |
else { |
| 146 |
auto slice = array::make_view<float, 2>(multiarray).slice(all, all); |
| 147 |
field = Field(name, slice.data(), field_array_spec); |
| 148 |
} |
| 149 |
} |
| 150 |
else { |
| 151 |
ATLAS_NOTIMPLEMENTED; |
| 152 |
} |
| 153 |
field.set_variables(field_vars); |
| 154 |
} |
| 155 |
else { |
| 156 |
array::ArraySpec field_array_spec; |
| 157 |
if (nlev > 0) { |
| 158 |
auto field_shape = array::make_shape(multiarray.shape(0), multiarray.shape(2), multiarray.shape(3)); |
| 159 |
auto field_strides = array::make_strides(multiarray.stride(0), multiarray.stride(2), multiarray.stride(3)); |
| 160 |
field_array_spec = array::ArraySpec(field_shape, field_strides); |
| 161 |
} |
| 162 |
else if (field_vars > 0) { |
| 163 |
auto field_shape = array::make_shape(multiarray.shape(0), multiarray.shape(2)); |
| 164 |
auto field_strides = array::make_strides(multiarray.stride(0), multiarray.stride(2)); |
| 165 |
field_array_spec = array::ArraySpec(field_shape, field_strides); |
| 166 |
} |
| 167 |
|
| 168 |
constexpr auto all = array::Range::all(); |
| 169 |
if (datatype.kind() == array::DataType::KIND_REAL64) { |
| 170 |
if (nlev > 0 and field_vars > 0) { |
| 171 |
auto slice = array::make_view<double, 4>(multiarray).slice(all, multiarray_field_idx, all, all); |
| 172 |
field = Field(name, slice.data(), field_array_spec); |
| 173 |
} |
| 174 |
else if (nlev > 0) { |
| 175 |
auto slice = array::make_view<double, 3>(multiarray).slice(all, all, all); |
| 176 |
field = Field(name, slice.data(), field_array_spec); |
| 177 |
} |
| 178 |
else if (field_vars > 0) { |
| 179 |
auto slice = array::make_view<double, 3>(multiarray).slice(all, multiarray_field_idx, all); |
| 180 |
field = Field(name, slice.data(), field_array_spec); |
| 181 |
} |
| 182 |
else { |
| 183 |
auto slice = array::make_view<double, 2>(multiarray).slice(all, all); |
| 184 |
field = Field(name, slice.data(), field_array_spec); |
| 185 |
} |
| 186 |
} |
| 187 |
else if (datatype.kind() == array::DataType::KIND_REAL32) { |
| 188 |
if (nlev > 0 and field_vars > 0) { |
| 189 |
auto slice = array::make_view<float, 4>(multiarray).slice(all, multiarray_field_idx, all, all); |
| 190 |
field = Field(name, slice.data(), field_array_spec); |
| 191 |
} |
| 192 |
else if (nlev > 0) { |
| 193 |
auto slice = array::make_view<float, 3>(multiarray).slice(all, all, all); |
| 194 |
field = Field(name, slice.data(), field_array_spec); |
| 195 |
} |
| 196 |
else if (field_vars > 0) { |
| 197 |
auto slice = array::make_view<float, 3>(multiarray).slice(all, multiarray_field_idx, all); |
| 198 |
field = Field(name, slice.data(), field_array_spec); |
| 199 |
} |
| 200 |
else { |
| 201 |
auto slice = array::make_view<float, 2>(multiarray).slice(all, all); |
| 202 |
field = Field(name, slice.data(), field_array_spec); |
| 203 |
} |
| 204 |
} |
| 205 |
else { |
| 206 |
ATLAS_NOTIMPLEMENTED; |
| 207 |
} |
| 208 |
} |
| 209 |
field.set_levels(nlev); |
| 210 |
//field.set_blocks(nblk); |
| 211 |
|
| 212 |
multifield->add(field); |
| 213 |
|
| 214 |
multiarray_field_idx += field_vars; |
| 215 |
} |
| 216 |
std::string name; |
| 217 |
config.get("name", name); |
| 218 |
Log::debug() << "Creating IFS " << datatype.str() << " multifield: " << name << "[nblk=" << nblk << "][nvar=" << nfld |
| 219 |
<< "][nlev=" << nlev << "][nproma=" << nproma << "]\n"; |
| 220 |
return multifield; |
| 221 |
} |
| 222 |
|
| 223 |
// ------------------------------------------------------------------ |
| 224 |
|
| 225 |
namespace { |
| 226 |
static MultiFieldCreatorBuilder<MultiFieldCreatorIFS> __MultiFieldCreatorIFS("MultiFieldCreatorIFS"); |
| 227 |
} |
| 228 |
|
| 229 |
} // namespace field |
| 230 |
} // namespace atlas |