TECA
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 
15 using p_teca_cf_layout_manager = std::shared_ptr<teca_cf_layout_manager>;
16 
17 // puts data on disk using NetCDF CF2 conventions
19 {
20 public:
21  // allocate and return a new object. The communicator passed in will be
22  // used for collective operations, file_id uniquely identifies the file,
23  // first_index and n_indices describe the index set that will be written to
24  // the file by all ranks.
25  static p_teca_cf_layout_manager New(MPI_Comm comm,
26  long file_id, long first_index, long n_indices)
27  {
28  return p_teca_cf_layout_manager(
29  new teca_cf_layout_manager(comm, file_id,
30  first_index, n_indices));
31  }
32 
33  // creates the NetCDF file. This is an MPI collective call.
34  int create(const std::string &file_name, const std::string &date_format,
35  const teca_metadata &md_in, int mode_flags, int use_unlimited_dim);
36 
37  // defines the NetCDF file layout. This is an MPI collective call. The
38  // metadata object must contain global view of coordinates, whole_extent,
39  // and for each array to be written there must be type code in the
40  // corresponding array attributes.
41  int define(const teca_metadata &md, unsigned long *extent,
42  const std::vector<std::string> &point_arrays,
43  const std::vector<std::string> &info_arrays, int compression_level);
44 
45  // writes the colllection of arrays to the NetCDF file
46  // in the correct spot.
47  int write(long index,
48  const const_p_teca_array_collection &point_arrays,
49  const const_p_teca_array_collection &info_arrays);
50 
51  // close the file. This is an MPI collecive call.
52  int close() { return this->handle.close(); }
53 
54  // return true if the file is open and can be written to
55  bool opened() { return bool(this->handle); }
56 
57  // return true if the file has been defined
58  bool defined() { return this->n_dims > 0; }
59 
60  // TODO -- this is no longer correct
61  bool completed()
62  {
63  return this->n_written == this->n_indices;
64  }
65 
66  // flush data to disk
67  int flush();
68 
69  // print a summary to the stream
70  int to_stream(std::ostream &os);
71 
72 protected:
73  teca_cf_layout_manager() : comm(MPI_COMM_SELF), file_id(-1),
74  first_index(-1), n_indices(-1)
75  {}
76 
77  teca_cf_layout_manager(MPI_Comm fcomm,
78  long fid, long first_id, long n_ids) : comm(fcomm), file_id(fid),
79  first_index(first_id), n_indices(n_ids), n_written(0), n_dims(0),
80  dims{0}
81  {}
82 
83  // remove these for now for convenience
86  void operator=(const teca_cf_layout_manager&) = delete;
87  void operator=(const teca_cf_layout_manager&&) = delete;
88 
89 protected:
90  // communicator decribing ranks that act on the file
91  MPI_Comm comm;
92 
93  // identifying the file
94  long file_id;
95  std::string file_name;
97 
98  // for indentifying the incoming dataset and determining its
99  // position in the file
100  long first_index;
101  long n_indices;
102  long n_written;
103 
104  // for low level NetCDF book keeping
105  int mode_flags;
106  int use_unlimited_dim;
107  int n_dims;
108  size_t dims[4];
109  using var_def_t = std::pair<int,unsigned int>;
110  std::map<std::string, var_def_t> var_def;
111  std::string t_variable;
112  p_teca_double_array t;
113 };
114 
115 #endif
teca_metadata
Definition: teca_metadata.h:17
teca_netcdf_util::netcdf_handle
Definition: teca_netcdf_util.h:116
teca_cf_layout_manager
Definition: teca_cf_layout_manager.h:19