/usr/include/magics/Netcdf.h is in libmagics++-dev 2.18.15-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | /******************************** LICENSE ********************************
Copyright 2007 European Centre for Medium-Range Weather Forecasts (ECMWF)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************** LICENSE ********************************/
/*! \file Netcdf.h
\brief Definition of the Netcdf access tools.
Magics Team - ECMWF 2004
Started: Fri 16-Jan-2004
Changes:
*/
#ifndef Netcdf_H
#define Netcdf_H
#include "magics.h"
#include "netcdfcpp.h"
#include "MagException.h"
#include "MagLog.h"
namespace magics {
class NoSuchNetcdfVariable : public MagicsException
{
public:
NoSuchNetcdfVariable( const string& var ):
MagicsException("Netcdf MagException: Can not find variable ---> " + var)
{ MagLog::warning() << what_ << "\n"; }
};
class NoSuchNetcdfDimension : public MagicsException
{
public:
NoSuchNetcdfDimension( const string& dim ):
MagicsException("Netcdf MagException : Can not find dimension ---> " + dim)
{ MagLog::warning() << what_ << "\n"; }
};
class NoSuchNetcdfFile : public MagicsException
{
public:
NoSuchNetcdfFile( const string& file ):
MagicsException("Netcdf MagException: The file " + file + " does not exist or is not a valid netcdf file")
{ MagLog::error() << what_ << "\n"; }
};
struct NetDimension
{
string name_;
long size_;
long first_;
long dim_;
long index_;
NcDim* id_;
NcVar* variable_;
NetDimension() {}
NetDimension(NcDim* id, NcVar* variable, long index) :
name_(id->name()), size_(id->size()),
first_(0), dim_(size_), index_(index),
id_(id), variable_(variable)
{}
void first(const string&);
void last(const string&);
int index(const string&);
void print(ostream& s) const
{
s << name_ << "(" << size_ << ", " << index_ << ", " << first_ << ", " << dim_ << ")";
}
friend ostream& operator<<(ostream& s,const NetDimension& p)
{ p.print(s); return s; }
};
struct NetAttribute
{
string name_;
NcAtt* id_;
NetAttribute(const string name, NcAtt* id) : name_(name), id_(id) {}
NetAttribute() {}
void get(double& val) { val = id_->as_double(0); }
void get(float& val) { val = id_->as_float(0); }
void get(const char*& val) { val = id_->as_string(0); }
};
class NetVariable;
template <class From, class To>
struct Convertor
{
Convertor(NetVariable& );
To operator()(From from)
{
return from * scale_factor_ + add_offset_;
}
NetVariable& variable_;
To scale_factor_;
To add_offset_;
};
template <class T>
class Accessor
{
public:
Accessor(NcType type) {
if ( !accessors_) accessors_ = new map<NcType, Accessor<T>*>;
accessors_->insert(std::make_pair(type, this));
}
virtual ~Accessor() { }
virtual void operator() (vector<T>&, vector<long>& , vector<long>&, NetVariable&) const {}
static map<NcType, Accessor<T>*>* accessors_;
static void release() {
if ( accessors_ )
for ( typename map<NcType, Accessor<T>*>::iterator a = accessors_.begin(); a != accessors_.end(); ++a) {
Accessor<T>* accessor = a->second;
a->second = 0;
delete accessor;
}
}
static void access(vector<T>& data, vector<long> start, vector<long> edges, NetVariable& var);
static void access(vector<T>& data, NetDimension& dim){}
};
template <class F, class T>
class TypedAccessor : public Accessor<T>
{
public:
TypedAccessor(NcType type) : Accessor<T>(type) {}
void operator() (vector<T>& to, vector<long>& start, vector<long>& edges, NetVariable& var) const;
void get (vector<F>& from, vector<long>& start, vector<long>& edges, NetVariable& var) const;
};
struct NetVariable
{
string name_;
NcVar* id_;
map<string, NetDimension> dimensions_;
map<string, NetAttribute> attributes_;
NetVariable(const string& name, NcVar* id, const NcFile& file);
void getStartingPoint(vector<long>& dims)
{
dims.resize(dimensions_.size());
for (map<string, NetDimension>::const_iterator dim = dimensions_.begin(); dim != dimensions_.end(); ++dim)
{
dims[(*dim).second.index_] = (*dim).second.first_;
}
}
void getDimensions(vector<long>& dims)
{
dims.resize(dimensions_.size());
for (map<string, NetDimension>::const_iterator dim = dimensions_.begin(); dim != dimensions_.end(); ++dim)
{
dims[(*dim).second.index_] = (*dim).second.dim_;
}
}
void setFirstPoint(const string& name, const string& first)
{
map<string, NetDimension>::iterator dim = dimensions_.find(name);
if ( dim == dimensions_.end() ) return;
(*dim).second.first(first);
}
void setLastPoint(const string& name, const string& last)
{
map<string, NetDimension>::iterator d = dimensions_.find(name);
if ( d == dimensions_.end() ) return;
(*d).second.last(last);
}
long getSize(const vector<long>& dims)
{
long size = 1;
for (unsigned int i = 0; i < dims.size(); i++)
size = (dims[i] ) * size;
return size;
}
void print(ostream& s) const
{
s << name_ << "[";
string sep = "";
for (map<string, NetDimension>::const_iterator dim = dimensions_.begin(); dim != dimensions_.end(); ++dim)
{
s << sep << (*dim).second;
sep = ", ";
}
s << "]" << "\n";
}
template <class T>
T getAttribute(const string& name, T def)
{
T val;
map<string, NetAttribute>::iterator attr = attributes_.find(name);
if ( attr == attributes_.end() ) return def;
(*attr).second.get(val);
return val;
}
double getDefaultMissing();
template <class T>
void get(vector<T>& vals, map<string, string> first, map<string, string> last)
{
for (map<string, string>::const_iterator f = first.begin(); f != first.end(); ++f) {
setFirstPoint((*f).first, (*f).second);
}
for (map<string, string>::const_iterator f = last.begin(); f != last.end(); ++f) {
setLastPoint((*f).first, (*f).second);
}
get(vals);
}
template <class T>
void get(vector<T>& vals)
{
vector<long> start;
getStartingPoint(start);
vector<long> end;
getDimensions(end);
vals.resize(getSize(end));
Accessor<T>::access(vals, start, end, *this);
}
friend ostream& operator<<(ostream& s,const NetVariable& p)
{ p.print(s); return s; }
};
class Netcdf {
public:
Netcdf(const string&);
virtual ~Netcdf();
typedef map<string, NetVariable> VariableMap;
double getDefaultMissing(const string& name)
{
map<string, NetVariable>::iterator var = variables_.find(name);
if ( var == variables_.end() ) throw NoSuchNetcdfVariable(name);
return var->second.getDefaultMissing();
}
template <class T>
void get(const string& name, vector<T>& vals,
map<string, string> first,
map<string, string> last)
{
map<string, NetVariable>::iterator var = variables_.find(name);
if ( var == variables_.end() ) throw NoSuchNetcdfVariable(name);
(*var).second.get(vals, first, last);
}
template <class T>
void get(const string& name, vector<T>& vals)
{
map<string, NetVariable>::iterator var = variables_.find(name);
if ( var == variables_.end() ) throw NoSuchNetcdfVariable(name);
(*var).second.get(vals);
}
int getDimension(const string& name)
{
NcDim* dim = file_.get_dim(name.c_str());
return dim->size();
}
template <class T>
T getVariableAttribute(const string& name, const string& attr, T def)
{
map<string, NetVariable>::iterator var = variables_.find(name);
if ( var == variables_.end() ) throw NoSuchNetcdfVariable(name);
return (*var).second.getAttribute(attr, def);
}
template <class T>
T getAttribute(const string& name, T def)
{
T val;
map<string, NetAttribute>::iterator attr = attributes_.find(name);
if ( attr == attributes_.end() ) return def;
(*attr).second.get(val);
return val;
}
NetVariable getVariable(const string& name)
{
map<string, NetVariable>::iterator var = variables_.find(name);
if ( var == variables_.end() ) throw NoSuchNetcdfVariable(name);
return (*var).second;
}
map<string, NetAttribute> getAttributes()
{
return attributes_;
}
protected:
virtual void print(ostream&) const;
map<string, NetVariable> variables_;
map<string, NetVariable> dataset_;
map<string, NetAttribute> attributes_;
private:
NcFile file_;
friend ostream& operator<<(ostream& s,const Netcdf& p)
{ p.print(s); return s; }
};
template <class From, class To>
class DataAccessor
{
public:
DataAccessor(Netcdf& netcdf) : netcdf_(netcdf) {}
void operator()(const string& name, vector<To>& to)
{
vector<From> from;
netcdf_.get(name, from, start_, end_);
int i = 0;
for (typename vector<From>::const_iterator val = from.begin(); val != from.end(); ++val) {
To add = To(*val);
to.push_back(To(*val));
}
}
void setDimension(const string& name, long from, long dim) {
start_.insert(std::make_pair(name, from));
end_.insert(std::make_pair(name, dim));
}
void setDimension(const string& name, long val) {
start_.insert(std::make_pair(name, val));
end_.insert(std::make_pair(name, 1));
}
map<string, long> start_;
map<string, long> end_;
Netcdf& netcdf_;
};
template <class T>
void Accessor<T>::access(vector<T>& data, vector<long> start, vector<long> edges, NetVariable& var)
{
typename map<NcType, Accessor<T>*>::const_iterator accessor = accessors_->find(var.id_->type());
if ( accessor == accessors_->end() ) throw new MagicsException("No accessor available");
(*(*accessor).second)(data, start, edges, var);
}
} // Namespace Magics
#endif
|