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  int define(const teca_metadata &md, unsigned long *extent,
44  const std::vector<std::string> &point_arrays,
45  const std::vector<std::string> &info_arrays, int compression_level);
46 
47  // writes the collection of arrays to the NetCDF file
48  // in the correct spot.
49  int write(long index,
50  const const_p_teca_array_collection &point_arrays,
51  const const_p_teca_array_collection &info_arrays);
52 
53  // close the file. This is an MPI collective call.
54  int close() { return this->handle.close(); }
55 
56  // return true if the file is open and can be written to
57  bool opened() { return bool(this->handle); }
58 
59  // return true if the file has been defined
60  bool defined() { return this->n_dims > 0; }
61 
62  // TODO -- this is only true when a rank writes all of the steps
63  // to the given file.
64  bool completed()
65  {
66  return this->n_written == this->n_indices;
67  }
68 
69  // flush data to disk
70  int flush();
71 
72  // print a summary to the stream
73  int to_stream(std::ostream &os);
74 
75 protected:
76  teca_cf_layout_manager() : comm(MPI_COMM_SELF), file_id(-1),
77  first_index(-1), n_indices(-1), n_written(0), n_dims(0),
78  dims{0}
79  {}
80 
81  teca_cf_layout_manager(MPI_Comm fcomm,
82  long fid, long first_id, long n_ids) : comm(fcomm), file_id(fid),
83  first_index(first_id), n_indices(n_ids), n_written(0), n_dims(0),
84  dims{0}
85  {}
86 
87  // remove these for now for convenience
90  void operator=(const teca_cf_layout_manager&) = delete;
91  void operator=(const teca_cf_layout_manager&&) = delete;
92 
93 protected:
94  // communicator describing ranks that act on the file
95  MPI_Comm comm;
96 
97  // identifying the file
98  long file_id;
99  std::string file_name;
101 
102  // for identifying the incoming dataset and determining its
103  // position in the file
104  long first_index;
105  long n_indices;
106  long n_written;
107 
108  // for low level NetCDF book keeping
109  int mode_flags;
110  int use_unlimited_dim;
111  int n_dims;
112  size_t dims[4];
113 
114  struct var_def_t
115  {
116  var_def_t() : var_id(0), type_code(0), active_dims{0,0,0,0} {}
117 
118  var_def_t(int aid, unsigned int atc, const std::array<int,4> &ada) :
119  var_id(aid), type_code(atc), active_dims(ada) {}
120 
121  var_def_t(int aid, unsigned int atc) :
122  var_id(aid), type_code(atc), active_dims{0,0,0,0} {}
123 
124  int var_id;
125  unsigned int type_code;
126  std::array<int,4> active_dims;
127  };
128 
129  std::map<std::string, var_def_t> var_def;
130  std::string t_variable;
132 };
133 
134 #endif
teca_variant_array.h
teca_metadata
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:21
teca_netcdf_util::netcdf_handle
A RAII class for managing NETCDF files. The file is kept open while the object exists.
Definition: teca_netcdf_util.h:126
teca_cf_layout_manager
Puts data on disk using NetCDF CF2 conventions.
Definition: teca_cf_layout_manager.h:20
teca_netcdf_util.h
p_teca_variant_array
std::shared_ptr< teca_variant_array > p_teca_variant_array
Definition: teca_variant_array.h:27
teca_cf_layout_manager::var_def_t
Definition: teca_cf_layout_manager.h:114
teca_error::TECA_EXPORT
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.