NCEPLIBS-g2  3.4.5
getidx.f
Go to the documentation of this file.
1 C> @file
2 C> @brief This subroutine finds, reads or generates a grib2 index for
3 C> the grib2 file associated with unit lugb.
4 C> @author Stephen Gilbert @date 2005-03-15
5 C>
6 
7 C> This subroutine finds, reads or generates a grib2 index for
8 C> the grib2 file associated with unit lugb. If the index already
9 C> exists, it is returned. otherwise, the index is (1) read from an
10 C> existing indexfile associated with unit LUGI. or (2) generated
11 C> from the grib2file LUGI. Users can force a regeneration of an
12 C> index. If LUGI equals LUGB, the index will be regenerated from
13 C> the data in file LUGB. If LUGI is less than zero, then the index
14 C> is re read from index file abs(lugi).
15 C>
16 C> PROGRAM HISTORY LOG:
17 C> - 2005-03-15 Stephen Gilbert Initial Programming
18 C> - 2009-07-09 Boi Vuong Fixed bug for checking (LUGB) unit index file
19 C> - 2016-03-29 Boi Vuong Restore original getidx.f from version
20 C> 1.2.3 modified getidex to allow to open range of unit file number
21 C> up to 9999 added new parameters and new product definition
22 C> template numbers: 4.60, 4.61
23 C>
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[out] CINDEX character*1 pointer to a buffer that contains
31 C> index records.
32 C> @param[out] NLEN integer total length of all index records
33 C> @param[out] NNUM integer number of index records
34 C> @param[out] IRET integer return code
35 C> - 0 all ok
36 C> - 90 unit number out of range
37 C> - 96 error reading/creating index file
38 C>
39 C> @note allow file unit numbers in range 0 - 9999
40 C> the grib index will automatically generate the index file.
41 C>
42 C> @author Stephen Gilbert @date 2005-03-15
43 C>
44 
45 C-----------------------------------------------------------------------
46  SUBROUTINE getidx(LUGB,LUGI,CINDEX,NLEN,NNUM,IRET)
47 
48  INTEGER,INTENT(IN) :: LUGB,LUGI
49  INTEGER,INTENT(OUT) :: NLEN,NNUM,IRET
50  CHARACTER(LEN=1),POINTER,DIMENSION(:) :: CINDEX
51 
52  INTEGER,PARAMETER :: MAXIDX=10000
53  INTEGER,PARAMETER :: MSK1=32000,msk2=4000
54 
55  TYPE gindex
56  integer :: nlen
57  integer :: nnum
58  character(len=1),pointer,dimension(:) :: cbuf
59  END TYPE gindex
60 
61  TYPE(gindex), save :: IDXLIST(10000)
62 
63  DATA lux/0/
64 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
65 C DECLARE INTERFACES (REQUIRED FOR CBUF POINTER)
66  INTERFACE
67  SUBROUTINE getg2i(LUGI,CBUF,NLEN,NNUM,IRET)
68  CHARACTER(LEN=1),POINTER,DIMENSION(:) :: CBUF
69  INTEGER,INTENT(IN) :: LUGI
70  INTEGER,INTENT(OUT) :: NLEN,NNUM,IRET
71  END SUBROUTINE getg2i
72  SUBROUTINE getg2ir(LUGB,MSK1,MSK2,MNUM,CBUF,NLEN,NNUM,
73  & NMESS,IRET)
74  CHARACTER(LEN=1),POINTER,DIMENSION(:) :: CBUF
75  INTEGER,INTENT(IN) :: LUGB,MSK1,MSK2,MNUM
76  INTEGER,INTENT(OUT) :: NLEN,NNUM,NMESS,IRET
77  END SUBROUTINE getg2ir
78  END INTERFACE
79 
80 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
81 C DETERMINE WHETHER INDEX BUFFER NEEDS TO BE INITIALIZED
82  lux=0
83  iret=0
84  IF ( lugb.LE.0 .OR. lugb.GT.9999 ) THEN
85  print*,' '
86  print *,' FILE UNIT NUMBER OUT OF RANGE'
87  print *,' USE UNIT NUMBERS IN RANGE: 0 - 9999 '
88  print*,' '
89  iret=90
90  RETURN
91  ENDIF
92  IF (lugi.EQ.lugb) THEN ! Force regeneration of index from GRIB2 File
93  IF ( ASSOCIATED( idxlist(lugb)%CBUF ) )
94  & DEALLOCATE(idxlist(lugb)%CBUF)
95  NULLIFY(idxlist(lugb)%CBUF)
96  idxlist(lugb)%NLEN=0
97  idxlist(lugb)%NNUM=0
98  lux=0
99  ENDIF
100 
101  IF (lugi.LT.0) THEN ! Force re-read of index from indexfile
102  ! associated with unit abs(lugi)
103  IF ( ASSOCIATED( idxlist(lugb)%CBUF ) )
104  & DEALLOCATE(idxlist(lugb)%CBUF)
105  NULLIFY(idxlist(lugb)%CBUF)
106  idxlist(lugb)%NLEN=0
107  idxlist(lugb)%NNUM=0
108  lux=abs(lugi)
109  ENDIF
110 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111 C Check if index already exists in memory
112  IF ( ASSOCIATED( idxlist(lugb)%CBUF ) ) THEN
113  cindex => idxlist(lugb)%CBUF
114  nlen = idxlist(lugb)%NLEN
115  nnum = idxlist(lugb)%NNUM
116  RETURN
117  ENDIF
118 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
119  irgi=0
120  IF(lux.GT.0) THEN
121  CALL getg2i(lux,idxlist(lugb)%CBUF,nlen,nnum,irgi)
122  ELSEIF(lux.LE.0) THEN
123  mskp=0
124  CALL getg2ir(lugb,msk1,msk2,mskp,idxlist(lugb)%CBUF,
125  & nlen,nnum,nmess,irgi)
126  ENDIF
127  IF(irgi.EQ.0) THEN
128  cindex => idxlist(lugb)%CBUF
129  idxlist(lugb)%NLEN = nlen
130  idxlist(lugb)%NNUM = nnum
131  ELSE
132  nlen = 0
133  nnum = 0
134  print*,' '
135  print *,' ERROR READING INDEX FILE '
136  print*,' '
137  iret=96
138  RETURN
139  ENDIF
140 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
141 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
142  RETURN
143  END
getg2i
subroutine getg2i(LUGI, CBUF, NLEN, NNUM, IRET)
READ A GRIB2 INDEX FILE AND RETURN ITS CONTENTS.
Definition: getg2i.f:57
getidx
subroutine getidx(LUGB, LUGI, CINDEX, NLEN, NNUM, IRET)
This subroutine finds, reads or generates a grib2 index for the grib2 file associated with unit lugb.
Definition: getidx.f:47
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