---
name: "g2cfile.c"
section: "3"
platform: "linux"
package: "libg2c-dev"
version: "2.1.0-2"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/g2cfile.c"
---

# g2cfile.c(3)

File functions for the g2c library.

## NAME

g2cfile.c - File functions for the g2c library.

## SYNOPSIS

`#include 'grib2_int.h'`\

### Macros

\#define **BYTES\_TO\_DISCIPLINE**   6\
 Number of bytes to discipline field in GRIB2 message.  #define **G2C\_SEEKMSG\_BUFSIZE**   4092\
 Size of buffer used in **g2c\_seekmsg()**.  #define **MIN**(a,  b)   ((a) \< (b) ? (a) : (b))\
 Find a minimum.  #define **READ\_BUF\_SIZE**   4092\
 Default size of read-buffer.

### Functions

int **add\_msg** (**G2C\_FILE\_INFO\_T** \*file, int msg\_num, size\_t bytes\_to\_msg, size\_t bytes\_in\_msg, int read\_file, **G2C\_MESSAGE\_INFO\_T** \*\*msg)\
 Add new message to linked list.  int **add\_section** (FILE \*f, **G2C\_MESSAGE\_INFO\_T** \*msg, int sec\_id, unsigned int sec\_len, size\_t bytes\_to\_sec, unsigned char sec\_num)\
 Add metadata about a new section 3, 4, 5, 6, or 7.  static int **determine\_dims** (**G2C\_SECTION\_INFO\_T** \*sec)\
 Determine the dimension information from the section 3 metadata.  static int **find\_available\_g2cid** (int \*g2cid)\
 Find a g2cid to use for a newly opened or created file.  static int **free\_metadata** (int g2cid)\
 Free resources holding the file metadata.  int **g2c\_add\_file** (const char \*path, int mode, int \*g2cid)\
 Open a GRIB2 file and add it to the list of open files.  int **g2c\_close** (int g2cid)\
 Close a GRIB2 file, freeing resources.  int **g2c\_find\_msg2** (int g2cid, size\_t skip\_bytes, size\_t max\_bytes, size\_t \*bytes\_to\_msg, size\_t \*bytes\_in\_msg)\
 Search a file for the next GRIB1 or GRIB2 message.  int **g2c\_get\_msg** (int g2cid, size\_t skip\_bytes, size\_t max\_bytes, size\_t \*bytes\_to\_msg, size\_t \*bytes\_in\_msg, unsigned char \*\*cbuf)\
 Search a file for the next GRIB1 or GRIB2 message, and read it, allocating space in memory to hold the message.  int **g2c\_open** (const char \*path, int mode, int \*g2cid)\
 Open an existing GRIB2 file.  int **g2c\_rw\_section1\_metadata** (FILE \*f, int rw\_flag, **G2C\_MESSAGE\_INFO\_T** \*msg)\
 Read Section 1.  int **g2c\_rw\_section3\_metadata** (FILE \*f, int rw\_flag, **G2C\_SECTION\_INFO\_T** \*sec)\
 Read the metadata from section 3 (Grid Definition Section) of a GRIB2 message.  int **g2c\_rw\_section4\_metadata** (FILE \*f, int rw\_flag, **G2C\_SECTION\_INFO\_T** \*sec)\
 Read or write the metadata from section 4 (Product Definition Section) of a GRIB2 message.  int **g2c\_rw\_section5\_metadata** (FILE \*f, int rw\_flag, **G2C\_SECTION\_INFO\_T** \*sec)\
 Read or write the metadata from section 5 (Data Representation Section) of a GRIB2 message.  int **g2c\_rw\_section6\_metadata** (FILE \*f, int rw\_flag, **G2C\_SECTION\_INFO\_T** \*sec)\
 Read or write the metadata from section 6 (Data Representation Section) of a GRIB2 message.  int **g2c\_seekmsg** (int g2cid, size\_t skip, size\_t \*offset, size\_t \*msglen)\
 Search a file for the next GRIB2 Message.  **MUTEX** (m)\
 Define mutex for thread-safety.  static int **read\_metadata** (int g2cid)\
 Read metadata from a GRIB2 file being opened with **g2c\_open()**.  static int **read\_msg\_metadata** (**G2C\_MESSAGE\_INFO\_T** \*msg)\
 Read the file to get metadata about a message.

### Variables

**G2C\_FILE\_INFO\_T** **g2c\_file** \[**G2C\_MAX\_FILES**+1\]\
 Global file information.  int **g2c\_next\_g2cid** = 1\
 Next g2cid file ID - used when opening or creating a file.

## Detailed Description

File functions for the g2c library.

**Author**

Ed Hartnett

**Date**

Aug 16, 2022

Definition in file **g2cfile.c**.

## Macro Definition Documentation

### #define BYTES\_TO\_DISCIPLINE 6

Number of bytes to discipline field in GRIB2 message.

Definition at line **22** of file **g2cfile.c**.

### #define G2C\_SEEKMSG\_BUFSIZE 4092

Size of buffer used in **g2c\_seekmsg()**.

Definition at line **27** of file **g2cfile.c**.

### #define MIN(a, b) ((a) \< (b) ? (a) : (b))

Find a minimum.

Definition at line **16** of file **g2cfile.c**.

### #define READ\_BUF\_SIZE 4092

Default size of read-buffer.

Definition at line **19** of file **g2cfile.c**.

## Function Documentation

### int add\_msg (G2C\_FILE\_INFO\_T \* file, int msg\_num, size\_t bytes\_to\_msg, size\_t bytes\_in\_msg, int read\_file, G2C\_MESSAGE\_INFO\_T \*\* msg)

Add new message to linked list.

**Parameters**

*file* Pointer to the G2C\_FILE\_INFO\_T for this file. \
 *msg\_num* Number of the message in file (0-based). \
 *bytes\_to\_msg* Number of bytes to the start of the message in the file. \
 *bytes\_in\_msg* Length of message in bytes. \
 *read\_file* Set to true to cause metadata to be read from a GRIB2 data file. \
 *msg* Pointer to a pointer that will get the location of the newly created **G2C\_MESSAGE\_INFO\_T** object. Ignored if NULL.

**Returns**

- •

  **G2C\_NOERROR** - No error.

**Author**

Ed Hartnett

**Date**

Sep 12, 2022

Definition at line **1017** of file **g2cfile.c**.

References **g2c\_message\_info::bytes\_in\_msg**, **g2c\_message\_info::bytes\_to\_msg**, **g2c\_message\_info::file**, **G2C\_ENOMEM**, **G2C\_NOERROR**, **LOG**, **g2c\_file\_info::msg**, **g2c\_message\_info::msg\_num**, **g2c\_message\_info::next**, **g2c\_file\_info::num\_messages**, and **read\_msg\_metadata()**.

Referenced by **g2c\_open\_index()**, and **read\_metadata()**.

### int add\_section (FILE \* f, G2C\_MESSAGE\_INFO\_T \* msg, int sec\_id, unsigned int sec\_len, size\_t bytes\_to\_sec, unsigned char sec\_num)

Add metadata about a new section 3, 4, 5, 6, or 7.

**Parameters**

*f* FILE pointer to open GRIB2 file. \
 *msg* Pointer to the G2C\_MESSAGE\_INFO\_T struct. \
 *sec\_id* 0-based section ID. \
 *sec\_len* Length of section. \
 *bytes\_to\_sec* Number of bytes from start of message to this section. \
 *sec\_num* Section number.

**Returns**

- •

  **G2C\_NOERROR** - No error.

**Author**

Ed Hartnett

**Date**

Sep 12, 2022

Definition at line **794** of file **g2cfile.c**.

References **g2c\_section\_info::bytes\_to\_sec**, **G2C\_EBADSECTION**, **G2C\_ENOMEM**, **G2C\_FILE\_READ**, **G2C\_NOERROR**, **g2c\_rw\_section3\_metadata()**, **g2c\_rw\_section4\_metadata()**, **g2c\_rw\_section5\_metadata()**, **g2c\_rw\_section6\_metadata()**, **LOG**, **g2c\_section\_info::msg**, **g2c\_section\_info::next**, **g2c\_message\_info::num\_local**, **g2c\_section\_info::prev**, **g2c\_message\_info::sec**, **g2c\_section\_info::sec\_id**, **g2c\_section\_info::sec\_len**, and **g2c\_section\_info::sec\_num**.

Referenced by **g2c\_open\_index()**, and **read\_msg\_metadata()**.

### static int determine\_dims (G2C\_SECTION\_INFO\_T \* sec)` [static]`

Determine the dimension information from the section 3 metadata. See (GRIB2 - SECTION 3 GRID DEFINITION SECTION)\[https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2\_doc/grib2\_sect3.shtml\].

For a list of grid definitions see \[GRIB2 - TABLE 3.1 Grid Definition Template Number\](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2\_doc/grib2\_table3-1.shtml).

**Parameters**

*sec* G2C\_SECTION3\_INFO\_T struct.

**Returns**

- •

  **G2C\_NOERROR** No error.

- •

  **G2C\_EINVAL** Invalid input.

- •

  **G2C\_ENOMEM** Out of memory.

**Author**

Ed Hartnett

**Date**

Sep 15, 2022

Definition at line **415** of file **g2cfile.c**.

References **g2c\_section3\_info::dim**, **G2C\_ENOMEM**, **G2C\_MAX\_NAME**, **G2C\_NOERROR**, **g2c\_section3\_info::grid\_def**, **LATITUDE**, **g2c\_dim\_info::len**, **LOG**, **LONGITUDE**, **g2c\_dim\_info::name**, **g2c\_section\_info::sec\_info**, **g2c\_section\_info::template**, and **g2c\_dim\_info::value**.

Referenced by **g2c\_rw\_section3\_metadata()**.

### static int find\_available\_g2cid (int \* g2cid)` [static]`

Find a g2cid to use for a newly opened or created file.

**Parameters**

*g2cid* Pointer that gets the next available g2cid.

**Returns**

- •

  **G2C\_NOERROR** - No error.

- •

  **G2C\_EINVAL** - Invalid input.

- •

  **G2C\_ETOOMANYFILES** - Trying to open too many files at the same time. '

**Author**

Ed Hartnett 8/18/22

Definition at line **363** of file **g2cfile.c**.

References **G2C\_EINVAL**, **G2C\_ETOOMANYFILES**, **g2c\_file**, **G2C\_MAX\_FILES**, **g2c\_next\_g2cid**, and **G2C\_NOERROR**.

Referenced by **g2c\_add\_file()**.

### static int free\_metadata (int g2cid)` [static]`

Free resources holding the file metadata.

**Parameters**

*g2cid* Indentifier for the file.

**Returns**

- •

  **G2C\_NOERROR** - No error.

- •

  **G2C\_EBADID** - Bad file ID.

**Author**

Ed Hartnett

**Date**

Aug 16, 2022

Definition at line **1304** of file **g2cfile.c**.

References **G2C\_EBADID**, **g2c\_file**, **G2C\_MAX\_FILES**, **G2C\_NOERROR**, **LOG**, **g2c\_file\_info::msg**, **g2c\_message\_info::next**, **g2c\_section\_info::next**, **g2c\_message\_info::sec**, **g2c\_section\_info::sec\_info**, **g2c\_section\_info::sec\_num**, and **g2c\_section\_info::template**.

Referenced by **g2c\_close()**.

### int g2c\_add\_file (const char \* path, int mode, int \* g2cid)

Open a GRIB2 file and add it to the list of open files.

**Parameters**

*path* Path of the file. \
 *mode* Open mode flags. \
 *g2cid* Pointer that gets an indentifier for the file.

**Returns**

- •

  **G2C\_NOERROR** - No error.

- •

  **G2C\_EINVAL** - Invalid input.

- •

  **G2C\_ETOOMANYFILES** - Trying to open too many files at the same time.

**Author**

Ed Hartnett

**Date**

Aug 16, 2022

Definition at line **1143** of file **g2cfile.c**.

References **find\_available\_g2cid()**, **G2C\_EFILE**, **G2C\_EINVAL**, **G2C\_ENAMETOOLONG**, **g2c\_file**, **G2C\_MAX\_NAME**, **G2C\_NOERROR**, **G2C\_WRITE**, **g2c\_file\_info::g2cid**, **LOG**, **g2c\_file\_info::msg**, and **g2c\_file\_info::num\_messages**.

Referenced by **g2c\_open()**, and **g2c\_open\_index()**.

### int g2c\_close (int g2cid)

Close a GRIB2 file, freeing resources.

**Parameters**

*g2cid* Indentifier for the file.

**Returns**

- •

  **G2C\_NOERROR** - No error.

- •

  **G2C\_EBADID** - Bad file ID.

**Author**

Ed Hartnett

**Date**

Aug 16, 2022

Definition at line **1367** of file **g2cfile.c**.

References **g2c\_file\_info::f**, **free\_metadata()**, **G2C\_EBADID**, **G2C\_EFILE**, **g2c\_file**, **G2C\_MAX\_FILES**, **G2C\_NOERROR**, **g2c\_file\_info::g2cid**, **LOG**, **MUTEX\_LOCK**, **MUTEX\_UNLOCK**, **g2c\_file\_info::num\_messages**, and **g2c\_file\_info::path**.

Referenced by **main()**.

### int g2c\_find\_msg2 (int g2cid, size\_t skip\_bytes, size\_t max\_bytes, size\_t \* bytes\_to\_msg, size\_t \* bytes\_in\_msg)

Search a file for the next GRIB1 or GRIB2 message. A grib message is identified by its indicator section, i.e. an 8-byte sequence with 'GRIB' in bytes 1-4 and a '1' or '2' in byte 8. If found, the length of the message is decoded from bytes 5-7. The search is done over a given section of the file. The search is terminated if an eof or i/o error is encountered.

**Parameters**

*g2cid* ID of the opened grib file, returned by **g2c\_open()**. \
 *skip\_bytes* Number of bytes to skip before search. \
 *max\_bytes* Maximum number of bytes to search. \
 *bytes\_to\_msg* Pointer that gets the number of bytes to skip before message. \
 *bytes\_in\_msg* Pointer that gets the number of bytes in message (or 0 if no message found)

**Returns**

- •

  **G2C\_NOERROR** No error.

- •

  **G2C\_EBADID** g2cid not found.

- •

  **G2C\_EFILE** File error.

- •

  **G2C\_EINVAL** Invalid input.

**Author**

Ed Hartnett

**Date**

2022-08-19

Definition at line **164** of file **g2cfile.c**.

References **G2C\_EBADID**, **G2C\_EFILE**, **G2C\_EINVAL**, **G2C\_EMSG**, **G2C\_ENOMEM**, **G2C\_ERROR**, **g2c\_file**, **G2C\_MAGIC\_HEADER\_LEN**, **G2C\_NOERROR**, **LOG**, **MIN**, and **READ\_BUF\_SIZE**.

### int g2c\_get\_msg (int g2cid, size\_t skip\_bytes, size\_t max\_bytes, size\_t \* bytes\_to\_msg, size\_t \* bytes\_in\_msg, unsigned char \*\* cbuf)

Search a file for the next GRIB1 or GRIB2 message, and read it, allocating space in memory to hold the message. A grib message is identified by its indicator section, i.e. an 8-byte sequence with 'GRIB' in bytes 1-4 and a '1' or '2' in byte 8. If found, the length of the message is decoded from bytes 5-7. The search is done over a given section of the file. The search is terminated if an EOF or I/O error is encountered.

**Parameters**

*g2cid* ID of the opened grib file, returned by **g2c\_open()**. \
 *skip\_bytes* The number of bytes to skip before search. \
 *max\_bytes* The maximum number of bytes to search. Must be at least 16. \
 *bytes\_to\_msg* A pointer that gets the number of bytes to skip before message. \
 *bytes\_in\_msg* A pointer that gets the number of bytes in message (or 0 if no message found) \
 *cbuf* A pointer that gets allocation of memory, into which the message is copied. This memory must be freed by the caller.

**Returns**

- •

  **G2C\_NOERROR** No error.

- •

  **G2C\_EBADID** g2cid not found.

- •

  **G2C\_EFILE** File error.

- •

  **G2C\_EINVAL** Invalid input.

- •

  **G2C\_ENOMEM** Out of memory.

- •

  **G2C\_ENOMSG** No GRIB message found.

**Author**

Ed Hartnett

**Date**

2022-08-20

Definition at line **291** of file **g2cfile.c**.

References **G2C\_EBADID**, **G2C\_EFILE**, **G2C\_EINVAL**, **G2C\_ENOMEM**, **G2C\_ENOMSG**, **G2C\_ERROR**, **g2c\_file**, **G2C\_MIN\_MAX\_BYTES**, **G2C\_NOERROR**, **LOG**, and **seekgb()**.

### int g2c\_open (const char \* path, int mode, int \* g2cid)

Open an existing GRIB2 file. This function opens the GRIB2 file and reads its metadata.

GRIB2 messages in the file are assigned a message ID, starting with 0 for the first message in the file.

Each product within a message is assigned a product ID, starting with 0 for the first product in the message.

Files opened with this function should be closed with a call **g2c\_close()** to release resources.

**Parameters**

*path* Path of the file. \
 *mode* Open mode flags. \
 *g2cid* Pointer that gets an indentifier for the file.

**Returns**

- •

  **G2C\_NOERROR** - No error.

- •

  **G2C\_EINVAL** - Invalid input.

- •

  **G2C\_ETOOMANYFILES** - Trying to open too many files at the same time.

**Author**

Ed Hartnett

**Date**

Aug 16, 2022

Definition at line **1201** of file **g2cfile.c**.

References **g2c\_add\_file()**, **LOG**, **MUTEX\_LOCK**, **MUTEX\_UNLOCK**, and **read\_metadata()**.

Referenced by **main()**.

### int g2c\_rw\_section1\_metadata (FILE \* f, int rw\_flag, G2C\_MESSAGE\_INFO\_T \* msg)

Read Section 1.

**Parameters**

*f* Pointer to open file. \
 *rw\_flag* **G2C\_FILE\_WRITE** if function should write, **G2C\_FILE\_READ** if it should read. \
 *msg* Pointer to G2C\_MESSAGE\_INFO\_T which will be populated with the values of section 0.

**Returns**

\-G2C\_NOERROR No error.

**Author**

Ed Hartnett

**Date**

10/16/22

Definition at line **874** of file **g2cfile.c**.

References **g2c\_message\_info::center**, **g2c\_message\_info::day**, **G2C\_ENOSECTION**, **g2c\_file\_io\_short()**, **g2c\_file\_io\_ubyte()**, **g2c\_file\_io\_uint()**, **G2C\_NOERROR**, **G2C\_SECTION1\_BYTES**, **g2c\_message\_info::hour**, **g2c\_message\_info::local\_version**, **LOG**, **g2c\_message\_info::master\_version**, **g2c\_message\_info::minute**, **g2c\_message\_info::month**, **g2c\_message\_info::sec1\_len**, **g2c\_message\_info::second**, **g2c\_message\_info::sig\_ref\_time**, **g2c\_message\_info::status**, **g2c\_message\_info::subcenter**, **g2c\_message\_info::type**, and **g2c\_message\_info::year**.

Referenced by **g2c\_open\_index()**, **g2c\_write\_index()**, and **read\_msg\_metadata()**.

### int g2c\_rw\_section3\_metadata (FILE \* f, int rw\_flag, G2C\_SECTION\_INFO\_T \* sec)

Read the metadata from section 3 (Grid Definition Section) of a GRIB2 message. When this function is called, the file cursor is positioned just after the section number field in the section. The size of the section, and the section number, have already been read when this function is called.

**Parameters**

*f* FILE pointer to open GRIB2 file. \
 *rw\_flag* **G2C\_FILE\_WRITE** if function should write, **G2C\_FILE\_READ** (0) if it should read. \
 *sec* Pointer to the G2C\_SECTION\_INFO\_T struct.

**Returns**

- •

  **G2C\_NOERROR** No error.

- •

  **G2C\_EINVAL** Invalid input.

- •

  **G2C\_ENOMEM** Out of memory.

- •

  **G2C\_ENOTEMPLATE** Can't find template.

**Author**

Ed Hartnett

**Date**

Sep 15, 2022

Definition at line **478** of file **g2cfile.c**.

References **determine\_dims()**, **G2C\_EINVAL**, **G2C\_ENOMEM**, **g2c\_file\_io\_template()**, **g2c\_file\_io\_ubyte()**, **g2c\_file\_io\_uint()**, **g2c\_file\_io\_ushort()**, **g2c\_get\_grid\_template()**, **G2C\_MAX\_GDS\_TEMPLATE\_MAPLEN**, **G2C\_NOERROR**, **g2c\_section3\_info::grid\_def**, **g2c\_section3\_info::interp\_list**, **LOG**, **g2c\_section3\_info::num\_data\_points**, **g2c\_section3\_info::num\_opt**, **g2c\_section\_info::sec\_info**, **g2c\_section\_info::sec\_num**, **g2c\_section3\_info::source\_grid\_def**, **g2c\_section\_info::template**, and **g2c\_section\_info::template\_len**.

Referenced by **add\_section()**, and **g2c\_write\_index()**.

### int g2c\_rw\_section4\_metadata (FILE \* f, int rw\_flag, G2C\_SECTION\_INFO\_T \* sec)

Read or write the metadata from section 4 (Product Definition Section) of a GRIB2 message. When this function is called, the file cursor is positioned just after the section number field in the section. The size of the section, and the section number, have already been read/written when this function is called.

**Parameters**

*f* FILE pointer to open GRIB2 file. \
 *rw\_flag* **G2C\_FILE\_WRITE** if function should write, **G2C\_FILE\_READ** if it should read. \
 *sec* Pointer to the G2C\_SECTION\_INFO\_T struct.

**Returns**

- •

  **G2C\_NOERROR** No error.

- •

  **G2C\_EINVAL** Invalid input.

- •

  **G2C\_ENOMEM** Out of memory.

- •

  **G2C\_ENOTEMPLATE** Can't find template.

**Author**

Ed Hartnett

**Date**

Sep 16, 2022

Definition at line **574** of file **g2cfile.c**.

References **G2C\_EINVAL**, **G2C\_ENOMEM**, **g2c\_file\_io\_template()**, **g2c\_file\_io\_ushort()**, **g2c\_get\_pds\_template()**, **G2C\_MAX\_PDS\_TEMPLATE\_MAPLEN**, **G2C\_NOERROR**, **LOG**, **g2c\_section\_info::msg**, **g2c\_section4\_info::num\_coord**, **g2c\_message\_info::num\_fields**, **g2c\_section4\_info::prod\_def**, **g2c\_section\_info::sec\_info**, **g2c\_section\_info::sec\_num**, **g2c\_section\_info::template**, and **g2c\_section\_info::template\_len**.

Referenced by **add\_section()**, and **g2c\_write\_index()**.

### int g2c\_rw\_section5\_metadata (FILE \* f, int rw\_flag, G2C\_SECTION\_INFO\_T \* sec)

Read or write the metadata from section 5 (Data Representation Section) of a GRIB2 message. When this function is called, the file cursor is positioned just after the section number field in the section. The size of the section, and the section number, have already been read when this function is called.

**Parameters**

*f* FILE pointer to open GRIB2 file. \
 *rw\_flag* **G2C\_FILE\_WRITE** if function should write, **G2C\_FILE\_READ** if it should read. \
 *sec* Pointer to the G2C\_SECTION\_INFO\_T struct.

**Returns**

- •

  **G2C\_NOERROR** No error.

- •

  **G2C\_EINVAL** Invalid input.

- •

  **G2C\_ENOMEM** Out of memory.

- •

  **G2C\_ENOTEMPLATE** Can't find template.

**Author**

Ed Hartnett

**Date**

Sep 16, 2022

Definition at line **663** of file **g2cfile.c**.

References **g2c\_section5\_info::data\_def**, **G2C\_EINVAL**, **G2C\_ENOMEM**, **g2c\_file\_io\_template()**, **g2c\_file\_io\_uint()**, **g2c\_file\_io\_ushort()**, **g2c\_get\_drs\_template()**, **G2C\_MAX\_PDS\_TEMPLATE\_MAPLEN**, **G2C\_NOERROR**, **LOG**, **g2c\_section5\_info::num\_data\_points**, **g2c\_section\_info::sec\_info**, **g2c\_section\_info::template**, and **g2c\_section\_info::template\_len**.

Referenced by **add\_section()**, and **g2c\_write\_index()**.

### int g2c\_rw\_section6\_metadata (FILE \* f, int rw\_flag, G2C\_SECTION\_INFO\_T \* sec)

Read or write the metadata from section 6 (Data Representation Section) of a GRIB2 message. When this function is called, the file cursor is positioned just after the section number field in the section. The size of the section, and the section number, have already been read when this function is called.

**Parameters**

*f* FILE pointer to open GRIB2 file. \
 *rw\_flag* **G2C\_FILE\_WRITE** if function should write, **G2C\_FILE\_READ** if it should read. \
 *sec* Pointer to the G2C\_SECTION\_INFO\_T struct.

**Returns**

- •

  **G2C\_NOERROR** No error.

- •

  **G2C\_EINVAL** Invalid input.

- •

  **G2C\_ENOMEM** Out of memory.

- •

  **G2C\_ENOTEMPLATE** Can't find template.

**Author**

Ed Hartnett

**Date**

Sep 16, 2022

Definition at line **745** of file **g2cfile.c**.

References **G2C\_EINVAL**, **G2C\_ENOMEM**, **g2c\_file\_io\_ubyte()**, **G2C\_NOERROR**, **g2c\_section6\_info::indicator**, **LOG**, and **g2c\_section\_info::sec\_info**.

Referenced by **add\_section()**.

### int g2c\_seekmsg (int g2cid, size\_t skip, size\_t \* offset, size\_t \* msglen)

Search a file for the next GRIB2 Message. The search is terminated when a GRIB2 message is found, or the end of the file is reached.

**Parameters**

*g2cid* ID of an open GRIB2 file, returned from **g2c\_open()**/g2c\_create(). \
 *skip* The number of bytes in the file to skip before starting the search. \
 *offset* Pointer that gets the number of bytes to skip from the beggining of the file to where the GRIB message starts. Ignored if NULL. \
 *msglen* Pointer that gets the number of bytes in message (set to 0, if no message found). Ignored if NULL.

**Returns**

- •

  **G2C\_NOERROR** No error.

- •

  **G2C\_EBADID** Bad g2cid.

- •

  **G2C\_ENOMEM** Out of memory.

**Author**

Ed Hartnett

**Date**

2022-09-11

Definition at line **56** of file **g2cfile.c**.

References **G2C\_EBADID**, **G2C\_EFILE**, **G2C\_ENOMEM**, **g2c\_file**, **G2C\_MAGIC\_HEADER**, **G2C\_NOERROR**, **G2C\_SEEKMSG\_BUFSIZE**, **hton64**, and **LOG**.

Referenced by **read\_metadata()**.

### MUTEX (m)

Define mutex for thread-safety.

### static int read\_metadata (int g2cid)` [static]`

Read metadata from a GRIB2 file being opened with **g2c\_open()**.

**Parameters**

*g2cid* The indentifier for the file.

**Returns**

- •

  **G2C\_NOERROR** - No error.

- •

  **G2C\_EBADID** g2cid not found.

- •

  **G2C\_EFILE** File error.

- •

  **G2C\_EINVAL** Invalid input.

- •

  **G2C\_ENOMEM** Out of memory.

- •

  **G2C\_ENOMSG** No GRIB message found.

**Author**

Ed Hartnett

**Date**

Aug 22, 2022

Definition at line **1079** of file **g2cfile.c**.

References **add\_msg()**, **G2C\_EBADID**, **G2C\_ENOMSG**, **g2c\_file**, **g2c\_log\_file()**, **G2C\_NOERROR**, **g2c\_seekmsg()**, and **LOG**.

Referenced by **g2c\_open()**.

### static int read\_msg\_metadata (G2C\_MESSAGE\_INFO\_T \* msg)` [static]`

Read the file to get metadata about a message.

**Parameters**

*msg* Pointer to the G2C\_MESSAGE\_INFO\_T struct for this message.

**Returns**

- •

  **G2C\_NOERROR** - No error.

**Author**

Ed Hartnett

**Date**

Sep 12, 2022

Definition at line **937** of file **g2cfile.c**.

References **add\_section()**, **BYTES\_TO\_DISCIPLINE**, **g2c\_message\_info::bytes\_to\_msg**, **g2c\_message\_info::discipline**, **g2c\_file\_info::f**, **g2c\_message\_info::file**, **FOUR\_BYTES**, **G2C\_EBADSECTION**, **G2C\_EFILE**, **g2c\_file\_io\_ubyte()**, **g2c\_file\_io\_uint()**, **G2C\_FILE\_READ**, **G2C\_NOERROR**, **g2c\_rw\_section1\_metadata()**, **G2C\_SECTION0\_BYTES**, **LOG**, **ONE\_BYTE**, and **g2c\_message\_info::sec1\_len**.

Referenced by **add\_msg()**.

## Variable Documentation

### G2C\_FILE\_INFO\_T g2c\_file\[G2C\_MAX\_FILES+1\]

Global file information.

Definition at line **10** of file **g2cfile.c**.

Referenced by **find\_available\_g2cid()**, **free\_metadata()**, **g2c\_add\_file()**, **g2c\_close()**, **g2c\_compare()**, **g2c\_degrib2()**, **g2c\_find\_msg2()**, **g2c\_get\_msg()**, **g2c\_get\_prod()**, **g2c\_inq()**, **g2c\_inq\_dim()**, **g2c\_inq\_msg()**, **g2c\_inq\_msg\_time()**, **g2c\_inq\_prod()**, **g2c\_log\_file()**, **g2c\_open\_index()**, **g2c\_seekmsg()**, **g2c\_write\_index()**, and **read\_metadata()**.

### int g2c\_next\_g2cid = 1

Next g2cid file ID - used when opening or creating a file.

Definition at line **13** of file **g2cfile.c**.

Referenced by **find\_available\_g2cid()**.

## Author

Generated automatically by Doxygen for NCEPLIBS-g2c from the source code.
