NCEPLIBS-g2c 2.0.0
Loading...
Searching...
No Matches
g2cprod.c
Go to the documentation of this file.
1
7#include "grib2_int.h"
8
11
27int
28g2c_get_prod(int g2cid, int msg_num, int prod_num, int *num_data_points, float *data)
29{
31 G2C_SECTION_INFO_T *sec3, *sec4, *sec5, *sec7;
32 G2C_SECTION3_INFO_T *sec3_info;
33 /* G2C_SECTION4_INFO_T *sec4_info; */
34 G2C_SECTION5_INFO_T *sec5_info;
35 unsigned char *buf;
36 size_t bytes_read;
37 int ret = G2C_NOERROR;
38
39 /* Check inputs. */
40 if (g2cid < 0 || g2cid > G2C_MAX_FILES)
41 return G2C_EBADID;
42 if (msg_num < 0 || prod_num < 0)
43 return G2C_EINVAL;
44 if (g2c_file[g2cid].g2cid != g2cid)
45 return G2C_EBADID;
46
47 /* Find the message. */
48 for (msg = g2c_file[g2cid].msg; msg; msg = msg->next)
49 if (msg->msg_num == msg_num)
50 break;
51 if (!msg)
52 return G2C_ENOMSG;
53
54 /* Find the product. After this, sec4 will point to the
55 * appropropriate section 4 G2C_SECTION_INFO_T. */
56 for (sec4 = msg->sec; sec4; sec4 = sec4->next)
57 if (sec4->sec_num == 4 && ((G2C_SECTION4_INFO_T *)sec4->sec_info)->field_num == prod_num)
58 break;
59 if (!sec4)
60 return G2C_ENOPRODUCT;
61 /* sec4_info = (G2C_SECTION4_INFO_T *)sec4->sec_info; */
62
63 /* Find the grid definiton section, section 3. It will come
64 * earlier in the list. */
65 for (sec3 = sec4; sec3; sec3 = sec3->prev)
66 if (sec3->sec_num == 3)
67 break;
68 if (!sec3)
69 return G2C_ENOSECTION;
70 sec3_info = (G2C_SECTION3_INFO_T *)sec3->sec_info;
71
72 /* Find the section 5, data representation section, to learn how
73 * this product is compressed. Section 5 is after section 4 in the
74 * list. */
75 for (sec5 = sec4; sec5; sec5 = sec5->next)
76 if (sec5->sec_num == 5)
77 break;
78 if (!sec5)
79 return G2C_ENOSECTION;
80 sec5_info = (G2C_SECTION5_INFO_T *)sec5->sec_info;
81
82 /* Find the section 7, data section. */
83 for (sec7 = sec5; sec7; sec7 = sec7->next)
84 if (sec7->sec_num == 7)
85 break;
86 if (!sec7)
87 return G2C_ENOSECTION;
88
89 /* Give the caller number of data points, if desired. */
90 if (num_data_points)
91 *num_data_points = sec5_info->num_data_points;
92
93 /* If user doesn't want data, we need go no further. */
94 if (!data)
95 return G2C_NOERROR;
96
97 /* Allocate a char buffer to hold the packed data. */
98 if (!(buf = malloc(sizeof(char) * sec7->sec_len)))
99 return G2C_ENOMEM;
100
101 /* Jump to this section in the file. */
102 if (fseek(g2c_file[g2cid].f, sec7->bytes_to_sec + sec7->msg->bytes_to_msg, SEEK_SET))
103 return G2C_ERROR;
104
105 /* Read the product into a char buffer. */
106 if ((bytes_read = fread(buf, 1, sec7->sec_len, g2c_file[g2cid].f)) != sec7->sec_len)
107 return G2C_EFILE;
108
109 /* Unpack the char buffer into a float array, which must be
110 * allocated by the caller. */
111 ret = g2c_unpack7(buf, sec3_info->grid_def, sec3->template_len, sec3->template,
112 sec5_info->data_def, sec5->template_len, sec5->template,
113 sec5_info->num_data_points, data);
114
115 /* Free the char buffer. */
116 free(buf);
117
118 return ret;
119}
int g2c_unpack7(unsigned char *cgrib, int igdsnum, int gds_tmpl_len, long long int *gdstmpl, int idrsnum, int drs_tmpl_len, long long int *drstmpl, int ndpts, float *fld)
This subroutine unpacks Section 7 (Data Section) of a GRIB2 message.
Definition g2_unpack7.c:259
G2C_FILE_INFO_T g2c_file[G2C_MAX_FILES+1]
Global file information.
Definition g2cfile.c:10
int g2c_get_prod(int g2cid, int msg_num, int prod_num, int *num_data_points, float *data)
Read the data for a product.
Definition g2cprod.c:28
#define G2C_MAX_FILES
Maximum number of open files.
Definition grib2.h:273
#define G2C_ENOMSG
No GRIB message found.
Definition grib2.h:487
#define G2C_ENOSECTION
Cannot find section.
Definition grib2.h:491
#define G2C_EFILE
File I/O error.
Definition grib2.h:482
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:485
#define G2C_ERROR
General error code, returned for some test errors.
Definition grib2.h:477
#define G2C_ENOPRODUCT
Product not found.
Definition grib2.h:500
#define G2C_EINVAL
Invalid input.
Definition grib2.h:481
#define G2C_EBADID
Bad ID.
Definition grib2.h:483
#define G2C_NOERROR
No error.
Definition grib2.h:476
Header file with internal function prototypes NCEPLIBS-g2c library.
struct g2c_section_info * next
Pointer to next in list.
Definition grib2_int.h:178
struct g2c_section_info * sec
List of section metadata.
Definition grib2_int.h:164
unsigned short grid_def
Grid definition template number (= N) (See Table 3.1).
Definition grib2_int.h:201
struct g2c_section_info * prev
Pointer to previous in list.
Definition grib2_int.h:179
unsigned char sec_num
Section number.
Definition grib2_int.h:175
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:223
void * sec_info
Pointer to struct specific for section 3, 4, 5, 6, or 7.
Definition grib2_int.h:177
unsigned int sec_len
Length of the section (in bytes).
Definition grib2_int.h:173
size_t bytes_to_sec
Number of bytes from start of message to this section.
Definition grib2_int.h:174
FILE * f
FILE pointer to open file.
Definition grib2_int.h:241
int template_len
Number of entries in template.
Definition grib2_int.h:181
size_t msg_num
Number of message in file (0-based).
Definition grib2_int.h:137
unsigned short data_def
Data representation template number (See Table 5.0).
Definition grib2_int.h:224
size_t bytes_to_msg
Number of bytes to skip in the file, to get to this message.
Definition grib2_int.h:138
long long int * template
Grid, product, or data template.
Definition grib2_int.h:180
struct g2c_message_info * next
Pointer to next in list.
Definition grib2_int.h:166
G2C_MESSAGE_INFO_T * msg
Pointer to contianing message.
Definition grib2_int.h:176
This is the information about each open file.
Definition grib2_int.h:238
This is the information about each message.
Definition grib2_int.h:136
Information about Section 3 GRID DEFINITION SECTION.
Definition grib2_int.h:196
Information about Section 4 PRODUCT DEFINITION SECTION.
Definition grib2_int.h:209
Information about Section 5 DATA REPRESENTATION SECTION.
Definition grib2_int.h:219
Information about a section 3 through 7 in a GRIB2 message.
Definition grib2_int.h:171