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, int stride);
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  int stride);
87 
88  // close the file. This is an MPI collective call.
89  int close() { return this->handle.close(); }
90 
91  // return true if the file is open and can be written to
92  bool opened() { return bool(this->handle); }
93 
94  // return true if the file has been defined
95  bool defined() { return this->n_dims > 0; }
96 
97  // TODO -- this is only true when a rank writes all of the steps
98  // to the given file.
99  bool completed()
100  {
101  return this->n_written == this->n_indices;
102  }
103 
104  // flush data to disk
105  int flush();
106 
107  // print a summary to the stream
108  int to_stream(std::ostream &os);
109 
110 protected:
111  teca_cf_layout_manager() : comm(MPI_COMM_SELF), file_id(-1),
112  first_index(-1), n_indices(-1), n_written(0), n_dims(0),
113  dims{0}
114  {}
115 
116  teca_cf_layout_manager(MPI_Comm fcomm,
117  long fid, long first_id, long n_ids) : comm(fcomm), file_id(fid),
118  first_index(first_id), n_indices(n_ids), n_written(0), n_dims(0),
119  dims{0}
120  {}
121 
122  // remove these for now for convenience
125  void operator=(const teca_cf_layout_manager&) = delete;
126  void operator=(const teca_cf_layout_manager&&) = delete;
127 
128 protected:
129  // communicator describing ranks that act on the file
130  MPI_Comm comm;
131 
132  // identifying the file
133  long file_id;
134  std::string file_name;
136 
137  // for identifying the incoming dataset and determining its
138  // position in the file
139  long first_index;
140  long n_indices;
141  long n_written;
142 
143  // for low level NetCDF book keeping
144  int mode_flags;
145  int use_unlimited_dim;
146  int n_dims;
147  size_t dims[4];
148  int mesh_axis[4];
149  unsigned long whole_extent[6];
150 
151  struct var_def_t
152  {
153  var_def_t() : parent_id(0), var_id(0), type_code(0), active_dims{0,0,0,0} {}
154 
155  var_def_t(int pid, int aid, unsigned int atc, const std::array<int,4> &ada) :
156  parent_id(pid), var_id(aid), type_code(atc), active_dims(ada) {}
157 
158  var_def_t(int pid, int aid, unsigned int atc) :
159  parent_id(pid), var_id(aid), type_code(atc), active_dims{0,0,0,0} {}
160 
161  int parent_id;
162  int var_id;
163  unsigned int type_code;
164  std::array<int,4> active_dims;
165  };
166 
167  std::map<std::string, var_def_t> var_def;
168  std::string t_variable;
170 };
171 
172 #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(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, int stride)
creates the NetCDF file. This is an MPI collective call.
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 stride)
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:152
std::shared_ptr< teca_variant_array > p_teca_variant_array
Definition: teca_variant_array.h:27