-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
212 lines (179 loc) · 7.69 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <CGAL/Delaunay_triangulation_2.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Triangulation_vertex_base_with_info_2.h>
#include <CGAL/Triangulation_face_base_2.h>
#include <random>
#include <vector>
#include <highfive/H5File.hpp>
#include <highfive/H5DataSet.hpp>
#include <chrono>
#include <cstdio>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Triangulation_vertex_base_with_info_2<double, K> Vb;
typedef CGAL::Triangulation_face_base_2<K> Fb;
typedef CGAL::Triangulation_data_structure_2<Vb, Fb> Tds;
typedef CGAL::Delaunay_triangulation_2<K, Tds> DT;
typedef K::Point_2 Point;
typedef std::chrono::high_resolution_clock high_resolution_clock;
typedef std::chrono::milliseconds milliseconds;
constexpr double INVALID_VALUE = -32767;
// following code from http://cgal-discuss.949826.n4.nabble.com/Efficient-insertion-of-points-with-info-td998300.html
//spatial sort traits to use with a pair of point pointers and integer.
struct Traits_for_spatial_sort : public DT::Geom_traits {
typedef DT::Geom_traits Gt;
typedef std::pair<const DT::Point*, double> Point_2;
struct Less_x_2 {
bool operator()(const Point_2& p, const Point_2& q) const {
return Gt::Less_x_2()(*(p.first), *(q.first));
}
};
struct Less_y_2 {
bool operator()(const Point_2& p, const Point_2& q) const {
return Gt::Less_y_2()(*(p.first), *(q.first));
}
};
Less_x_2 less_x_2_object() const { return Less_x_2(); }
Less_y_2 less_y_2_object() const { return Less_y_2(); }
private:
};
//function inserting points into a triangulation
//and setting the info field to the order in the input list.
void build_triangulation_with_value(const std::vector<Point>& pts, const std::vector<double>& values, DT& T)
{
std::vector<std::pair<const DT::Point*, double> > points;
points.reserve(pts.size());
for (int i = 0; i < pts.size(); i++)
points.emplace_back(&(pts[i]), values[i]);
std::shuffle(points.begin(), points.end(), std::mt19937(std::random_device()()));
spatial_sort(points.begin(), points.end(), Traits_for_spatial_sort());
DT::Face_handle hint;
for (auto& point : points) {
DT::Locate_type lt;
int li;
DT::Face_handle f = T.locate(*(point.first), lt, li, hint);
DT::Vertex_handle v = T.insert(*(point.first), lt, f, li);
if (v == DT::Vertex_handle())
hint = f;
else {
v->info() = point.second;
hint = v->face();
}
}
}
void griddata(const std::vector<Point>& pts, const std::vector<double>& values,
const std::vector<Point>& grid, std::vector<double>& grid_values) {
grid_values.resize(grid.size());
DT dt;
auto start = high_resolution_clock::now();
build_triangulation_with_value(pts, values, dt);
std::cout << "\tBuild Delaunay triangulation used "
<< std::chrono::duration_cast<milliseconds>(high_resolution_clock::now() - start).count() << "ms" << std::endl;
start = high_resolution_clock::now();
DT::Face_handle hint = DT::Face_handle();
for (int i = 0; i < grid.size(); i++) {
Point query = grid[i];
DT::Face_handle f = dt.locate(grid[i], hint);
hint = f;
if (dt.is_infinite(f)) {
grid_values[i] = INVALID_VALUE;
}
else {
double value[3];
for (int k = 0; k < 3; k++) value[k] = f->vertex(k)->info();
if (value[0] == INVALID_VALUE || value[1] == INVALID_VALUE || value[2] == INVALID_VALUE) {
grid_values[i] = INVALID_VALUE;
continue;
}
Point p[3];
for (int k = 0; k < 3; k++) p[k] = f->vertex(k)->point();
double area[3];
area[0] = CGAL::area(query, p[1], p[2]);
area[1] = CGAL::area(p[0], query, p[2]);
area[2] = CGAL::area(p[0], p[1], query);
double area_all = area[0] + area[1] + area[2];
double interpo = 0.;
for (int k = 0; k < 3; k++) interpo += area[k] / area_all*value[k];
grid_values[i] = interpo;
}
}
std::cout << "\tValue query used "
<< std::chrono::duration_cast<milliseconds>(high_resolution_clock::now() - start).count() << "ms" << std::endl;
}
void do_work(const char* FILE_NAME, const char* OUT_FILE_NAME) {
auto start = high_resolution_clock::now();
std::vector<double> aaigol;
std::vector<unsigned char> mask;
std::vector<float> latgol;
std::vector<float> longol;
std::cout << "\tReading file: " << FILE_NAME << std::endl;
try {
HighFive::File file(FILE_NAME, HighFive::File::ReadOnly);
HighFive::DataSet aaigol_set = file.getDataSet("AAI");
HighFive::DataSet mask_set = file.getDataSet("mask");
HighFive::DataSet latgol_set = file.getDataSet("Latitude");
HighFive::DataSet longol_set = file.getDataSet("Longitude");
aaigol_set.read(aaigol);
mask_set.read(mask);
latgol_set.read(latgol);
longol_set.read(longol);
}
catch (std::exception& e) {
std::cout << "Error : " << e.what() << std::endl;
return;
}
int N = aaigol.size();
std::cout << "\t" << N << " points" << std::endl;
std::vector<Point> pts(N);
for (int i = 0; i < N; i++) {
pts[i] = Point(latgol[i], longol[i]);
if (mask[i] != 0) aaigol[i] = INVALID_VALUE;
}
std::vector<Point> grid;
std::vector<float> new_lat, new_lon;
for (int lat = -90; lat < 90; lat++) {
for (int lon = -180; lon < 180; lon++) {
grid.emplace_back(lat, lon);
new_lat.push_back(lat);
new_lon.push_back(lon);
}
}
std::vector<double> grid_values(grid.size());
DT dt;
griddata(pts, aaigol, grid, grid_values);
std::vector<unsigned char> new_mask(grid.size(), 0);
for (int i = 0; i < grid.size(); i++) if (grid_values[i] == INVALID_VALUE) new_mask[i] = 1;
std::cout << "\tGridding : " << std::chrono::duration_cast<milliseconds>(high_resolution_clock::now() - start).count()
<< "ms" << std::endl;
std::vector<size_t> dims = { 180, 360 };
std::cout << "\tWriting file: " << OUT_FILE_NAME << std::endl;
try {
HighFive::File file_out(OUT_FILE_NAME, HighFive::File::ReadWrite | HighFive::File::Create | HighFive::File::Truncate);
HighFive::DataSet dataset1 = file_out.createDataSet<double>("AAI", HighFive::DataSpace(dims));
dataset1.write<double>(grid_values.data());
HighFive::DataSet dataset2 = file_out.createDataSet<unsigned char>("mask", HighFive::DataSpace(dims));
dataset2.write<unsigned char>(new_mask.data());
HighFive::DataSet dataset3 = file_out.createDataSet<float>("Latitude", HighFive::DataSpace(dims));
dataset3.write<float>(new_lat.data());
HighFive::DataSet dataset4 = file_out.createDataSet<float>("Longitude", HighFive::DataSpace(dims));
dataset4.write<float>(new_lon.data());
}
catch (std::exception& e) {
std::cout << "Error : " << e.what() << std::endl;
return;
}
}
int main() {
for (int year = 2017; year <= 2017; year++) {
for (int month = 1; month <= 1; month++) {
for (int day = 1; day <= 4; day++) {
std::cout << year << "-" << month << "-" << day << std::endl;
char FILE_NAME[256];
char OUT_FILE_NAME[256];
std::sprintf(FILE_NAME, "E:/griddata/Orbit_data/OMAERO_orbit_%d-%02d-%02d.h5", year, month, day);
std::sprintf(OUT_FILE_NAME, "E:/griddata/OMAERO_grid_%d-%02d-%02d.h5", year, month, day);
do_work(FILE_NAME, OUT_FILE_NAME);
}
}
}
return 0;
}