IODA
NetcdfIO.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2017-2019 UCAR
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 
8 #ifndef IO_NETCDFIO_H_
9 #define IO_NETCDFIO_H_
10 
11 #include <map>
12 #include <string>
13 #include <vector>
14 
15 #include "oops/util/DateTime.h"
16 #include "oops/util/ObjectCounter.h"
17 
18 #include "ioda/io/IodaIO.h"
19 
20 ////////////////////////////////////////////////////////////////////////
21 // Implementation of IodaIO for netcdf.
22 ////////////////////////////////////////////////////////////////////////
23 
24 // Forward declarations
25 namespace eckit {
26  class Configuration;
27 }
28 
29 namespace ioda {
30 
31 /*! \brief Implementation of IodaIO for netcdf.
32  *
33  * \details The NetcdfIO class defines the constructor and methods for netcdf
34  * file access. These fill in the abstract base class IodaIO methods.
35  *
36  * \author Stephen Herbener (JCSDA)
37  */
38 class NetcdfIO : public IodaIO,
39  private util::ObjectCounter<NetcdfIO> {
40  public:
41  /*!
42  * \brief classname method for object counter
43  *
44  * \details This method is supplied for the ObjectCounter base class.
45  * It defines a name to identify an object of this class
46  * for reporting by OOPS.
47  */
48  static const std::string classname() {return "ioda::NetcdfIO";}
49 
50  NetcdfIO(const std::string & FileName, const std::string & FileMode,
51  const std::size_t MaxFrameSize);
52  ~NetcdfIO();
53 
54  private:
55  // For the oops::Printable base class
56  void print(std::ostream & os) const;
57 
58  void NcReadVar(const std::string & GroupName, const std::string & VarName,
59  const std::vector<std::size_t> & Starts,
60  const std::vector<std::size_t> & Counts,
61  int & FillValue, std::vector<int> & VarData);
62  void NcReadVar(const std::string & GroupName, const std::string & VarName,
63  const std::vector<std::size_t> & Starts,
64  const std::vector<std::size_t> & Counts,
65  float & FillValue, std::vector<float> & VarData);
66  void NcReadVar(const std::string & GroupName, const std::string & VarName,
67  const std::vector<std::size_t> & Starts,
68  const std::vector<std::size_t> & Counts,
69  double & FillValue, std::vector<double> & VarData);
70  void NcReadVar(const std::string & GroupName, const std::string & VarName,
71  const std::vector<std::size_t> & Starts,
72  const std::vector<std::size_t> & Counts,
73  char & FillValue, std::vector<std::string> & VarData);
74 
75  void NcWriteVar(const std::string & GroupName, const std::string & VarName,
76  const std::vector<std::size_t> & Starts,
77  const std::vector<std::size_t> & Counts,
78  const std::vector<int> & VarData);
79  void NcWriteVar(const std::string & GroupName, const std::string & VarName,
80  const std::vector<std::size_t> & Starts,
81  const std::vector<std::size_t> & Counts,
82  const std::vector<float> & VarData);
83  void NcWriteVar(const std::string & GroupName, const std::string & VarName,
84  const std::vector<std::size_t> & Starts,
85  const std::vector<std::size_t> & Counts,
86  const std::vector<std::string> & VarData);
87 
88  void CheckNcCall(int RetCode, std::string & ErrorMsg);
89 
90  bool NcAttrExists(const int & AttrOwnerId, const std::string & AttrName);
91 
92  std::string FormNcVarName(const std::string & GroupName, const std::string & VarName);
93 
94  int GetStringDimBySize(const std::size_t DimSize);
95 
96  void ReadConvertDateTime(const std::string & GroupName, const std::string & VarName,
97  const std::vector<std::size_t> & Starts,
98  const std::vector<std::size_t> & Counts,
99  std::vector<std::string> & VarData);
100 
101  template <typename DataType>
102  void ReplaceFillWithMissing(std::vector<DataType> & VarData, DataType NcFillValue);
103 
104  std::size_t GetMaxStringSize(const std::vector<std::string> & Strings);
105  std::vector<int> GetNcDimIds(const std::string & GroupName,
106  const std::vector<std::size_t> & VarShape);
107 
108  void DimInsert(const std::string & Name, const std::size_t Size);
109 
110  void InitializeFrame() {}
111  void FinalizeFrame() {}
112  void ReadFrame(IodaIO::FrameIter & iframe);
113  void WriteFrame(IodaIO::FrameIter & iframe);
114 
115  void GrpVarInsert(const std::string & GroupName, const std::string & VarName,
116  const std::string & VarType, const std::vector<std::size_t> & VarShape,
117  const std::string & FileVarName, const std::string & FileType,
118  const std::size_t MaxStringSize);
119 
120  // Data members
121  /*!
122  * \brief netcdf file id
123  *
124  * \details This data member holds the file id of the open netcdf file.
125  * It gives access to the dimensions, attributes and variables in
126  * the netcdf file.
127  */
128  int ncid_;
129 
130  /*!
131  * \brief offset time flag
132  *
133  * \details This data member is a flag indicating the existence of the
134  * offset time variable in the netcdf file.
135  */
137 
138  /*!
139  * \brief date time flag
140  *
141  * \details This data member is a flag indicating the existence of the
142  * date_time variable in the netcdf file.
143  */
145 };
146 
147 } // namespace ioda
148 
149 #endif // IO_NETCDFIO_H_
ioda::NetcdfIO::NcWriteVar
void NcWriteVar(const std::string &GroupName, const std::string &VarName, const std::vector< std::size_t > &Starts, const std::vector< std::size_t > &Counts, const std::vector< int > &VarData)
Write data from memory to netcdf file.
Definition: NetcdfIO.cc:406
ioda::NetcdfIO::NcAttrExists
bool NcAttrExists(const int &AttrOwnerId, const std::string &AttrName)
Check existence of netcdf attribute.
Definition: NetcdfIO.cc:826
ioda::NetcdfIO::GetNcDimIds
std::vector< int > GetNcDimIds(const std::string &GroupName, const std::vector< std::size_t > &VarShape)
Get the netcdf dimension ids.
Definition: NetcdfIO.cc:487
ioda::NetcdfIO::have_offset_time_
bool have_offset_time_
offset time flag
Definition: NetcdfIO.h:136
ioda::NetcdfIO::InitializeFrame
void InitializeFrame()
Definition: NetcdfIO.h:110
ioda::NetcdfIO::DimInsert
void DimInsert(const std::string &Name, const std::size_t Size)
create a dimension in the netcdf file
Definition: NetcdfIO.cc:546
ioda::NetcdfIO::ReplaceFillWithMissing
void ReplaceFillWithMissing(std::vector< DataType > &VarData, DataType NcFillValue)
Replace netcdf fill values with JEDI missing values.
Definition: NetcdfIO.cc:382
ioda::IodaIO::FrameIter
FrameInfo::const_iterator FrameIter
Definition: src/io/IodaIO.h:275
ioda::NetcdfIO::NetcdfIO
NetcdfIO(const std::string &FileName, const std::string &FileMode, const std::size_t MaxFrameSize)
Definition: NetcdfIO.cc:49
ioda::NetcdfIO::FinalizeFrame
void FinalizeFrame()
Definition: NetcdfIO.h:111
ioda::NetcdfIO
Implementation of IodaIO for netcdf.
Definition: NetcdfIO.h:39
ioda
Definition: IodaUtils.cc:13
ioda::NetcdfIO::have_date_time_
bool have_date_time_
date time flag
Definition: NetcdfIO.h:144
ioda::NetcdfIO::CheckNcCall
void CheckNcCall(int RetCode, std::string &ErrorMsg)
check results of netcdf call
Definition: NetcdfIO.cc:806
ioda::NetcdfIO::GrpVarInsert
void GrpVarInsert(const std::string &GroupName, const std::string &VarName, const std::string &VarType, const std::vector< std::size_t > &VarShape, const std::string &FileVarName, const std::string &FileType, const std::size_t MaxStringSize)
Add entry to the group, variable info container.
Definition: NetcdfIO.cc:722
ioda::NetcdfIO::classname
static const std::string classname()
classname method for object counter
Definition: NetcdfIO.h:48
eckit
Definition: LocalObsSpaceParameters.h:24
ioda::NetcdfIO::NcReadVar
void NcReadVar(const std::string &GroupName, const std::string &VarName, const std::vector< std::size_t > &Starts, const std::vector< std::size_t > &Counts, int &FillValue, std::vector< int > &VarData)
Read data from netcdf file to memory.
Definition: NetcdfIO.cc:239
ioda::NetcdfIO::print
void print(std::ostream &os) const
print method for stream output
Definition: NetcdfIO.cc:788
ioda::NetcdfIO::ncid_
int ncid_
netcdf file id
Definition: NetcdfIO.h:128
ioda::NetcdfIO::~NetcdfIO
~NetcdfIO()
Definition: NetcdfIO.cc:219
ioda::NetcdfIO::ReadConvertDateTime
void ReadConvertDateTime(const std::string &GroupName, const std::string &VarName, const std::vector< std::size_t > &Starts, const std::vector< std::size_t > &Counts, std::vector< std::string > &VarData)
read date and time information from the input netcdf file
Definition: NetcdfIO.cc:910
ioda::NetcdfIO::WriteFrame
void WriteFrame(IodaIO::FrameIter &iframe)
Write data from the frame containers into the file.
Definition: NetcdfIO.cc:673
ioda::NetcdfIO::GetStringDimBySize
int GetStringDimBySize(const std::size_t DimSize)
allocate a dimension for writing a character array
Definition: NetcdfIO.cc:869
ioda::NetcdfIO::ReadFrame
void ReadFrame(IodaIO::FrameIter &iframe)
Read data from the file into the frame containers.
Definition: NetcdfIO.cc:559
ioda::IodaIO
File access class for IODA.
Definition: src/io/IodaIO.h:116
ioda::NetcdfIO::GetMaxStringSize
std::size_t GetMaxStringSize(const std::vector< std::string > &Strings)
Get the max string size in a vector of strings.
Definition: NetcdfIO.cc:471
ioda::NetcdfIO::FormNcVarName
std::string FormNcVarName(const std::string &GroupName, const std::string &VarName)
form the netcdf variable name
Definition: NetcdfIO.cc:844