NCEPLIBS-g2c  1.6.4
seekgb.c
Go to the documentation of this file.
1 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "grib2.h"
8 
32 void seekgb(FILE *lugb,g2int iseek,g2int mseek,g2int *lskip,g2int *lgrib)
33 {
34  g2int k,k4,ipos,nread,lim,start,vers,lengrib;
35  int end;
36  unsigned char *cbuf;
37 
38 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39  *lgrib=0;
40  cbuf=(unsigned char *)malloc(mseek);
41  nread=mseek;
42  ipos=iseek;
43 
44 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
45 // LOOP UNTIL GRIB MESSAGE IS FOUND
46 
47  while (*lgrib==0 && nread==mseek) {
48 
49 // READ PARTIAL SECTION
50 
51  fseek(lugb,ipos,SEEK_SET);
52  nread=fread(cbuf,sizeof(unsigned char),mseek,lugb);
53  lim=nread-8;
54 
55 // LOOK FOR 'GRIB...' IN PARTIAL SECTION
56 
57  for (k=0;k<lim;k++) {
58  gbit(cbuf,&start,(k+0)*8,4*8);
59  gbit(cbuf,&vers,(k+7)*8,1*8);
60  if (start==1196575042 && (vers==1 || vers==2)) {
61 // LOOK FOR '7777' AT END OF GRIB MESSAGE
62  if (vers == 1) gbit(cbuf,&lengrib,(k+4)*8,3*8);
63  if (vers == 2) gbit(cbuf,&lengrib,(k+12)*8,4*8);
64  fseek(lugb,ipos+k+lengrib-4,SEEK_SET);
65 // Hard code to 4 instead of sizeof(g2int)
66  k4=fread(&end,4,1,lugb);
67  if (k4 == 1 && end == 926365495) { //GRIB message found
68  *lskip=ipos+k;
69  *lgrib=lengrib;
70  break;
71  }
72  }
73  }
74  ipos=ipos+lim;
75  }
76 
77  free(cbuf);
78 }
void gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbyte)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
Definition: gbits.c:20
Header file for NCEPLIBS-g2c library.
int64_t g2int
Long integer type.
Definition: grib2.h:20
void seekgb(FILE *lugb, g2int iseek, g2int mseek, g2int *lskip, g2int *lgrib)
This subprogram searches a file for the next GRIB Message.
Definition: seekgb.c:32