NCEPLIBS-g2  3.4.5
getg2ir.f
Go to the documentation of this file.
1 C> @file
2 C> @brief This subroutine read a GRIB file and return its index content.
3 C> @author Mark Iredell @date 1995-10-31
4 C>
5 
6 C> This subroutine read a GRIB file and return its index content.
7 C> the index buffer returned contains index records with the internal format:
8 C> - byte 001 - 004 length of index record
9 C> - byte 005 - 008 bytes to skip in data file before grib message
10 C> - byte 009 - 012 bytes to skip in message before lus (local use)
11 C> set = 0, if no local use section in grib2 message.
12 C> - byte 013 - 016 bytes to skip in message before gds
13 C> - byte 017 - 020 bytes to skip in message before pds
14 C> - byte 021 - 024 bytes to skip in message before drs
15 C> - byte 025 - 028 bytes to skip in message before bms
16 C> - byte 029 - 032 bytes to skip in message before data section
17 C> - byte 033 - 040 bytes total in the message
18 C> - byte 041 - 041 grib version number (currently 2)
19 C> - byte 042 - 042 message discipline
20 C> - byte 043 - 044 field number within grib2 message
21 C> - byte 045 - ii identification section (ids)
22 C> - byte ii+1- jj grid definition section (gds)
23 C> - byte jj+1- kk product definition section (pds)
24 C> - byte kk+1- ll the data representation section (drs)
25 C> - byte ll+1-ll+6 first 6 bytes of the bit map section (bms)
26 C>
27 C> Program history log:
28 C> - 1995-10-31 mark iredell
29 C> - 1996-10-31 mark iredell augmented optional definitions to byte 320
30 C> - 2002-01-02 stephen gilbert modified from getgir to create grib2 indexes
31 C>
32 C> @param[in] lugb integer unit of the unblocked grib file
33 C> @param[in] msk1 integer number of bytes to search for first message
34 C> @param[in] msk2 integer number of bytes to search for other messages
35 C> @param[in] mnum integer number of grib messages to skip (usually 0)
36 C> output arguments:
37 C> @param[out] cbuf character*1 pointer to a buffer that contains index
38 C> records. users should free memory that cbuf points to, using
39 C> deallocate(cbuf) when cbuf is no longer needed.
40 C> @param[out] nlen integer total length of index record buffer in bytes
41 C> @param[out] nnum integer number of index records, =0 if no grib
42 C> messages are found)
43 C> @param[out] nmess last grib message in file successfully processed
44 C> @param[out] iret integer return code
45 C> - 0 all ok
46 C> - 1 not enough memory available to hold full index buffer
47 C> - 2 not enough memory to allocate initial index buffer
48 C>
49 C> subprograms called:
50 C> - skgb seek next grib message
51 C> - ixgb2 make index record
52 C>
53 C> @note subprogram can be called from a multiprocessing environment.
54 C> do not engage the same logical unit from more than one processor.
55 C>
56 C> @author Mark Iredell @date 1995-10-31
57 C>
58 
59  SUBROUTINE getg2ir(LUGB,MSK1,MSK2,MNUM,CBUF,NLEN,NNUM,NMESS,IRET)
60 
61  USE re_alloc ! NEEDED FOR SUBROUTINE REALLOC
62  parameter(init=50000,next=10000)
63  CHARACTER(LEN=1),POINTER,DIMENSION(:) :: CBUF
64  INTEGER,INTENT(IN) :: LUGB,MSK1,MSK2,MNUM
65  INTEGER,INTENT(OUT) :: NLEN,NNUM,NMESS,IRET
66  CHARACTER(LEN=1),POINTER,DIMENSION(:) :: CBUFTMP
67  INTERFACE ! REQUIRED FOR CBUF POINTER
68  SUBROUTINE ixgb2(LUGB,LSKIP,LGRIB,CBUF,NUMFLD,MLEN,IRET)
69  INTEGER,INTENT(IN) :: LUGB,LSKIP,LGRIB
70  CHARACTER(LEN=1),POINTER,DIMENSION(:) :: CBUF
71  INTEGER,INTENT(OUT) :: NUMFLD,MLEN,IRET
72  END SUBROUTINE ixgb2
73  END INTERFACE
74 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
75 C INITIALIZE
76  iret=0
77  IF (ASSOCIATED(cbuf)) NULLIFY(cbuf)
78  mbuf=init
79  ALLOCATE(cbuf(mbuf),stat=istat) ! ALLOCATE INITIAL SPACE FOR CBUF
80  IF (istat.NE.0) THEN
81  iret=2
82  RETURN
83  ENDIF
84 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
85 C SEARCH FOR FIRST GRIB MESSAGE
86  iseek=0
87  CALL skgb(lugb,iseek,msk1,lskip,lgrib)
88  DO m=1,mnum
89  IF(lgrib.GT.0) THEN
90  iseek=lskip+lgrib
91  CALL skgb(lugb,iseek,msk2,lskip,lgrib)
92  ENDIF
93  ENDDO
94 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95 C GET INDEX RECORDS FOR EVERY GRIB MESSAGE FOUND
96  nlen=0
97  nnum=0
98  nmess=mnum
99  dowhile(iret.EQ.0.AND.lgrib.GT.0)
100  CALL ixgb2(lugb,lskip,lgrib,cbuftmp,numfld,nbytes,iret1)
101  IF (iret1.NE.0) print *,' SAGT ',numfld,nbytes,iret1
102  IF((nbytes+nlen).GT.mbuf) THEN ! ALLOCATE MORE SPACE, IF
103  ! NECESSARY
104  newsize=max(mbuf+next,mbuf+nbytes)
105  CALL realloc(cbuf,nlen,newsize,istat)
106  IF ( istat .NE. 0 ) THEN
107  iret=1
108  RETURN
109  ENDIF
110  mbuf=newsize
111  ENDIF
112  !
113  ! IF INDEX RECORDS WERE RETURNED IN CBUFTMP FROM IXGB2,
114  ! COPY CBUFTMP INTO CBUF, THEN DEALLOCATE CBUFTMP WHEN DONE
115  !
116  IF ( ASSOCIATED(cbuftmp) ) THEN
117  cbuf(nlen+1:nlen+nbytes)=cbuftmp(1:nbytes)
118  DEALLOCATE(cbuftmp,stat=istat)
119  IF (istat.NE.0) THEN
120  print *,' deallocating cbuftmp ... ',istat
121  stop 99
122  ENDIF
123  NULLIFY(cbuftmp)
124  nnum=nnum+numfld
125  nlen=nlen+nbytes
126  nmess=nmess+1
127  ENDIF
128  ! LOOK FOR NEXT GRIB MESSAGE
129  iseek=lskip+lgrib
130  CALL skgb(lugb,iseek,msk2,lskip,lgrib)
131  ENDDO
132 C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
133  RETURN
134  END
re_alloc
This module contains three subroutines to reorganize the integer, real and character data in memory i...
Definition: realloc.f:14
re_alloc::realloc
Definition: realloc.f:16
ixgb2
subroutine ixgb2(LUGB, LSKIP, LGRIB, CBUF, NUMFLD, MLEN, IRET)
This subroutine generates an index record for each field in a grib2 message.
Definition: ixgb2.f:55
skgb
subroutine skgb(LUGB, ISEEK, MSEEK, LSKIP, LGRIB)
This subroutine searches a file for the next grib 1 message.
Definition: skgb.f:33
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