NCEPLIBS-g2  3.5.0
gdt2gds.F90
Go to the documentation of this file.
1 
5 
44 subroutine gdt2gds(igds, igdstmpl, idefnum, ideflist, kgds, igrid, iret)
45  implicit none
46 
47  integer, intent(in) :: idefnum
48  integer, intent(in) :: igds(*), igdstmpl(*), ideflist(*)
49  integer, intent(out) :: kgds(*), igrid, iret
50 
51  integer :: kgds72(200), kgds71(200), idum(200), jdum(200)
52  integer :: ierr, j
53 
54  iret = 0
55  idum = 0
56  if (igds(5) .eq. 0) then ! Lat / Lon grid
57  kgds(1) = 0
58  kgds(2) = igdstmpl(8) ! Ni
59  kgds(3) = igdstmpl(9) ! Nj
60  kgds(4) = igdstmpl(12) / 1000 ! Lat of 1st grid point
61  kgds(5) = igdstmpl(13) / 1000 ! Long of 1st grid point
62  kgds(6) = 0 ! resolution and component flags
63  if (igdstmpl(1) == 2) kgds(6) = 64
64  if (btest(igdstmpl(14), 4).OR.btest(igdstmpl(14), 5)) kgds(6) = kgds(6) + 128
65  if (btest(igdstmpl(14), 3)) kgds(6) = kgds(6) + 8
66  kgds(7) = igdstmpl(15) / 1000 ! Lat of last grid point
67  kgds(8) = igdstmpl(16) / 1000 ! Long of last grid point
68  kgds(9) = igdstmpl(17) / 1000 ! Di
69  kgds(10) = igdstmpl(18) / 1000 ! Dj
70  kgds(11) = igdstmpl(19) ! Scanning mode
71  kgds(12) = 0
72  kgds(13) = 0
73  kgds(14) = 0
74  kgds(15) = 0
75  kgds(16) = 0
76  kgds(17) = 0
77  kgds(18) = 0
78  kgds(19) = 0
79  kgds(20) = 255
80  kgds(21) = 0
81  kgds(22) = 0
82  !
83  ! Process irreg grid stuff, if necessary
84  !
85  if (idefnum.ne.0) then
86  if (igdstmpl(8) .eq. -1) then
87  kgds(2) = 65535
88  kgds(9) = 65535
89  endif
90  if (igdstmpl(9) .eq. -1) then
91  kgds(3) = 65535
92  kgds(10) = 65535
93  endif
94  kgds(19) = 0
95  kgds(20) = 33
96  if (kgds(1) .eq. 1.OR.kgds(1) .eq. 3) kgds(20) = 43
97  kgds(21) = igds(2) ! num of grid points
98  do j = 1, idefnum
99  kgds(21 + j) = ideflist(j)
100  enddo
101  endif
102  elseif (igds(5) .eq. 10) then ! Mercator grid
103  kgds(1) = 1 ! Grid Definition Template number
104  kgds(2) = igdstmpl(8) ! Ni
105  kgds(3) = igdstmpl(9) ! Nj
106  kgds(4) = igdstmpl(10) / 1000 ! Lat of 1st grid point
107  kgds(5) = igdstmpl(11) / 1000 ! Long of 1st grid point
108  kgds(6) = 0 ! resolution and component flags
109  if (igdstmpl(1)==2) kgds(6) = 64
110  if (btest(igdstmpl(12), 4).OR.btest(igdstmpl(12), 5)) kgds(6) = kgds(6) + 128
111  if (btest(igdstmpl(12), 3)) kgds(6) = kgds(6) + 8
112  kgds(7) = igdstmpl(14) / 1000 ! Lat of last grid point
113  kgds(8) = igdstmpl(15) / 1000 ! Long of last grid point
114  kgds(9) = igdstmpl(13) / 1000 ! Lat intersects earth
115  kgds(10) = 0
116  kgds(11) = igdstmpl(16) ! Scanning mode
117  kgds(12) = igdstmpl(18) / 1000 ! Di
118  kgds(13) = igdstmpl(19) / 1000 ! Dj
119  kgds(14) = 0
120  kgds(15) = 0
121  kgds(16) = 0
122  kgds(17) = 0
123  kgds(18) = 0
124  kgds(19) = 0
125  kgds(20) = 255
126  kgds(21) = 0
127  kgds(22) = 0
128  elseif (igds(5) .eq. 30) then ! Lambert Conformal Grid
129  kgds(1) = 3
130  kgds(2) = igdstmpl(8) ! Nx
131  kgds(3) = igdstmpl(9) ! Ny
132  kgds(4) = igdstmpl(10) / 1000 ! Lat of 1st grid point
133  kgds(5) = igdstmpl(11) / 1000 ! Long of 1st grid point
134  kgds(6) = 0 ! resolution and component flags
135  if (igdstmpl(1)==2) kgds(6) = 64
136  if (btest(igdstmpl(12), 4).OR.btest(igdstmpl(12), 5)) kgds(6) = kgds(6) + 128
137  if (btest(igdstmpl(12), 3)) kgds(6) = kgds(6) + 8
138  kgds(7) = igdstmpl(14) / 1000 ! Lon of orientation
139  kgds(8) = igdstmpl(15) / 1000 ! Dx
140  kgds(9) = igdstmpl(16) / 1000 ! Dy
141  kgds(10) = igdstmpl(17) ! Projection Center Flag
142  kgds(11) = igdstmpl(18) ! Scanning mode
143  kgds(12) = igdstmpl(19) / 1000 ! Lat in 1
144  kgds(13) = igdstmpl(20) / 1000 ! Lat in 2
145  kgds(14) = igdstmpl(21) / 1000 ! Lat of S. Pole of projection
146  kgds(15) = igdstmpl(22) / 1000 ! Lon of S. Pole of projection
147  kgds(16) = 0
148  kgds(17) = 0
149  kgds(18) = 0
150  kgds(19) = 0
151  kgds(20) = 255
152  kgds(21) = 0
153  kgds(22) = 0
154  elseif (igds(5) .eq. 40) then ! Gaussian Lat / Lon grid
155  kgds(1) = 4
156  kgds(2) = igdstmpl(8) ! Ni
157  kgds(3) = igdstmpl(9) ! Nj
158  kgds(4) = igdstmpl(12) / 1000 ! Lat of 1st grid point
159  kgds(5) = igdstmpl(13) / 1000 ! Long of 1st grid point
160  kgds(6) = 0 ! resolution and component flags
161  if (igdstmpl(1)==2) kgds(6) = 64
162  if (btest(igdstmpl(14), 4).OR.btest(igdstmpl(14), 5)) kgds(6) = kgds(6) + 128
163  if (btest(igdstmpl(14), 3)) kgds(6) = kgds(6) + 8
164  kgds(7) = igdstmpl(15) / 1000 ! Lat of last grid point
165  kgds(8) = igdstmpl(16) / 1000 ! Long of last grid point
166  kgds(9) = igdstmpl(17) / 1000 ! Di
167  kgds(10) = igdstmpl(18) ! N - Number of parallels
168  kgds(11) = igdstmpl(19) ! Scanning mode
169  kgds(12) = 0
170  kgds(13) = 0
171  kgds(14) = 0
172  kgds(15) = 0
173  kgds(16) = 0
174  kgds(17) = 0
175  kgds(18) = 0
176  kgds(19) = 0
177  kgds(20) = 255
178  kgds(21) = 0
179  kgds(22) = 0
180  elseif (igds(5) .eq. 20) then ! Polar Stereographic Grid
181  kgds(1) = 5
182  kgds(2) = igdstmpl(8) ! Nx
183  kgds(3) = igdstmpl(9) ! Ny
184  kgds(4) = igdstmpl(10) / 1000 ! Lat of 1st grid point
185  kgds(5) = igdstmpl(11) / 1000 ! Long of 1st grid point
186  kgds(6) = 0 ! resolution and component flags
187  if (igdstmpl(1)==2) kgds(6) = 64
188  if (btest(igdstmpl(12), 4).OR.btest(igdstmpl(12), 5)) kgds(6) = kgds(6) + 128
189  if (btest(igdstmpl(12), 3)) kgds(6) = kgds(6) + 8
190  kgds(7) = igdstmpl(14) / 1000 ! Lon of orientation
191  kgds(8) = igdstmpl(15) / 1000 ! Dx
192  kgds(9) = igdstmpl(16) / 1000 ! Dy
193  kgds(10) = igdstmpl(17) ! Projection Center Flag
194  kgds(11) = igdstmpl(18) ! Scanning mode
195  kgds(12) = 0
196  kgds(13) = 0
197  kgds(14) = 0
198  kgds(15) = 0
199  kgds(16) = 0
200  kgds(17) = 0
201  kgds(18) = 0
202  kgds(19) = 0
203  kgds(20) = 255
204  kgds(21) = 0
205  kgds(22) = 0
206  elseif (igds(5) .eq. 204) then ! Curvilinear Orthogonal
207  kgds(1) = 204
208  kgds(2) = igdstmpl(8) ! Ni
209  kgds(3) = igdstmpl(9) ! Nj
210  kgds(4) = 0
211  kgds(5) = 0
212  kgds(6) = 0 ! resolution and component flags
213  if (igdstmpl(1)==2) kgds(6) = 64
214  if (btest(igdstmpl(14), 4).OR.btest(igdstmpl(14), 5)) kgds(6) = kgds(6) + 128
215  if (btest(igdstmpl(14), 3)) kgds(6) = kgds(6) + 8
216  kgds(7) = 0
217  kgds(8) = 0
218  kgds(9) = 0
219  kgds(10) = 0
220  kgds(11) = igdstmpl(19) ! Scanning mode
221  kgds(12) = 0
222  kgds(13) = 0
223  kgds(14) = 0
224  kgds(15) = 0
225  kgds(16) = 0
226  kgds(17) = 0
227  kgds(18) = 0
228  kgds(19) = 0
229  kgds(20) = 255
230  kgds(21) = 0
231  kgds(22) = 0
232  !
233  ! Process irreg grid stuff, if necessary
234  !
235  if (idefnum.ne.0) then
236  if (igdstmpl(8) .eq. -1) then
237  kgds(2) = 65535
238  kgds(9) = 65535
239  endif
240  if (igdstmpl(9) .eq. -1) then
241  kgds(3) = 65535
242  kgds(10) = 65535
243  endif
244  kgds(19) = 0
245  kgds(20) = 33
246  if (kgds(1) .eq. 1.OR.kgds(1) .eq. 3) kgds(20) = 43
247  kgds(21) = igds(2) ! num of grid points
248  do j = 1, idefnum
249  kgds(21 + j) = ideflist(j)
250  enddo
251  endif
252  elseif (igds(5) .eq. 32768) then ! Rotate Lat / Lon grid
253  kgds(1) = 203 ! Arakawa Staggerred E / B grid
254  kgds(2) = igdstmpl(8) ! Ni
255  kgds(3) = igdstmpl(9) ! Nj
256  kgds(4) = igdstmpl(12) / 1000 ! Lat of 1st grid point
257  kgds(5) = igdstmpl(13) / 1000 ! Lon of 1st grid point
258  kgds(6) = 0 ! resolution and component flags
259  if (igdstmpl(1)==2) kgds(6) = 64
260  if (btest(igdstmpl(14), 4).OR.btest(igdstmpl(14), 5)) kgds(6) = kgds(6) + 128
261  if (btest(igdstmpl(14), 3)) kgds(6) = kgds(6) + 8
262  kgds(7) = igdstmpl(15) / 1000 ! Lat of last grid point
263  kgds(8) = igdstmpl(16) / 1000 ! Lon of last grid point
264  kgds(9) = igdstmpl(17) / 1000 ! Di
265  kgds(10) = igdstmpl(18) / 1000 ! Dj
266  kgds(11) = igdstmpl(19) ! Scanning mode
267  kgds(12) = 0
268  kgds(13) = 0
269  kgds(14) = 0
270  kgds(15) = 0
271  kgds(16) = 0
272  kgds(17) = 0
273  kgds(18) = 0
274  kgds(19) = 0
275  kgds(20) = 255
276  kgds(21) = 0
277  kgds(22) = 0
278  !
279  ! Process irreg grid stuff, if necessary
280  !
281  if (idefnum.ne.0) then
282  if (igdstmpl(8) .eq. -1) then
283  kgds(2) = 65535
284  kgds(9) = 65535
285  endif
286  if (igdstmpl(9) .eq. -1) then
287  kgds(3) = 65535
288  kgds(10) = 65535
289  endif
290  kgds(19) = 0
291  kgds(20) = 33
292  if (kgds(1) .eq. 1.OR.kgds(1) .eq. 3) kgds(20) = 43
293  kgds(21) = igds(2) ! num of grid points
294  do j = 1, idefnum
295  kgds(21 + j) = ideflist(j)
296  enddo
297  endif
298  elseif (igds(5) .eq. 32769) then ! Rotate Lat / Lon grid
299  kgds(1) = 205 ! Arakawa Staggerred for Non-E Stagger grid
300  kgds(2) = igdstmpl(8) ! Ni
301  kgds(3) = igdstmpl(9) ! Nj
302  kgds(4) = igdstmpl(12) / 1000 ! Lat of 1st grid point
303  kgds(5) = igdstmpl(13) / 1000 ! Lon of 1st grid point
304  kgds(6) = 0 ! resolution and component flags
305  if (igdstmpl(1)==2) kgds(6) = 64
306  if (btest(igdstmpl(14), 4).OR.btest(igdstmpl(14), 5)) kgds(6) = kgds(6) + 128
307  if (btest(igdstmpl(14), 3)) kgds(6) = kgds(6) + 8
308  kgds(7) = igdstmpl(15) / 1000 ! Lat of last grid point
309  kgds(8) = igdstmpl(16) / 1000 ! Lon of last grid point
310  kgds(9) = igdstmpl(17) / 1000 ! Di
311  kgds(10) = igdstmpl(18) / 1000 ! Dj
312  kgds(11) = igdstmpl(19) ! Scanning mode
313  kgds(12) = igdstmpl(20) / 1000
314  kgds(13) = igdstmpl(21) / 1000
315  kgds(14) = 0
316  kgds(15) = 0
317  kgds(16) = 0
318  kgds(17) = 0
319  kgds(18) = 0
320  kgds(19) = 0
321  kgds(20) = 255
322  kgds(21) = 0
323  kgds(22) = 0
324  else
325  print *, 'gdt2gds: Unrecognized GRIB2 GDT = 3.', igds(5)
326  iret = 1
327  kgds(1:22) = 0
328  return
329  endif
330  !
331  ! Can we determine NCEP grid number ?
332  !
333  igrid = 255
334  do j = 254, 1, -1
335  !do j = 225, 225
336  kgds71 = 0
337  kgds72 = 0
338  call w3fi71(j, kgds71, ierr)
339  if (ierr.ne.0) cycle
340  ! convert W to E for longitudes
341  if (kgds71(3) .eq. 0) then ! lat / lon
342  if (kgds71(7) .lt. 0) kgds71(7) = 360000 + kgds71(7)
343  if (kgds71(10) .lt. 0) kgds71(10) = 360000 + kgds71(10)
344  elseif (kgds71(3) .eq. 1) then ! mercator
345  if (kgds71(7) .lt. 0) kgds71(7) = 360000 + kgds71(7)
346  if (kgds71(10) .lt. 0) kgds71(10) = 360000 + kgds71(10)
347  elseif (kgds71(3) .eq. 3) then ! lambert conformal
348  if (kgds71(7) .lt. 0) kgds71(7) = 360000 + kgds71(7)
349  if (kgds71(9) .lt. 0) kgds71(9) = 360000 + kgds71(9)
350  if (kgds71(18) .lt. 0) kgds71(18) = 360000 + kgds71(18)
351  elseif (kgds71(3) .eq. 4) then ! Guassian lat / lon
352  if (kgds71(7) .lt. 0) kgds71(7) = 360000 + kgds71(7)
353  if (kgds71(10) .lt. 0) kgds71(10) = 360000 + kgds71(10)
354  elseif (kgds71(3) .eq. 5) then ! polar stereographic
355  if (kgds71(7) .lt. 0) kgds71(7) = 360000 + kgds71(7)
356  if (kgds71(9) .lt. 0) kgds71(9) = 360000 + kgds71(9)
357  endif
358  call r63w72(idum, kgds, jdum, kgds72)
359  if (kgds72(3) .eq. 3) kgds72(14) = 0 ! lambert conformal fix
360  if (kgds72(3) .eq. 1) kgds72(15:18) = 0 ! mercator fix
361  if (kgds72(3) .eq. 5) kgds72(14:18) = 0 ! polar str fix
362  ! print *, ' kgds71(', j, ') = ', kgds71(1:30)
363  ! print *, ' kgds72 = ', kgds72(1:30)
364  if (all(kgds71 .eq. kgds72) ) then
365  igrid = j
366  return
367  endif
368  enddo
369 
370 end subroutine gdt2gds
subroutine gdt2gds(igds, igdstmpl, idefnum, ideflist, kgds, igrid, iret)
Convert grid information from a GRIB2 Grid Description Section as well as its Grid Definition Templat...
Definition: gdt2gds.F90:45