Loading...
Searching...
No Matches
g2cprod.c
Go to the documentation of this file.
#include "grib2_int.h"
extern G2C_FILE_INFO_T g2c_file[G2C_MAX_FILES + 1];
EXTERN_MUTEX(m);
int
g2c_get_prod(int g2cid, int msg_num, int prod_num, int *num_data_points, float *data)
{
G2C_MESSAGE_INFO_T *msg;
G2C_SECTION_INFO_T *sec3, *sec4, *sec5, *sec7;
G2C_SECTION3_INFO_T *sec3_info;
/* G2C_SECTION4_INFO_T *sec4_info; */
G2C_SECTION5_INFO_T *sec5_info;
unsigned char *buf;
size_t bytes_read;
int ret = G2C_NOERROR;
/* Check inputs. */
if (g2cid < 0 || g2cid > G2C_MAX_FILES)
return G2C_EBADID;
if (msg_num < 0 || prod_num < 0)
return G2C_EINVAL;
/* If using threading, lock the mutex. */
MUTEX_LOCK(m);
/* Find the file. */
if (g2c_file[g2cid].g2cid != g2cid)
ret = G2C_EBADID;
/* Find the message. */
if (!ret)
{
for (msg = g2c_file[g2cid].msg; msg; msg = msg->next)
if (msg->msg_num == msg_num)
break;
if (!msg)
ret = G2C_ENOMSG;
}
/* Find the product. After this, sec4 will point to the
* appropropriate section 4 G2C_SECTION_INFO_T. */
if (!ret)
{
for (sec4 = msg->sec; sec4; sec4 = sec4->next)
if (sec4->sec_num == 4 && ((G2C_SECTION4_INFO_T *)sec4->sec_info)->field_num == prod_num)
break;
if (!sec4)
ret = G2C_ENOPRODUCT;
/* sec4_info = (G2C_SECTION4_INFO_T *)sec4->sec_info; */
}
/* Find the grid definiton section, section 3. It will come
* earlier in the list. */
if (!ret)
{
for (sec3 = sec4; sec3; sec3 = sec3->prev)
if (sec3->sec_num == 3)
break;
if (!sec3)
ret = G2C_ENOSECTION;
sec3_info = (G2C_SECTION3_INFO_T *)sec3->sec_info;
}
/* Find the section 5, data representation section, to learn how
* this product is compressed. Section 5 is after section 4 in the
* list. */
if (!ret)
{
for (sec5 = sec4; sec5; sec5 = sec5->next)
if (sec5->sec_num == 5)
break;
if (!sec5)
ret = G2C_ENOSECTION;
sec5_info = (G2C_SECTION5_INFO_T *)sec5->sec_info;
}
/* Find the section 7, data section. */
if (!ret)
{
for (sec7 = sec5; sec7; sec7 = sec7->next)
if (sec7->sec_num == 7)
break;
if (!sec7)
ret = G2C_ENOSECTION;
}
/* Give the caller number of data points, if desired. */
if (!ret)
{
if (num_data_points)
*num_data_points = sec5_info->num_data_points;
}
/* Allocate a char buffer to hold the packed data. */
if (data)
{
if (!ret)
if (!(buf = malloc(sizeof(char) * sec7->sec_len)))
ret = G2C_ENOMEM;
/* Jump to this section in the file. */
if (!ret)
if (fseek(g2c_file[g2cid].f, sec7->bytes_to_sec + sec7->msg->bytes_to_msg, SEEK_SET))
ret = G2C_ERROR;
/* Read the product into a char buffer. */
if (!ret)
if ((bytes_read = fread(buf, 1, sec7->sec_len, g2c_file[g2cid].f)) != sec7->sec_len)
ret = G2C_EFILE;
/* Unpack the char buffer into a float array, which must be
* allocated by the caller. */
if (!ret)
ret = g2c_unpack7(buf, sec3_info->grid_def, sec3->template_len, sec3->template,
sec5_info->data_def, sec5->template_len, sec5->template,
sec5_info->num_data_points, data);
/* Free the char buffer. */
free(buf);
}
/* If using threading, unlock the mutex. */
MUTEX_UNLOCK(m);
return ret;
}
g2c_unpack7
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
G2C_FILE_INFO_T g2c_file[G2C_MAX_FILES+1]
Global file information.
Definition g2cfile.c:10
g2c_get_prod
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
G2C_MAX_FILES
#define G2C_MAX_FILES
Maximum number of open files.
Definition grib2.h:273
G2C_ENOMSG
#define G2C_ENOMSG
No GRIB message found.
Definition grib2.h:495
G2C_ENOSECTION
#define G2C_ENOSECTION
Cannot find section.
Definition grib2.h:499
G2C_EFILE
#define G2C_EFILE
File I/O error.
Definition grib2.h:490
G2C_ENOMEM
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:493
G2C_ERROR
#define G2C_ERROR
General error code, returned for some test errors.
Definition grib2.h:485
G2C_ENOPRODUCT
#define G2C_ENOPRODUCT
Product not found.
Definition grib2.h:508
G2C_EINVAL
#define G2C_EINVAL
Invalid input.
Definition grib2.h:489
G2C_EBADID
#define G2C_EBADID
Bad ID.
Definition grib2.h:491
G2C_NOERROR
#define G2C_NOERROR
No error.
Definition grib2.h:484
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
g2c_section_info::next
struct g2c_section_info * next
Pointer to next in list.
Definition grib2_int.h:178
g2c_message_info::sec
struct g2c_section_info * sec
List of section metadata.
Definition grib2_int.h:164
MUTEX_UNLOCK
#define MUTEX_UNLOCK(m)
Pthreads not enabled, so do nothing.
Definition grib2_int.h:119
g2c_section3_info::grid_def
unsigned short grid_def
Grid definition template number (= N) (See Table 3.1).
Definition grib2_int.h:201
g2c_section_info::prev
struct g2c_section_info * prev
Pointer to previous in list.
Definition grib2_int.h:179
g2c_section_info::sec_num
unsigned char sec_num
Section number.
Definition grib2_int.h:175
g2c_section5_info::num_data_points
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
g2c_section_info::sec_info
void * sec_info
Pointer to struct specific for section 3, 4, 5, 6, or 7.
Definition grib2_int.h:177
g2c_section_info::sec_len
unsigned int sec_len
Length of the section (in bytes).
Definition grib2_int.h:173
g2c_section_info::bytes_to_sec
size_t bytes_to_sec
Number of bytes from start of message to this section.
Definition grib2_int.h:174
g2c_file_info::f
FILE * f
FILE pointer to open file.
Definition grib2_int.h:241
g2c_section_info::template_len
int template_len
Number of entries in template.
Definition grib2_int.h:181
g2c_message_info::msg_num
size_t msg_num
Number of message in file (0-based).
Definition grib2_int.h:137
g2c_section5_info::data_def
unsigned short data_def
Data representation template number (See Table 5.0).
Definition grib2_int.h:224
g2c_message_info::bytes_to_msg
size_t bytes_to_msg
Number of bytes to skip in the file, to get to this message.
Definition grib2_int.h:138
MUTEX_LOCK
#define MUTEX_LOCK(m)
Pthreads not enabled, so do nothing.
Definition grib2_int.h:116
EXTERN_MUTEX
#define EXTERN_MUTEX(m)
Pthreads not enabled, so do nothing.
Definition grib2_int.h:113
g2c_section_info::template
long long int * template
Grid, product, or data template.
Definition grib2_int.h:180
g2c_message_info::next
struct g2c_message_info * next
Pointer to next in list.
Definition grib2_int.h:166
g2c_section_info::msg
G2C_MESSAGE_INFO_T * msg
Pointer to contianing message.
Definition grib2_int.h:176
g2c_file_info
This is the information about each open file.
Definition grib2_int.h:238
g2c_message_info
This is the information about each message.
Definition grib2_int.h:136
g2c_section3_info
Information about Section 3 GRID DEFINITION SECTION.
Definition grib2_int.h:196
g2c_section4_info
Information about Section 4 PRODUCT DEFINITION SECTION.
Definition grib2_int.h:209
g2c_section5_info
Information about Section 5 DATA REPRESENTATION SECTION.
Definition grib2_int.h:219
g2c_section_info
Information about a section 3 through 7 in a GRIB2 message.
Definition grib2_int.h:171