NCEPLIBS-g2 4.0.0
Loading...
Searching...
No Matches
tocgrib2super.F90
Go to the documentation of this file.
1
5
42 use grib_mod
43 use pdstemplates
45 !
46 integer,dimension(200) :: ids,gdt,pdt
47 integer :: dscpl,gdtn,pdtn
48 integer :: nbul,nrec,mbul,dayofmonth,hourofday,minofhour
49 integer,parameter :: lenhead=21,jrew=0
50 integer itime(8)
51 integer fsize
52 !
53 CHARACTER * 16 superwmo
54 CHARACTER * 80 desc,wmohead
55 CHARACTER * 200 fileb,filei,fileo,filea
56 CHARACTER * 6 envvar
57 CHARACTER * 1 csep(80)
58 CHARACTER * 1 wmohdr(lenhead)
59 character(len=1),pointer,dimension(:) :: gribm
60
61 logical :: extract=.false.
62 integer idxver
63 integer (kind = 8) :: itot8
64
65 interface
66 subroutine getgb2p2(lugb, lugi, j, jdisc, jids, jpdtn, jpdt, jgdtn, jgdt, &
67 extract, idxver, k, gribm, leng8, iret)
68 integer, intent(in) :: lugb, lugi, j, jdisc
69 integer, dimension(:) :: jids(*)
70 integer, intent(in) :: jpdtn
71 integer, dimension(:) :: jpdt(*)
72 integer, intent(in) :: jgdtn
73 integer, dimension(:) :: jgdt(*)
74 logical, intent(in) :: extract
75 integer, intent(inout) :: idxver
76 integer, intent(out) :: k
77 character(len = 1), pointer, dimension(:) :: gribm
78 integer (kind = 8), intent(out) :: leng8
79 integer, intent(out) :: iret
80 end subroutine getgb2p2
81 end interface
82
83 namelist /gribids/dscpl,ids,gdtn,gdt,pdtn,pdt,desc,wmohead,extract
84 !
85 CALL w3tagb('tocgrib2super',2010,0900,0246,'NP11')
86 lugb=11 ! Input GRIB2 File
87 luga=12 ! Input file size
88 lugi=31 ! Input GRIB2 INdex File
89 lugo=51 ! Output transmission file.
90 superwmo = 'SUPER WMO HEADER'
91 insize = 19 ! The size of the flag field separator
92 !
93 ! Read GRIB2 data and index file names from the FORTnn
94 ! environment variables, and open the files.
95 !
96 envvar='FORT '
97 write(envvar(5:6),fmt='(I2)') lugb
98 call getenv(envvar,fileb)
99 write(envvar(5:6),fmt='(I2)') luga
100 call getenv(envvar,filea)
101 write(envvar(5:6),fmt='(I2)') lugi
102 call getenv(envvar,filei)
103 !
104 ! Get the file size of the grib file
105 !
106 ! call baopenr(luga,filea,iret1)
107 open(unit=luga,file='filesize',form='formatted',status='old', &
108 iostat=iret1)
109 if (iret1 .ne. 0) then
110 write(6,fmt='(" Error opening GRIB file: ",A200)') filea
111 write(6,fmt='(" baopenr error = ",I5)') iret1
112 stop 10
113 endif
114 !
115 READ(luga,'(I15)') fsize
116 print *,' filesize ', fsize
117 !
118 call baopenr(lugb,fileb,iret1)
119 if (iret1 .ne. 0) then
120 write(6,fmt='(" Error opening GRIB file: ",A200)')fileb
121 write(6,fmt='(" baopenr error = ",I5)') iret1
122 stop 10
123 endif
124 !
125 ! Open GRIB2 index file. If doesn't open, use just the data
126 ! file.
127 !
128 call baopenr(lugi,filei,iret2)
129 if (iret2 .ne. 0) then
130 lugi=0
131 endif
132 !
133 ! Read output GRIB bulletin file name from FORT
134 ! environment variable, and open file.
135 !
136 envvar='FORT '
137 write(envvar(5:6),fmt='(I2)') lugo
138 call getenv(envvar,fileo)
139 call baopenw(lugo,fileo,iret1)
140 if (iret1 .ne. 0) then
141 write(6,fmt='(" Error opening output transmission file: ", &
142 A200)') fileo
143 write(6,fmt='(" baopenw error = ",I5)') iret1
144 stop 20
145 endif
146 !
147 ! NDFD grib file begins with a flag flied separator and size bytes of the file
148 ! ****nnnnnnnnn****\lf where "nnnnnnnn" is the size of the file(minus the separator)
149 ! The size of the flag field separator is 19 bytes.
150 !
151 if (fsize >= insize) then
152 fsize = fsize - insize
153 else
154 fsize=insize
155 ENDIF
156 !
157 ! loop through input control records.
158 !
159 iret=0
160 nbul=0
161 nrec = 0
162 foreachinputrecord: do
163
164 !
165 ! Set Namelist defaults
166 !
167 dscpl=-1 ! Grib2 Discipline number
168 ids=-9999 ! GRIB2 Identification Section
169 gdtn=-1 ! Grid Definition Template Number
170 gdt=-9999 ! Grid Definition Template
171 pdtn=-1 ! Product Definition Template Number
172 pdt=-9999 ! Product Definition Template
173 wmohead='TTAAnn CCCC'
174 extract=.false.
175 iopt=2
176
177 READ (*,gribids,iostat=ios,end=999)
178 !
179 if (desc(1:16) .EQ. superwmo(1:16)) then
180 !
181 ! MAKE Flag Field Separator block
182 !
183 call mkfldsep(csep,iopt,insize,fsize,lenout)
184 !
185 ! MAKE SUPER WMO HEADER
186 ! &
187 ! GET COMPUTER DATE-TIME SAVE FOR DATA DATE VERIFICATION
188 CALL w3utcdat(itime)
189 dayofmonth=itime(3)
190 hourofday=itime(5)
191 minofhour=itime(6)
192 CALL makwmo (wmohead(1:6),dayofmonth,hourofday,minofhour, &
193 wmohead(8:11),wmohdr)
194 !
195 ! write out Separator block, Abbreviated WMO Heading,
196 ! and GRIB2 field to output file.
197 !
198 call wryte(lugo,lenout,csep)
199 call wryte(lugo,lenhead,wmohdr)
200 !
201 write(6,'(16A)') ' DESC=',desc(1:16)
202 write(6,'(11A)') ' WMOHEAD=',wmohead(1:11)
203 !
204 cycle
205 endif
206
207 nrec = nrec + 1
208 if (ios .ne. 0) then
209 write(6,fmt='(" Error reading PDS from input file. iostat = " &
210 ,i5)') ios
211 cycle
212 endif
213 !
214 ! Echo input record
215 !
216 WRITE(6,fmt='(/,''***********************************'', &
217 ''********************************************'')')
218 write(6,'(A,I0)') ' Start new record no. = ',nrec
219 write(6,'(73A)') ' DESC=',desc(1:73)
220 write(6,'(11A)') ' WMOHEAD=',wmohead(1:11)
221 write(6,'(A,I0)') ' GRIB2 DISCIPLINE= ',dscpl
222 write(6,'(A,20(1x,I0))')' Section 1=', &
223 (ids(j2),j2=1,13)
224 if (gdtn .ne. -1) then
225 write(6,'(A,I0,A,100(1x,I0))') ' GDT 3. ',gdtn,' =', &
226 (gdt(j2),j2=1,getgdtlen(gdtn))
227 endif
228 if (pdtn .ne. -1) then
229 write(6,'(A,I0,A,100(1x,I0))') ' PDT 4. ',pdtn,' =', &
230 (pdt(j2),j2=1,getpdtlen(pdtn))
231 endif
232 !
233 ! Read and return packed GRIB field
234 !
235 idxver = 2
236 CALL getgb2p2(lugb,lugi,jrew,dscpl,ids,pdtn,pdt, &
237 gdtn,gdt,extract,idxver,krew,gribm,itot8,iret)
238 itot = int(itot8, kind(4))
239 IF (iret.NE.0) THEN
240 IF (iret.EQ.96)WRITE(6,'(A)')' GETGB2P: ERROR READING INDEX' &
241 //' FILE'
242 IF (iret.EQ.97)WRITE(6,'(A)')' GETGB2P: ERROR READING GRIB' &
243 //' FILE'
244 IF (iret.EQ.99)WRITE(6,'(A)')' GETGB2P: ERROR REQUEST NOT' &
245 //' FOUND'
246 cycle
247 END IF
248 WRITE (6,'(A,1x,I0)')' RECORD NO. OF GRIB RECORD IN INPUT ' &
249 //'FILE = ', krew
250 !
251 WRITE (6,'(A,I0)')' Size of GRIB Field = ',itot
252 !
253 ! MAKE Flag Field Separator block
254 !
255 call mkfldsep(csep,iopt,insize,itot+lenhead,lenout)
256 ! WRITE(6,'(A,80A)')' csep = ',csep
257 !
258 ! MAKE WMO HEADER
259 !
260 CALL makwmo (wmohead(1:6),dayofmonth,hourofday,minofhour, &
261 wmohead(8:11),wmohdr)
262 ! WRITE(6,'(21A)') ' WMOHEADER= ',WMOHDR
263 !
264 ! write out Separator block, Abbreviated WMO Heading,
265 ! and GRIB2 field to output file.
266 !
267 call wryte(lugo,lenout,csep)
268 call wryte(lugo,lenhead,wmohdr)
269 call wryte(lugo,itot,gribm)
270 nbul=nbul+1
271 if (associated(gribm)) then
272 deallocate(gribm)
273 nullify(gribm)
274 endif
275 !
276 enddo foreachinputrecord
277 !
278 ! CLOSING SECTION
279 !
280999 if (nbul .EQ. 0) then
281 WRITE (6,fmt='('' SOMETHING WRONG WITH DATA CARDS...'', &
282 ''NOTHING WAS PROCESSED'')')
283 ! CALL W3TAGE('tocgrib2super')
284 stop 19
285 else
286 call baclose (lugb,iret)
287 call baclose (lugi,iret)
288 call baclose (lugo,iret)
289 WRITE (6,fmt='(//,'' ******** RECAP OF THIS EXECUTION '', &
290 ''********'',/,5X,''READ '',I6,'' INDIVIDUAL IDS'', &
291 /,5X,''WROTE '',I6,'' BULLETINS OUT FOR TRANSMISSION'', &
292 //)') nrec, nbul
293 endif
294 !
295 ! TEST TO SEE IF ANY BULLETINS MISSING
296 !
297 mbul = nrec - nbul
298 IF (mbul .ne. 0) THEN
299 WRITE(6,'(A,1X,I0)')' BULLETINS MISSING = ',mbul
300 ! CALL W3TAGE('tocgrib2super')
301 stop 30
302 END IF
303 !
304 ! CALL W3TAGE('tocgrib2super')
305 stop
306END PROGRAM tocgrib2super
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 tocgrib2super
This program reads selected GRIB2 fields from a file, adds a flag Field separator block and the size ...
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