NCEPLIBS-g2 4.0.0
Loading...
Searching...
No Matches
tocgrib2.F90
Go to the documentation of this file.
1
5
39PROGRAM tocgrib2
40 use grib_mod
41 use pdstemplates
43 integer, dimension(200) :: ids, gdt, pdt
44 integer :: dscpl, gdtn, pdtn
45 integer :: nbul, nrec, mbul, dayofmonth, hourofday
46 integer, parameter :: lenhead=21, jrew=0
47
48 CHARACTER * 80 desc, wmohead
49 CHARACTER * 200 fileb, filei, fileo
50 CHARACTER * 6 envvar
51 CHARACTER * 1 csep(80)
52 CHARACTER * 1 wmohdr(lenhead)
53 character(len=1), pointer, dimension(:) :: gribm
54
55 logical :: extract=.false.
56 integer idxver
57 integer (kind = 8) :: itot8
58
59 interface
60 subroutine getgb2p2(lugb, lugi, j, jdisc, jids, jpdtn, jpdt, jgdtn, jgdt, &
61 extract, idxver, k, gribm, leng8, iret)
62 integer, intent(in) :: lugb, lugi, j, jdisc
63 integer, dimension(:) :: jids(*)
64 integer, intent(in) :: jpdtn
65 integer, dimension(:) :: jpdt(*)
66 integer, intent(in) :: jgdtn
67 integer, dimension(:) :: jgdt(*)
68 logical, intent(in) :: extract
69 integer, intent(inout) :: idxver
70 integer, intent(out) :: k
71 character(len = 1), pointer, dimension(:) :: gribm
72 integer (kind = 8), intent(out) :: leng8
73 integer, intent(out) :: iret
74 end subroutine getgb2p2
75 end interface
76 namelist /gribids/dscpl,ids,gdtn,gdt,pdtn,pdt,desc,wmohead,extract
77
78 CALL w3tagb('tocgrib2', 2012, 0916, 0083, 'NP11')
79
80 lugb=11 ! Input GRIB2 File
81 lugi=31 ! Input GRIB2 INdex File
82 lugo=51 ! Output transmission file.
83
84 ! Read GRIB2 data and index file names from the FORT_nn
85 ! environment variables, and open the files.
86 envvar='FORT '
87 write(envvar(5:6), fmt='(I2)') lugb
88 call getenv(envvar, fileb)
89 write(envvar(5:6), fmt='(I2)') lugi
90 call getenv(envvar, filei)
91
92 call baopenr(lugb, fileb, iret1)
93 if (iret1 .ne. 0) then
94 write(6, fmt='(" Error opening GRIB file: ", A200)') fileb
95 write(6, fmt='(" baopenr error = ", I5)') iret1
96 stop 10
97 endif
98
99 ! Open GRIB2 index file. If doesn't open, use just the data
100 ! file.
101 call baopenr(lugi, filei, iret2)
102 if (iret2 .ne. 0) then
103 lugi=0
104 endif
105
106 ! Read output GRIB bulletin file name from FORTnn
107 ! environment variable, and open file.
108 write(envvar(5:6), fmt='(I2)') lugo
109 call getenv(envvar, fileo)
110 call baopenw(lugo, fileo, iret1)
111 if (iret1 .ne. 0) then
112 write(6, fmt='(" Error opening output transmission file: ", &
113 A200)') fileo
114 write(6, fmt='(" baopenw error = ", I5)') iret1
115 stop 20
116 endif
117
118 ! loop through input control records.
119 iret=0
120 nbul=0
121 nrec = 0
122 foreachinputrecord: do
123
124 ! Set Namelist defaults
125 dscpl=-1 ! Grib2 Discipline number
126 ids=-9999 ! GRIB2 Identification Section
127 gdtn=-1 ! Grid Definition Template Number
128 gdt=-9999 ! Grid Definition Template
129 pdtn=-1 ! Product Definition Template Number
130 pdt=-9999 ! Product Definition Template
131 wmohead='TTAAnn CCCC'
132 extract=.false.
133
134 READ (*, gribids, iostat=ios, end=999)
135 nrec = nrec + 1
136 if (ios .ne. 0) then
137 write(6, fmt='(" Error reading PDS from input file. iostat = " &
138 , i5)') ios
139 cycle
140 endif
141
142 ! Echo input record
143 WRITE(6, fmt='(/, ''***********************************'', &
144 ''********************************************'')')
145 write(6, '(A, I0)') ' Start new record no. = ', nrec
146 write(6, '(73A)') ' DESC=', desc(1:73)
147 write(6, '(11A)') ' WMOHEAD=', wmohead(1:11)
148 write(6, '(A, I0)') ' GRIB2 DISCIPLINE= ', dscpl
149 write(6, '(A, 20(1x, I0))')' Section 1=', &
150 (ids(j2), j2=1, 13)
151 if (gdtn .ne. -1) then
152 write(6, '(A, I0, A, 100(1x, I0))') ' GDT 3. ', gdtn, ' =', &
153 (gdt(j2), j2=1, getgdtlen(gdtn))
154 endif
155 if (pdtn .ne. -1) then
156 write(6, '(A, I0, A, 100(1x, I0))') ' PDT 4. ', pdtn, ' =', &
157 (pdt(j2), j2=1, getpdtlen(pdtn))
158 endif
159
160 ! Read and return packed GRIB field
161 idxver = 2
162 CALL getgb2p2(lugb, lugi, jrew, dscpl, ids, pdtn, pdt, &
163 gdtn, gdt, extract, idxver, krew, gribm, itot8, iret)
164 itot = int(itot8, kind(4))
165 IF (iret.NE.0) THEN
166 IF (iret.EQ.96)WRITE(6, '(A)')' GETGB2P: ERROR READING INDEX' &
167 //' FILE'
168 IF (iret.EQ.97)WRITE(6, '(A)')' GETGB2P: ERROR READING GRIB' &
169 //' FILE'
170 IF (iret.EQ.99)WRITE(6, '(A)')' GETGB2P: ERROR REQUEST NOT' &
171 //' FOUND'
172 cycle
173 END IF
174 WRITE (6, '(A, 1x, I0)')' RECORD NO. OF GRIB RECORD IN INPUT ' &
175 //'FILE = ', krew
176 !
177 WRITE (6, '(A, I0)')' Size of GRIB Field = ', itot
178
179 ! MAKE Flag Field Separator block
180 iopt=2
181 insize=19
182 call mkfldsep(csep, iopt, insize, itot+lenhead, lenout)
183 ! WRITE(6, '(A, 80A)')' csep = ', csep
184
185 ! MAKE WMO HEADER
186 dayofmonth=mova2i(gribm(16+16))
187 hourofday=mova2i(gribm(16+17))
188 CALL makwmo (wmohead(1:6), dayofmonth, hourofday, &
189 wmohead(8:11), wmohdr)
190 ! WRITE(6, '(21A)') ' WMOHEADER= ', WMOHDR
191
192 ! write out Separator block, Abbreviated WMO Heading,
193 ! and GRIB2 field to output file.
194 call wryte(lugo, lenout, csep)
195 call wryte(lugo, lenhead, wmohdr)
196 call wryte(lugo, itot, gribm)
197 nbul=nbul+1
198 if (associated(gribm)) then
199 deallocate(gribm)
200 nullify(gribm)
201 endif
202 !
203 enddo foreachinputrecord
204
205 ! CLOSING SECTION
206999 if (nbul .EQ. 0) then
207 WRITE (6, fmt='('' SOMETHING WRONG WITH DATA CARDS...'', &
208 ''NOTHING WAS PROCESSED'')')
209 ! CALL W3TAGE('tocgrib2')
210 stop 19
211 else
212 call baclose (lugb, iret)
213 call baclose (lugi, iret)
214 call baclose (lugo, iret)
215 WRITE (6, fmt='(//, '' ******** RECAP OF THIS EXECUTION '', &
216 ''********'', /, 5X, ''READ '', I6, '' INDIVIDUAL IDS'', &
217 /, 5X, ''WROTE '', I6, '' BULLETINS OUT FOR TRANSMISSION'', &
218 //)') nrec, nbul
219 endif
220 ! TEST TO SEE IF ANY BULLETINS MISSING
221 mbul = nrec - nbul
222 IF (mbul .ne. 0) THEN
223 WRITE(6, '(A, 1X, I0)')' BULLETINS MISSING = ', mbul
224 ! CALL W3TAGE('tocgrib2')
225 stop 30
226 END IF
227
228 ! CALL W3TAGE('tocgrib2')
229 stop
230END PROGRAM tocgrib2
subroutine getgb2p2(lugb, lugi, j, jdisc, jids, jpdtn, jpdt, jgdtn, jgdt, extract, idxver, k, gribm, leng8, iret)
Find and extract a GRIB2 message from a file.
Definition g2getgb2.F90:656
subroutine makwmo(bulhed, iday, ihour, imin, kwbx, header)
Forms the wmo header for a given bulletin.
Definition makwmo.F90:25
This Fortran module contains the declaration of derived type gribfield.
Definition gribmod.F90:10
This Fortran module contains info on all the available GRIB2 Grid Definition Templates used in [Secti...
Information on all GRIB2 Product Definition Templates used in Section 4 - the Product Definition Sect...
program tocgrib2
Program reads selected GRIB2 fields from a file, adds a TOC Flag Field separator block and WMO Header...
Definition tocgrib2.F90:39
subroutine mkfldsep(csep, iopt, lenin, lenbull, lenout)
Generates a TOC Flag Field Separator Block used to separate WMO Bulletins within a transmission file ...
Definition tocgrib.F90:298