NCEPLIBS-g2  3.4.5
getgb2rp.f
Go to the documentation of this file.
1 C> @file
2 C> @brief This subroutine find and extracts the index for the
3 C> requested field from a grib file.
4 C> @author Stephen Gilbert @date 2003-12-31
5 C>
6 
7 C> This subroutine find and extracts a grib message from a file given
8 C> the index for the requested field. The grib message returned can
9 C> contain only the requested field (extract=.true.). or the complete
10 C> grib message originally containing the desired field can be
11 C> returned (extract=.false.) even if other fields were included in
12 C> the grib message. If the grib field is not found, then the return
13 C> code will be nonzero.
14 
15 C> @param[in] LUGB integer unit of the unblocked grib data file.
16 C> file must be opened with baopen or baopenr before calling
17 C> this routine.
18 C> @param[in] CINDEX index record of the grib field (see docblock of
19 C> subroutine ixgb2() for description of an index record.)
20 C> @param[in] EXTRACT logical value indicating whether to return a
21 C> grib2 message with just the requested field, or the entire
22 C> grib2 message containing the requested field.
23 C> - .true. = return grib2 message containing only the requested field.
24 C> - .false. = return entire grib2 message containing the requested field.
25 C> @param[out] GRIBM returned grib message.
26 C> @param[out] LENG length of returned grib message in bytes.
27 C> @param[out] IRET integer return code
28 C> - 0 all ok
29 C> - 97 error reading grib file
30 C>
31 C> @author Stephen Gilbert @date 2003-12-31
32 C>
33 
34 C-----------------------------------------------------------------------
35  SUBROUTINE getgb2rp(LUGB,CINDEX,EXTRACT,GRIBM,LENG,IRET)
36 
37  INTEGER,INTENT(IN) :: LUGB
38  CHARACTER(LEN=1),INTENT(IN) :: CINDEX(*)
39  LOGICAL,INTENT(IN) :: EXTRACT
40  INTEGER,INTENT(OUT) :: LENG,IRET
41  CHARACTER(LEN=1),POINTER,DIMENSION(:) :: GRIBM
42 
43  INTEGER,PARAMETER :: ZERO=0
44  CHARACTER(LEN=1),ALLOCATABLE,DIMENSION(:) :: CSEC2,CSEC6,CSEC7
45  CHARACTER(LEN=4) :: Ctemp
46 
47  iret=0
48 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49 C EXTRACT GRIB MESSAGE FROM FILE
50  IF ( extract ) THEN
51  len0=16
52  len8=4
53  CALL g2_gbytec(cindex,iskip,4*8,4*8) ! BYTES TO SKIP IN FILE
54  CALL g2_gbytec(cindex,iskp2,8*8,4*8) ! BYTES TO SKIP FOR section 2
55  if ( iskp2 .gt. 0 ) then
56  CALL baread(lugb,iskip+iskp2,4,lread,ctemp)
57  CALL g2_gbytec(ctemp,len2,0,4*8) ! LENGTH OF SECTION 2
58  ALLOCATE(csec2(len2))
59  CALL baread(lugb,iskip+iskp2,len2,lread,csec2)
60  else
61  len2=0
62  endif
63  CALL g2_gbytec(cindex,len1,44*8,4*8) ! LENGTH OF SECTION 1
64  ipos=44+len1
65  CALL g2_gbytec(cindex,len3,ipos*8,4*8) ! LENGTH OF SECTION 3
66  ipos=ipos+len3
67  CALL g2_gbytec(cindex,len4,ipos*8,4*8) ! LENGTH OF SECTION 4
68  ipos=ipos+len4
69  CALL g2_gbytec(cindex,len5,ipos*8,4*8) ! LENGTH OF SECTION 5
70  ipos=ipos+len5
71  CALL g2_gbytec(cindex,len6,ipos*8,4*8) ! LENGTH OF SECTION 6
72  ipos=ipos+5
73  CALL g2_gbytec(cindex,ibmap,ipos*8,1*8) ! Bitmap indicator
74  IF ( ibmap .eq. 254 ) THEN
75  CALL g2_gbytec(cindex,iskp6,24*8,4*8) ! BYTES TO SKIP FOR section 6
76  CALL baread(lugb,iskip+iskp6,4,lread,ctemp)
77  CALL g2_gbytec(ctemp,len6,0,4*8) ! LENGTH OF SECTION 6
78  ENDIF
79  !
80  ! READ IN SECTION 7 from file
81  !
82  CALL g2_gbytec(cindex,iskp7,28*8,4*8) ! BYTES TO SKIP FOR section 7
83  CALL baread(lugb,iskip+iskp7,4,lread,ctemp)
84  CALL g2_gbytec(ctemp,len7,0,4*8) ! LENGTH OF SECTION 7
85  ALLOCATE(csec7(len7))
86  CALL baread(lugb,iskip+iskp7,len7,lread,csec7)
87 
88  leng=len0+len1+len2+len3+len4+len5+len6+len7+len8
89  IF (.NOT. ASSOCIATED(gribm)) ALLOCATE(gribm(leng))
90 
91  ! Create Section 0
92  !
93  gribm(1)='G'
94  gribm(2)='R'
95  gribm(3)='I'
96  gribm(4)='B'
97  gribm(5)=char(0)
98  gribm(6)=char(0)
99  gribm(7)=cindex(42)
100  gribm(8)=cindex(41)
101  gribm(9)=char(0)
102  gribm(10)=char(0)
103  gribm(11)=char(0)
104  gribm(12)=char(0)
105  CALL g2_sbytec(gribm,leng,12*8,4*8)
106  !
107  ! Copy Section 1
108  !
109  gribm(17:16+len1)=cindex(45:44+len1)
110  lencur=16+len1
111  ipos=44+len1
112  !
113  ! Copy Section 2, if necessary
114  !
115  if ( iskp2 .gt. 0 ) then
116  gribm(lencur+1:lencur+len2)=csec2(1:len2)
117  lencur=lencur+len2
118  endif
119  !
120  ! Copy Sections 3 through 5
121  !
122  gribm(lencur+1:lencur+len3+len4+len5)=
123  & cindex(ipos+1:ipos+len3+len4+len5)
124  lencur=lencur+len3+len4+len5
125  ipos=ipos+len3+len4+len5
126  !
127  ! Copy Section 6
128  !
129  if ( len6 .eq. 6 .AND. ibmap .ne. 254 ) then
130  gribm(lencur+1:lencur+len6)=cindex(ipos+1:ipos+len6)
131  lencur=lencur+len6
132  else
133  CALL g2_gbytec(cindex,iskp6,24*8,4*8) ! BYTES TO SKIP FOR section 6
134  CALL baread(lugb,iskip+iskp6,4,lread,ctemp)
135  CALL g2_gbytec(ctemp,len6,0,4*8) ! LENGTH OF SECTION 6
136  ALLOCATE(csec6(len6))
137  CALL baread(lugb,iskip+iskp6,len6,lread,csec6)
138  gribm(lencur+1:lencur+len6)=csec6(1:len6)
139  lencur=lencur+len6
140  IF ( allocated(csec6)) DEALLOCATE(csec6)
141  endif
142  !
143  ! Copy Section 7
144  !
145  gribm(lencur+1:lencur+len7)=csec7(1:len7)
146  lencur=lencur+len7
147  !
148  ! Section 8
149  !
150  gribm(lencur+1)='7'
151  gribm(lencur+2)='7'
152  gribm(lencur+3)='7'
153  gribm(lencur+4)='7'
154 
155  ! clean up
156  !
157  IF ( allocated(csec2)) DEALLOCATE(csec2)
158  IF ( allocated(csec7)) deallocate(csec7)
159 
160  ELSE ! DO NOT extract field from message : Get entire message
161 
162  CALL g2_gbytec(cindex,iskip,4*8,4*8) ! BYTES TO SKIP IN FILE
163  CALL g2_gbytec(cindex,leng,36*8,4*8) ! LENGTH OF GRIB MESSAGE
164  IF (.NOT. ASSOCIATED(gribm)) ALLOCATE(gribm(leng))
165  CALL baread(lugb,iskip,leng,lread,gribm)
166  IF ( leng .NE. lread ) THEN
167  DEALLOCATE(gribm)
168  NULLIFY(gribm)
169  iret=97
170  RETURN
171  ENDIF
172  ENDIF
173 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
174  RETURN
175  END
g2_sbytec
subroutine g2_sbytec(OUT, IN, ISKIP, NBYTE)
This subrountine is to put arbitrary size values into a packed bit string, taking the low order bits ...
Definition: g2_gbytesc.f:39
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
g2_gbytec
subroutine g2_gbytec(IN, IOUT, ISKIP, NBYTE)
This subrountine is to extract arbitrary size values from a packed bit string, right justifying each ...
Definition: g2_gbytesc.f:20