File nc_format.hxx

netCDF data format interface

Records: In netCDF, the time dimension for each dimension must be the same. Hence when a record is appended to a variable, the size of all variables is increased. To work out which record to write to, a map of variable names to record number is kept.

Author
B.Dudson
Date
April 2009

Copyright 2010 B.D.Dudson, S.Farley, M.V.Umansky, X.Q.Xu

Contact: Ben Dudson, bd512@york.ac.uk

This file is part of BOUT++.

BOUT++ is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

BOUT++ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with BOUT++. If not, see http://www.gnu.org/licenses/.

class NcFormat : public DataFormat

Public Functions

NcFormat(Mesh *mesh_in = nullptr)
NcFormat(const char *name, Mesh *mesh_in = nullptr)
NcFormat(const std::string &name, Mesh *mesh_in = nullptr)
~NcFormat()
bool openr(const char *name)
bool openw(const char *name, bool append = false)
bool is_valid()
void close()
void flush()
const char *filename()
const vector<int> getSize(const char *var)
const vector<int> getSize(const std::string &var)
bool setGlobalOrigin(int x = 0, int y = 0, int z = 0)
bool setLocalOrigin(int x = 0, int y = 0, int z = 0, int offset_x = 0, int offset_y = 0, int offset_z = 0)
bool setRecord(int t)
bool addVarInt(const std::string &name, bool repeat)
bool addVarBoutReal(const std::string &name, bool repeat)
bool addVarField2D(const std::string &name, bool repeat)
bool addVarField3D(const std::string &name, bool repeat)
bool addVarFieldPerp(const std::string &name, bool repeat)
bool read(int *var, const char *name, int lx = 1, int ly = 0, int lz = 0)
bool read(int *var, const std::string &name, int lx = 1, int ly = 0, int lz = 0)
bool read(BoutReal *var, const char *name, int lx = 1, int ly = 0, int lz = 0)
bool read(BoutReal *var, const std::string &name, int lx = 1, int ly = 0, int lz = 0)
bool read_perp(BoutReal *var, const std::string &name, int lx = 1, int lz = 0)
bool write(int *var, const char *name, int lx = 0, int ly = 0, int lz = 0)
bool write(int *var, const std::string &name, int lx = 0, int ly = 0, int lz = 0)
bool write(BoutReal *var, const char *name, int lx = 0, int ly = 0, int lz = 0)
bool write(BoutReal *var, const std::string &name, int lx = 0, int ly = 0, int lz = 0)
bool write_perp(BoutReal *var, const std::string &name, int lx = 0, int lz = 0)
bool read_rec(int *var, const char *name, int lx = 1, int ly = 0, int lz = 0)
bool read_rec(int *var, const std::string &name, int lx = 1, int ly = 0, int lz = 0)
bool read_rec(BoutReal *var, const char *name, int lx = 1, int ly = 0, int lz = 0)
bool read_rec(BoutReal *var, const std::string &name, int lx = 1, int ly = 0, int lz = 0)
bool read_rec_perp(BoutReal *var, const std::string &name, int lx = 1, int lz = 0)
bool write_rec(int *var, const char *name, int lx = 0, int ly = 0, int lz = 0)
bool write_rec(int *var, const std::string &name, int lx = 0, int ly = 0, int lz = 0)
bool write_rec(BoutReal *var, const char *name, int lx = 0, int ly = 0, int lz = 0)
bool write_rec(BoutReal *var, const std::string &name, int lx = 0, int ly = 0, int lz = 0)
bool write_rec_perp(BoutReal *var, const std::string &name, int lx = 0, int lz = 0)
void setLowPrecision()
void setAttribute(const std::string &varname, const std::string &attrname, const std::string &text)

Sets a string attribute

Inputs

Parameters
  • varname: Variable name. The variable must already exist. If varname is the empty string “” then the attribute will be added to the file instead of to a variable.
  • attrname: Attribute name
  • text: A string attribute to attach to the variable

void setAttribute(const std::string &varname, const std::string &attrname, int value)

Sets an integer attribute

Inputs

Parameters
  • varname: Variable name. The variable must already exist. If varname is the empty string “” then the attribute will be added to the file instead of to a variable.
  • attrname: Attribute name
  • value: An int attribute to attach to the variable

void setAttribute(const std::string &varname, const std::string &attrname, BoutReal value)

Sets a BoutReal attribute

Inputs

Parameters
  • varname: Variable name. The variable must already exist. If varname is the empty string “” then the attribute will be added to the file instead of to a variable.
  • attrname: Attribute name
  • value: A BoutReal attribute to attach to the variable

bool getAttribute(const std::string &varname, const std::string &attrname, std::string &text)

Gets a string attribute

Inputs

Returns

Parameters
  • varname: Variable name. The variable must already exist. If varname is the empty string “” then get the attribute from the top-level of the file instead of from a variable.
  • attrname: Attribute name

text A string attribute of the variable

bool getAttribute(const std::string &varname, const std::string &attrname, int &value)

Gets an integer attribute

Inputs

Returns

Parameters
  • varname: Variable name. The variable must already exist. If varname is the empty string “” then get the attribute from the top-level of the file instead of from a variable.
  • attrname: Attribute name

value An int attribute of the variable

bool getAttribute(const std::string &varname, const std::string &attrname, BoutReal &value)

Gets a BoutReal attribute

Inputs

Returns

Parameters
  • varname: Variable name. The variable must already exist. If varname is the empty string “” then get the attribute from the top-level of the file instead of from a variable.
  • attrname: Attribute name

value A BoutReal attribute of the variable

Private Functions

void checkName(const char *name)

Check if a name contains invalid characters.

Private Members

char *fname

Current file name.

NcFile *dataFile

Pointer to netCDF file.

NcDim *xDim

Dimensions.

NcDim *yDim
NcDim *zDim
NcDim *tDim
const NcDim **dimList

List of dimensions (x,y,z)

const NcDim **recDimList

List of dimensions (t,x,y,z)

bool lowPrecision

When writing, down-convert to floats.

int x0
int y0
int z0
int t0

Data origins.

std::map<std::string, int> rec_nr
int default_rec