| 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/local/LegendreCacheCreatorLocal.h" |
| 12 |
|
| 13 |
#include <cmath> |
| 14 |
#include <sstream> |
| 15 |
#include <string> |
| 16 |
|
| 17 |
#include "eckit/types/FloatCompare.h" |
| 18 |
#include "eckit/utils/MD5.h" |
| 19 |
|
| 20 |
#include "atlas/grid.h" |
| 21 |
#include "atlas/option.h" |
| 22 |
#include "atlas/runtime/Exception.h" |
| 23 |
#include "atlas/trans/Trans.h" |
| 24 |
#include "atlas/trans/local/TransLocal.h" |
| 25 |
|
| 26 |
namespace atlas { |
| 27 |
namespace trans { |
| 28 |
|
| 29 |
namespace { |
| 30 |
static LegendreCacheCreatorBuilder<LegendreCacheCreatorLocal> builder("local"); |
| 31 |
} |
| 32 |
|
| 33 |
namespace { |
| 34 |
|
| 35 |
std::string truncate(const std::string& str) { |
| 36 |
const int trunc = std::min(10ul, str.size()); |
| 37 |
return str.substr(0, trunc); |
| 38 |
} |
| 39 |
|
| 40 |
std::string hash(const Grid& grid) { |
| 41 |
eckit::MD5 h; |
| 42 |
|
| 43 |
StructuredGrid structured(grid); |
| 44 |
if (structured && not grid.projection()) { |
| 45 |
for (auto& y : structured.y()) { |
| 46 |
h.add(std::lround(y * 1.e8)); |
| 47 |
} |
| 48 |
} |
| 49 |
else { |
| 50 |
grid.hash(h); |
| 51 |
} |
| 52 |
return truncate(h.digest()); |
| 53 |
} |
| 54 |
|
| 55 |
std::string hash(const eckit::Configuration& config) { |
| 56 |
eckit::MD5 h; |
| 57 |
|
| 58 |
// Add options and other unique keys |
| 59 |
h << "flt" << config.getBool("flt", false); |
| 60 |
|
| 61 |
return truncate(h.digest()); |
| 62 |
} |
| 63 |
|
| 64 |
} // namespace |
| 65 |
|
| 66 |
std::string LegendreCacheCreatorLocal::uid() const { |
| 67 |
if (unique_identifier_.empty()) { |
| 68 |
std::ostringstream stream; |
| 69 |
auto give_up = [&]() { |
| 70 |
// We cannot make more assumptions on reusability for different grids |
| 71 |
stream << "grid-" << hash(grid_); |
| 72 |
}; |
| 73 |
stream << "local-T" << truncation_ << "-"; |
| 74 |
StructuredGrid structured(grid_); |
| 75 |
if (grid_.projection()) { |
| 76 |
give_up(); |
| 77 |
} |
| 78 |
else if (GaussianGrid(grid_)) { |
| 79 |
// Same cache for any global Gaussian grid |
| 80 |
stream << "GaussianN" << GaussianGrid(grid_).N(); |
| 81 |
} |
| 82 |
else if (RegularLonLatGrid(grid_)) { |
| 83 |
// Same cache for any global regular grid |
| 84 |
auto g = RegularLonLatGrid(grid_); |
| 85 |
|
| 86 |
const double dy_2 = 90. / double(g.ny()); |
| 87 |
bool shifted_lat = eckit::types::is_approximately_equal(g.y().front(), 90. - dy_2) && |
| 88 |
eckit::types::is_approximately_equal(g.y().back(), -90. + dy_2); |
| 89 |
bool standard_lat = eckit::types::is_approximately_equal(g.y().front(), 90.) && |
| 90 |
eckit::types::is_approximately_equal(g.y().back(), -90.); |
| 91 |
|
| 92 |
if (standard_lat) { |
| 93 |
stream << "L" |
| 94 |
<< "-ny" << g.ny(); |
| 95 |
} |
| 96 |
else if (shifted_lat) { |
| 97 |
stream << "S" |
| 98 |
<< "-ny" << g.ny(); |
| 99 |
} |
| 100 |
else { // I don't think we get here, but just in case, give up |
| 101 |
give_up(); |
| 102 |
} |
| 103 |
} |
| 104 |
else if (RegularGrid(grid_) && structured.yspace().type() == "linear") { |
| 105 |
RectangularDomain domain(grid_.domain()); |
| 106 |
ATLAS_ASSERT(domain); |
| 107 |
stream << "Regional"; |
| 108 |
stream << "-south" << domain.ymin(); |
| 109 |
stream << "-north" << domain.ymax(); |
| 110 |
stream << "-ny" << structured.ny(); |
| 111 |
} |
| 112 |
else { // It gets too complicated, so let's not be smart |
| 113 |
give_up(); |
| 114 |
} |
| 115 |
stream << "-OPT" << hash(config_); |
| 116 |
unique_identifier_ = stream.str(); |
| 117 |
} |
| 118 |
return unique_identifier_; |
| 119 |
} |
| 120 |
|
| 121 |
LegendreCacheCreatorLocal::~LegendreCacheCreatorLocal() = default; |
| 122 |
|
| 123 |
LegendreCacheCreatorLocal::LegendreCacheCreatorLocal(const Grid& grid, int truncation, |
| 124 |
const eckit::Configuration& config): |
| 125 |
grid_(grid), truncation_(truncation), config_(config) {} |
| 126 |
|
| 127 |
bool LegendreCacheCreatorLocal::supported() const { |
| 128 |
if (not StructuredGrid(grid_)) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
if (grid_.projection()) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
void LegendreCacheCreatorLocal::create(const std::string& path) const { |
| 138 |
Trans tmp(grid_, truncation_, config_ | option::type("local") | option::write_legendre(path)); |
| 139 |
} |
| 140 |
|
| 141 |
Cache LegendreCacheCreatorLocal::create() const { |
| 142 |
util::Config export_legendre("export_legendre", true); |
| 143 |
Trans tmp(grid_, truncation_, config_ | option::type("local") | export_legendre); |
| 144 |
auto impl = dynamic_cast<const TransLocal*>(tmp.get()); |
| 145 |
return impl->export_legendre_; |
| 146 |
} |
| 147 |
|
| 148 |
size_t LegendreCacheCreatorLocal::estimate() const { |
| 149 |
return size_t(truncation_ * truncation_ * truncation_) / 2 * sizeof(double); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
} // namespace trans |
| 154 |
} // namespace atlas |