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_metadata.h"
5 #include "teca_variant_array.h"
6 #include "teca_array_collection.h"
7 #include "teca_netcdf_util.h"
8 #include "teca_mpi.h"
9 
10 #include <memory>
11 #include <vector>
12 #include <string>
13 #include <array>
14 
16 using p_teca_cf_layout_manager = std::shared_ptr<teca_cf_layout_manager>;
17 
18 /// Puts data on disk using NetCDF CF2 conventions.
20 {
21 public:
22  // allocate and return a new object. The communicator passed in will be
23  // used for collective operations, file_id uniquely identifies the file,
24  // first_index and n_indices describe the index set that will be written to
25  // the file by all ranks.
26  static p_teca_cf_layout_manager New(MPI_Comm comm,
27  long file_id, long first_index, long n_indices)
28  {
29  return p_teca_cf_layout_manager(
30  new teca_cf_layout_manager(comm, file_id,
31  first_index, n_indices));
32  }
33 
34  // creates the NetCDF file. This is an MPI collective call.
35  int create(const std::string &file_name, const std::string &date_format,
36  const teca_metadata &md_in, int mode_flags, int use_unlimited_dim);
37 
38  // defines the NetCDF file layout. This is an MPI collective call. The
39  // metadata object must contain global view of coordinates, whole_extent,
40  // and for each array to be written there must be type code in the
41  // corresponding array attributes.
42  int define(const teca_metadata &md, unsigned long *extent,
43  const std::vector<std::string> &point_arrays,
44  const std::vector<std::string> &info_arrays, int compression_level);
45 
46  // writes the collection of arrays to the NetCDF file
47  // in the correct spot.
48  int write(long index,
49  const const_p_teca_array_collection &point_arrays,
50  const const_p_teca_array_collection &info_arrays);
51 
52  // close the file. This is an MPI collective call.
53  int close() { return this->handle.close(); }
54 
55  // return true if the file is open and can be written to
56  bool opened() { return bool(this->handle); }
57 
58  // return true if the file has been defined
59  bool defined() { return this->n_dims > 0; }
60 
61  // TODO -- this is only true when a rank writes all of the steps
62  // to the given file.
63  bool completed()
64  {
65  return this->n_written == this->n_indices;
66  }
67 
68  // flush data to disk
69  int flush();
70 
71  // print a summary to the stream
72  int to_stream(std::ostream &os);
73 
74 protected:
75  teca_cf_layout_manager() : comm(MPI_COMM_SELF), file_id(-1),
76  first_index(-1), n_indices(-1), n_written(0), n_dims(0),
77  dims{0}
78  {}
79 
80  teca_cf_layout_manager(MPI_Comm fcomm,
81  long fid, long first_id, long n_ids) : comm(fcomm), file_id(fid),
82  first_index(first_id), n_indices(n_ids), n_written(0), n_dims(0),
83  dims{0}
84  {}
85 
86  // remove these for now for convenience
89  void operator=(const teca_cf_layout_manager&) = delete;
90  void operator=(const teca_cf_layout_manager&&) = delete;
91 
92 protected:
93  // communicator describing ranks that act on the file
94  MPI_Comm comm;
95 
96  // identifying the file
97  long file_id;
98  std::string file_name;
100 
101  // for identifying the incoming dataset and determining its
102  // position in the file
103  long first_index;
104  long n_indices;
105  long n_written;
106 
107  // for low level NetCDF book keeping
108  int mode_flags;
109  int use_unlimited_dim;
110  int n_dims;
111  size_t dims[4];
112 
113  struct var_def_t
114  {
115  var_def_t() : var_id(0), type_code(0), active_dims{0,0,0,0} {}
116 
117  var_def_t(int aid, unsigned int atc, const std::array<int,4> &ada) :
118  var_id(aid), type_code(atc), active_dims(ada) {}
119 
120  var_def_t(int aid, unsigned int atc) :
121  var_id(aid), type_code(atc), active_dims{0,0,0,0} {}
122 
123  int var_id;
124  unsigned int type_code;
125  std::array<int,4> active_dims;
126  };
127 
128  std::map<std::string, var_def_t> var_def;
129  std::string t_variable;
130  p_teca_double_array t;
131 };
132 
133 #endif
teca_variant_array.h
teca_metadata
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:18
teca_netcdf_util::netcdf_handle::close
int close()
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:19
teca_netcdf_util.h
teca_cf_layout_manager::var_def_t
Definition: teca_cf_layout_manager.h:113