Subversion Repositories lagranto.icon

Rev

Go to most recent revision | 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 (z)
373
c                                        4: binary (p)
374
 
375
 
376
      implicit none
377
 
378
c     Declartion of function parameters
379
      integer   nx,ny,nz
380
      real      vert(nx*ny*nz)
381
      real      surf(nx*ny)
382
      real      rid,rjd,rkd
383
      real      xpo,ypo,ppo
384
      real      dlat,dlon,lats,lonw
385
      integer   mode
386
 
387
c     Numerical epsilon
388
      real      eps
389
      parameter (eps=1.e-8)
390
 
391
c     Local variables
392
      integer   i,j,k
393
      real      ppo0,ppo1,ppom,psur
394
      integer   i0,im,i1
395
 
396
c     Externals 
397
      real      int_index3
398
      external  int_index3
399
 
400
c     Get the horizontal grid indices
401
      rid=(xpo-lonw)/dlon+1.
402
      rjd=(ypo-lats)/dlat+1.
403
 
404
c     Two-dimensional interpolation on horizontal plane: return
405
      if ( nz.eq.1 ) then
406
         rkd = 1.
407
         goto 100
408
      endif
409
 
410
c     Lowest-level interpolation: return
411
      if ( abs(ppo).lt.eps ) then
412
         rkd = 1.
413
         goto 100
414
      endif
415
 
416
c     Get the pressure at the lowest level and at the surface 
417
      ppo0 = int_index3(vert,nx,ny,nz,rid,rjd,real(1),0.)
418
      psur = int_index3(surf,nx,ny, 1,rid,rjd,real(1),0.)
419
 
420
c     The point is between the surface and the lowest level: return
421
      if ( (ppo.ge.ppo0).and.(ppo.le.psur).or.
422
     >     (ppo.le.ppo0).and.(ppo.ge.psur) )
423
     >then 
424
         psur = int_index3(surf,nx,ny, 1,rid,rjd,real(1),0.)
425
c         rkd  = (psur-ppo)/(psur-ppo0)
426
          rkd = 1.
427
         goto 100
428
      endif
429
 
430
c     Full-level search (TH): linear ascending scanning through all levels
431
      if ( mode.eq.1 ) then
432
 
433
         ppo0 = int_index3(vert,nx,ny,nz,rid,rjd,real(1),0.)
434
         rkd=0
435
         do i=1,nz-1
436
            ppo1 = int_index3(vert,nx,ny,nz,rid,rjd,real(i+1),0.)
437
            if ( (ppo0.lt.ppo).and.(ppo1.ge.ppo) ) then
438
               rkd=real(i)+(ppo0-ppo)/(ppo0-ppo1)
439
               goto 100
440
            endif
441
            ppo0 = ppo1
442
         enddo
443
 
444
c     Full-level search (PV): linear descending scanning through all levels
445
      elseif ( mode.eq.2 ) then
446
 
447
         ppo1 = int_index3(vert,nx,ny,nz,rid,rjd,real(nz),0.)
448
         rkd=0
449
         do i=nz-1,1,-1
450
            ppo0 = int_index3(vert,nx,ny,nz,rid,rjd,real(i),0.)
451
            if ( (ppo1.gt.ppo).and.(ppo0.le.ppo) ) then
452
               rkd=real(i)+(ppo0-ppo)/(ppo0-ppo1)
453
               goto 100
454
            endif
455
            ppo1 = ppo0
456
         enddo
457
 
458
c     Full-level search (Z):  binary search
459
      elseif ( mode.eq.3 ) then
460
 
461
         rkd  = 0
462
         i0   = 1
463
         i1   = nz
464
         ppo0 = int_index3(vert,nx,ny,nz,rid,rjd,real( 1),0.)
465
         ppo1 = int_index3(vert,nx,ny,nz,rid,rjd,real(nz),0.)
466
 
467
         do while ( i1.gt.(i0+1) )
468
            im   = (i0+i1)/2
469
            ppom = int_index3(vert,nx,ny,nz,rid,rjd,real(im),0.)
470
            if (ppom.gt.ppo) then
471
               i1   = im
472
               ppo1 = ppom
473
            else
474
               i0   = im
475
               ppo0 = ppom
476
            endif
477
 
478
         enddo
479
 
480
         rkd=real(i0)+(ppo0-ppo)/(ppo0-ppo1)
481
 
482
c     Full-level search (P):  binary search
483
      elseif ( mode.eq.4 ) then
484
 
485
         rkd  = 0
486
         i0   = 1
487
         i1   = nz
488
         ppo0 = int_index3(vert,nx,ny,nz,rid,rjd,real( 1),0.)
489
         ppo1 = int_index3(vert,nx,ny,nz,rid,rjd,real(nz),0.)
490
 
491
         do while ( i1.gt.(i0+1) )
492
            im   = (i0+i1)/2
493
            ppom = int_index3(vert,nx,ny,nz,rid,rjd,real(im),0.)
494
            if (ppom.lt.ppo) then
495
               i1   = im
496
               ppo1 = ppom
497
            else
498
               i0   = im
499
               ppo0 = ppom
500
            endif
501
 
502
         enddo
503
 
504
         rkd=real(i0)+(ppo0-ppo)/(ppo0-ppo1)
505
 
506
      endif
507
 
508
c     Exit point for subroutine
509
 100  continue
510
 
511
      end
512