Extension: Json and NetCDF utilities
#include "dg/file/file.h"
Loading...
Searching...
No Matches
Collaboration diagram for Utilities for the NcFile class:

Classes

struct  dg::file::NC_Error
 Class thrown by the NC_Error_Handle. More...
 
struct  dg::file::NcHyperslab
 A NetCDF Hyperslab for SerialNcFile. More...
 
struct  dg::file::MPINcHyperslab
 A NetCDF Hyperslab for MPINcFile. More...
 
struct  dg::file::LongNameAttribute
 Facilitate construction of CF attribute "long_name" in records lists. More...
 
struct  dg::file::Record< SignatureType, Attributes >
 A realisation of the Record concept. Helper to generate NetCDF variables. More...
 

Typedefs

using dg::file::nc_att_t
 Utility type to simplify dealing with heterogeneous attribute types.
 
template<class Signature >
using dg::file::get_first_argument_type_t = std::decay_t<typename detail::get_first_argument_type<Signature>::type>
 
template<class Signature >
using dg::file::get_result_type_t = typename std::function<Signature>::result_type
 

Enumerations

enum  dg::file::NcFileMode { dg::file::nc_nowrite , dg::file::nc_write , dg::file::nc_clobber , dg::file::nc_noclobber }
 NetCDF file format. More...
 

Functions

std::string dg::file::timestamp (int argc, char *argv[])
 Generate one line entry for the history global attribute.
 

Variables

const std::map< std::string, std::string > dg::file::version_flags
 Version compile time constants available as a map.
 

Detailed Description

Typedef Documentation

◆ get_first_argument_type_t

template<class Signature >
using dg::file::get_first_argument_type_t = std::decay_t<typename detail::get_first_argument_type<Signature>::type>

If Signature = R(Arg1, A...) return Arg1

◆ get_result_type_t

template<class Signature >
using dg::file::get_result_type_t = typename std::function<Signature>::result_type

If Signature = R(Arg1, A...) return R

◆ nc_att_t

Initial value:
std::variant<int, unsigned, float, double, bool, std::string,
std::vector<int>, std::vector<unsigned>, std::vector<float>,
std::vector<double>, std::vector<bool>>

Utility type to simplify dealing with heterogeneous attribute types.

Note
Unfortunately, user defined types exist so not every attribute can be an nc_att_t

Enumeration Type Documentation

◆ NcFileMode

NetCDF file format.

All Files are opened/ created in Netcdf-4 data format

Note
If you are looking for an "nc_append" you can use
auto nc_append = std::filesystem::exists(filename) ? nc_write : nc_noclobber;
@ nc_write
NC_WRITE Open an existing file for read and write access, fail if it does not exist.
Definition nc_file.h:91
@ nc_noclobber
NC_NOCLOBBER Create new file for read and write access, fail if already exists.
Definition nc_file.h:93
Enumerator
nc_nowrite 

NC_NOWRITE Open an existing file for read-only access, fail if it does not exist.

nc_write 

NC_WRITE Open an existing file for read and write access, fail if it does not exist.

nc_clobber 

NC_CLOBBER Create a new file for read and write access, overwrite if file exists.

nc_noclobber 

NC_NOCLOBBER Create new file for read and write access, fail if already exists.

Function Documentation

◆ timestamp()

std::string dg::file::timestamp ( int argc,
char * argv[] )
inline

Generate one line entry for the history global attribute.

This will generate a string containing a whitespace seperated list of

  1. the current day in "%Y-%m-%d" (the ISO 8601 date format)
  2. the current time in "%H:%M:%S" (the ISO 8601 time format)
  3. locale-dependent time zone name or abbreviation "%Z%"
  4. all given argv (whitespace separated)
  5. A newline
int argc = 2;
char *argv[] = {
(char*)"./netcdf_t",
(char*)"input.json",
NULL
};
file.put_att( {"history", dg::file::timestamp(argc, argv)});
auto history = file.get_att_as<std::string>( "history");
INFO("history "<<history);
std::istringstream ss( history);
std::tm t = {};
ss >> std::get_time( &t, "%Y-%m-%d %H:%M:%S %Z");
CHECK( not ss.fail());
std::string program;
ss >> program;
INFO( "Program "<<program);
CHECK( program == "./netcdf_t");
std::string arg;
ss >> arg;
CHECK( arg == "input.json");
CHECK( not ss.fail());
Parameters
argcfrom main function
argvfrom main function
Returns
string containing current time followed by the parameters with which the program was invoked
See also
See history in Attribute Convenctions
std::put_time

Variable Documentation

◆ version_flags

const std::map<std::string, std::string> dg::file::version_flags
inline
Initial value:
=
{
{"git_hash", GIT_HASH},
{"git_branch", GIT_BRANCH},
{"compile_time", COMPILE_TIME},
}

Version compile time constants available as a map.

Is intended to be used as NetCDF file attributes

int argc = 2;
char *argv[] = {
(char*)"./netcdf_t",
(char*)"input.json",
NULL
};
file.put_att( {"history", dg::file::timestamp(argc, argv)});
auto history = file.get_att_as<std::string>( "history");
INFO("history "<<history);
std::istringstream ss( history);
std::tm t = {};
ss >> std::get_time( &t, "%Y-%m-%d %H:%M:%S %Z");
CHECK( not ss.fail());
std::string program;
ss >> program;
INFO( "Program "<<program);
CHECK( program == "./netcdf_t");
std::string arg;
ss >> arg;
CHECK( arg == "input.json");
CHECK( not ss.fail());
Note
We use underscore instead of "git-hash" so that python netcdf can more easily read the attribute

The entries in the map are filled only if the corresponding MACROs are defined at compile time. Use -DVERSION_FLAGS during compilation to define all otherwise it remains empty. This is the corresponding entry in feltor/config/version.mk

GIT_HASH=$(git rev-parse HEAD)
COMPILE_TIME=$(date -u +'%Y-%m-%d %H:%M:%S UTC')
GIT_BRANCH=$(git branch --show-current)
See also
This approach follows stackoverflow.