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 <string>
38#include <vector>
39
40using std::string;
41using std::vector;
42
43namespace netcdf_sinmod
44{
47{
48public:
53 Exception(string errorMessage)
54 {
55 m_error_message = "Error in NetCDF parser: " + errorMessage;
56 }
61 void AppendErrorMessage(string errormessage)
62 {
63 m_error_message.append("\nError in NetCDF parser: " + errormessage);
64 }
68 string GetErrorMessage() const
69 {
70 return m_error_message;
71 }
72
73private:
74 string m_error_message;
75};
76
82class Parser
83{
84public:
87
89 ~Parser(void);
90
91 Parser(const Parser&) = delete;
92 Parser& operator=(const Parser&) = delete;
93
98 void LoadFile(string NetCDF_Filepath);
99
104
111 void GetStaticData(double* layer_depth, double* depth_map, Date* timeline);
112
117 void SetCurrentTime(int offset_from_start_time);
118
125 void GetDynamicData(double* x_velocity, double* y_velocity, double* z_velocity);
126
127private:
129 enum DIMENSION_NUMBER
130 {
131 X_NUM = 0,
132 Y_NUM = 1,
133 Z_NUM = 2,
134 T_NUM = 3,
135 T2_NUM = 4
136 };
138 enum DIMENSION_BITMASK
139 {
140 X_MASK = 1 << X_NUM,
141 Y_MASK = 1 << Y_NUM,
142 Z_MASK = 1 << Z_NUM,
143 T_MASK = 1 << T_NUM,
144 T2_MASK = 1 << T2_NUM,
145 VOID_DIMENSION_MASK = 1 << 5
146 };
147 static const DIMENSION_BITMASK SURFACE = (DIMENSION_BITMASK)(X_MASK | Y_MASK);
148 static const DIMENSION_BITMASK DEPTH = Z_MASK;
149 static const DIMENSION_BITMASK SPACE = (DIMENSION_BITMASK)(SURFACE | DEPTH);
150 static const DIMENSION_BITMASK SPACE_TIME = (DIMENSION_BITMASK)(SPACE | T_MASK);
151 static const DIMENSION_BITMASK SURFACE_TIME = (DIMENSION_BITMASK)(SURFACE | T_MASK);
152 static const DIMENSION_BITMASK EXPANDED_TIME = (DIMENSION_BITMASK)(T_MASK | T2_MASK);
153 static const DIMENSION_BITMASK ALL_SPACE_AND_TIME = (DIMENSION_BITMASK)(SPACE | EXPANDED_TIME);
154
156 struct variable_info
157 {
158 string name;
159 netCDF::NcVar ncvar;
160 DIMENSION_BITMASK required_dimensions;
161 };
163 struct system_variables
164 {
165 variable_info vx;
166 variable_info vy;
167 variable_info vz;
168 variable_info depth;
169 variable_info layer_depth;
170 variable_info time;
171 };
172
173 string m_x_dim_name;
174 string m_y_dim_name;
175 string m_z_dim_name;
176 string m_t_dim_name;
177 string m_t2_dim_name;
178 string m_file_path;
179
181 void set_to_NULL();
182
188 void read_dynamic_variable_data(double* value_array, variable_info variable);
189
196 void read_variable_info_from_file(variable_info& variable);
197
205 void check_dimension_order(const variable_info& variable);
206
212 int dimension_size(const string& dimension_name);
213
215 void read_resolution();
216
221 void report_exception(string error_message);
222
223 std::unique_ptr<netCDF::NcFile> m_ncfile;
224 Grid m_grid;
225 int m_current_time_offset;
226
227 system_variables m_variables;
228
229 double m_fill_value;
230 string m_fill_value_attribute_name;
231 string m_offset_value_attribute_name;
232 string m_scaling_value_attribute_name;
233 string m_resolution_attribute_name;
234
235 std::optional<Exception> m_exception_collector;
236};
237} // namespace netcdf_sinmod
238
NetCDF exception for error reporting.
Definition NetCDFParser.h:47
void AppendErrorMessage(string errormessage)
Definition NetCDFParser.h:61
Exception(string errorMessage)
Definition NetCDFParser.h:53
string GetErrorMessage() const
Definition NetCDFParser.h:68
Definition NetCDFParser.h:83
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