-
Notifications
You must be signed in to change notification settings - Fork 22
/
grid_stat.cc
3011 lines (2420 loc) · 113 KB
/
grid_stat.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: grid_stat.cc
//
// Description:
// Based on user-specified parameters, this tool compares a
// a gridded forecast fields to gridded analyses. It computes
// many verification scores and statistics, including confidence
// intervals, to summarize the comparison.
//
// Mod# Date Name Description
// ---- ---- ---- -----------
// 000 04/18/07 Halley Gotway New
// 001 10/05/07 Halley Gotway Add checks to only compute
// confidence intervals for sufficiently large
// sample sizes.
// 002 10/19/07 Halley Gotway Add ability to smooth the forecast
// field using the interp_method and interp_width
// parameters
// 003 01/11/08 Halley Gotway Modify sprintf statements which
// use the GRIB code abbreviation string
// 004 02/06/08 Halley Gotway Modify to read the updated NetCDF
// output of PCP-Combine
// 005 02/11/08 Halley Gotway Remove BIAS from the CNT line
// since it's the same as the ME.
// 006 02/12/08 Halley Gotway Fix bug in writing COP line to
// write out OY and ON rather than FY and FN.
// 007 02/14/08 Halley Gotway Apply the smoothing operation to
// both forecast and observation fields rather than
// just the forecast.
// 008 02/25/08 Halley Gotway Write the STAT lines using the
// routines in the vx_met_util library.
// 009 02/25/08 Halley Gotway Add support for the NBRCTS and
// NBRCNT line types.
// 010 07/01/08 Halley Gotway Add the rank_corr_flag to the
// config file to disable computing rank
// correlations.
// 011 09/23/08 Halley Gotway Change argument sequence for the
// get_grib_record routine.
// 012 10/31/08 Halley Gotway Use the get_file_type routine to
// determine the input file types.
// 013 11/14/08 Halley Gotway Restructure organization of Grid-
// Stat and move several functions to libraries.
// 014 03/13/09 Halley Gotway Add support for verifying
// probabilistic forecasts.
// 015 10/22/09 Halley Gotway Fix output_flag cut and paste bug.
// 016 05/03/10 Halley Gotway Don't write duplicate NetCDF
// matched pair variables or probabilistic
// difference fields.
// 017 05/06/10 Halley Gotway Add interp_flag config parameter.
// 018 05/27/10 Halley Gotway Add -fcst_valid, -fcst_lead,
// -obs_valid, and -obs_lead command line options.
// 019 06/08/10 Halley Gotway Add support for multi-category
// contingency tables.
// 020 06/30/10 Halley Gotway Enhance grid equality checks.
// 021 07/27/10 Halley Gotway Add lat/lon variables to NetCDF.
// 022 10/28/11 Holmes Added use of command line class to
// parse the command line arguments.
// 023 11/14/11 Holmes Added code to enable reading of
// multiple config files.
// 024 12/09/11 Halley Gotway When zero matched pairs are found
// print the status message before continuing.
// 025 02/02/12 Bullock Changed default output directory to "."
// 026 04/30/12 Halley Gotway Switch to using vx_config library.
// 027 04/30/12 Halley Gotway Move -fcst_valid, -fcst_lead,
// -obs_valid, and -obs_lead command line options
// to config file.
// 028 08/21/13 Halley Gotway Fix sizing of output tables for 12
// or more probabilstic thresholds.
// 029 05/20/14 Halley Gotway Add AFSS, UFSS, F_RATE, and O_RATE
// to the NBRCNT line type.
// 030 11/12/14 Halley Gotway Pass the obtype entry from the
// from the config file to the output files.
// 031 02/19/15 Halley Gotway Add automated regridding.
// 032 08/04/15 Halley Gotway Add conditional continuous verification.
// 033 09/04/15 Halley Gotway Add climatology and SAL1L2 and VAL1L2
// output line types.
// 034 05/10/16 Halley Gotway Add grid weighting.
// 035 05/20/16 Prestopnik J Removed -version (now in command_line.cc)
// 036 02/14/17 Win MET-621 enhancement support- additional
// nc_pairs_flag 'apply_mask'
// 037 05/15/17 Prestopnik P Add shape for regrid, nbrhd and interp
// 038 06/26/17 Halley Gotway Add ECLV line type.
// 039 08/18/17 Halley Gotway Add fourier decomposition.
// 040 08/18/17 Halley Gotway Add GRAD output line type.
// 041 11/01/17 Halley Gotway Add binned climatologies.
// 042 12/15/17 Halley Gotway Restructure config logic to make
// all options settable for each verification task.
// 043 02/14/17 Halley Gotway Add nbrhd.field option to skip the
// computation of fractional coverage fields.
// 044 02/22/19 Halley Gotway Make gradient dx/dy configurable.
// 045 04/08/19 Halley Gotway Add percentile thresholds.
// 046 04/01/19 Fillmore Add FCST and OBS units.
// 047 06/19/19 Halley Gotway Add DMAP output line type.
// 048 10/14/19 Halley Gotway Add support for climo distribution
// percentile thresholds.
// 049 11/15/19 Halley Gotway Apply climatology bins to
// continuous and probabilistic statistics.
// 050 03/02/20 Halley Gotway Add nc_pairs_var_name and rename
// nc_pairs_var_str to nc_pairs_var_suffix.
//
////////////////////////////////////////////////////////////////////////
using namespace std;
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <ctype.h>
#include <dirent.h>
#include <iostream>
#include <fstream>
#include <math.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "grid_stat.h"
#include "vx_statistics.h"
#include "vx_nc_util.h"
#include "vx_regrid.h"
#include "vx_log.h"
////////////////////////////////////////////////////////////////////////
static void process_command_line(int, char **);
static void setup_first_pass (const DataPlane &);
static void setup_txt_files(unixtime, int);
static void setup_table (AsciiTable &);
static void setup_nc_file (const GridStatNcOutInfo &, unixtime, int);
static void build_outfile_name(unixtime, int, const char *,
ConcatString &);
static void process_scores();
static void get_mask_points(const MaskPlane &, const DataPlane *,
const DataPlane *, const DataPlane *,
const DataPlane *, const DataPlane *,
PairDataPoint &);
static void do_cts (CTSInfo *&, int, const PairDataPoint *);
static void do_mcts (MCTSInfo &, int, const PairDataPoint *);
static void do_cnt_sl1l2 (const GridStatVxOpt &, const PairDataPoint *);
static void do_vl1l2 (VL1L2Info *&, int, const PairDataPoint *, const PairDataPoint *);
static void do_pct (const GridStatVxOpt &, const PairDataPoint *);
static void do_nbrcts(NBRCTSInfo *&, int, int, int, const PairDataPoint *);
static void do_nbrcnt(NBRCNTInfo &, int, int, int, const PairDataPoint *, const PairDataPoint *);
static void write_nc(const ConcatString &, const DataPlane &, int,
const ConcatString &, int, FieldType);
static void write_nbrhd_nc(const DataPlane &, const DataPlane &, int,
const SingleThresh &, const SingleThresh &,
int, int);
static void add_var_att_local(NcVar *, const char *, const ConcatString);
static void finish_txt_files();
static void clean_up();
static void usage();
static void set_outdir(const StringArray &);
static void set_logfile(const StringArray &);
static void set_verbosity(const StringArray &);
static void set_compress(const StringArray &);
static bool read_data_plane(VarInfo* info, DataPlane& dp, Met2dDataFile* mtddf,
const ConcatString &filename);
////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[]) {
// Set handler to be called for memory allocation error
set_new_handler(oom);
// Process the command line arguments
process_command_line(argc, argv);
// Compute the scores and write them out
process_scores();
// Close the text files and deallocate memory
clean_up();
return(0);
}
////////////////////////////////////////////////////////////////////////
void process_command_line(int argc, char **argv) {
CommandLine cline;
GrdFileType ftype, otype;
ConcatString default_config_file;
DataPlane dp;
// Set the default output directory
out_dir = replace_path(default_out_dir);
// Check for zero arguments
if(argc == 1) usage();
// Parse the command line into tokens
cline.set(argc, argv);
// Set the usage function
cline.set_usage(usage);
// Add the options function calls
cline.add(set_outdir, "-outdir", 1);
cline.add(set_logfile, "-log", 1);
cline.add(set_verbosity, "-v", 1);
cline.add(set_compress, "-compress", 1);
// Parse the command line
cline.parse();
// Check for error. There should be three arguments left:
// forecast, observation, and config filenames
if(cline.n() != 3) usage();
// Store the input file names
fcst_file = cline[0];
obs_file = cline[1];
config_file = cline[2];
// Create the default config file name
default_config_file = replace_path(default_config_filename);
// List the config files
mlog << Debug(1)
<< "Default Config File: " << default_config_file << "\n"
<< "User Config File: " << config_file << "\n";
// Read the config files
conf_info.read_config(default_config_file.c_str(), config_file.c_str());
// Get the forecast and observation file types from config, if present
ftype = parse_conf_file_type(conf_info.conf.lookup_dictionary(conf_key_fcst));
otype = parse_conf_file_type(conf_info.conf.lookup_dictionary(conf_key_obs));
// Read forecast file
if(!(fcst_mtddf = mtddf_factory.new_met_2d_data_file(fcst_file.c_str(), ftype))) {
mlog << Error << "\nTrouble reading forecast file \""
<< fcst_file << "\"\n\n";
exit(1);
}
// Read observation file
if(!(obs_mtddf = mtddf_factory.new_met_2d_data_file(obs_file.c_str(), otype))) {
mlog << Error << "\nTrouble reading observation file \""
<< obs_file << "\"\n\n";
exit(1);
}
// Store the input data file types
ftype = fcst_mtddf->file_type();
otype = obs_mtddf->file_type();
// Process the configuration
conf_info.process_config(ftype, otype);
// For python types read the first field to set the grid
if(is_python_grdfiletype(ftype)) {
if(!fcst_mtddf->data_plane(*conf_info.vx_opt[0].fcst_info, dp)) {
mlog << Error << "\nTrouble reading data from forecast file \""
<< fcst_file << "\"\n\n";
exit(1);
}
}
if(is_python_grdfiletype(otype)) {
if(!obs_mtddf->data_plane(*conf_info.vx_opt[0].obs_info, dp)) {
mlog << Error << "\nTrouble reading data from observation file \""
<< obs_file << "\"\n\n";
exit(1);
}
}
// Determine the verification grid
grid = parse_vx_grid(conf_info.vx_opt[0].fcst_info->regrid(),
&(fcst_mtddf->grid()), &(obs_mtddf->grid()));
// Compute weight for each grid point
parse_grid_weight(grid, conf_info.grid_weight_flag, wgt_dp);
// Set the model name
shc.set_model(conf_info.model.c_str());
// Set the obtype column
shc.set_obtype(conf_info.obtype.c_str());
// Use the first verification task to set the random number generator
// and seed value for bootstrap confidence intervals
rng_set(rng_ptr,
conf_info.vx_opt[0].boot_info.rng.c_str(),
conf_info.vx_opt[0].boot_info.seed.c_str());
// List the input files
mlog << Debug(1)
<< "Forecast File: " << fcst_file << "\n"
<< "Observation File: " << obs_file << "\n";
return;
}
////////////////////////////////////////////////////////////////////////
void setup_first_pass(const DataPlane &dp) {
// Unset the flag
is_first_pass = false;
// Process masks grids and polylines in the config file
conf_info.process_masks(grid);
// Create output text files as requested in the config file
// only if the ascii_output_flag is true.
if(conf_info.get_output_ascii_flag()){
setup_txt_files(dp.valid(), dp.lead());
}
// If requested, create a NetCDF file to store the matched pairs and
// difference fields for each GRIB code and masking region
if(conf_info.get_output_nc_flag()) {
setup_nc_file(conf_info.nc_info, dp.valid(), dp.lead());
}
return;
}
////////////////////////////////////////////////////////////////////////
void setup_txt_files(unixtime valid_ut, int lead_sec) {
int i, max_col, max_prob_col, max_mctc_col, n_prob, n_cat, n_eclv;
ConcatString base_name;
// Create output file names for the stat file and optional text files
build_outfile_name(valid_ut, lead_sec, "", base_name);
/////////////////////////////////////////////////////////////////////
//
// Setup the output STAT file
//
/////////////////////////////////////////////////////////////////////
// Get the maximum number of data columns
n_prob = conf_info.get_max_n_fprob_thresh();
n_cat = conf_info.get_max_n_cat_thresh() + 1;
n_eclv = conf_info.get_max_n_eclv_points();
max_prob_col = get_n_pjc_columns(n_prob);
max_mctc_col = get_n_mctc_columns(n_cat);
// Determine the maximum number of data columns
max_col = ( max_prob_col > max_stat_col ? max_prob_col : max_stat_col );
max_col = ( max_mctc_col > max_col ? max_mctc_col : max_col );
// Add the header columns
max_col += n_header_columns + 1;
// Initialize file stream
stat_out = (ofstream *) 0;
// Build the file name
stat_file << base_name << stat_file_ext;
// Create the output STAT file
open_txt_file(stat_out, stat_file.c_str());
// Setup the STAT AsciiTable
stat_at.set_size(conf_info.n_stat_row() + 1, max_col);
setup_table(stat_at);
// Write the text header row
write_header_row((const char **) 0, 0, 1, stat_at, 0, 0);
// Initialize the row index to 1 to account for the header
i_stat_row = 1;
/////////////////////////////////////////////////////////////////////
//
// Setup each of the optional output text files
//
/////////////////////////////////////////////////////////////////////
// Loop through output file type
for(i=0; i<n_txt; i++) {
// Only set it up if requested in the config file
if(conf_info.output_flag[i] == STATOutputType_Both) {
// Initialize file stream
txt_out[i] = (ofstream *) 0;
// Build the file name
txt_file[i] << base_name << "_" << txt_file_abbr[i]
<< txt_file_ext;
// Create the output text file
open_txt_file(txt_out[i], txt_file[i].c_str());
// Get the maximum number of columns for this line type
switch(i) {
case(i_mctc):
max_col = get_n_mctc_columns(n_cat) + n_header_columns + 1;
break;
case(i_pct):
max_col = get_n_pct_columns(n_prob) + n_header_columns + 1;
break;
case(i_pstd):
max_col = get_n_pstd_columns(n_prob) + n_header_columns + 1;
break;
case(i_pjc):
max_col = get_n_pjc_columns(n_prob) + n_header_columns + 1;
break;
case(i_prc):
max_col = get_n_prc_columns(n_prob) + n_header_columns + 1;
break;
case(i_eclv):
max_col = get_n_eclv_columns(n_eclv) + n_header_columns + 1;
break;
default:
max_col = n_txt_columns[i] + n_header_columns + 1;
break;
} // end switch
// Setup the text AsciiTable
txt_at[i].set_size(conf_info.n_txt_row(i) + 1, max_col);
setup_table(txt_at[i]);
// Write the text header row
switch(i) {
case(i_mctc):
write_mctc_header_row(1, n_cat, txt_at[i], 0, 0);
break;
case(i_pct):
write_pct_header_row(1, n_prob, txt_at[i], 0, 0);
break;
case(i_pstd):
write_pstd_header_row(1, n_prob, txt_at[i], 0, 0);
break;
case(i_pjc):
write_pjc_header_row(1, n_prob, txt_at[i], 0, 0);
break;
case(i_prc):
write_prc_header_row(1, n_prob, txt_at[i], 0, 0);
break;
case(i_eclv):
write_eclv_header_row(1, n_eclv, txt_at[i], 0, 0);
break;
default:
write_header_row(txt_columns[i], n_txt_columns[i], 1,
txt_at[i], 0, 0);
break;
} // end switch
// Initialize the row index to 1 to account for the header
i_txt_row[i] = 1;
}
}
return;
}
////////////////////////////////////////////////////////////////////////
void setup_table(AsciiTable &at) {
// Justify the STAT AsciiTable objects
justify_stat_cols(at);
// Set the precision
at.set_precision(conf_info.conf.output_precision());
// Set the bad data value
at.set_bad_data_value(bad_data_double);
// Set the bad data string
at.set_bad_data_str(na_str);
// Don't write out trailing blank rows
at.set_delete_trailing_blank_rows(1);
return;
}
////////////////////////////////////////////////////////////////////////
void setup_nc_file(const GridStatNcOutInfo & nc_info,
unixtime valid_ut, int lead_sec) {
// Create output NetCDF file name
build_outfile_name(valid_ut, lead_sec, "_pairs.nc", out_nc_file);
// Create a new NetCDF file and open it
nc_out = open_ncfile(out_nc_file.c_str(), true);
if(IS_INVALID_NC_P(nc_out)) {
mlog << Error << "\nsetup_nc_file() -> "
<< "trouble opening output NetCDF file "
<< out_nc_file << "\n\n";
exit(1);
}
// Add global attributes
write_netcdf_global(nc_out, out_nc_file.c_str(), program_name,
conf_info.model.c_str(), conf_info.obtype.c_str());
if(nc_info.do_diff) {
add_att(nc_out, "Difference", "Forecast Value - Observation Value");
}
// Add the projection information
write_netcdf_proj(nc_out, grid);
// Define Dimensions
lat_dim = add_dim(nc_out,"lat", (long) grid.ny());
lon_dim = add_dim(nc_out,"lon", (long) grid.nx());
// Add the lat/lon variables
if(nc_info.do_latlon) {
write_netcdf_latlon(nc_out, &lat_dim, &lon_dim, grid);
}
// Add grid weight variable
if(nc_info.do_weight) {
write_netcdf_grid_weight(nc_out, &lat_dim, &lon_dim,
conf_info.grid_weight_flag, wgt_dp);
}
return;
}
////////////////////////////////////////////////////////////////////////
void build_outfile_name(unixtime valid_ut, int lead_sec,
const char *suffix, ConcatString &str) {
//
// Create output file name
//
// Append the output directory and program name
str << cs_erase << out_dir << "/" << program_name;
// Append the output prefix, if defined
if(conf_info.output_prefix.nonempty())
str << "_" << conf_info.output_prefix;
// Append the timing information
str << "_"
<< sec_to_hhmmss(lead_sec) << "L_"
<< unix_to_yyyymmdd_hhmmss(valid_ut) << "V";
// Append the suffix
str << suffix;
return;
}
////////////////////////////////////////////////////////////////////////
void process_scores() {
int i, j, k, m, n;
int n_cat, n_wind, n_cov;
double dmin, dmax;
ConcatString cs;
// Forecast and observation fields
MaskPlane mask_mp;
DataPlane fcst_dp, obs_dp;
DataPlane fcst_dp_smooth, obs_dp_smooth;
DataPlane fcst_dp_thresh, obs_dp_thresh;
// Climatology mean and standard deviation
DataPlane cmn_dp, csd_dp;
DataPlane cmn_dp_smooth;
// Paired forecast, observation, climatology, and weight values
PairDataPoint pd;
// Thresholded fractional coverage pairs
PairDataPoint pd_thr;
// Allocate memory in one big chunk based on grid size
pd.extend(grid.nx()*grid.ny());
if(conf_info.output_flag[i_nbrctc] != STATOutputType_None ||
conf_info.output_flag[i_nbrcts] != STATOutputType_None ||
conf_info.output_flag[i_nbrcnt] != STATOutputType_None ||
conf_info.output_flag[i_dmap] != STATOutputType_None) {
pd_thr.extend(grid.nx()*grid.ny());
}
// Objects to handle vector winds
DataPlane fu_dp, ou_dp;
DataPlane fu_dp_smooth, ou_dp_smooth;
DataPlane cmnu_dp, csdu_dp, cmnu_dp_smooth;
PairDataPoint pd_u;
CTSInfo *cts_info = (CTSInfo *) 0;
MCTSInfo mcts_info;
VL1L2Info *vl1l2_info = (VL1L2Info *) 0;
NBRCNTInfo nbrcnt_info;
NBRCTSInfo *nbrcts_info = (NBRCTSInfo *) 0;
GRADInfo grad_info;
DMAPInfo dmap_info;
// Store the maximum number of each threshold type
n_cat = conf_info.get_max_n_cat_thresh();
n_wind = conf_info.get_max_n_wind_thresh();
n_cov = conf_info.get_max_n_cov_thresh();
// Allocate space for output statistics types
cts_info = new CTSInfo [n_cat];
vl1l2_info = new VL1L2Info [n_wind];
nbrcts_info = new NBRCTSInfo [n_cov];
// Compute scores for each verification task and write output_flag
for(i=0; i<conf_info.get_n_vx(); i++) {
// Read the gridded data from the input forecast file
if(!read_data_plane(conf_info.vx_opt[i].fcst_info,
fcst_dp, fcst_mtddf, fcst_file)) continue;
mlog << Debug(3)
<< "Reading forecast data for "
<< conf_info.vx_opt[i].fcst_info->magic_str() << ".\n";
// Set the forecast lead time
shc.set_fcst_lead_sec(fcst_dp.lead());
// Set the forecast valid time
shc.set_fcst_valid_beg(fcst_dp.valid());
shc.set_fcst_valid_end(fcst_dp.valid());
// Read the gridded data from the input observation file
if(!read_data_plane(conf_info.vx_opt[i].obs_info,
obs_dp, obs_mtddf, obs_file)) continue;
mlog << Debug(3)
<< "Reading observation data for "
<< conf_info.vx_opt[i].obs_info->magic_str() << ".\n";
// Set the observation lead time
shc.set_obs_lead_sec(obs_dp.lead());
// Set the observation valid time
shc.set_obs_valid_beg(obs_dp.valid());
shc.set_obs_valid_end(obs_dp.valid());
// Check that the valid times match
if(fcst_dp.valid() != obs_dp.valid()) {
mlog << Warning << "\nprocess_scores() -> "
<< "Forecast and observation valid times do not match "
<< unix_to_yyyymmdd_hhmmss(fcst_dp.valid()) << " != "
<< unix_to_yyyymmdd_hhmmss(obs_dp.valid()) << " for "
<< conf_info.vx_opt[i].fcst_info->magic_str() << " versus "
<< conf_info.vx_opt[i].obs_info->magic_str() << ".\n\n";
}
// Check that the accumulation intervals match
if(conf_info.vx_opt[i].fcst_info->level().type() == LevelType_Accum &&
conf_info.vx_opt[i].obs_info->level().type() == LevelType_Accum &&
fcst_dp.accum() != obs_dp.accum()) {
mlog << Warning << "\nprocess_scores() -> "
<< "Forecast and observation accumulation times "
<< "do not match " << sec_to_hhmmss(fcst_dp.accum())
<< " != " << sec_to_hhmmss(obs_dp.accum())
<< " for " << conf_info.vx_opt[i].fcst_info->magic_str()
<< " versus " << conf_info.vx_opt[i].obs_info->magic_str()
<< ".\n\n";
}
// Read climatology data
cmn_dp = read_climo_data_plane(
conf_info.conf.lookup_array(conf_key_climo_mean_field, false),
i, fcst_dp.valid(), grid);
csd_dp = read_climo_data_plane(
conf_info.conf.lookup_array(conf_key_climo_stdev_field, false),
i, fcst_dp.valid(), grid);
mlog << Debug(3)
<< "Found " << (cmn_dp.nx() == 0 ? 0 : 1)
<< " climatology mean and " << (csd_dp.nx() == 0 ? 0 : 1)
<< " climatology standard deviation field(s) for forecast "
<< conf_info.vx_opt[i].fcst_info->magic_str() << ".\n";
// Setup the first pass through the data
if(is_first_pass) setup_first_pass(fcst_dp);
// Store the description
shc.set_desc(conf_info.vx_opt[i].desc.c_str());
// Store the forecast variable name
shc.set_fcst_var(conf_info.vx_opt[i].fcst_info->name_attr());
// Store the forecast variable units
shc.set_fcst_units(conf_info.vx_opt[i].fcst_info->units_attr());
// Set the forecast level name
shc.set_fcst_lev(conf_info.vx_opt[i].fcst_info->level_attr().c_str());
// Store the observation variable name
shc.set_obs_var(conf_info.vx_opt[i].obs_info->name_attr());
// Store the observation variable units
shc.set_obs_units(conf_info.vx_opt[i].obs_info->units_attr());
// Set the observation level name
shc.set_obs_lev(conf_info.vx_opt[i].obs_info->level_attr().c_str());
mlog << Debug(2) << "\n" << sep_str << "\n\n";
// Pointer to current interpolation object
InterpInfo * interp = &conf_info.vx_opt[i].interp_info;
// Loop through and apply each of the smoothing operations
for(j=0; j<conf_info.vx_opt[i].get_n_interp(); j++) {
// Current interpolation method
InterpMthd interp_mthd = string_to_interpmthd(interp->method[j].c_str());
// Create grid template to find the number of points
GridTemplateFactory gtf;
GridTemplate* gt = gtf.buildGT(interp->shape, interp->width[j]);
shc.set_interp_mthd(interp_mthd, interp->shape);
int interp_pnts = gt->size();
shc.set_interp_pnts(interp_pnts);
delete gt;
// If requested in the config file, smooth the forecast field
if(interp->field == FieldType_Fcst ||
interp->field == FieldType_Both) {
smooth_field(fcst_dp, fcst_dp_smooth, interp_mthd,
interp->width[j], interp->shape, interp->vld_thresh,
interp->gaussian);
}
// Do not smooth the forecast field
else {
fcst_dp_smooth = fcst_dp;
}
// If requested in the config file, smooth the observation field
if(interp->field == FieldType_Obs ||
interp->field == FieldType_Both) {
smooth_field(obs_dp, obs_dp_smooth, interp_mthd,
interp->width[j], interp->shape, interp->vld_thresh,
interp->gaussian);
}
// Do not smooth the observation field
else {
obs_dp_smooth = obs_dp;
}
// Loop through the masks to be applied
for(k=0; k<conf_info.vx_opt[i].get_n_mask(); k++) {
// Store the current mask
mask_mp = conf_info.mask_map[conf_info.vx_opt[i].mask_name[k]];
// Turn off the mask for missing data values
mask_bad_data(mask_mp, fcst_dp_smooth);
mask_bad_data(mask_mp, obs_dp_smooth);
if(cmn_dp.nx() == fcst_dp_smooth.nx() &&
cmn_dp.ny() == fcst_dp_smooth.ny()) {
mask_bad_data(mask_mp, cmn_dp);
}
if(csd_dp.nx() == fcst_dp_smooth.nx() &&
csd_dp.ny() == fcst_dp_smooth.ny()) {
mask_bad_data(mask_mp, csd_dp);
}
// Apply the current mask to the current fields
get_mask_points(mask_mp, &fcst_dp_smooth, &obs_dp_smooth,
&cmn_dp, &csd_dp, &wgt_dp, pd);
// Set the mask name
shc.set_mask(conf_info.vx_opt[i].mask_name[k].c_str());
mlog << Debug(2)
<< "Processing " << conf_info.vx_opt[i].fcst_info->magic_str()
<< " versus " << conf_info.vx_opt[i].obs_info->magic_str()
<< ", for smoothing method "
<< shc.get_interp_mthd() << "("
<< shc.get_interp_pnts_str()
<< "), over region " << shc.get_mask()
<< ", using " << pd.f_na.n() << " matched pairs.\n";
// Continue if no pairs were found
if(pd.f_na.n() == 0) continue;
// Process percentile thresholds
conf_info.vx_opt[i].set_perc_thresh(pd);
// Compute CTS scores
if(!conf_info.vx_opt[i].fcst_info->is_prob() &&
conf_info.vx_opt[i].fcat_ta.n() > 0 &&
(conf_info.vx_opt[i].output_flag[i_fho] != STATOutputType_None ||
conf_info.vx_opt[i].output_flag[i_ctc] != STATOutputType_None ||
conf_info.vx_opt[i].output_flag[i_cts] != STATOutputType_None ||
conf_info.vx_opt[i].output_flag[i_eclv] != STATOutputType_None)) {
// Initialize
for(m=0; m<n_cat; m++) cts_info[m].clear();
// Compute CTS
do_cts(cts_info, i, &pd);
// Loop through all of the thresholds
for(m=0; m<conf_info.vx_opt[i].fcat_ta.n(); m++) {
// Write out FHO
if(conf_info.vx_opt[i].output_flag[i_fho] != STATOutputType_None &&
cts_info[m].cts.n() > 0) {
write_fho_row(shc, cts_info[m],
conf_info.vx_opt[i].output_flag[i_fho],
stat_at, i_stat_row,
txt_at[i_fho], i_txt_row[i_fho]);
}
// Write out CTC
if(conf_info.vx_opt[i].output_flag[i_ctc] != STATOutputType_None &&
cts_info[m].cts.n() > 0) {
write_ctc_row(shc, cts_info[m],
conf_info.vx_opt[i].output_flag[i_ctc],
stat_at, i_stat_row,
txt_at[i_ctc], i_txt_row[i_ctc]);
}
// Write out CTS
if(conf_info.vx_opt[i].output_flag[i_cts] != STATOutputType_None &&
cts_info[m].cts.n() > 0) {
write_cts_row(shc, cts_info[m],
conf_info.vx_opt[i].output_flag[i_cts],
stat_at, i_stat_row,
txt_at[i_cts], i_txt_row[i_cts]);
}
// Write out ECLV
if(conf_info.vx_opt[i].output_flag[i_eclv] != STATOutputType_None &&
cts_info[m].cts.n() > 0) {
write_eclv_row(shc, cts_info[m], conf_info.vx_opt[i].eclv_points,
conf_info.vx_opt[i].output_flag[i_eclv],
stat_at, i_stat_row,
txt_at[i_eclv], i_txt_row[i_eclv]);
}
} // end for m
} // end Compute CTS
// Compute MCTS scores
if(!conf_info.vx_opt[i].fcst_info->is_prob() &&
conf_info.vx_opt[i].fcat_ta.n() > 1 &&
(conf_info.vx_opt[i].output_flag[i_mctc] != STATOutputType_None ||
conf_info.vx_opt[i].output_flag[i_mcts] != STATOutputType_None)) {
// Initialize
mcts_info.clear();
// Compute MCTS
do_mcts(mcts_info, i, &pd);
// Write out MCTC
if(conf_info.vx_opt[i].output_flag[i_mctc] != STATOutputType_None &&
mcts_info.cts.total() > 0) {
write_mctc_row(shc, mcts_info,
conf_info.vx_opt[i].output_flag[i_mctc],
stat_at, i_stat_row,
txt_at[i_mctc], i_txt_row[i_mctc]);
}
// Write out MCTS
if(conf_info.vx_opt[i].output_flag[i_mcts] != STATOutputType_None &&
mcts_info.cts.total() > 0) {
write_mcts_row(shc, mcts_info,
conf_info.vx_opt[i].output_flag[i_mcts],
stat_at, i_stat_row,
txt_at[i_mcts], i_txt_row[i_mcts]);
}
} // end Compute MCTS
// Compute CNT, SL1L2, and SAL1L2 scores
if(!conf_info.vx_opt[i].fcst_info->is_prob() &&
(conf_info.vx_opt[i].output_flag[i_cnt] != STATOutputType_None ||
conf_info.vx_opt[i].output_flag[i_sl1l2] != STATOutputType_None ||
conf_info.vx_opt[i].output_flag[i_sal1l2] != STATOutputType_None)) {
do_cnt_sl1l2(conf_info.vx_opt[i], &pd);
}
// Compute VL1L2 and VAL1L2 partial sums for UGRD,VGRD
if(!conf_info.vx_opt[i].fcst_info->is_prob() &&
conf_info.vx_opt[i].fcst_info->is_v_wind() &&
conf_info.vx_opt[i].fcst_info->uv_index() >= 0 &&
(conf_info.vx_opt[i].output_flag[i_vl1l2] != STATOutputType_None ||
conf_info.vx_opt[i].output_flag[i_val1l2] != STATOutputType_None) ) {
// Store the forecast variable name
shc.set_fcst_var(ugrd_vgrd_abbr_str);
// Store the observation variable name
shc.set_obs_var(ugrd_vgrd_abbr_str);
// Initialize
for(m=0; m<n_wind; m++) vl1l2_info[m].clear();
// Index for UGRD
int ui = conf_info.vx_opt[i].fcst_info->uv_index();
// Read forecast data for UGRD
if(!read_data_plane(conf_info.vx_opt[ui].fcst_info,
fu_dp, fcst_mtddf, fcst_file)) continue;
// Read observation data for UGRD
if(!read_data_plane(conf_info.vx_opt[ui].obs_info,
ou_dp, obs_mtddf, obs_file)) continue;
// Read climatology data for UGRD
cmnu_dp = read_climo_data_plane(
conf_info.conf.lookup_array(conf_key_climo_mean_field, false),
ui, fcst_dp.valid(), grid);
csdu_dp = read_climo_data_plane(
conf_info.conf.lookup_array(conf_key_climo_stdev_field, false),
ui, fcst_dp.valid(), grid);
// If requested in the config file, smooth the forecast
// and climatology U-wind fields
if(interp->field == FieldType_Fcst ||
interp->field == FieldType_Both) {
smooth_field(fu_dp, fu_dp_smooth, interp_mthd,
interp->width[j], interp->shape, interp->vld_thresh,
interp->gaussian);
}
// Do not smooth the forecast field
else {
fu_dp_smooth = fu_dp;
}
// If requested in the config file, smooth the observation
// U-wind field
if(interp->field == FieldType_Obs ||
interp->field == FieldType_Both) {
smooth_field(ou_dp, ou_dp_smooth,
interp_mthd, interp->width[j],
interp->shape, interp->vld_thresh,
interp->gaussian);
}
// Do not smooth the observation field
else {
ou_dp_smooth = ou_dp;
}
// Apply the current mask to the U-wind fields
get_mask_points(mask_mp, &fu_dp_smooth, &ou_dp_smooth,
&cmnu_dp, &csdu_dp, &wgt_dp, pd_u);
// Compute VL1L2
do_vl1l2(vl1l2_info, i, &pd_u, &pd);
// Loop through all of the wind speed thresholds
for(m=0; m<conf_info.vx_opt[i].fwind_ta.n(); m++) {
// Write out VL1L2
if(conf_info.vx_opt[i].output_flag[i_vl1l2] != STATOutputType_None &&
vl1l2_info[m].vcount > 0) {