NCEPLIBS-g2 4.0.0
Loading...
Searching...
No Matches
tocgrib.F90
Go to the documentation of this file.
1
4
32PROGRAM tocgrib
33 parameter(mxsiz3=1000000)
34
35 INTEGER dum
36 INTEGER jgds(200)
37 INTEGER mpds(200)
38 INTEGER kgds(200)
39 INTEGER kpds(200)
40 INTEGER mapnum
41 INTEGER nbits
42 INTEGER nbul
43 INTEGER nparm
44 INTEGER punum
45 INTEGER,dimension(28) :: hexpds
46
47 CHARACTER * 6 bulhed
48 CHARACTER * 100 cparm
49 CHARACTER * 20 desc
50 CHARACTER * 3 eoml
51 CHARACTER * 1 grib(mxsiz3)
52 CHARACTER * 200 fileb,filei,fileo
53 CHARACTER * 6 envvar
54 CHARACTER * 4 kwbx
55 CHARACTER * 1 pds(28)
56 CHARACTER * 1 pdsl(28)
57 CHARACTER * 1 csep(80)
58 CHARACTER * 132 title
59 integer,parameter :: lenhead=21
60 CHARACTER * 1 wmohdr(lenhead)
61
62 LOGICAL iw3pds
63
64 hexpds = 0
65 lugb = 11
66 lugi = 31
67 lugo = 51
68
69 ! Get parm field with up to 100 characters. Parm field should
70 ! contain the originating center part of the WMO Header.
71 cparm = ' '
72 kwbx = 'KWBC'
73 CALL w3as00(nparm, cparm, ier)
74 IF (ier .EQ. 0) THEN
75 IF (nparm .EQ. 0 .OR. cparm(1:4) .EQ. ' ') THEN
76 print *,'THERE IS A PARM FIELD BUT IT IS EMPTY'
77 print *,'OR BLANK, I WILL USE THE DEFAULT KWBC'
78 ELSE
79 kwbx(1:4) = cparm(1:4)
80 END IF
81 ELSE IF (ier .EQ. 2 .OR. ier .EQ. 3) THEN
82 print *,'W3AS00 ERROR = ', ier
83 print *,'THERE IS NO PARM FIELD, I USED DEFAULT KWBC'
84 ELSE
85 print *,'W3AS00 ERROR = ', ier
86 END IF
87 print *,'NPARM = ',nparm
88 print *,'CPARM = ',cparm(1:4)
89 print *,'KWBX = ',kwbx(1:4)
90
91 ! Read GRIB data and index file names from the FORTnn
92 ! environment variables, and open the files.
93 envvar='FORT '
94 write(envvar(5:6),fmt='(I2)') lugb
95 call getenv(envvar,fileb)
96 write(envvar(5:6),fmt='(I2)') lugi
97 call getenv(envvar,filei)
98
99 call baopenr(lugb,fileb,iret1)
100 if ( iret1 .ne. 0 ) then
101 write(6,fmt='(" Error opening GRIB file: ", A200)') fileb
102 write(6,fmt='(" baopenr error = ",I5)') iret1
103 stop 10
104 endif
105
106 ! Open GRIB index file. If doesn't open, use just the data
107 ! file.
108 call baopenr(lugi,filei,iret2)
109 if ( iret2 .ne. 0 ) then
110 lugi=0
111 endif
112
113 ! Read output GRIB bulletin file name from FORTnn
114 ! environment variable, and open file.
115 write(envvar(5:6),fmt='(I2)') lugo
116 call getenv(envvar,fileo)
117 call baopenw(lugo,fileo,iret1)
118 if ( iret1 .ne. 0 ) then
119 write(6,fmt='(" Error opening GRIB file: ",A200)') fileo
120 write(6,fmt='(" baopenw error = ",I5)') iret1
121 stop 20
122 endif
123
124 iret = 0
125 iopt=2
126 insize=19
127 nbul = 0
128
129 ! loop through input control records.
130 nrec = 0
131 foreachelement: do
132
133 READ (*,66,iostat=ios) (hexpds(j),j=1,12), &
134 (hexpds(j),j=17,20), punum, desc
13566 FORMAT(3(2x,4z2.2),3x,4z2.2,6x,i3,1x,a20)
136 if ( ios .ne. 0 ) then
137 write(6,fmt='(" Error reading PDS from input file. iostat = ", i5)') ios
138 exit
139 endif
140 pds=char(hexpds)
141
142 ! exit loop, if no more bulletins in input cards
143 IF ( mova2i(pds(1)) .EQ. 255) exit
144
145 nrec = nrec + 1
146 WRITE(6,fmt='(/,''***********************************'', ''********************************************'')')
147 print *,'Start new record no. = ',nrec
148 WRITE (6,fmt='('' INPUT PDS, PUNUM'', '' & DESC...DESIRED GRIB MAPS LISTED ON FOLLOWING '', ' // &
149 ' ''LINES...'',/,4X,3(2X,4z2.2),3X,4z2.2,6X,I3,1X, A20)') (hexpds(j),j=1,12), &
150 (hexpds(j),j=17,20), punum, desc
151
152 ! Read WNO Header associated with this element
153 READ (*,iostat=ios,fmt='(4X,I3,2X,I2,2X,A6,1X,I3,24X,A3)') &
154 mapnum,nbits, bulhed, dum, eoml
155 WRITE (6,fmt='(4X,I3,2X,I2,2X,A6,1X,I3,24X,A3)') &
156 mapnum,nbits, bulhed, dum, eoml
157 if ( ios .ne. 0 ) then
158 write(6,fmt='(" Error reading PDS from input file. iostat =", i6)') ios
159 endif
160
161 ! Set up 25 word PDS array of GRIB field to read
162 jrew = 0
163 jgds = -1
164 mpds = -1
165 mpds(3) = mova2i(pds(7))
166 mpds(5) = mova2i(pds(9))
167 mpds(6) = mova2i(pds(10))
168 mpds(7) = mova2i(pds(11)) * 256 + mova2i(pds(12))
169 mpds(14) = mova2i(pds(19))
170 mpds(15) = mova2i(pds(20))
171
172 ! Read and return packed GRIB field
173 CALL getgbp(lugb,lugi,mxsiz3,jrew,mpds,jgds, &
174 itot,krew,kpds,kgds,grib,iret)
175 IF (iret.NE.0) THEN
176 IF (iret.LT.96) print *,'GETGB-W3FI63: ERROR = ',iret
177 IF (iret.EQ.96) print *,'GETGB: ERROR READING INDEX FILE'
178 IF (iret.EQ.97) print *,'GETGB: ERROR READING GRIB FILE'
179 IF (iret.EQ.98) THEN
180 print *,'GETGB ERROR: NUM. OF DATA POINTS GREATER THAN JF'
181 END IF
182 IF (iret.EQ.99) print *,'GETGB ERROR: REQUEST NOT FOUND'
183 IF (iret.GT.99) print *,'GETGB ERROR = ',iret
184 cycle
185 END IF
186 print *,'RECORD NO. OF GRIB RECORD IN INPUT FILE = ',krew
187
188 ! COMPARE RECORD (GRIB) TO CONTROL CARD (PDS), THEY SHOULD MATCH
189 pdsl(1:28)=grib(9:36)
190 key = 2
191 IF (.NOT.iw3pds(pdsl,pds,key)) THEN
192 print 2900, nrec,(mova2i(pdsl(j)),j=1,28), &
193 (mova2i(pds(j)),j=1,28)
1942900 FORMAT ( 1x,i4,' (PDS) IN RECORD DOES NOT MATCH (PDS) IN ', 'CONTROL CARD ',/,7(1x,4z2.2), /,7(1x,4z2.2))
195 cycle
196 END IF
197
198 ! Print PDS
199 print 2, (mova2i(pdsl(j)),j=1,28)
2002 FORMAT (' PDS = ',7(4z2.2,1x))
201
202 ! Construct and print Description of GRIB field
203 CALL w3fp11 (grib,pdsl,title,ier)
204 IF (ier.NE.0) print *,'W3FP11 ERROR = ',ier
205 print *,title(1:86)
206
207 print *,' Size of GRIB Field = ',itot
208
209 ! MAKE Flag Field Separator block
210 call mkfldsep(csep,iopt,insize,itot+lenhead,lenout)
211
212 ! MAKE WMO HEADER
213 ! Get system date and time
214 CALL makwmo (bulhed,kpds(10),kpds(11),kwbx,wmohdr)
215
216 ! write out Separator block, Abbreviated WMO Heading,
217 ! and GRIB field to output file.
218 call wryte(lugo,lenout,csep)
219 call wryte(lugo,lenhead,wmohdr)
220 call wryte(lugo,itot,grib)
221 nbul=nbul+1
222 enddo foreachelement
223
224 ! CLOSING SECTION
225 IF (nbul .EQ. 0 ) THEN
226 WRITE (6,fmt='('' SOMETHING WRONG WITH DATA CARDS...'', ''NOTHING WAS PROCESSED'')')
227 stop 19
228 ELSE
229 CALL baclose (lugb,iret)
230 CALL baclose (lugi,iret)
231 CALL baclose (lugo,iret)
232 WRITE (6,fmt='(//,'' ******** RECAP OF THIS EXECUTION '', ''********'',/,5X,''READ '',I6,'' INDIVIDUAL IDS'', ' // &
233 ' /,5X,''WROTE '',I6,'' BULLETINS OUT FOR TRANSMISSION'', //)') nrec, nbul
234 ENDIF
235
236 ! TEST TO SEE IF ANY BULLETINS MISSING
237 mbul = nrec - nbul
238 IF (mbul.NE.0) THEN
239 print *,'BULLETINS MISSING = ',mbul
240 stop 30
241 END IF
242
243 stop
244END PROGRAM tocgrib
245
297subroutine mkfldsep(csep,iopt,lenin,lenbull,lenout)
298 !
299 character*(*),intent(out) :: csep
300 integer,intent(in) :: iopt,lenin,lenbull
301 integer,intent(out) :: lenout
302 !
303 character(len=4),parameter :: cstar='****',clb='####'
304 !
305 if (iopt.eq.1) then
306 if ( lenin .le. 18 .and. lenbull .le. 999999 ) then
307 ! Create OPTION 1 separator block
308 csep(1:4)=clb
309 csep(5:7)='018'
310 write(csep(8:13),fmt='(I6.6)') lenbull
311 csep(14:17)=clb
312 csep(18:18)=char(10)
313 lenout=18
314 else ! Create OPTION 1a separator block
315 nnn=lenin
316 if ( nnn.lt.23 ) nnn=23
317 csep(1:4)=clb
318 write(csep(5:7),fmt='(I3.3)') nnn
319 write(csep(8:18),fmt='(I11.11)') lenbull
320 csep(19:nnn-5)='0'
321 csep(nnn-4:nnn-1)=clb
322 csep(nnn:nnn)=char(10)
323 lenout=nnn
324 endif
325 elseif (iopt.eq.2) then ! Create OPTION 2 separator block
326 csep(1:4)=cstar
327 write(csep(5:14),fmt='(I10.10)') lenbull
328 csep(15:18)=cstar
329 csep(19:19)=char(10)
330 lenout=19
331 else
332 print *,"mkfldsep: Option ",iopt," not recognized."
333 csep(1:lenin)=' '
334 endif
335 !
336 return
337end subroutine mkfldsep
subroutine makwmo(bulhed, iday, ihour, imin, kwbx, header)
Forms the wmo header for a given bulletin.
Definition makwmo.F90:25
program tocgrib
Create new GRIB2 file from exiting GRIB2 file.
Definition tocgrib.F90:32
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