NCEPLIBS-g2  3.4.5
getgb2p.f
Go to the documentation of this file.
1 C> @file
2 C> @brief This subroutine find and extracts a grib message from a file.
3 C> @author Mark Iredell @date 1994-04-01
4 C>
5 
6 C> This subroutine find and extracts a grib message from a file.
7 C> It reads a grib index file (or optionally the grib file itself) to
8 C> get the index buffer (i.e. table of contents) for the grib file.
9 C> find in the index buffer a reference to the grib field requested.
10 C> the grib field request specifies the number of fields to skip
11 C> and the unpacked identification section, grid definition template
12 C> and product defintion section parameters. (a requested parameter
13 C> of -9999 means to allow any value of this parameter to be found.)
14 C> if the requested grib field is found, then it is read from the
15 C> grib file and unpacked. If the grib field is not found, then the
16 C> return code will be nonzero.
17 C>
18 C> PROGRAM HISTORY LOG:
19 C> - 1994-04-01 Mark Iredell
20 C> - 1995-10-31 Mark Iredell modularized portions of code into subprograms
21 C> and allowed for unspecified index file
22 C> - 2002-01-11 Stephen Gilbert modified from getgb and getgbm to work with grib2
23 C> - 2003-12-17 Stephen Gilbert modified from getgb2 to return packed grib2 message
24 C> @param[in] LUGB integer unit of the unblocked grib data file.
25 C> file must be opened with baopen or baopenr before calling
26 C> this routine.
27 C> @param[in] LUGI integer unit of the unblocked grib index file.
28 C> if nonzero, file must be opened with baopen baopenr before
29 C> calling this routine. (=0 to get index buffer from the grib file)
30 C> @param[in] J integer number of fields to skip
31 C> (=0 to search from beginning)
32 C> @param[in] JDISC grib2 discipline number of requested field
33 C> (if = -1, accept any discipline see code table 0.0)
34 C> - 0 meteorological products
35 C> - 1 hydrological products
36 C> - 2 land surface products
37 C> - 3 space products
38 C> - 10 oceanographic products
39 C> @param[in] JIDS integer array of values in the identification section
40 C> (=-9999 for wildcard)
41 C> - JIDS(1) identification of originating centre
42 C> (see common code table c-1)
43 C> - JIDS(2) identification of originating sub-centre
44 C> - JIDS(3) grib master tables version number
45 C> (see code table 1.0) 0 experimental;1 initial operational version number.
46 C> - JIDS(4) grib local tables version number (see code table 1.1)
47 C> 0 local tables not used; 1-254 number of local tables version used.
48 C> - JIDS(5) significance of reference time (code table 1.2)
49 C> 0 analysis; 1 start of forecast; 2 verifying time of forecast; 3 observation time
50 C> - JIDS(6) year (4 digits)
51 C> - JIDS(7) month
52 C> - JIDS(8) day
53 C> - JIDS(9) hour
54 C> - JIDS(10) minute
55 C> - JIDS(11) second
56 C> - JIDS(12) production status of processed data (see code table 1.3)
57 C> 0 operational products; 1 operational test products;
58 C> 2 research products; 3 re-analysis products.
59 C> - JIDS(13) type of processed data (see code table 1.4)
60 C> 0 analysis products; 1 forecast products; 2 analysis and forecast
61 C> products; 3 control forecast products; 4 perturbed forecast products;
62 C> 5 control and perturbed forecast products; 6 processed satellite
63 C> observations; 7 processed radar observations.
64 C> @param[in] JPDTN integer product definition template number (n)
65 C> (if = -1, don't bother matching pdt - accept any)
66 C> @param[in] JPDT integer array of values defining the product definition
67 C> template 4.n of the field for which to search (=-9999 for wildcard)
68 C> @param[in] JGDTN integer grid definition template number (m)
69 C> (if = -1, don't bother matching gdt - accept any )
70 C> @param[in] JGDT integer array of values defining the grid definition
71 C> template 3.m of the field for which to search (=-9999 for wildcard)
72 C> @param[in] EXTRACT logical value indicating whether to return a
73 C> grib2 message with just the requested field, or the entire
74 C> grib2 message containing the requested field.
75 C> - .true. = return grib2 message containing only the requested field.
76 C> - .false. = return entire grib2 message containing the requested field.
77 C> @param[out] K integer field number unpacked.
78 C> @param[out] GRIBM returned grib message.
79 C> @param[out] LENG length of returned grib message in bytes.
80 C> @param[out] IRET integer return code
81 C> - 0 all ok
82 C> - 96 error reading index
83 C> - 97 error reading grib file
84 C> - 99 request not found
85 C> @note specify an index file if feasible to increase speed.
86 C> do not engage the same logical unit from more than one processor.
87 C> Note that derived type gribfield contains pointers to many
88 C> arrays of data. The memory for these arrays is allocated
89 C> when the values in the arrays are set, to help minimize
90 C> problems with array overloading. Because of this users are
91 C> encouraged to free up this memory, when it is no longer
92 C> needed, by an explicit call to subroutine gf_free.
93 C>
94 C> @author Mark Iredell @date 1994-04-01
95 C>
96 
97 C-----------------------------------------------------------------------
98  SUBROUTINE getgb2p(LUGB,LUGI,J,JDISC,JIDS,JPDTN,JPDT,JGDTN,JGDT,
99  & EXTRACT,K,GRIBM,LENG,IRET)
100 
101  USE grib_mod
102 
103  INTEGER,INTENT(IN) :: LUGB,LUGI,J,JDISC,JPDTN,JGDTN
104  INTEGER,DIMENSION(:) :: JIDS(*),JPDT(*),JGDT(*)
105  LOGICAL,INTENT(IN) :: EXTRACT
106  INTEGER,INTENT(OUT) :: K,IRET,LENG
107  CHARACTER(LEN=1),POINTER,DIMENSION(:) :: GRIBM
108 
109  TYPE(gribfield) :: GFLD
110 
111  CHARACTER(LEN=1),POINTER,DIMENSION(:) :: CBUF
112  parameter(msk1=32000,msk2=4000)
113 
114  SAVE cbuf,nlen,nnum
115  DATA lux/0/
116 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
117 C DECLARE INTERFACES (REQUIRED FOR CBUF POINTER)
118  INTERFACE
119  SUBROUTINE getg2i(LUGI,CBUF,NLEN,NNUM,IRET)
120  CHARACTER(LEN=1),POINTER,DIMENSION(:) :: CBUF
121  INTEGER,INTENT(IN) :: LUGI
122  INTEGER,INTENT(OUT) :: NLEN,NNUM,IRET
123  END SUBROUTINE getg2i
124  SUBROUTINE getg2ir(LUGB,MSK1,MSK2,MNUM,CBUF,NLEN,NNUM,
125  & NMESS,IRET)
126  CHARACTER(LEN=1),POINTER,DIMENSION(:) :: CBUF
127  INTEGER,INTENT(IN) :: LUGB,MSK1,MSK2,MNUM
128  INTEGER,INTENT(OUT) :: NLEN,NNUM,NMESS,IRET
129  END SUBROUTINE getg2ir
130  SUBROUTINE getgb2rp(LUGB,CINDEX,EXTRACT,GRIBM,LENG,IRET)
131  INTEGER,INTENT(IN) :: LUGB
132  CHARACTER(LEN=1),INTENT(IN) :: CINDEX(*)
133  LOGICAL,INTENT(IN) :: EXTRACT
134  INTEGER,INTENT(OUT) :: LENG,IRET
135  CHARACTER(LEN=1),POINTER,DIMENSION(:) :: GRIBM
136  END SUBROUTINE getgb2rp
137  END INTERFACE
138 
139 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
140 C DETERMINE WHETHER INDEX BUFFER NEEDS TO BE INITIALIZED
141  irgi=0
142  IF(lugi.GT.0.AND.lugi.NE.lux) THEN
143  CALL getg2i(lugi,cbuf,nlen,nnum,irgi)
144  lux=lugi
145  ELSEIF(lugi.LE.0.AND.lugb.NE.lux) THEN
146  mskp=0
147  CALL getg2ir(lugb,msk1,msk2,mskp,cbuf,nlen,nnum,nmess,irgi)
148  lux=lugb
149  ENDIF
150  IF(irgi.GT.1) THEN
151  iret=96
152  lux=0
153  RETURN
154  ENDIF
155 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
156 C SEARCH INDEX BUFFER
157  CALL getgb2s(cbuf,nlen,nnum,j,jdisc,jids,jpdtn,jpdt,jgdtn,jgdt,
158  & jk,gfld,lpos,irgs)
159  IF(irgs.NE.0) THEN
160  iret=99
161  CALL gf_free(gfld)
162  RETURN
163  ENDIF
164 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
165 C EXTRACT GRIB MESSAGE FROM FILE
166  CALL getgb2rp(lugb,cbuf(lpos:),extract,gribm,leng,iret)
167 ! IF ( EXTRACT ) THEN
168 ! PRINT *,'NOT SUPPOSED TO BE HERE.'
169 ! ELSE
170 ! IPOS=(LPOS+3)*8
171 ! CALL G2_GBYTEC(CBUF,ISKIP,IPOS,32) ! BYTES TO SKIP IN FILE
172 ! IPOS=IPOS+(32*8)
173 ! CALL G2_GBYTEC(CBUF,LENG,IPOS,32) ! LENGTH OF GRIB MESSAGE
174 ! IF (.NOT. ASSOCIATED(GRIBM)) ALLOCATE(GRIBM(LENG))
175 ! CALL BAREAD(LUGB,ISKIP,LENG,LREAD,GRIBM)
176 ! IF ( LENG .NE. LREAD ) THEN
177 ! IRET=97
178 ! CALL GF_FREE(GFLD)
179 ! RETURN
180 ! ENDIF
181 ! ENDIF
182 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
183  k=jk
184  CALL gf_free(gfld)
185 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
186  RETURN
187  END
getg2i
subroutine getg2i(LUGI, CBUF, NLEN, NNUM, IRET)
READ A GRIB2 INDEX FILE AND RETURN ITS CONTENTS.
Definition: getg2i.f:57
getgb2s
subroutine getgb2s(CBUF, NLEN, NNUM, J, JDISC, JIDS, JPDTN, JPDT, JGDTN, JGDT, K, GFLD, LPOS, IRET)
This subroutine find in the index file for a reference to the grib field requested.
Definition: getgb2s.f:245
grib_mod::gribfield
Definition: gribmod.f:155
grib_mod
PROGRAM HISTORY LOG:
Definition: gribmod.f:151
getgb2rp
subroutine getgb2rp(LUGB, CINDEX, EXTRACT, GRIBM, LENG, IRET)
This subroutine find and extracts a grib message from a file given the index for the requested field.
Definition: getgb2rp.f:36
getgb2p
subroutine getgb2p(LUGB, LUGI, J, JDISC, JIDS, JPDTN, JPDT, JGDTN, JGDT, EXTRACT, K, GRIBM, LENG, IRET)
This subroutine find and extracts a grib message from a file.
Definition: getgb2p.f:100
getg2ir
subroutine getg2ir(LUGB, MSK1, MSK2, MNUM, CBUF, NLEN, NNUM, NMESS, IRET)
This subroutine read a GRIB file and return its index content.
Definition: getg2ir.f:60
gf_free
subroutine gf_free(gfld)
This subroutine frees up memory that was used to store array values in derived type gribfield.
Definition: gf_free.f:139