NCEPLIBS-w3emc  2.11.0
skgb.f
Go to the documentation of this file.
1 C> @file
2 C> @brief Search for next grib message.
3 C> @author Mark Iredell @date 1993-11-22
4 
5 C> This subprogram searches a file for the next grib 1 message.
6 C> A grib 1 message is identified by its indicator section, i.e.
7 C> an 8-byte sequence with 'grib' in bytes 1-4 and 1 in byte 8.
8 C> If found, the length of the message is decoded from bytes 5-7.
9 C> The search is done over a given section of the file.
10 C> The search is terminated if an eof or i/o error is encountered.
11 C>
12 C> Program history log:
13 C> - Mark Iredell 1993-11-22
14 C> - Mark Iredell 1995-10-31 Add call to baread.
15 C> - Mark Iredell 1997-03-14 Check for '7777'.
16 C> - Stephen Gilbert 2001-12-05 Modified to also look for grib2 messages.
17 C>
18 C> @param[in] LUGB Integer logical unit of input grib file.
19 C> @param[in] ISEEK Integer number of bytes to skip before search.
20 C> @param[in] MSEEK Integer maximum number of bytes to search.
21 C> @param[out] LSKIP Integer number of bytes to skip before message.
22 C> @param[out] LGRIB Integer number of bytes in message (0 if not found).
23 C>
24 C> @author Mark Iredell @date 1993-11-22
25 C-----------------------------------------------------------------------
26  SUBROUTINE skgb(LUGB,ISEEK,MSEEK,LSKIP,LGRIB)
27  parameter(lseek=128)
28  CHARACTER Z(LSEEK)
29  CHARACTER Z4(4)
30 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
31  lgrib=0
32  ks=iseek
33  kn=min(lseek,mseek)
34  kz=lseek
35 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
36 C LOOP UNTIL GRIB MESSAGE IS FOUND
37  dowhile(lgrib.EQ.0.AND.kn.GE.8.AND.kz.EQ.lseek)
38 C READ PARTIAL SECTION
39  CALL baread(lugb,ks,kn,kz,z)
40  km=kz-8+1
41  k=0
42 C LOOK FOR 'GRIB...1' IN PARTIAL SECTION
43  dowhile(lgrib.EQ.0.AND.k.LT.km)
44  CALL gbytec(z,i4,(k+0)*8,4*8)
45  CALL gbytec(z,i1,(k+7)*8,1*8)
46  IF(i4.EQ.1196575042.AND.(i1.EQ.1.OR.i1.EQ.2)) THEN
47 C LOOK FOR '7777' AT END OF GRIB MESSAGE
48  IF (i1.EQ.1) CALL gbytec(z,kg,(k+4)*8,3*8)
49  IF (i1.EQ.2) CALL gbytec(z,kg,(k+12)*8,4*8)
50  CALL baread(lugb,ks+k+kg-4,4,k4,z4)
51  IF(k4.EQ.4) THEN
52  CALL gbytec(z4,i4,0,4*8)
53  IF(i4.EQ.926365495) THEN
54 C GRIB MESSAGE FOUND
55  lskip=ks+k
56  lgrib=kg
57  ENDIF
58  ENDIF
59  ENDIF
60  k=k+1
61  ENDDO
62  ks=ks+km
63  kn=min(lseek,iseek+mseek-ks)
64  ENDDO
65 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
66  RETURN
67  END
subroutine gbytec(IN, IOUT, ISKIP, NBYTE)
Wrapper for gbytesc() limiting NSKIP and N to 0 and 1.
Definition: gbytec.f:14
subroutine skgb(LUGB, ISEEK, MSEEK, LSKIP, LGRIB)
This subprogram searches a file for the next grib 1 message.
Definition: skgb.f:27