-
Notifications
You must be signed in to change notification settings - Fork 24
/
ensemble_stat.cc
2872 lines (2255 loc) · 94.9 KB
/
ensemble_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 - 2023
// ** 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: ensemble_stat.cc
//
// Description:
//
// Mod# Date Name Description
// ---- ---- ---- -----------
// 000 02/01/10 Halley Gotway New
// 001 09/09/11 Halley Gotway Call set_grid after reading
// gridded observation files.
// 002 10/13/11 Holmes Added use of command line class to
// parse the command line arguments.
// 003 10/18/11 Halley Gotway When the user requests verification
// in the config file, check that obs files have been
// specified on the command line.
// 004 11/14/11 Holmes Added code to enable reading of
// multiple config files.
// 005 12/09/11 Halley Gotway When gridded observations are
// missing, print a warning and continue rather than exiting
// with error status.
// 006 05/08/12 Halley Gotway Switch to using vx_config library.
// 007 05/08/12 Halley Gotway Move -ens_valid, -ens_lead,
// and -obs_lead command line options to config file.
// 008 05/18/12 Halley Gotway Modify logic to better handle
// missing files, fields, and data values.
// 009 08/30/12 Oldenburg Add spread/skill functionality
// 010 06/03/14 Halley Gotway Add PHIST line type
// 011 07/09/14 Halley Gotway Add station id exclusion option.
// 012 11/12/14 Halley Gotway Pass the obtype entry from the
// from the config file to the output files.
// 013 03/02/15 Halley Gotway Add automated regridding.
// 014 09/11/15 Halley Gotway Add climatology.
// 015 05/10/16 Halley Gotway Add grid weighting.
// 016 05/20/16 Prestopnik J Removed -version (now in command_line.cc)
// 017 08/09/16 Halley Gotway Fixed n_ens_vld vs n_vx_vld bug.
// 018 05/15/17 Prestonik P Add shape for regrid and interp.
// 019 05/15/17 Halley Gotway Add RELP line type and skip_const.
// 020 02/21/18 Halley Gotway Restructure config logic to make
// all options settable for each verification task.
// 021 03/21/18 Halley Gotway Add obs_error perturbation.
// 022 04/05/18 Halley Gotway Replace -ssvar_mean with -ens_mean.
// 023 08/15/18 Halley Gotway Add mask.llpnt type.
// 024 04/01/19 Fillmore Add FCST and OBS units.
// 025 04/15/19 Halley Gotway Add percentile thresholds.
// 026 11/26/19 Halley Gotway Add neighborhood probabilities.
// 027 12/11/19 Halley Gotway Reorganize logic to support the use
// of python embedding.
// 028 12/17/19 Halley Gotway Apply climatology bins to ensemble
// continuous statistics.
// 029 01/21/20 Halley Gotway Add RPS output line type.
// 030 02/19/21 Halley Gotway MET #1450, #1451 Overhaul CRPS
// statistics in the ECNT line type.
// 031 09/13/21 Seth Linden Changed obs_qty to obs_qty_inc.
// Added code for obs_qty_exc.
// 032 10/07/21 Halley Gotway MET #1905 Add -ctrl option.
// 033 11/15/21 Halley Gotway MET #1968 Ensemble -ctrl error check.
// 034 01/14/21 McCabe MET #1695 All members in one file.
// 035 02/15/22 Halley Gotway MET #1583 Add HiRA option.
// 036 02/20/22 Halley Gotway MET #1259 Write probabilistic statistics.
// 037 07/06/22 Howard Soh METplus-Internal #19 Rename main to met_main.
// 038 09/06/22 Halley Gotway MET #1908 Remove ensemble processing logic.
// 039 09/29/22 Halley Gotway MET #2286 Refine GRIB1 table lookup logic.
// 040 10/03/22 Prestopnik MET #2227 Remove using namespace netCDF from header files
//
////////////////////////////////////////////////////////////////////////
using namespace std;
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <dirent.h>
#include <fstream>
#include <limits.h>
#include <math.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <netcdf>
using namespace netCDF;
#include "main.h"
#include "ensemble_stat.h"
#include "vx_nc_util.h"
#include "vx_data2d_nc_met.h"
#include "vx_regrid.h"
#include "vx_log.h"
#include "nc_obs_util.h"
#include "nc_point_obs_in.h"
#include "handle_openmp.h"
#ifdef WITH_PYTHON
#include "data2d_nc_met.h"
#include "pointdata_python.h"
#endif
////////////////////////////////////////////////////////////////////////
static void process_command_line (int, char **);
static void process_grid (const Grid &);
static void process_n_vld ();
static void process_vx ();
static bool get_data_plane (const char *, GrdFileType, VarInfo *,
DataPlane &, bool do_regrid);
static bool get_data_plane_array (const char *, GrdFileType, VarInfo *,
DataPlaneArray &, bool do_regrid);
static void process_point_vx ();
static void process_point_obs (int);
static bool process_point_ens (int, int, DataPlaneArray &);
static void process_point_scores ();
static void process_grid_vx ();
static void process_grid_scores (int,
const DataPlane *, const DataPlane *,
const DataPlane &, const DataPlane &,
const DataPlane &, const DataPlane &,
const DataPlane &, const MaskPlane &,
ObsErrorEntry *, PairDataEnsemble &);
static void do_ecnt (const EnsembleStatVxOpt &,
const SingleThresh &,
const PairDataEnsemble *);
static void do_rps (const EnsembleStatVxOpt &,
const SingleThresh &,
const PairDataEnsemble *);
static void setup_nc_file (unixtime, const char *);
static void setup_txt_files ();
static void setup_table (AsciiTable &);
static void build_outfile_name(unixtime, const char *, ConcatString &);
static void write_txt_files(const EnsembleStatVxOpt &,
const PairDataEnsemble &, bool);
static void do_pct(const EnsembleStatVxOpt &, const PairDataEnsemble &);
static void do_pct_cat_thresh(const EnsembleStatVxOpt &, const PairDataEnsemble &);
static void do_pct_cdp_thresh(const EnsembleStatVxOpt &, const PairDataEnsemble &);
static void write_pct_info(const EnsembleStatVxOpt &, const PCTInfo *, int, bool);
static void write_orank_nc(PairDataEnsemble &, DataPlane &, int, int, int);
static void write_orank_var_float(int, int, int, float *, DataPlane &,
const char *, const char *);
static void write_orank_var_int(int, int, int, int *, DataPlane &,
const char *, const char *);
static void add_var_att_local(VarInfo *, NcVar *, bool is_int,
const DataPlane &, const char *, const char *);
static void finish_txt_files();
static void clean_up();
static void usage();
static void set_grid_obs(const StringArray &);
static void set_point_obs(const StringArray &);
static void set_ens_mean(const StringArray & a);
static void set_ctrl_file(const StringArray &);
static void set_obs_valid_beg(const StringArray &);
static void set_obs_valid_end(const StringArray &);
static void set_outdir(const StringArray &);
static void set_compress(const StringArray &);
////////////////////////////////////////////////////////////////////////
int met_main(int argc, char *argv[]) {
// Set up OpenMP (if enabled)
init_openmp();
// Process the command line arguments
process_command_line(argc, argv);
// Check for valid ensemble data
process_n_vld();
// Perform verification
process_vx();
// Close the text files and deallocate memory
clean_up();
return(0);
}
////////////////////////////////////////////////////////////////////////
const string get_tool_name()
{
return "ensemble_stat";
}
////////////////////////////////////////////////////////////////////////
void process_command_line(int argc, char **argv) {
int i;
CommandLine cline;
ConcatString default_config_file;
Met2dDataFile *ens_mtddf = (Met2dDataFile *) 0;
Met2dDataFile *obs_mtddf = (Met2dDataFile *) 0;
// Set default output directory
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 option function calls
// quietly support deprecated -ssvar_mean option
//
cline.add(set_grid_obs, "-grid_obs", 1);
cline.add(set_point_obs, "-point_obs", 1);
cline.add(set_ens_mean, "-ens_mean", 1);
cline.add(set_ctrl_file, "-ctrl", 1);
cline.add(set_obs_valid_beg, "-obs_valid_beg", 1);
cline.add(set_obs_valid_end, "-obs_valid_end", 1);
cline.add(set_outdir, "-outdir", 1);
cline.add(set_compress, "-compress", 1);
cline.add(set_ens_mean, "-ssvar_mean", 1);
//
// parse the command line
//
cline.parse();
//
// Check for error. There should be either a number followed by that
// number of filenames or a single filename and a config filename.
//
if(cline.n() < 2) usage();
if(cline.n() == 2) {
//
// It should be a filename and then a config filename
//
if(is_integer(cline[0].c_str()) == 0) {
ens_file_list = parse_ascii_file_list(cline[0].c_str());
n_ens_files = ens_file_list.n();
}
else {
usage();
}
}
else {
//
// More than two arguments. Check that the first is an integer
// followed by that number of filenames and a config filename.
//
if(is_integer(cline[0].c_str()) == 1) {
n_ens_files = atoi(cline[0].c_str());
if(n_ens_files <= 0) {
mlog << Error << "\nprocess_command_line() -> "
<< "the number of ensemble member files must be >= 1 ("
<< n_ens_files << ")\n\n";
exit(1);
}
if((cline.n() - 2) == n_ens_files) {
//
// Add each of the ensemble members to the list of files.
//
for(i=1; i<=n_ens_files; i++) ens_file_list.add(cline[i]);
}
else {
usage();
}
}
}
// Check for at least one valid input ensemble file
if(n_ens_files == 0) {
mlog << Error << "\nprocess_command_line() -> "
<< "no valid input ensemble member files specified!\n\n";
exit(1);
}
// Append the control member, if specified
if(ctrl_file.nonempty()) {
if(n_ens_files == 1 && !ens_file_list.has(ctrl_file)) {
mlog << Error << "\nprocess_command_line() -> "
<< "when reading all ensemble members from the same file, "
<< "the control member must be in that file as well: "
<< ctrl_file << "\n\n";
exit(1);
}
else if(n_ens_files > 1 && ens_file_list.has(ctrl_file)) {
mlog << Error << "\nprocess_command_line() -> "
<< "the ensemble control file should not appear in the list "
<< "of ensemble member files: " << ctrl_file << "\n\n";
exit(1);
}
// Store control member file information
if(n_ens_files == 1) {
ctrl_file_index = 0;
}
else {
ens_file_list.add(ctrl_file.c_str());
n_ens_files++;
ctrl_file_index = ens_file_list.n()-1;
}
}
// Check that the end_ut >= beg_ut
if(obs_valid_beg_ut != (unixtime) 0 &&
obs_valid_end_ut != (unixtime) 0 &&
obs_valid_beg_ut > obs_valid_end_ut) {
mlog << Error << "\nprocess_command_line() -> "
<< "the ending time ("
<< unix_to_yyyymmdd_hhmmss(obs_valid_end_ut)
<< ") must be greater than the beginning time ("
<< unix_to_yyyymmdd_hhmmss(obs_valid_beg_ut)
<< ").\n\n";
exit(1);
}
// Store the config file name
config_file = cline[cline.n() - 1];
// 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, config_file);
// Get the ensemble file type from config, if present
etype = parse_conf_file_type(conf_info.conf.lookup_dictionary(conf_key_fcst));
// Read the first input ensemble file
if(!(ens_mtddf = mtddf_factory.new_met_2d_data_file(ens_file_list[0].c_str(), etype))) {
mlog << Error << "\nprocess_command_line() -> "
<< "trouble reading ensemble file \""
<< ens_file_list[0] << "\"\n\n";
exit(1);
}
// Store the input ensemble file type
etype = ens_mtddf->file_type();
// Observation files are required
if(!grid_obs_flag && !point_obs_flag) {
mlog << Error << "\nprocess_command_line() -> "
<< "the \"-grid_obs\" or \"-point_obs\" command line option "
<< "must be used at least once.\n\n";
exit(1);
}
// Determine the input observation file type
if(point_obs_flag) {
otype = FileType_Gb1;
}
else if(!grid_obs_flag) {
otype = FileType_None;
}
else {
// Get the observation file type from config, if present
otype = parse_conf_file_type(conf_info.conf.lookup_dictionary(conf_key_obs));
// Read the first gridded observation file
if(!(obs_mtddf = mtddf_factory.new_met_2d_data_file(grid_obs_file_list[0].c_str(), otype))) {
mlog << Error << "\nprocess_command_line() -> "
<< "trouble reading gridded observation file \""
<< grid_obs_file_list[0] << "\"\n\n";
exit(1);
}
// Store the gridded observation file type
otype = obs_mtddf->file_type();
}
// Process the configuration
conf_info.process_config(etype, otype, grid_obs_flag, point_obs_flag,
&ens_file_list, ctrl_file.nonempty());
// Set output_nc_flag
out_nc_flag = (grid_obs_flag && !conf_info.nc_info.all_false());
// Set the model name
shc.set_model(conf_info.model.c_str());
// List the input ensemble files
mlog << Debug(1) << "Ensemble Files["
<< n_ens_files << "]:\n";
for(i=0; i<n_ens_files; i++) {
mlog << " " << ens_file_list[i] << "\n";
}
// List the control member file
mlog << Debug(1) << "Ensemble Control: "
<< (ctrl_file.empty() ? "None" : ctrl_file.c_str()) << "\n";
// List the input gridded observations files
if(grid_obs_file_list.n() > 0) {
mlog << Debug(1) << "Gridded Observation Files["
<< grid_obs_file_list.n() << "]:\n" ;
for(i=0; i<grid_obs_file_list.n(); i++) {
mlog << " " << grid_obs_file_list[i] << "\n" ;
}
}
// List the input point observations files
if(point_obs_file_list.n() > 0) {
mlog << Debug(1) << "Point Observation Files["
<< point_obs_file_list.n() << "]:\n" ;
for(i=0; i<point_obs_file_list.n(); i++) {
mlog << " " << point_obs_file_list[i] << "\n" ;
}
}
// Check for missing non-python ensemble files
for(i=0; i<n_ens_files; i++) {
if(!file_exists(ens_file_list[i].c_str()) &&
!is_python_grdfiletype(etype)) {
mlog << Warning << "\nprocess_command_line() -> "
<< "can't open input ensemble file: "
<< ens_file_list[i] << "\n\n";
ens_file_vld.add(0);
}
else {
ens_file_vld.add(1);
}
}
// User-specified ensemble mean file
if(ens_mean_file.nonempty()) {
if(!file_exists(ens_mean_file.c_str())) {
mlog << Warning << "\nprocess_command_line() -> "
<< "can't open input ensemble mean file: "
<< ens_mean_file << "\n\n";
ens_mean_file = "";
}
}
// User-specified ensemble control file
if(conf_info.control_id.nonempty() && ctrl_file.empty()) {
mlog << Warning << "\nprocess_command_line() -> "
<< "control_id is set in the config file but "
<< "control file is not provided with -ctrl argument\n\n";
}
// Deallocate memory for data files
if(ens_mtddf) { delete ens_mtddf; ens_mtddf = (Met2dDataFile *) 0; }
if(obs_mtddf) { delete obs_mtddf; obs_mtddf = (Met2dDataFile *) 0; }
return;
}
////////////////////////////////////////////////////////////////////////
void process_grid(const Grid &fcst_grid) {
Grid obs_grid;
// Parse regridding logic
RegridInfo ri;
ri = conf_info.vx_opt[0].vx_pd.fcst_info->get_var_info()->regrid();
// Read gridded observation data, if necessary
if(ri.field == FieldType_Obs) {
if(!grid_obs_flag) {
mlog << Error << "\nprocess_grid() -> "
<< "attempting to regrid to the observation grid, but no "
<< "gridded observations provided!\n\n";
exit(1);
}
DataPlane dp;
Met2dDataFile *mtddf = (Met2dDataFile *) 0;
if(!(mtddf = mtddf_factory.new_met_2d_data_file(
grid_obs_file_list[0].c_str(), otype))) {
mlog << Error << "\nprocess_grid() -> "
<< "trouble reading file \"" << grid_obs_file_list[0]
<< "\"\n\n";
exit(1);
}
if(!mtddf->data_plane(*(conf_info.vx_opt[0].vx_pd.obs_info), dp)) {
mlog << Error << "\nprocess_grid() -> "
<< "observation field \""
<< conf_info.vx_opt[0].vx_pd.obs_info->magic_str()
<< "\" not found in file \"" << grid_obs_file_list[0]
<< "\"\n\n";
exit(1);
}
// Store the observation grid
obs_grid = mtddf->grid();
if(mtddf) { delete mtddf; mtddf = (Met2dDataFile *) 0; }
}
else {
obs_grid = fcst_grid;
}
// Determine the verification grid
grid = parse_vx_grid(ri, &fcst_grid, &obs_grid);
nxy = grid.nx() * grid.ny();
// Compute weight for each grid point
parse_grid_weight(grid, conf_info.grid_weight_flag, wgt_dp);
return;
}
////////////////////////////////////////////////////////////////////////
void process_n_vld() {
int i_var, i_ens, j, n_vld, n_ens_inputs;
DataPlane dp;
DataPlaneArray dpa;
VarInfo * var_info;
ConcatString fcst_file;
vector<EnsVarInfo*>::const_iterator var_it;
// Initialize
n_vx_vld.clear();
// Loop through the verification fields to be processed
for(i_var=0; i_var<conf_info.get_n_vx(); i_var++) {
n_ens_inputs = conf_info.vx_opt[i_var].vx_pd.fcst_info->inputs_n();
// Loop through the forecast inputs
for(i_ens=n_vld=0; i_ens<n_ens_inputs; i_ens++) {
// Get forecast file and VarInfo to process
fcst_file = conf_info.vx_opt[i_var].vx_pd.fcst_info->get_file(i_ens);
var_info = conf_info.vx_opt[i_var].vx_pd.fcst_info->get_var_info(i_ens);
j = conf_info.vx_opt[i_var].vx_pd.fcst_info->get_file_index(i_ens);
// Check for valid file
if(!ens_file_vld[j]) continue;
// Check for valid data fields.
// Call data_plane_array to handle multiple levels.
if(!get_data_plane_array(fcst_file.c_str(), etype,
var_info,
dpa, false)) {
mlog << Warning << "\nprocess_n_vld() -> "
<< "no data found for forecast field \""
<< var_info->magic_str()
<< "\" in file \"" << fcst_file
<< "\"\n\n";
}
else {
// Store the lead times for the first verification field
if(i_var==0) ens_lead_na.add(dpa[0].lead());
// Increment the valid counter
n_vld++;
}
} // end for j
// Check for enough valid data
if((double) n_vld/n_ens_inputs < conf_info.vld_ens_thresh) {
mlog << Error << "\nprocess_n_vld() -> "
<< n_vld << " of " << n_ens_inputs
<< " (" << (double) n_vld/n_ens_inputs << ")"
<< " forecast fields found for \""
<< conf_info.vx_opt[i_var].vx_pd.fcst_info->get_var_info()->magic_str()
<< "\" does not meet the threshold specified by \""
<< conf_key_fcst_ens_thresh << "\" (" << conf_info.vld_ens_thresh
<< ") in the configuration file.\n\n";
exit(1);
}
// Store the valid data count
n_vx_vld.add(n_vld);
} // end for i
return;
}
////////////////////////////////////////////////////////////////////////
bool get_data_plane(const char *infile, GrdFileType ftype,
VarInfo *info, DataPlane &dp, bool do_regrid) {
bool found;
Met2dDataFile *mtddf = (Met2dDataFile *) 0;
// Read the current ensemble file
if(!(mtddf = mtddf_factory.new_met_2d_data_file(infile, ftype))) {
mlog << Error << "\nget_data_plane() -> "
<< "trouble reading file \"" << infile << "\"\n\n";
exit(1);
}
// Read the gridded data field
if((found = mtddf->data_plane(*info, dp))) {
// Setup the verification grid, if necessary
if(nxy == 0) process_grid(mtddf->grid());
// Regrid, if requested and necessary
if(do_regrid && !(mtddf->grid() == grid)) {
mlog << Debug(1)
<< "Regridding field \"" << info->magic_str()
<< "\" to the verification grid.\n";
dp = met_regrid(dp, mtddf->grid(), grid, info->regrid());
}
// Store the valid time, if not already set
if(ens_valid_ut == (unixtime) 0) {
ens_valid_ut = dp.valid();
}
// Check to make sure that the valid time doesn't change
else if(ens_valid_ut != dp.valid()) {
mlog << Warning << "\nget_data_plane() -> "
<< "The valid time has changed, "
<< unix_to_yyyymmdd_hhmmss(ens_valid_ut)
<< " != " << unix_to_yyyymmdd_hhmmss(dp.valid())
<< " in \"" << infile << "\"\n\n";
}
} // end if found
// Deallocate the data file pointer, if necessary
if(mtddf) { delete mtddf; mtddf = (Met2dDataFile *) 0; }
return(found);
}
////////////////////////////////////////////////////////////////////////
bool get_data_plane_array(const char *infile, GrdFileType ftype,
VarInfo *info, DataPlaneArray &dpa,
bool do_regrid) {
int n, i;
bool found;
Met2dDataFile *mtddf = (Met2dDataFile *) 0;
// Read the current ensemble file
if(!(mtddf = mtddf_factory.new_met_2d_data_file(infile, ftype))) {
mlog << Error << "\nget_data_plane_array() -> "
<< "trouble reading file \"" << infile << "\"\n\n";
exit(1);
}
// Read the gridded data field
n = mtddf->data_plane_array(*info, dpa);
// Check for at least one field
if((found = (n > 0))) {
// Setup the verification grid, if necessary
if(nxy == 0) process_grid(mtddf->grid());
// Regrid, if requested and necessary
if(do_regrid && !(mtddf->grid() == grid)) {
mlog << Debug(1)
<< "Regridding " << dpa.n_planes()
<< " field(s) \"" << info->magic_str()
<< "\" to the verification grid.\n";
// Loop through the forecast fields
for(i=0; i<dpa.n_planes(); i++) {
dpa[i] = met_regrid(dpa[i], mtddf->grid(), grid,
info->regrid());
}
}
// Store the valid time, if not already set
if(ens_valid_ut == (unixtime) 0) {
ens_valid_ut = dpa[0].valid();
}
// Check to make sure that the valid time doesn't change
else if(ens_valid_ut != dpa[0].valid()) {
mlog << Warning << "\nget_data_plane_array() -> "
<< "The valid time has changed, "
<< unix_to_yyyymmdd_hhmmss(ens_valid_ut)
<< " != " << unix_to_yyyymmdd_hhmmss(dpa[0].valid())
<< " in \"" << infile << "\"\n\n";
}
} // end if found
// Deallocate the data file pointer, if necessary
if(mtddf) { delete mtddf; mtddf = (Met2dDataFile *) 0; }
return(found);
}
////////////////////////////////////////////////////////////////////////
void process_vx() {
// Process masks Grids and Polylines in the config file
conf_info.process_masks(grid);
// Determine the index of the control member in list of data values
int ctrl_data_index = (is_bad_data(ctrl_file_index) ?
bad_data_int : ens_file_vld.sum()-1);
// Setup the PairDataEnsemble objects
conf_info.set_vx_pd(n_vx_vld, ctrl_data_index);
// Process the point observations
if(point_obs_flag) process_point_vx();
// Process the gridded observations
if(grid_obs_flag) process_grid_vx();
return;
}
////////////////////////////////////////////////////////////////////////
void process_point_vx() {
int i, j, i_file, n_miss;
unixtime beg_ut, end_ut;
DataPlaneArray fcst_dpa, emn_dpa;
DataPlaneArray cmn_dpa, csd_dpa;
// Loop through each of the fields to be verified
for(i=0; i<conf_info.get_n_vx(); i++) {
// Set the observation time window with obs_valid_beg_ut
// and obs_valid_end_ut, if set on the command line.
if(obs_valid_beg_ut != (unixtime) 0 ||
obs_valid_end_ut != (unixtime) 0) {
beg_ut = obs_valid_beg_ut;
end_ut = obs_valid_end_ut;
}
// Otherwise, set the observation time window with beg_ds and end_ds.
else {
beg_ut = ens_valid_ut + conf_info.vx_opt[i].beg_ds;
end_ut = ens_valid_ut + conf_info.vx_opt[i].end_ds;
}
// Store the timing info
conf_info.vx_opt[i].vx_pd.set_fcst_ut(ens_valid_ut);
conf_info.vx_opt[i].vx_pd.set_beg_ut(beg_ut);
conf_info.vx_opt[i].vx_pd.set_end_ut(end_ut);
// Read climatology data
cmn_dpa = read_climo_data_plane_array(
conf_info.conf.lookup_array(conf_key_climo_mean_field, false),
i, ens_valid_ut, grid);
csd_dpa = read_climo_data_plane_array(
conf_info.conf.lookup_array(conf_key_climo_stdev_field, false),
i, ens_valid_ut, grid);
mlog << Debug(3)
<< "Found " << cmn_dpa.n_planes()
<< " climatology mean field(s) and " << csd_dpa.n_planes()
<< " climatology standard deviation field(s) for forecast "
<< conf_info.vx_opt[i].vx_pd.fcst_info->get_var_info()->magic_str() << ".\n";
// Store climatology information
conf_info.vx_opt[i].vx_pd.set_climo_mn_dpa(cmn_dpa);
conf_info.vx_opt[i].vx_pd.set_climo_sd_dpa(csd_dpa);
}
// Process each point observation NetCDF file
for(i=0; i<point_obs_file_list.n(); i++) process_point_obs(i);
// Calculate and print observation summaries
for(i=0; i<conf_info.get_n_vx(); i++) {
conf_info.vx_opt[i].vx_pd.calc_obs_summary();
conf_info.vx_opt[i].vx_pd.print_obs_summary();
}
// Loop through each of the fields to be verified
for(i=0; i<conf_info.get_n_vx(); i++) {
// Initialize
emn_dpa.clear();
// Loop through the ensemble inputs
for(j=0, n_miss=0; j<conf_info.vx_opt[i].vx_pd.fcst_info->inputs_n(); j++) {
i_file = conf_info.vx_opt[i].vx_pd.fcst_info->get_file_index(j);
// If the current forecast file is valid, process it
if(!ens_file_vld[i_file]) {
n_miss++;
continue;
}
else if(!process_point_ens(i, j, fcst_dpa)) {
n_miss++;
continue;
}
// Store ensemble member data
conf_info.vx_opt[i].vx_pd.set_fcst_dpa(fcst_dpa);
// Compute ensemble values for this member
conf_info.vx_opt[i].vx_pd.add_ens(j-n_miss, false, grid);
// Running sum for the ensemble mean
if(emn_dpa.n_planes() == 0) emn_dpa = fcst_dpa;
else emn_dpa += fcst_dpa;
} // end for j
// Read the ensemble mean from a file
if(ens_mean_file.nonempty()) {
mlog << Debug(2) << "Processing ensemble mean file: "
<< ens_mean_file << "\n";
VarInfo *info = conf_info.vx_opt[i].vx_pd.fcst_info->get_var_info();
// Read the gridded data from the ensemble mean file
if(!get_data_plane_array(ens_mean_file.c_str(), info->file_type(), info,
emn_dpa, true)) {
mlog << Error << "\nprocess_point_vx() -> "
<< "trouble reading the ensemble mean field \""
<< info->magic_str() << "\" from file \""
<< ens_mean_file << "\"\n\n";
exit(1);
}
// Dump out the number of levels found
mlog << Debug(2) << "For " << info->magic_str()
<< " found " << emn_dpa.n_planes() << " forecast levels.\n";
}
// Otherwise, compute the ensemble mean from the members
else {
mlog << Debug(2) << "Computing the ensemble mean from the members.\n";
int n = conf_info.vx_opt[i].vx_pd.fcst_info->inputs_n() - n_miss;
if(n <= 0) {
mlog << Error << "\nprocess_point_vx() -> "
<< "all inputs are missing!\n\n";
exit(1);
}
// Convert running sums to a mean
emn_dpa /= (double) n;
}
// Store ensemble mean data
conf_info.vx_opt[i].vx_pd.set_fcst_dpa(emn_dpa);
// Compute ensemble mean values
conf_info.vx_opt[i].vx_pd.add_ens(-1, true, grid);
} // end for i
// Compute the scores and write them out
process_point_scores();
return;
}
////////////////////////////////////////////////////////////////////////
void process_point_obs(int i_nc) {
int i_obs, j;
unixtime hdr_ut;
NcFile *obs_in = (NcFile *) 0;
const char *method_name = "process_point_obs() -> ";
mlog << Debug(2) << "\n" << sep_str << "\n\n"
<< "Processing point observation file: "
<< point_obs_file_list[i_nc] << "\n";
// Open the observation file as a NetCDF file.
bool status;
bool use_var_id = true;
bool use_arr_vars = false;
bool use_python = false;
MetNcPointObsIn nc_point_obs;
MetPointData *met_point_obs = 0;
// Check for python format
string python_command = point_obs_file_list[i_nc];
bool use_xarray = (0 == python_command.find(conf_val_python_xarray));
use_python = use_xarray || (0 == python_command.find(conf_val_python_numpy));
#ifdef WITH_PYTHON
MetPythonPointDataFile met_point_file;
if (use_python) {
int offset = python_command.find("=");
if (offset == std::string::npos) {
mlog << Error << "\n" << method_name
<< "trouble parsing the python command " << python_command << ".\n\n";
exit(1);
}
if(!met_point_file.open(python_command.substr(offset+1).c_str(), use_xarray)) {
met_point_file.close();
mlog << Error << "\n" << method_name
<< "trouble getting point observation file from python command "
<< python_command << ".\n\n";
exit(1);
}
met_point_obs = met_point_file.get_met_point_data();
use_var_id = met_point_file.is_using_var_id();
}
else {
#else
if (use_python) python_compile_error(method_name);
#endif
if(!nc_point_obs.open(point_obs_file_list[i_nc].c_str())) {
nc_point_obs.close();
mlog << Warning << "\n" << method_name
<< "can't open observation netCDF file: "
<< point_obs_file_list[i_nc] << "\n\n";
return;
}
// Read the dimensions and variables
nc_point_obs.read_dim_headers();
nc_point_obs.check_nc(point_obs_file_list[i_nc].c_str(), method_name); // exit if missing dims/vars
nc_point_obs.read_obs_data_table_lookups();
met_point_obs = (MetPointData *)&nc_point_obs;
use_var_id = nc_point_obs.is_using_var_id();
use_arr_vars = nc_point_obs.is_using_obs_arr();
#ifdef WITH_PYTHON
}
#endif
// Perform GRIB table lookups, if needed
if(!use_var_id) conf_info.process_grib_codes();
int hdr_count = met_point_obs->get_hdr_cnt();
int obs_count = met_point_obs->get_obs_cnt();
mlog << Debug(2) << "Searching " << (obs_count)
<< " observations from " << (hdr_count)
<< " header messages.\n";
const int buf_size = ((obs_count > DEF_NC_BUFFER_SIZE) ? DEF_NC_BUFFER_SIZE : (obs_count));
int obs_qty_idx_block[buf_size];
float obs_arr_block[buf_size][OBS_ARRAY_LEN];
float obs_arr[OBS_ARRAY_LEN], hdr_arr[HDR_ARRAY_LEN];
int hdr_typ_arr[HDR_TYPE_ARR_LEN];
ConcatString hdr_typ_str;
ConcatString hdr_sid_str;
ConcatString hdr_vld_str;
ConcatString obs_qty_str;
ConcatString var_name;
StringArray var_names;
StringArray obs_qty_array = met_point_obs->get_qty_data();
if(use_var_id) var_names = met_point_obs->get_var_names();
for(int i_start=0; i_start<obs_count; i_start+=buf_size) {
int buf_size2 = (obs_count-i_start);
if (buf_size2 > buf_size) buf_size2 = buf_size;
#ifdef WITH_PYTHON
if (use_python)
status = met_point_obs->get_point_obs_data()->fill_obs_buf(
buf_size2, i_start, (float *)obs_arr_block, obs_qty_idx_block);
else
#endif
status = nc_point_obs.read_obs_data(buf_size2, i_start, (float *)obs_arr_block,
obs_qty_idx_block, (char *)0);
if (!status) exit(1);
// Process each observation in the file