NCEPLIBS-g2c  1.8.0
g2ccompare.c
Go to the documentation of this file.
1 
7 #include "grib2_int.h"
8 #include <stdarg.h>
9 #include <math.h>
10 
13 
27 int
28 g2c_compare(int g2cid1, int g2cid2)
29 {
30  G2C_MESSAGE_INFO_T *msg1, *msg2;
31  int m;
32  /* int total_fields = 0; */
33  /* int i; */
34  /* int ret; */
35 
36  /* Check inputs. */
37  if (g2cid1 < 0 || g2cid1 > G2C_MAX_FILES || g2c_file[g2cid1].g2cid != g2cid1)
38  return G2C_EBADID;
39  if (g2cid2 < 0 || g2cid2 > G2C_MAX_FILES || g2c_file[g2cid2].g2cid != g2cid2)
40  return G2C_EBADID;
41 
42  LOG((2, "g2c_metadata_cmp g2cid1 %d g2cid2 %d", g2cid1, g2cid2));
43 
44  /* Same number of messages? */
45  if (g2c_file[g2cid1].num_messages != g2c_file[g2cid2].num_messages)
46  return G2C_ERROR;
47 
48  /* Check metadata of each message. */
49  msg1 = g2c_file[g2cid1].msg;
50  msg2 = g2c_file[g2cid2].msg;
51  for (m = 0; m < g2c_file[g2cid1].num_messages; m++)
52  {
53  int fld;
54 
55  if (msg1->discipline != msg2->discipline)
56  return G2C_ERROR;
57  if (msg1->center != msg2->center || msg1->subcenter != msg2->subcenter ||
58  msg1->master_version != msg2->master_version || msg1->local_version != msg2->local_version ||
59  msg1->sig_ref_time != msg2->sig_ref_time || msg1->year != msg2->year ||
60  msg1->month != msg2->month || msg1->day != msg2->day || msg1->hour != msg2->hour ||
61  msg1->minute != msg2->minute || msg1->second != msg2->second || msg1->status != msg2->status ||
62  msg1->type != msg2->type)
63  return G2C_ERROR;
64  if (msg1->num_local != msg2->num_local || msg1->num_fields != msg2->num_fields)
65  return G2C_ERROR;
66 
67  /* For each field, print info. */
68  for (fld = 0; fld < msg1->num_fields; fld++)
69  {
70  G2C_SECTION_INFO_T *sec_1, *sec_2;
71  G2C_SECTION_INFO_T *sec3_1, *sec3_2;
72  G2C_SECTION_INFO_T *sec5_1, *sec5_2;
73  G2C_SECTION3_INFO_T *sec3_info_1, *sec3_info_2;
74  G2C_SECTION4_INFO_T *sec4_info_1, *sec4_info_2;
75  G2C_SECTION5_INFO_T *sec5_info_1, *sec5_info_2;
76  int t;
77 
78  /* Find this field (a.k.a. product, a.k.a. section 4). */
79  for (sec_1 = msg1->sec; sec_1; sec_1 = sec_1->next)
80  {
81  sec4_info_1 = (G2C_SECTION4_INFO_T *)sec_1->sec_info;
82  if (sec_1->sec_num == 4 && sec4_info_1->field_num == fld)
83  break;
84  }
85  if (!sec_1)
86  return G2C_ENOSECTION;
87  for (sec_2 = msg2->sec; sec_2; sec_2 = sec_2->next)
88  {
89  sec4_info_2 = (G2C_SECTION4_INFO_T *)sec_2->sec_info;
90  if (sec_2->sec_num == 4 && sec4_info_2->field_num == fld)
91  break;
92  }
93  if (!sec_2)
94  return G2C_ENOSECTION;
95  if (sec4_info_1->num_coord != sec4_info_2->num_coord ||
96  sec4_info_1->prod_def != sec4_info_2->prod_def)
97  return G2C_ERROR;
98  if (sec_1->template_len != sec_2->template_len)
99  return G2C_ERROR;
100  for (t = 0; t < sec_1->template_len; t++)
101  if (sec_1->template[t] != sec_2->template[t])
102  return G2C_ERROR;
103 
104  /* Find the sec3 that applies to this field. */
105  for (sec3_1 = sec_1; sec3_1; sec3_1 = sec3_1->prev)
106  if (sec3_1->sec_num == 3)
107  break;
108  if (!sec3_1)
109  return G2C_ENOSECTION;
110  sec3_info_1 = (G2C_SECTION3_INFO_T *)sec3_1->sec_info;
111  for (sec3_2 = sec_2; sec3_2; sec3_2 = sec3_2->prev)
112  if (sec3_2->sec_num == 3)
113  break;
114  if (!sec3_2)
115  return G2C_ENOSECTION;
116  sec3_info_2 = (G2C_SECTION3_INFO_T *)sec3_2->sec_info;
117  if (sec3_info_1->source_grid_def != sec3_info_2->source_grid_def ||
118  sec3_info_1->num_data_points != sec3_info_2->num_data_points ||
119  sec3_info_1->num_opt != sec3_info_2->num_opt ||
120  sec3_info_1->interp_list != sec3_info_2->interp_list ||
121  sec3_info_1->grid_def != sec3_info_2->grid_def)
122  return G2C_ERROR;
123  if (sec3_1->template_len != sec3_2->template_len)
124  return G2C_ERROR;
125  for (t = 0; t < sec3_1->template_len; t++)
126  if (sec3_1->template[t] != sec3_2->template[t])
127  return G2C_ERROR;
128 
129  /* Find the sec5 that applies to this field. */
130  for (sec5_1 = sec_1; sec5_1; sec5_1 = sec5_1->next)
131  if (sec5_1->sec_num == 5)
132  break;
133  if (!sec5_1)
134  return G2C_ENOSECTION;
135  sec5_info_1 = (G2C_SECTION5_INFO_T *)sec5_1->sec_info;
136  for (sec5_2 = sec_2; sec5_2; sec5_2 = sec5_2->next)
137  if (sec5_2->sec_num == 5)
138  break;
139  if (!sec5_2)
140  return G2C_ENOSECTION;
141  sec5_info_2 = (G2C_SECTION5_INFO_T *)sec5_2->sec_info;
142  if (sec5_info_1->num_data_points != sec5_info_2->num_data_points ||
143  sec5_info_1->data_def != sec5_info_2->data_def)
144  return G2C_ERROR;
145  if (sec5_1->template_len != sec5_2->template_len)
146  return G2C_ERROR;
147  for (t = 0; t < sec5_1->template_len; t++)
148  if (sec5_1->template[t] != sec5_2->template[t])
149  return G2C_ERROR;
150 
151  }
152  msg1 = msg1->next;
153  msg2 = msg2->next;
154  }
155 
156  /* fprintf(f, " \n Total Number of Fields Found = %d\n", total_fields); */
157 
158  return G2C_NOERROR;
159 }
G2C_FILE_INFO_T g2c_file[G2C_MAX_FILES+1]
Global file information.
Definition: g2cfile.c:30
int g2c_compare(int g2cid1, int g2cid2)
Compare the metadata of two open GRIB2 files.
Definition: g2ccompare.c:28
#define G2C_MAX_FILES
Maximum number of open files.
Definition: grib2.h:289
#define G2C_ENOSECTION
Cannot find section.
Definition: grib2.h:506
#define G2C_ERROR
General error code, returned for some test errors.
Definition: grib2.h:492
#define G2C_EBADID
Bad ID.
Definition: grib2.h:498
#define G2C_NOERROR
No error.
Definition: grib2.h:491
Header file with internal function prototypes NCEPLIBS-g2c library.
struct g2c_section_info * next
Pointer to next in list.
Definition: grib2_int.h:176
struct g2c_section_info * sec
List of section metadata.
Definition: grib2_int.h:162
short subcenter
Originating subcenter.
Definition: grib2_int.h:150
unsigned char master_version
GRIB master tables version number.
Definition: grib2_int.h:151
unsigned char type
Type of processed data in this GRIB message.
Definition: grib2_int.h:161
unsigned short grid_def
Grid definition template number (= N) (See Table 3.1).
Definition: grib2_int.h:199
unsigned char hour
Hour.
Definition: grib2_int.h:157
unsigned char minute
Minute.
Definition: grib2_int.h:158
unsigned int num_data_points
Number of data points.
Definition: grib2_int.h:196
size_t num_messages
Number of messages in the file.
Definition: grib2_int.h:240
unsigned short num_coord
Number of coordinate values after template.
Definition: grib2_int.h:209
int num_local
Number of local sections in the message.
Definition: grib2_int.h:141
unsigned char sig_ref_time
Significance of reference time.
Definition: grib2_int.h:153
unsigned char num_opt
Number of octets for optional list of numbers defining number of points.
Definition: grib2_int.h:197
unsigned char discipline
Discipline from section 0.
Definition: grib2_int.h:138
unsigned char interp_list
Interpetation of list of numbers defining number of points (See Table 3.11).
Definition: grib2_int.h:198
struct g2c_section_info * prev
Pointer to previous in list.
Definition: grib2_int.h:177
int num_fields
Number of fields in the message.
Definition: grib2_int.h:140
unsigned char sec_num
Section number.
Definition: grib2_int.h:173
unsigned int num_data_points
Number of data points where one or more values are specified in Section 7 when a bit map is present,...
Definition: grib2_int.h:221
void * sec_info
Pointer to struct specific for section 3, 4, 5, 6, or 7.
Definition: grib2_int.h:175
short year
Year.
Definition: grib2_int.h:154
unsigned char status
Production Status of Processed data in the GRIB message.
Definition: grib2_int.h:160
G2C_MESSAGE_INFO_T * msg
Information about each message in the file.
Definition: grib2_int.h:241
#define LOG(e)
Ignore logging to stdout.
Definition: grib2_int.h:426
unsigned char second
Second.
Definition: grib2_int.h:159
unsigned char day
Day.
Definition: grib2_int.h:156
unsigned char source_grid_def
Source of grid definition (See Table 3.0).
Definition: grib2_int.h:195
int template_len
Number of entries in template.
Definition: grib2_int.h:179
unsigned char local_version
Version number of GRIB local tables used to augment Master Tables.
Definition: grib2_int.h:152
unsigned char month
Month.
Definition: grib2_int.h:155
unsigned short data_def
Data representation template number (See Table 5.0).
Definition: grib2_int.h:222
long long int * template
Grid, product, or data template.
Definition: grib2_int.h:178
struct g2c_message_info * next
Pointer to next in list.
Definition: grib2_int.h:164
short center
Originating center.
Definition: grib2_int.h:149
unsigned short prod_def
Product definition template number (See Table 4.0).
Definition: grib2_int.h:210
This is the information about each open file.
Definition: grib2_int.h:236
This is the information about each message.
Definition: grib2_int.h:134
Information about Section 3 GRID DEFINITION SECTION.
Definition: grib2_int.h:194
Information about Section 4 PRODUCT DEFINITION SECTION.
Definition: grib2_int.h:207
Information about Section 5 DATA REPRESENTATION SECTION.
Definition: grib2_int.h:217
Information about a section 3 through 7 in a GRIB2 message.
Definition: grib2_int.h:169