-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathatcf_line_base.cc
538 lines (394 loc) · 14.7 KB
/
atcf_line_base.cc
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
// ** Copyright UCAR (c) 1992 - 2020
// ** University Corporation for Atmospheric Research (UCAR)
// ** National Center for Atmospheric Research (NCAR)
// ** Research Applications Lab (RAL)
// ** P.O.Box 3000, Boulder, Colorado, 80307-3000, USA
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
////////////////////////////////////////////////////////////////////////
using namespace std;
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <cstdio>
#include <cmath>
#include <map>
#include "vx_math.h"
#include "atcf_line_base.h"
#include "atcf_offsets.h"
////////////////////////////////////////////////////////////////////////
// Only print the AVN to GFS conversion message once
static bool print_avn_to_gfs_message = true;
////////////////////////////////////////////////////////////////////////
//
// Code for class ATCFLineBase
//
////////////////////////////////////////////////////////////////////////
ATCFLineBase::ATCFLineBase() {
init_from_scratch();
}
////////////////////////////////////////////////////////////////////////
ATCFLineBase::~ATCFLineBase() {
clear();
}
////////////////////////////////////////////////////////////////////////
ATCFLineBase::ATCFLineBase(const ATCFLineBase &l) {
init_from_scratch();
assign(l);
}
////////////////////////////////////////////////////////////////////////
ATCFLineBase & ATCFLineBase::operator=(const ATCFLineBase &l) {
if(this == &l) return(*this);
assign(l);
return(*this);
}
////////////////////////////////////////////////////////////////////////
bool ATCFLineBase::operator==(const ATCFLineBase &l) {
return(get_line() == l.get_line());
}
////////////////////////////////////////////////////////////////////////
void ATCFLineBase::init_from_scratch() {
DataLine::init_from_scratch();
// ATCF lines are comma-delimited
set_delimiter(",");
// Initialize pointers
BasinMap = (map<ConcatString,ConcatString> *) 0;
BestTechnique = (StringArray *) 0;
OperTechnique = (StringArray *) 0;
TechSuffix = (ConcatString *) 0;
clear();
return;
}
////////////////////////////////////////////////////////////////////////
void ATCFLineBase::assign(const ATCFLineBase &l) {
clear();
DataLine::assign(l);
BasinMap = l.BasinMap;
BestTechnique = l.BestTechnique;
OperTechnique = l.OperTechnique;
TechSuffix = l.TechSuffix;
Type = l.Type;
Basin = l.Basin;
Technique = l.Technique;
IsBestTrack = l.IsBestTrack;
IsOperTrack = l.IsOperTrack;
return;
}
////////////////////////////////////////////////////////////////////////
void ATCFLineBase::dump(ostream &out, int indent_depth) const {
Indent prefix(indent_depth);
ConcatString cs;
out << prefix << "Line = " << (*this) << "\n";
out << prefix << "Type = " << atcflinetype_to_string(Type) << "\n";
out << prefix << "Basin = \"" << Basin.contents() << "\"\n";
cs = cyclone_number();
out << prefix << "CycloneNumber = \"" << cs.contents() << "\"\n";
out << prefix << "WarningTime = " << unix_to_yyyymmdd_hhmmss(warning_time()) << "\n";
out << prefix << "TechniqueNumber = " << technique_number() << "\n";
out << prefix << "ForecastPeriod = " << forecast_period() << "\n";
out << prefix << "Valid = " << unix_to_yyyymmdd_hhmmss(valid()) << "\n";
cs = technique();
out << prefix << "Technique = \"" << cs.contents() << "\"\n";
out << prefix << "IsBestTrack = \"" << bool_to_string(IsBestTrack) << "\"\n";
out << prefix << "IsOperTrack = \"" << bool_to_string(IsOperTrack) << "\"\n";
out << prefix << "Lat = " << lat() << "\n";
out << prefix << "Lon = " << lon() << "\n";
out << flush;
return;
}
////////////////////////////////////////////////////////////////////////
void ATCFLineBase::clear() {
DataLine::clear();
// Do not reset pointers:
// BasinMap, BestTechnique, OperTechnique, TechSuffix
Type = NoATCFLineType;
Basin.clear();
Technique.clear();
IsBestTrack = false;
IsOperTrack = false;
return;
}
////////////////////////////////////////////////////////////////////////
int ATCFLineBase::read_line(LineDataFile * ldf) {
int status;
clear();
status = DataLine::read_line(ldf);
// Check for bad return status or blank line
if(!status || n_items() == 0) {
clear();
return(0);
}
// Set the line type from the technique number column
Type = string_to_atcflinetype(get_item(TechniqueNumberOffset).c_str());
// Set the basin
Basin = get_item(BasinOffset);
// Update the basin name, if specified
if(BasinMap) {
if(BasinMap->count(Basin) > 0) Basin = BasinMap->at(Basin);
}
// Check for BEST and Operational tracks, if specified
if(BestTechnique) IsBestTrack = BestTechnique->has(technique());
if(OperTechnique) IsOperTrack = OperTechnique->has(technique());
// Append the technique suffix, if specified
if(TechSuffix) {
if(TechSuffix->length() > 0) {
ConcatString cs;
cs << get_item(TechniqueOffset) << TechSuffix->c_str();
Technique = cs;
}
}
return(1);
}
////////////////////////////////////////////////////////////////////////
bool ATCFLineBase::is_header() const {
if(basin().comparecase(0, 5, "BASIN") == 0) return(true);
else return(false);
}
////////////////////////////////////////////////////////////////////////
ConcatString ATCFLineBase::get_item(int i) const {
ConcatString cs;
int i_col = i;
// For ATCFLineType_GenTrack:
// Columns 1 and 2 are consistent, use offsets 0 and 1
// Columns 4-20 are the same as columns 3-19 of ATCFLineType_Track
// Shift those column indices by 1.
if(Type == ATCFLineType_GenTrack && i >= 2 && i <= 18) i_col++;
cs = DataLine::get_item(i_col);
// Strip off any whitespace
cs.ws_strip();
return(cs);
}
////////////////////////////////////////////////////////////////////////
ConcatString ATCFLineBase::get_line() const {
ConcatString cs;
if(N_items == 0) return(cs);
for(int i=0; i<N_items-1; i++) cs << DataLine::get_item(i) << DataLine::get_delimiter();
cs << DataLine::get_item(N_items-1);
return(cs);
}
////////////////////////////////////////////////////////////////////////
ATCFLineType ATCFLineBase::type() const {
return(Type);
}
////////////////////////////////////////////////////////////////////////
ConcatString ATCFLineBase::basin() const {
return(Basin);
}
////////////////////////////////////////////////////////////////////////
ConcatString ATCFLineBase::cyclone_number() const {
return(get_item(CycloneNumberOffset)); }
////////////////////////////////////////////////////////////////////////
unixtime ATCFLineBase::warning_time() const {
return(parse_time(get_item(WarningTimeOffset).c_str()));
}
////////////////////////////////////////////////////////////////////////
int ATCFLineBase::technique_number() const {
return(parse_int(get_item(TechniqueNumberOffset).c_str()));
}
////////////////////////////////////////////////////////////////////////
ConcatString ATCFLineBase::technique() const {
ConcatString cs;
// Use Technique, if already set
if(Technique.nonempty()) cs = Technique;
else cs = get_item(TechniqueOffset);
// Replace instances of AVN with GFS
if(strstr(cs.c_str(), "AVN") != NULL) {
if(print_avn_to_gfs_message) {
mlog << Debug(1)
<< "When reading ATCF track data, all instances of "
<< "\"AVN\" are automatically replaced with \"GFS\" "
<< "(ATCF ID = " << cs << ").\n";
print_avn_to_gfs_message = false;
}
cs.replace("AVN", "GFS");
}
return(cs);
}
////////////////////////////////////////////////////////////////////////
int ATCFLineBase::forecast_period() const {
return(parse_int(get_item(ForecastPeriodOffset).c_str()));
}
////////////////////////////////////////////////////////////////////////
double ATCFLineBase::lat() const {
return(parse_lat(get_item(LatTenthsOffset).c_str()));
}
////////////////////////////////////////////////////////////////////////
double ATCFLineBase::lon() const {
return(parse_lon(get_item(LonTenthsOffset).c_str()));
}
////////////////////////////////////////////////////////////////////////
unixtime ATCFLineBase::valid() const {
unixtime wt = warning_time();
double fp = forecast_period();
int tn = technique_number();
unixtime ut = 0;
// Compute the valid time if WarningTime and ForecastPeriod are valid
if(wt > 0 && !is_bad_data(fp)) {
ut = wt + sec_per_hour * fp;
}
// Add minutes for the BEST track
if(IsBestTrack && !is_bad_data(tn)) {
ut += sec_per_minute * tn;
}
return(ut);
}
////////////////////////////////////////////////////////////////////////
int ATCFLineBase::valid_hour() const {
return(unix_to_sec_of_day(valid()));
}
////////////////////////////////////////////////////////////////////////
int ATCFLineBase::lead() const {
double fp = forecast_period();
int s = bad_data_int;
// Lead time for the BEST track is 0
if(IsBestTrack) {
s = 0;
}
else if(!is_bad_data(fp)) {
s = sec_per_hour * fp;
}
return(s);
}
////////////////////////////////////////////////////////////////////////
ConcatString ATCFLineBase::storm_id() const {
ConcatString cs;
if(Type == ATCFLineType_GenTrack) {
cs = get_item(GenStormIdOffset);
}
else {
unixtime ut = valid();
cs = define_storm_id(warning_time(), ut, ut, basin(),
cyclone_number());
}
return(cs);
}
////////////////////////////////////////////////////////////////////////
//
// Code for misc functions
//
////////////////////////////////////////////////////////////////////////
unixtime parse_time(const char *s) {
int year, month, day, hour;
if(sscanf(s, "%4d%2d%2d%2d", &year, &month, &day, &hour) != 4) {
mlog << Error
<< "\nunixtime parse_time(const char *) -> "
<< "bad time format ... \"" << s << "\"\n\n";
exit(1);
}
return(mdyhms_to_unix(month, day, year, hour, 0, 0));
}
////////////////////////////////////////////////////////////////////////
double parse_lat(const char *s) {
double v;
v = parse_int(s);
// Convert tenths of a degree to degrees
switch(s[strlen(s) - 1]) {
case 'N': v *= 0.1; break;
case 'S': v *= -0.1; break; // Degrees south is negative
default:
mlog << Warning
<< "\nint parse_lat(const char *) -> "
<< "bad latitude ... \"" << s
<< "\"\n\n";
v = bad_data_double;
break;
}
// Range check
if(is_bad_data(v) || v < -90.0 || v > 90.0) v = bad_data_double;
return(v);
}
////////////////////////////////////////////////////////////////////////
double parse_lon(const char *s) {
double v;
v = parse_int(s);
// Convert tenths of a degree to degrees
switch(s[strlen(s) - 1]) {
case 'E': v *= 0.1; break;
case 'W': v *= -0.1; break; // Degrees west is negative
default:
mlog << Warning
<< "\nint parse_lon(const char *) -> "
<< "bad longitude ... \"" << s
<< "\"\n\n";
v = bad_data_double;
break;
}
// Range check
if(is_bad_data(v) || v < -360.0 || v > 360.0) v = bad_data_double;
return(v);
}
////////////////////////////////////////////////////////////////////////
int parse_int(const char *s, const int bad_data) {
int v;
if(strlen(s) > 0) v = atoi(s);
else v = bad_data_int;
// Check bad data value
if(v == bad_data) v = bad_data_int;
return(v);
}
////////////////////////////////////////////////////////////////////////
//
// Interpret values of 0 as bad data
//
////////////////////////////////////////////////////////////////////////
int parse_int_check_zero(const char *s) {
return(parse_int(s, 0));
}
////////////////////////////////////////////////////////////////////////
ATCFLineType string_to_atcflinetype(const char *s) {
ATCFLineType t;
if(!s) t = NoATCFLineType;
// YYYYMMDDHH in the 4th column for Genesis Tracks
else if(is_yyyymmddhh(s)) t = ATCFLineType_GenTrack;
else if(is_number(s)) t = ATCFLineType_Track; // ADECK
else if(strlen(s) == 0) t = ATCFLineType_Track; // BDECK
else if(strcasecmp(s, "TR") == 0) t = ATCFLineType_ProbTR;
else if(strcasecmp(s, "IN") == 0) t = ATCFLineType_ProbIN;
else if(strcasecmp(s, "RI") == 0) t = ATCFLineType_ProbRIRW;
else if(strcasecmp(s, "WD") == 0) t = ATCFLineType_ProbWD;
else if(strcasecmp(s, "PR") == 0) t = ATCFLineType_ProbPR;
else if(strcasecmp(s, "GN") == 0) t = ATCFLineType_ProbGN;
else if(strcasecmp(s, "GS") == 0) t = ATCFLineType_ProbGS;
else t = NoATCFLineType;
return(t);
}
////////////////////////////////////////////////////////////////////////
ConcatString atcflinetype_to_string(const ATCFLineType t) {
const char *s = (const char *) 0;
switch(t) {
case ATCFLineType_Track: s = "Track"; break;
case ATCFLineType_GenTrack: s = "GenTrack"; break;
case ATCFLineType_ProbTR: s = "ProbTR"; break;
case ATCFLineType_ProbIN: s = "ProbIN"; break;
case ATCFLineType_ProbRIRW: s = "ProbRIRW"; break;
case ATCFLineType_ProbWD: s = "ProbWD"; break;
case ATCFLineType_ProbPR: s = "ProbPR"; break;
case ATCFLineType_ProbGN: s = "ProbGN"; break;
case ATCFLineType_ProbGS: s = "ProbGS"; break;
case NoATCFLineType: s = na_str; break;
default: s = na_str; break;
}
return(ConcatString(s));
}
////////////////////////////////////////////////////////////////////////
ConcatString define_storm_id(unixtime init_ut, unixtime min_valid_ut,
unixtime max_valid_ut,
const ConcatString &basin,
const ConcatString &cyclone) {
ConcatString storm_id;
int year, mon, day, hr, minute, sec;
unixtime ut;
// Use timing information to determine the year.
if(init_ut > 0) ut = init_ut;
else if(min_valid_ut > 0) ut = min_valid_ut;
else ut = max_valid_ut;
// Ensure that the storm_id components are valid
if(!basin.empty() && !cyclone.empty() && ut > 0) {
unix_to_mdyhms(ut, mon, day, year, hr, minute, sec);
// Set StormId
storm_id << basin << cyclone << year;
}
return(storm_id);
}
////////////////////////////////////////////////////////////////////////