FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
NetCDFParser.h
1#pragma once
2
8namespace netcdf_sinmod
9{
11struct Date
12{
13 int year;
14 int month;
15 int day;
16 int hour;
17 int minute;
18 int second;
19};
20
22struct Grid
23{
24 int numX;
25 int numY;
26 int numZ;
27 int numT;
28 double sizeX;
29 double sizeY;
30};
31} // namespace netcdf_sinmod
32
33#include "netcdf-cxx4/netcdf"
34
35#include <memory>
36#include <optional>
37#include <stdexcept>
38#include <string>
39#include <vector>
40
41using std::string;
42using std::vector;
43
44namespace netcdf_sinmod
45{
47class Exception : public std::runtime_error
48{
49public:
54 Exception(const string& errorMessage)
55 : std::runtime_error("Error in NetCDF parser: " + errorMessage)
56 , m_error_message("Error in NetCDF parser: " + errorMessage)
57 {
58 }
63 void AppendErrorMessage(const string& errormessage)
64 {
65 m_error_message.append("\nError in NetCDF parser: " + errormessage);
66 }
70 const char* what() const noexcept override
71 {
72 return m_error_message.c_str();
73 }
74
75private:
76 string m_error_message;
77};
78
84class Parser
85{
86public:
89
91 ~Parser(void);
92
93 Parser(const Parser&) = delete;
94 Parser& operator=(const Parser&) = delete;
95
100 void LoadFile(string NetCDF_Filepath);
101
106
113 void GetStaticData(double* layer_depth, double* depth_map, Date* timeline);
114
119 void SetCurrentTime(int offset_from_start_time);
120
127 void GetDynamicData(double* x_velocity, double* y_velocity, double* z_velocity);
128
129private:
131 enum DIMENSION_NUMBER
132 {
133 X_NUM = 0,
134 Y_NUM = 1,
135 Z_NUM = 2,
136 T_NUM = 3,
137 T2_NUM = 4
138 };
140 enum DIMENSION_BITMASK
141 {
142 X_MASK = 1 << X_NUM,
143 Y_MASK = 1 << Y_NUM,
144 Z_MASK = 1 << Z_NUM,
145 T_MASK = 1 << T_NUM,
146 T2_MASK = 1 << T2_NUM,
147 VOID_DIMENSION_MASK = 1 << 5
148 };
149 static const DIMENSION_BITMASK SURFACE = (DIMENSION_BITMASK)(X_MASK | Y_MASK);
150 static const DIMENSION_BITMASK DEPTH = Z_MASK;
151 static const DIMENSION_BITMASK SPACE = (DIMENSION_BITMASK)(SURFACE | DEPTH);
152 static const DIMENSION_BITMASK SPACE_TIME = (DIMENSION_BITMASK)(SPACE | T_MASK);
153 static const DIMENSION_BITMASK SURFACE_TIME = (DIMENSION_BITMASK)(SURFACE | T_MASK);
154 static const DIMENSION_BITMASK EXPANDED_TIME = (DIMENSION_BITMASK)(T_MASK | T2_MASK);
155 static const DIMENSION_BITMASK ALL_SPACE_AND_TIME = (DIMENSION_BITMASK)(SPACE | EXPANDED_TIME);
156
158 struct variable_info
159 {
160 string name;
161 netCDF::NcVar ncvar;
162 DIMENSION_BITMASK required_dimensions;
163 };
165 struct system_variables
166 {
167 variable_info vx;
168 variable_info vy;
169 variable_info vz;
170 variable_info depth;
171 variable_info layer_depth;
172 variable_info time;
173 };
174
175 string m_x_dim_name;
176 string m_y_dim_name;
177 string m_z_dim_name;
178 string m_t_dim_name;
179 string m_t2_dim_name;
180 string m_file_path;
181
183 void set_to_NULL();
184
190 void read_dynamic_variable_data(double* value_array, variable_info variable);
191
198 void read_variable_info_from_file(variable_info& variable);
199
207 void check_dimension_order(const variable_info& variable);
208
214 int dimension_size(const string& dimension_name);
215
217 void read_resolution();
218
223 void report_exception(string error_message);
224
225 std::unique_ptr<netCDF::NcFile> m_ncfile;
226 Grid m_grid;
227 int m_current_time_offset;
228
229 system_variables m_variables;
230
231 double m_fill_value;
232 string m_fill_value_attribute_name;
233 string m_offset_value_attribute_name;
234 string m_scaling_value_attribute_name;
235 string m_resolution_attribute_name;
236
237 std::optional<Exception> m_exception_collector;
238};
239} // namespace netcdf_sinmod
240
NetCDF exception for error reporting; what() is the full message.
Definition NetCDFParser.h:48
Exception(const string &errorMessage)
Definition NetCDFParser.h:54
void AppendErrorMessage(const string &errormessage)
Definition NetCDFParser.h:63
const char * what() const noexcept override
Definition NetCDFParser.h:70
Definition NetCDFParser.h:85
void GetStaticData(double *layer_depth, double *depth_map, Date *timeline)
Parser(const Parser &)=delete
Owns the file handle: not copyable.
~Parser(void)
Destructor - closes file.
void LoadFile(string NetCDF_Filepath)
Parser & operator=(const Parser &)=delete
Owns the file handle: not copyable.
void GetDynamicData(double *x_velocity, double *y_velocity, double *z_velocity)
void SetCurrentTime(int offset_from_start_time)
Parser()
Construct parser with null values.
Definition NetCDFParser.h:9
@ Grid
An edge of a gridded wake's box.
Date structure for time coordinates.
Definition NetCDFParser.h:12
int hour
Hour.
Definition NetCDFParser.h:16
int minute
Minute.
Definition NetCDFParser.h:17
int day
Day.
Definition NetCDFParser.h:15
int year
Year.
Definition NetCDFParser.h:13
int second
Second.
Definition NetCDFParser.h:18
int month
Month.
Definition NetCDFParser.h:14
Grid dimensions structure.
Definition NetCDFParser.h:23
double sizeY
Size of grid cells in Y-direction [m].
Definition NetCDFParser.h:29
int numX
Number of grid cells in X-direction (xc, NED north)
Definition NetCDFParser.h:24
int numT
Number of time steps.
Definition NetCDFParser.h:27
int numY
Number of grid cells in Y-direction (yc, NED east)
Definition NetCDFParser.h:25
int numZ
Number of grid cells in Z-direction.
Definition NetCDFParser.h:26
double sizeX
Size of grid cells in X-direction [m].
Definition NetCDFParser.h:28