TECA
The Toolkit for Extreme Climate Analysis
teca_cf_layout_manager.h
1 #ifndef teca_cf_layout_manager_h
2 #define teca_cf_layout_manager_h
3 
4 #include "teca_config.h"
5 #include "teca_metadata.h"
6 #include "teca_variant_array.h"
7 #include "teca_array_collection.h"
8 #include "teca_netcdf_util.h"
9 #include "teca_mpi.h"
10 
11 #include <memory>
12 #include <vector>
13 #include <string>
14 #include <array>
15 
17 using p_teca_cf_layout_manager = std::shared_ptr<teca_cf_layout_manager>;
18 
19 /// Puts data on disk using NetCDF CF2 conventions.
21 {
22 public:
23  // allocate and return a new object. The communicator passed in will be
24  // used for collective operations, file_id uniquely identifies the file,
25  // first_index and n_indices describe the index set that will be written to
26  // the file by all ranks.
27  static p_teca_cf_layout_manager New(MPI_Comm comm,
28  long file_id, long first_index, long n_indices)
29  {
30  return p_teca_cf_layout_manager(
31  new teca_cf_layout_manager(comm, file_id,
32  first_index, n_indices));
33  }
34 
35  /// creates the NetCDF file. This is an MPI collective call.
36  int create(const std::string &file_name, const std::string &date_format,
37  const teca_metadata &md_in, int mode_flags, int use_unlimited_dim);
38 
39  /** defines the NetCDF file layout. This is an MPI collective call. The
40  * metadata object must contain global view of coordinates, whole_extent,
41  * and for each array to be written there must be type code in the
42  * corresponding array attributes.
43  *
44  * @param[in] md a metadata object compatible with that provided by the
45  * teca_cf_reader
46  * @param[in] whole_extent the extent of data that will be written to disk,
47  * including runtime specified subsetting.
48  * @param[in] point_arrays a list of point centered array names to write
49  * @param[in] info_arrays a list of point centered array names to write
50  * @param[in] collective_buffer set to zero to disable collective buffering
51  * @param[in] compression_level set greater than 1 to enable compression.
52  * this is incomatible with MPI parallel I/O and cannot be used
53  * in a parallel setting.
54  * @param[in] move_vars_to_root move variable in groups to root of file
55  * (and do not create groups)
56  *
57  * @returns zero if successful
58  */
59  int define(const teca_metadata &md, unsigned long *whole_extent,
60  const std::vector<std::string> &point_arrays,
61  const std::vector<std::string> &info_arrays,
62  int collective_buffer, int compression_level,
63  bool move_vars_to_root);
64 
65  /// writes the collection of arrays to the NetCDF file in the correct spot.
66  int write(long index,
67  const const_p_teca_array_collection &point_arrays,
68  const const_p_teca_array_collection &info_arrays);
69 
70  /** Writes the collection of arrays defined over a spatio-temporal extent
71  * to the NetCDF file in the correct location in the file.
72  *
73  * @param[in] extent the spatial extent of the arrays
74  * @param[in] temporal_extent the temporal extent of the arrays
75  * @param[in] point_arrays a collection of point centered data arrays to
76  * write
77  * @param[in] info_arrays a collection of non-geometrically oriented arrays
78  * to write
79  *
80  * @returns zero if successful
81  */
82  int write(const unsigned long extent[6],
83  const unsigned long temporal_extent[2],
84  const const_p_teca_array_collection &point_arrays,
85  const const_p_teca_array_collection &info_arrays);
86 
87  // close the file. This is an MPI collective call.
88  int close() { return this->handle.close(); }
89 
90  // return true if the file is open and can be written to
91  bool opened() { return bool(this->handle); }
92 
93  // return true if the file has been defined
94  bool defined() { return this->n_dims > 0; }
95 
96  // TODO -- this is only true when a rank writes all of the steps
97  // to the given file.
98  bool completed()
99  {
100  return this->n_written == this->n_indices;
101  }
102 
103  // flush data to disk
104  int flush();
105 
106  // print a summary to the stream
107  int to_stream(std::ostream &os);
108 
109 protected:
110  teca_cf_layout_manager() : comm(MPI_COMM_SELF), file_id(-1),
111  first_index(-1), n_indices(-1), n_written(0), n_dims(0),
112  dims{0}
113  {}
114 
115  teca_cf_layout_manager(MPI_Comm fcomm,
116  long fid, long first_id, long n_ids) : comm(fcomm), file_id(fid),
117  first_index(first_id), n_indices(n_ids), n_written(0), n_dims(0),
118  dims{0}
119  {}
120 
121  // remove these for now for convenience
124  void operator=(const teca_cf_layout_manager&) = delete;
125  void operator=(const teca_cf_layout_manager&&) = delete;
126 
127 protected:
128  // communicator describing ranks that act on the file
129  MPI_Comm comm;
130 
131  // identifying the file
132  long file_id;
133  std::string file_name;
135 
136  // for identifying the incoming dataset and determining its
137  // position in the file
138  long first_index;
139  long n_indices;
140  long n_written;
141 
142  // for low level NetCDF book keeping
143  int mode_flags;
144  int use_unlimited_dim;
145  int n_dims;
146  size_t dims[4];
147  int mesh_axis[4];
148  unsigned long whole_extent[6];
149 
150  struct var_def_t
151  {
152  var_def_t() : parent_id(0), var_id(0), type_code(0), active_dims{0,0,0,0} {}
153 
154  var_def_t(int pid, int aid, unsigned int atc, const std::array<int,4> &ada) :
155  parent_id(pid), var_id(aid), type_code(atc), active_dims(ada) {}
156 
157  var_def_t(int pid, int aid, unsigned int atc) :
158  parent_id(pid), var_id(aid), type_code(atc), active_dims{0,0,0,0} {}
159 
160  int parent_id;
161  int var_id;
162  unsigned int type_code;
163  std::array<int,4> active_dims;
164  };
165 
166  std::map<std::string, var_def_t> var_def;
167  std::string t_variable;
169 };
170 
171 #endif
Puts data on disk using NetCDF CF2 conventions.
Definition: teca_cf_layout_manager.h:21
int define(const teca_metadata &md, unsigned long *whole_extent, const std::vector< std::string > &point_arrays, const std::vector< std::string > &info_arrays, int collective_buffer, int compression_level, bool move_vars_to_root)
int write(const unsigned long extent[6], const unsigned long temporal_extent[2], const const_p_teca_array_collection &point_arrays, const const_p_teca_array_collection &info_arrays)
int write(long index, const const_p_teca_array_collection &point_arrays, const const_p_teca_array_collection &info_arrays)
writes the collection of arrays to the NetCDF file in the correct spot.
int create(const std::string &file_name, const std::string &date_format, const teca_metadata &md_in, int mode_flags, int use_unlimited_dim)
creates the NetCDF file. This is an MPI collective call.
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:22
A RAII class for managing NETCDF files. The file is kept open while the object exists.
Definition: teca_netcdf_util.h:133
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
auto New(size_t n_elem, teca_variant_array::allocator alloc=teca_variant_array::allocator::malloc)
Definition: teca_variant_array_util.h:269
Definition: teca_cf_layout_manager.h:151
std::shared_ptr< teca_variant_array > p_teca_variant_array
Definition: teca_variant_array.h:27