PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
io_helpers.hh
Go to the documentation of this file.
1 /* Copyright (C) 2015, 2016, 2017, 2018, 2019, 2020, 2021, 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 
20 #ifndef PISM_IO_HELPERS_H
21 #define PISM_IO_HELPERS_H
22 
23 #include <string>
24 #include <vector>
25 #include <mpi.h>
26 
27 #include "IO_Flags.hh"
28 #include "LocalInterpCtx.hh"
29 #include "pism/util/Units.hh"
30 
31 namespace pism {
32 
33 class VariableMetadata;
34 class SpatialVariableMetadata;
35 class Grid;
36 class File;
37 class Time;
38 class Logger;
39 class Context;
40 class Config;
41 
42 namespace grid {
43 class InputGridInfo;
44 }
45 
46 enum InterpolationType : int;
47 
48 class LocalInterpCtx;
49 
50 namespace io {
51 
52 enum Type : int;
53 
54 void check_input_grid(const grid::InputGridInfo &input_grid,
55  const Grid& internal_grid,
56  const std::vector<double> &internal_z_levels);
57 
58 void regrid_spatial_variable(SpatialVariableMetadata &variable,
59  const Grid& internal_grid,
60  const LocalInterpCtx &lic,
61  const File &file,
62  double *output);
63 
64 void read_spatial_variable(const SpatialVariableMetadata &variable,
65  const Grid& grid, const File &file,
66  unsigned int time, double *output);
67 
68 void write_spatial_variable(const SpatialVariableMetadata &metadata,
69  const Grid& grid, const File &file,
70  const double *input);
71 
72 void define_dimension(const File &nc, unsigned long int length,
73  const VariableMetadata &metadata);
74 
75 void define_time(const File &file, const Context &ctx);
76 
77 void define_time(const File &nc, const std::string &name, const std::string &calendar,
78  const std::string &units, units::System::Ptr unit_system);
79 
80 void append_time(const File &file, const Config &ctx, double time_seconds);
81 void append_time(const File &nc, const std::string &name, double time_seconds);
82 
83 void define_spatial_variable(const SpatialVariableMetadata &metadata,
84  const Grid &grid, const File &file,
85  io::Type default_type);
86 
87 void define_timeseries(const VariableMetadata& var,
88  const std::string &dimension_name,
89  const File &nc, io::Type nctype);
90 
91 void define_time_bounds(const VariableMetadata& metadata,
92  const std::string &dimension_name,
93  const std::string &bounds_name,
94  const File &file, io::Type nctype);
95 
96 void read_timeseries(const File &nc, const VariableMetadata &metadata,
97  const Logger &log, std::vector<double> &data);
98 
99 void write_timeseries(const File &nc, const VariableMetadata &metadata,
100  size_t t_start, const std::vector<double> &data);
101 
102 void read_time_bounds(const File &file,
103  const VariableMetadata &metadata,
104  const Logger &log, std::vector<double> &data);
105 
106 void write_time_bounds(const File &file, const VariableMetadata &metadata,
107  size_t t_start, const std::vector<double> &data);
108 
109 void read_time_info(const Logger &log,
110  std::shared_ptr<units::System> unit_system,
111  const File &file,
112  const std::string &time_name,
113  const std::string &time_units,
114  std::vector<double> &times,
115  std::vector<double> &bounds);
116 
117 std::string time_dimension(units::System::Ptr unit_system,
118  const File &file,
119  const std::string &variable_name);
120 
121 void read_attributes(const File &file, const std::string &variable_name, VariableMetadata &variable);
122 
123 void write_attributes(const File &file, const VariableMetadata &variable, io::Type nctype);
124 
125 void read_valid_range(const File &file, const std::string &name, VariableMetadata &variable);
126 
127 bool file_exists(MPI_Comm com, const std::string &filename);
128 
129 void move_if_exists(MPI_Comm com, const std::string &file_to_move, int rank_to_use = 0);
130 
131 void remove_if_exists(MPI_Comm com, const std::string &file_to_remove, int rank_to_use = 0);
132 
133 } // end of namespace io
134 } // end of namespace pism
135 
136 #endif /* PISM_IO_HELPERS_H */
std::shared_ptr< System > Ptr
Definition: Units.hh:47
void append_time(const File &file, const Config &config, double time_seconds)
Prepare a file for output.
Definition: io_helpers.cc:247
void regrid_spatial_variable(SpatialVariableMetadata &variable, const Grid &internal_grid, const LocalInterpCtx &lic, const File &file, double *output)
Regrid from a NetCDF file into a distributed array output.
Definition: io_helpers.cc:793
void read_spatial_variable(const SpatialVariableMetadata &variable, const Grid &grid, const File &file, unsigned int time, double *output)
Read a variable from a file into an array output.
Definition: io_helpers.cc:528
void read_timeseries(const File &file, const VariableMetadata &metadata, const Logger &log, std::vector< double > &data)
Read a time-series variable from a NetCDF file to a vector of doubles.
Definition: io_helpers.cc:860
void define_dimension(const File &file, unsigned long int length, const VariableMetadata &metadata)
Define a dimension and the associated coordinate variable. Set attributes.
Definition: io_helpers.cc:195
void write_spatial_variable(const SpatialVariableMetadata &metadata, const Grid &grid, const File &file, const double *input)
Write a double array to a file.
Definition: io_helpers.cc:624
void write_attributes(const File &file, const VariableMetadata &variable, io::Type nctype)
Write variable attributes to a NetCDF file.
Definition: io_helpers.cc:1200
void define_time_bounds(const VariableMetadata &var, const std::string &dimension_name, const std::string &bounds_name, const File &file, io::Type nctype)
Definition: io_helpers.cc:951
void check_input_grid(const grid::InputGridInfo &input_grid, const Grid &internal_grid, const std::vector< double > &internal_z_levels)
Check that x, y, and z coordinates of the input grid are strictly increasing.
Definition: io_helpers.cc:757
void write_time_bounds(const File &file, const VariableMetadata &metadata, size_t t_start, const std::vector< double > &data)
Definition: io_helpers.cc:1057
void read_time_info(const Logger &log, std::shared_ptr< units::System > unit_system, const File &file, const std::string &time_name, const std::string &time_units, std::vector< double > &times, std::vector< double > &bounds)
Definition: io_helpers.cc:1092
void define_spatial_variable(const SpatialVariableMetadata &metadata, const Grid &grid, const File &file, io::Type default_type)
Define a NetCDF variable corresponding to a VariableMetadata object.
Definition: io_helpers.cc:467
void read_time_bounds(const File &file, const VariableMetadata &metadata, const Logger &log, std::vector< double > &data)
Definition: io_helpers.cc:974
void move_if_exists(MPI_Comm com, const std::string &file_to_move, int rank_to_use)
Moves the file aside (file.nc -> file.nc~).
Definition: io_helpers.cc:1362
void read_attributes(const File &file, const std::string &variable_name, VariableMetadata &variable)
Definition: io_helpers.cc:1162
void write_timeseries(const File &file, const VariableMetadata &metadata, size_t t_start, const std::vector< double > &data)
Write a time-series data to a file.
Definition: io_helpers.cc:926
bool file_exists(MPI_Comm com, const std::string &filename)
Definition: io_helpers.cc:1144
void remove_if_exists(MPI_Comm com, const std::string &file_to_remove, int rank_to_use)
Check if a file is present are remove it.
Definition: io_helpers.cc:1397
void read_valid_range(const File &file, const std::string &name, VariableMetadata &variable)
Read the valid range information from a file.
Definition: io_helpers.cc:1297
std::string time_dimension(units::System::Ptr unit_system, const File &file, const std::string &variable_name)
Definition: io_helpers.cc:1343
void define_time(const File &file, const Context &ctx)
Prepare a file for output.
Definition: io_helpers.cc:213
void define_timeseries(const VariableMetadata &var, const std::string &dimension_name, const File &file, io::Type nctype)
Define a NetCDF variable corresponding to a time-series.
Definition: io_helpers.cc:839
InterpolationType
static std::string calendar(const File *input_file, const Config &config, const Logger &log)
Definition: Time.cc:146