Debian Codegraph @

src/atlas/trans/ifs/LegendreCacheCreatorIFS.cc

raw
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/trans/ifs/LegendreCacheCreatorIFS.h"
12
13 #include <cmath>
14 #include <sstream>
15 #include <string>
16
17 #include "eckit/utils/MD5.h"
18
19 #include "atlas/grid.h"
20 #include "atlas/option.h"
21 #include "atlas/runtime/Exception.h"
22 #include "atlas/trans/Trans.h"
23
24 namespace atlas {
25 namespace trans {
26
27 namespace {
28 static LegendreCacheCreatorBuilder<LegendreCacheCreatorIFS> builder_ifs("ifs"); // Deprecated
29 static LegendreCacheCreatorBuilder<LegendreCacheCreatorIFS> builder_ectrans("ectrans");
30 } // namespace
31
32 namespace {
33
34 std::string truncate(const std::string& str) {
35 const int trunc = std::min(10ul, str.size());
36 return str.substr(0, trunc);
37 }
38
39 std::string hash(const Grid& grid) {
40 eckit::MD5 h;
41
42 StructuredGrid structured(grid);
43 if (structured && not grid.projection()) {
44 for (auto& y : structured.y()) {
45 h.add(std::lround(y * 1.e8));
46 }
47 }
48 else {
49 grid.hash(h);
50 }
51 return truncate(h.digest());
52 }
53
54 std::string hash_pl(const Grid& grid) {
55 eckit::MD5 h;
56
57 StructuredGrid structured(grid);
58 ATLAS_ASSERT(structured);
59
60 for (auto& n : structured.nx()) {
61 h.add(long(n));
62 }
63
64 return truncate(h.digest());
65 }
66
67 std::string hash(const eckit::Configuration& config) {
68 eckit::MD5 h;
69
70 // Add options and other unique keys
71 h << "flt" << config.getBool("flt", false);
72
73 return truncate(h.digest());
74 }
75
76 } // namespace
77
78 std::string LegendreCacheCreatorIFS::uid() const {
79 if (unique_identifier_.empty()) {
80 std::ostringstream stream;
81 stream << "ifs-T" << truncation_ << "-";
82 GaussianGrid gaussian(grid_);
83 if (gaussian) {
84 if (RegularGaussianGrid(grid_)) {
85 stream << "RegularGaussianN" << gaussian.N();
86 }
87 else {
88 stream << "ReducedGaussianN" << gaussian.N() << "-PL" << hash_pl(grid_);
89 }
90 }
91 else if (RegularLonLatGrid(grid_)) {
92 auto g = RegularLonLatGrid(grid_);
93 if (g.standard() || g.shifted()) {
94 stream << (g.standard() ? "L" : "S") << g.nx() << "x" << g.ny();
95 }
96 else {
97 // We cannot make more assumptions on reusability for different grids
98 stream << "grid-" << hash(grid_);
99 }
100 }
101 else {
102 // We cannot make more assumptions on reusability for different grids
103 stream << "grid-" << hash(grid_);
104 }
105 stream << "-OPT" << hash(config_);
106 unique_identifier_ = stream.str();
107 }
108 return unique_identifier_;
109 }
110
111 LegendreCacheCreatorIFS::~LegendreCacheCreatorIFS() = default;
112
113 bool LegendreCacheCreatorIFS::supported() const {
114 if (GaussianGrid(grid_)) {
115 return true;
116 }
117 else if (RegularLonLatGrid(grid_)) {
118 auto g = RegularLonLatGrid(grid_);
119 if (g.standard() || g.shifted()) {
120 return true;
121 }
122 }
123 return false;
124 }
125
126 LegendreCacheCreatorIFS::LegendreCacheCreatorIFS(const Grid& grid, int truncation, const eckit::Configuration& config):
127 grid_(grid), truncation_(truncation), config_(config) {}
128
129 void LegendreCacheCreatorIFS::create(const std::string& path) const {
130 Trans(grid_, truncation_, config_ | option::type("ectrans") | option::write_legendre(path));
131 }
132
133 Cache LegendreCacheCreatorIFS::create() const {
134 return TransCache(Trans(grid_, truncation_, config_ | option::type("ectrans")));
135 }
136
137 size_t LegendreCacheCreatorIFS::estimate() const {
138 return size_t(truncation_ * truncation_ * truncation_) / 2 * sizeof(double);
139 }
140
141
142 } // namespace trans
143 } // namespace atlas