1 #ifndef teca_netcdf_util_h
2 #define teca_netcdf_util_h
4 #include "teca_config.h"
6 #include "teca_metadata.h"
7 #include "teca_thread_pool.h"
13 #if defined(TECA_HAS_NETCDF_MPI)
14 #include <netcdf_par.h>
18 #define NC_DISPATCH_FP(tc_, code_) \
21 NC_DISPATCH_CASE(NC_FLOAT, float, code_) \
22 NC_DISPATCH_CASE(NC_DOUBLE, double, code_) \
24 TECA_ERROR("netcdf type code_ " << tc_ \
25 << " is not a floating point type") \
28 #define NC_DISPATCH(tc_, code_) \
31 NC_DISPATCH_CASE(NC_BYTE, char, code_) \
32 NC_DISPATCH_CASE(NC_UBYTE, unsigned char, code_) \
33 NC_DISPATCH_CASE(NC_CHAR, char, code_) \
34 NC_DISPATCH_CASE(NC_SHORT, short int, code_) \
35 NC_DISPATCH_CASE(NC_USHORT, unsigned short int, code_) \
36 NC_DISPATCH_CASE(NC_INT, int, code_) \
37 NC_DISPATCH_CASE(NC_UINT, unsigned int, code_) \
38 NC_DISPATCH_CASE(NC_INT64, long long, code_) \
39 NC_DISPATCH_CASE(NC_UINT64, unsigned long long, code_) \
40 NC_DISPATCH_CASE(NC_FLOAT, float, code_) \
41 NC_DISPATCH_CASE(NC_DOUBLE, double, code_) \
43 TECA_ERROR("netcdf type code " << tc_ \
44 << " is not supported") \
47 #define NC_DISPATCH_CASE(cc_, tt_, code_) \
55 namespace teca_netcdf_util
60 template<
int nc_enum>
class cpp_tt {};
62 #define DECLARE_NETCDF_TT(cpp_t_, nc_c_) \
63 template <> class netcdf_tt<cpp_t_> \
66 enum { type_code = nc_c_ }; \
67 static const char *name() { return #nc_c_; } \
69 DECLARE_NETCDF_TT(
char, NC_BYTE)
70 DECLARE_NETCDF_TT(
unsigned char, NC_UBYTE)
72 DECLARE_NETCDF_TT(
short int, NC_SHORT)
73 DECLARE_NETCDF_TT(
unsigned short int, NC_USHORT)
74 DECLARE_NETCDF_TT(
int, NC_INT)
75 DECLARE_NETCDF_TT(
long, NC_LONG)
76 DECLARE_NETCDF_TT(
unsigned long, NC_LONG)
77 DECLARE_NETCDF_TT(
unsigned int, NC_UINT)
78 DECLARE_NETCDF_TT(
long long, NC_INT64)
79 DECLARE_NETCDF_TT(
unsigned long long, NC_UINT64)
80 DECLARE_NETCDF_TT(
float, NC_FLOAT)
81 DECLARE_NETCDF_TT(
double, NC_DOUBLE)
83 #define DECLARE_CPP_TT(cpp_t_, nc_c_) \
84 template <> class cpp_tt<nc_c_> \
87 using type = cpp_t_; \
88 static const char *name() { return #cpp_t_; } \
90 DECLARE_CPP_TT(
char, NC_BYTE)
91 DECLARE_CPP_TT(
unsigned char, NC_UBYTE)
93 DECLARE_CPP_TT(
short int, NC_SHORT)
94 DECLARE_CPP_TT(
unsigned short int, NC_USHORT)
95 DECLARE_CPP_TT(
int, NC_INT)
98 DECLARE_CPP_TT(
unsigned int, NC_UINT)
99 DECLARE_CPP_TT(
long long, NC_INT64)
100 DECLARE_CPP_TT(
unsigned long long, NC_UINT64)
101 DECLARE_CPP_TT(
float, NC_FLOAT)
102 DECLARE_CPP_TT(
double, NC_DOUBLE)
106 void crtrim(
char *s,
long n);
112 std::mutex &get_netcdf_mutex();
139 m_handle = other.m_handle;
148 m_handle = other.m_handle;
154 int open(
const std::string &file_path,
int mode);
160 int open(MPI_Comm comm,
const std::string &file_path,
int mode);
164 int create(
const std::string &file_path,
int mode);
170 int create(MPI_Comm comm,
const std::string &file_path,
int mode);
183 operator bool()
const
184 {
return m_handle > 0; }
235 using data_elem_t = std::pair<p_teca_variant_array, teca_metadata>;
236 using data_t = std::pair<unsigned long, data_elem_t>;
237 using task_t = std::packaged_task<data_t()>;
239 using p_queue_t = std::shared_ptr<queue_t>;
242 unsigned long id,
const std::string &variable) : m_path(path),
243 m_file(file), m_variable(variable), m_id(
id)
247 data_t package(
unsigned long id,
248 p_teca_variant_array var =
nullptr,
251 return std::make_pair(
id, std::make_pair(var, md));
259 std::string m_variable;
278 using data_t = std::pair<unsigned long, p_teca_variant_array>;
279 using task_t = std::packaged_task<data_t()>;
281 using p_queue_t = std::shared_ptr<queue_t>;
284 read_variable(
const std::string &path,
const std::string &file,
285 unsigned long id,
const std::string &variable) : m_path(path),
286 m_file(file), m_variable(variable), m_id(
id)
290 data_t package(
unsigned long id,
291 p_teca_variant_array var =
nullptr)
293 return std::make_pair(
id, var);
301 std::string m_variable;