ARTS 2.5.11 (git: 6827797f)
nc_io.h File Reference

This file contains basic functions to handle NetCDF data files. More...

#include "arts.h"
#include <netcdf.h>
#include "exceptions.h"
#include "messages.h"
#include "mystring.h"
#include "species_tags.h"

Go to the source code of this file.

Functions

void nca_filename (String &filename, const String &varname)
 Gives the default filename for the NetCDF formats.
 
void nca_filename_with_index (String &filename, const Index &file_index, const String &varname)
 Gives the default filename, with file index, for the NetCDF formats.
 
template<typename T >
void nca_read_from_file (const String &filename, T &type, const Verbosity &verbosity)
 Reads a variable from a NetCDF file.
 
template<typename T >
void nca_write_to_file (const String &filename, const T &type, const Verbosity &verbosity)
 Writes a variable to a NetCDF file.
 
void nca_def_dim (const int ncid, const String &name, const Index nelem, int *ncdim)
 Define NetCDF dimension.
 
void nca_def_var (const int ncid, const String &name, const nc_type type, const int ndims, const int *dims, int *varid)
 Define NetCDF variable.
 
int nca_def_ArrayOfIndex (const int ncid, const String &name, const ArrayOfIndex &a)
 Define NetCDF dimensions and variable for an ArrayOfIndex.
 
int nca_def_Vector (const int ncid, const String &name, const Vector &v)
 Define NetCDF dimensions and variable for a Vector.
 
int nca_def_Matrix (const int ncid, const String &name, const Matrix &m)
 Define NetCDF dimensions and variable for a Matrix.
 
int nca_def_Tensor4 (const int ncid, const String &name, const Tensor4 &t)
 Define NetCDF dimensions and variable for a Tensor4.
 
Index nca_get_dim (const int ncid, const String &name, const bool noerror=false)
 Read a dimension from NetCDF file.
 
void nca_get_data (const int ncid, const String &name, int *data)
 Read variable of type int from NetCDF file.
 
void nca_get_data (const int ncid, const String &name, long *data)
 Read variable of type long from NetCDF file.
 
void nca_get_data (const int ncid, const String &name, long long *data)
 Read variable of type long long from NetCDF file.
 
void nca_get_data (const int ncid, const String &name, Numeric *data)
 Read variable of type double from NetCDF file.
 
void nca_get_data (const int ncid, const String &name, size_t start, size_t count, Numeric *data)
 Read variable of type array of double from NetCDF file.
 
void nca_get_data (const int ncid, const String &name, char *data)
 Read variable of type array of char from NetCDF file.
 
void nca_get_data (const int ncid, const String &name, ArrayOfIndex &aoi, const bool noerror)
 Read variable of type ArrayOfIndex from NetCDF file.
 
void nca_get_data (const int ncid, const String &name, ArrayOfArrayOfSpeciesTag &aast, const bool noerror)
 Read variable of type ArrayOfArrayOfSpeciesTag from NetCDF file.
 
void nca_get_data (const int ncid, const String &name, Vector &v, const bool noerror=false)
 Read variable of type Vector from NetCDF file.
 
void nca_get_data (const int ncid, const String &name, Matrix &m, const bool noerror=false)
 Read variable of type Matrix from NetCDF file.
 
void nca_get_data (const int ncid, const String &name, Tensor4 &m, const bool noerror=false)
 Read variable of type Tensor4 from NetCDF file.
 
void nca_put_var (const int ncid, const int varid, const long *ind_arr)
 Write variable of type long* to NetCDF file.
 
void nca_put_var (const int ncid, const int varid, const long long *ind_arr)
 Write variable of type long long* to NetCDF file.
 
bool nca_put_var (const int ncid, const int varid, const ArrayOfIndex &a)
 Write variable of type ArrayOfIndex to NetCDF file.
 
bool nca_put_var (const int ncid, const int varid, const Vector &v)
 Write variable of type Vector to NetCDF file.
 
bool nca_put_var (const int ncid, const int varid, const Matrix &m)
 Write variable of type Matrix to NetCDF file.
 
bool nca_put_var (const int ncid, const int varid, const Tensor4 &t)
 Write variable of type Tensor4 to NetCDF file.
 
void nca_error (const int err, const std::string_view msg)
 Throws a runtime error for the given NetCDF error code.
 

Detailed Description

This file contains basic functions to handle NetCDF data files.

Author
Oliver Lemke olemk.nosp@m.e@co.nosp@m.re-du.nosp@m.mp.i.nosp@m.nfo
Date
2008-09-12

Definition in file nc_io.h.

Function Documentation

◆ nca_def_ArrayOfIndex()

int nca_def_ArrayOfIndex ( const int  ncid,
const String name,
const ArrayOfIndex a 
)

Define NetCDF dimensions and variable for an ArrayOfIndex.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[in]aArrayOfIndex
Returns
Variable handle
Author
Oliver Lemke

Definition at line 190 of file nc_io.cc.

References a, nca_def_dim(), and nca_def_var().

◆ nca_def_dim()

void nca_def_dim ( const int  ncid,
const String name,
const Index  nelem,
int *  ncdim 
)

Define NetCDF dimension.

Parameters
[in]ncidNetCDF file descriptor
[in]nameDimension name
[in]nelemDimension size
[out]ncdimNetCDF dimension handle
Author
Oliver Lemke

Definition at line 156 of file nc_io.cc.

References nca_error().

Referenced by nca_def_ArrayOfIndex(), nca_def_Matrix(), nca_def_Tensor4(), and nca_def_Vector().

◆ nca_def_Matrix()

int nca_def_Matrix ( const int  ncid,
const String name,
const Matrix &  m 
)

Define NetCDF dimensions and variable for a Matrix.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[in]mMatrix
Returns
Variable handle
Author
Oliver Lemke

Definition at line 233 of file nc_io.cc.

References nca_def_dim(), and nca_def_var().

◆ nca_def_Tensor4()

int nca_def_Tensor4 ( const int  ncid,
const String name,
const Tensor4 &  t 
)

Define NetCDF dimensions and variable for a Tensor4.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[in]tTensor4
Returns
Variable handle
Author
Oliver Lemke

Definition at line 255 of file nc_io.cc.

References nca_def_dim(), and nca_def_var().

◆ nca_def_var()

void nca_def_var ( const int  ncid,
const String name,
const nc_type  type,
const int  ndims,
const int *  dims,
int *  varid 
)

Define NetCDF variable.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[in]typeNetCDF type
[in]ndimsNumber of dimensions
[in]dimsPointer to dimensions
[out]varidNetCDF variable handle
Author
Oliver Lemke

Definition at line 174 of file nc_io.cc.

References nca_error().

Referenced by nca_def_ArrayOfIndex(), nca_def_Matrix(), nca_def_Tensor4(), and nca_def_Vector().

◆ nca_def_Vector()

int nca_def_Vector ( const int  ncid,
const String name,
const Vector &  v 
)

Define NetCDF dimensions and variable for a Vector.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[in]vVector
Returns
Variable handle
Author
Oliver Lemke

Definition at line 212 of file nc_io.cc.

References nca_def_dim(), nca_def_var(), and v.

◆ nca_error()

void nca_error ( const int  e,
const std::string_view  s 
)

Throws a runtime error for the given NetCDF error code.

Parameters
[in]eNetCDF error code
[in]sError message string
Author
Oliver Lemke

Definition at line 626 of file nc_io.cc.

References ARTS_USER_ERROR.

Referenced by nca_def_dim(), nca_def_var(), nca_get_data(), nca_get_dim(), nca_put_var(), nca_write_to_file(), and WriteMolTau().

◆ nca_filename()

void nca_filename ( String filename,
const String varname 
)

Gives the default filename for the NetCDF formats.

The default name is only used if the filename is empty.

Parameters
filenamefilename
varnamevariable name
Author
Oliver Lemke

Definition at line 36 of file nc_io.cc.

References out_basename.

Referenced by WriteNetCDF().

◆ nca_filename_with_index()

void nca_filename_with_index ( String filename,
const Index &  file_index,
const String varname 
)

Gives the default filename, with file index, for the NetCDF formats.

The default name is only used if the filename is empty.

Parameters
[out]filenamefilename
[in]file_indexIndex appended to the filename
[in]varnamevariable name
Author
Oliver Lemke

Definition at line 53 of file nc_io.cc.

References out_basename.

Referenced by WriteNetCDFIndexed().

◆ nca_get_data() [1/11]

void nca_get_data ( const int  ncid,
const String name,
ArrayOfArrayOfSpeciesTag aast,
const bool  noerror 
)

Read variable of type ArrayOfArrayOfSpeciesTag from NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[out]aastData read from file
[in]noerrorReturn empty variable instead of throwing an exception if variable does not exist in file
Author
Oliver Lemke

Definition at line 424 of file nc_io.cc.

References nca_get_data(), nca_get_dim(), and Array< base >::nelem().

◆ nca_get_data() [2/11]

void nca_get_data ( const int  ncid,
const String name,
ArrayOfIndex aoi,
const bool  noerror 
)

Read variable of type ArrayOfIndex from NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[out]aoiData read from file
[in]noerrorReturn empty variable instead of throwing an exception if variable does not exist in file
Author
Oliver Lemke

Definition at line 405 of file nc_io.cc.

References nca_get_data(), and nca_get_dim().

◆ nca_get_data() [3/11]

void nca_get_data ( const int  ncid,
const String name,
char *  data 
)

Read variable of type array of char from NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[out]dataData read from file
Author
Oliver Lemke

Definition at line 387 of file nc_io.cc.

References nca_error().

◆ nca_get_data() [4/11]

void nca_get_data ( const int  ncid,
const String name,
int *  data 
)

Read variable of type int from NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[out]dataData read from file
Author
Oliver Lemke

Definition at line 306 of file nc_io.cc.

References nca_error().

Referenced by nca_get_data(), and nca_read_from_file().

◆ nca_get_data() [5/11]

void nca_get_data ( const int  ncid,
const String name,
long *  data 
)

Read variable of type long from NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[out]dataData read from file
Author
Oliver Lemke

Definition at line 322 of file nc_io.cc.

References nca_error().

◆ nca_get_data() [6/11]

void nca_get_data ( const int  ncid,
const String name,
long long *  data 
)

Read variable of type long long from NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[out]dataData read from file
Author
Oliver Lemke

Definition at line 338 of file nc_io.cc.

References nca_error().

◆ nca_get_data() [7/11]

void nca_get_data ( const int  ncid,
const String name,
Matrix &  m,
const bool  noerror 
)

Read variable of type Matrix from NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[out]mData read from file
[in]noerrorReturn empty variable instead of throwing an exception if variable does not exist in file
Author
Oliver Lemke

Definition at line 480 of file nc_io.cc.

References nca_get_data(), and nca_get_dim().

◆ nca_get_data() [8/11]

void nca_get_data ( const int  ncid,
const String name,
Numeric *  data 
)

Read variable of type double from NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[out]dataData read from file
Author
Oliver Lemke

Definition at line 354 of file nc_io.cc.

References nca_error().

◆ nca_get_data() [9/11]

void nca_get_data ( const int  ncid,
const String name,
size_t  start,
size_t  count,
Numeric *  data 
)

Read variable of type array of double from NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[out]dataData read from file
Author
Oliver Lemke

Definition at line 370 of file nc_io.cc.

References nca_error().

◆ nca_get_data() [10/11]

void nca_get_data ( const int  ncid,
const String name,
Tensor4 &  t,
const bool  noerror 
)

Read variable of type Tensor4 from NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[out]tData read from file
[in]noerrorReturn empty variable instead of throwing an exception if variable does not exist in file
Author
Oliver Lemke

Definition at line 499 of file nc_io.cc.

References nca_get_data(), and nca_get_dim().

◆ nca_get_data() [11/11]

void nca_get_data ( const int  ncid,
const String name,
Vector &  v,
const bool  noerror 
)

Read variable of type Vector from NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[out]vData read from file
[in]noerrorReturn empty variable instead of throwing an exception if variable does not exist in file
Author
Oliver Lemke

Definition at line 462 of file nc_io.cc.

References nca_get_data(), nca_get_dim(), and v.

◆ nca_get_dim()

Index nca_get_dim ( const int  ncid,
const String name,
const bool  noerror 
)

Read a dimension from NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameDimension name in NetCDF file
[in]noerrorReturn 0 instead of throwing an exception if dimension does not exist in file
Returns
Dimension size
Author
Oliver Lemke

Definition at line 279 of file nc_io.cc.

References nca_error().

Referenced by nca_get_data(), and nca_read_from_file().

◆ nca_put_var() [1/6]

bool nca_put_var ( const int  ncid,
const int  varid,
const ArrayOfIndex a 
)

Write variable of type ArrayOfIndex to NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[in]aData to be written
Returns
True if variable was not empty
Author
Oliver Lemke

Definition at line 549 of file nc_io.cc.

References a, and nca_put_var().

◆ nca_put_var() [2/6]

void nca_put_var ( const int  ncid,
const int  varid,
const long *  ind_arr 
)

Write variable of type long* to NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[in]aData to be written
Returns
True if variable was not empty
Author
Oliver Lemke

Definition at line 519 of file nc_io.cc.

References nca_error().

Referenced by nca_put_var().

◆ nca_put_var() [3/6]

void nca_put_var ( const int  ncid,
const int  varid,
const long long *  ind_arr 
)

Write variable of type long long* to NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[in]aData to be written
Returns
True if variable was not empty
Author
Oliver Lemke

Definition at line 534 of file nc_io.cc.

References nca_error().

◆ nca_put_var() [4/6]

bool nca_put_var ( const int  ncid,
const int  varid,
const Matrix &  m 
)

Write variable of type Matrix to NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[in]mData to be written
Returns
True if variable was not empty
Author
Oliver Lemke

Definition at line 587 of file nc_io.cc.

References nca_error().

◆ nca_put_var() [5/6]

bool nca_put_var ( const int  ncid,
const int  varid,
const Tensor4 &  t 
)

Write variable of type Tensor4 to NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[in]tData to be written
Returns
True if variable was not empty
Author
Oliver Lemke

Definition at line 607 of file nc_io.cc.

References nca_error().

◆ nca_put_var() [6/6]

bool nca_put_var ( const int  ncid,
const int  varid,
const Vector &  v 
)

Write variable of type Vector to NetCDF file.

Parameters
[in]ncidNetCDF file descriptor
[in]nameVariable name in NetCDF file
[in]vData to be written
Returns
True if variable was not empty
Author
Oliver Lemke

Definition at line 567 of file nc_io.cc.

References nca_error(), and v.

◆ nca_read_from_file()

template<typename T >
void nca_read_from_file ( const String filename,
T &  type,
const Verbosity verbosity 
)

Reads a variable from a NetCDF file.

Parameters
[in]filenameNetCDF filename
[out]typeInput variable
[in]verbosityVerbosity
Author
Oliver Lemke

Definition at line 76 of file nc_io.cc.

References ARTS_USER_ERROR, CREATE_OUT2, expand_path(), and nca_read_from_file().

Referenced by nca_read_from_file(), and ReadNetCDF().

◆ nca_write_to_file()

template<typename T >
void nca_write_to_file ( const String filename,
const T &  type,
const Verbosity verbosity 
)

Writes a variable to a NetCDF file.

Parameters
[in]filenameNetCDF filename
[in]typeOutput variable
[in]verbosityVerbosity
Author
Oliver Lemke

Definition at line 116 of file nc_io.cc.

References add_basedir(), ARTS_USER_ERROR, CREATE_OUT2, and nca_write_to_file().

Referenced by nca_write_to_file(), and WriteNetCDF().