|
| 1 | +/// nc-complex: A lightweight, drop-in extension for complex number support in |
| 2 | +/// netCDF |
| 3 | +/// |
| 4 | +/// Copyright (C) 2023 Peter Hill |
| 5 | +/// |
| 6 | +/// SPDX-License-Identifier: MIT |
| 7 | + |
| 8 | +#ifndef PLASMA_FAIR_NC_COMPLEX |
| 9 | +#define PLASMA_FAIR_NC_COMPLEX |
| 10 | + |
| 11 | +// This header is required when building as a DLL on Windows and is |
| 12 | +// automatically generated by CMake. If you're not using CMake (and |
| 13 | +// not on Windows) for some reason, then define `NC_COMPLEX_NO_EXPORT` |
| 14 | +// to skip this. |
| 15 | +#ifndef NC_COMPLEX_NO_EXPORT |
| 16 | +#include "nc_complex/nc_complex_export.h" |
| 17 | +#else |
| 18 | +#define NC_COMPLEX_EXPORT |
| 19 | +#endif |
| 20 | + |
| 21 | +#include <complex.h> |
| 22 | +#include <netcdf.h> |
| 23 | +#include <stdbool.h> |
| 24 | +#include <stddef.h> |
| 25 | + |
| 26 | +#ifdef __cplusplus |
| 27 | +#include <complex> |
| 28 | +#endif |
| 29 | + |
| 30 | +//@{ |
| 31 | +/// Portable typedefs for complex numbers |
| 32 | +/// |
| 33 | +/// These become aliases for `std::complex` with C++. |
| 34 | +#ifdef _MSC_VER |
| 35 | +typedef _Dcomplex double_complex; |
| 36 | +typedef _Fcomplex float_complex; |
| 37 | +#else |
| 38 | +#if defined(__cplusplus) && defined(__clang__) |
| 39 | +using double_complex = std::complex<double>; |
| 40 | +using float_complex = std::complex<float>; |
| 41 | +#else |
| 42 | +typedef double _Complex double_complex; |
| 43 | +typedef float _Complex float_complex; |
| 44 | +#endif |
| 45 | +#endif |
| 46 | +//@} |
| 47 | + |
| 48 | +#ifdef __cplusplus |
| 49 | +/// @name Helper functions |
| 50 | +///@{ |
| 51 | +/// Helper functions for converting between (pointers to) C++ and C complex types |
| 52 | +NC_COMPLEX_EXPORT inline double_complex* cpp_to_c_complex(std::complex<double>* data) { |
| 53 | + return reinterpret_cast<double_complex*>(data); |
| 54 | +} |
| 55 | + |
| 56 | +NC_COMPLEX_EXPORT inline std::complex<double>* c_to_cpp_complex(double_complex* data) { |
| 57 | + return reinterpret_cast<std::complex<double>*>(data); |
| 58 | +} |
| 59 | + |
| 60 | +NC_COMPLEX_EXPORT inline float_complex* cpp_to_c_complex(std::complex<float>* data) { |
| 61 | + return reinterpret_cast<float_complex*>(data); |
| 62 | +} |
| 63 | + |
| 64 | +NC_COMPLEX_EXPORT inline std::complex<float>* c_to_cpp_complex(float_complex* data) { |
| 65 | + return reinterpret_cast<std::complex<float>*>(data); |
| 66 | +} |
| 67 | +///@} |
| 68 | +extern "C" { |
| 69 | +#endif |
| 70 | + |
| 71 | +/// @name Complex datatype defines |
| 72 | +/// Datatype for complex numbers, for use with \rstref{pfnc_def_var} |
| 73 | +/// |
| 74 | +/// @note |
| 75 | +/// These *only* work when defining a variable with \rstref{pfnc_def_var}. To |
| 76 | +/// check the type of an existing variable use \rstref{pfnc_var_is_complex}, and |
| 77 | +/// to check if it is specifically using a compound datatype or a dimension use |
| 78 | +/// \rstref{pfnc_var_is_complex_type} or \rstref{pfnc_var_has_complex_dimension} |
| 79 | +/// respectively. |
| 80 | +/// @endnote |
| 81 | +///@{ |
| 82 | + |
| 83 | +/// Uses complex compound datatype with netCDF4 format, and complex dimension otherwise |
| 84 | +#define PFNC_FLOAT_COMPLEX (NC_FIRSTUSERTYPEID - 4) |
| 85 | +/// Always use a complex dimension, regardless of file format |
| 86 | +#define PFNC_FLOAT_COMPLEX_DIM (NC_FIRSTUSERTYPEID - 3) |
| 87 | +/// Uses complex compound datatype with netCDF4 format, and complex dimension otherwise |
| 88 | +#define PFNC_DOUBLE_COMPLEX (NC_FIRSTUSERTYPEID - 2) |
| 89 | +/// Always use a complex dimension, regardless of file format |
| 90 | +#define PFNC_DOUBLE_COMPLEX_DIM (NC_FIRSTUSERTYPEID - 1) |
| 91 | +///@} |
| 92 | + |
| 93 | +/// Return true if variable is complex |
| 94 | +NC_COMPLEX_EXPORT bool pfnc_var_is_complex(int ncid, int varid); |
| 95 | +/// Return true if variable is complex and uses a compound datatype |
| 96 | +NC_COMPLEX_EXPORT bool pfnc_var_is_complex_type(int ncid, int varid); |
| 97 | +/// Return true if variable is complex and has a complex dimension |
| 98 | +/// (assumed to be the last dimension) |
| 99 | +NC_COMPLEX_EXPORT bool pfnc_var_has_complex_dimension(int ncid, int varid); |
| 100 | + |
| 101 | +/// Return true if dimension is complex |
| 102 | +NC_COMPLEX_EXPORT bool pfnc_is_complex_dim(int ncid, int dim_id); |
| 103 | + |
| 104 | +/// Get the ID for the complex datatype with `double` elements, creating it if it doesn't already exist |
| 105 | +NC_COMPLEX_EXPORT int pfnc_get_double_complex_typeid(int ncid, nc_type* nc_typeid); |
| 106 | +/// Get the ID for the complex datatype with `float` elements, creating it if it doesn't already exist |
| 107 | +NC_COMPLEX_EXPORT int pfnc_get_float_complex_typeid(int ncid, nc_type* nc_typeid); |
| 108 | + |
| 109 | +/// Get complex dimension, creating one if it doesn't already exist |
| 110 | +NC_COMPLEX_EXPORT int pfnc_get_complex_dim(int ncid, int* nc_dim); |
| 111 | + |
| 112 | +/// Get the base numerical type of a complex type |
| 113 | +/// |
| 114 | +/// Returns the type of the components for a compound type, or the |
| 115 | +/// type of an element for a dimension type. |
| 116 | +NC_COMPLEX_EXPORT int pfnc_complex_base_type( |
| 117 | + int ncid, nc_type nc_typeid, nc_type* base_type_id |
| 118 | +); |
| 119 | + |
| 120 | +/// Get the base numerical type of a complex variable |
| 121 | +NC_COMPLEX_EXPORT int pfnc_inq_var_complex_base_type( |
| 122 | + int ncid, int varid, nc_type* nc_typeid |
| 123 | +); |
| 124 | + |
| 125 | +/// Return some information about the `nc-complex` library |
| 126 | +NC_COMPLEX_EXPORT const char* pfnc_inq_libvers(void); |
| 127 | + |
| 128 | +/// @name Wrappers |
| 129 | +/// Wrappers for the equivalent `nc_*` functions that correctly handle |
| 130 | +/// the start/count/stride arrays for complex dimensions. |
| 131 | +/// |
| 132 | +/// When the variable is stored using a complex dimension, the file |
| 133 | +/// representation has one more dimension than the user-visible |
| 134 | +/// in-memory representation. For example, a 1D array: |
| 135 | +/// |
| 136 | +/// ```c |
| 137 | +/// double_complex data[5]; |
| 138 | +/// ``` |
| 139 | +/// |
| 140 | +/// would be represented in the file with two dimensions (when not |
| 141 | +/// using a compound datatype!), and so if we use the standard netCDF |
| 142 | +/// API we would need to use `{5, 2}` for the `countp` arguments, for |
| 143 | +/// example, while using nc-complex, we only need `{5}`. |
| 144 | +/// |
| 145 | +/// NOTE: The `pfnc_put/get*` functions do *not* currently handle |
| 146 | +/// conversion between `float/double` base types |
| 147 | +///@{ |
| 148 | + |
| 149 | +/// Extension to `nc_def_var` that also accepts |
| 150 | +/// \rstref{PFNC_FLOAT_COMPLEX}, \rstref{PFNC_FLOAT_COMPLEX_DIM}, |
| 151 | +/// \rstref{PFNC_DOUBLE_COMPLEX}, and \rstref{PFNC_DOUBLE_COMPLEX_DIM} |
| 152 | +NC_COMPLEX_EXPORT int pfnc_def_var( |
| 153 | + int ncid, |
| 154 | + const char* name, |
| 155 | + nc_type xtype, |
| 156 | + int ndims, |
| 157 | + const int* dimidsp, |
| 158 | + int* varidp |
| 159 | +); |
| 160 | + |
| 161 | +NC_COMPLEX_EXPORT int pfnc_put_vara_double_complex( |
| 162 | + int ncid, |
| 163 | + int varid, |
| 164 | + const size_t* startp, |
| 165 | + const size_t* countp, |
| 166 | + const double_complex* op |
| 167 | +); |
| 168 | + |
| 169 | +NC_COMPLEX_EXPORT int pfnc_get_vara_double_complex( |
| 170 | + int ncid, int varid, const size_t* startp, const size_t* countp, double_complex* ip |
| 171 | +); |
| 172 | + |
| 173 | +NC_COMPLEX_EXPORT int pfnc_put_vars_double_complex( |
| 174 | + int ncid, |
| 175 | + int varid, |
| 176 | + const size_t* startp, |
| 177 | + const size_t* countp, |
| 178 | + const ptrdiff_t* stridep, |
| 179 | + const double_complex* op |
| 180 | +); |
| 181 | + |
| 182 | +NC_COMPLEX_EXPORT int pfnc_get_vars_double_complex( |
| 183 | + int ncid, |
| 184 | + int varid, |
| 185 | + const size_t* startp, |
| 186 | + const size_t* countp, |
| 187 | + const ptrdiff_t* stridep, |
| 188 | + double_complex* ip |
| 189 | +); |
| 190 | + |
| 191 | +NC_COMPLEX_EXPORT int pfnc_put_var1_double_complex( |
| 192 | + int ncid, int varid, const size_t* indexp, const double_complex* data |
| 193 | +); |
| 194 | +NC_COMPLEX_EXPORT int pfnc_get_var1_double_complex( |
| 195 | + int ncid, int varid, const size_t* indexp, double_complex* data |
| 196 | +); |
| 197 | + |
| 198 | +NC_COMPLEX_EXPORT int pfnc_put_vara_float_complex( |
| 199 | + int ncid, |
| 200 | + int varid, |
| 201 | + const size_t* startp, |
| 202 | + const size_t* countp, |
| 203 | + const float_complex* op |
| 204 | +); |
| 205 | + |
| 206 | +NC_COMPLEX_EXPORT int pfnc_get_vara_float_complex( |
| 207 | + int ncid, int varid, const size_t* startp, const size_t* countp, float_complex* ip |
| 208 | +); |
| 209 | + |
| 210 | +NC_COMPLEX_EXPORT int pfnc_put_vars_float_complex( |
| 211 | + int ncid, |
| 212 | + int varid, |
| 213 | + const size_t* startp, |
| 214 | + const size_t* countp, |
| 215 | + const ptrdiff_t* stridep, |
| 216 | + const float_complex* op |
| 217 | +); |
| 218 | + |
| 219 | +NC_COMPLEX_EXPORT int pfnc_get_vars_float_complex( |
| 220 | + int ncid, |
| 221 | + int varid, |
| 222 | + const size_t* startp, |
| 223 | + const size_t* countp, |
| 224 | + const ptrdiff_t* stridep, |
| 225 | + float_complex* ip |
| 226 | +); |
| 227 | + |
| 228 | +NC_COMPLEX_EXPORT int pfnc_put_var1_float_complex( |
| 229 | + int ncid, int varid, const size_t* indexp, const float_complex* data |
| 230 | +); |
| 231 | +NC_COMPLEX_EXPORT int pfnc_get_var1_float_complex( |
| 232 | + int ncid, int varid, const size_t* indexp, float_complex* data |
| 233 | +); |
| 234 | + |
| 235 | +NC_COMPLEX_EXPORT int pfnc_inq_var( |
| 236 | + int ncid, |
| 237 | + int varid, |
| 238 | + char* name, |
| 239 | + nc_type* xtypep, |
| 240 | + int* ndimsp, |
| 241 | + int* dimidsp, |
| 242 | + int* nattsp |
| 243 | +); |
| 244 | + |
| 245 | +// NOLINTBEGIN(modernize-use-nullptr) |
| 246 | +NC_COMPLEX_EXPORT inline int pfnc_inq_varndims(int ncid, int varid, int* ndimsp) { |
| 247 | + return pfnc_inq_var(ncid, varid, NULL, NULL, ndimsp, NULL, NULL); |
| 248 | +} |
| 249 | +NC_COMPLEX_EXPORT inline int pfnc_inq_vardimid(int ncid, int varid, int* dimidsp) { |
| 250 | + return pfnc_inq_var(ncid, varid, NULL, NULL, NULL, dimidsp, NULL); |
| 251 | +} |
| 252 | +// NOLINTEND(modernize-use-nullptr) |
| 253 | + |
| 254 | +NC_COMPLEX_EXPORT int pfnc_def_var_chunking( |
| 255 | + int ncid, int varid, int storage, const size_t* chunksizesp |
| 256 | +); |
| 257 | +NC_COMPLEX_EXPORT int pfnc_inq_var_chunking( |
| 258 | + int ncid, int varid, int* storagep, size_t* chunksizesp |
| 259 | +); |
| 260 | + |
| 261 | +NC_COMPLEX_EXPORT int pfnc_get_vara( |
| 262 | + int ncid, int varid, const size_t* startp, const size_t* countp, void* ip |
| 263 | +); |
| 264 | +NC_COMPLEX_EXPORT int pfnc_get_vars( |
| 265 | + int ncid, |
| 266 | + int varid, |
| 267 | + const size_t* startp, |
| 268 | + const size_t* countp, |
| 269 | + const ptrdiff_t* stridep, |
| 270 | + void* ip |
| 271 | +); |
| 272 | + |
| 273 | +NC_COMPLEX_EXPORT int pfnc_put_vara( |
| 274 | + int ncid, int varid, const size_t* startp, const size_t* countp, const void* op |
| 275 | +); |
| 276 | + |
| 277 | +NC_COMPLEX_EXPORT int pfnc_put_vars( |
| 278 | + int ncid, |
| 279 | + int varid, |
| 280 | + const size_t* startp, |
| 281 | + const size_t* countp, |
| 282 | + const ptrdiff_t* stridep, |
| 283 | + const void* op |
| 284 | +); |
| 285 | +///@} |
| 286 | + |
| 287 | +#ifdef __cplusplus |
| 288 | +} |
| 289 | +#endif |
| 290 | + |
| 291 | +#endif |
0 commit comments