|
| 1 | +/* |
| 2 | + * (C) Copyright 2017- 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 nor |
| 8 | + * does it submit to any jurisdiction. |
| 9 | + */ |
| 10 | + |
| 11 | +#include "metkit/codes/CodesDataContent.h" |
| 12 | + |
| 13 | +#include "eccodes.h" |
| 14 | + |
| 15 | +#include "eckit/exception/Exceptions.h" |
| 16 | +#include "eckit/io/DataHandle.h" |
| 17 | +#include "eckit/io/MemoryHandle.h" |
| 18 | + |
| 19 | +#include "metkit/codes/GribHandle.h" |
| 20 | + |
| 21 | +namespace metkit { |
| 22 | +namespace codes { |
| 23 | + |
| 24 | +//---------------------------------------------------------------------------------------------------------------------- |
| 25 | + |
| 26 | +CodesDataContent::CodesDataContent(std::unique_ptr<CodesHandle> handle) : |
| 27 | + handle_(std::move(handle)), offset_(handle_->getLong("offset")) {} |
| 28 | + |
| 29 | + |
| 30 | +CodesDataContent::CodesDataContent(std::unique_ptr<CodesHandle> handle, eckit::Offset offset) : |
| 31 | + handle_(std::move(handle)), offset_(std::move(offset)) {} |
| 32 | + |
| 33 | + |
| 34 | +//---------------------------------------------------------------------------------------------------------------------- |
| 35 | + |
| 36 | +size_t CodesDataContent::length() const { |
| 37 | + return handle_->messageSize(); |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +//---------------------------------------------------------------------------------------------------------------------- |
| 42 | + |
| 43 | +void CodesDataContent::write(eckit::DataHandle& handle) const { |
| 44 | + auto data = handle_->messageData(); |
| 45 | + if (handle.write(data.data(), data.size()) != data.size()) { |
| 46 | + std::ostringstream oss; |
| 47 | + oss << "Write error to data handle " << handle; |
| 48 | + throw eckit::WriteError(oss.str(), Here()); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +//---------------------------------------------------------------------------------------------------------------------- |
| 54 | + |
| 55 | +eckit::DataHandle* CodesDataContent::readHandle() const { |
| 56 | + auto data = handle_->messageData(); |
| 57 | + return new eckit::MemoryHandle(data.data(), data.size()); |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +//---------------------------------------------------------------------------------------------------------------------- |
| 62 | + |
| 63 | +void CodesDataContent::print(std::ostream& s) const { |
| 64 | + s << "CodesDataContent[]"; |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +//---------------------------------------------------------------------------------------------------------------------- |
| 69 | + |
| 70 | +std::string CodesDataContent::getString(const std::string& key) const { |
| 71 | + return handle_->getString(key); |
| 72 | +} |
| 73 | + |
| 74 | +//---------------------------------------------------------------------------------------------------------------------- |
| 75 | + |
| 76 | +long CodesDataContent::getLong(const std::string& key) const { |
| 77 | + return handle_->getLong(key); |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +//---------------------------------------------------------------------------------------------------------------------- |
| 82 | + |
| 83 | +double CodesDataContent::getDouble(const std::string& key) const { |
| 84 | + return handle_->getDouble(key); |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +//---------------------------------------------------------------------------------------------------------------------- |
| 89 | + |
| 90 | +void CodesDataContent::getDoubleArray(const std::string& key, std::vector<double>& values) const { |
| 91 | + values = handle_->getDoubleArray(key); |
| 92 | +} |
| 93 | + |
| 94 | +//---------------------------------------------------------------------------------------------------------------------- |
| 95 | + |
| 96 | +void CodesDataContent::getFloatArray(const std::string& key, std::vector<float>& values) const { |
| 97 | + values = handle_->getFloatArray(key); |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +//---------------------------------------------------------------------------------------------------------------------- |
| 102 | + |
| 103 | +size_t CodesDataContent::getSize(const std::string& key) const { |
| 104 | + return handle_->size(key); |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +//---------------------------------------------------------------------------------------------------------------------- |
| 109 | + |
| 110 | +void CodesDataContent::getDoubleArray(const std::string& key, double* data, size_t len) const { |
| 111 | + auto arr = handle_->getDoubleArray(key); |
| 112 | + ASSERT(len == arr.size()); |
| 113 | + std::copy(arr.begin(), arr.end(), data); |
| 114 | +} |
| 115 | + |
| 116 | +//---------------------------------------------------------------------------------------------------------------------- |
| 117 | + |
| 118 | +void CodesDataContent::getFloatArray(const std::string& key, float* data, size_t len) const { |
| 119 | + auto arr = handle_->getFloatArray(key); |
| 120 | + ASSERT(len == arr.size()); |
| 121 | + std::copy(arr.begin(), arr.end(), data); |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +//---------------------------------------------------------------------------------------------------------------------- |
| 126 | + |
| 127 | +void CodesDataContent::transform(const eckit::OrderedStringDict& dict) { |
| 128 | + for (auto& [key, value] : dict) { |
| 129 | + handle_->set(key, value); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +//---------------------------------------------------------------------------------------------------------------------- |
| 134 | + |
| 135 | +eckit::Offset CodesDataContent::offset() const { |
| 136 | + return offset_; |
| 137 | +} |
| 138 | + |
| 139 | +//---------------------------------------------------------------------------------------------------------------------- |
| 140 | + |
| 141 | +const void* CodesDataContent::data() const { |
| 142 | + return handle_->messageData().data(); |
| 143 | +} |
| 144 | + |
| 145 | +//---------------------------------------------------------------------------------------------------------------------- |
| 146 | + |
| 147 | +const CodesHandle& CodesDataContent::codesHandle() const { |
| 148 | + return *handle_.get(); |
| 149 | +} |
| 150 | + |
| 151 | +CodesHandle& CodesDataContent::codesHandle() { |
| 152 | + return *handle_.get(); |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | +} // namespace codes |
| 157 | +} // namespace metkit |
0 commit comments