PISM, A Parallel Ice Sheet Model 2.3.0-79cae578d committed by Constantine Khrulev on 2026-03-22
Loading...
Searching...
No Matches
NCFile.cc
Go to the documentation of this file.
1// Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2023, 2024, 2025 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
32namespace pism {
33namespace io {
34
35NCFile::NCFile(MPI_Comm c)
36 : m_com(c), m_file_id(-1), m_define_mode(false) {
37}
38
39std::string NCFile::filename() const {
40 return m_filename;
41}
42
43void NCFile::set_compression_level(int level) const {
45}
46
48 (void) level;
49 // the default implementation does nothing
50}
51
52void 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
60void NCFile::open(const std::string &filename, io::Mode mode) {
61 this->open_impl(filename, mode);
63 m_define_mode = false;
64}
65
66void NCFile::create(const std::string &filename) {
67 this->create_impl(filename);
69 m_define_mode = true;
70}
71
72void NCFile::sync() const {
73 enddef();
74 this->sync_impl();
75}
76
78 this->close_impl();
79 m_filename.clear();
80 m_file_id = -1;
81}
82
83void NCFile::enddef() const {
84 if (m_define_mode) {
85 this->enddef_impl();
86 m_define_mode = false;
87 }
88}
89
90void NCFile::redef() const {
91 if (not m_define_mode) {
92 this->redef_impl();
93 m_define_mode = true;
94 }
95}
96
97void NCFile::def_dim(const std::string &name, size_t length) const {
98 redef();
99 this->def_dim_impl(name, length);
100}
101
102void NCFile::inq_dimid(const std::string &dimension_name, bool &exists) const {
103 this->inq_dimid_impl(dimension_name,exists);
104}
105
106void NCFile::inq_dimlen(const std::string &dimension_name, unsigned int &result) const {
107 this->inq_dimlen_impl(dimension_name,result);
108}
109
110void NCFile::inq_unlimdim(std::string &result) const {
111 this->inq_unlimdim_impl(result);
112}
113
114void 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
120void 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
126void 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
141void 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
157void NCFile::put_vara_text(const std::string &variable_name, const std::vector<unsigned int> &start,
158 const std::vector<unsigned int> &count, const std::string &data) const {
159 enddef();
160 this->put_vara_text_impl(variable_name, start, count, data.c_str());
161}
162
163void NCFile::write_darray(const std::string &variable_name,
164 const grid::DistributedGridInfo &grid,
165 unsigned int z_count,
166 bool time_dependent,
167 unsigned int record,
168 const double *input) {
169 enddef();
170 this->write_darray_impl(variable_name, grid, z_count, time_dependent, record, input);
171}
172
173/*!
174 * The default implementation computes start and count and calls put_vara_double()
175 */
176void NCFile::write_darray_impl(const std::string &variable_name,
177 const grid::DistributedGridInfo &grid,
178 unsigned int z_count,
179 bool time_dependent,
180 unsigned int record,
181 const double *input) {
182 std::vector<unsigned int> start, count;
183
184 if (time_dependent) {
185 start = { record, grid.ys, grid.xs, 0 };
186 count = { 1, grid.ym, grid.xm, z_count };
187 } else {
188 start = { grid.ys, grid.xs, 0 };
189 count = { grid.ym, grid.xm, z_count };
190 }
191
192 this->put_vara_double(variable_name, start, count, input);
193}
194
195void NCFile::inq_nvars(int &result) const {
196 this->inq_nvars_impl(result);
197}
198
199void NCFile::inq_vardimid(const std::string &variable_name, std::vector<std::string> &result) const {
200 this->inq_vardimid_impl(variable_name, result);
201}
202
203void NCFile::inq_varnatts(const std::string &variable_name, int &result) const {
204 this->inq_varnatts_impl(variable_name, result);
205}
206
207void NCFile::inq_varid(const std::string &variable_name, bool &result) const {
208 this->inq_varid_impl(variable_name, result);
209}
210
211void NCFile::inq_varname(unsigned int j, std::string &result) const {
212 this->inq_varname_impl(j, result);
213}
214
215void NCFile::get_att_double(const std::string &variable_name,
216 const std::string &att_name,
217 std::vector<double> &result) const {
218 this->get_att_double_impl(variable_name, att_name, result);
219}
220
221void NCFile::get_att_text(const std::string &variable_name,
222 const std::string &att_name,
223 std::string &result) const {
224 this->get_att_text_impl(variable_name, att_name, result);
225}
226
227void NCFile::put_att_double(const std::string &variable_name,
228 const std::string &att_name,
229 io::Type xtype,
230 const std::vector<double> &data) const {
231 this->put_att_double_impl(variable_name, att_name, xtype, data);
232}
233
234void NCFile::put_att_text(const std::string &variable_name,
235 const std::string &att_name,
236 const std::string &value) const {
237 this->put_att_text_impl(variable_name, att_name, value);
238}
239
240void NCFile::inq_attname(const std::string &variable_name,
241 unsigned int n,
242 std::string &result) const {
243 this->inq_attname_impl(variable_name, n, result);
244}
245
246void NCFile::inq_atttype(const std::string &variable_name,
247 const std::string &att_name,
248 io::Type &result) const {
249 this->inq_atttype_impl(variable_name, att_name, result);
250}
251
252void NCFile::set_fill(int fillmode, int &old_modep) const {
253 redef();
254 this->set_fill_impl(fillmode, old_modep);
255}
256
257void NCFile::del_att(const std::string &variable_name, const std::string &att_name) const {
258 this->del_att_impl(variable_name, att_name);
259}
260
261} // end of namespace io
262} // end of namespace pism
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:211
void sync() const
Definition NCFile.cc:72
virtual void enddef_impl() const =0
std::string m_filename
Definition NCFile.hh:233
void inq_atttype(const std::string &variable_name, const std::string &att_name, io::Type &result) const
Definition NCFile.cc:246
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 put_vara_text(const std::string &variable_name, const std::vector< unsigned int > &start, const std::vector< unsigned int > &count, const std::string &data) const
Definition NCFile.cc:157
void inq_attname(const std::string &variable_name, unsigned int n, std::string &result) const
Definition NCFile.cc:240
virtual void write_darray_impl(const std::string &variable_name, const grid::DistributedGridInfo &grid, unsigned int z_count, bool time_dependent, unsigned int record, const double *input)
Definition NCFile.cc:176
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:257
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
void write_darray(const std::string &variable_name, const grid::DistributedGridInfo &grid, unsigned int z_count, bool time_dependent, unsigned int record, const double *input)
Definition NCFile.cc:163
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
virtual void put_vara_text_impl(const std::string &variable_name, const std::vector< unsigned int > &start, const std::vector< unsigned int > &count, const char *data) 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:221
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:227
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:215
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
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
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:252
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
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
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:195
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:234
void inq_vardimid(const std::string &variable_name, std::vector< std::string > &result) const
Definition NCFile.cc:199
void inq_varnatts(const std::string &variable_name, int &result) const
Definition NCFile.cc:203
void inq_varid(const std::string &variable_name, bool &result) const
Definition NCFile.cc:207
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