PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
NC4_Serial.cc
Go to the documentation of this file.
1 // Copyright (C) 2012, 2013, 2014, 2015, 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/NC4_Serial.hh"
20 
21 // The following is a stupid kludge necessary to make NetCDF 4.x work in
22 // serial mode in an MPI program:
23 #ifndef MPI_INCLUDED
24 #define MPI_INCLUDED 1
25 #endif
26 #include <netcdf.h>
27 
28 #include "pism/util/error_handling.hh"
29 #include "pism/util/pism_utilities.hh"
30 
31 namespace pism {
32 namespace io {
33 
34 //! \brief Prints an error message; for debugging.
35 static void check(const ErrorLocation &where, int return_code) {
36  if (return_code != NC_NOERR) {
37  throw RuntimeError(where, nc_strerror(return_code));
38  }
39 }
40 
42  : NC_Serial(c), m_compression_level(0) {
43  // empty
44 }
45 
46 void NC4_Serial::create_impl(const std::string &fname) {
47  int stat = NC_NOERR;
48 
49  if (m_rank == 0) {
50  stat = nc_create(fname.c_str(), NC_CLOBBER | NC_NETCDF4, &m_file_id);
51  }
52 
53  MPI_Barrier(m_com);
54  MPI_Bcast(&m_file_id, 1, MPI_INT, 0, m_com);
55  MPI_Bcast(&stat, 1, MPI_INT, 0, m_com);
56 
58 }
59 
61  m_compression_level = pism::clip(level, 0, 9);
62 }
63 
64 void NC4_Serial::def_var_impl(const std::string &name,
65  io::Type nctype,
66  const std::vector<std::string> &dims) const {
67 
68  // use the parent class to define the variable
69  NC_Serial::def_var_impl(name, nctype, dims);
70 
71  int stat = NC_NOERR;
72 
73  // set compression level (except for scalars and time-series)
74  if (m_compression_level > 0 and dims.size() > 1) {
75  if (m_rank == 0) {
76  int varid = get_varid(name);
77 
78  stat = nc_def_var_deflate(m_file_id, varid, 0, 1, m_compression_level);
79 
80  // The NetCDF version used by PISM may not support compression.
81  if (stat == NC_EINVAL) {
82  stat = NC_NOERR;
83  }
84  }
85  }
86 
87  MPI_Barrier(m_com);
88  MPI_Bcast(&stat, 1, MPI_INT, 0, m_com);
89 
91 }
92 
93 } // end of namespace io
94 } // end of namespace pism
void create_impl(const std::string &filename)
Create a NetCDF file.
Definition: NC4_Serial.cc:46
void set_compression_level_impl(int level) const
Definition: NC4_Serial.cc:60
void def_var_impl(const std::string &name, io::Type nctype, const std::vector< std::string > &dims) const
Define a variable.
Definition: NC4_Serial.cc:64
NC4_Serial(MPI_Comm c)
Definition: NC4_Serial.cc:41
MPI_Comm m_com
Definition: NCFile.hh:232
virtual void def_var_impl(const std::string &name, io::Type nctype, const std::vector< std::string > &dims) const
Define a variable.
Definition: NC_Serial.cc:252
int get_varid(const std::string &variable_name) const
Definition: NC_Serial.cc:917
#define PISM_ERROR_LOCATION
static void check(const ErrorLocation &where, int return_code)
Prints an error message; for debugging.
Definition: NC4_Par.cc:36
T clip(T x, T a, T b)