PISM, A Parallel Ice Sheet Model 2.2.1-cd005eec8 committed by Constantine Khrulev on 2025-03-07
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 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::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 */
170void 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
189void NCFile::inq_nvars(int &result) const {
190 this->inq_nvars_impl(result);
191}
192
193void NCFile::inq_vardimid(const std::string &variable_name, std::vector<std::string> &result) const {
194 this->inq_vardimid_impl(variable_name, result);
195}
196
197void NCFile::inq_varnatts(const std::string &variable_name, int &result) const {
198 this->inq_varnatts_impl(variable_name, result);
199}
200
201void NCFile::inq_varid(const std::string &variable_name, bool &result) const {
202 this->inq_varid_impl(variable_name, result);
203}
204
205void NCFile::inq_varname(unsigned int j, std::string &result) const {
206 this->inq_varname_impl(j, result);
207}
208
209void NCFile::get_att_double(const std::string &variable_name,
210 const std::string &att_name,
211 std::vector<double> &result) const {
212 this->get_att_double_impl(variable_name, att_name, result);
213}
214
215void NCFile::get_att_text(const std::string &variable_name,
216 const std::string &att_name,
217 std::string &result) const {
218 this->get_att_text_impl(variable_name, att_name, result);
219}
220
221void NCFile::put_att_double(const std::string &variable_name,
222 const std::string &att_name,
223 io::Type xtype,
224 const std::vector<double> &data) const {
225 this->put_att_double_impl(variable_name, att_name, xtype, data);
226}
227
228void NCFile::put_att_text(const std::string &variable_name,
229 const std::string &att_name,
230 const std::string &value) const {
231 this->put_att_text_impl(variable_name, att_name, value);
232}
233
234void NCFile::inq_attname(const std::string &variable_name,
235 unsigned int n,
236 std::string &result) const {
237 this->inq_attname_impl(variable_name, n, result);
238}
239
240void NCFile::inq_atttype(const std::string &variable_name,
241 const std::string &att_name,
242 io::Type &result) const {
243 this->inq_atttype_impl(variable_name, att_name, result);
244}
245
246void NCFile::set_fill(int fillmode, int &old_modep) const {
247 redef();
248 this->set_fill_impl(fillmode, old_modep);
249}
250
251void NCFile::del_att(const std::string &variable_name, const std::string &att_name) const {
252 this->del_att_impl(variable_name, att_name);
253}
254
255} // end of namespace io
256} // end of namespace pism
Describes the PISM grid and the distribution of data across processors.
Definition Grid.hh:290
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:205
void sync() const
Definition NCFile.cc:72
virtual void enddef_impl() const =0
std::string m_filename
Definition NCFile.hh:223
void inq_atttype(const std::string &variable_name, const std::string &att_name, io::Type &result) const
Definition NCFile.cc:240
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:234
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:251
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:215
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:221
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:209
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
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:246
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
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:189
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:228
void inq_vardimid(const std::string &variable_name, std::vector< std::string > &result) const
Definition NCFile.cc:193
void inq_varnatts(const std::string &variable_name, int &result) const
Definition NCFile.cc:197
void inq_varid(const std::string &variable_name, bool &result) const
Definition NCFile.cc:201
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