-
Notifications
You must be signed in to change notification settings - Fork 24
/
point2grid.cc
3049 lines (2611 loc) · 112 KB
/
point2grid.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
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
///////////////////////////////////////////////////////////////////////
//
//
// Description:
// This tool reads the point observation fields from a GOES16/17 or
// MET generated point NetCDF (by ascii2nc, madis2nc, pb2nc, etc),
// regrids them to the user-specified output grid, and writes the
// regridded output data in NetCDF format.
//
// Mod# Date Name Description
// ---- ---- ---- -----------
// 000 12-11-19 Howard Soh Support GOES-16
// 001 01-25-21 Halley Gotway MET #1630 Handle zero obs.
// 002 07-06-22 Howard Soh METplus-Internal #19 Rename main to met_main
// 003 10-03-23 Prestopnik MET #2227 Remove namespace std and netCDF from header files
//
////////////////////////////////////////////////////////////////////////
using namespace std;
#include <cstdio>
#include <cstdlib>
#include <dirent.h>
#include<netcdf>
using namespace netCDF;
#include "main.h"
#include "vx_log.h"
#include "vx_data2d_factory.h"
#include "vx_data2d.h"
#include "vx_nc_util.h"
#include "vx_grid.h"
#include "vx_regrid.h"
#include "vx_util.h"
#include "vx_statistics.h"
#include "nc_obs_util.h"
#include "nc_point_obs_in.h"
#include "point2grid_conf_info.h"
#ifdef WITH_PYTHON
#include "data2d_nc_met.h"
#include "pointdata_python.h"
#endif
////////////////////////////////////////////////////////////////////////
static ConcatString program_name;
// Constants
static const int TYPE_UNKNOWN = 0; // Can not process the input file
static const int TYPE_OBS = 1; // MET Point Obs NetCDF (from xxx2nc)
static const int TYPE_NCCF = 2; // CF NetCDF with time and lat/lon variables
static const int TYPE_GOES = 5;
static const int TYPE_GOES_ADP = 6;
static const int TYPE_PYTHON = 7; // MET Point Obs NetCDF from PYTHON
static const InterpMthd DefaultInterpMthd = InterpMthd_UW_Mean;
static const int DefaultInterpWdth = 2;
static const double DefaultVldThresh = 0.5;
static const float MISSING_LATLON = -999.0;
static const int QC_NA_INDEX = -1;
static const int LEVEL_FOR_PERFORMANCE = 6;
static const char * default_config_filename = "MET_BASE/config/Point2GridConfig_default";
static const string lat_dim_name_list = "x"; // "lat,latitude";
static const string lon_dim_name_list = "y"; // "lon,longitude";
static const char * GOES_global_attr_names[] = {
"naming_authority",
"project",
"production_site",
"production_environment",
"spatial_resolution",
"orbital_slot",
"platform_ID",
"instrument_type",
"scene_id",
"instrument_ID",
"dataset_name",
"iso_series_metadata_id",
"title",
"keywords",
"keywords_vocabulary",
"processing_level",
"date_created",
"cdm_data_type",
"time_coverage_start",
"time_coverage_end",
"timeline_id",
"id"
};
// Beginning and ending retention times
static IntArray message_type_list;
// Variables for command line arguments
static ConcatString InputFilename;
static ConcatString OutputFilename;
static ConcatString AdpFilename;
static ConcatString config_filename;
static PointToGridConfInfo conf_info;
static StringArray FieldSA;
static RegridInfo RGInfo;
static StringArray VarNameSA;
static int compress_level = -1;
static bool opt_override_method = false;
static bool do_gaussian_filter = false;
static SingleThresh prob_cat_thresh;
// Output NetCDF file
static NcFile *nc_out = (NcFile *) 0;
static NcDim lat_dim ;
static NcDim lon_dim ;
static int adp_qc_high; /* 3 as baseline algorithm, 0 for enterpirse algorithm */
static int adp_qc_medium; /* 1 as baseline algorithm, 1 for enterpirse algorithm */
static int adp_qc_low; /* 0 as baseline algorithm, 2 for enterpirse algorithm */
////////////////////////////////////////////////////////////////////////
static void process_command_line(int, char **);
static void process_data_file();
static void process_point_file(NcFile *nc_in, MetConfig &config,
VarInfo *, const Grid to_grid);
#ifdef WITH_PYTHON
static void process_point_python(string python_command, MetConfig &config,
VarInfo *vinfo, const Grid to_grid, bool use_xarray);
#endif
static void process_point_nccf_file(NcFile *nc_in, MetConfig &config,
VarInfo *, Met2dDataFile *fr_mtddf,
const Grid to_grid);
static void open_nc(const Grid &grid, const ConcatString run_cs);
static void write_nc(const DataPlane &dp, const Grid &grid,
const VarInfo *vinfo, const char *vname);
static void write_nc_int(const DataPlane &dp, const Grid &grid,
const VarInfo *vinfo, const char *vname);
static void close_nc();
static void usage();
static void set_field(const StringArray &);
static void set_method(const StringArray &);
static void set_prob_cat_thresh(const StringArray &);
static void set_vld_thresh(const StringArray &);
static void set_name(const StringArray &);
static void set_config(const StringArray &);
static void set_compress(const StringArray &);
static void set_adp(const StringArray &);
static void set_gaussian_dx(const StringArray &);
static void set_gaussian_radius(const StringArray &);
static unixtime compute_unixtime(NcVar *time_var, unixtime var_value);
static bool get_grid_mapping(Grid fr_grid, Grid to_grid, IntArray *cellMapping,
NcVar var_lat, NcVar var_lon, bool *skip_times);
static bool get_grid_mapping(Grid to_grid, IntArray *cellMapping,
const IntArray obs_index_array, const int *obs_hids,
const float *hdr_lats, const float *hdr_lons);
static int get_obs_type(NcFile *nc_in);
static void regrid_nc_variable(NcFile *nc_in, Met2dDataFile *fr_mtddf,
VarInfo *vinfo, DataPlane &fr_dp, DataPlane &to_dp,
Grid to_grid, IntArray *cellMapping);
static bool keep_message_type(const int mt_index);
static bool has_lat_lon_vars(NcFile *nc_in);
static void set_adp_gc_values(NcVar var_adp_qc);
////////////////////////////////////////////////////////////////////////
// for GOES 16
//
static const int factor_float_to_int = 1000000;
static const char *key_geostationary_data = "MET_GEOSTATIONARY_DATA";
static const char *dim_name_lat = "lat";
static const char *dim_name_lon = "lon";
static const char *var_name_lat = "latitude";
static const char *var_name_lon = "longitude";
static const ConcatString vname_dust("Dust");
static const ConcatString vname_smoke("Smoke");
static IntArray qc_flags;
static void process_goes_file(NcFile *nc_in, MetConfig &config,
VarInfo *, const Grid fr_grid, const Grid to_grid);
static unixtime find_valid_time(NcVar time_var);
static ConcatString get_goes_grid_input(MetConfig config, Grid fr_grid, Grid to_grid);
static void get_grid_mapping(Grid fr_grid, Grid to_grid,
IntArray *cellMapping, ConcatString geostationary_file);
static int get_lat_count(NcFile *);
static int get_lon_count(NcFile *);
static NcVar get_goes_nc_var(NcFile *nc, const ConcatString var_name,
bool exit_if_error=true);
static bool is_time_mismatch(NcFile *nc_in, NcFile *nc_adp);
static ConcatString make_geostationary_filename(Grid fr_grid, Grid to_grid,
ConcatString regrid_name);
static void regrid_goes_variable(NcFile *nc_in, VarInfo *vinfo,
DataPlane &fr_dp, DataPlane &to_dp,
Grid fr_grid, Grid to_grid, IntArray *cellMapping, NcFile *nc_adp);
static void save_geostationary_data(const ConcatString geostationary_file,
const float *latitudes, const float *longitudes,
const GoesImagerData grid_data);
static void set_qc_flags(const StringArray &);
////////////////////////////////////////////////////////////////////////
int met_main(int argc, char *argv[]) {
// Store the program name
program_name = get_short_name(argv[0]);
// Process the command line arguments
process_command_line(argc, argv);
// Process the input data file
process_data_file();
return(0);
}
////////////////////////////////////////////////////////////////////////
const string get_tool_name() {
return "point2grid";
}
////////////////////////////////////////////////////////////////////////
void process_command_line(int argc, char **argv) {
CommandLine cline;
static const char *method_name = "process_command_line() -> ";
// Set default regridding options
RGInfo.enable = true;
RGInfo.field = FieldType_None;
RGInfo.method = DefaultInterpMthd;
RGInfo.width = DefaultInterpWdth;
RGInfo.vld_thresh = DefaultVldThresh;
RGInfo.gaussian.dx = default_gaussian_dx;
RGInfo.gaussian.radius = default_gaussian_radius;
RGInfo.gaussian.trunc_factor = default_trunc_factor;
// 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_field, "-field", 1);
cline.add(set_method, "-method", 1);
cline.add(set_vld_thresh, "-vld_thresh", 1);
cline.add(set_name, "-name", 1);
cline.add(set_compress, "-compress", 1);
cline.add(set_qc_flags, "-qc", 1);
cline.add(set_adp, "-adp", 1);
cline.add(set_config, "-config", 1);
cline.add(set_prob_cat_thresh, "-prob_cat_thresh", 1);
cline.add(set_gaussian_radius, "-gaussian_radius", 1);
cline.add(set_gaussian_dx, "-gaussian_dx", 1);
cline.allow_numbers();
// Parse the command line
cline.parse();
// Check for error. There should be three arguments left:
// - input filename
// - destination grid,
// - output filename
if(cline.n() != 3) usage();
// Store the filenames and config string.
InputFilename = cline[0];
RGInfo.name = cline[1];
OutputFilename = cline[2];
// Check for python format
string python_command = InputFilename;
bool use_xarray = (0 == python_command.find(conf_val_python_xarray));
bool use_python = use_xarray || (0 == python_command.find(conf_val_python_numpy));
// Check if the input file
#ifdef WITH_PYTHON
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);
}
}
else
#else
if (use_python) python_compile_error(method_name);
#endif
if ( !file_exists(InputFilename.c_str()) ) {
mlog << Error << "\n" << method_name
<< "file does not exist \"" << InputFilename << "\"\n\n";
exit(1);
}
// Check for at least one configuration string
if(FieldSA.n() < 1) {
mlog << Error << "\nprocess_command_line() -> "
<< "The -field option must be used at least once!\n\n";
usage();
}
// Check that same variable is required multiple times without -name argument
if(VarNameSA.n() == 0) {
VarInfo *vinfo;
MetConfig config;
VarInfoFactory v_factory;
ConcatString vname;
StringArray var_names;
vinfo = v_factory.new_var_info(FileType_NcMet);
for(int i=0; i<FieldSA.n(); i++) {
vinfo->clear();
// Populate the VarInfo object using the config string
config.read_string(FieldSA[i].c_str());
vinfo->set_dict(config);
vname = vinfo->name();
if (var_names.has(vname)) {
mlog << Error << "\n" << method_name
<< "Please add -name argument to avoid the output name "
<< "conflicts on handling the variable \""
<< vname << "\" multiple times.\n\n";
usage();
}
else var_names.add(vname);
}
// Clean up
if(vinfo) { delete vinfo; vinfo = (VarInfo *) 0; }
}
// Check that the number of output names and fields match
else if(VarNameSA.n() != FieldSA.n()) {
mlog << Error << "\n" << method_name
<< "When the -name option is used, the number of entries ("
<< VarNameSA.n() << ") must match the number of "
<< "-field entries (" << FieldSA.n() << ")!\n\n";
usage();
}
RGInfo.validate_point();
if (do_gaussian_filter) RGInfo.gaussian.compute();
// Read the config files
conf_info.read_config(default_config_filename,
(config_filename.empty()
? default_config_filename : config_filename.c_str()));
// Process the configuration
conf_info.process_config();
}
////////////////////////////////////////////////////////////////////////
void process_data_file() {
Grid fr_grid, to_grid;
GrdFileType ftype;
ConcatString run_cs;
NcFile *nc_in = (NcFile *)0;
static const char *method_name = "process_data_file() -> ";
// Initialize configuration object
MetConfig config;
config.read(replace_path(config_const_filename).c_str());
config.read_string(FieldSA[0].c_str());
// Note: The command line argument MUST processed before this
if (compress_level < 0) compress_level = config.nc_compression();
// Get the gridded file type from config string, if present
ftype = parse_conf_file_type(&config);
// Open the input file
mlog << Debug(1) << "Reading data file: " << InputFilename << "\n";
bool goes_data = false;
bool use_python = false;
int obs_type;
Met2dDataFileFactory m_factory;
Met2dDataFile *fr_mtddf = (Met2dDataFile *) 0;
#ifdef WITH_PYTHON
string python_command = InputFilename;
bool use_xarray = (0 == python_command.find(conf_val_python_xarray));
use_python = use_xarray || (0 == python_command.find(conf_val_python_numpy));
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);
}
python_command = python_command.substr(offset+1);
obs_type = TYPE_PYTHON;
fr_mtddf = new MetNcMetDataFile();
}
else
#endif
{
nc_in = open_ncfile(InputFilename.c_str());
// Get the obs type before opening NetCDF
obs_type = get_obs_type(nc_in);
goes_data = (obs_type == TYPE_GOES || obs_type == TYPE_GOES_ADP);
if (obs_type == TYPE_NCCF) setenv(nc_att_met_point_nccf, "yes", 1);
// Read the input data file
fr_mtddf = m_factory.new_met_2d_data_file(InputFilename.c_str(), ftype);
}
if(!fr_mtddf) {
mlog << Error << "\n" << method_name
<< "\"" << InputFilename << "\" not a valid data file\n\n";
exit(1);
}
// Store the input data file types
ftype = fr_mtddf->file_type();
// Setup the VarInfo request object
VarInfoFactory v_factory;
VarInfo *vinfo;
vinfo = v_factory.new_var_info(ftype);
if(!vinfo) {
mlog << Error << "\n" << method_name
<< "unable to determine file type of \"" << InputFilename
<< "\"\n\n";
exit(1);
}
// For python types read the first field to set the grid
// Determine the "from" grid
#ifdef WITH_PYTHON
if (!use_python) fr_grid = fr_mtddf->grid();
#else
fr_grid = fr_mtddf->grid();
#endif
// Determine the "to" grid
to_grid = parse_vx_grid(RGInfo, &fr_grid, &fr_grid);
mlog << Debug(2) << "Interpolation options: "
<< "method = " << interpmthd_to_string(RGInfo.method)
<< ", vld_thresh = " << RGInfo.vld_thresh << "\n";
// Build the run command string
run_cs << "Point obs (" << fr_grid.serialize() << ") to " << to_grid.serialize();
if (goes_data) {
mlog << Debug(2) << "Input grid: " << fr_grid.serialize() << "\n";
ConcatString grid_string = get_goes_grid_input(config, fr_grid, to_grid);
if (grid_string.length() > 0) run_cs << " with " << grid_string;
}
mlog << Debug(2) << "Output grid: " << to_grid.serialize() << "\n";
// Open the output file
open_nc(to_grid, run_cs);
if (goes_data) {
process_goes_file(nc_in, config, vinfo, fr_grid, to_grid);
}
else if (TYPE_OBS == obs_type) {
process_point_file(nc_in, config, vinfo, to_grid);
}
else if (TYPE_NCCF == obs_type) {
process_point_nccf_file(nc_in, config, vinfo, fr_mtddf, to_grid);
unsetenv(nc_att_met_point_nccf);
}
#ifdef WITH_PYTHON
else if (TYPE_PYTHON == obs_type) {
process_point_python(python_command, config, vinfo, to_grid, use_xarray);
}
#endif
else {
mlog << Error << "\n" << method_name
<< "Please check the input file. Only supports GOES, MET point obs, "
<< "and CF complaint NetCDF with time/lat/lon variables.\n\n";
exit(1);
}
// Close the output file
close_nc();
// Clean up
if(nc_in) { delete nc_in; nc_in = 0; }
if(fr_mtddf) { delete fr_mtddf; fr_mtddf = (Met2dDataFile *) 0; }
if(vinfo) { delete vinfo; vinfo = (VarInfo *) 0; }
return;
}
////////////////////////////////////////////////////////////////////////
// returns true if no error
bool get_nc_data_int_array(NcFile *nc, char *var_name, int *data_array, bool stop=true) {
bool status = false;
NcVar nc_var = get_nc_var(nc, (char *)var_name, stop);
if (IS_INVALID_NC(nc_var)) {
if (stop) exit(1);
}
else {
status = get_nc_data(&nc_var, data_array);
}
return status;
}
////////////////////////////////////////////////////////////////////////
// returns true if no error
bool get_nc_data_int_array(NcFile *nc, const char *var_name, int *data_array, bool stop=true) {
bool status = false;
char *_var_name = strdup(var_name);
if (_var_name) {
status = get_nc_data_int_array(nc, _var_name, data_array, stop);
free(_var_name);
}
return status;
}
////////////////////////////////////////////////////////////////////////
// returns true if no error
bool get_nc_data_float_array(NcFile *nc, char *var_name, float *data_array) {
NcVar nc_var = get_nc_var(nc, (char *)var_name);
if (IS_INVALID_NC(nc_var)) exit(1);
bool status = get_nc_data(&nc_var, data_array);
return status;
}
////////////////////////////////////////////////////////////////////////
// returns true if no error
bool get_nc_data_float_array(NcFile *nc, const char *var_name, float *data_array) {
bool status = false;
char *_var_name = strdup(var_name);
if (_var_name) {
status = get_nc_data_float_array(nc, _var_name, data_array);
free(_var_name);
}
return status;
}
////////////////////////////////////////////////////////////////////////
// returns true if no error
bool get_nc_data_string_array(NcFile *nc, char *var_name,
StringArray *stringArray) {
NcVar nc_var = get_nc_var(nc, var_name, true);
if (IS_INVALID_NC(nc_var)) exit(1);
bool status = get_nc_data_to_array(&nc_var, stringArray);
return status;
}
////////////////////////////////////////////////////////////////////////
// returns true if no error
bool get_nc_data_string_array(NcFile *nc, const char *var_name,
StringArray *stringArray) {
bool status = false;
char *_var_name = strdup(var_name);
if (_var_name) {
status = get_nc_data_string_array(nc, _var_name, stringArray);
free(_var_name);
}
return status;
}
////////////////////////////////////////////////////////////////////////
int get_obs_type(NcFile *nc) {
int obs_type = TYPE_UNKNOWN;
ConcatString att_val_scene_id;
ConcatString att_val_project;
ConcatString input_type;
static const char *method_name = "get_obs_type() -> ";
bool has_project = get_global_att(nc, (string)"project", att_val_project);
bool has_scene_id = get_global_att(nc, (string)"scene_id", att_val_scene_id);
if( has_scene_id && has_project && att_val_project == "GOES" ) {
obs_type = TYPE_GOES;
input_type = "GOES";
if (0 < AdpFilename.length()) {
obs_type = TYPE_GOES_ADP;
input_type = "GOES_ADP";
if (!file_exists(AdpFilename.c_str())) {
mlog << Error << "\n" << method_name
<< "ADP input \"" << AdpFilename << "\" does not exist!\n\n";
exit(1);
}
}
}
else if (has_lat_lon_vars(nc)) {
obs_type = TYPE_NCCF;
input_type = "OBS_NCCF";
}
else if (has_dim(nc, nc_dim_nhdr) && has_dim(nc, nc_dim_nobs)) {
obs_type = TYPE_OBS;
input_type = "OBS_MET";
}
mlog << Debug(5) << method_name << "input type: \"" << input_type << "\"\n";
return obs_type;
}
////////////////////////////////////////////////////////////////////////
// Check the message types
void prepare_message_types(const StringArray hdr_types) {
static const char *method_name = "prepare_message_types() -> ";
message_type_list.clear();
if (0 < conf_info.message_type.n()) {
int hdr_idx;
ConcatString msg_list_str;
for(int idx=0; idx<conf_info.message_type.n(); idx++) {
string message_type_str = conf_info.message_type[idx];
if (hdr_types.has(message_type_str, hdr_idx)) {
message_type_list.add(hdr_idx);
}
else {
mlog << Debug(3) << method_name << "The message type \""
<< message_type_str << "\" does not exist\n";
}
if (idx > 0) msg_list_str << ",";
msg_list_str << message_type_str;
}
if (0 == message_type_list.n()) {
mlog << Error << "\n" << method_name
<< "No matching message types ["
<< msg_list_str << "].\n\n";
exit(1);
}
}
}
////////////////////////////////////////////////////////////////////////
IntArray prepare_qc_array(const IntArray qc_flags, StringArray qc_tables) {
IntArray qc_idx_array;
bool has_qc_flags = (qc_flags.n() > 0);
if (has_qc_flags) {
for(int idx=0; idx<qc_tables.n(); idx++) {
int qc_value = (qc_tables[idx] == "NA")
? QC_NA_INDEX : atoi(qc_tables[idx].c_str());
if (qc_flags.has(qc_value) && !qc_idx_array.has(idx)) {
qc_idx_array.add(idx);
}
}
}
return qc_idx_array;
}
////////////////////////////////////////////////////////////////////////
void process_point_met_data(MetPointData *met_point_obs, MetConfig &config, VarInfo *vinfo,
const Grid to_grid) {
int nhdr, nobs;
int nx, ny, var_count, to_count, var_count2;
int idx, hdr_idx;
int var_idx_or_gc;
int filtered_by_time, filtered_by_msg_type, filtered_by_qc;
ConcatString vname, vname_cnt, vname_mask;
DataPlane fr_dp, to_dp;
DataPlane cnt_dp, mask_dp;
DataPlane prob_dp, prob_mask_dp;
NcVar var_obs_gc, var_obs_var;
clock_t start_clock = clock();
bool has_prob_thresh = !prob_cat_thresh.check(bad_data_double);
unixtime requested_valid_time, valid_time;
static const char *method_name = "process_point_met_data() -> ";
static const char *method_name_s = "process_point_met_data()";
// Check for at least one configuration string
if(FieldSA.n() < 1) {
mlog << Error << "\n" << method_name
<< "The -field option must be used at least once!\n\n";
usage();
}
// MetNcPointObsIn nc_point_obs;
MetPointHeader *header_data = met_point_obs->get_header_data();
MetPointObsData *obs_data = met_point_obs->get_point_obs_data();
nhdr = met_point_obs->get_hdr_cnt();
nobs = met_point_obs->get_obs_cnt();
bool empty_input = (nhdr == 0 && nobs == 0);
bool use_var_id = met_point_obs->is_using_var_id();
float *hdr_lats = new float[nhdr];
float *hdr_lons = new float[nhdr];
IntArray var_index_array;
IntArray valid_time_array;
StringArray qc_tables = met_point_obs->get_qty_data();
StringArray var_names = met_point_obs->get_var_names();
StringArray hdr_valid_times = header_data->vld_array;
hdr_valid_times.sort();
met_point_obs->get_lats(hdr_lats);
met_point_obs->get_lons(hdr_lons);
// Check the message types
prepare_message_types(header_data->typ_array);
// Check and read obs_vid and obs_var if exists
bool success_to_read = true;
if (success_to_read) {
bool has_qc_flags = (qc_flags.n() > 0);
IntArray qc_idx_array = prepare_qc_array(qc_flags, qc_tables);
// Initialize size and values of output fields
nx = to_grid.nx();
ny = to_grid.ny();
to_dp.set_size(nx, ny);
to_dp.set_constant(bad_data_double);
cnt_dp.set_size(nx, ny);
cnt_dp.set_constant(0);
mask_dp.set_size(nx, ny);
mask_dp.set_constant(0);
if (has_prob_thresh || do_gaussian_filter) {
prob_dp.set_size(nx, ny);
prob_dp.set_constant(0);
prob_mask_dp.set_size(nx, ny);
prob_mask_dp.set_constant(0);
}
// Loop through the requested fields
int obs_count_zero_to, obs_count_non_zero_to;
int obs_count_zero_from, obs_count_non_zero_from;
IntArray *cellMapping = (IntArray *)0;
obs_count_zero_to = obs_count_non_zero_to = 0;
obs_count_zero_from = obs_count_non_zero_from = 0;
for(int i=0; i<FieldSA.n(); i++) {
var_idx_or_gc = -1;
// Initialize
vinfo->clear();
// Populate the VarInfo object using the config string
config.read_string(FieldSA[i].c_str());
vinfo->set_dict(config);
// Check the variable name
ConcatString error_msg;
vname = vinfo->name();
bool exit_by_field_name_error = false;
if (vname == "obs_val" || vname == "obs_lvl" || vname == "obs_hgt") {
exit_by_field_name_error = true;
error_msg << "The variable \"" << vname
<< "\" exists but is not a valid field name.\n";
}
else {
if (use_var_id) {
if (!var_names.has(vname, var_idx_or_gc)) {
exit_by_field_name_error = true;
error_msg << "The variable \"" << vname << "\" is not available.\n";
}
}
else {
const int TMP_BUF_LEN = 128;
char grib_code[TMP_BUF_LEN + 1];
var_idx_or_gc = atoi(vname.c_str());
snprintf(grib_code, TMP_BUF_LEN, "%d", var_idx_or_gc);
if (vname != grib_code) {
ConcatString var_id = conf_info.get_var_id(vname);
if( var_id.nonempty() ) {
var_idx_or_gc = atoi(var_id.c_str());
snprintf(grib_code, TMP_BUF_LEN, "%d", var_idx_or_gc);
}
else {
exit_by_field_name_error = true;
error_msg << "Invalid GRIB code [" << vname << "]\n";
}
}
else {
bool not_found_grib_code = true;
for (idx=0; idx<nobs; idx++) {
if (var_idx_or_gc == obs_data->obs_ids[idx]) {
not_found_grib_code = false;
break;
}
}
if (not_found_grib_code) {
exit_by_field_name_error = true;
error_msg << "No data for the GRIB code [" << vname << "]\n";
}
}
}
}
if (exit_by_field_name_error) {
ConcatString log_msg;
if (use_var_id) {
for (idx=0; idx<var_names.n(); idx++) {
if (0 < idx) log_msg << ", ";
log_msg << var_names[idx];
}
}
else {
log_msg << "GRIB codes: ";
IntArray grib_codes;
for (idx=0; idx<nobs; idx++) {
if (!grib_codes.has(obs_data->obs_ids[idx])) {
grib_codes.add(obs_data->obs_ids[idx]);
if (0 < idx) log_msg << ", ";
log_msg << obs_data->obs_ids[idx];
}
}
}
if (empty_input) {
mlog << Warning << "\n" << method_name
<< error_msg << "\tBut ignored because of empty input\n\n";
}
else {
mlog << Error << "\n" << method_name
<< error_msg
<< "Try setting the \"name\" in the \"-field\" command line option to one of the available names:\n"
<< "\t" << log_msg << "\n\n";
exit(1);
}
}
// Check the time range. Apply the time window
bool valid_time_from_config = true;
unixtime valid_beg_ut, valid_end_ut, obs_time;
valid_time_array.clear();
valid_time = vinfo->valid();
if (valid_time == 0) valid_time = conf_info.valid_time;
requested_valid_time = valid_time;
if (0 < valid_time) {
valid_beg_ut = valid_end_ut = valid_time;
if (!is_bad_data(conf_info.beg_ds)) valid_beg_ut += conf_info.beg_ds;
if (!is_bad_data(conf_info.end_ds)) valid_end_ut += conf_info.end_ds;
for(idx=0; idx<hdr_valid_times.n(); idx++) {
obs_time = timestring_to_unix(hdr_valid_times[idx].c_str());
if (valid_beg_ut <= obs_time && obs_time <= valid_end_ut) {
valid_time_array.add(idx);
mlog << Debug(4) << method_name << "The obs time "
<< hdr_valid_times[idx] << " was included\n";
}
}
valid_time_array.add(bad_data_int); // added dummy entry
}
else {
valid_time_from_config = false;
// Set the latest available valid time
valid_time = 0;
for(idx=0; idx<hdr_valid_times.n(); idx++) {
obs_time = timestring_to_unix(hdr_valid_times[idx].c_str());
if (obs_time > valid_time) valid_time = obs_time;
}
}
mlog << Debug(3) << method_name << "valid_time from "
<< (valid_time_from_config ? "config" : "input data") << ": "
<< unix_to_yyyymmdd_hhmmss(valid_time) << "\n";
to_dp.set_init(valid_time);
to_dp.set_valid(valid_time);
cnt_dp.set_init(valid_time);
cnt_dp.set_valid(valid_time);
mask_dp.set_init(valid_time);
mask_dp.set_valid(valid_time);
if (has_prob_thresh || do_gaussian_filter) {
prob_dp.set_init(valid_time);
prob_dp.set_valid(valid_time);
prob_mask_dp.set_init(valid_time);
prob_mask_dp.set_valid(valid_time);
}
var_index_array.clear();
// Select output variable name
vname = (VarNameSA.n() == 0)
? conf_info.get_var_name(vinfo->name())
: conf_info.get_var_name(VarNameSA[i]);
mlog << Debug(4) << method_name
<< "var: " << vname << ", index: " << var_idx_or_gc << ".\n";
var_count = var_count2 = to_count = 0;
filtered_by_time = filtered_by_msg_type = filtered_by_qc = 0;
for (idx=0; idx < nobs; idx++) {
if (var_idx_or_gc == obs_data->obs_ids[idx]) {
var_count2++;
hdr_idx = obs_data->obs_hids[idx];
if (0 < valid_time_array.n() &&
!valid_time_array.has(header_data->vld_idx_array[hdr_idx])) {
filtered_by_time++;
continue;
}
if(!keep_message_type(header_data->typ_idx_array[hdr_idx])) {
filtered_by_msg_type++;
continue;
}
// Filter by QC flag
if (has_qc_flags && !qc_idx_array.has(obs_data->obs_qids[idx])) {
filtered_by_qc++;
continue;
}
var_index_array.add(idx);
var_count++;
if (is_eq(obs_data->obs_vals[idx], 0.)) obs_count_zero_from++;
else obs_count_non_zero_from++;
}
}
if (cellMapping) {
for (idx=0; idx<(nx*ny); idx++) cellMapping[idx].clear();
delete [] cellMapping;
}
cellMapping = new IntArray[nx * ny];
if( get_grid_mapping(to_grid, cellMapping, var_index_array,
obs_data->obs_hids, hdr_lats, hdr_lons) ) {
int from_index;
IntArray cellArray;
NumArray dataArray;
int offset = 0;
int valid_count = 0;
int absent_count = 0;
int censored_count = 0;
float data_value;
float from_min_value = 10e10;
float from_max_value = -10e10;
// Initialize counter and output fields
to_count = 0;
to_dp.set_constant(bad_data_double);
cnt_dp.set_constant(0);
mask_dp.set_constant(0);
if (has_prob_thresh || do_gaussian_filter) {
prob_dp.set_constant(0);
prob_mask_dp.set_constant(0);
}
for (int x_idx = 0; x_idx<nx; x_idx++) {
for (int y_idx = 0; y_idx<ny; y_idx++) {
offset = to_dp.two_to_one(x_idx,y_idx);
cellArray = cellMapping[offset];
int prob_cnt = 0;
int prob_value_sum = 0;
if (0 < cellArray.n()) {
valid_count = 0;
dataArray.clear();
dataArray.extend(cellArray.n());
for (int dIdx=0; dIdx<cellArray.n(); dIdx++) {
from_index = cellArray[dIdx];
data_value = obs_data->get_obs_val(from_index);
if (is_bad_data(data_value)) continue;
if(mlog.verbosity_level() >= 4) {
if (from_min_value > data_value) from_min_value = data_value;
if (from_max_value < data_value) from_max_value = data_value;
}
for(int ic=0; ic<vinfo->censor_thresh().n(); ic++) {
// Break out after the first match.
if(vinfo->censor_thresh()[ic].check(data_value)) {
data_value = vinfo->censor_val()[ic];
censored_count++;
break;
}
}
dataArray.add(data_value);
valid_count++;
}
if (0 < valid_count) to_count++;
int data_count = dataArray.n();
if (0 < data_count) {
float to_value;
if (RGInfo.method == InterpMthd_Min) to_value = dataArray.min();
else if (RGInfo.method == InterpMthd_Max) to_value = dataArray.max();
else if (RGInfo.method == InterpMthd_Median) {