PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
NCFile.hh
Go to the documentation of this file.
1 // Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2020, 2021, 2023 PISM Authors
2 //
3 // This file is part of PISM.
4 //
5 // PISM is free software; you can redistribute it and/or modify it under the
6 // terms of the GNU General Public License as published by the Free Software
7 // Foundation; either version 3 of the License, or (at your option) any later
8 // version.
9 //
10 // PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with PISM; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 #ifndef PISM_NCFILE_H
20 #define PISM_NCFILE_H
21 
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include <mpi.h>
27 
28 namespace pism {
29 
30 class Grid;
31 
32 namespace io {
33 enum Mode : int;
34 enum Type : int;
35 }
36 
37 //! Input and output code (NetCDF wrappers, etc)
38 namespace io {
39 
40 //! \brief The PISM wrapper for a subset of the NetCDF C API.
41 /*!
42  * The goal of this class is to hide the fact that we need to communicate data
43  * to and from the processor zero. Using this wrapper we should be able to
44  * write code that looks good and works both on 1-processor and
45  * multi-processor systems.
46  *
47  * Moreover, this way we can switch underlying I/O implementations.
48  *
49  * Notes:
50  * - It uses C++ STL strings instead of C character arrays
51  * - It hides NetCDF ncid, dimid and varid and uses strings to reference
52  * dimensions and variables instead.
53  * - This class does not and should not use any PETSc API calls.
54  * - This wrapper provides access to a very small portion of the NetCDF C API.
55  * (Only calls used in PISM.) This is intentional.
56  * - Methods of this class should do what corresponding NetCDF C API calls do,
57  * no more and no less.
58  */
59 class NCFile {
60 public:
61  typedef std::shared_ptr<NCFile> Ptr;
62 
63  NCFile(MPI_Comm com);
64  virtual ~NCFile() = default;
65 
66  // open/create/close
67  void open(const std::string &filename, io::Mode mode);
68 
69  void create(const std::string &filename);
70 
71  void sync() const;
72 
73  void close();
74 
75  // redef/enddef
76  void enddef() const;
77 
78  void redef() const;
79 
80  // dim
81  void def_dim(const std::string &name, size_t length) const;
82 
83  void inq_dimid(const std::string &dimension_name, bool &exists) const;
84 
85  void inq_dimlen(const std::string &dimension_name, unsigned int &result) const;
86 
87  void inq_unlimdim(std::string &result) const;
88 
89  // var
90  void def_var(const std::string &name, io::Type nctype,
91  const std::vector<std::string> &dims) const;
92 
93  void def_var_chunking(const std::string &name, std::vector<size_t> &dimensions) const;
94 
95  void get_vara_double(const std::string &variable_name, const std::vector<unsigned int> &start,
96  const std::vector<unsigned int> &count, double *ip) const;
97 
98  void put_vara_double(const std::string &variable_name, const std::vector<unsigned int> &start,
99  const std::vector<unsigned int> &count, const double *op) const;
100 
101  void write_darray(const std::string &variable_name, const Grid &grid, unsigned int z_count,
102  bool time_dependent, unsigned int record, const double *input);
103 
104  void get_varm_double(const std::string &variable_name, const std::vector<unsigned int> &start,
105  const std::vector<unsigned int> &count,
106  const std::vector<unsigned int> &imap, double *ip) const;
107 
108  void inq_nvars(int &result) const;
109 
110  void inq_vardimid(const std::string &variable_name, std::vector<std::string> &result) const;
111 
112  void inq_varnatts(const std::string &variable_name, int &result) const;
113 
114  void inq_varid(const std::string &variable_name, bool &result) const;
115 
116  void inq_varname(unsigned int j, std::string &result) const;
117 
118  void set_compression_level(int level) const;
119 
120  // att
121  void get_att_double(const std::string &variable_name, const std::string &att_name,
122  std::vector<double> &result) const;
123 
124  void get_att_text(const std::string &variable_name, const std::string &att_name,
125  std::string &result) const;
126 
127  void put_att_double(const std::string &variable_name, const std::string &att_name, io::Type xtype,
128  const std::vector<double> &data) const;
129 
130  void put_att_text(const std::string &variable_name, const std::string &att_name,
131  const std::string &value) const;
132 
133  void inq_attname(const std::string &variable_name, unsigned int n, std::string &result) const;
134 
135  void inq_atttype(const std::string &variable_name, const std::string &att_name,
136  io::Type &result) const;
137 
138  // misc
139  void set_fill(int fillmode, int &old_modep) const;
140 
141  std::string filename() const;
142 
143  void del_att(const std::string &variable_name, const std::string &att_name) const;
144 
145 protected:
146  // implementations:
147 
148  // open/create/close
149  virtual void open_impl(const std::string &filename, io::Mode mode) = 0;
150  virtual void create_impl(const std::string &filename) = 0;
151  virtual void sync_impl() const = 0;
152  virtual void close_impl() = 0;
153 
154  // redef/enddef
155  virtual void enddef_impl() const = 0;
156 
157  virtual void redef_impl() const = 0;
158 
159  // dim
160  virtual void def_dim_impl(const std::string &name, size_t length) const = 0;
161 
162  virtual void inq_dimid_impl(const std::string &dimension_name, bool &exists) const = 0;
163 
164  virtual void inq_dimlen_impl(const std::string &dimension_name, unsigned int &result) const = 0;
165 
166  virtual void inq_unlimdim_impl(std::string &result) const = 0;
167 
168  // var
169  virtual void def_var_impl(const std::string &name, io::Type nctype,
170  const std::vector<std::string> &dims) const = 0;
171 
172  virtual void def_var_chunking_impl(const std::string &name,
173  std::vector<size_t> &dimensions) const;
174 
175  virtual void get_vara_double_impl(const std::string &variable_name,
176  const std::vector<unsigned int> &start,
177  const std::vector<unsigned int> &count, double *ip) const = 0;
178 
179  virtual void put_vara_double_impl(const std::string &variable_name,
180  const std::vector<unsigned int> &start,
181  const std::vector<unsigned int> &count,
182  const double *op) const = 0;
183 
184  virtual void write_darray_impl(const std::string &variable_name, const Grid &grid,
185  unsigned int z_count, bool time_dependent, unsigned int record,
186  const double *input);
187 
188  virtual void get_varm_double_impl(const std::string &variable_name,
189  const std::vector<unsigned int> &start,
190  const std::vector<unsigned int> &count,
191  const std::vector<unsigned int> &imap, double *ip) const = 0;
192 
193  virtual void inq_nvars_impl(int &result) const = 0;
194 
195  virtual void inq_vardimid_impl(const std::string &variable_name,
196  std::vector<std::string> &result) const = 0;
197 
198  virtual void inq_varnatts_impl(const std::string &variable_name, int &result) const = 0;
199 
200  virtual void inq_varid_impl(const std::string &variable_name, bool &exists) const = 0;
201 
202  virtual void inq_varname_impl(unsigned int j, std::string &result) const = 0;
203 
204  virtual void set_compression_level_impl(int level) const = 0;
205 
206  // att
207  virtual void get_att_double_impl(const std::string &variable_name, const std::string &att_name,
208  std::vector<double> &result) const = 0;
209 
210  virtual void get_att_text_impl(const std::string &variable_name, const std::string &att_name,
211  std::string &result) const = 0;
212 
213  virtual void put_att_double_impl(const std::string &variable_name, const std::string &att_name,
214  io::Type xtype, const std::vector<double> &data) const = 0;
215 
216  virtual void put_att_text_impl(const std::string &variable_name, const std::string &att_name,
217  const std::string &value) const = 0;
218 
219  virtual void inq_attname_impl(const std::string &variable_name, unsigned int n,
220  std::string &result) const = 0;
221 
222  virtual void inq_atttype_impl(const std::string &variable_name, const std::string &att_name,
223  io::Type &result) const = 0;
224 
225  // misc
226  virtual void set_fill_impl(int fillmode, int &old_modep) const = 0;
227 
228  virtual void del_att_impl(const std::string &variable_name, const std::string &att_name) const = 0;
229 
230 protected: // data members
231 
232  MPI_Comm m_com;
234  std::string m_filename;
235 private:
236  mutable bool m_define_mode;
237 };
238 
239 } // end of namespace io
240 } // end of namespace pism
241 
242 #endif /* PISM_NCFILE_H */
Describes the PISM grid and the distribution of data across processors.
Definition: Grid.hh:282
void inq_varname(unsigned int j, std::string &result) const
Definition: NCFile.cc:224
void sync() const
Definition: NCFile.cc:72
virtual void enddef_impl() const =0
std::string m_filename
Definition: NCFile.hh:234
void inq_atttype(const std::string &variable_name, const std::string &att_name, io::Type &result) const
Definition: NCFile.cc:259
virtual void put_vara_double_impl(const std::string &variable_name, const std::vector< unsigned int > &start, const std::vector< unsigned int > &count, const double *op) const =0
virtual void inq_attname_impl(const std::string &variable_name, unsigned int n, std::string &result) const =0
void inq_attname(const std::string &variable_name, unsigned int n, std::string &result) const
Definition: NCFile.cc:253
virtual void create_impl(const std::string &filename)=0
virtual void inq_dimlen_impl(const std::string &dimension_name, unsigned int &result) const =0
void del_att(const std::string &variable_name, const std::string &att_name) const
Definition: NCFile.cc:270
bool m_define_mode
Definition: NCFile.hh:236
void get_vara_double(const std::string &variable_name, const std::vector< unsigned int > &start, const std::vector< unsigned int > &count, double *ip) const
Definition: NCFile.cc:126
virtual void close_impl()=0
NCFile(MPI_Comm com)
Definition: NCFile.cc:35
virtual void put_att_text_impl(const std::string &variable_name, const std::string &att_name, const std::string &value) const =0
void inq_unlimdim(std::string &result) const
Definition: NCFile.cc:110
virtual void del_att_impl(const std::string &variable_name, const std::string &att_name) const =0
virtual void inq_atttype_impl(const std::string &variable_name, const std::string &att_name, io::Type &result) const =0
void create(const std::string &filename)
Definition: NCFile.cc:66
void get_att_text(const std::string &variable_name, const std::string &att_name, std::string &result) const
Definition: NCFile.cc:234
void put_att_double(const std::string &variable_name, const std::string &att_name, io::Type xtype, const std::vector< double > &data) const
Definition: NCFile.cc:240
virtual void open_impl(const std::string &filename, io::Mode mode)=0
void get_att_double(const std::string &variable_name, const std::string &att_name, std::vector< double > &result) const
Definition: NCFile.cc:228
void inq_dimlen(const std::string &dimension_name, unsigned int &result) const
Definition: NCFile.cc:106
void def_dim(const std::string &name, size_t length) const
Definition: NCFile.cc:97
void put_vara_double(const std::string &variable_name, const std::vector< unsigned int > &start, const std::vector< unsigned int > &count, const double *op) const
Definition: NCFile.cc:141
virtual void get_varm_double_impl(const std::string &variable_name, const std::vector< unsigned int > &start, const std::vector< unsigned int > &count, const std::vector< unsigned int > &imap, double *ip) const =0
MPI_Comm m_com
Definition: NCFile.hh:232
void set_compression_level(int level) const
Definition: NCFile.cc:43
void enddef() const
Definition: NCFile.cc:83
virtual void get_vara_double_impl(const std::string &variable_name, const std::vector< unsigned int > &start, const std::vector< unsigned int > &count, double *ip) const =0
virtual ~NCFile()=default
virtual void inq_varname_impl(unsigned int j, std::string &result) const =0
virtual void set_compression_level_impl(int level) const =0
Definition: NCFile.cc:47
std::shared_ptr< NCFile > Ptr
Definition: NCFile.hh:61
void write_darray(const std::string &variable_name, const Grid &grid, unsigned int z_count, bool time_dependent, unsigned int record, const double *input)
Definition: NCFile.cc:157
virtual void def_dim_impl(const std::string &name, size_t length) const =0
void open(const std::string &filename, io::Mode mode)
Definition: NCFile.cc:60
virtual void inq_vardimid_impl(const std::string &variable_name, std::vector< std::string > &result) const =0
void set_fill(int fillmode, int &old_modep) const
Definition: NCFile.cc:265
virtual void inq_varnatts_impl(const std::string &variable_name, int &result) const =0
virtual void inq_dimid_impl(const std::string &dimension_name, bool &exists) const =0
void redef() const
Definition: NCFile.cc:90
void def_var_chunking(const std::string &name, std::vector< size_t > &dimensions) const
Definition: NCFile.cc:120
void get_varm_double(const std::string &variable_name, const std::vector< unsigned int > &start, const std::vector< unsigned int > &count, const std::vector< unsigned int > &imap, double *ip) const
Definition: NCFile.cc:190
virtual void get_att_double_impl(const std::string &variable_name, const std::string &att_name, std::vector< double > &result) const =0
virtual void inq_unlimdim_impl(std::string &result) const =0
virtual void set_fill_impl(int fillmode, int &old_modep) const =0
virtual void write_darray_impl(const std::string &variable_name, const Grid &grid, unsigned int z_count, bool time_dependent, unsigned int record, const double *input)
Definition: NCFile.cc:170
void inq_dimid(const std::string &dimension_name, bool &exists) const
Definition: NCFile.cc:102
virtual void sync_impl() const =0
std::string filename() const
Definition: NCFile.cc:39
virtual void def_var_impl(const std::string &name, io::Type nctype, const std::vector< std::string > &dims) const =0
void inq_nvars(int &result) const
Definition: NCFile.cc:208
virtual void redef_impl() const =0
virtual void def_var_chunking_impl(const std::string &name, std::vector< size_t > &dimensions) const
Definition: NCFile.cc:52
virtual void put_att_double_impl(const std::string &variable_name, const std::string &att_name, io::Type xtype, const std::vector< double > &data) const =0
virtual void get_att_text_impl(const std::string &variable_name, const std::string &att_name, std::string &result) const =0
virtual void inq_varid_impl(const std::string &variable_name, bool &exists) const =0
void put_att_text(const std::string &variable_name, const std::string &att_name, const std::string &value) const
Definition: NCFile.cc:247
void close()
Definition: NCFile.cc:77
void inq_vardimid(const std::string &variable_name, std::vector< std::string > &result) const
Definition: NCFile.cc:212
void inq_varnatts(const std::string &variable_name, int &result) const
Definition: NCFile.cc:216
void inq_varid(const std::string &variable_name, bool &result) const
Definition: NCFile.cc:220
virtual void inq_nvars_impl(int &result) const =0
void def_var(const std::string &name, io::Type nctype, const std::vector< std::string > &dims) const
Definition: NCFile.cc:114
The PISM wrapper for a subset of the NetCDF C API.
Definition: NCFile.hh:59
#define n
Definition: exactTestM.c:37
int count
Definition: test_cube.c:16