forked from erget/wgrib2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polates2.f
223 lines (223 loc) · 9.04 KB
/
polates2.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
C-----------------------------------------------------------------------
SUBROUTINE POLATES2(IPOPT,KGDSI,KGDSO,MI,MO,KM,IBI,LI,GI,
& NO,RLAT,RLON,IBO,LO,GO,IRET)
C$$$ SUBPROGRAM DOCUMENTATION BLOCK
C
C SUBPROGRAM: POLATES2 INTERPOLATE SCALAR FIELDS (NEIGHBOR)
C PRGMMR: IREDELL ORG: W/NMC23 DATE: 96-04-10
C
C ABSTRACT: THIS SUBPROGRAM PERFORMS NEIGHBOR INTERPOLATION
C FROM ANY GRID TO ANY GRID FOR SCALAR FIELDS.
C OPTIONS ALLOW CHOOSING THE WIDTH OF THE GRID SQUARE
C (IPOPT(1)) TO SEARCH FOR VALID DATA, WHICH DEFAULTS TO 1
C (IF IPOPT(1)=-1). ODD WIDTH SQUARES ARE CENTERED ON
C THE NEAREST INPUT GRID POINT; EVEN WIDTH SQUARES ARE
C CENTERED ON THE NEAREST FOUR INPUT GRID POINTS.
C SQUARES ARE SEARCHED FOR VALID DATA IN A SPIRAL PATTERN
C STARTING FROM THE CENTER. NO SEARCHING IS DONE WHERE
C THE OUTPUT GRID IS OUTSIDE THE INPUT GRID.
C ONLY HORIZONTAL INTERPOLATION IS PERFORMED.
C THE GRIDS ARE DEFINED BY THEIR GRID DESCRIPTION SECTIONS
C (PASSED IN INTEGER FORM AS DECODED BY SUBPROGRAM W3FI63).
C THE CURRENT CODE RECOGNIZES THE FOLLOWING PROJECTIONS:
C (KGDS(1)=000) EQUIDISTANT CYLINDRICAL
C (KGDS(1)=001) MERCATOR CYLINDRICAL
C (KGDS(1)=003) LAMBERT CONFORMAL CONICAL
C (KGDS(1)=004) GAUSSIAN CYLINDRICAL (SPECTRAL NATIVE)
C (KGDS(1)=005) POLAR STEREOGRAPHIC AZIMUTHAL
C (KGDS(1)=202) ROTATED EQUIDISTANT CYLINDRICAL (ETA NATIVE)
C WHERE KGDS COULD BE EITHER INPUT KGDSI OR OUTPUT KGDSO.
C AS AN ADDED BONUS THE NUMBER OF OUTPUT GRID POINTS
C AND THEIR LATITUDES AND LONGITUDES ARE ALSO RETURNED.
C ON THE OTHER HAND, THE OUTPUT CAN BE A SET OF STATION POINTS
C IF KGDSO(1)<0, IN WHICH CASE THE NUMBER OF POINTS
C AND THEIR LATITUDES AND LONGITUDES MUST BE INPUT.
C INPUT BITMAPS WILL BE INTERPOLATED TO OUTPUT BITMAPS.
C OUTPUT BITMAPS WILL ALSO BE CREATED WHEN THE OUTPUT GRID
C EXTENDS OUTSIDE OF THE DOMAIN OF THE INPUT GRID.
C THE OUTPUT FIELD IS SET TO 0 WHERE THE OUTPUT BITMAP IS OFF.
C
C PROGRAM HISTORY LOG:
C 96-04-10 IREDELL
C 1999-04-08 IREDELL SPLIT IJKGDS INTO TWO PIECES
C 2001-06-18 IREDELL INCLUDE SPIRAL SEARCH OPTION
C 2006-01-04 GAYNO MINOR BUG FIX
C 2007-10-30 IREDELL SAVE WEIGHTS AND THREAD FOR PERFORMANCE
C
C USAGE: CALL POLATES2(IPOPT,KGDSI,KGDSO,MI,MO,KM,IBI,LI,GI,
C & NO,RLAT,RLON,IBO,LO,GO,IRET)
C
C INPUT ARGUMENT LIST:
C IPOPT - INTEGER (20) INTERPOLATION OPTIONS
C IPOPT(1) IS WIDTH OF SQUARE TO EXAMINE IN SPIRAL SEARCH
C (DEFAULTS TO 1 IF IPOPT(1)=-1)
C KGDSI - INTEGER (200) INPUT GDS PARAMETERS AS DECODED BY W3FI63
C KGDSO - INTEGER (200) OUTPUT GDS PARAMETERS
C (KGDSO(1)<0 IMPLIES RANDOM STATION POINTS)
C MI - INTEGER SKIP NUMBER BETWEEN INPUT GRID FIELDS IF KM>1
C OR DIMENSION OF INPUT GRID FIELDS IF KM=1
C MO - INTEGER SKIP NUMBER BETWEEN OUTPUT GRID FIELDS IF KM>1
C OR DIMENSION OF OUTPUT GRID FIELDS IF KM=1
C KM - INTEGER NUMBER OF FIELDS TO INTERPOLATE
C IBI - INTEGER (KM) INPUT BITMAP FLAGS
C LI - LOGICAL*1 (MI,KM) INPUT BITMAPS (IF SOME IBI(K)=1)
C GI - REAL (MI,KM) INPUT FIELDS TO INTERPOLATE
C NO - INTEGER NUMBER OF OUTPUT POINTS (ONLY IF KGDSO(1)<0)
C RLAT - REAL (NO) OUTPUT LATITUDES IN DEGREES (IF KGDSO(1)<0)
C RLON - REAL (NO) OUTPUT LONGITUDES IN DEGREES (IF KGDSO(1)<0)
C
C OUTPUT ARGUMENT LIST:
C NO - INTEGER NUMBER OF OUTPUT POINTS (ONLY IF KGDSO(1)>=0)
C RLAT - REAL (MO) OUTPUT LATITUDES IN DEGREES (IF KGDSO(1)>=0)
C RLON - REAL (MO) OUTPUT LONGITUDES IN DEGREES (IF KGDSO(1)>=0)
C IBO - INTEGER (KM) OUTPUT BITMAP FLAGS
C LO - LOGICAL*1 (MO,KM) OUTPUT BITMAPS (ALWAYS OUTPUT)
C GO - REAL (MO,KM) OUTPUT FIELDS INTERPOLATED
C IRET - INTEGER RETURN CODE
C 0 SUCCESSFUL INTERPOLATION
C 2 UNRECOGNIZED INPUT GRID OR NO GRID OVERLAP
C 3 UNRECOGNIZED OUTPUT GRID
C
C SUBPROGRAMS CALLED:
C GDSWIZ GRID DESCRIPTION SECTION WIZARD
C IJKGDS0 SET UP PARAMETERS FOR IJKGDS1
C (IJKGDS1) RETURN FIELD POSITION FOR A GIVEN GRID POINT
C POLFIXS MAKE MULTIPLE POLE SCALAR VALUES CONSISTENT
C
C ATTRIBUTES:
C LANGUAGE: FORTRAN 77
C
C$$$
IMPLICIT NONE
INTEGER,INTENT(IN):: IPOPT(20),KGDSI(200),KGDSO(200),MI,MO,KM
INTEGER,INTENT(IN):: IBI(KM)
LOGICAL*1,INTENT(IN):: LI(MI,KM)
REAL,INTENT(IN):: GI(MI,KM)
INTEGER,INTENT(INOUT):: NO
REAL,INTENT(INOUT):: RLAT(MO),RLON(MO)
INTEGER,INTENT(OUT):: IBO(KM)
LOGICAL*1,INTENT(OUT):: LO(MO,KM)
REAL,INTENT(OUT):: GO(MO,KM)
INTEGER,INTENT(OUT):: IRET
REAL XPTS(MO),YPTS(MO)
INTEGER IJKGDSA(20)
REAL,PARAMETER:: FILL=-9999.
INTEGER MSPIRAL,N,K,NK,NV,IJKGDS1
INTEGER I1,J1,IXS,JXS,MX,KXS,KXT,IX,JX,NX
REAL DUM
INTEGER,SAVE:: KGDSIX(200)=-1,KGDSOX(200)=-1,NOX=-1,IRETX=-1
INTEGER,ALLOCATABLE,SAVE:: NXY(:)
REAL,ALLOCATABLE,SAVE:: RLATX(:),RLONX(:),XPTSX(:),YPTSX(:)
C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C SET PARAMETERS
IRET=0
MSPIRAL=MAX(IPOPT(1),1)
C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C SAVE OR SKIP WEIGHT COMPUTATION
IF(IRET.EQ.0.AND.(KGDSO(1).LT.0.OR.
& ANY(KGDSI.NE.KGDSIX).OR.ANY(KGDSO.NE.KGDSOX))) THEN
C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C COMPUTE NUMBER OF OUTPUT POINTS AND THEIR LATITUDES AND LONGITUDES.
IF(KGDSO(1).GE.0) THEN
CALL GDSWIZ(KGDSO, 0,MO,FILL,XPTS,YPTS,RLON,RLAT,NO,0,DUM,DUM)
IF(NO.EQ.0) IRET=3
ENDIF
C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C LOCATE INPUT POINTS
CALL GDSWIZ(KGDSI,-1,NO,FILL,XPTS,YPTS,RLON,RLAT,NV,0,DUM,DUM)
IF(IRET.EQ.0.AND.NV.EQ.0) IRET=2
C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C ALLOCATE AND SAVE GRID DATA
KGDSIX=KGDSI
KGDSOX=KGDSO
IF(NOX.NE.NO) THEN
IF(NOX.GE.0) DEALLOCATE(RLATX,RLONX,XPTSX,YPTSX,NXY)
ALLOCATE(RLATX(NO),RLONX(NO),XPTSX(NO),YPTSX(NO),NXY(NO))
NOX=NO
ENDIF
IRETX=IRET
C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C COMPUTE WEIGHTS
IF(IRET.EQ.0) THEN
CALL IJKGDS0(KGDSI,IJKGDSA)
DO N=1,NO
RLONX(N)=RLON(N)
RLATX(N)=RLAT(N)
XPTSX(N)=XPTS(N)
YPTSX(N)=YPTS(N)
IF(XPTS(N).NE.FILL.AND.YPTS(N).NE.FILL) THEN
NXY(N)=IJKGDS1(NINT(XPTS(N)),NINT(YPTS(N)),IJKGDSA)
ELSE
NXY(N)=0
ENDIF
ENDDO
ENDIF
ENDIF
C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C INTERPOLATE OVER ALL FIELDS
IF(IRET.EQ.0.AND.IRETX.EQ.0) THEN
IF(KGDSO(1).GE.0) THEN
NO=NOX
DO N=1,NO
RLON(N)=RLONX(N)
RLAT(N)=RLATX(N)
ENDDO
ENDIF
DO N=1,NO
XPTS(N)=XPTSX(N)
YPTS(N)=YPTSX(N)
ENDDO
DO NK=1,NO*KM
K=(NK-1)/NO+1
N=NK-NO*(K-1)
GO(N,K)=0
LO(N,K)=.FALSE.
IF(NXY(N).GT.0) THEN
IF(IBI(K).EQ.0.OR.LI(NXY(N),K)) THEN
GO(N,K)=GI(NXY(N),K)
LO(N,K)=.TRUE.
C SPIRAL AROUND UNTIL VALID DATA IS FOUND.
ELSEIF(MSPIRAL.GT.1) THEN
I1=NINT(XPTS(N))
J1=NINT(YPTS(N))
IXS=SIGN(1.,XPTS(N)-I1)
JXS=SIGN(1.,YPTS(N)-J1)
DO MX=2,MSPIRAL**2
KXS=SQRT(4*MX-2.5)
KXT=MX-(KXS**2/4+1)
SELECT CASE(MOD(KXS,4))
CASE(1)
IX=I1-IXS*(KXS/4-KXT)
JX=J1-JXS*KXS/4
CASE(2)
IX=I1+IXS*(1+KXS/4)
JX=J1-JXS*(KXS/4-KXT)
CASE(3)
IX=I1+IXS*(1+KXS/4-KXT)
JX=J1+JXS*(1+KXS/4)
CASE DEFAULT
IX=I1-IXS*KXS/4
JX=J1+JXS*(KXS/4-KXT)
END SELECT
NX=IJKGDS1(IX,JX,IJKGDSA)
IF(NX.GT.0.AND.LI(NX,K)) THEN
GO(N,K)=GI(NX,K)
LO(N,K)=.TRUE.
EXIT
ENDIF
ENDDO
ENDIF
ENDIF
ENDDO
DO K=1,KM
IBO(K)=IBI(K)
IF(.NOT.ALL(LO(1:NO,K))) IBO(K)=1
ENDDO
IF(KGDSO(1).EQ.0) CALL POLFIXS(NO,MO,KM,RLAT,RLON,IBO,LO,GO)
C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ELSE
IF(IRET.EQ.0) IRET=IRETX
IF(KGDSO(1).GE.0) NO=0
ENDIF
C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
END