NCEPLIBS-w3emc  2.11.0
getgb1.f
Go to the documentation of this file.
1 C> @file
2 C> @brief Find and unpacks a grib message.
3 C> @author Mark Iredell @date 1994-04-01
4 
5 C> Find and unpack a grib message.
6 C> Read an associated grib index file (unless it already was read).
7 C> Find in the index file a reference to the grib message requested.
8 C> The grib message request specifies the number of messages to skip
9 C> and the unpacked pds and gds parameters. (A requested parameter
10 C> of -1 means to allow any value of this parameter to be found.)
11 C> If the requested grib message is found, then it is read from the
12 C> grib file and unpacked. Its message number is returned along with
13 C> the unpacked pds and gds parameters, the unpacked bitmap (if any),
14 C> and the unpacked data. If the grib message is not found, then the
15 C> return code will be nonzero.
16 C>
17 C> Program history log:
18 C> - Mark Iredell 1994-04-01
19 C> - Ralph Jones 1995-05-10 Add one more parameter to getgb and
20 C> change name to getgb1.
21 C>
22 C> @param[in] lugb logical unit of the unblocked grib data file.
23 C> @param[in] lugi logical unit of the unblocked grib index file.
24 C> @param[in] jf integer maximum number of data points to unpack.
25 C> @param[in] j integer number of messages to skip (=0 to search from beginning)
26 C> (<0 to reopen index file and search from beginning).
27 C> @param[in] jpds integer (25) pds parameters for which to search
28 C> (=-1 for wildcard) look in doc block of w3fi63 for array kpds
29 C> for list of order of unpacked pds values.
30 C> In most cases you only need to set 4 or 5 values to pick up record.
31 C> @param[in] jgds integer (22) gds parameters for which to search
32 C> (only searched if jpds(3)=255) (=-1 for wildcard).
33 C> @param[out] grib Grib data array before it is unpacked.
34 C> @param[out] kf Integer number of data points unpacked.
35 C> @param[out] k Integer message number unpacked
36 C> (can be same as j in calling program
37 C> in order to facilitate multiple searches).
38 C> @param[out] kpds Integer (25) unpacked pds parameters.
39 C> @param[out] kgds Integer (22) unpacked gds parameters.
40 C> @param[out] lb Logical (kf) unpacked bitmap if present.
41 C> @param[out] f Real (kf) unpacked data.
42 C> @param[out] iret Integer return code.
43 C> - 0 All ok.
44 C> - 96 Error reading index file.
45 C> - 97 Error reading grib file.
46 C> - 98 Number of data points greater than jf.
47 C> - 99 Request not found.
48 C> - other w3fi63 grib unpacker return code.
49 C>
50 C> @author Mark Iredell @date 1994-04-01
51  SUBROUTINE getgb1(LUGB,LUGI,JF,J,JPDS,JGDS,
52  & GRIB,KF,K,KPDS,KGDS,LB,F,IRET)
53 C
54  parameter(mbuf=8192*128)
55  parameter(lpds=23,lgds=22)
56 C
57  INTEGER JPDS(25),JGDS(*),KPDS(25),KGDS(*)
58  INTEGER IPDSP(LPDS),JPDSP(LPDS),IGDSP(LGDS)
59  INTEGER JGDSP(LGDS)
60  INTEGER KPTR(20)
61 C
62  LOGICAL LB(*)
63 C
64  REAL F(*)
65 C
66  CHARACTER CBUF(MBUF)
67  CHARACTER*81 CHEAD(2)
68  CHARACTER*1 CPDS(28)
69  CHARACTER*1 CGDS(42)
70  CHARACTER*1 GRIB(*)
71 C
72 C SAVE LUX,NSKP,NLEN,NNUM,CBUF
73  SAVE
74 C
75  DATA lux/0/
76 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77 C READ INDEX FILE
78  IF(j.LT.0.OR.lugi.NE.lux) THEN
79 C REWIND LUGI
80 C READ(LUGI,fmt='(2A81)',IOSTAT=IOS) CHEAD
81  CALL baread(lugi,0,162,ios,chead)
82  IF(ios.EQ.162.AND.chead(1)(42:47).EQ.'GB1IX1') THEN
83  lux=0
84  READ(chead(2),'(8X,3I10,2X,A40)',iostat=ios) nskp,nlen,nnum
85  IF(ios.EQ.0) THEN
86  nbuf=nnum*nlen
87  IF(nbuf.GT.mbuf) THEN
88  print *,'GETGB1: INCREASE BUFFER FROM ',mbuf,' TO ',nbuf
89  nnum=mbuf/nlen
90  nbuf=nnum*nlen
91  ENDIF
92  CALL baread(lugi,nskp,nbuf,lbuf,cbuf)
93  IF(lbuf.EQ.nbuf) THEN
94  lux=lugi
95  j=max(j,0)
96  ENDIF
97  ENDIF
98  ENDIF
99  ENDIF
100 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
101 C SEARCH FOR REQUEST
102  lgrib=0
103  kj=j
104  k=j
105  kf=0
106  IF(j.GE.0.AND.lugi.EQ.lux) THEN
107  lpdsp=0
108  DO i=1,lpds
109  IF(jpds(i).NE.-1) THEN
110  lpdsp=lpdsp+1
111  ipdsp(lpdsp)=i
112  jpdsp(lpdsp)=jpds(i)
113  ENDIF
114  ENDDO
115  lgdsp=0
116  IF(jpds(3).EQ.255) THEN
117  DO i=1,lgds
118  IF(jgds(i).NE.-1) THEN
119  lgdsp=lgdsp+1
120  igdsp(lgdsp)=i
121  jgdsp(lgdsp)=jgds(i)
122  ENDIF
123  ENDDO
124  ENDIF
125  iret=99
126  dowhile(lgrib.EQ.0.AND.kj.LT.nnum)
127  kj=kj+1
128  lt=0
129  IF(lpdsp.GT.0) THEN
130  cpds=cbuf((kj-1)*nlen+26:(kj-1)*nlen+53)
131  kptr=0
132  CALL gbyte(cbuf,kptr(3),(kj-1)*nlen*8+25*8,3*8)
133  CALL fi632(cpds,kptr,kpds,iret)
134  DO i=1,lpdsp
135  ip=ipdsp(i)
136  lt=lt+abs(jpds(ip)-kpds(ip))
137  ENDDO
138  ENDIF
139  IF(lt.EQ.0.AND.lgdsp.GT.0) THEN
140  cgds=cbuf((kj-1)*nlen+54:(kj-1)*nlen+95)
141  kptr=0
142  CALL fi633(cgds,kptr,kgds,iret)
143  DO i=1,lgdsp
144  ip=igdsp(i)
145  lt=lt+abs(jgds(ip)-kgds(ip))
146  ENDDO
147  ENDIF
148 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
149 C READ AND UNPACK GRIB DATA
150  IF(lt.EQ.0) THEN
151  CALL gbyte(cbuf,lskip,(kj-1)*nlen*8,4*8)
152  CALL gbyte(cbuf,lgrib,(kj-1)*nlen*8+20*8,4*8)
153  cgds=cbuf((kj-1)*nlen+54:(kj-1)*nlen+95)
154  kptr=0
155  CALL fi633(cgds,kptr,kgds,iret)
156 C BSM IF(LGRIB.LE.200+17*JF/8.AND.KGDS(2)*KGDS(3).LE.JF) THEN
157 C Change number of bits that can be handled to 25
158  IF(lgrib.LE.200+25*jf/8.AND.kgds(2)*kgds(3).LE.jf) THEN
159  CALL baread(lugb,lskip,lgrib,lread,grib)
160  IF(lread.EQ.lgrib) THEN
161  CALL w3fi63(grib,kpds,kgds,lb,f,kptr,iret)
162  IF(iret.EQ.0) THEN
163  k=kj
164  kf=kptr(10)
165  ENDIF
166  ELSE
167  iret=97
168  ENDIF
169  ELSE
170  iret=98
171  ENDIF
172  ENDIF
173  ENDDO
174  ELSE
175  iret=96
176  ENDIF
177 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
178  RETURN
179  END
subroutine gbyte(IPACKD, IUNPKD, NOFF, NBITS)
This is the fortran version of gbyte.
Definition: gbyte.f:27
subroutine getgb1(LUGB, LUGI, JF, J, JPDS, JGDS, GRIB, KF, K, KPDS, KGDS, LB, F, IRET)
Find and unpack a grib message.
Definition: getgb1.f:53
subroutine fi632(MSGA, KPTR, KPDS, KRET)
Gather info from product definition sec.
Definition: w3fi63.f:635
subroutine w3fi63(MSGA, KPDS, KGDS, KBMS, DATA, KPTR, KRET)
Unpack a GRIB (edition 1) field to the exact grid specified in the GRIB message, isolate the bit map,...
Definition: w3fi63.f:243
subroutine fi633(MSGA, KPTR, KGDS, KRET)
Extract info from grib-gds.
Definition: w3fi63.f:981