File: netCdfInfo.cpp

package info (click to toggle)
groops 0%2Bgit20250907%2Bds-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 11,140 kB
  • sloc: cpp: 135,607; fortran: 1,603; makefile: 20
file content (97 lines) | stat: -rw-r--r-- 2,656 bytes parent folder | download | duplicates (2)
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
/***********************************************/
/**
* @file netCdfInfo.cpp
*
* @brief Content information of a NetCDF file.
*
* @author Torsten Mayer-Guerr
* @date 2020-09-03
*/
/***********************************************/

// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
Print content information of a NetCDF file like
dimensions, variables and attributes.

See also \program{NetCdf2GriddedData}, \program{NetCdf2GriddedDataTimeSeries},
\program{GriddedData2NetCdf}, \program{GriddedDataTimeSeries2NetCdf}.
)";

/***********************************************/

#include "programs/program.h"
#include "inputOutput/fileNetCdf.h"

/***** CLASS ***********************************/

/** @brief Content information of a NetCDF file.
* @ingroup programsConversionGroup */
class NetCdfInfo
{
public:
  void run(Config &config, Parallel::CommunicatorPtr comm);
};

GROOPS_REGISTER_PROGRAM(NetCdfInfo, SINGLEPROCESS, "Content information of a NetCDF file", Conversion)

/***********************************************/

void NetCdfInfo::run(Config &config, Parallel::CommunicatorPtr /*comm*/)
{
  try
  {
    FileName fileNameIn;

    readConfig(config, "inputfileNetCdf", fileNameIn, Config::MUSTSET,  "", "");
    if(isCreateSchema(config)) return;

#ifdef GROOPS_DISABLE_NETCDF
    throw(Exception("Compiled without NetCDF library"));
#else
    // open netCDF file
    // ----------------
    logStatus<<"read netCDF file <"<<fileNameIn<<">"<<Log::endl;
    NetCdf::InFile file(fileNameIn);

    logInfo<<"  global attributes:"<<Log::endl;
    auto attributes = file.attributes();
    for(auto &attr : attributes)
      logInfo<<"    - "<<attr.name()<<" = "<<attr.value()<<Log::endl;

    logInfo<<"  dimensions:"<<Log::endl;
    auto dimensions = file.dimensions();
    for(auto &dim : dimensions)
      logInfo<<"    - "<<dim.name()<<" = "<<dim.length()<<Log::endl;

    auto variables = file.variables();
    for(auto &var : variables)
    {
      std::stringstream ss;
      auto dimensions = var.dimensions();
      if(dimensions.size())
      {
        ss<<"  variable: "<<var.name()<<"(";
        ss<<dimensions.at(0).name();
        for(UInt i=1; i<dimensions.size(); i++)
          ss<<", "<<dimensions.at(i).name();
        ss<<")";
      }
      else
        ss<<"  - "<<var.name();
      logInfo<<ss.str()<<Log::endl;

      auto attributes = var.attributes();
      for(auto &attr : attributes)
        logInfo<<"    - "<<attr.name()<<" value = "<<attr.value()<<Log::endl;
    }
#endif
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/