PISM, A Parallel Ice Sheet Model  stable v2.0.4 committed by Constantine Khrulev on 2022-05-25 12:02:27 -0800
File.hh
Go to the documentation of this file.
1 // Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2020 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 #include "pism/util/io/IO_Flags.hh"
28 
29 namespace pism {
30 
32 
33 class IceGrid;
34 
35 /*!
36  * Convert a string to PISM's backend type.
37  */
38 IO_Backend string_to_backend(const std::string &backend);
39 
41  bool exists;
43  std::string name;
44 };
45 
46 //! \brief High-level PISM I/O class.
47 /*!
48  * Hides the low-level NetCDF wrapper.
49  */
50 class File
51 {
52 public:
53  File(MPI_Comm com, const std::string &filename, IO_Backend backend, IO_Mode mode,
54  int iosysid = -1);
55  ~File();
56 
57  IO_Backend backend() const;
58 
59  MPI_Comm com() const;
60 
61  void close();
62 
63  void redef() const;
64 
65  void enddef() const;
66 
67  void sync() const;
68 
69  std::string filename() const;
70 
71  unsigned int nrecords() const;
72 
73  unsigned int nrecords(const std::string &name, const std::string &std_name,
74  units::System::Ptr unit_system) const;
75 
76  unsigned int nvariables() const;
77 
78  unsigned int nattributes(const std::string &var_name) const;
79 
80  // dimensions
81 
82  void define_dimension(const std::string &name, size_t length) const;
83 
84  unsigned int dimension_length(const std::string &name) const;
85 
86  std::vector<std::string> dimensions(const std::string &variable_name) const;
87 
88  bool find_dimension(const std::string &name) const;
89 
90  AxisType dimension_type(const std::string &name,
91  units::System::Ptr unit_system) const;
92 
93  std::vector<double> read_dimension(const std::string &name) const;
94 
95  // variables
96 
97  std::string variable_name(unsigned int id) const;
98 
99  void define_variable(const std::string &name, IO_Type nctype,
100  const std::vector<std::string> &dims) const;
101 
102  VariableLookupData find_variable(const std::string &short_name, const std::string &std_name) const;
103 
104  bool find_variable(const std::string &short_name) const;
105 
106  void read_variable(const std::string &variable_name,
107  const std::vector<unsigned int> &start,
108  const std::vector<unsigned int> &count,
109  double *ip) const;
110 
111  void read_variable_transposed(const std::string &variable_name,
112  const std::vector<unsigned int> &start,
113  const std::vector<unsigned int> &count,
114  const std::vector<unsigned int> &imap, double *ip) const;
115 
116  void write_variable(const std::string &variable_name,
117  const std::vector<unsigned int> &start,
118  const std::vector<unsigned int> &count,
119  const double *op) const;
120 
121  void write_distributed_array(const std::string &variable_name,
122  const IceGrid &grid,
123  unsigned int z_count,
124  const double *input) const;
125 
126  void set_compression_level(int level) const;
127 
128  // attributes
129 
130  void remove_attribute(const std::string &variable_name, const std::string &att_name) const;
131 
132  std::string attribute_name(const std::string &var_name, unsigned int n) const;
133 
134  IO_Type attribute_type(const std::string &var_name, const std::string &att_name) const;
135 
136  void write_attribute(const std::string &var_name, const std::string &att_name,
137  IO_Type nctype, const std::vector<double> &values) const;
138 
139  void write_attribute(const std::string &var_name, const std::string &att_name,
140  const std::string &value) const;
141 
142  std::vector<double> read_double_attribute(const std::string &var_name,
143  const std::string &att_name) const;
144 
145  std::string read_text_attribute(const std::string &var_name, const std::string &att_name) const;
146 
147  void append_history(const std::string &history) const;
148 private:
149  struct Impl;
151 
152  void open(const std::string &filename, IO_Mode mode);
153 
154  // disable copying and assignments
155  File(const File &other);
156  File & operator=(const File &);
157 };
158 
159 } // end of namespace pism
160 
161 #endif /* _PISM_FILE_ACCESS_H_ */
bool find_dimension(const std::string &name) const
Checks if a dimension exists.
Definition: File.cc:442
unsigned int nvariables() const
Definition: File.cc:795
Impl * m_impl
Definition: File.hh:149
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:740
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:783
void define_dimension(const std::string &name, size_t length) const
Definition: File.cc:556
std::string attribute_name(const std::string &var_name, unsigned int n) const
Definition: File.cc:716
void write_distributed_array(const std::string &variable_name, const IceGrid &grid, unsigned int z_count, const double *input) const
Definition: File.cc:766
AxisType dimension_type(const std::string &name, units::System::Ptr unit_system) const
Get the "type" of a dimension.
Definition: File.cc:476
void set_compression_level(int level) const
Definition: File.cc:225
void define_variable(const std::string &name, IO_Type nctype, const std::vector< std::string > &dims) const
Define a variable.
Definition: File.cc:566
File(MPI_Comm com, const std::string &filename, IO_Backend backend, IO_Mode mode, int iosysid=-1)
Definition: File.cc:183
void redef() const
Definition: File.cc:291
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:364
void enddef() const
Definition: File.cc:301
unsigned int nrecords() const
Get the number of records. Uses the length of an unlimited dimension.
Definition: File.cc:316
MPI_Comm com() const
Definition: File.cc:217
std::string variable_name(unsigned int id) const
Definition: File.cc:808
~File()
Definition: File.cc:204
std::string filename() const
Definition: File.cc:310
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:753
void sync() const
Definition: File.cc:282
void close()
Definition: File.cc:273
void remove_attribute(const std::string &variable_name, const std::string &att_name) const
Definition: File.cc:264
std::vector< double > read_double_attribute(const std::string &var_name, const std::string &att_name) const
Get a double attribute.
Definition: File.cc:658
void open(const std::string &filename, IO_Mode mode)
Definition: File.cc:229
IO_Backend backend() const
Definition: File.cc:221
unsigned int nattributes(const std::string &var_name) const
Definition: File.cc:704
void append_history(const std::string &history) const
Append to the history global attribute.
Definition: File.cc:619
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:631
unsigned int dimension_length(const std::string &name) const
Get the length of a dimension.
Definition: File.cc:457
std::vector< double > read_dimension(const std::string &name) const
Get dimension data (a coordinate variable).
Definition: File.cc:596
File(const File &other)
IO_Type attribute_type(const std::string &var_name, const std::string &att_name) const
Definition: File.cc:728
std::vector< std::string > dimensions(const std::string &variable_name) const
Definition: File.cc:428
std::string read_text_attribute(const std::string &var_name, const std::string &att_name) const
Get a text attribute.
Definition: File.cc:686
File & operator=(const File &)
High-level PISM I/O class.
Definition: File.hh:51
Describes the PISM grid and the distribution of data across processors.
Definition: IceGrid.hh:228
std::shared_ptr< System > Ptr
Definition: Units.hh:47
#define n
Definition: exactTestM.c:37
IO_Type
Definition: IO_Flags.hh:31
IO_Backend string_to_backend(const std::string &backend)
Definition: File.cc:60
IO_Backend
Definition: IO_Flags.hh:41
IO_Mode
Definition: IO_Flags.hh:47
AxisType
Definition: File.hh:31
@ UNKNOWN_AXIS
Definition: File.hh:31
@ T_AXIS
Definition: File.hh:31
@ X_AXIS
Definition: File.hh:31
@ Z_AXIS
Definition: File.hh:31
@ Y_AXIS
Definition: File.hh:31
std::string name
Definition: File.hh:43
bool found_using_standard_name
Definition: File.hh:42
int count
Definition: test_cube.c:16