| 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 <algorithm> |
| 12 |
#include <vector> |
| 13 |
#include <string> |
| 14 |
#include <string_view> |
| 15 |
|
| 16 |
#include "atlas/library/config.h" |
| 17 |
#include "atlas/field/MultiField.h" |
| 18 |
#include "atlas/field/detail/MultiFieldImpl.h" |
| 19 |
#include "atlas/field/detail/MultiFieldInterface.h" |
| 20 |
#include "atlas/runtime/Exception.h" |
| 21 |
|
| 22 |
namespace atlas { |
| 23 |
namespace field { |
| 24 |
|
| 25 |
extern "C" { |
| 26 |
MultiFieldImpl* atlas__MultiField__create(eckit::Configuration* config) { |
| 27 |
ATLAS_ASSERT(config != nullptr); |
| 28 |
MultiFieldImpl* multifield; |
| 29 |
{ |
| 30 |
MultiField f(*config); |
| 31 |
multifield = f.get(); |
| 32 |
multifield->attach(); |
| 33 |
} |
| 34 |
multifield->detach(); |
| 35 |
ATLAS_ASSERT(multifield); |
| 36 |
return multifield; |
| 37 |
} |
| 38 |
|
| 39 |
MultiFieldImpl* atlas__MultiField__create_shape(int kind, int rank, int shapef[], const char* var_names, |
| 40 |
size_t length, size_t size) { |
| 41 |
array::ArrayShape shape; |
| 42 |
shape.resize(rank); |
| 43 |
for (idx_t j = 0, jf = rank - 1; j < rank; ++j) { |
| 44 |
shape[j] = shapef[jf--]; |
| 45 |
} |
| 46 |
|
| 47 |
std::vector<std::string> var_names_str; |
| 48 |
for (size_t jj = 0; jj < size; ++jj) { |
| 49 |
std::string str(std::string_view(var_names + jj * length, length)); |
| 50 |
str.erase(std::find_if(str.rbegin(), str.rend(), [](unsigned char ch) { |
| 51 |
return !std::isspace(ch); |
| 52 |
}).base(), str.end()); |
| 53 |
var_names_str.push_back(str); |
| 54 |
} |
| 55 |
|
| 56 |
MultiFieldImpl* multifield; |
| 57 |
{ |
| 58 |
MultiField f(array::DataType{kind}, shape, var_names_str); |
| 59 |
multifield = f.get(); |
| 60 |
multifield->attach(); |
| 61 |
} |
| 62 |
multifield->detach(); |
| 63 |
ATLAS_ASSERT(multifield); |
| 64 |
return multifield; |
| 65 |
} |
| 66 |
|
| 67 |
void atlas__MultiField__delete(MultiFieldImpl* This) { |
| 68 |
delete This; |
| 69 |
} |
| 70 |
|
| 71 |
int atlas__MultiField__size(MultiFieldImpl* This) { |
| 72 |
return This->size(); |
| 73 |
} |
| 74 |
|
| 75 |
FieldSetImpl* atlas__MultiField__fieldset(MultiFieldImpl* This) { |
| 76 |
return This->fieldset().get(); |
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
// ------------------------------------------------------------------ |
| 82 |
|
| 83 |
} // namespace field |
| 84 |
} // namespace atlas |