Loading...
Searching...
No Matches
seekgb.c
Go to the documentation of this file.
#include "grib2_int.h"
#include <stdio.h>
#include <stdlib.h>
void
seekgb(FILE *lugb, g2int iseek, g2int mseek, g2int *lskip, g2int *lgrib)
{
g2int k, k4, ipos, nread, lim, start, vers, lengrib;
int end;
unsigned char *cbuf;
LOG((3, "seekgb iseek %ld mseek %ld", iseek, mseek));
*lgrib = 0;
cbuf = (unsigned char *)malloc(mseek);
nread = mseek;
ipos = iseek;
/* Loop until grib message is found. */
while (*lgrib == 0 && nread == mseek)
{
/* Read partial section. */
fseek(lugb, ipos, SEEK_SET);
nread = fread(cbuf, sizeof(unsigned char), mseek, lugb);
lim = nread - 8;
/* Look for 'grib...' in partial section. */
for (k = 0; k < lim; k++)
{
/* Look at the first 4 bytes - should be 'GRIB'. */
gbit(cbuf, &start, k * BYTE, 4 * BYTE);
/* Look at the 8th byte, it has the GRIB version. */
gbit(cbuf, &vers, (k + 7) * BYTE, 1 * BYTE);
/* If the message starts with 'GRIB', and is version 1 or
* 2, then this is a GRIB message. */
if (start == 1196575042 && (vers == 1 || vers == 2))
{
/* Find the length of the message. */
if (vers == 1)
gbit(cbuf, &lengrib, (k + 4) * BYTE, 3 * BYTE);
if (vers == 2)
gbit(cbuf, &lengrib, (k + 12) * BYTE, 4 * BYTE);
LOG((4, "lengrib %ld", lengrib));
/* Read the last 4 bytesof the message. */
fseek(lugb, ipos + k + lengrib - 4, SEEK_SET);
k4 = fread(&end, 4, 1, lugb);
/* Look for '7777' at end of grib message. */
if (k4 == 1 && end == 926365495)
{
/* GRIB message found. */
*lskip = ipos + k;
*lgrib = lengrib;
LOG((4, "found end of message lengrib %ld", lengrib));
break;
}
}
}
ipos = ipos + lim;
}
free(cbuf);
}
gbit
void gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbits)
Get arbitrary size values from a packed bit string, right justifying each value in the unpacked iout ...
Definition gbits.c:20
g2int
int64_t g2int
Long integer type.
Definition grib2.h:32
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
LOG
#define LOG(e)
Ignore logging to stdout.
Definition grib2_int.h:428
BYTE
#define BYTE
Number of bits in a byte.
Definition grib2_int.h:47
seekgb
void seekgb(FILE *lugb, g2int iseek, g2int mseek, g2int *lskip, g2int *lgrib)
Search a file for the next GRIB Message.
Definition seekgb.c:46