PISM, A Parallel Ice Sheet Model  stable v2.1.1 committed by Constantine Khrulev on 2024-12-04 13:36:58 -0900
File.hh
Go to the documentation of this file.
1 // Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, 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 #ifndef _PISM_FILE_ACCESS_H_
20 #define _PISM_FILE_ACCESS_H_
21 
22 #include <vector>
23 #include <string>
24 #include <mpi.h>
25 
26 #include "pism/util/Units.hh"
27 
28 namespace pism {
29 
30 enum AxisType : int;
31 
32 namespace io {
33 enum Type : int;
34 enum Backend : int;
35 enum Mode : int;
36 } // namespace io
37 
38 class Grid;
39 
40 /*!
41  * Convert a string to PISM's backend type.
42  */
43 io::Backend string_to_backend(const std::string &backend);
44 
46  bool exists;
48  std::string name;
49 };
50 
51 //! \brief High-level PISM I/O class.
52 /*!
53  * Hides the low-level NetCDF wrapper.
54  */
55 class File
56 {
57 public:
58  File(MPI_Comm com, const std::string &filename, io::Backend backend, io::Mode mode,
59  int iosysid = -1);
60  ~File();
61 
62  io::Backend backend() const;
63 
64  MPI_Comm com() const;
65 
66  void close();
67 
68  void redef() const;
69 
70  void enddef() const;
71 
72  void sync() const;
73 
74  std::string filename() const;
75 
76  unsigned int nrecords() const;
77 
78  unsigned int nrecords(const std::string &name, const std::string &std_name,
79  units::System::Ptr unit_system) const;
80 
81  unsigned int nvariables() const;
82 
83  unsigned int nattributes(const std::string &var_name) const;
84 
85  // dimensions
86 
87  void define_dimension(const std::string &name, size_t length) const;
88 
89  unsigned int dimension_length(const std::string &name) const;
90 
91  std::vector<std::string> dimensions(const std::string &variable_name) const;
92 
93  bool find_dimension(const std::string &name) const;
94 
95  AxisType dimension_type(const std::string &name,
96  units::System::Ptr unit_system) const;
97 
98  std::vector<double> read_dimension(const std::string &name) const;
99 
100  // variables
101 
102  std::string variable_name(unsigned int id) const;
103 
104  void define_variable(const std::string &name, io::Type nctype,
105  const std::vector<std::string> &dims) const;
106 
107  VariableLookupData find_variable(const std::string &short_name, const std::string &std_name) const;
108 
109  bool find_variable(const std::string &short_name) const;
110 
111  void read_variable(const std::string &variable_name,
112  const std::vector<unsigned int> &start,
113  const std::vector<unsigned int> &count,
114  double *ip) const;
115 
116  void read_variable_transposed(const std::string &variable_name,
117  const std::vector<unsigned int> &start,
118  const std::vector<unsigned int> &count,
119  const std::vector<unsigned int> &imap, double *ip) const;
120 
121  void write_variable(const std::string &variable_name,
122  const std::vector<unsigned int> &start,
123  const std::vector<unsigned int> &count,
124  const double *op) const;
125 
126  void write_distributed_array(const std::string &variable_name,
127  const Grid &grid,
128  unsigned int z_count,
129  bool time_dependent,
130  const double *input) const;
131 
132  void set_compression_level(int level) const;
133 
134  // attributes
135 
136  void remove_attribute(const std::string &variable_name, const std::string &att_name) const;
137 
138  std::string attribute_name(const std::string &var_name, unsigned int n) const;
139 
140  io::Type attribute_type(const std::string &var_name, const std::string &att_name) const;
141 
142  void write_attribute(const std::string &var_name, const std::string &att_name,
143  io::Type nctype, const std::vector<double> &values) const;
144 
145  void write_attribute(const std::string &var_name, const std::string &att_name,
146  const std::string &value) const;
147 
148  std::vector<double> read_double_attribute(const std::string &var_name,
149  const std::string &att_name) const;
150 
151  std::string read_text_attribute(const std::string &var_name, const std::string &att_name) const;
152 
153  void append_history(const std::string &history) const;
154 private:
155  struct Impl;
157 
158  void open(const std::string &filename, io::Mode mode);
159 
160  // disable copying and assignments
161  File(const File &other);
162  File & operator=(const File &);
163 };
164 
165 } // end of namespace pism
166 
167 #endif /* _PISM_FILE_ACCESS_H_ */
bool find_dimension(const std::string &name) const
Checks if a dimension exists.
Definition: File.cc:439
unsigned int nvariables() const
Definition: File.cc:803
Impl * m_impl
Definition: File.hh:155
void write_distributed_array(const std::string &variable_name, const Grid &grid, unsigned int z_count, bool time_dependent, const double *input) const
Definition: File.cc:773
void read_variable(const std::string &variable_name, const std::vector< unsigned int > &start, const std::vector< unsigned int > &count, double *ip) const
Definition: File.cc:747
void read_variable_transposed(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: File.cc:791
io::Backend backend() const
Definition: File.cc:218
void define_dimension(const std::string &name, size_t length) const
Definition: File.cc:563
std::string attribute_name(const std::string &var_name, unsigned int n) const
Definition: File.cc:723
AxisType dimension_type(const std::string &name, units::System::Ptr unit_system) const
Get the "type" of a dimension.
Definition: File.cc:493
void set_compression_level(int level) const
Definition: File.cc:222
void open(const std::string &filename, io::Mode mode)
Definition: File.cc:226
void redef() const
Definition: File.cc:288
VariableLookupData find_variable(const std::string &short_name, const std::string &std_name) const
Find a variable using its standard name and/or short name.
Definition: File.cc:361
void enddef() const
Definition: File.cc:298
unsigned int nrecords() const
Get the number of records. Uses the length of an unlimited dimension.
Definition: File.cc:313
MPI_Comm com() const
Definition: File.cc:214
std::string variable_name(unsigned int id) const
Definition: File.cc:816
~File()
Definition: File.cc:201
std::string filename() const
Definition: File.cc:307
void write_variable(const std::string &variable_name, const std::vector< unsigned int > &start, const std::vector< unsigned int > &count, const double *op) const
Definition: File.cc:760
void sync() const
Definition: File.cc:279
void close()
Definition: File.cc:270
void define_variable(const std::string &name, io::Type nctype, const std::vector< std::string > &dims) const
Define a variable.
Definition: File.cc:573
io::Type attribute_type(const std::string &var_name, const std::string &att_name) const
Definition: File.cc:735
void remove_attribute(const std::string &variable_name, const std::string &att_name) const
Definition: File.cc:261
void write_attribute(const std::string &var_name, const std::string &att_name, io::Type nctype, const std::vector< double > &values) const
Write a multiple-valued double attribute.
Definition: File.cc:638
std::vector< double > read_double_attribute(const std::string &var_name, const std::string &att_name) const
Get a double attribute.
Definition: File.cc:665
unsigned int nattributes(const std::string &var_name) const
Definition: File.cc:711
void append_history(const std::string &history) const
Append to the history global attribute.
Definition: File.cc:626
unsigned int dimension_length(const std::string &name) const
Get the length of a dimension.
Definition: File.cc:454
std::vector< double > read_dimension(const std::string &name) const
Get dimension data (a coordinate variable).
Definition: File.cc:603
File(const File &other)
std::vector< std::string > dimensions(const std::string &variable_name) const
Definition: File.cc:425
std::string read_text_attribute(const std::string &var_name, const std::string &att_name) const
Get a text attribute.
Definition: File.cc:693
File & operator=(const File &)
File(MPI_Comm com, const std::string &filename, io::Backend backend, io::Mode mode, int iosysid=-1)
Definition: File.cc:180
High-level PISM I/O class.
Definition: File.hh:56
Describes the PISM grid and the distribution of data across processors.
Definition: Grid.hh:282
std::shared_ptr< System > Ptr
Definition: Units.hh:47
#define n
Definition: exactTestM.c:37
AxisType
Definition: IO_Flags.hh:33
io::Backend string_to_backend(const std::string &backend)
Definition: File.cc:57
std::string name
Definition: File.hh:48
bool found_using_standard_name
Definition: File.hh:47
int count
Definition: test_cube.c:16