tNCFile.hh - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
HTML git clone git://src.adamsgaard.dk/pism
DIR Log
DIR Files
DIR Refs
DIR LICENSE
---
tNCFile.hh (8903B)
---
1 // Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, 2019 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 _PISMNCWRAPPER_H_
20 #define _PISMNCWRAPPER_H_
21
22 #include <memory>
23 #include <string>
24 #include <vector>
25
26 #include <mpi.h>
27
28 #include "IO_Flags.hh"
29
30 namespace pism {
31
32 class IceGrid;
33
34 //! Input and output code (NetCDF wrappers, etc)
35 namespace io {
36
37 //! \brief The PISM wrapper for a subset of the NetCDF C API.
38 /*!
39 * The goal of this class is to hide the fact that we need to communicate data
40 * to and from the processor zero. Using this wrapper we should be able to
41 * write code that looks good and works both on 1-processor and
42 * multi-processor systems.
43 *
44 * Moreover, this way we can switch underlying I/O implementations.
45 *
46 * Notes:
47 * - It uses C++ STL strings instead of C character arrays
48 * - It hides NetCDF ncid, dimid and varid and uses strings to reference
49 * dimensions and variables instead.
50 * - This class does not and should not use any PETSc API calls.
51 * - This wrapper provides access to a very small portion of the NetCDF C API.
52 * (Only calls used in PISM.) This is intentional.
53 * - Methods of this class should do what corresponding NetCDF C API calls do,
54 * no more and no less.
55 */
56 class NCFile
57 {
58 public:
59 typedef std::shared_ptr<NCFile> Ptr;
60
61 NCFile(MPI_Comm com);
62 virtual ~NCFile();
63
64 // open/create/close
65 void open(const std::string &filename, IO_Mode mode);
66
67 void create(const std::string &filename);
68
69 void sync() const;
70
71 void close();
72
73 // redef/enddef
74 void enddef() const;
75
76 void redef() const;
77
78 // dim
79 void def_dim(const std::string &name, size_t length) const;
80
81 void inq_dimid(const std::string &dimension_name, bool &exists) const;
82
83 void inq_dimlen(const std::string &dimension_name, unsigned int &result) const;
84
85 void inq_unlimdim(std::string &result) const;
86
87 // var
88 void def_var(const std::string &name, IO_Type nctype,
89 const std::vector<std::string> &dims) const;
90
91 void def_var_chunking(const std::string &name, std::vector<size_t> &dimensions) const;
92
93 void get_vara_double(const std::string &variable_name,
94 const std::vector<unsigned int> &start,
95 const std::vector<unsigned int> &count,
96 double *ip) const;
97
98 void put_vara_double(const std::string &variable_name,
99 const std::vector<unsigned int> &start,
100 const std::vector<unsigned int> &count,
101 const double *op) const;
102
103 void write_darray(const std::string &variable_name,
104 const IceGrid &grid,
105 unsigned int z_count,
106 unsigned int record,
107 const double *input);
108
109 void get_varm_double(const std::string &variable_name,
110 const std::vector<unsigned int> &start,
111 const std::vector<unsigned int> &count,
112 const std::vector<unsigned int> &imap,
113 double *ip) const;
114
115 void inq_nvars(int &result) const;
116
117 void inq_vardimid(const std::string &variable_name, std::vector<std::string> &result) const;
118
119 void inq_varnatts(const std::string &variable_name, int &result) const;
120
121 void inq_varid(const std::string &variable_name, bool &exists) const;
122
123 void inq_varname(unsigned int j, std::string &result) const;
124
125 // att
126 void get_att_double(const std::string &variable_name, const std::string &att_name,
127 std::vector<double> &result) const;
128
129 void get_att_text(const std::string &variable_name, const std::string &att_name,
130 std::string &result) const;
131
132 void put_att_double(const std::string &variable_name, const std::string &att_name,
133 IO_Type xtype, const std::vector<double> &data) const;
134
135 void put_att_text(const std::string &variable_name, const std::string &att_name,
136 const std::string &value) const;
137
138 void inq_attname(const std::string &variable_name, unsigned int n, std::string &result) const;
139
140 void inq_atttype(const std::string &variable_name, const std::string &att_name, IO_Type &result) const;
141
142 // misc
143 void set_fill(int fillmode, int &old_modep) const;
144
145 std::string filename() const;
146
147 void del_att(const std::string &variable_name, const std::string &att_name) const;
148
149 protected:
150 // implementations:
151
152 // open/create/close
153 virtual void open_impl(const std::string &filename, IO_Mode mode) = 0;
154 virtual void create_impl(const std::string &filename) = 0;
155 virtual void sync_impl() const = 0;
156 virtual void close_impl() = 0;
157
158 // redef/enddef
159 virtual void enddef_impl() const = 0;
160
161 virtual void redef_impl() const = 0;
162
163 // dim
164 virtual void def_dim_impl(const std::string &name, size_t length) const = 0;
165
166 virtual void inq_dimid_impl(const std::string &dimension_name, bool &exists) const = 0;
167
168 virtual void inq_dimlen_impl(const std::string &dimension_name, unsigned int &result) const = 0;
169
170 virtual void inq_unlimdim_impl(std::string &result) const = 0;
171
172 // var
173 virtual void def_var_impl(const std::string &name, IO_Type nctype,
174 const std::vector<std::string> &dims) const = 0;
175
176 virtual void def_var_chunking_impl(const std::string &name,
177 std::vector<size_t> &dimensions) const;
178
179 virtual void get_vara_double_impl(const std::string &variable_name,
180 const std::vector<unsigned int> &start,
181 const std::vector<unsigned int> &count,
182 double *ip) const = 0;
183
184 virtual void put_vara_double_impl(const std::string &variable_name,
185 const std::vector<unsigned int> &start,
186 const std::vector<unsigned int> &count,
187 const double *op) const = 0;
188
189 virtual void write_darray_impl(const std::string &variable_name,
190 const IceGrid &grid,
191 unsigned int z_count,
192 unsigned int record,
193 const double *input);
194
195 virtual void get_varm_double_impl(const std::string &variable_name,
196 const std::vector<unsigned int> &start,
197 const std::vector<unsigned int> &count,
198 const std::vector<unsigned int> &imap,
199 double *ip) const = 0;
200
201 virtual void inq_nvars_impl(int &result) const = 0;
202
203 virtual void inq_vardimid_impl(const std::string &variable_name, std::vector<std::string> &result) const = 0;
204
205 virtual void inq_varnatts_impl(const std::string &variable_name, int &result) const = 0;
206
207 virtual void inq_varid_impl(const std::string &variable_name, bool &exists) const = 0;
208
209 virtual void inq_varname_impl(unsigned int j, std::string &result) const = 0;
210
211 // att
212 virtual void get_att_double_impl(const std::string &variable_name, const std::string &att_name, std::vector<double> &result) const = 0;
213
214 virtual void get_att_text_impl(const std::string &variable_name, const std::string &att_name, std::string &result) const = 0;
215
216 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;
217
218 virtual void put_att_text_impl(const std::string &variable_name, const std::string &att_name, const std::string &value) const = 0;
219
220 virtual void inq_attname_impl(const std::string &variable_name, unsigned int n, std::string &result) const = 0;
221
222 virtual void inq_atttype_impl(const std::string &variable_name, const std::string &att_name, IO_Type &result) const = 0;
223
224 // misc
225 virtual void set_fill_impl(int fillmode, int &old_modep) const = 0;
226
227 virtual void del_att_impl(const std::string &variable_name, const std::string &att_name) const = 0;
228
229 protected: // data members
230
231 MPI_Comm m_com;
232 int m_file_id;
233 std::string m_filename;
234 private:
235 mutable bool m_define_mode;
236 };
237
238 } // end of namespace io
239 } // end of namespace pism
240
241 #endif /* _PISMNCWRAPPER_H_ */