forked from erget/wgrib2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdswizca.f
233 lines (233 loc) · 8.48 KB
/
gdswizca.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
SUBROUTINE GDSWIZCA(KGDS,IOPT,NPTS,FILL,XPTS,YPTS,RLON,RLAT,NRET,
& LROT,CROT,SROT)
C$$$ SUBPROGRAM DOCUMENTATION BLOCK
C
C SUBPROGRAM: GDSWIZCA GDS WIZARD FOR ROTATED EQUIDISTANT CYLINDRICAL
C PRGMMR: IREDELL ORG: W/NMC23 DATE: 96-04-10
C
C ABSTRACT: THIS SUBPROGRAM DECODES THE GRIB GRID DESCRIPTION SECTION
C (PASSED IN INTEGER FORM AS DECODED BY SUBPROGRAM W3FI63)
C AND RETURNS ONE OF THE FOLLOWING:
C (IOPT=+1) EARTH COORDINATES OF SELECTED GRID COORDINATES
C (IOPT=-1) GRID COORDINATES OF SELECTED EARTH COORDINATES
C FOR ROTATED EQUIDISTANT CYLINDRICAL PROJECTIONS.
C IF THE SELECTED COORDINATES ARE MORE THAN ONE GRIDPOINT
C BEYOND THE THE EDGES OF THE GRID DOMAIN, THEN THE RELEVANT
C OUTPUT ELEMENTS ARE SET TO FILL VALUES.
C THE ACTUAL NUMBER OF VALID POINTS COMPUTED IS RETURNED TOO.
C
C PROGRAM HISTORY LOG:
C 96-04-10 IREDELL
C
C USAGE: CALL GDSWIZCA(KGDS,IOPT,NPTS,FILL,XPTS,YPTS,RLON,RLAT,NRET,
C & LROT,CROT,SROT)
C
C INPUT ARGUMENT LIST:
C KGDS - INTEGER (200) GDS PARAMETERS AS DECODED BY W3FI63
C IOPT - INTEGER OPTION FLAG
C (+1 TO COMPUTE EARTH COORDS OF SELECTED GRID COORDS)
C (-1 TO COMPUTE GRID COORDS OF SELECTED EARTH COORDS)
C NPTS - INTEGER MAXIMUM NUMBER OF COORDINATES
C FILL - REAL FILL VALUE TO SET INVALID OUTPUT DATA
C (MUST BE IMPOSSIBLE VALUE; SUGGESTED VALUE: -9999.)
C XPTS - REAL (NPTS) GRID X POINT COORDINATES IF IOPT>0
C YPTS - REAL (NPTS) GRID Y POINT COORDINATES IF IOPT>0
C RLON - REAL (NPTS) EARTH LONGITUDES IN DEGREES E IF IOPT<0
C (ACCEPTABLE RANGE: -360. TO 360.)
C RLAT - REAL (NPTS) EARTH LATITUDES IN DEGREES N IF IOPT<0
C (ACCEPTABLE RANGE: -90. TO 90.)
C LROT - INTEGER FLAG TO RETURN VECTOR ROTATIONS IF 1
C
C OUTPUT ARGUMENT LIST:
C XPTS - REAL (NPTS) GRID X POINT COORDINATES IF IOPT<0
C YPTS - REAL (NPTS) GRID Y POINT COORDINATES IF IOPT<0
C RLON - REAL (NPTS) EARTH LONGITUDES IN DEGREES E IF IOPT>0
C RLAT - REAL (NPTS) EARTH LATITUDES IN DEGREES N IF IOPT>0
C NRET - INTEGER NUMBER OF VALID POINTS COMPUTED
C CROT - REAL (NPTS) CLOCKWISE VECTOR ROTATION COSINES IF LROT=1
C SROT - REAL (NPTS) CLOCKWISE VECTOR ROTATION SINES IF LROT=1
C (UGRID=CROT*UEARTH-SROT*VEARTH;
C VGRID=SROT*UEARTH+CROT*VEARTH)
C
C ATTRIBUTES:
C LANGUAGE: FORTRAN 77
C
C$$$
INTEGER KGDS(200)
REAL XPTS(NPTS),YPTS(NPTS),RLON(NPTS),RLAT(NPTS)
REAL CROT(NPTS),SROT(NPTS)
PARAMETER(RERTH=6.3712E6)
PARAMETER(PI=3.14159265358979,DPR=180./PI)
C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IF(KGDS(1).EQ.202) THEN
RLAT1=KGDS(4)*1.E-3
RLON1=KGDS(5)*1.E-3
IROT=MOD(KGDS(6)/8,2)
IM=KGDS(7)
JM=KGDS(8)
DLON=KGDS(9)*1.E-3
DLAT=KGDS(10)*1.E-3
ISCAN=MOD(KGDS(11)/128,2)
JSCAN=MOD(KGDS(11)/64,2)
NSCAN=MOD(KGDS(11)/32,2)
HI=(-1.)**ISCAN
HJ=(-1.)**(1-JSCAN)
DLONS=HI*DLON
DLATS=HJ*DLAT
RLONR=-(IM-1)/2*DLONS
RLATR=-(JM-1)/2*DLATS
SLAT1=SIN(RLAT1/DPR)
CLAT1=COS(RLAT1/DPR)
SLONR=SIN(RLONR/DPR)
CLONR=COS(RLONR/DPR)
SLATR=SIN(RLATR/DPR)
CLATR=COS(RLATR/DPR)
DENOM=1-(CLATR*SLONR)**2
SLAT0=(SLAT1*CLATR*CLONR-SLATR*SQRT(DENOM-SLAT1**2))/DENOM
CLAT0=SQRT(1-SLAT0**2)
RLON0=RLON1+HI*DPR*ACOS((CLAT0*CLATR*CLONR-SLAT0*SLATR)/CLAT1)
C THE FOLLOWING INDENTED LINES ARE A TEMPORARY FIX OF IMPRECISE GRID.
C CAUTION: CENTRAL LATITUDE AND LONGITUDE ARE ASSUMED TO BE INTEGERS.
SLAT0=SIN(NINT(ASIN(SLAT0)*DPR)/DPR)
CLAT0=SQRT(1-SLAT0**2)
RLON0=NINT(RLON0)
HS=SIGN(1.,MOD(RLON1-RLON0+180+3600,360.)-180)
CLON1=COS((RLON1-RLON0)/DPR)
SLATR=CLAT0*SLAT1-SLAT0*CLAT1*CLON1
CLATR=SQRT(1-SLATR**2)
CLONR=(CLAT0*CLAT1*CLON1+SLAT0*SLAT1)/CLATR
RLATR=DPR*ASIN(SLATR)
RLONR=HS*DPR*ACOS(CLONR)
DLATS=RLATR/(-(JM-1)/2)
DLONS=RLONR/(-(IM-1)/2)
XMIN=0
XMAX=IM+1
IF(IM.EQ.NINT(360/ABS(DLONS))) XMAX=IM+2
YMIN=0
YMAX=JM+1
NRET=0
C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C TRANSLATE GRID COORDINATES TO EARTH COORDINATES
IF(IOPT.EQ.0.OR.IOPT.EQ.1) THEN
DO N=1,NPTS
IF(XPTS(N).GE.XMIN.AND.XPTS(N).LE.XMAX.AND.
& YPTS(N).GE.YMIN.AND.YPTS(N).LE.YMAX) THEN
HS=HI*SIGN(1.,XPTS(N)-(IM+1)/2)
RLONR=(XPTS(N)-(IM+1)/2)*DLONS
RLATR=(YPTS(N)-(JM+1)/2)*DLATS
CLONR=COS(RLONR/DPR)
SLATR=SIN(RLATR/DPR)
CLATR=COS(RLATR/DPR)
SLAT=CLAT0*SLATR+SLAT0*CLATR*CLONR
IF(SLAT.LE.-1) THEN
CLAT=0.
CLON=COS(RLON0/DPR)
RLON(N)=0
RLAT(N)=-90
ELSEIF(SLAT.GE.1) THEN
CLAT=0.
CLON=COS(RLON0/DPR)
RLON(N)=0
RLAT(N)=90
ELSE
CLAT=SQRT(1-SLAT**2)
CLON=(CLAT0*CLATR*CLONR-SLAT0*SLATR)/CLAT
CLON=MIN(MAX(CLON,-1.),1.)
RLON(N)=MOD(RLON0+HS*DPR*ACOS(CLON)+3600,360.)
RLAT(N)=DPR*ASIN(SLAT)
ENDIF
NRET=NRET+1
IF(LROT.EQ.1) THEN
IF(IROT.EQ.1) THEN
IF(CLATR.LE.0) THEN
CROT(N)=-SIGN(1.,SLATR*SLAT0)
SROT(N)=0
ELSE
SLON=SIN((RLON(N)-RLON0)/DPR)
CROT(N)=(CLAT0*CLAT+SLAT0*SLAT*CLON)/CLATR
SROT(N)=SLAT0*SLON/CLATR
ENDIF
ELSE
CROT(N)=1
SROT(N)=0
ENDIF
ENDIF
ELSE
RLON(N)=FILL
RLAT(N)=FILL
ENDIF
ENDDO
C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C TRANSLATE EARTH COORDINATES TO GRID COORDINATES
ELSEIF(IOPT.EQ.-1) THEN
DO N=1,NPTS
IF(ABS(RLON(N)).LE.360.AND.ABS(RLAT(N)).LE.90) THEN
HS=SIGN(1.,MOD(RLON(N)-RLON0+180+3600,360.)-180)
CLON=COS((RLON(N)-RLON0)/DPR)
SLAT=SIN(RLAT(N)/DPR)
CLAT=COS(RLAT(N)/DPR)
SLATR=CLAT0*SLAT-SLAT0*CLAT*CLON
IF(SLATR.LE.-1) THEN
CLATR=0.
RLONR=0
RLATR=-90
ELSEIF(SLATR.GE.1) THEN
CLATR=0.
RLONR=0
RLATR=90
ELSE
CLATR=SQRT(1-SLATR**2)
CLONR=(CLAT0*CLAT*CLON+SLAT0*SLAT)/CLATR
CLONR=MIN(MAX(CLONR,-1.),1.)
RLONR=HS*DPR*ACOS(CLONR)
RLATR=DPR*ASIN(SLATR)
ENDIF
XPTS(N)=(IM+1)/2+RLONR/DLONS
YPTS(N)=(JM+1)/2+RLATR/DLATS
IF(XPTS(N).GE.XMIN.AND.XPTS(N).LE.XMAX.AND.
& YPTS(N).GE.YMIN.AND.YPTS(N).LE.YMAX) THEN
NRET=NRET+1
IF(LROT.EQ.1) THEN
IF(IROT.EQ.1) THEN
IF(CLATR.LE.0) THEN
CROT(N)=-SIGN(1.,SLATR*SLAT0)
SROT(N)=0
ELSE
SLON=SIN((RLON(N)-RLON0)/DPR)
CROT(N)=(CLAT0*CLAT+SLAT0*SLAT*CLON)/CLATR
SROT(N)=SLAT0*SLON/CLATR
ENDIF
ELSE
CROT(N)=1
SROT(N)=0
ENDIF
ENDIF
ELSE
XPTS(N)=FILL
YPTS(N)=FILL
ENDIF
ELSE
XPTS(N)=FILL
YPTS(N)=FILL
ENDIF
ENDDO
ENDIF
C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C PROJECTION UNRECOGNIZED
ELSE
IRET=-1
IF(IOPT.GE.0) THEN
DO N=1,NPTS
RLON(N)=FILL
RLAT(N)=FILL
ENDDO
ENDIF
IF(IOPT.LE.0) THEN
DO N=1,NPTS
XPTS(N)=FILL
YPTS(N)=FILL
ENDDO
ENDIF
ENDIF
C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
END