Loading...
Searching...
No Matches
g2cinq.c
Go to the documentation of this file.
#include "grib2_int.h"
#include <stdarg.h>
extern G2C_FILE_INFO_T g2c_file[G2C_MAX_FILES + 1];
EXTERN_MUTEX(m);
int
g2c_inq(int g2cid, int *num_msg)
{
int ret = G2C_NOERROR;
/* Check input parameters. */
if (g2cid < 0 || g2cid > G2C_MAX_FILES)
return G2C_EBADID;
/* If using threading, lock the mutex. */
MUTEX_LOCK(m);
/* Find the open file. */
if (g2c_file[g2cid].g2cid != g2cid)
ret = G2C_EBADID;
/* If the caller wants to know the number of messages, tell
* them. */
if (!ret)
if (num_msg)
*num_msg = g2c_file[g2cid].num_messages;
/* If using threading, unlock the mutex. */
MUTEX_UNLOCK(m);
return ret;
}
int
g2c_inq_msg(int g2cid, int msg_num, unsigned char *discipline, int *num_fields,
int *num_local, short *center, short *subcenter, unsigned char *master_version,
unsigned char *local_version)
{
G2C_MESSAGE_INFO_T *msg;
int ret = G2C_NOERROR;
/* Check input parameters. */
if (g2cid < 0 || g2cid > G2C_MAX_FILES)
return G2C_EBADID;
if (msg_num < 0)
return G2C_EINVAL;
/* If using threading, lock the mutex. */
MUTEX_LOCK(m);
/* Find the open file. */
if (g2c_file[g2cid].g2cid != g2cid)
ret = G2C_EBADID;
/* Find the file and message. */
if (!ret)
{
ret = G2C_ENOMSG;
for (msg = g2c_file[g2cid].msg; msg; msg = msg->next)
{
if (msg->msg_num == msg_num)
{
if (discipline)
*discipline = msg->discipline;
if (num_fields)
*num_fields = msg->num_fields;
if (num_local)
*num_local = msg->num_local;
if (center)
*center = msg->center;
if (subcenter)
*subcenter = msg->subcenter;
if (master_version)
*master_version = msg->master_version;
if (local_version)
*local_version = msg->local_version;
ret = G2C_NOERROR;
break;
}
}
}
/* If using threading, unlock the mutex. */
MUTEX_UNLOCK(m);
return ret;
}
int
g2c_inq_msg_time(int g2cid, int msg_num, unsigned char *sig_ref_time, short *year,
unsigned char *month, unsigned char *day, unsigned char *hour,
unsigned char *minute, unsigned char *second)
{
G2C_MESSAGE_INFO_T *msg;
int ret = G2C_NOERROR;
/* Check input parameters. */
if (g2cid < 0 || g2cid > G2C_MAX_FILES)
return G2C_EBADID;
if (msg_num < 0)
return G2C_EINVAL;
/* If using threading, lock the mutex. */
MUTEX_LOCK(m);
/* Find the open file. */
if (g2c_file[g2cid].g2cid != g2cid)
ret = G2C_EBADID;
/* Find the file and message. */
if (!ret)
{
ret = G2C_ENOMSG;
for (msg = g2c_file[g2cid].msg; msg; msg = msg->next)
{
if (msg->msg_num == msg_num)
{
if (sig_ref_time)
*sig_ref_time = msg->sig_ref_time;
if (year)
*year = msg->year;
if (month)
*month = msg->month;
if (day)
*day = msg->day;
if (hour)
*hour = msg->hour;
if (minute)
*minute = msg->minute;
if (second)
*second = msg->second;
ret = G2C_NOERROR;
break;
}
}
}
/* If using threading, unlock the mutex. */
MUTEX_UNLOCK(m);
return ret;
}
int
g2c_inq_prod(int g2cid, int msg_num, int prod_num, int *pds_template_len,
long long int *pds_template, int *gds_template_len, long long int *gds_template,
int *drs_template_len, long long int *drs_template)
{
G2C_MESSAGE_INFO_T *msg;
G2C_SECTION_INFO_T *sec4, *sec3, *sec5;
int t;
int ret = G2C_NOERROR;
/* Is this an open GRIB2 file? */
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);
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; */
}
/* Return the info to the caller. */
if (!ret)
{
if (pds_template_len)
*pds_template_len = sec4->template_len;
if (pds_template)
for (t = 0; t < sec4->template_len; t++)
pds_template[t] = sec4->template[t];
}
/* Find the GDS. */
if (!ret)
{
for (sec3 = sec4->prev; sec3; sec3 = sec3->prev)
if (sec3->sec_num == 3)
break;
if (!sec3)
ret = G2C_ENOSECTION;
}
/* Return the info to the caller. */
if (!ret)
{
if (gds_template_len)
*gds_template_len = sec3->template_len;
if (gds_template)
for (t = 0; t < sec3->template_len; t++)
gds_template[t] = sec3->template[t];
}
/* Find the DRS. */
if (!ret)
{
for (sec5 = sec4->next; sec5; sec5 = sec5->next)
if (sec5->sec_num == 5)
break;
if (!sec5)
ret = G2C_ENOSECTION;
}
/* Return the info to the caller. */
if (!ret)
{
if (drs_template_len)
*drs_template_len = sec5->template_len;
if (drs_template)
for (t = 0; t < sec5->template_len; t++)
drs_template[t] = sec5->template[t];
}
/* If using threading, unlock the mutex. */
MUTEX_UNLOCK(m);
return ret;
}
int
g2c_inq_dim(int g2cid, int msg_num, int prod_num, int dim_num, size_t *len,
char *name, float *val)
{
G2C_MESSAGE_INFO_T *msg;
G2C_SECTION_INFO_T *sec4, *sec3;
G2C_DIM_INFO_T *dim;
int d;
int ret = G2C_NOERROR;
/* Are these valid IDs? */
if (g2cid < 0 || g2cid > G2C_MAX_FILES)
return G2C_EBADID;
if (msg_num < 0 || prod_num < 0 || dim_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 GDS. */
if (!ret)
{
for (sec3 = sec4->prev; sec3; sec3 = sec3->prev)
if (sec3->sec_num == 3)
break;
if (!sec3)
ret = G2C_ENOSECTION;
dim = &((G2C_SECTION3_INFO_T *)sec3->sec_info)->dim[dim_num];
}
/* Give the caller the info they want. */
if (!ret)
{
if (len)
*len = dim->len;
if (name)
strncpy(name, dim->name, G2C_MAX_NAME);
if (val)
for (d = 0; d < dim->len; d++)
val[d] = dim->value[d];
}
/* If using threading, unlock the mutex. */
MUTEX_UNLOCK(m);
return ret;
}
int
g2c_inq_dim_info(int g2cid, int msg_num, int prod_num, int dim_num, size_t *len,
char *name)
{
return g2c_inq_dim(g2cid, msg_num, prod_num, dim_num, len, name, NULL);
}
g2c_inq
int g2c_inq(int g2cid, int *num_msg)
Learn about a GRIB2 file.
Definition g2cinq.c:31
g2c_file
G2C_FILE_INFO_T g2c_file[G2C_MAX_FILES+1]
Global file information.
Definition g2cfile.c:10
g2c_inq_dim
int g2c_inq_dim(int g2cid, int msg_num, int prod_num, int dim_num, size_t *len, char *name, float *val)
Learn about the one of the dimensions of a GRIB2 product.
Definition g2cinq.c:372
g2c_inq_msg_time
int g2c_inq_msg_time(int g2cid, int msg_num, unsigned char *sig_ref_time, short *year, unsigned char *month, unsigned char *day, unsigned char *hour, unsigned char *minute, unsigned char *second)
Learn about the date/time information in a GRIB2 message.
Definition g2cinq.c:170
g2c_inq_prod
int g2c_inq_prod(int g2cid, int msg_num, int prod_num, int *pds_template_len, long long int *pds_template, int *gds_template_len, long long int *gds_template, int *drs_template_len, long long int *drs_template)
Inquire about a product.
Definition g2cinq.c:247
g2c_inq_msg
int g2c_inq_msg(int g2cid, int msg_num, unsigned char *discipline, int *num_fields, int *num_local, short *center, short *subcenter, unsigned char *master_version, unsigned char *local_version)
Learn about a GRIB2 message.
Definition g2cinq.c:87
g2c_inq_dim_info
int g2c_inq_dim_info(int g2cid, int msg_num, int prod_num, int dim_num, size_t *len, char *name)
Learn about the one of the dimensions of a GRIB2 product.
Definition g2cinq.c:468
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_MAX_NAME
#define G2C_MAX_NAME
Maximum length of a name.
Definition grib2.h:274
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
g2c_dim_info::len
size_t len
Length of dimension.
Definition grib2_int.h:188
g2c_message_info::subcenter
short subcenter
Originating subcenter.
Definition grib2_int.h:152
g2c_message_info::master_version
unsigned char master_version
GRIB master tables version number.
Definition grib2_int.h:153
MUTEX_UNLOCK
#define MUTEX_UNLOCK(m)
Pthreads not enabled, so do nothing.
Definition grib2_int.h:119
g2c_message_info::hour
unsigned char hour
Hour.
Definition grib2_int.h:159
g2c_message_info::minute
unsigned char minute
Minute.
Definition grib2_int.h:160
g2c_file_info::num_messages
size_t num_messages
Number of messages in the file.
Definition grib2_int.h:242
g2c_message_info::num_local
int num_local
Number of local sections in the message.
Definition grib2_int.h:143
g2c_message_info::sig_ref_time
unsigned char sig_ref_time
Significance of reference time.
Definition grib2_int.h:155
g2c_message_info::discipline
unsigned char discipline
Discipline from section 0.
Definition grib2_int.h:140
g2c_dim_info::name
char name[G2C_MAX_NAME+1]
Name of dimension.
Definition grib2_int.h:189
g2c_section_info::prev
struct g2c_section_info * prev
Pointer to previous in list.
Definition grib2_int.h:179
g2c_message_info::num_fields
int num_fields
Number of fields in the message.
Definition grib2_int.h:142
g2c_section_info::sec_num
unsigned char sec_num
Section number.
Definition grib2_int.h:175
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_dim_info::value
float * value
Array of dimension values.
Definition grib2_int.h:190
g2c_message_info::year
short year
Year.
Definition grib2_int.h:156
g2c_message_info::second
unsigned char second
Second.
Definition grib2_int.h:161
g2c_message_info::day
unsigned char day
Day.
Definition grib2_int.h:158
g2c_section_info::template_len
int template_len
Number of entries in template.
Definition grib2_int.h:181
g2c_message_info::local_version
unsigned char local_version
Version number of GRIB local tables used to augment Master Tables.
Definition grib2_int.h:154
g2c_message_info::month
unsigned char month
Month.
Definition grib2_int.h:157
g2c_message_info::msg_num
size_t msg_num
Number of message in file (0-based).
Definition grib2_int.h:137
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_message_info::center
short center
Originating center.
Definition grib2_int.h:151
g2c_dim_info
Keep information about dimensions defined in section 3.
Definition grib2_int.h:186
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_section_info
Information about a section 3 through 7 in a GRIB2 message.
Definition grib2_int.h:171