NCEPLIBS-g2 4.0.0
Loading...
Searching...
No Matches
cnvgrib.F90
Go to the documentation of this file.
1
4
32program cnvgrib
33
34 integer :: inver = 0, outver = 0, ipack = -1
35 character(len = 500) :: gfilein, gfileout, copt
36 character(len = 2) :: master_table_ver, curmastertab_ver
37 INTEGER(4) narg, iargc, table_ver, mastertab
38 logical :: usemiss = .false., uvvect = .true.
39 !
40 ! Set current Master table version 2
41 !
42 curmastertab_ver = '2'
43 table_ver = 2
44 mastertab = 21 ! WMO GRIB2 version 21 (released in May 2, 2018)
45
46 ! GET ARGUMENTS
47 narg = iargc()
48 IF (narg .lt. 3) THEN ! may be a problem with args
49 IF (narg .eq. 0) THEN
50 !CALL ERRMSG('cnvgrib: Incorrect usage')
51 call usage(0)
52 CALL errexit(2)
53 ELSE ! look for -h "help" option
54 do j = 1, narg
55 call getarg(j, copt)
56 if (copt .eq. '-h' .or. copt .eq. '-help') then
57 call usage(1)
58 CALL errexit(0)
59 endif
60 ENDDO
61 call usage(0)
62 CALL errexit(2)
63 ENDIF
64 ELSE
65 j = 1
66 do while (j.le.narg-2) ! parse first narg-2 args
67 call getarg(j, copt)
68 j = j+1
69 selectcase(copt)
70 case('-g12')
71 inver = 1
72 outver = 2
73 case('-g21')
74 inver = 2
75 outver = 1
76 case('-g22')
77 inver = 2
78 outver = 2
79 case('-p0')
80 ipack = 0
81 case('-p2')
82 ipack = 2
83 case('-p31')
84 ipack = 31
85 case('-p32')
86 ipack = 32
87 case('-p40')
88 ipack = 40
89 case('-p41')
90 ipack = 41
91 case('-p40000') ! Obsolete
92 ipack = 40000
93 case('-p40010') ! Obsolete
94 ipack = 40010
95 case('-m')
96 usemiss = .true.
97 imiss = 1
98 case('-m0')
99 usemiss = .true.
100 imiss = 0
101 case('-nv')
102 uvvect = .false.
103 case('-mastertable_ver_1')
104 table_ver = 1
105 master_table_ver = '1'
106 case('-mastertable_ver_2')
107 table_ver = 2
108 master_table_ver = '2'
109 case('-mastertable_ver_3')
110 table_ver = 3
111 master_table_ver = '3'
112 case('-mastertable_ver_4')
113 table_ver = 4
114 master_table_ver = '4'
115 case('-mastertable_ver_5')
116 table_ver = 5
117 master_table_ver = '5'
118 case('-mastertable_ver_6')
119 table_ver = 6
120 master_table_ver = '6'
121 case('-mastertable_ver_7')
122 table_ver = 7
123 master_table_ver = '7'
124 case('-mastertable_ver_8')
125 table_ver = 8
126 master_table_ver = '8'
127 case('-mastertable_ver_9')
128 table_ver = 9
129 master_table_ver = '9'
130 case('-mastertable_ver_10')
131 table_ver = 10
132 master_table_ver = '10'
133 case('-mastertable_ver_11')
134 table_ver = 11
135 master_table_ver = '11'
136 case('-mastertable_ver_12')
137 table_ver = 12
138 master_table_ver = '12'
139 case('-mastertable_ver_13')
140 table_ver = 13
141 master_table_ver = '13'
142 case('-mastertable_ver_14')
143 table_ver = 14
144 master_table_ver = '14'
145 case('-mastertable_ver_15')
146 table_ver = 15
147 master_table_ver = '15'
148 case('-mastertable_ver_16')
149 table_ver = 16
150 master_table_ver = '16'
151 case('-mastertable_ver_17')
152 table_ver = 17
153 master_table_ver = '17'
154 case('-mastertable_ver_18')
155 table_ver = 18
156 master_table_ver = '18'
157 case('-mastertable_ver_19')
158 table_ver = 19
159 master_table_ver = '19'
160 case('-mastertable_ver_20')
161 table_ver = 20
162 master_table_ver = '20'
163 case('-mastertable_ver_21')
164 table_ver = 21
165 master_table_ver = '21'
166 case('-mastertable_ver_22')
167 table_ver = 22
168 master_table_ver = '22'
169 case default
170 call usage(0)
171 CALL errexit(2)
172 end select
173 ENDDO
174
175 if (table_ver .le. 1 .OR. &
176 table_ver .gt. mastertab) then
177 call usage(0)
178 call errmsg (' ')
179 call errmsg('cnvgrib: cannot change to master table '// &
180 'version ' // master_table_ver)
181 call errmsg (' ')
182 call errmsg('Current GRIB master table version is '// &
183 curmastertab_ver)
184 call errmsg (' ')
185 CALL errexit(2)
186 end if
187 !
188 ! get filenames from last two arguments
189 !
190 CALL getarg(narg-1, gfilein)
191 CALL getarg(narg, gfileout)
192 !
193 ! If -p option specified, must be writing out grib2
194 !
195 if ((ipack .ne. -1).and.(outver .eq. 1)) then
196 CALL errmsg('cnvgrib: -pxx option ignored when using -g21')
197 endif
198 !
199 ! Must have -g option
200 !
201 if ((inver .eq. 0).or.(outver .eq. 0)) then
202 CALL errmsg('cnvgrib: must use one -gxx option')
203 call usage(0)
204 CALL errexit(2)
205 endif
206 !
207 ! If -m or -m0 option specified, must be writing out grib2
208 ! and using DRT 5.2 or 5.3
209 !
210 if ((usemiss).and.(ipack .ne. 2 .AND. ipack .ne. 31 .AND. &
211 ipack .ne. 32)) then
212 CALL errmsg('cnvgrib: -m or -m0 option ignored when not '// &
213 'using -p2, -p31 or -p32.')
214 usemiss = .false.
215 endif
216 ENDIF
217 !
218 ! Open input and output grib files
219 !
220 ifl1 = 10
221 ifl2 = 50
222 ncgb = len_trim(gfilein)
223 CALL baopenr(ifl1, gfilein(1:ncgb), ios)
224 if (ios .NE. 0) then
225 call errmsg('cnvgrib: cannot open input GRIB file '// &
226 gfilein(1:ncgb))
227 call errexit(3)
228 endif
229 ncgb = len_trim(gfileout)
230 CALL baopenw(ifl2, gfileout(1:ncgb), ios)
231 if (ios .NE. 0) then
232 call errmsg('cnvgrib: cannot open output GRIB file '// &
233 gfileout(1:ncgb))
234 call errexit(4)
235 endif
236 !
237 ! convert grib file
238 !
239 if ((inver .eq. 1).AND.(outver .eq. 2)) then
240 call cnv12(ifl1, ifl2, ipack, usemiss, imiss, uvvect, table_ver)
241 elseif ((inver .eq. 2).AND.(outver .eq. 1)) then
242 call cnv21(ifl1, ifl2)
243 elseif ((inver .eq. 2).AND.(outver .eq. 2)) then
244 call cnv22(ifl1, ifl2, ipack, usemiss, imiss, table_ver)
245 else
246 print *, ' Unknown conversion option.'
247 call errexit(5)
248 endif
249 !
250 ! close grib files
251 !
252 CALL baclose(ifl1, ios)
253 CALL baclose(ifl2, ios)
254
255 stop
256end program cnvgrib
257
264subroutine usage(iopt)
265 character(len = 15) :: cnvgrib_ver = "cnvgrib-v3.1.1"
266 integer, intent(in) :: iopt
267
268 if (iopt .eq. 0) then
269 call errmsg (' ')
270 call errmsg('Usage: cnvgrib [-h] {-g12|-g21|-g22} [-m|-m0]'// &
271 ' [-nv] [-mastertable_ver_x]')
272 call errmsg(' [{-p0|-p2|-p31|-p32|-p40'// &
273 '|-p41}] ingribfile outgribfile')
274 call errmsg (' ')
275 call errmsg('Usage: cnvgrib -h For helps and shows all'// &
276 ' options')
277 call errmsg (' ')
278 endif
279
280 if (iopt .eq. 1) then
281 call errmsg (' ')
282 call errmsg('cnvgrib: version '//cnvgrib_ver)
283 call errmsg (' ')
284 call errmsg('Must use one of the following options:')
285 call errmsg(' -g12 converts GRIB1 to GRIB2')
286 call errmsg(' -g21 converts GRIB2 to GRIB1')
287 call errmsg(' -g22 converts GRIB2 to GRIB2 '// &
288 ' (used to change packing option)')
289 call errmsg (' ')
290 call errmsg('Optional packing options: (for use with '// &
291 ' -g12 and -g22 only)')
292 call errmsg(' -p0 simple packing')
293 call errmsg(' -p2 complex packing')
294 call errmsg(' -p31 complex pack with 1st order diffs')
295 call errmsg(' -p32 complex pack with 2nd order diffs')
296 call errmsg(' -p40 JPEG2000 encoding')
297 call errmsg(' -p41 PNG encoding')
298 call errmsg (' ')
299 call errmsg('Other Optional options: ')
300 call errmsg(' -nv Do not combine U, V wind components')
301 call errmsg (' ')
302 call errmsg(' Use missing value management'// &
303 ' instead of bitmap')
304 call errmsg(' (ONLY valid with Complex Packing options:'// &
305 ' -p2, -p31 or -p32)')
306 call errmsg (' ')
307 call errmsg(' -m Primary missing values'// &
308 ' included within the data values')
309 call errmsg(' -m0 No explicit missing values'// &
310 ' included within the data values')
311 call errmsg(' -mastertable_ver_x Master Table version'// &
312 ' where x is number from 2 to 21')
313 call errmsg (' ')
314 endif
315 return
316end subroutine usage
subroutine cnv12(ifl1, ifl2, ipack, usemiss, imiss, uvvect, table_ver)
This subroutine converts every GRIB1 field in a file to a GRIB2 field.
Definition cnv12.F90:48
subroutine cnv21(ifl1, ifl2)
This subroutine converts every GRIB2 field in a file to a GRIB1 field.
Definition cnv21.F90:20
subroutine cnv22(ifl1, ifl2, ipack, usemiss, imiss, table_ver)
This subroutine converts every GRIB2 field in a file to another GRIB2 field, most likely one using a ...
Definition cnv22.F90:38
program cnvgrib
This program converts every GRIB field in a file between GRIB1 and GRIB2.
Definition cnvgrib.F90:32
subroutine usage(iopt)
This routine prints a brief description of the command line options.
Definition cnvgrib.F90:265