Subversion Repositories lagranto.um

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 michaesp 1
c      *************************************************************
2
c      * This package provides a general interpolaton routine      *
3
c      *************************************************************	
4
 
5
c     The main interface routines are:
6
c         get_index3,4 : get the grid indices for interpolation
7
c         int_index3,4 : interpolate to the grid position
8
 
9
c     -------------------------------------------------------------
10
c     Get index in grid space for interpolation
11
c     -------------------------------------------------------------
12
 
13
      subroutine get_index4 (rid,rjd,rkd,xpo,ypo,ppo,rtp,
14
     >                       vert0,vert1,surf0,surf1,mode,
15
     >                       nx,ny,nz,lonw,lats,dlon,dlat,misdat)
16
 
17
c     Purpose:
18
c        This subroutine determines the indices (rid,rjd,rkd) in grid 
19
c        space for a point in physical space (xpo,ypo,ppo). The 
20
c        horizontal grid is specified by the south-west point (lats,lonw)
21
c        and the grid spacing (dlat,dlon). The vertical grid is given
22
c        by <vert(n1,n2,n3)>. The lower boundary (typicall surface 
23
c        pressure) is given by <surf(n1,n2)>.
24
c     Arguments:
25
c        rid,rjd,rkd  real  output   grid location to be interpolated to
26
c        xpo,ypo,ppo  real  input    physical coordinates
27
c        rtp          real  input    relative time position (0=beginning, 1=end)
28
c        n1,n2,n3     int   input    grid dimensions in x-, y- and p-direction
29
c        lats,lonw    real  input    south and west boundary of grid space
30
c        vert         real  input    vertical coordinate grid
31
c        surf         real  input    lower boundary (surface pressure)
32
c        mode         int   input    direction of vertical axis (p=1,th=-1)
33
c                                        1: linear, 1 -> nz (th)
34
c                                        2: linear, nz -> 1 (pv)
35
c                                        3: binary (p)
36
 
37
      implicit none
38
 
39
c     Declartion of function parameters
40
      integer   nx,ny,nz
41
      real      xpo,ypo,ppo,rtp
42
      real      vert0(nx*ny*nz),vert1(nx*ny*nz)
43
      real      surf0(nx*ny)   ,surf1(nx*ny*nz)
44
      real      rid,rjd,rkd
45
      real      dlat,dlon,lats,lonw
46
      real      misdat
47
      integer   mode
48
 
49
c     Set numerical parameters
50
      real      eps
51
      parameter (eps=1.e-8)
52
 
53
c     Auxiliary variables
54
      real      rid0,rjd0,rkd0,rid1,rjd1,rkd1
55
 
56
c     Externals 
57
      real      int_time
58
      external  int_time
59
 
60
c     Get the inidices
61
      if (abs(rtp).lt.eps) then
62
         call  get_index3 (rid,rjd,rkd,xpo,ypo,ppo,mode,
63
     >                     vert0,surf0,nx,ny,nz,lonw,lats,dlon,dlat)
64
      elseif (abs(rtp-1.).lt.eps) then
65
         call  get_index3 (rid,rjd,rkd,xpo,ypo,ppo,mode,
66
     >                     vert1,surf1,nx,ny,nz,lonw,lats,dlon,dlat)
67
      else
68
         call  get_index3 (rid0,rjd0,rkd0,xpo,ypo,ppo,mode,
69
     >                     vert0,surf0,nx,ny,nz,lonw,lats,dlon,dlat)
70
         call  get_index3 (rid1,rjd1,rkd1,xpo,ypo,ppo,mode,
71
     >                     vert1,surf1,nx,ny,nz,lonw,lats,dlon,dlat)
72
         rid = int_time (rid0,rid1,rtp,misdat)
73
         rjd = int_time (rjd0,rjd1,rtp,misdat)
74
         rkd = int_time (rkd0,rkd1,rtp,misdat)
75
 
76
      endif
77
 
78
      end
79
 
80
c     -------------------------------------------------------------
81
c     Interpolate to an arbitrary position in grid space and time
82
c     -------------------------------------------------------------
83
 
84
      real function int_index4 (ar0,ar1,n1,n2,n3,rid,rjd,rkd,rtp,misdat)
85
 
86
c     Purpose:
87
c        This subroutine interpolates a 3d-array to an arbitrary
88
c        location within the grid including a linear time-interpolation. 
89
c     Arguments:
90
c        rid,rjd,rkd  real  output   grid location to be interpolated to
91
c        xpo,ypo,ppo  real  input    physical coordinates
92
c        n1,n2,n3     int   input    grid dimensions in x-, y- and p-direction
93
c        lats,lonw    real  input    south and west boundary of grid space
94
c        vert         real  input    vertical coordinate grid
95
c        surf         real  input    lower boundary (surface pressure)
96
 
97
      implicit none
98
 
99
c     Declartion of function parameters
100
      integer   n1,n2,n3
101
      real      ar0(n1*n2*n3),ar1(n1*n2*n3)
102
      real      rid,rjd,rkd
103
      real      rtp
104
      real      misdat
105
 
106
c     Set numerical parameters
107
      real      eps
108
      parameter (eps=1.e-8)
109
 
110
c     Externals  
111
      real      int_index3,int_time
112
      external  int_index3,int_time
113
 
114
c     Auxiliary variables
115
      real      val0,val1,val
116
 
117
c     Do the 3d-interpolation
118
      if (abs(rtp).lt.eps) then
119
         val = int_index3 (ar0,n1,n2,n3,rid,rjd,rkd,misdat)
120
      elseif (abs(rtp-1.).lt.eps) then
121
         val = int_index3 (ar1,n1,n2,n3,rid,rjd,rkd,misdat)
122
      else
123
         val0 = int_index3 (ar0,n1,n2,n3,rid,rjd,rkd,misdat)
124
         val1 = int_index3 (ar1,n1,n2,n3,rid,rjd,rkd,misdat)
125
         val  = int_time (val0,val1,rtp,misdat)
126
      endif
127
 
128
c     Return value
129
      int_index4 = val
130
 
131
      return
132
      end
133
 
134
 
135
c     -------------------------------------------------------------
136
c     Interpolate to an arbitrary position in grid space
137
c     -------------------------------------------------------------
138
 
139
      real function int_index3 (ar,n1,n2,n3,rid,rjd,rkd,misdat)
140
 
141
c     Purpose:
142
c        This subroutine interpolates a 3d-array to an arbitrary
143
c        location within the grid. The interpolation includes the 
144
c        testing of the missing data flag 'misdat'. If one dimension
145
c        is 1, a 2d-interpolation is performed; if two dimensions
146
c        are 1, it is a 1d-interpolation; if all three dimensions are
147
c        1, no interpolation is performed and the input value is
148
c        returned.
149
c     Arguments:
150
c        ar        real  input   input data array
151
c        n1,n2,n3  int   input   dimensions of ar
152
c        ri,rj,rk  real  input   grid location to be interpolated to
153
c        misdat    real  input   missing data flag (on if misdat<>0)
154
 
155
      implicit none
156
 
157
c     Declartion of function parameters 
158
      integer   n1,n2,n3
159
      real      ar(n1*n2*n3)
160
      real      rid,rjd,rkd
161
      real      misdat
162
 
163
c     Set numerical parameters
164
      real      eps
165
      parameter (eps=1.e-8)
166
 
167
c     Local variables
168
      integer   i,j,k,ip1,jp1,kp1
169
      real      frac0i,frac0j,frac0k,frac1i,frac1j,frac1k
170
      real      ri,rj,rk
171
      real      val000,val001,val010,val011,val100,val101,val110,val111
172
      real      frc000,frc001,frc010,frc011,frc100,frc101,frc110,frc111
173
      real      frc
174
      real      mdv
175
      real      val
176
 
177
c     Elementary test for dimensions
178
      if ( (n1.lt.1).or.(n2.lt.1).or.(n3.lt.1) ) then
179
         print*,'Invalid grid dimensions ',n1,n2,n3
180
         stop
181
      endif
182
 
183
c     Activate or inactive the missing data check (quick and dirty)
184
      if (misdat.ne.0.) then
185
         mdv = misdat
186
      else
187
         mdv = 257.22725394015
188
      endif
189
 
190
c     Bring the indices into the grid space
191
      ri = amax1(1.,amin1(float(n1),rid))
192
      rj = amax1(1.,amin1(float(n2),rjd))
193
      rk = amax1(1.,amin1(float(n3),rkd))
194
 
195
c     Get the index of the west-south-bottom corner of the box
196
      i   = min0(int(ri),n1-1)
197
      ip1 = i+1
198
      j   = min0(int(rj),n2-1)
199
      jp1 = j+1
200
      k   = min0(int(rk),n3-1)
201
      kp1 = k+1
202
 
203
c     Special handling for 2d arrays
204
      if (n3.eq.1) then
205
         k=1
206
         kp1=1
207
      endif
208
 
209
c     Get location relative to grid box
210
      if ( i.ne.ip1 ) then
211
         frac0i = ri-float(i)
212
         frac1i = 1.-frac0i
213
      else
214
         frac0i = 0.
215
         frac1i = 1.
216
      endif
217
      if ( j.ne.jp1 ) then
218
         frac0j = rj-float(j)
219
         frac1j = 1.-frac0j
220
      else
221
         frac0j = 0.
222
         frac1j = 1.
223
      endif
224
      if ( k.ne.kp1 ) then
225
         frac0k = rk-float(k)
226
         frac1k = 1.-frac0k
227
      else
228
         frac0k = 0.
229
         frac1k = 1.
230
      endif
231
 
232
c     On a grid point - take the grid point value 
233
      if ( ( abs(frac0i).lt.eps ).and.
234
     >     ( abs(frac0j).lt.eps ).and.
235
     >     ( abs(frac0k).lt.eps ) ) then
236
 
237
         val = ar( i + n1*(j -1) + n1*n2*(k -1) )
238
         goto 100
239
 
240
      endif
241
 
242
c     Init the fractions
243
      frc000 = frac1i * frac1j * frac1k
244
      frc001 = frac0i * frac1j * frac1k
245
      frc010 = frac1i * frac0j * frac1k
246
      frc011 = frac0i * frac0j * frac1k
247
      frc100 = frac1i * frac1j * frac0k
248
      frc101 = frac0i * frac1j * frac0k
249
      frc110 = frac1i * frac0j * frac0k
250
      frc111 = frac0i * frac0j * frac0k
251
 
252
c     Init the values
253
      val000 = ar( i   + n1*(j  -1) + n1*n2*(k  -1) )
254
      val001 = ar( ip1 + n1*(j  -1) + n1*n2*(k  -1) )
255
      val010 = ar( i   + n1*(jp1-1) + n1*n2*(k  -1) )
256
      val011 = ar( ip1 + n1*(jp1-1) + n1*n2*(k  -1) )
257
      val100 = ar( i   + n1*(j  -1) + n1*n2*(kp1-1) )
258
      val101 = ar( ip1 + n1*(j  -1) + n1*n2*(kp1-1) )
259
      val110 = ar( i   + n1*(jp1-1) + n1*n2*(kp1-1) )
260
      val111 = ar( ip1 + n1*(jp1-1) + n1*n2*(kp1-1) )
261
 
262
c     Handle missing data
263
      if ( abs(val000-mdv).lt.eps ) frc000 = 0.
264
      if ( abs(val001-mdv).lt.eps ) frc001 = 0.
265
      if ( abs(val010-mdv).lt.eps ) frc010 = 0.
266
      if ( abs(val011-mdv).lt.eps ) frc011 = 0.
267
      if ( abs(val100-mdv).lt.eps ) frc100 = 0.
268
      if ( abs(val101-mdv).lt.eps ) frc101 = 0.
269
      if ( abs(val110-mdv).lt.eps ) frc110 = 0.
270
      if ( abs(val111-mdv).lt.eps ) frc111 = 0.
271
 
272
c     Build the final value
273
      frc = frc000 + frc001 + frc010 + frc011 + 
274
     >      frc100 + frc101 + frc110 + frc111   
275
      if ( frc.gt.0. ) then
276
         val = 1./frc * ( frc000 * val000 + frc001 * val001 +
277
     >                    frc010 * val010 + frc011 * val011 +
278
     >                    frc100 * val100 + frc101 * val101 +
279
     >                    frc110 * val110 + frc111 * val111 )
280
      else
281
         val = misdat
282
      endif
283
 
284
c     Return the value 
285
 100  continue
286
 
287
      int_index3 = val
288
 
289
      end
290
 
291
 
292
c     -------------------------------------------------------------
293
c     Time interpolation
294
c     -------------------------------------------------------------
295
 
296
      real function int_time (val0,val1,reltpos,misdat)
297
 
298
c     Purpose:
299
c        This subroutine interpolates linearly in time between two
300
c        values.
301
c     Arguments:
302
c        val0      real  input   value at time 0
303
c        val1      real  input   value at time 1
304
c        reltpos   real  input   relative time (between 0 and 1)
305
c        misdat    real  input   missing data flag (on if misdat<>0)
306
 
307
      implicit none
308
 
309
c     Declaration of parameters
310
      real      val0
311
      real      val1
312
      real      reltpos
313
      real      misdat
314
 
315
c     Numerical epsilon
316
      real      eps
317
      parameter (eps=1.e-8)
318
 
319
c     Local variables
320
      real      val
321
      real      mdv
322
 
323
c     Activate or inactive the missing data check (quick and dirty)
324
      if (misdat.ne.0.) then
325
         mdv = misdat
326
      else
327
         mdv = 257.22725394015
328
      endif
329
 
330
c     Do the linear interpolation
331
      if ( abs(reltpos).lt.eps ) then
332
         val = val0
333
      elseif ( abs(reltpos-1.).lt.eps ) then
334
         val = val1
335
      elseif ( (abs(val0-mdv).gt.eps).and.
336
     >         (abs(val1-mdv).gt.eps) ) then
337
         val = (1.-reltpos)*val0+reltpos*val1
338
      else
339
         val = mdv
340
      endif
341
 
342
c     Return value
343
      int_time = val
344
 
345
      end
346
 
347
 
348
c     -------------------------------------------------------------
349
c     Get the position of a physical point in grid space
350
c     -------------------------------------------------------------
351
 
352
      subroutine get_index3 (rid,rjd,rkd,xpo,ypo,ppo,mode,
353
     >                       vert,surf,nx,ny,nz,lonw,lats,dlon,dlat)
354
 
355
c     Purpose:
356
c        This subroutine determines the indices (rid,rjd,rkd) in grid 
357
c        space for a point in physical space (xpo,ypo,ppo). The 
358
c        horizontal grid is specified by the south-west point (lats,lonw)
359
c        and the grid spacing (dlat,dlon). The vertical grid is given
360
c        by <vert(n1,n2,n3)>. The lower boundary (typicall surface 
361
c        pressure) is given by <surf(n1,n2)>.
362
c     Arguments:
363
c        rid,rjd,rkd  real  output   grid location to be interpolated to
364
c        xpo,ypo,ppo  real  input    physical coordinates
365
c        n1,n2,n3     int   input    grid dimensions in x-, y- and p-direction
366
c        lats,lonw    real  input    south and west boundary of grid space
367
c        vert         real  input    vertical coordinate grid
368
c        surf         real  input    lower boundary (surface pressure)
369
c        mode         int   input    direction of vertical axis 
370
c                                        1: linear, 1 -> nz (th)
371
c                                        2: linear, nz -> 1 (pv)
372
c                                        3: binary (p)
373
 
374
 
375
      implicit none
376
 
377
c     Declartion of function parameters
378
      integer   nx,ny,nz
379
      real      vert(nx*ny*nz)
380
      real      surf(nx*ny)
381
      real      rid,rjd,rkd
382
      real      xpo,ypo,ppo
383
      real      dlat,dlon,lats,lonw
384
      integer   mode
385
 
386
c     Numerical epsilon
387
      real      eps
388
      parameter (eps=1.e-8)
389
 
390
c     Local variables
391
      integer   i,j,k
392
      real      ppo0,ppo1,ppom,psur
393
      integer   i0,im,i1
394
 
395
c     Externals 
396
      real      int_index3
397
      external  int_index3
398
 
399
c     Get the horizontal grid indices
400
      rid=(xpo-lonw)/dlon+1.
401
      rjd=(ypo-lats)/dlat+1.
402
 
403
c     Two-dimensional interpolation on horizontal plane: return
404
      if ( nz.eq.1 ) then
405
         rkd = 1.
406
         goto 100
407
      endif
408
 
409
c     Lowest-level interpolation: return
410
      if ( abs(ppo-1050.).lt.eps ) then
411
         rkd = 1.
412
         goto 100
413
      endif
414
 
415
c     Get the pressure at the lowest level and at the surface 
416
      ppo0 = int_index3(vert,nx,ny,nz,rid,rjd,real(1),0.)
417
      psur = int_index3(surf,nx,ny, 1,rid,rjd,real(1),0.)
418
 
419
c     The point is between the surface and the lowest level: return
420
      if ( (ppo.ge.ppo0).and.(ppo.le.psur).or.
421
     >     (ppo.le.ppo0).and.(ppo.ge.psur) )
422
     >then 
423
         psur = int_index3(surf,nx,ny, 1,rid,rjd,real(1),0.)
424
         rkd  = (psur-ppo)/(psur-ppo0)
425
         goto 100
426
      endif
427
 
428
c     Full-level search (TH): linear ascending scanning through all levels
429
      if ( mode.eq.1 ) then
430
 
431
         ppo0 = int_index3(vert,nx,ny,nz,rid,rjd,real(1),0.)
432
         rkd=0
433
         do i=1,nz-1
434
            ppo1 = int_index3(vert,nx,ny,nz,rid,rjd,real(i+1),0.)
435
            if ( (ppo0.lt.ppo).and.(ppo1.ge.ppo) ) then
436
               rkd=real(i)+(ppo0-ppo)/(ppo0-ppo1)
437
               goto 100
438
            endif
439
            ppo0 = ppo1
440
         enddo
441
 
442
c     Full-level search (PV): linear descending scanning through all levels
443
      elseif ( mode.eq.2 ) then
444
 
445
         ppo1 = int_index3(vert,nx,ny,nz,rid,rjd,real(nz),0.)
446
         rkd=0
447
         do i=nz-1,1,-1
448
            ppo0 = int_index3(vert,nx,ny,nz,rid,rjd,real(i),0.)
449
            if ( (ppo1.gt.ppo).and.(ppo0.le.ppo) ) then
450
               rkd=real(i)+(ppo0-ppo)/(ppo0-ppo1)
451
               goto 100
452
            endif
453
            ppo1 = ppo0
454
         enddo
455
 
456
c     Full-level search (P):  binary search 
457
      elseif ( mode.eq.3 ) then
458
 
459
         rkd  = 0
460
         i0   = 1
461
         i1   = nz
462
         ppo0 = int_index3(vert,nx,ny,nz,rid,rjd,real( 1),0.)
463
         ppo1 = int_index3(vert,nx,ny,nz,rid,rjd,real(nz),0.)
464
 
465
         do while ( i1.gt.(i0+1) )
466
            im   = (i0+i1)/2
467
            ppom = int_index3(vert,nx,ny,nz,rid,rjd,real(im),0.)
468
            if (ppom.lt.ppo) then
469
               i1   = im
470
               ppo1 = ppom
471
            else
472
               i0   = im
473
               ppo0 = ppom
474
            endif
475
         enddo
476
 
477
         rkd=real(i0)+(ppo0-ppo)/(ppo0-ppo1)
478
 
479
      endif
480
 
481
c     Exit point for subroutine
482
 100  continue
483
 
484
      end
485