• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

icsm-au / DynAdjust / 4558854622

pending completion
4558854622

push

github

GitHub
Merge pull request #213 from icsm-au/1.2.7

949 of 949 new or added lines in 30 files covered. (100.0%)

30192 of 38394 relevant lines covered (78.64%)

3682.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.39
/dynadjust/include/io/dnaiotbu.cpp
1
//============================================================================
2
// Name         : dnaiotbu.cpp
3
// Author       : Roger Fraser
4
// Contributors :
5
// Version      : 1.00
6
// Copyright    : Copyright 2017 Geoscience Australia
7
//
8
//                Licensed under the Apache License, Version 2.0 (the "License");
9
//                you may not use this file except in compliance with the License.
10
//                You may obtain a copy of the License at
11
//               
12
//                http ://www.apache.org/licenses/LICENSE-2.0
13
//               
14
//                Unless required by applicable law or agreed to in writing, software
15
//                distributed under the License is distributed on an "AS IS" BASIS,
16
//                WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
//                See the License for the specific language governing permissions and
18
//                limitations under the License.
19
//
20
// Description  : Type B uncertainty file io and helps
21
//============================================================================
22

23
#include <include/io/dnaiotbu.hpp>
24

25
namespace dynadjust {
26
namespace iostreams {
27

28
void dna_io_tbu::assign_typeb_values_global(const vstring& typeBUncertainties, type_b_uncertainty& type_b)
1✔
29
{
30
        double e(0.), n(0.), u(0.);
1✔
31
        UINT32 i(0);
1✔
32
        stringstream ss;
1✔
33

34
        if (typeBUncertainties.size() > 2)
1✔
35
        {
36
                // 3 dimensions (east, north, up)
37
                if (!typeBUncertainties.at(i).empty())
1✔
38
                        e = DoubleFromString<double>(typeBUncertainties.at(i));
1✔
39
                if (!typeBUncertainties.at(++i).empty())
1✔
40
                        n = DoubleFromString<double>(typeBUncertainties.at(i));
1✔
41
                if (!typeBUncertainties.at(++i).empty())
1✔
42
                        u = DoubleFromString<double>(typeBUncertainties.at(i));                
1✔
43
        }
44
        else if (typeBUncertainties.size() > 1) 
×
45
        {
46
                // 2 dimensions (east, north)
47
                if (!typeBUncertainties.at(i).empty())
×
48
                        e = DoubleFromString<double>(typeBUncertainties.at(i));
×
49
                if (!typeBUncertainties.at(++i).empty())
×
50
                        n = DoubleFromString<double>(typeBUncertainties.at(i));
×
51
        }        
52
        else if (typeBUncertainties.size() > 0) 
×
53
        {
54
                // 1 dimension (up)
55
                if (!typeBUncertainties.at(i).empty())
×
56
                        u = DoubleFromString<double>(typeBUncertainties.at(i));
×
57
        }
58
        else {
59
                ss << "  No Type b uncertainties provided." << endl;
×
60
                throw boost::enable_current_exception(runtime_error(ss.str()));
×
61
        }
62

63
        // assign
64
        type_b.set_typeb_values_3d(e*e, n*n, u*u);
1✔
65
}
1✔
66
        
67

68
void dna_io_tbu::assign_typeb_values_local(const vstring& typeBUncertainties, type_b_uncertainty& type_b)
8✔
69
{
70
        double e(0.), n(0.), u(0.);
8✔
71
        UINT32 i(0);
8✔
72

73
        // 3 dimensions (east, north, up)
74
        if (!typeBUncertainties.at(i).empty())
8✔
75
                e = DoubleFromString<double>(typeBUncertainties.at(i));
7✔
76
        if (!typeBUncertainties.at(++i).empty())
8✔
77
                n = DoubleFromString<double>(typeBUncertainties.at(i));
7✔
78
        if (!typeBUncertainties.at(++i).empty())
8✔
79
                u = DoubleFromString<double>(typeBUncertainties.at(i));
7✔
80
        
81
        // assign
82
        type_b.set_typeb_values_3d(e*e, n*n, u*u);
8✔
83
}
8✔
84
        
85

86
void dna_io_tbu::validate_typeb_values(const string& argument, vstring& typeBUncertainties)
9✔
87
{
88
        stringstream ss;
9✔
89

90
        if (typeBUncertainties.size() == 0)
9✔
91
        {
92
                ss << "  No Type b uncertainties provided:" << endl <<
×
93
                        "    " << argument << endl;
×
94
                throw boost::enable_current_exception(runtime_error(ss.str()));
×
95
        }
96

97
        UINT32 i(0), j;
98

99
        for (j = i; j < typeBUncertainties.size(); j++)
36✔
100
        {
101
                if (!is_floatingpoint<string>(typeBUncertainties.at(i)))
27✔
102
                {
103
                        ss << "  Type b uncertainty '" << typeBUncertainties.at(i) <<
×
104
                                "' is not a number:" << endl <<
×
105
                                "    " << argument << endl;
×
106
                        throw boost::enable_current_exception(runtime_error(ss.str()));
×
107
                }
108
                
109
                i++;
27✔
110
        }
111
}
9✔
112

113
void dna_io_tbu::read_tbu_header(std::ifstream* ptr, string& version, INPUT_DATA_TYPE& idt)
1✔
114
{
115
        string sBuf;
1✔
116
        getline((*ptr), sBuf);
1✔
117
        sBuf = trimstr(sBuf);
1✔
118

119
        // Set the default version
120
        version = "1.00";
1✔
121

122
        // Attempt to get the file's version
123
        try {
1✔
124
                if (iequals("!#=DNA", sBuf.substr(0, 6)))
1✔
125
                        version = trimstr(sBuf.substr(6, 6));
1✔
126
        }
127
        catch (const runtime_error& e) {
×
128
                throw boost::enable_current_exception(runtime_error(e.what()));
×
129
        }
×
130

131
        string type;
1✔
132
        stringstream ssError;
1✔
133
        ssError << "  File type has not been provided in the header:" << endl <<
1✔
134
                "  " << sBuf << endl;
1✔
135

136
        if (sBuf.length() < 15)
1✔
137
                throw boost::enable_current_exception(runtime_error(ssError.str()));
×
138

139
        // Attempt to get the file's type
140
        try {
1✔
141
                type = trimstr(sBuf.substr(12, 3));
1✔
142
        }
143
        catch (const runtime_error& e) {
×
144
                ssError << "  " << e.what() << endl;
×
145
                throw boost::enable_current_exception(runtime_error(ssError.str()));
×
146
        }
×
147

148
        // Check this is a Type B file
149
        if (iequals(type, "tbu"))
1✔
150
                idt = tbu_data;
1✔
151
        else
152
        {
153
                idt = unknown;
×
154
                stringstream ssError;
×
155
                ssError << "  The supplied filetype '" << type << "' is not recognised:" << endl <<
×
156
                        "  " << sBuf << endl;
×
157
                throw boost::enable_current_exception(runtime_error(ssError.str()));
×
158
        }
×
159
}
1✔
160

161
void dna_io_tbu::load_tbu_argument(const string& argument, type_b_uncertainty& type_b_uncertainties)
1✔
162
{
163
        vstring typeBUncertainties;
1✔
164
        typeBUncertainties.resize(3);
1✔
165

166
        // Extract constraints from comma delimited string
167
        SplitDelimitedString<string>(
1✔
168
                argument,                // the comma delimited string
169
                string(","),                                                        // the delimiter
1✔
170
                &typeBUncertainties);                                        // the respective values
171

172
        // validate values. Throws on exception
173
        validate_typeb_values(argument, typeBUncertainties);
1✔
174

175
        // Assign the type b uncertainties to be applied to all stations, except
176
        // when site-specific type b uncertainties have been loaded from a file
177
        assign_typeb_values_global(typeBUncertainties, type_b_uncertainties);
1✔
178
}
1✔
179
        
180

181
void dna_io_tbu::identify_station_id(const string& stn_str, UINT32& stn_id, v_string_uint32_pair& vStnsMap)
10✔
182
{
183
        it_pair_string_vUINT32 it_stnmap_range;
10✔
184
        stringstream ss;
10✔
185

186
        // find this station in the station map
187
        it_stnmap_range = equal_range(vStnsMap.begin(), vStnsMap.end(), stn_str, StationNameIDCompareName());
10✔
188
        if (it_stnmap_range.first == it_stnmap_range.second)
10✔
189
        {
190
                ss << "  Station '" << stn_str <<
1✔
191
                        "' is not included in the network." << endl;
1✔
192
                throw boost::enable_current_exception(runtime_error(ss.str()));
2✔
193
        }
194

195
        stn_id = it_stnmap_range.first->second;
9✔
196

197
}
10✔
198
        
199

200
void dna_io_tbu::load_tbu_file(const string& tbu_filename, v_type_b_uncertainty& type_b_uncertainties, v_string_uint32_pair& vStnsMap)
1✔
201
{
202
        // Type B uncertainty file structure is as follows.
203
        // Note - uncertainties are in metres, in the local reference frame (e,n,u)
204
        // and are given at 1 sigma (68%). 
205
        //
206
        //   station id (20 chars)  east uncertainty (13) north uncertainty (13) up uncertainty (13)
207
        //      ...
208
        //   EOF
209

210
        std::ifstream tbu_file;
1✔
211
        stringstream ss;
1✔
212
        ss << "load_tbu_file(): An error was encountered when opening " << tbu_filename << "." << endl;
1✔
213
        
214
        INPUT_DATA_TYPE idt;
1✔
215
        string version;
1✔
216

217
        try {
1✔
218
                // open ascii plate boundaries file.  Throws runtime_error on failure.
219
                file_opener(tbu_file, tbu_filename, ios::in, ascii, true);
1✔
220

221
                // read header information
222
                read_tbu_header(&tbu_file, version, idt);
1✔
223

224
        }
225
        catch (const runtime_error& e) {
×
226
                ss << e.what();
×
227
                throw boost::enable_current_exception(runtime_error(ss.str()));
×
228
        }
×
229
        catch (...) {
×
230
                throw boost::enable_current_exception(runtime_error(ss.str()));
×
231
        }
×
232

233
        ss.str("");
2✔
234
        ss << "load_tbu_file(): An error was encountered when reading from " << tbu_filename << "." << endl;
1✔
235

236
        UINT32 stn_id;
1✔
237
        string record, stn_str;
1✔
238
        vstring typeb_values;
1✔
239
        const UINT16 STDDEVn(STATION + STDDEV);
1✔
240
        const UINT16 STDDEVu(STATION + STDDEV + STDDEV);
1✔
241

242
        type_b_uncertainty typeBUncertainties;
1✔
243

244
        type_b_uncertainties.clear();
1✔
245

246
        try {
1✔
247

248
                typeb_values.resize(3);
1✔
249

250
                while (!tbu_file.eof())                        // while EOF not found
15✔
251
                {
252
                        typeb_values.at(0) = "";
15✔
253
                        typeb_values.at(1) = "";
15✔
254
                        typeb_values.at(2) = "";
15✔
255

256
                        // get the plate identifier
257
                        getline(tbu_file, record);
15✔
258

259
                        // blank or whitespace?
260
                        if (trimstr(record).empty())
14✔
261
                                continue;
×
262

263
                        // Ignore lines with comments
264
                        if ((record.compare(0, 1, "*") == 0))
14✔
265
                                continue;
4✔
266

267
                        // obtain station name and unique ID
268
                        stn_str = trimstr(record.substr(0, STATION));
10✔
269
                        try {
10✔
270
                                identify_station_id(stn_str, stn_id, vStnsMap);
10✔
271
                        }
272
                        catch (const runtime_error&) {
1✔
273
                                // Can't find the station, ignore.
274
                                continue;
1✔
275
                        }
1✔
276

277
                        typeBUncertainties.set_station_id(stn_id);
9✔
278
                        
279
                        // east uncertainty (may be blank)
280
                        if (record.length() > STATION)
9✔
281
                        {
282
                                if (record.length() > STDDEVn)
8✔
283
                                        typeb_values.at(0) = trimstr(record.substr(STATION, STDDEV));
16✔
284
                                else
285
                                        typeb_values.at(0) = trimstr(record.substr(STATION));
×
286
                        }
287
                        else
288
                                // no uncertainties!
289
                                continue;
1✔
290

291
                        // north uncertainty (may be blank)
292
                        if (record.length() > STDDEVn)
8✔
293
                        {
294
                                if (record.length() > STDDEVu)
8✔
295
                                        typeb_values.at(1) = trimstr(record.substr(STDDEVn, STDDEV));
14✔
296
                                else
297
                                        typeb_values.at(1) = trimstr(record.substr(STDDEVn));
2✔
298
                        }
299
                        
300
                        // up uncertainty (may be blank)
301
                        if (record.length() > STDDEVu)
8✔
302
                                typeb_values.at(2) = trimstr(record.substr(STDDEVu));
14✔
303

304
                        try {
8✔
305
                                // validate values. Throws on exception.  Print error and continue 
306
                                // to the next set of values
307
                                validate_typeb_values(record, typeb_values);
8✔
308
                        }
309
                        catch (const runtime_error&) {
×
310
                                continue;
×
311
                        }
×
312

313
                        // Assign the site-specific type b uncertainties
314
                        assign_typeb_values_local(typeb_values, typeBUncertainties);
8✔
315

316
                        // add to the list
317
                        type_b_uncertainties.push_back(typeBUncertainties);
8✔
318

319
                }
320

321
                tbu_file.close();
×
322

323
        }
324
        catch (const ios_base::failure& f) {
1✔
325
                if (tbu_file.eof())
1✔
326
                {
327
                        tbu_file.close();
1✔
328
                        return;
1✔
329
                }
330
                ss << f.what();
×
331
                throw boost::enable_current_exception(runtime_error(ss.str()));
×
332
        }
1✔
333
        catch (const runtime_error& e) {
×
334
                ss << e.what();
×
335
                throw boost::enable_current_exception(runtime_error(ss.str()));
×
336
        }
×
337
        catch (...) {
×
338
                if (tbu_file.eof())
×
339
                {
340
                        tbu_file.close();
×
341
                        return;
×
342
                }
343
                throw boost::enable_current_exception(runtime_error(ss.str()));
×
344
        }        
×
345

346
        return;
347
}
1✔
348

349
void dna_io_tbu::reduce_uncertainties_global(type_b_uncertainty& type_b, matrix_2d& var_cart, stn_t& bstBinaryRecord)
×
350
{
351

352
        matrix_2d var_local(type_b.type_b);
×
353
        PropagateVariances_LocalCart<double>(var_local, var_cart,
×
354
                bstBinaryRecord.currentLatitude, bstBinaryRecord.currentLongitude, true);
×
355
}
×
356
        
357

358
void dna_io_tbu::reduce_uncertainties_local(v_type_b_uncertainty& type_b, vstn_t& bstBinaryRecords)
1✔
359
{
360
        it_vstn_t stn_it(bstBinaryRecords.begin());
1✔
361
        it_type_b_uncertainty _it_tbu;
1✔
362
        matrix_2d var_local;
1✔
363

364
        for (_it_tbu = type_b.begin(); _it_tbu != type_b.end(); ++_it_tbu)
9✔
365
        {
366
                var_local = _it_tbu->type_b;
8✔
367
                stn_it = bstBinaryRecords.begin() + _it_tbu->station_id;
8✔
368

369
                PropagateVariances_LocalCart<double>(var_local, _it_tbu->type_b,
8✔
370
                        stn_it->currentLatitude, stn_it->currentLongitude, true);
8✔
371
        }
372
}
1✔
373

374

375

376
} // dnaiostreams
377
} // dynadjust
378

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc