-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpair_data_ensemble.cc
1907 lines (1450 loc) · 52.8 KB
/
pair_data_ensemble.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
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
////////////////////////////////////////////////////////////////////////
using namespace std;
#include <cstdio>
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <cmath>
#include <set>
#include <limits>
#include "pair_data_ensemble.h"
#include "ens_stats.h"
#include "obs_error.h"
#include "vx_data2d_factory.h"
#include "vx_data2d.h"
#include "vx_data2d_grib.h"
#include "vx_util.h"
#include "vx_grid.h"
#include "vx_math.h"
#include "vx_log.h"
using namespace std;
////////////////////////////////////////////////////////////////////////
//
// Code for class PairDataEnsemble
//
////////////////////////////////////////////////////////////////////////
PairDataEnsemble::PairDataEnsemble() {
init_from_scratch();
}
////////////////////////////////////////////////////////////////////////
PairDataEnsemble::~PairDataEnsemble() {
clear();
}
////////////////////////////////////////////////////////////////////////
PairDataEnsemble::PairDataEnsemble(const PairDataEnsemble &pd) {
init_from_scratch();
assign(pd);
}
////////////////////////////////////////////////////////////////////////
PairDataEnsemble & PairDataEnsemble::operator=(const PairDataEnsemble &pd) {
if(this == &pd) return(*this);
assign(pd);
return(*this);
}
////////////////////////////////////////////////////////////////////////
void PairDataEnsemble::init_from_scratch() {
e_na = (NumArray *) 0;
n_ens = 0;
ssvar_bins = (SSVARInfo *) 0;
clear();
return;
}
////////////////////////////////////////////////////////////////////////
void PairDataEnsemble::clear() {
int i;
PairBase::clear();
obs_error_entry.clear();
obs_error_flag = false;
for(i=0; i<n_ens; i++) e_na[i].clear();
if(e_na) { delete [] e_na; e_na = (NumArray *) 0; }
n_ens = 0;
v_na.clear();
r_na.clear();
crps_emp_na.clear();
crps_na.clear();
ign_na.clear();
pit_na.clear();
n_pair = 0;
skip_const = false;
skip_ba.clear();
rhist_na.clear();
relp_na.clear();
phist_na.clear();
var_na.clear();
var_oerr_na.clear();
var_plus_oerr_na.clear();
esum_na.clear();
esumsq_na.clear();
mn_na.clear();
mn_oerr_na.clear();
if(ssvar_bins) { delete [] ssvar_bins; ssvar_bins = (SSVARInfo *) 0; }
ssvar_bin_size = bad_data_double;
phist_bin_size = bad_data_double;
crpss = bad_data_double;
me = bad_data_double;
rmse = bad_data_double;
me_oerr = bad_data_double;
rmse_oerr = bad_data_double;
return;
}
////////////////////////////////////////////////////////////////////////
void PairDataEnsemble::extend(int n, bool exact) {
int i;
// Allocate memory for the number of observations.
// Only applies to arrays sized by n_obs which does not include:
// rhist_na, relp_na, phist_na
PairBase::extend(n, exact);
obs_error_entry.extend(n, exact);
for(i=0; i<n_ens; i++) e_na[i].extend(n, exact);
v_na.extend (n, exact);
r_na.extend (n, exact);
crps_emp_na.extend (n, exact);
crps_na.extend (n, exact);
ign_na.extend (n, exact);
pit_na.extend (n, exact);
skip_ba.extend (n, exact);
var_na.extend (n, exact);
var_oerr_na.extend (n, exact);
var_plus_oerr_na.extend (n, exact);
esum_na.extend (n, exact);
esumsq_na.extend (n, exact);
mn_na.extend (n, exact);
mn_oerr_na.extend (n, exact);
return;
}
////////////////////////////////////////////////////////////////////////
void PairDataEnsemble::assign(const PairDataEnsemble &pd) {
int i;
clear();
set_mask_name(pd.mask_name.c_str());
set_mask_area_ptr(pd.mask_area_ptr);
set_msg_typ(pd.msg_typ.c_str());
set_msg_typ_vals(pd.msg_typ_vals);
set_interp_mthd(pd.interp_mthd);
set_interp_wdth(pd.interp_wdth);
set_interp_shape(pd.interp_shape);
// PairBase
n_obs = pd.n_obs;
sid_sa = pd.sid_sa;
lat_na = pd.lat_na;
lon_na = pd.lon_na;
x_na = pd.x_na;
y_na = pd.y_na;
wgt_na = pd.wgt_na;
vld_ta = pd.vld_ta;
lvl_na = pd.lvl_na;
elv_na = pd.elv_na;
o_na = pd.o_na;
o_qc_sa = pd.o_qc_sa;
cmn_na = pd.cmn_na;
csd_na = pd.csd_na;
cdf_na = pd.cdf_na;
// PairDataEnsemble
v_na = pd.v_na;
r_na = pd.r_na;
crps_emp_na = pd.crps_emp_na;
crps_na = pd.crps_na;
ign_na = pd.ign_na;
pit_na = pd.pit_na;
n_pair = pd.n_pair;
skip_const = pd.skip_const;
skip_ba = pd.skip_ba;
var_na = pd.var_na;
var_oerr_na = pd.var_oerr_na;
var_plus_oerr_na = pd.var_plus_oerr_na;
esum_na = pd.esum_na;
esumsq_na = pd.esumsq_na;
mn_na = pd.mn_na;
mn_oerr_na = pd.mn_oerr_na;
rhist_na = pd.rhist_na;
relp_na = pd.relp_na;
phist_na = pd.phist_na;
if(pd.ssvar_bins){
ssvar_bins = new SSVARInfo[pd.ssvar_bins[0].n_bin];
for(i=0; i < pd.ssvar_bins[0].n_bin; i++){
ssvar_bins[i] = pd.ssvar_bins[i];
}
} else ssvar_bins = 0;
ssvar_bin_size = pd.ssvar_bin_size;
phist_bin_size = pd.phist_bin_size;
crpss = pd.crpss;
me = pd.me;
rmse = pd.rmse;
me_oerr = pd.me_oerr;
rmse_oerr = pd.rmse_oerr;
set_ens_size(pd.n_ens);
for(i=0; i<n_ens; i++) e_na[i] = pd.e_na[i];
obs_error_entry = pd.obs_error_entry;
obs_error_flag = pd.obs_error_flag;
return;
}
////////////////////////////////////////////////////////////////////////
bool PairDataEnsemble::has_obs_error() const {
return(obs_error_flag);
}
////////////////////////////////////////////////////////////////////////
void PairDataEnsemble::add_ens(int member, double v) {
e_na[member].add(v);
return;
}
////////////////////////////////////////////////////////////////////////
void PairDataEnsemble::add_ens_var_sums(int i_obs, double v) {
// Initialize new sums to 0
if(i_obs >= esum_na.n()) {
esum_na.add(0.0);
esumsq_na.add(0.0);
}
// Track sums of the raw ensemble member values
if(!is_bad_data(v)) {
esum_na.inc(i_obs, v);
esumsq_na.inc(i_obs, v*v);
}
return;
}
////////////////////////////////////////////////////////////////////////
void PairDataEnsemble::set_ens_size(int n) {
// Allocate a NumArray to store ensemble values for each member
n_ens = n;
e_na = new NumArray [n_ens];
return;
}
////////////////////////////////////////////////////////////////////////
void PairDataEnsemble::add_obs_error_entry(ObsErrorEntry *e) {
obs_error_entry.add(e);
if(e != 0) obs_error_flag = true;
return;
}
////////////////////////////////////////////////////////////////////////
void PairDataEnsemble::compute_pair_vals(const gsl_rng *rng_ptr) {
int i, j, k, n_vld, n_bel, n_tie;
int n_skip_const, n_skip_vld;
NumArray src_na, dest_na, cur_ens;
double mean, var_unperturbed, var_perturbed;
double crps, ign, pit;
// Check if the ranks have already been computed
if(r_na.n() == o_na.n()) return;
// Compute the rank for each observation
for(i=0, n_pair=0, n_skip_const=0, n_skip_vld=0; i<o_na.n(); i++) {
// Initialize
cur_ens.erase();
// Compute the number of ensemble values less than the observation
for(j=0, n_vld=0, n_bel=0, n_tie=0; j<n_ens; j++) {
// Skip bad data
if(!is_bad_data(e_na[j][i])) {
// Increment the valid count
n_vld++;
// Store the current ensemble value
cur_ens.add(e_na[j][i]);
// Keep track of the number of ties and the number below
if(is_eq(e_na[j][i], o_na[i])) n_tie++;
else if(e_na[j][i] < o_na[i]) n_bel++;
}
} // end for j
// Store the number of valid ensemble values
v_na.add(n_vld);
// Skip points missing ensemble data
if(n_vld != n_ens) {
n_skip_vld++;
skip_ba.add(true);
}
// Skip points with constant value, if requested
else if(skip_const && n_tie == n_ens) {
n_skip_const++;
skip_ba.add(true);
}
// Increment the n_pair counter
else {
n_pair++;
skip_ba.add(false);
}
// Store bad data values if skipping this point
if(skip_ba[i]) {
var_na.add(bad_data_double);
mn_oerr_na.add(bad_data_double);
var_oerr_na.add(bad_data_double);
var_plus_oerr_na.add(bad_data_double);
r_na.add(bad_data_int);
crps_emp_na.add(bad_data_double);
crps_na.add(bad_data_double);
ign_na.add(bad_data_double);
pit_na.add(bad_data_double);
}
// Otherwise, compute scores
else {
// Compute the variance of the unperturbed ensemble members
var_unperturbed = compute_variance(esum_na[i], esumsq_na[i], v_na[i]);
var_na.add(var_unperturbed);
// Process the observation error information.
ObsErrorEntry * e = (has_obs_error() ? obs_error_entry[i] : 0);
if(e) {
// Compute perturbed ensemble mean and variance
cur_ens.compute_mean_variance(mean, var_perturbed);
mn_oerr_na.add(mean);
var_oerr_na.add(var_perturbed);
// Compute the variance plus observation error variance.
var_plus_oerr_na.add(var_unperturbed +
dist_var(e->dist_type,
e->dist_parm[0], e->dist_parm[1]));
}
// If no observation error specified, store bad data values.
else {
mn_oerr_na.add(bad_data_double);
var_oerr_na.add(bad_data_double);
var_plus_oerr_na.add(bad_data_double);
}
// With no ties, the rank is the number below plus 1
if(n_tie == 0) {
r_na.add(n_bel+1);
}
// With ties present, randomly assign the rank in:
// [n_bel+1, n_bel+n_tie+1]
else {
// Initialize
dest_na.clear();
src_na.clear();
for(k=n_bel+1; k<=n_bel+n_tie+1; k++) src_na.add(k);
// Randomly choose one of the ranks
ran_choose(rng_ptr, src_na, dest_na, 1);
// Store the rank
r_na.add(nint(dest_na[0]));
}
// Store ensemble stats for the current point
crps_emp_na.add(compute_crps_emp(o_na[i], cur_ens));
compute_crps_ign_pit(o_na[i], cur_ens, crps, ign, pit);
crps_na.add(crps);
ign_na.add(ign);
pit_na.add(pit);
}
} // end for i
if(n_skip_vld > 0) {
mlog << Debug(2)
<< "Skipping " << n_skip_vld << " of " << o_na.n()
<< " points due to missing ensemble values.\n";
}
if(skip_const) {
mlog << Debug(2)
<< "Skipping " << n_skip_const << " of " << o_na.n()
<< " points with constant value.\n";
}
return;
}
////////////////////////////////////////////////////////////////////////
void PairDataEnsemble::compute_rhist() {
int i, rank;
// Clear the ranked histogram
rhist_na.clear();
// Initialize the histogram counts to 0
for(i=0; i<=n_ens; i++) rhist_na.add(0);
// The compute_pair_vals() routine should have already been called.
// Loop through the ranks and populate the histogram.
for(i=0; i<r_na.n(); i++) {
// Get the current rank
rank = nint(r_na[i]);
// Increment the histogram counts
if(!is_bad_data(rank)) rhist_na.set(rank-1, rhist_na[rank-1]+1);
} // end for i
return;
}
////////////////////////////////////////////////////////////////////////
void PairDataEnsemble::compute_relp() {
int i, j, n;
double d, min_d;
NumArray min_ens;
// Clear the RELP histogram
relp_na.clear();
// Allocate space
min_ens.extend(n_ens);
// Initialize counts to 0
for(i=0; i<n_ens; i++) relp_na.add(0);
// Loop through the observations and update the counts
for(i=0; i<o_na.n(); i++) {
if(skip_ba[i]) continue;
// Search for the minimum difference
for(j=0, min_d=1.0e10; j<n_ens; j++) {
// Absolute value of the difference
d = abs(e_na[j][i] - o_na[i]);
// Store the closest member
if(d < min_d) {
min_ens.erase();
min_ens.add(j);
min_d = d;
}
// Store all members tied for closest
else if(is_eq(d, min_d)) {
min_ens.add(j);
}
} // end for j
// Increment fractional RELP counts for each closest member
for(j=0, n=min_ens.n(); j<n; j++) {
relp_na.set(min_ens[j], relp_na[(min_ens[j])] + (double) 1.0/n);
}
} // end for i
return;
}
////////////////////////////////////////////////////////////////////////
void PairDataEnsemble::compute_phist() {
int i, bin;
// Clear the PIT histogram
phist_na.clear();
// Initialize the histogram counts to 0
for(i=0; i<ceil(1.0/phist_bin_size); i++) phist_na.add(0);
// The compute_pair_vals() routine should have already been called.
// Loop through the PIT values and populate the histogram.
for(i=0; i<pit_na.n(); i++) {
if(skip_ba[i] || is_bad_data(pit_na[i])) continue;
if(pit_na[i] < 0.0 || pit_na[i] > 1.0) {
mlog << Warning << "\nPairDataEnsemble::compute_phist() -> "
<< "probability integral transform value ("
<< pit_na[i] << ") is outside of valid range [0, 1].\n\n";
continue;
}
// Determine the bin
bin = (is_eq(pit_na[i], 1.0) ?
phist_na.n() - 1 : floor(pit_na[i]/phist_bin_size));
// Increment the histogram counts
phist_na.set(bin, phist_na[bin]+1);
} // end for i
return;
}
////////////////////////////////////////////////////////////////////////
// Comparison method for ssvar bins
struct ssvar_bin_comp {
bool operator() (const string& lhs, const string& rhs) const {
return atof(lhs.data()) < atof(rhs.data());
}
};
void PairDataEnsemble::compute_ssvar() {
int i, j;
double mn, var;
ssvar_bin_map bins;
NumArray cur;
// Check number of points
if(o_na.n() != mn_na.n()) {
mlog << Error << "\nPairDataEnsemble::compute_ssvar() -> "
<< "the number of ensemble mean points ("
<< mn_na.n()
<< ") should match the number of observation points ("
<< o_na.n() << ")!\n\n";
exit(1);
}
for(j=0; j<n_ens; j++) {
if(o_na.n() != e_na[j].n()) {
mlog << Error << "\nPairDataEnsemble::compute_ssvar() -> "
<< "the number of ensemble member " << j+1 << " points ("
<< e_na[j].n()
<< ") should match the number of observation points ("
<< o_na.n() << ")!\n\n";
exit(1);
}
}
// Compute the variance of ensemble member values at each point
for(i=0; i<o_na.n(); i++) {
// Check if point should be skipped
if(skip_ba[i]) continue;
// Store ensemble values for the current observation
for(j=0, cur.erase(); j<n_ens; j++) cur.add(e_na[j][i]);
// Compute standard deviation
cur.compute_mean_variance(mn, var);
// Build a variance point
ens_ssvar_pt pt;
pt.var = var;
pt.f = mn_na[i];
pt.o = o_na[i];
pt.w = wgt_na[i];
// Determine the bin for the current point and add it to the list
// Bins are defined starting at 0 and are left-closed, right-open
j = floor(var/ssvar_bin_size);
string ssvar_min = str_format("%.5e", j*ssvar_bin_size).contents();
if( !bins.count(ssvar_min) ){
ssvar_pt_list pts;
pts.push_back(pt);
bins[ssvar_min] = pts;
// Print warning for too many bins
if(bins.size() == n_warn_ssvar_bins) {
mlog << Warning << "\nPairDataEnsemble::compute_ssvar() -> "
<< "writing at least " << n_warn_ssvar_bins
<< " SSVAR output lines. Increase the ssvar_bin_size "
<< "config file setting for this variable to reduce "
<< "the number of variance bins.\n\n";
}
} else {
bins[ssvar_min].push_back(pt);
}
} // end for i
// Sort the bins
set<string,ssvar_bin_comp> sorted_bins;
for( ssvar_bin_map::iterator map_it = bins.begin();
map_it != bins.end(); map_it++ ){
sorted_bins.insert( (*map_it).first );
}
// Report the number of bins built
int n_bin = sorted_bins.size();
mlog << Debug(4) << "PairDataEnsemble::compute_ssvar() - "
<< "Built " << n_bin << " variance spread/skill bins from "
<< o_na.n() << " observations\n";
// Check for no bins
if(n_bin == 0) return;
// Build a list of SSVARInfo objects
ssvar_bins = new SSVARInfo[n_bin];
i=0;
for( set<string>::iterator set_it = sorted_bins.begin();
set_it != sorted_bins.end(); set_it++, i++ ){
ssvar_pt_list* pts = &( bins[*set_it] );
var = 0;
double f = 0, o = 0, fo = 0, ff = 0, oo = 0, w = 0;
for(j=0; j < (int)pts->size(); j++){
var += (*pts)[j].w * (*pts)[j].var;
f += (*pts)[j].w * (*pts)[j].f;
o += (*pts)[j].w * (*pts)[j].o;
fo += (*pts)[j].w * (*pts)[j].f * (*pts)[j].o;
ff += (*pts)[j].w * (*pts)[j].f * (*pts)[j].f;
oo += (*pts)[j].w * (*pts)[j].o * (*pts)[j].o;
w += (*pts)[j].w;
}
ssvar_bins[i].n_bin = n_bin;
ssvar_bins[i].bin_i = i;
ssvar_bins[i].bin_n = pts->size();
ssvar_bins[i].var_min = atof( (*set_it).data() );
ssvar_bins[i].var_max = ssvar_bins[i].var_min + ssvar_bin_size;
ssvar_bins[i].var_mean = var / w;
ssvar_bins[i].sl1l2_info.scount = pts->size();
ssvar_bins[i].sl1l2_info.fbar = f / w;
ssvar_bins[i].sl1l2_info.obar = o / w;
ssvar_bins[i].sl1l2_info.fobar = fo / w;
ssvar_bins[i].sl1l2_info.ffbar = ff / w;
ssvar_bins[i].sl1l2_info.oobar = oo / w;
if( i < 100 ){
mlog << Debug(4) << " SSVAR[ "
<< "bin_i: " << ssvar_bins[i].bin_i << " "
<< "bin_n: " << ssvar_bins[i].bin_n << " "
<< "var: (" << ssvar_bins[i].var_min << ", "
<< ssvar_bins[i].var_max << ") "
<< "fbar: " << ssvar_bins[i].sl1l2_info.fbar << " "
<< "obar: " << ssvar_bins[i].sl1l2_info.obar << " ]\n";
} else if( i == 100 ){
mlog << Debug(4) << " SSVAR message 101 through "
<< n_bin << " omitted\n";
}
}
return;
}
////////////////////////////////////////////////////////////////////////
//
// Apply conditional observation threshold and return a subset of pairs.
// The compute_pair_vals() functions should have already been called.
// After retrieving the subset, the compute statistics functions should
// be called again.
//
////////////////////////////////////////////////////////////////////////
PairDataEnsemble PairDataEnsemble::subset_pairs(const SingleThresh &ot) const {
// Check for no work to be done
if(ot.get_type() == thresh_na) return(*this);
int i, j;
PairDataEnsemble pd;
// Set the ensemble size and allocate memory
pd.set_ens_size(n_ens);
pd.extend(n_obs);
pd.phist_bin_size = phist_bin_size;
pd.ssvar_bin_size = ssvar_bin_size;
pd.obs_error_entry = obs_error_entry;
pd.obs_error_flag = obs_error_flag;
bool cmn_flag = set_climo_flag(o_na, cmn_na);
bool csd_flag = set_climo_flag(o_na, csd_na);
bool wgt_flag = set_climo_flag(o_na, wgt_na);
// Loop over the pairs
for(i=0; i<n_obs; i++) {
// Check for bad data and apply observation threshold
if(is_bad_data(o_na[i]) ||
skip_ba[i] ||
(cmn_flag && is_bad_data(cmn_na[i])) ||
(csd_flag && is_bad_data(csd_na[i])) ||
(wgt_flag && is_bad_data(wgt_na[i])) ||
!ot.check(o_na[i], cmn_na[i], csd_na[i])) continue;
// Add data for the current observation but only include data
// required for ensemble output line types.
//
// Include in subset:
// wgt_na, o_na, cmn_na, csd_na, v_na, r_na,
// crps_emp_na, crps_na, ign_na, pit_na, var_na,
// var_oerr_na, var_plus_oerr_na, mn_na, mn_oerr_na,
// e_na
//
// Exclude from subset:
// sid_sa, lat_na, lon_na, x_na, y_na, vld_ta, lvl_ta, elv_ta,
// o_qc_sa, esum_na, esumsq_na
pd.wgt_na.add(wgt_na[i]);
pd.o_na.add(o_na[i]);
pd.cmn_na.add(cmn_na[i]);
pd.csd_na.add(csd_na[i]);
pd.cdf_na.add(cdf_na[i]);
pd.v_na.add(v_na[i]);
pd.r_na.add(r_na[i]);
pd.crps_emp_na.add(crps_emp_na[i]);
pd.crps_na.add(crps_na[i]);
pd.ign_na.add(ign_na[i]);
pd.pit_na.add(pit_na[i]);
pd.skip_ba.add(false);
pd.var_na.add(var_na[i]);
pd.var_oerr_na.add(var_oerr_na[i]);
pd.var_plus_oerr_na.add(var_plus_oerr_na[i]);
pd.mn_na.add(mn_na[i]);
pd.mn_oerr_na.add(mn_oerr_na[i]);
for(j=0; j<n_ens; j++) pd.e_na[j].add(e_na[j][i]);
// Increment counters
pd.n_obs++;
pd.n_pair++;
} // end for i
mlog << Debug(3)
<< "Using " << pd.n_obs << " of " << n_obs
<< " ensemble pairs for observation filtering threshold "
<< ot.get_str() << ".\n";
return(pd);
}
////////////////////////////////////////////////////////////////////////
//
// Code for class VxPairDataEnsemble
//
////////////////////////////////////////////////////////////////////////
VxPairDataEnsemble::VxPairDataEnsemble() {
init_from_scratch();
}
////////////////////////////////////////////////////////////////////////
VxPairDataEnsemble::~VxPairDataEnsemble() {
clear();
}
////////////////////////////////////////////////////////////////////////
VxPairDataEnsemble::VxPairDataEnsemble(const VxPairDataEnsemble &vx_pd) {
init_from_scratch();
assign(vx_pd);
}
////////////////////////////////////////////////////////////////////////
VxPairDataEnsemble & VxPairDataEnsemble::operator=(const VxPairDataEnsemble &vx_pd) {
if(this == &vx_pd) return(*this);
assign(vx_pd);
return(*this);
}
////////////////////////////////////////////////////////////////////////
void VxPairDataEnsemble::init_from_scratch() {
fcst_info = (VarInfo *) 0;
climo_info = (VarInfo *) 0;
obs_info = (VarInfo *) 0;
pd = (PairDataEnsemble ***) 0;
n_msg_typ = 0;
n_mask = 0;
n_interp = 0;
clear();
return;
}
////////////////////////////////////////////////////////////////////////
void VxPairDataEnsemble::clear() {
int i, j, k;
if(fcst_info) { delete fcst_info; fcst_info = (VarInfo *) 0; }
if(climo_info) { delete climo_info; climo_info = (VarInfo *) 0; }
if(obs_info) { delete obs_info; obs_info = (VarInfo *) 0; }
desc.clear();
interp_thresh = 0;
msg_typ_sfc.clear();
fcst_dpa.clear();
climo_mn_dpa.clear();
climo_sd_dpa.clear();
sid_inc_filt.clear();
sid_exc_filt.clear();
obs_qty_filt.clear();
obs_error_info = (ObsErrorInfo *) 0;
fcst_ut = (unixtime) 0;
beg_ut = (unixtime) 0;
end_ut = (unixtime) 0;
for(i=0; i<n_msg_typ; i++) {
for(j=0; j<n_mask; j++) {
for(k=0; k<n_interp; k++) {
pd[i][j][k].clear();
}
}
}
n_msg_typ = 0;
n_mask = 0;
n_interp = 0;
return;
}
////////////////////////////////////////////////////////////////////////
void VxPairDataEnsemble::assign(const VxPairDataEnsemble &vx_pd) {
int i, j, k;
clear();
set_fcst_info(vx_pd.fcst_info);
set_climo_info(vx_pd.climo_info);
set_obs_info(vx_pd.obs_info);
desc = vx_pd.desc;
fcst_ut = vx_pd.fcst_ut;
beg_ut = vx_pd.beg_ut;
end_ut = vx_pd.end_ut;
sid_inc_filt = vx_pd.sid_inc_filt;
sid_exc_filt = vx_pd.sid_exc_filt;
obs_qty_filt = vx_pd.obs_qty_filt;
obs_error_info = vx_pd.obs_error_info;
interp_thresh = vx_pd.interp_thresh;
msg_typ_sfc = vx_pd.msg_typ_sfc;
fcst_dpa = vx_pd.fcst_dpa;
climo_mn_dpa = vx_pd.climo_mn_dpa;
climo_sd_dpa = vx_pd.climo_sd_dpa;
set_pd_size(vx_pd.n_msg_typ, vx_pd.n_mask, vx_pd.n_interp);
for(i=0; i<vx_pd.n_msg_typ; i++) {
for(j=0; j<vx_pd.n_mask; j++) {
for(k=0; k<vx_pd.n_interp; k++) {
pd[i][j][k] = vx_pd.pd[i][j][k];
}
}
}
return;
}
////////////////////////////////////////////////////////////////////////
void VxPairDataEnsemble::set_fcst_info(VarInfo *info) {
VarInfoFactory f;
// Deallocate, if necessary
if(fcst_info) { delete fcst_info; fcst_info = (VarInfo *) 0; }
// Perform a deep copy
fcst_info = f.new_var_info(info->file_type());
*fcst_info = *info;
return;
}
////////////////////////////////////////////////////////////////////////
void VxPairDataEnsemble::set_climo_info(VarInfo *info) {
VarInfoFactory f;
// Deallocate, if necessary
if(climo_info) { delete climo_info; climo_info = (VarInfo *) 0; }
// Perform a deep copy
climo_info = f.new_var_info(info->file_type());
*climo_info = *info;
return;
}
////////////////////////////////////////////////////////////////////////
void VxPairDataEnsemble::set_obs_info(VarInfo *info) {
VarInfoFactory f;
// Deallocate, if necessary
if(obs_info) { delete obs_info; obs_info = (VarInfo *) 0; }
// Perform a deep copy
obs_info = f.new_var_info(info->file_type());
*obs_info = *info;
return;
}
////////////////////////////////////////////////////////////////////////
void VxPairDataEnsemble::set_desc(const char *s) {
desc = s;
return;
}
////////////////////////////////////////////////////////////////////////
void VxPairDataEnsemble::set_interp_thresh(double t) {
interp_thresh = t;
return;
}
////////////////////////////////////////////////////////////////////////
void VxPairDataEnsemble::set_msg_typ_sfc(const StringArray &sa) {