| 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 |
// file deepcode ignore CppMemoryLeak: static pointers for global registry are OK and will be cleaned up at end |
| 12 |
|
| 13 |
#include <map> |
| 14 |
#include <string> |
| 15 |
|
| 16 |
#include "eckit/thread/AutoLock.h" |
| 17 |
#include "eckit/thread/Mutex.h" |
| 18 |
|
| 19 |
#include "atlas/field/Field.h" |
| 20 |
#include "atlas/field/FieldSet.h" |
| 21 |
#include "atlas/functionspace/FunctionSpace.h" |
| 22 |
#include "atlas/mesh/Mesh.h" |
| 23 |
#include "atlas/output/Gmsh.h" |
| 24 |
#include "atlas/output/Output.h" |
| 25 |
#include "atlas/runtime/Exception.h" |
| 26 |
#include "atlas/runtime/Log.h" |
| 27 |
|
| 28 |
using atlas::FieldSet; |
| 29 |
using atlas::FunctionSpace; |
| 30 |
using atlas::Mesh; |
| 31 |
using atlas::field::FieldImpl; |
| 32 |
using atlas::field::FieldSetImpl; |
| 33 |
|
| 34 |
namespace atlas { |
| 35 |
namespace output { |
| 36 |
|
| 37 |
static eckit::Mutex* local_mutex = nullptr; |
| 38 |
static std::map<std::string, detail::OutputFactory*>* m = nullptr; |
| 39 |
static pthread_once_t once = PTHREAD_ONCE_INIT; |
| 40 |
|
| 41 |
static void init() { |
| 42 |
local_mutex = new eckit::Mutex(); |
| 43 |
m = new std::map<std::string, detail::OutputFactory*>(); |
| 44 |
} |
| 45 |
|
| 46 |
namespace detail { |
| 47 |
|
| 48 |
OutputImpl::OutputImpl() = default; |
| 49 |
|
| 50 |
OutputImpl::~OutputImpl() = default; |
| 51 |
|
| 52 |
} // namespace detail |
| 53 |
|
| 54 |
Output::Output(const std::string& key, std::ostream& stream, const eckit::Parametrisation& params): |
| 55 |
Handle(detail::OutputFactory::build(key, stream, params)) {} |
| 56 |
|
| 57 |
/// Write mesh file |
| 58 |
const Output& Output::write(const Mesh& m, const eckit::Parametrisation& c) const { |
| 59 |
get()->write(m, c); |
| 60 |
return *this; |
| 61 |
} |
| 62 |
|
| 63 |
/// Write field to file |
| 64 |
const Output& Output::write(const Field& f, const eckit::Parametrisation& c) const { |
| 65 |
get()->write(f, c); |
| 66 |
return *this; |
| 67 |
} |
| 68 |
|
| 69 |
/// Write fieldset to file using FunctionSpace |
| 70 |
const Output& Output::write(const FieldSet& f, const eckit::Parametrisation& c) const { |
| 71 |
get()->write(f, c); |
| 72 |
return *this; |
| 73 |
} |
| 74 |
|
| 75 |
/// Write field to file using Functionspace |
| 76 |
const Output& Output::write(const Field& f, const FunctionSpace& fs, const eckit::Parametrisation& c) const { |
| 77 |
get()->write(f, fs, c); |
| 78 |
return *this; |
| 79 |
} |
| 80 |
|
| 81 |
/// Write fieldset to file using FunctionSpace |
| 82 |
const Output& Output::write(const FieldSet& f, const FunctionSpace& fs, const eckit::Parametrisation& c) const { |
| 83 |
get()->write(f, fs, c); |
| 84 |
return *this; |
| 85 |
} |
| 86 |
|
| 87 |
namespace detail { |
| 88 |
|
| 89 |
OutputFactory::OutputFactory(const std::string& name): name_(name) { |
| 90 |
pthread_once(&once, init); |
| 91 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 92 |
|
| 93 |
ATLAS_ASSERT(m); |
| 94 |
if (m->find(name) != m->end()) { |
| 95 |
throw_Exception("Duplicate OutputFactory entry " + name); |
| 96 |
} |
| 97 |
|
| 98 |
(*m)[name] = this; |
| 99 |
} |
| 100 |
|
| 101 |
OutputFactory::~OutputFactory() { |
| 102 |
pthread_once(&once, init); |
| 103 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 104 |
ATLAS_ASSERT(m); |
| 105 |
m->erase(name_); |
| 106 |
} |
| 107 |
|
| 108 |
void OutputFactory::list(std::ostream& out) { |
| 109 |
pthread_once(&once, init); |
| 110 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 111 |
|
| 112 |
ATLAS_ASSERT(m); |
| 113 |
const char* sep = ""; |
| 114 |
for (std::map<std::string, OutputFactory*>::const_iterator j = m->begin(); j != m->end(); ++j) { |
| 115 |
out << sep << (*j).first; |
| 116 |
sep = ", "; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
const OutputImpl* OutputFactory::build(const std::string& name, std::ostream& stream) { |
| 121 |
pthread_once(&once, init); |
| 122 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 123 |
|
| 124 |
ATLAS_ASSERT(m); |
| 125 |
std::map<std::string, OutputFactory*>::const_iterator j = m->find(name); |
| 126 |
|
| 127 |
Log::debug() << "Looking for OutputFactory [" << name << "]" << std::endl; |
| 128 |
|
| 129 |
if (j == m->end()) { |
| 130 |
Log::error() << "No OutputFactory for [" << name << "]" << std::endl; |
| 131 |
Log::error() << "OutputFactories are:" << std::endl; |
| 132 |
for (j = m->begin(); j != m->end(); ++j) { |
| 133 |
Log::error() << " " << (*j).first << std::endl; |
| 134 |
} |
| 135 |
throw_Exception(std::string("No OutputFactory called ") + name); |
| 136 |
} |
| 137 |
|
| 138 |
return (*j).second->make(stream); |
| 139 |
} |
| 140 |
|
| 141 |
const OutputImpl* OutputFactory::build(const std::string& name, std::ostream& stream, |
| 142 |
const eckit::Parametrisation& param) { |
| 143 |
pthread_once(&once, init); |
| 144 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 145 |
|
| 146 |
ATLAS_ASSERT(m); |
| 147 |
std::map<std::string, OutputFactory*>::const_iterator j = m->find(name); |
| 148 |
|
| 149 |
Log::debug() << "Looking for OutputFactory [" << name << "]" << std::endl; |
| 150 |
|
| 151 |
if (j == m->end()) { |
| 152 |
Log::error() << "No OutputFactory for [" << name << "]" << std::endl; |
| 153 |
Log::error() << "OutputFactories are:" << std::endl; |
| 154 |
for (j = m->begin(); j != m->end(); ++j) { |
| 155 |
Log::error() << " " << (*j).first << std::endl; |
| 156 |
} |
| 157 |
throw_Exception(std::string("No OutputFactory called ") + name); |
| 158 |
} |
| 159 |
|
| 160 |
return (*j).second->make(stream, param); |
| 161 |
} |
| 162 |
|
| 163 |
extern "C" { |
| 164 |
|
| 165 |
void atlas__Output__delete(OutputImpl* This) { |
| 166 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialisd atlas_Output"); |
| 167 |
delete This; |
| 168 |
} |
| 169 |
|
| 170 |
const OutputImpl* atlas__Output__create(const char* factory_key, std::ostream* stream, |
| 171 |
const eckit::Parametrisation* config) { |
| 172 |
ATLAS_ASSERT(config != nullptr, "Cannot access uninitialisd atlas_Config"); |
| 173 |
const OutputImpl* output(nullptr); |
| 174 |
{ |
| 175 |
Output o(std::string{factory_key}, *stream, *config); |
| 176 |
output = o.get(); |
| 177 |
output->attach(); |
| 178 |
} |
| 179 |
output->detach(); |
| 180 |
return output; |
| 181 |
} |
| 182 |
|
| 183 |
void atlas__Output__write_mesh(const OutputImpl* This, Mesh::Implementation* mesh, |
| 184 |
const eckit::Parametrisation* params) { |
| 185 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialisd atlas_Output"); |
| 186 |
Mesh m(mesh); |
| 187 |
This->write(m, *params); |
| 188 |
} |
| 189 |
void atlas__Output__write_fieldset(const OutputImpl* This, const FieldSetImpl* fieldset, |
| 190 |
const eckit::Parametrisation* config) { |
| 191 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialisd atlas_Output"); |
| 192 |
ATLAS_ASSERT(fieldset != nullptr, "Cannot access uninitialisd atlas_FieldSet"); |
| 193 |
This->write(fieldset, *config); |
| 194 |
} |
| 195 |
void atlas__Output__write_field(const OutputImpl* This, const FieldImpl* field, const eckit::Parametrisation* config) { |
| 196 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialisd atlas_Output"); |
| 197 |
ATLAS_ASSERT(field != nullptr, "Cannot access uninitialisd atlas_Field"); |
| 198 |
ATLAS_ASSERT(config != nullptr, "Cannot access uninitialisd atlas_Config"); |
| 199 |
This->write(field, *config); |
| 200 |
} |
| 201 |
void atlas__Output__write_fieldset_fs(const OutputImpl* This, const FieldSetImpl* fieldset, |
| 202 |
const functionspace::FunctionSpaceImpl* functionspace, |
| 203 |
const eckit::Parametrisation* params) { |
| 204 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialisd atlas_Output"); |
| 205 |
ATLAS_ASSERT(fieldset != nullptr, "Cannot access uninitialisd atlas_FieldSet"); |
| 206 |
ATLAS_ASSERT(functionspace != nullptr, "Cannot access uninitialisd atlas_FunctionSpace"); |
| 207 |
|
| 208 |
This->write(fieldset, functionspace, *params); |
| 209 |
} |
| 210 |
void atlas__Output__write_field_fs(const OutputImpl* This, const FieldImpl* field, |
| 211 |
const functionspace::FunctionSpaceImpl* functionspace, |
| 212 |
const eckit::Parametrisation* config) { |
| 213 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialisd atlas_Output"); |
| 214 |
ATLAS_ASSERT(field != nullptr, "Cannot access uninitialisd atlas_Field"); |
| 215 |
ATLAS_ASSERT(functionspace != nullptr, "Cannot access uninitialisd atlas_FunctionSpace"); |
| 216 |
ATLAS_ASSERT(config != nullptr, "Cannot access uninitialisd atlas_Config"); |
| 217 |
This->write(field, functionspace, *config); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
} // namespace detail |
| 222 |
} // namespace output |
| 223 |
} // namespace atlas |