/
magdectool.cpp
287 lines (228 loc) · 9.4 KB
/
magdectool.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*****************************************************************************
* Copyright 2015-2019 Alexander Barthel alex@littlenavmap.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include "magdectool.h"
#include "io/tempfile.h"
#include "exception.h"
#include "geo/pos.h"
extern "C" {
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "wmm/GeomagnetismHeader.h"
#ifdef WRITE_GEOID_BUFFER
#include "EGM9615.h"
#endif
}
#include <QCoreApplication>
#include <QDataStream>
#include <QDir>
#include <QFile>
#include <QVector>
#include <cstring>
namespace atools {
namespace wmm {
/* Copy of MAG_Grid function with simplifications also avoiding the need to write the output to a file */
QVector<float> MAG_GridInternal(int year, int month,
MAGtype_MagneticModel *magneticModel,
MAGtype_Geoid *Geoid, MAGtype_Ellipsoid ellipsoid);
// ==============================================================================
MagDecTool::MagDecTool()
{
}
MagDecTool::~MagDecTool()
{
clear();
}
void MagDecTool::init(const QDate& dateTimeParam)
{
return init(dateTimeParam.year(), dateTimeParam.month());
}
void MagDecTool::init(int year, int month)
{
clear();
if(year <= 0 || month <= 0)
{
QDateTime dt = QDateTime::currentDateTimeUtc();
if(year <= 0)
year = dt.date().year();
if(month <= 0)
month = dt.date().month();
}
referenceDate.setDate(year, month, 1);
// Put coeffizients file into a temporary, so that the C code can read it
atools::io::TempFile temp(QString(":/atools/resources/wmm/WMM.COF"), "_wmm.cof");
MAGtype_MagneticModel *magneticModel;
// https://stackoverflow.com/questions/30470866/c-to-c-array-of-pointers-conversion-issue
if(!MAG_robustReadMagModels(temp.getFilePathData(), &magneticModel, 1))
throw atools::Exception(tr("Magnetic coeffizient file \"%1\" not found.").arg(temp.getFilePath()));
MAGtype_Ellipsoid ellipsoid;
MAGtype_Geoid geoid;
if(!MAG_SetDefaults(&ellipsoid, &geoid))
throw atools::Exception(tr("Error in MAG_SetDefaults."));
QVector<float> geoidBuffer = readGeoidBuffer();
geoid.GeoidHeightBuffer = geoidBuffer.data();
geoid.Geoid_Initialized = 1;
// Calculate declination grid
QVector<float> declinations = MAG_GridInternal(year, month, magneticModel, &geoid, ellipsoid);
if(declinations.isEmpty())
throw atools::Exception(tr("Error in MAG_GridInternal."));
MAG_FreeMagneticModelMemory(magneticModel);
// Create new plain float array and copy data before vector is destroyed
magdecGrid = new float[static_cast<unsigned int>(declinations.size())];
std::memcpy(magdecGrid, declinations.data(), static_cast<unsigned int>(declinations.size()) * sizeof(float));
}
// Only needed to write the cumbersome large 8MB EGM9615.h file into a plain file
#ifdef WRITE_GEOID_BUFFER
void MagDecTool::writeGeoidBuffer()
{
size_t bufferSize = sizeof(GeoidHeightBuffer);
size_t bufferNum = bufferSize / sizeof(GeoidHeightBuffer[0]);
QFile file("EGM9615.buf");
if(file.open(QIODevice::WriteOnly))
{
QDataStream ds(&file);
ds.setFloatingPointPrecision(QDataStream::SinglePrecision);
for(float value : GeoidHeightBuffer)
ds << value;
file.close();
}
}
#endif
QString MagDecTool::getVersion() const
{
return VERSIONDATE_LARGE;
}
float MagDecTool::getMagVar(const geo::Pos& pos)
{
if(pos.nearGrid(atools::geo::Pos::POS_EPSILON_500M))
// Get point value without interpolation if position is close to a grid point
return getMagVar(atools::roundToInt(pos.getLonX()), atools::roundToInt(pos.getLatY()));
else
{
// Get all four boundaries
float left = std::floor(pos.getLonX());
float right = std::ceil(pos.getLonX());
float top = std::ceil(pos.getLatY());
float bottom = std::floor(pos.getLatY());
// Interpolate along top boundary
float t = atools::interpolate(getMagVar(left, top), getMagVar(right, top), left, right, pos.getLonX());
// Interpolate along bottom boundary
float b = atools::interpolate(getMagVar(left, bottom), getMagVar(right, bottom), left, right, pos.getLonX());
// Interpolate between top and bottom
return atools::interpolate(t, b, top, bottom, pos.getLatY());
}
}
void MagDecTool::clear()
{
delete magdecGrid;
magdecGrid = nullptr;
}
QVector<float> MagDecTool::readGeoidBuffer()
{
QVector<float> retval;
QFile file(":/atools/resources/wmm/EGM9615.buf");
if(file.open(QIODevice::ReadOnly))
{
QDataStream ds(&file);
ds.setFloatingPointPrecision(QDataStream::SinglePrecision);
while(!ds.atEnd())
{
float value;
ds >> value;
retval.append(value);
}
file.close();
}
else
throw atools::Exception(tr("Cannot open geoid buffer \"%1\".").arg(file.fileName()));
return retval;
}
QVector<float> MAG_GridInternal(int year, int month, MAGtype_MagneticModel *magneticModel,
MAGtype_Geoid *geoid, MAGtype_Ellipsoid ellipsoid)
{
// Boundary always covers whole world
MAGtype_CoordGeodetic minimum;
minimum.phi = -90.;
minimum.lambda = -180.;
minimum.HeightAboveGeoid = minimum.HeightAboveEllipsoid = 0.f;
minimum.UseGeoid = 1;
MAGtype_CoordGeodetic maximum;
maximum.phi = 90.;
maximum.lambda = 179.;
maximum.HeightAboveGeoid = maximum.HeightAboveEllipsoid = 0.f;
maximum.UseGeoid = 1;
// Only one date - no range
MAGtype_Date startdate, enddate;
startdate.DecimalYear = enddate.DecimalYear = year + (month - 1) / 12.;
int numTerms;
double b, c;
MAGtype_MagneticModel *timedMagneticModel;
MAGtype_CoordSpherical coordSpherical;
MAGtype_MagneticResults magneticResultsSph, magneticResultsGeo, magneticResultsSphVar, magneticResultsGeoVar;
MAGtype_SphericalHarmonicVariables *sphericalVariables;
MAGtype_GeoMagneticElements geoMagneticElements, errors;
MAGtype_LegendreFunction *legendreFunction;
double cord_step_size = 1.;
if(fabs(cord_step_size) < 1.0e-10)
cord_step_size = 99999.0; // checks to make sure that the step_size is not too small
numTerms = ((magneticModel->nMax + 1) * (magneticModel->nMax + 2) / 2);
timedMagneticModel = MAG_AllocateModelMemory(numTerms);
legendreFunction = MAG_AllocateLegendreFunctionMemory(numTerms); // For storing the ALF functions
sphericalVariables = MAG_AllocateSphVarMemory(magneticModel->nMax);
QVector<float> retval;
retval.reserve(360 * 181);
b = minimum.phi;
c = minimum.lambda;
for(minimum.phi = b; minimum.phi <= maximum.phi; minimum.phi += cord_step_size) // Latitude Y loop
{
for(minimum.lambda = c; minimum.lambda <= maximum.lambda; minimum.lambda += cord_step_size) // Longitude X loop
{
if(geoid->UseGeoid == 1)
// This converts the height above mean sea level to height above the WGS-84 ellipsoid
MAG_ConvertGeoidToEllipsoidHeight(&minimum, geoid);
else
minimum.HeightAboveEllipsoid = minimum.HeightAboveGeoid;
MAG_GeodeticToSpherical(ellipsoid, minimum, &coordSpherical);
// Compute Spherical Harmonic variables
MAG_ComputeSphericalHarmonicVariables(ellipsoid, coordSpherical, magneticModel->nMax, sphericalVariables);
// Compute ALF Equations 5-6, WMM Technical report
MAG_AssociatedLegendreFunction(coordSpherical, magneticModel->nMax, legendreFunction);
// This modifies the Magnetic coefficients to the correct date.
MAG_TimelyModifyMagneticModel(startdate, magneticModel, timedMagneticModel);
// Accumulate the spherical harmonic coefficients Equations 10:12 , WMM Technical report
MAG_Summation(legendreFunction, timedMagneticModel, *sphericalVariables, coordSpherical, &magneticResultsSph);
// Sum the Secular Variation Coefficients, Equations 13:15 , WMM Technical report
MAG_SecVarSummation(legendreFunction, timedMagneticModel, *sphericalVariables, coordSpherical,
&magneticResultsSphVar);
// Map the computed Magnetic fields to Geodetic coordinates Equation 16 , WMM Technical report
MAG_RotateMagneticVector(coordSpherical, minimum, magneticResultsSph, &magneticResultsGeo);
// Map the secular variation field components to Geodetic coordinates, Equation 17 , WMM Technical report
MAG_RotateMagneticVector(coordSpherical, minimum, magneticResultsSphVar, &magneticResultsGeoVar);
// Calculate the Geomagnetic elements, Equation 18 , WMM Technical report
MAG_CalculateGeoMagneticElements(&magneticResultsGeo, &geoMagneticElements);
MAG_WMMErrorCalc(geoMagneticElements.H, &errors);
retval.append(static_cast<float>(geoMagneticElements.Decl));
} // Longitude Loop
} // Latitude Loop
MAG_FreeMagneticModelMemory(timedMagneticModel);
MAG_FreeLegendreMemory(legendreFunction);
MAG_FreeSphVarMemory(sphericalVariables);
return retval;
}
} // namespace wmm
} // namespace atools