Peano 4
Loading...
Searching...
No Matches
NetCDFReader.cpp
Go to the documentation of this file.
1#include "NetCDFReader.hpp"
2
3
5 {
6 //assert that file exists (nc_open does not do that apparently)
7 struct stat buffer;
8 if (stat(filename.c_str(), &buffer) != 0)
9 return -1;
10 int ncid;
11 if (nc_open(filename.c_str(), NC_NOWRITE, &ncid) != NC_NOERR)
12 return -1;
13 return ncid;
14 }
15
17 {
18 if (nc_close(ncid) != NC_NOERR)
19 return -1;
20 return 0;
21 }
22
24 {
25 int dimidkey;
26 size_t dim;
27 if (nc_inq_dimid(ncid, key, &dimidkey) != NC_NOERR)
28 return 0;
29 if (nc_inq_dimlen(ncid, dimidkey, &dim) != NC_NOERR)
30 return 0;
31 return dim;
32 }
33
35 {
36 size_t len;
37 if (nc_inq_attlen(ncid, NC_GLOBAL, name, &len) != NC_NOERR || len == 0)
38 return nullptr;
39
40 int *tmp = new int[len];
41 if (nc_get_att_int(ncid, NC_GLOBAL, name, tmp) != NC_NOERR)
42 return nullptr;
43
44 return tmp;
45 }
46
48 {
49 size_t len;
50 if (nc_inq_attlen(ncid, NC_GLOBAL, name, &len) != NC_NOERR || len == 0)
51 return nullptr;
52
53 float *tmp = new float[len];
54 if (nc_get_att_float(ncid, NC_GLOBAL, name, tmp) != NC_NOERR)
55 return nullptr;
56
57 return tmp;
58 }
59
61 {
62 size_t len;
63 if (nc_inq_attlen(ncid, NC_GLOBAL, name, &len) != NC_NOERR || len == 0)
64 return nullptr;
65
66 double *tmp = new double[len];
67 if (nc_get_att_double(ncid, NC_GLOBAL, name, tmp) != NC_NOERR)
68 return nullptr;
69
70 return tmp;
71 }
72
74 {
75 size_t len;
76 if (nc_inq_attlen(ncid, NC_GLOBAL, name, &len) != NC_NOERR || len == 0)
77 return nullptr;
78
79 char *tmp = new char[len];
80 if (nc_get_att_text(ncid, NC_GLOBAL, name, tmp) != NC_NOERR)
81 return nullptr;
82
83 return tmp;
84 }
85
86 int applications::exahype2::swe::parser::NetCDFReader::readVariable1D(int ncid, const char *key, double *variable)
87 {
88 int varidkey;
89 if (nc_inq_varid(ncid, key, &varidkey) != NC_NOERR)
90 return -1;
91 if (nc_get_var_double(ncid, varidkey, variable) != NC_NOERR)
92 return -1;
93
94 return 0;
95 }
96
97 int applications::exahype2::swe::parser::NetCDFReader::readVariable2D(int ncid, const char *key, double **variable, size_t rows, size_t cols)
98 {
99 int varidkey;
100 if (nc_inq_varid(ncid, key, &varidkey) != NC_NOERR)
101 return -1;
102 double *tmp = new double[rows * cols];
103 if (nc_get_var_double(ncid, varidkey, tmp) != NC_NOERR)
104 return -1;
105 size_t j = 0;
106 for (size_t i = 0; i < rows * cols; i++)
107 {
108 if(i % rows == 0 && i > 0)
109 j++;
110 variable[i%rows][j] = tmp[i];
111 }
112 delete [] tmp;
113
114 return 0;
115 }
116
117 int applications::exahype2::swe::parser::NetCDFReader::readVariable2D(int ncid, const char *key, double *variable)
118 {
119 int varidkey;
120 if (nc_inq_varid(ncid, key, &varidkey) != NC_NOERR)
121 return -1;
122 if (nc_get_var_double(ncid, varidkey, variable) != NC_NOERR)
123 return -1;
124
125 return 0;
126 }
127
128 int applications::exahype2::swe::parser::NetCDFReader::readVariable2DHyperslab(int ncid, const char *key, double **variable, size_t rows, size_t cols, size_t start)
129 {
130 int varidkey;
131 if (nc_inq_varid(ncid, key, &varidkey) != NC_NOERR)
132 return -1;
133 size_t startp[]{start, 0, 0};
134 size_t countp[]{1, cols, rows};
135 double *tmp = new double[cols * rows];
136 if (nc_get_vara_double(ncid, varidkey, startp, countp, tmp) != NC_NOERR)
137 return -1;
138 // STOP LOOKING FOR ERRORS HERE
139 size_t j = 0;
140 for (size_t i = 0; i < rows * cols; i++)
141 {
142 if(i % rows == 0 && i > 0)
143 j++;
144 variable[i%rows][j] = tmp[i];
145 }
146 delete [] tmp;
147
148 return 0;
149 }
150
151 int applications::exahype2::swe::parser::NetCDFReader::readVariable3DHyperslab(int ncid, const char *key, double *variable, size_t rows, size_t cols, size_t start, size_t end)
152 {
153 int varidkey;
154 if(nc_inq_varid(ncid, key, &varidkey) != NC_NOERR)
155 return -1;
156 size_t startp[]{start, 0, 0};
157 size_t countp[]{end, cols, rows};
158 if(nc_get_vara_double(ncid, varidkey, startp, countp, variable) != NC_NOERR)
159 return -1;
160
161 return 0;
162 }
And from this we can write down f$ nabla phi_i nabla phi_i dx but since we are constructing matrix let s investigate the f$ j
std::string String
char * getGlobalAttText(int ncid, const char *name)
reads the global attribute name of type text and returns an array containing all the associated value...
int readVariable2D(int ncid, const char *key, double **variable, size_t rows, size_t cols)
Read contents of a 2D double variable from netCDF file into 2D array.
int closeCdf(int ncid)
Closes netCDF file.
int readVariable3DHyperslab(int ncid, const char *key, double *variable, size_t rows, size_t cols, size_t start, size_t end)
Read contents of a 3D double variable from netCDF file into 1D array starting at a specified first-di...
size_t getDimension(int ncid, const char *key)
Get the dimension of a variable.
int * getGlobalAttInt(int ncid, const char *name)
reads the global attribute name of type int and returns an array containing all the associated values...
int readVariable2DHyperslab(int ncid, const char *key, double **variable, size_t rows, size_t cols, size_t start)
Read contents of a 2D double variable from netCDF file into 2D array starting at a specified first-di...
int readVariable1D(int ncid, const char *key, double *variable)
Read contents of a 1D double variable from netCDF file.
double * getGlobalAttDouble(int ncid, const char *name)
reads the global attribute name of type double and returns an array containing all the associated val...
int openCdf(String filename)
Opens a netCDF file, returns ncid.
float * getGlobalAttFloat(int ncid, const char *name)
reads the global attribute name of type float and returns an array containing all the associated valu...