PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
NCFile.cc
Go to the documentation of this file.
1 // Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 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 #include "pism/util/io/NCFile.hh"
20 
21 #include <cstdio> // fprintf, stderr, rename, remove
22 #include "pism/util/error_handling.hh"
23 #include "pism/util/Grid.hh"
24 
25 // The following is a stupid kludge necessary to make NetCDF 4.x work in
26 // serial mode in an MPI program:
27 #ifndef MPI_INCLUDED
28 #define MPI_INCLUDED 1
29 #endif
30 #include <netcdf.h>
31 
32 namespace pism {
33 namespace io {
34 
35 NCFile::NCFile(MPI_Comm c)
36  : m_com(c), m_file_id(-1), m_define_mode(false) {
37 }
38 
39 std::string NCFile::filename() const {
40  return m_filename;
41 }
42 
43 void NCFile::set_compression_level(int level) const {
45 }
46 
47 void NCFile::set_compression_level_impl(int level) const {
48  (void) level;
49  // the default implementation does nothing
50 }
51 
52 void NCFile::def_var_chunking_impl(const std::string &name,
53  std::vector<size_t> &dimensions) const {
54  (void) name;
55  (void) dimensions;
56  // the default implementation does nothing
57 }
58 
59 
60 void NCFile::open(const std::string &filename, io::Mode mode) {
61  this->open_impl(filename, mode);
63  m_define_mode = false;
64 }
65 
66 void NCFile::create(const std::string &filename) {
67  this->create_impl(filename);
69  m_define_mode = true;
70 }
71 
72 void NCFile::sync() const {
73  enddef();
74  this->sync_impl();
75 }
76 
77 void NCFile::close() {
78  this->close_impl();
79  m_filename.clear();
80  m_file_id = -1;
81 }
82 
83 void NCFile::enddef() const {
84  if (m_define_mode) {
85  this->enddef_impl();
86  m_define_mode = false;
87  }
88 }
89 
90 void NCFile::redef() const {
91  if (not m_define_mode) {
92  this->redef_impl();
93  m_define_mode = true;
94  }
95 }
96 
97 void NCFile::def_dim(const std::string &name, size_t length) const {
98  redef();
99  this->def_dim_impl(name, length);
100 }
101 
102 void NCFile::inq_dimid(const std::string &dimension_name, bool &exists) const {
103  this->inq_dimid_impl(dimension_name,exists);
104 }
105 
106 void NCFile::inq_dimlen(const std::string &dimension_name, unsigned int &result) const {
107  this->inq_dimlen_impl(dimension_name,result);
108 }
109 
110 void NCFile::inq_unlimdim(std::string &result) const {
111  this->inq_unlimdim_impl(result);
112 }
113 
114 void NCFile::def_var(const std::string &name, io::Type nctype,
115  const std::vector<std::string> &dims) const {
116  redef();
117  this->def_var_impl(name, nctype, dims);
118 }
119 
120 void NCFile::def_var_chunking(const std::string &name,
121  std::vector<size_t> &dimensions) const {
122  this->def_var_chunking_impl(name, dimensions);
123 }
124 
125 
126 void NCFile::get_vara_double(const std::string &variable_name,
127  const std::vector<unsigned int> &start,
128  const std::vector<unsigned int> &count,
129  double *ip) const {
130 #if (Pism_DEBUG==1)
131  if (start.size() != count.size()) {
133  "start and count arrays have to have the same size");
134  }
135 #endif
136 
137  enddef();
138  this->get_vara_double_impl(variable_name, start, count, ip);
139 }
140 
141 void NCFile::put_vara_double(const std::string &variable_name,
142  const std::vector<unsigned int> &start,
143  const std::vector<unsigned int> &count,
144  const double *op) const {
145 #if (Pism_DEBUG==1)
146  if (start.size() != count.size()) {
148  "start and count arrays have to have the same size");
149  }
150 #endif
151 
152  enddef();
153  this->put_vara_double_impl(variable_name, start, count, op);
154 }
155 
156 
157 void NCFile::write_darray(const std::string &variable_name,
158  const Grid &grid,
159  unsigned int z_count,
160  bool time_dependent,
161  unsigned int record,
162  const double *input) {
163  enddef();
164  this->write_darray_impl(variable_name, grid, z_count, time_dependent, record, input);
165 }
166 
167 /*!
168  * The default implementation computes start and count and calls put_vara_double()
169  */
170 void NCFile::write_darray_impl(const std::string &variable_name,
171  const Grid &grid,
172  unsigned int z_count,
173  bool time_dependent,
174  unsigned int record,
175  const double *input) {
176  std::vector<unsigned int> start, count;
177 
178  if (time_dependent) {
179  start = { record, (unsigned)grid.ys(), (unsigned)grid.xs(), 0 };
180  count = { 1, (unsigned)grid.ym(), (unsigned)grid.xm(), z_count };
181  } else {
182  start = { (unsigned)grid.ys(), (unsigned)grid.xs(), 0 };
183  count = { (unsigned)grid.ym(), (unsigned)grid.xm(), z_count };
184  }
185 
186  this->put_vara_double(variable_name, start, count, input);
187 }
188 
189 
190 void NCFile::get_varm_double(const std::string &variable_name,
191  const std::vector<unsigned int> &start,
192  const std::vector<unsigned int> &count,
193  const std::vector<unsigned int> &imap,
194  double *ip) const {
195 
196 #if (Pism_DEBUG==1)
197  if (start.size() != count.size() or
198  start.size() != imap.size()) {
200  "start, count and imap arrays have to have the same size");
201  }
202 #endif
203 
204  enddef();
205  this->get_varm_double_impl(variable_name, start, count, imap, ip);
206 }
207 
208 void NCFile::inq_nvars(int &result) const {
209  this->inq_nvars_impl(result);
210 }
211 
212 void NCFile::inq_vardimid(const std::string &variable_name, std::vector<std::string> &result) const {
213  this->inq_vardimid_impl(variable_name, result);
214 }
215 
216 void NCFile::inq_varnatts(const std::string &variable_name, int &result) const {
217  this->inq_varnatts_impl(variable_name, result);
218 }
219 
220 void NCFile::inq_varid(const std::string &variable_name, bool &result) const {
221  this->inq_varid_impl(variable_name, result);
222 }
223 
224 void NCFile::inq_varname(unsigned int j, std::string &result) const {
225  this->inq_varname_impl(j, result);
226 }
227 
228 void NCFile::get_att_double(const std::string &variable_name,
229  const std::string &att_name,
230  std::vector<double> &result) const {
231  this->get_att_double_impl(variable_name, att_name, result);
232 }
233 
234 void NCFile::get_att_text(const std::string &variable_name,
235  const std::string &att_name,
236  std::string &result) const {
237  this->get_att_text_impl(variable_name, att_name, result);
238 }
239 
240 void NCFile::put_att_double(const std::string &variable_name,
241  const std::string &att_name,
242  io::Type xtype,
243  const std::vector<double> &data) const {
244  this->put_att_double_impl(variable_name, att_name, xtype, data);
245 }
246 
247 void NCFile::put_att_text(const std::string &variable_name,
248  const std::string &att_name,
249  const std::string &value) const {
250  this->put_att_text_impl(variable_name, att_name, value);
251 }
252 
253 void NCFile::inq_attname(const std::string &variable_name,
254  unsigned int n,
255  std::string &result) const {
256  this->inq_attname_impl(variable_name, n, result);
257 }
258 
259 void NCFile::inq_atttype(const std::string &variable_name,
260  const std::string &att_name,
261  io::Type &result) const {
262  this->inq_atttype_impl(variable_name, att_name, result);
263 }
264 
265 void NCFile::set_fill(int fillmode, int &old_modep) const {
266  redef();
267  this->set_fill_impl(fillmode, old_modep);
268 }
269 
270 void NCFile::del_att(const std::string &variable_name, const std::string &att_name) const {
271  this->del_att_impl(variable_name, att_name);
272 }
273 
274 } // end of namespace io
275 } // end of namespace pism
int ys() const
Global starting index of this processor's subset.
Definition: Grid.cc:736
int xs() const
Global starting index of this processor's subset.
Definition: Grid.cc:731
int xm() const
Width of this processor's sub-domain.
Definition: Grid.cc:741
int ym() const
Width of this processor's sub-domain.
Definition: Grid.cc:746
Describes the PISM grid and the distribution of data across processors.
Definition: Grid.hh:282
static RuntimeError formatted(const ErrorLocation &location, const char format[],...) __attribute__((format(printf
build a RuntimeError with a formatted message
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
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 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
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
#define PISM_ERROR_LOCATION
#define n
Definition: exactTestM.c:37
int count
Definition: test_cube.c:16