NCEPLIBS-g2c 2.2.0
Loading...
Searching...
No Matches
g2cprod.c
Go to the documentation of this file.
1
7#include "grib2_int.h"
8
11
15
31int
32g2c_get_prod(int g2cid, int msg_num, int prod_num, int *num_data_points, float *data)
33{
35 G2C_SECTION_INFO_T *sec3, *sec4, *sec5, *sec7;
36 G2C_SECTION3_INFO_T *sec3_info;
37 /* G2C_SECTION4_INFO_T *sec4_info; */
38 G2C_SECTION5_INFO_T *sec5_info;
39 unsigned char *buf;
40 size_t bytes_read;
41 int ret = G2C_NOERROR;
42
43 /* Check inputs. */
44 if (g2cid < 0 || g2cid > G2C_MAX_FILES)
45 return G2C_EBADID;
46 if (msg_num < 0 || prod_num < 0)
47 return G2C_EINVAL;
48
49 /* If using threading, lock the mutex. */
50 MUTEX_LOCK(m);
51
52 /* Find the file. */
53 if (g2c_file[g2cid].g2cid != g2cid)
54 ret = G2C_EBADID;
55
56 /* Find the message. */
57 if (!ret)
58 {
59 for (msg = g2c_file[g2cid].msg; msg; msg = msg->next)
60 if (msg->msg_num == msg_num)
61 break;
62 if (!msg)
63 ret = G2C_ENOMSG;
64 }
65
66 /* Find the product. After this, sec4 will point to the
67 * appropropriate section 4 G2C_SECTION_INFO_T. */
68 if (!ret)
69 {
70 for (sec4 = msg->sec; sec4; sec4 = sec4->next)
71 if (sec4->sec_num == 4 && ((G2C_SECTION4_INFO_T *)sec4->sec_info)->field_num == prod_num)
72 break;
73 if (!sec4)
74 ret = G2C_ENOPRODUCT;
75 /* sec4_info = (G2C_SECTION4_INFO_T *)sec4->sec_info; */
76 }
77
78 /* Find the grid definiton section, section 3. It will come
79 * earlier in the list. */
80 if (!ret)
81 {
82 for (sec3 = sec4; sec3; sec3 = sec3->prev)
83 if (sec3->sec_num == 3)
84 break;
85 if (!sec3)
86 ret = G2C_ENOSECTION;
87 sec3_info = (G2C_SECTION3_INFO_T *)sec3->sec_info;
88 }
89
90 /* Find the section 5, data representation section, to learn how
91 * this product is compressed. Section 5 is after section 4 in the
92 * list. */
93 if (!ret)
94 {
95 for (sec5 = sec4; sec5; sec5 = sec5->next)
96 if (sec5->sec_num == 5)
97 break;
98 if (!sec5)
99 ret = G2C_ENOSECTION;
100 sec5_info = (G2C_SECTION5_INFO_T *)sec5->sec_info;
101 }
102
103 /* Find the section 7, data section. */
104 if (!ret)
105 {
106 for (sec7 = sec5; sec7; sec7 = sec7->next)
107 if (sec7->sec_num == 7)
108 break;
109 if (!sec7)
110 ret = G2C_ENOSECTION;
111 }
112
113 /* Give the caller number of data points, if desired. */
114 if (!ret)
115 {
116 if (num_data_points)
117 *num_data_points = sec5_info->num_data_points;
118 }
119
120 /* Allocate a char buffer to hold the packed data. */
121 if (data)
122 {
123 if (!ret)
124 if (!(buf = malloc(sizeof(char) * sec7->sec_len)))
125 ret = G2C_ENOMEM;
126
127 /* Jump to this section in the file. */
128 if (!ret)
129 if (fseek(g2c_file[g2cid].f, sec7->bytes_to_sec + sec7->msg->bytes_to_msg, SEEK_SET))
130 ret = G2C_ERROR;
131
132 /* Read the product into a char buffer. */
133 if (!ret)
134 if ((bytes_read = fread(buf, 1, sec7->sec_len, g2c_file[g2cid].f)) != sec7->sec_len)
135 ret = G2C_EFILE;
136
137 /* Unpack the char buffer into a float array, which must be
138 * allocated by the caller. */
139 if (!ret)
140 ret = g2c_unpack7(buf, sec3_info->grid_def, sec3->template_len, sec3->template,
141 sec5_info->data_def, sec5->template_len, sec5->template,
142 sec5_info->num_data_points, data);
143
144 /* Free the char buffer. */
145 free(buf);
146 }
147
148 /* If using threading, unlock the mutex. */
149 MUTEX_UNLOCK(m);
150
151 return ret;
152}
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:32
#define G2C_MAX_FILES
Maximum number of open files.
Definition grib2.h:273
#define G2C_ENOMSG
No GRIB message found.
Definition grib2.h:497
#define G2C_ENOSECTION
Cannot find section.
Definition grib2.h:501
#define G2C_EFILE
File I/O error.
Definition grib2.h:492
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:495
#define G2C_ERROR
General error code, returned for some test errors.
Definition grib2.h:487
#define G2C_ENOPRODUCT
Product not found.
Definition grib2.h:510
#define G2C_EINVAL
Invalid input.
Definition grib2.h:491
#define G2C_EBADID
Bad ID.
Definition grib2.h:493
#define G2C_NOERROR
No error.
Definition grib2.h:486
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
#define MUTEX_UNLOCK(m)
Pthreads not enabled, so do nothing.
Definition grib2_int.h:119
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
#define MUTEX_LOCK(m)
Pthreads not enabled, so do nothing.
Definition grib2_int.h:116
#define EXTERN_MUTEX(m)
Pthreads not enabled, so do nothing.
Definition grib2_int.h:113
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