/
aggr_stat_line.cc
3693 lines (3112 loc) · 109 KB
/
aggr_stat_line.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
// ** 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
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
////////////////////////////////////////////////////////////////////////
//
// Filename: aggr_stat_line.cc
//
// Description:
//
// Mod# Date Name Description
// ---- ---- ---- -----------
// 000 12/17/08 Halley Gotway New
// 001 05/24/10 Halley Gotway Add aggr_rhist_lines and
// aggr_orank_lines.
// 002 06/09/10 Halley Gotway Add aggr_mctc_lines.
// 003 06/21/10 Halley Gotway Add support for vif_flag.
// 004 07/28/10 Halley Gotway Write out lines prior to error
// checking and add input line to error messages.
// 005 03/07/13 Halley Gotway Add aggregate SSVAR lines.
// 006 06/03/14 Halley Gotway Add aggregate PHIST lines.
// 007 07/28/14 Halley Gotway Add aggregate_stat for MPR to WDIR.
// 008 02/05/15 Halley Gotway Add StatHdrInfo to keep track of
// unique header entries for each aggregation.
// 009 03/30/15 Halley Gotway Add ramp job type.
// 010 06/09/17 Halley Gotway Add aggregate RELP lines.
// 011 06/09/17 Halley Gotway Add aggregate GRAD lines.
// 012 03/01/18 Halley Gotway Update summary job type.
// 013 04/25/18 Halley Gotway Add ECNT line type.
// 014 04/01/19 Fillmore Add FCST and OBS units.
// 015 01/24/20 Halley Gotway Add aggregate RPS lines.
//
////////////////////////////////////////////////////////////////////////
using namespace std;
#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <cmath>
#include "vx_log.h"
#include "vx_data2d_grib.h"
#include "vx_data2d_nc_pinterp.h"
#include "aggr_stat_line.h"
#include "parse_stat_line.h"
////////////////////////////////////////////////////////////////////////
static bool is_precip_var_name(const ConcatString &s);
static const std::string case_str = "CASE";
////////////////////////////////////////////////////////////////////////
//
// Code for StatHdrInfo structure.
//
////////////////////////////////////////////////////////////////////////
StatHdrInfo::StatHdrInfo() {
clear();
}
////////////////////////////////////////////////////////////////////////
void StatHdrInfo::clear() {
model.clear();
desc.clear();
fcst_lead.clear();
fcst_valid_beg = fcst_valid_end = (unixtime) 0;
obs_lead.clear();
obs_valid_beg = obs_valid_end = (unixtime) 0;
fcst_var.clear();
fcst_units.clear();
fcst_lev.clear();
obs_var.clear();
obs_units.clear();
obs_lev.clear();
obtype.clear();
vx_mask.clear();
interp_mthd.clear();
interp_pnts.clear();
fcst_thresh.clear();
obs_thresh.clear();
cov_thresh.clear();
alpha.clear();
return;
}
////////////////////////////////////////////////////////////////////////
//
// Keep track the unique STAT header entries for each line.
//
////////////////////////////////////////////////////////////////////////
void StatHdrInfo::add(const STATLine &line) {
ConcatString cs;
if(!model.has(line.model()))
model.add(line.model());
if(!desc.has(line.desc()))
desc.add(line.desc());
if(!fcst_lead.has(line.fcst_lead()))
fcst_lead.add(line.fcst_lead());
if(fcst_valid_beg == (unixtime) 0 || line.fcst_valid_beg() < fcst_valid_beg)
fcst_valid_beg = line.fcst_valid_beg();
if(fcst_valid_end == (unixtime) 0 || line.fcst_valid_end() > fcst_valid_end)
fcst_valid_end = line.fcst_valid_end();
if(!obs_lead.has(line.obs_lead()))
obs_lead.add(line.obs_lead());
if(obs_valid_beg == (unixtime) 0 || line.obs_valid_beg() < obs_valid_beg)
obs_valid_beg = line.obs_valid_beg();
if(obs_valid_end == (unixtime) 0 || line.obs_valid_end() > obs_valid_end)
obs_valid_end = line.obs_valid_end();
if(!fcst_var.has(line.fcst_var()))
fcst_var.add(line.fcst_var());
if(!fcst_units.has(line.fcst_units()))
fcst_units.add(line.fcst_units());
if(!fcst_lev.has(line.fcst_lev()))
fcst_lev.add(line.fcst_lev());
if(!obs_var.has(line.obs_var()))
obs_var.add(line.obs_var());
if(!obs_units.has(line.obs_units()))
obs_units.add(line.obs_units());
if(!obs_lev.has(line.obs_lev()))
obs_lev.add(line.obs_lev());
if(!obtype.has(line.obtype()))
obtype.add(line.obtype());
if(!vx_mask.has(line.vx_mask()))
vx_mask.add(line.vx_mask());
if(!interp_mthd.has(line.interp_mthd()))
interp_mthd.add(line.interp_mthd());
if(!interp_pnts.has(line.interp_pnts()))
interp_pnts.add(line.interp_pnts());
cs = line.get_item("FCST_THRESH", false);
cs.strip_paren();
if(!fcst_thresh.has(cs)) fcst_thresh.add(cs);
cs = line.get_item("OBS_THRESH", false);
cs.strip_paren();
if(!obs_thresh.has(cs)) obs_thresh.add(cs);
cs = line.get_item("COV_THRESH", false);
cs.strip_paren();
if(!cov_thresh.has(cs)) cov_thresh.add(cs);
if(!alpha.has(line.alpha()))
alpha.add(line.alpha());
return;
}
////////////////////////////////////////////////////////////////////////
//
// Check for and print debug and warning messages about multiple header
// column values found.
//
////////////////////////////////////////////////////////////////////////
void StatHdrInfo::check_shc(const ConcatString &cur_case) {
// MODEL
if(model.n() > 1) {
mlog << Debug(2)
<< "For case \"" << cur_case << "\", found "
<< model.n()
<< " unique MODEL values: "
<< write_css(model) << "\n";
}
// DESC
if(desc.n() > 1) {
mlog << Debug(2)
<< "For case \"" << cur_case << "\", found "
<< desc.n()
<< " unique DESC values: "
<< write_css(desc) << "\n";
}
// FCST_LEAD
if(fcst_lead.n() > 1) {
mlog << Debug(2)
<< "For case \"" << cur_case << "\", found "
<< fcst_lead.n()
<< " unique FCST_LEAD values: "
<< write_css_hhmmss(fcst_lead) << "\n";
}
// OBS_LEAD
if(obs_lead.n() > 1) {
mlog << Debug(2)
<< "For case \"" << cur_case << "\", found "
<< obs_lead.n()
<< " unique OBS_LEAD values: "
<< write_css_hhmmss(obs_lead) << "\n";
}
// FCST_VAR
if(fcst_var.n() > 1) {
mlog << Debug(2)
<< "For case \"" << cur_case << "\", found "
<< fcst_var.n()
<< " unique FCST_VAR values: "
<< write_css(fcst_var) << "\n";
}
// FCST_UNITS
if(fcst_units.n() > 1) {
mlog << Debug(2)
<< "For case \"" << cur_case << "\", found "
<< fcst_units.n()
<< " unique FCST_UNITS values: "
<< write_css(fcst_units) << "\n";
}
// FCST_LEV
if(fcst_lev.n() > 1) {
mlog << Debug(2)
<< "For case \"" << cur_case << "\", found "
<< fcst_lev.n()
<< " unique FCST_LEV values: "
<< write_css(fcst_lev) << "\n";
}
// OBS_VAR
if(obs_var.n() > 1) {
mlog << Debug(2)
<< "For case \"" << cur_case << "\", found "
<< obs_var.n()
<< " unique OBS_VAR values: "
<< write_css(obs_var) << "\n";
}
// OBS_UNITS
if(obs_units.n() > 1) {
mlog << Debug(2)
<< "For case \"" << cur_case << "\", found "
<< obs_units.n()
<< " unique OBS_UNITS values: "
<< write_css(obs_units) << "\n";
}
// OBS_LEV
if(obs_lev.n() > 1) {
mlog << Debug(2)
<< "For case \"" << cur_case << "\", found "
<< obs_lev.n()
<< " unique OBS_LEV values: "
<< write_css(obs_lev) << "\n";
}
// OBTYPE
if(obtype.n() > 1) {
mlog << Debug(2)
<< "For case \"" << cur_case << "\", found "
<< obtype.n()
<< " unique OBTYPE values: "
<< write_css(obtype) << "\n";
}
// VX_MASK
if(vx_mask.n() > 1) {
mlog << Debug(2)
<< "For case \"" << cur_case << "\", found "
<< vx_mask.n()
<< " unique VX_MASK values: "
<< write_css(vx_mask) << "\n";
}
// INTERP_MTHD
if(interp_mthd.n() > 1) {
mlog << Warning
<< "For case \"" << cur_case << "\", found "
<< interp_mthd.n()
<< " unique INTERP_MTHD values: "
<< write_css(interp_mthd) << ".\n";
}
// INTERP_PNTS
if(interp_pnts.n() > 1) {
mlog << Warning
<< "For case \"" << cur_case << "\", found "
<< interp_pnts.n()
<< " unique INTERP_PNTS values: "
<< write_css(interp_pnts) << ".\n";
}
// FCST_THRESH
if(fcst_thresh.n() > 1) {
mlog << Warning
<< "For case \"" << cur_case << "\", found "
<< fcst_thresh.n()
<< " unique FCST_THRESH values: "
<< write_css(fcst_thresh) << "\n";
}
// OBS_THRESH
if(obs_thresh.n() > 1) {
mlog << Warning
<< "For case \"" << cur_case << "\", found "
<< obs_thresh.n()
<< " unique OBS_THRESH values: "
<< write_css(obs_thresh) << "\n";
}
// COV_THRESH
if(cov_thresh.n() > 1) {
mlog << Warning
<< "For case \"" << cur_case << "\", found "
<< cov_thresh.n()
<< " unique COV_THRESH values: "
<< write_css(cov_thresh) << ".\n";
}
// ALPHA
if(alpha.n() > 1) {
mlog << Warning
<< "For case \"" << cur_case << "\", found "
<< alpha.n()
<< " unique ALPHA values: "
<< write_css(alpha) << ".\n";
}
return;
}
////////////////////////////////////////////////////////////////////////
//
// Use the StatHdrInfo struct to populate a StatHdrColumns object
//
////////////////////////////////////////////////////////////////////////
StatHdrColumns StatHdrInfo::get_shc(const ConcatString &cur_case,
const StringArray &case_cols,
const StringArray &hdr_cols,
const StringArray &hdr_vals,
const STATLineType lt) {
ConcatString css;
StringArray case_vals;
ThreshArray ta;
double out_alpha;
int index, wdth;
StatHdrColumns shc;
// Split up the current case into values
case_vals = cur_case.split(":");
// MODEL
shc.set_model(
get_shc_str(cur_case, case_cols, case_vals, hdr_cols, hdr_vals,
"MODEL", model, false).c_str());
// DESC
shc.set_desc(
get_shc_str(cur_case, case_cols, case_vals, hdr_cols, hdr_vals,
"DESC", desc, false).c_str());
// FCST_LEAD
css = write_css_hhmmss(fcst_lead);
if(fcst_lead.n() > 1) {
mlog << Debug(2)
<< "For case \"" << cur_case << "\", found "
<< fcst_lead.n()
<< " unique FCST_LEAD values: " << css << "\n";
}
if(hdr_cols.has("FCST_LEAD", index)) {
shc.set_fcst_lead_sec(timestring_to_sec(hdr_vals[index].c_str()));
}
else {
shc.set_fcst_lead_sec(fcst_lead.max());
}
// FCST_VALID_BEG, FCST_VALID_END
if(hdr_cols.has("FCST_VALID_BEG", index)) {
shc.set_fcst_valid_beg(timestring_to_unix(hdr_vals[index].c_str()));
}
else {
shc.set_fcst_valid_beg(fcst_valid_beg);
}
if(hdr_cols.has("FCST_VALID_END", index)) {
shc.set_fcst_valid_end(timestring_to_unix(hdr_vals[index].c_str()));
}
else {
shc.set_fcst_valid_end(fcst_valid_end);
}
// OBS_LEAD
css = write_css_hhmmss(obs_lead);
if(obs_lead.n() > 1) {
mlog << Debug(2)
<< "For case \"" << cur_case << "\", found "
<< obs_lead.n()
<< " unique OBS_LEAD values: " << css << "\n";
}
if(hdr_cols.has("OBS_LEAD", index)) {
shc.set_obs_lead_sec(timestring_to_sec(hdr_vals[index].c_str()));
}
else {
shc.set_obs_lead_sec(obs_lead.max());
}
// OBS_VALID_BEG, OBS_VALID_END
if(hdr_cols.has("OBS_VALID_BEG", index)) {
shc.set_obs_valid_beg(timestring_to_unix(hdr_vals[index].c_str()));
}
else {
shc.set_obs_valid_beg(obs_valid_beg);
}
if(hdr_cols.has("OBS_VALID_END", index)) {
shc.set_obs_valid_end(timestring_to_unix(hdr_vals[index].c_str()));
}
else {
shc.set_obs_valid_end(obs_valid_end);
}
// FCST_VAR
shc.set_fcst_var(
get_shc_str(cur_case, case_cols, case_vals, hdr_cols, hdr_vals,
"FCST_VAR", fcst_var, false));
// FCST_UNITS
shc.set_fcst_units(
get_shc_str(cur_case, case_cols, case_vals, hdr_cols, hdr_vals,
"FCST_UNITS", fcst_units, false));
// FCST_LEV
shc.set_fcst_lev(
get_shc_str(cur_case, case_cols, case_vals, hdr_cols, hdr_vals,
"FCST_LEV", fcst_lev, false).c_str());
// OBS_VAR
shc.set_obs_var(
get_shc_str(cur_case, case_cols, case_vals, hdr_cols, hdr_vals,
"OBS_VAR", obs_var, false));
// OBS_UNITS
shc.set_obs_units(
get_shc_str(cur_case, case_cols, case_vals, hdr_cols, hdr_vals,
"OBS_UNITS", obs_units, false));
// OBS_LEV
shc.set_obs_lev(
get_shc_str(cur_case, case_cols, case_vals, hdr_cols, hdr_vals,
"OBS_LEV", obs_lev, false).c_str());
// OBTYPE
shc.set_obtype(
get_shc_str(cur_case, case_cols, case_vals, hdr_cols, hdr_vals,
"OBTYPE", obtype, false).c_str());
// VX_MASK
shc.set_mask(
get_shc_str(cur_case, case_cols, case_vals, hdr_cols, hdr_vals,
"VX_MASK", vx_mask, false).c_str());
// INTERP_MTHD
shc.set_interp_mthd(
get_shc_str(cur_case, case_cols, case_vals, hdr_cols, hdr_vals,
"INTERP_MTHD", interp_mthd, true));
// INTERP_PNTS
css = write_css(interp_pnts);
if(interp_pnts.n() > 1) {
mlog << Warning
<< "For case \"" << cur_case << "\", found "
<< interp_pnts.n()
<< " unique INTERP_PNTS values: " << css << ".\n";
wdth = bad_data_int;
}
else {
wdth = nint(sqrt(interp_pnts[0]));
}
if(hdr_cols.has("INTERP_PNTS", index)) {
wdth = nint(sqrt(atof(hdr_vals[index].c_str())));
}
shc.set_interp_wdth(wdth);
// FCST_THRESH
ta.clear();
ta.add_css(get_shc_str(cur_case, case_cols, case_vals, hdr_cols, hdr_vals,
"FCST_THRESH", fcst_thresh, true).c_str());
shc.set_fcst_thresh(ta);
// OBS_THRESH
ta.clear();
ta.add_css(get_shc_str(cur_case, case_cols, case_vals, hdr_cols, hdr_vals,
"OBS_THRESH", obs_thresh, true).c_str());
shc.set_obs_thresh(ta);
// COV_THRESH
ta.clear();
ta.add_css(get_shc_str(cur_case, case_cols, case_vals, hdr_cols, hdr_vals,
"COV_THRESH", cov_thresh, true).c_str());
shc.set_cov_thresh(ta);
// ALPHA
css = write_css(alpha);
if(alpha.n() > 1) {
mlog << Warning
<< "For case \"" << cur_case << "\", found "
<< alpha.n()
<< " unique ALPHA values: " << css << ".\n";
out_alpha = bad_data_double;
}
else {
out_alpha = alpha[0];
}
if(hdr_cols.has("ALPHA", index)) {
out_alpha = atof(hdr_vals[index].c_str());
}
shc.set_alpha(out_alpha);
// LINE_TYPE
shc.set_line_type(statlinetype_to_string(lt));
return(shc);
}
////////////////////////////////////////////////////////////////////////
ConcatString StatHdrInfo::get_shc_str(const ConcatString &cur_case,
const StringArray &case_cols,
const StringArray &case_vals,
const StringArray &hdr_cols,
const StringArray &hdr_vals,
const char *col_name,
const StringArray &col_vals,
bool warning) {
ConcatString css, shc_str;
int hdr_index, case_index;
// Build comma-separated list of column values
css = write_css(col_vals);
// Check for multiple entries
if(col_vals.n() > 1) {
ConcatString msg;
msg << "For case \"" << cur_case << "\", found "
<< col_vals.n() << " unique " << col_name
<< " values: " << css << "\n";
if(warning) mlog << Warning << msg;
else mlog << Debug(2) << msg;
}
// Check the header options.
if(hdr_cols.has(col_name, hdr_index)) {
// Check for the full CASE string.
if(case_str.compare(hdr_vals[hdr_index]) == 0) {
shc_str = cur_case;
}
// Check for one of the case columns.
else if(case_cols.has(hdr_vals[hdr_index], case_index)) {
shc_str = case_vals[case_index];
}
// Otherwise, use the constant header string.
else {
shc_str = hdr_vals[hdr_index];
}
}
// Otherwise, use the comma-separated list of values.
else {
shc_str = css;
}
return(shc_str);
}
////////////////////////////////////////////////////////////////////////
//
// Code for AggrTimeSeriesInfo structure.
//
////////////////////////////////////////////////////////////////////////
AggrTimeSeriesInfo::AggrTimeSeriesInfo() {
clear();
}
////////////////////////////////////////////////////////////////////////
void AggrTimeSeriesInfo::clear() {
hdr.clear();
fcst_var.clear();
obs_var.clear();
f_na.clear();
o_na.clear();
init_ts.clear();
valid_ts.clear();
}
////////////////////////////////////////////////////////////////////////
void AggrTimeSeriesInfo::sort() {
int j;
AggrTimeSeriesInfo ri_sort;
// Copy
ri_sort.hdr = hdr;
ri_sort.fcst_var = fcst_var;
ri_sort.obs_var = obs_var;
// Sort by valid time
if(valid_ts.n() == f_na.n()) {
ri_sort.valid_ts = valid_ts;
ri_sort.valid_ts.sort_array();
for(int i=0; i<valid_ts.n(); i++) {
j = valid_ts.index(ri_sort.valid_ts[i]);
ri_sort.f_na.add(f_na[j]);
ri_sort.o_na.add(o_na[j]);
ri_sort.init_ts.add(init_ts[j]);
}
}
// Sort by initialization time
else if(init_ts.n() == f_na.n()) {
ri_sort.init_ts = init_ts;
ri_sort.init_ts.sort_array();
ri_sort.valid_ts = valid_ts; // Single value
for(int i=0; i<init_ts.n(); i++) {
j = init_ts.index(ri_sort.init_ts[i]);
ri_sort.f_na.add(f_na[j]);
ri_sort.o_na.add(o_na[j]);
}
}
else {
mlog << Warning << "\nAggrTimeSeriesInfo::sort() -> "
<< "can't sort when the number of times and the data values "
<< "differ.\n\n";
return;
}
// Copy sorted version
*this = ri_sort;
return;
}
////////////////////////////////////////////////////////////////////////
void aggr_summary_lines(LineDataFile &f, STATAnalysisJob &job,
map<ConcatString, AggrSummaryInfo> &m,
int &n_in, int &n_out) {
STATLine line;
AggrSummaryInfo aggr;
ConcatString key, cs;
StringArray sa, req_stat, req_lty, req_col;
STATLineType lty;
NumArray empty_na;
int i, n_add;
double v, w;
//
// Objects for derived statistics
//
CTSInfo cts_info;
SL1L2Info sl1l2_info;
CNTInfo cnt_info;
VL1L2Info vl1l2_info;
//
// Build list of requested line types and column names
//
for(i=0; i<job.column.n(); i++) {
// Split -column entry on colon
sa = ConcatString(job.column[i]).split(":");
// Option 1: Use -line_type once with no colons in -column option
if(sa.n() == 1 && job.line_type.n() == 1) {
cs << cs_erase << job.line_type[0] << ":" << sa[0];
req_stat.add(cs);
req_lty.add(job.line_type[0]);
req_col.add(sa[0]);
}
// Option 2: Use colons in -column option
else if(sa.n() == 2) {
req_stat.add(job.column[i]);
req_lty.add(sa[0]);
req_col.add(sa[1]);
}
else {
mlog << Error << "\naggr_summary_lines() -> "
<< "trouble parsing \"-column " << job.column[i]
<< "\" option. Either use \"-line_type\" exactly once "
<< "or use format \"-column LINE_TYPE:COLUMN\".\n\n";
throw(1);
}
}
//
// When -column_union is true, store a list of unique line types and columns
//
if(job.column_union) {
cs << cs_erase << write_css(req_lty.uniq()) << ":" << write_css(req_col.uniq());
req_stat.set(0, cs);
}
//
// Check for derived types:
// FHO, CTC -> CTS
// SL1L2, SAL1L2 -> CNT
// VL1L2 -> VCNT
//
bool do_cts = job.do_derive && req_lty.has(stat_cts_str);
if(do_cts) mlog << Debug(3)
<< "Deriving CTS statistics from input FHO/CTC lines.\n";
bool do_cnt = job.do_derive && req_lty.has(stat_cnt_str);
if(do_cnt) mlog << Debug(3)
<< "Deriving CNT statistics from input SL1L2/SAL1L2 lines.\n";
bool do_vcnt = job.do_derive && req_lty.has(stat_vcnt_str);
if(do_vcnt) mlog << Debug(3)
<< "Deriving VCNT statistics from input VL1L2 lines.\n";
//
// Process the STAT lines
//
while(f >> line) {
if(line.is_header()) continue;
n_in++;
if(job.is_keeper(line)) {
//
// Build the map key for the current line
//
key = job.get_case_info(line);
//
// Add a new map entry, if necessary
//
if(m.count(key) == 0) {
aggr.val.clear();
aggr.wgt.clear();
for(i=0; i<req_stat.n(); i++) {
aggr.val[req_stat[i]] = empty_na;
aggr.wgt[req_stat[i]] = empty_na;
}
m[key] = aggr;
}
//
// Derive categorical statistics
//
if(do_cts && line.type() == stat_fho) {
parse_fho_ctable(line, cts_info.cts);
}
else if(do_cts && line.type() == stat_ctc) {
parse_ctc_ctable(line, cts_info.cts);
}
//
// Derive continuous statistics
//
else if(do_cnt && (line.type() == stat_sl1l2 ||
line.type() == stat_sal1l2)) {
parse_sl1l2_line(line, sl1l2_info);
compute_cntinfo(sl1l2_info, 0, cnt_info);
}
//
// Derive vector continuous statistics
//
else if(do_vcnt && line.type() == stat_vl1l2) {
parse_vl1l2_line(line, vl1l2_info);
}
//
// Update the map entries for each requested statistic
//
for(i=0,n_add=0; i<req_col.n(); i++) {
//
// Store the current requested line type
//
lty = string_to_statlinetype(req_lty[i].c_str());
//
// Retrieve the column value, checking dervied stats
//
if((line.type() == stat_fho ||
line.type() == stat_ctc) && lty == stat_cts) {
v = cts_info.get_stat(req_col[i].c_str());
w = cts_info.cts.n();
}
else if(line.type() == stat_sl1l2 && lty == stat_cnt) {
v = cnt_info.get_stat(req_col[i].c_str());
w = cnt_info.n;
}
else if(line.type() == stat_sal1l2 && lty == stat_cnt) {
v = cnt_info.get_stat(req_col[i].c_str());
w = cnt_info.n;
}
else if(line.type() == stat_vl1l2 && lty == stat_vcnt) {
v = vl1l2_info.get_stat(req_col[i].c_str());
w = vl1l2_info.vcount;
}
else if(line.type() != lty) {
continue;
}
else {
v = job.get_column_double(line, req_col[i]);
w = atoi(line.get_item("TOTAL"));
}
//
// Check for bad data
//
if(is_bad_data(v)) continue;
//
// Add the current column value and weight
// If computing a union, concatentate them all
//
if(job.column_union) {
m[key].val[req_stat[0]].add(v);
m[key].wgt[req_stat[0]].add(w);
}
else {
m[key].val[req_stat[i]].add(v);
m[key].wgt[req_stat[i]].add(w);
}
// Keep track of the unique header column entries
m[key].hdr.add(line);
n_add++;
}
//
// Dump only the lines that were actually used
//
if(n_add > 0) {
job.dump_stat_line(line);
n_out++;
}
}
} // end while
return;
}
////////////////////////////////////////////////////////////////////////
//
// The aggr_ctc_lines routine should only be called when the
// -line_type option has been used exactly once.
//
////////////////////////////////////////////////////////////////////////
void aggr_ctc_lines(LineDataFile &f, STATAnalysisJob &job,
map<ConcatString, AggrCTCInfo> &m,
int &n_in, int &n_out) {
STATLine line;
AggrCTCInfo aggr;
CTSInfo cur;
ConcatString key;
unixtime ut;
int n, n_ties;
map<ConcatString, AggrCTCInfo>::iterator it;
//
// Process the STAT lines
//
while(f >> line) {
if(line.is_header()) continue;
n_in++;
if(job.is_keeper(line)) {
job.dump_stat_line(line);
//
// Zero out the contingency table object
//
cur.cts.zero_out();
//
// Switch on the line type looking only for contingency
// table types of lines
//
switch(line.type()) {
case(stat_fho):
parse_fho_ctable(line, cur.cts);
break;
case(stat_ctc):
parse_ctc_ctable(line, cur.cts);
break;
case(stat_nbrctc):
parse_nbrctc_ctable(line, cur.cts);
break;
default:
mlog << Error << "\naggr_ctc_lines() -> "
<< "line type value of " << statlinetype_to_string(line.type())
<< " not currently supported for the aggregation job.\n"
<< "ERROR occurred on STAT line:\n" << line << "\n\n";
throw(1);
} // end switch
//
// Build the map key for the current line
//
key = job.get_case_info(line);
//
// Add a new map entry, if necessary
//
if(m.count(key) == 0) {
aggr.valid_ts.clear();
aggr.baser_ts.clear();
aggr.fmean_ts.clear();
aggr.acc_ts.clear();
aggr.pody_ts.clear();
aggr.podn_ts.clear();
aggr.pofd_ts.clear();
aggr.far_ts.clear();
aggr.csi_ts.clear();
aggr.hk_ts.clear();
aggr.cts_info = cur;
aggr.hdr.clear();
m[key] = aggr;
}
//
// Increment counts in the existing map entry
//
else {
m[key].cts_info.cts.set_fy_oy(m[key].cts_info.cts.fy_oy() +
cur.cts.fy_oy());
m[key].cts_info.cts.set_fy_on(m[key].cts_info.cts.fy_on() +
cur.cts.fy_on());
m[key].cts_info.cts.set_fn_oy(m[key].cts_info.cts.fn_oy() +
cur.cts.fn_oy());
m[key].cts_info.cts.set_fn_on(m[key].cts_info.cts.fn_on() +
cur.cts.fn_on());
}
//
// Keep track of scores for each time step for VIF
//
if(job.vif_flag) {
//
// Cannot compute VIF when the times are not unique
//
ut = line.fcst_valid_beg();
if(m[key].valid_ts.has((double) ut)) {
mlog << Warning << "\naggr_ctc_lines() -> "
<< "the variance inflation factor adjustment can "
<< "only be computed for time series with unique "
<< "valid times.\n\n";
job.vif_flag = 0;
}
else {
m[key].valid_ts.add((double) ut);
}
//
// Compute the stats for the current time
//
cur.compute_stats();
//
// Append the stats
//
m[key].baser_ts.add(cur.baser.v);
m[key].fmean_ts.add(cur.fmean.v);
m[key].acc_ts.add(cur.acc.v);
m[key].pody_ts.add(cur.pody.v);
m[key].podn_ts.add(cur.podn.v);
m[key].pofd_ts.add(cur.pofd.v);
m[key].far_ts.add(cur.far.v);
m[key].csi_ts.add(cur.csi.v);
m[key].hk_ts.add(cur.hk.v);
}
//
// Keep track of the unique header column entries
//
m[key].hdr.add(line);
n_out++;
}
} // end while