Subversion Repositories lagranto.ecmwf

Rev

Rev 3 | Rev 13 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 5
1
c     ****************************************************************
1
c     ****************************************************************
2
c     * This package provides IO routines for trajectories. A file   *
2
c     * This package provides IO routines for trajectories. A file   *
3
c     * is characterised by the filename <filename> and the file     *
3
c     * is characterised by the filename <filename> and the file     *
4
c     * identifier <fid>. Different modes <mode> are supported:      *
4
c     * identifier <fid>. Different modes <mode> are supported:      *
5
c     *     mode=1: ascii, sorted by trajectory;                     *
5
c     *     mode=1: ascii, sorted by trajectory;                     *
6
c     *     mode=2: ascii, sorted by time;                           *
6
c     *     mode=2: ascii, sorted by time;                           *
7
c     *     mode=3: fortran (unformatted)                            * 
7
c     *     mode=3: fortran (unformatted)                            * 
8
c     *     mode=4: IVE netcdf (for compatibiltzy reasons)           *
8
c     *     mode=4: IVE netcdf (for compatibiltzy reasons)           *
9
c     * A trajectory set is given as 3d array <tra(ntra,ntim,ncol)>  *
9
c     * A trajectory set is given as 3d array <tra(ntra,ntim,ncol)>  *
10
c     * where <ntra> is the number of trajectories, <ntim> the       *
10
c     * where <ntra> is the number of trajectories, <ntim> the       *
11
c     * number of times of each trajectory and <ncol> the number of  *
11
c     * number of times of each trajectory and <ncol> the number of  *
12
c     * columns of the trajectory. The first 4 columns are: time,    *
12
c     * columns of the trajectory. The first 4 columns are: time,    *
13
c     * longitude, latitude, pressure. The other columns are traced  *
13
c     * longitude, latitude, pressure. The other columns are traced  *
14
c     * fields. The complete list of all columns is given in the     *
14
c     * fields. The complete list of all columns is given in the     *
15
c     * array <vars(ncol)>. Finally, the reference date is given in  *
15
c     * array <vars(ncol)>. Finally, the reference date is given in  *
16
c     * the array <time(6)=year,month,day,hour,time length of the    *
16
c     * the array <time(6)=year,month,day,hour,time length of the    *
17
c     * trajectory (hour,min)>.                                      *
17
c     * trajectory (hour,min)>.                                      *
18
c     *                                                              *
18
c     *                                                              *
19
c     * Author: Michael Sprenger, September 2008                     *
19
c     * Author: Michael Sprenger, September 2008                     *
20
c     ****************************************************************
20
c     ****************************************************************
21
 
21
 
22
c     ----------------------------------------------------------------
22
c     ----------------------------------------------------------------
23
c     Open a trajectory file for reading
23
c     Open a trajectory file for reading
24
c     ----------------------------------------------------------------
24
c     ----------------------------------------------------------------
25
      
25
      
26
      subroutine ropen_tra(fid,filename,ntra,ntim,ncol,time,vars,mode)
26
      subroutine ropen_tra(fid,filename,ntra,ntim,ncol,time,vars,mode)
27
 
27
 
28
      implicit none
28
      implicit none
29
      
29
      
30
c     Declaration of subroutine parameters
30
c     Declaration of subroutine parameters
31
      integer      fid
31
      integer      fid
32
      character*80 filename
32
      character*80 filename
33
      integer      mode
33
      integer      mode
34
      integer      ntra,ntim,ncol
34
      integer      ntra,ntim,ncol
35
      integer      time(6)
35
      integer      time(6)
36
      character*80 vars(ncol)
36
      character*80 vars(ncol)
37
 
37
 
38
c     Auxiliary variables
38
c     Auxiliary variables
39
      integer      vardim(4)
39
      integer      vardim(4)
40
      real         varmin(4),varmax(4),stag(4)
40
      real         varmin(4),varmax(4),stag(4)
41
      real         mdv
41
      real         mdv
42
      character*80 cfn
42
      character*80 cfn
43
      integer      ierr
43
      integer      ierr
44
      integer      i
44
      integer      i
45
      integer      nvars
45
      integer      nvars
46
 
46
 
47
c     Open file
47
c     Open file
48
      if (mode.eq.1) then
48
      if (mode.eq.1) then
49
         fid = 10
49
         fid = 10
50
         open(fid,file=filename)
50
         open(fid,file=filename)
51
      elseif (mode.eq.2) then
51
      elseif (mode.eq.2) then
52
         fid = 10
52
         fid = 10
53
         open(fid,file=filename)
53
         open(fid,file=filename)
54
      elseif (mode.eq.3) then
54
      elseif (mode.eq.3) then
55
         open(fid,file=filename,form='unformatted')
55
         open(fid,file=filename,form='unformatted')
56
      elseif (mode.eq.4) then
56
      elseif (mode.eq.4) then
57
         call cdfopn(filename,fid,ierr)
57
         call cdfopn(filename,fid,ierr)
-
 
58
      elseif (mode.eq.5) then
-
 
59
         print*,' ERROR: Reading KML not supported'
-
 
60
         stop
58
      endif
61
      endif
59
 
62
 
60
c     Read header information
63
c     Read header information
61
      call read_hea(fid,time,vars,ntra,ntim,ncol,mode)
64
      call read_hea(fid,time,vars,ntra,ntim,ncol,mode)
62
 
65
 
63
      end
66
      end
64
      
67
      
65
 
68
 
66
c     ----------------------------------------------------------------
69
c     ----------------------------------------------------------------
67
c     Open a trajectory file for wrinting
70
c     Open a trajectory file for wrinting
68
c     ----------------------------------------------------------------
71
c     ----------------------------------------------------------------
69
      
72
      
70
      subroutine wopen_tra(fid,filename,ntra,ntim,ncol,time,vars,mode)
73
      subroutine wopen_tra(fid,filename,ntra,ntim,ncol,time,vars,mode)
71
      
74
      
72
      implicit none
75
      implicit none
73
      
76
      
74
c     Declaration of subroutine parameters
77
c     Declaration of subroutine parameters
75
      integer      fid
78
      integer      fid
76
      character*80 filename
79
      character*80 filename
77
      integer      mode
80
      integer      mode
78
      integer      ntra,ntim,ncol
81
      integer      ntra,ntim,ncol
79
      integer      time(6) 
82
      integer      time(6) 
80
      character*80 vars(ncol)
83
      character*80 vars(ncol)
81
 
84
 
82
c     Auxiliary variables
85
c     Auxiliary variables
83
      integer      vardim(4)
86
      integer      vardim(4)
84
      real         varmin(4),varmax(4),stag(4)
87
      real         varmin(4),varmax(4),stag(4)
85
      real         mdv
88
      real         mdv
86
      character*80 cfn
89
      character*80 cfn
87
      integer      ierr
90
      integer      ierr
88
      integer      i
91
      integer      i
89
      character*80 varname
92
      character*80 varname
90
      real         rtime(6)
93
      real         rtime(6)
91
 
94
 
92
c     Open file
95
c     Open file
93
      if (mode.eq.1) then
96
      if (mode.eq.1) then
94
         fid = 10
97
         fid = 10
95
         open(fid,file=filename)
98
         open(fid,file=filename)
96
      elseif (mode.eq.2) then
99
      elseif (mode.eq.2) then
97
         fid = 10
100
         fid = 10
98
         open(fid,file=filename)
101
         open(fid,file=filename)
99
      elseif (mode.eq.3) then
102
      elseif (mode.eq.3) then
100
         open(fid,file=filename,form='unformatted')
103
         open(fid,file=filename,form='unformatted')
101
      elseif (mode.eq.4) then
104
      elseif (mode.eq.4) then
102
         vardim(1)=ntra
105
         vardim(1)=ntra
103
         vardim(2)=1
106
         vardim(2)=1
104
         vardim(3)=1
107
         vardim(3)=1
105
         vardim(4)=1
108
         vardim(4)=1
106
         cfn      =trim(filename)//'_cst'
109
         cfn      =trim(filename)//'_cst'
107
         mdv      =-999.98999
110
         mdv      =-999.98999
108
         call crecdf(filename,fid,varmin,varmax,3,cfn,ierr)
111
         call crecdf(filename,fid,varmin,varmax,3,cfn,ierr)
-
 
112
      elseif (mode.eq.5) then
-
 
113
         fid = 10
-
 
114
         open(fid,file=filename)
109
      endif
115
      endif
110
 
116
 
111
c     Write header information
117
c     Write header information
112
      call write_hea(fid,time,vars,ntra,ntim,ncol,mode)
118
      call write_hea(fid,time,vars,ntra,ntim,ncol,mode)
113
 
119
 
114
      end
120
      end
115
 
121
 
116
 
122
 
117
c     ----------------------------------------------------------------
123
c     ----------------------------------------------------------------
118
c     Read a trajectory
124
c     Read a trajectory
119
c     ----------------------------------------------------------------
125
c     ----------------------------------------------------------------
120
 
126
 
121
      subroutine read_tra(fid,tra,ntra,ntim,ncol,mode)
127
      subroutine read_tra(fid,tra,ntra,ntim,ncol,mode)
122
 
128
 
123
      implicit none
129
      implicit none
124
 
130
 
125
c     Declaration of subroutine parameters
131
c     Declaration of subroutine parameters
126
      integer   fid
132
      integer   fid
127
      integer   ntim
133
      integer   ntim
128
      integer   ncol
134
      integer   ncol
129
      integer   ntra
135
      integer   ntra
130
      real      tra(ntra,ntim,ncol)
136
      real      tra(ntra,ntim,ncol)
131
      integer   mode
137
      integer   mode
132
 
138
 
133
c     Auxiliary variables
139
c     Auxiliary variables
134
      integer      i,j,n
140
      integer      i,j,n
135
      real         arr(ntra)
141
      real         arr(ntra)
136
      integer      ntimes
142
      integer      ntimes
137
      real         times(1000)
143
      real         times(1000)
138
      integer      ierr
144
      integer      ierr
139
      character*80 vars(ncol+2)
145
      character*80 vars(ncol+2)
140
      integer      nvars
146
      integer      nvars
141
 
147
 
142
c     Read ascii mode, sorted by trajectory (mode=1)
148
c     Read ascii mode, sorted by trajectory (mode=1)
143
      if (mode.eq.1) then
149
      if (mode.eq.1) then
144
         read(fid,*,end=100)
150
         read(fid,*,end=100)
145
         do n=1,ntra
151
         do n=1,ntra
146
            do i=1,ntim
152
            do i=1,ntim
147
               read(fid,*,end=110) (tra(n,i,j),j=1,ncol)
153
               read(fid,*,end=110) (tra(n,i,j),j=1,ncol)
148
            enddo
154
            enddo
149
         enddo
155
         enddo
150
 
156
 
151
c     Read ascii mode, sorted by time (mode=2)
157
c     Read ascii mode, sorted by time (mode=2)
152
      elseif (mode.eq.2) then
158
      elseif (mode.eq.2) then
153
         read(fid,*,end=100)
159
         read(fid,*,end=100)
154
         do i=1,ntim
160
         do i=1,ntim
155
            do n=1,ntra
161
            do n=1,ntra
156
               read(fid,*,end=100) (tra(n,i,j),j=1,ncol)
162
               read(fid,*,end=100) (tra(n,i,j),j=1,ncol)
157
            enddo
163
            enddo
158
         enddo
164
         enddo
159
 
165
 
160
c     Read fortran mode (mode=3)
166
c     Read fortran mode (mode=3)
161
      elseif (mode.eq.3) then
167
      elseif (mode.eq.3) then
162
         read(fid) tra
168
         read(fid) tra
163
 
169
 
164
c     Read IVE netcdf mode (mode=4)
170
c     Read IVE netcdf mode (mode=4)
165
      elseif (mode.eq.4) then
171
      elseif (mode.eq.4) then
166
          call gettimes(fid,times,ntimes,ierr)
172
          call gettimes(fid,times,ntimes,ierr)
167
          call getvars(fid,nvars,vars,ierr)
173
          call getvars(fid,nvars,vars,ierr)
168
          do i=1,ntim
174
          do i=1,ntim
169
             do j=1,ncol
175
             do j=1,ncol
170
                if (j.eq.1) then
176
                if (j.eq.1) then
171
                   do n=1,ntra
177
                   do n=1,ntra
172
                      tra(n,i,1)=times(i)
178
                      tra(n,i,1)=times(i)
173
                   enddo
179
                   enddo
174
                else
180
                else
175
                   call getdat(fid,vars(j),times(i),0,arr,ierr)
181
                   call getdat(fid,vars(j),times(i),0,arr,ierr)
176
                   do n=1,ntra
182
                   do n=1,ntra
177
                      tra(n,i,j)=arr(n)
183
                      tra(n,i,j)=arr(n)
178
                   enddo
184
                   enddo
179
                endif
185
                endif
180
            enddo
186
            enddo
181
         enddo
187
         enddo
182
 
188
 
183
      endif
189
      endif
184
 
190
 
185
      return
191
      return
186
 
192
 
187
c     End of file has been reached: set negative <fid>
193
c     End of file has been reached: set negative <fid>
188
 100  fid=-fid
194
 100  fid=-fid
189
      return
195
      return
190
 
196
 
191
c     Error: incomplete trajectory
197
c     Error: incomplete trajectory
192
 110  print*,'<read_tra>: Incomplete trajectory... Stop'
198
 110  print*,'<read_tra>: Incomplete trajectory... Stop'
193
      stop
199
      stop
194
      
200
      
195
      end
201
      end
196
 
202
 
197
 
203
 
198
c     ----------------------------------------------------------------
204
c     ----------------------------------------------------------------
199
c     Write a trajectory
205
c     Write a trajectory
200
c     ----------------------------------------------------------------
206
c     ----------------------------------------------------------------
201
 
207
 
202
      subroutine write_tra(fid,tra,ntra,ntim,ncol,mode)
208
      subroutine write_tra(fid,tra,ntra,ntim,ncol,mode)
203
 
209
 
204
      implicit none
210
      implicit none
205
 
211
 
206
c     Declaration of subroutine parameters
212
c     Declaration of subroutine parameters
207
      integer   fid
213
      integer   fid
208
      integer   ntim
214
      integer   ntim
209
      integer   ncol
215
      integer   ncol
210
      integer   ntra
216
      integer   ntra
211
      real      tra(ntra,ntim,ncol)
217
      real      tra(ntra,ntim,ncol)
212
      integer   mode
218
      integer   mode
213
 
219
 
214
c     Auxiliary variables
220
c     Auxiliary variables
215
      integer      i,j,n
221
      integer      i,j,n
216
      real         arr(ntra)
222
      real         arr(ntra)
217
      integer      ierr
223
      integer      ierr
218
      real         time
224
      real         time
219
      character*80 vars(ncol+2)
225
      character*80 vars(ncol+2)
220
      integer      nvars
226
      integer      nvars
-
 
227
      character*20 lonstr,latstr,levstr
-
 
228
      character*80 outstr
-
 
229
      real         ref_z(3000),ref_p(3000),ref_t(3000)
-
 
230
      real         lev
-
 
231
      character*80 path
221
 
232
 
222
c     Write ascii mode, sorted by trajectory (mode=1)
233
c     Write ascii mode, sorted by trajectory (mode=1)
223
      if (mode.eq.1) then
234
      if (mode.eq.1) then
224
         do n=1,ntra
235
         do n=1,ntra
225
            write(fid,*)
236
            write(fid,*)
226
            do i=1,ntim
237
            do i=1,ntim
227
 
238
 
228
c              Avoid ugly *s or missing space in output
239
c              Avoid ugly *s or missing space in output
229
               do j=5,ncol
240
               do j=5,ncol
230
                  if ( abs(tra(n,i,j)).gt.9999.) then                     
241
                  if ( abs(tra(n,i,j)).gt.9999.) then                     
231
                     print*,'Format problem : ',tra(n,i,j),' -> -999.99'
242
                     print*,'Format problem : ',tra(n,i,j),' -> -999.99'
232
                     tra(n,i,j) = -999.99
243
                     tra(n,i,j) = -999.99
233
                  endif
244
                  endif
234
               enddo
245
               enddo
235
 
246
 
236
               write(fid,'(1f7.2,f9.2,f8.2,i6,100f10.3)') 
247
               write(fid,'(1f7.2,f9.2,f8.2,i6,100f10.3)') 
237
     >               (tra(n,i,j),j=1,3),             ! time, lon, lat
248
     >               (tra(n,i,j),j=1,3),             ! time, lon, lat
238
     >               nint(tra(n,i,4)),               ! p
249
     >               nint(tra(n,i,4)),               ! p
239
     >               (tra(n,i,j),j=5,ncol)           ! fields
250
     >               (tra(n,i,j),j=5,ncol)           ! fields
240
            enddo
251
            enddo
241
         enddo
252
         enddo
242
 
253
 
243
c     Write ascii mode, sorted by time (mode=2)
254
c     Write ascii mode, sorted by time (mode=2)
244
      elseif (mode.eq.2) then
255
      elseif (mode.eq.2) then
245
         do i=1,ntim
256
         do i=1,ntim
246
            write(fid,*)
257
            write(fid,*)
247
            do n=1,ntra
258
            do n=1,ntra
248
 
259
 
249
c              Avoid ugly *s or missing space in output
260
c              Avoid ugly *s or missing space in output
250
               do j=5,ncol
261
               do j=5,ncol
251
                  if ( abs(tra(n,i,j)).gt.9999.) then                     
262
                  if ( abs(tra(n,i,j)).gt.9999.) then                     
252
                     print*,'Format problem : ',tra(n,i,j),' -> -999.99'
263
                     print*,'Format problem : ',tra(n,i,j),' -> -999.99'
253
                     tra(n,i,j) = -999.99
264
                     tra(n,i,j) = -999.99
254
                  endif
265
                  endif
255
               enddo
266
               enddo
256
 
267
 
257
               write(fid,'(1f7.2,f9.2,f8.2,i6,100f10.3)') 
268
               write(fid,'(1f7.2,f9.2,f8.2,i6,100f10.3)') 
258
     >               (tra(n,i,j),j=1,3),             ! time, lon, lat
269
     >               (tra(n,i,j),j=1,3),             ! time, lon, lat
259
     >               nint(tra(n,i,4)),               ! p
270
     >               nint(tra(n,i,4)),               ! p
260
     >               (tra(n,i,j),j=5,ncol)           ! fields
271
     >               (tra(n,i,j),j=5,ncol)           ! fields
261
            enddo
272
            enddo
262
         enddo
273
         enddo
263
 
274
 
264
c     Write fortran mode (mode=3)
275
c     Write fortran mode (mode=3)
265
      elseif (mode.eq.3) then
276
      elseif (mode.eq.3) then
266
         write(fid) tra                              
277
         write(fid) tra                              
267
 
278
 
268
c     Write netcdf mode (mode=4)
279
c     Write netcdf mode (mode=4)
269
      elseif (mode.eq.4) then
280
      elseif (mode.eq.4) then
270
         call getvars(fid,nvars,vars,ierr)
281
         call getvars(fid,nvars,vars,ierr)
271
         do i=1,ntim
282
         do i=1,ntim
272
            time=tra(1,i,1)
283
            time=tra(1,i,1)
273
            do j=2,ncol
284
            do j=2,ncol
274
               do n=1,ntra
285
               do n=1,ntra
275
                  arr(n)=tra(n,i,j)
286
                  arr(n)=tra(n,i,j)
276
               enddo
287
               enddo
277
               call putdat(fid,vars(j),time,0,arr,ierr)
288
               call putdat(fid,vars(j),time,0,arr,ierr)
278
            enddo
289
            enddo
279
         enddo
290
         enddo
-
 
291
 
-
 
292
c     Write KML mode (mode=5)
-
 
293
      elseif (mode.eq.5) then
-
 
294
 
-
 
295
         call getenv('DYN_TOOLS',path)
-
 
296
         path = trim(path)//'/lagranto.ecmwf/goodies/'
-
 
297
 
-
 
298
         open(fid+1,file=trim(path)//'reformat.refprof')
-
 
299
           
-
 
300
           do n=1,6
-
 
301
              read(fid+1,*)
-
 
302
           enddo
-
 
303
           do n=1,3000
-
 
304
              read(fid+1,*)  ref_z(n),ref_t(n),ref_p(n)
-
 
305
              ref_p(n) = 0.01 * ref_p(n)
-
 
306
           enddo
-
 
307
 
-
 
308
         close(fid+1)
-
 
309
 
-
 
310
         do n=1,ntra
-
 
311
           write(fid,"(A)") '<Placemark>'
-
 
312
           write(fid,"(A)") '<name>Absolute Extruded</name>'
-
 
313
           write(fid,"(A)") '<styleUrl>#yellowkLineGreenPoly</styleUrl>'
-
 
314
           write(fid,"(A)") '<LineString>'
-
 
315
           write(fid,"(A)") '<extrude>1</extrude>'
-
 
316
           write(fid,"(A)") '<tessellate>1</tessellate>'
-
 
317
           write(fid,"(A)") '<altitudeMode>absolute</altitudeMode>'
-
 
318
           write(fid,"(A)") '<coordinates>'
-
 
319
 
-
 
320
           do i=1,ntim
-
 
321
             write(lonstr,*) tra(n,i,2)
-
 
322
             write(latstr,*) tra(n,i,3)
-
 
323
             
-
 
324
             call binary(lev,tra(n,i,4),ref_z,ref_p)
-
 
325
             write(levstr,*) lev
-
 
326
 
-
 
327
             outstr = trim(adjustl(lonstr))//','//
-
 
328
     >                trim(adjustl(latstr))//','//
-
 
329
     >                trim(adjustl(levstr))
-
 
330
 
-
 
331
             write(fid,"(A)") outstr
-
 
332
 
-
 
333
           enddo
-
 
334
 
-
 
335
           write(fid,*) '</coordinates>'
-
 
336
           write(fid,*) '</LineString>'
-
 
337
           write(fid,*) '</Placemark>'
-
 
338
         enddo
-
 
339
 
280
      endif
340
      endif
281
 
341
 
282
      end
342
      end
283
 
343
 
284
 
344
 
285
c     ----------------------------------------------------------------
345
c     ----------------------------------------------------------------
286
c     Read header from trajectory file
346
c     Read header from trajectory file
287
c     ----------------------------------------------------------------
347
c     ----------------------------------------------------------------
288
 
348
 
289
      subroutine read_hea(fid,time,vars,ntra,ntim,ncol,mode)
349
      subroutine read_hea(fid,time,vars,ntra,ntim,ncol,mode)
290
 
350
 
291
      implicit none
351
      implicit none
292
      
352
      
293
c     Declaration of subroutine parameters
353
c     Declaration of subroutine parameters
294
      integer       fid
354
      integer       fid
295
      integer       time(6)
355
      integer       time(6)
296
      integer       ntra,ntim,ncol
356
      integer       ntra,ntim,ncol
297
      character*80  vars(ncol)
357
      character*80  vars(ncol)
298
      integer       mode
358
      integer       mode
299
 
359
 
300
c     Auxiliary variables
360
c     Auxiliary variables
301
      integer       i
361
      integer       i
302
      character     ch(500)
362
      character     ch(500)
303
      character*500 str
363
      character*500 str
304
      integer       ich(500)
364
      integer       ich(500)
305
      integer       isstr,ileft,iright
365
      integer       isstr,ileft,iright
306
      character*80  varname
366
      character*80  varname
307
      real          rtime(6)
367
      real          rtime(6)
308
      integer       ierr
368
      integer       ierr
309
      integer       nvars
369
      integer       nvars
310
      character*15  str1
370
      character*15  str1
311
      character     str2
371
      character     str2
312
      character*13  str3
372
      character*13  str3
313
      character*4   str4
373
      character*4   str4
314
      character*80  linestr
374
      character*80  linestr
315
      integer       itmp1,itmp2
375
      integer       itmp1,itmp2
316
 
376
 
317
c     Read ascii format (mode=1,2)
377
c     Read ascii format (mode=1,2)
318
      if ( (mode.eq.1).or.(mode.eq.2) ) then
378
      if ( (mode.eq.1).or.(mode.eq.2) ) then
319
 
379
 
320
c        Read the time specification (old and new format)
380
c        Read the time specification (old and new format)
321
         read(fid,'(a80)') linestr
381
         read(fid,'(a80)') linestr
322
         
382
         
323
         if ( linestr(1:14).eq.'Reference date' ) then
383
         if ( linestr(1:14).eq.'Reference date' ) then
324
            read(linestr,'(a15,i4,i2,i2,a1,i2,i2,a13,i8,a4)') 
384
            read(linestr,'(a15,i4,i2,i2,a1,i2,i2,a13,i8,a4)') 
325
     >           str1,
385
     >           str1,
326
     >           time(1),time(2),time(3),str2,time(4),time(5),
386
     >           time(1),time(2),time(3),str2,time(4),time(5),
327
     >           str3,time(6),str4
387
     >           str3,time(6),str4
328
                       
388
                       
329
         elseif ( linestr(1:11).eq.'time period' ) then
389
         elseif ( linestr(1:11).eq.'time period' ) then
330
            read(linestr,'(a12,i4,i2,i2,a1,i2,a4,i6,a1,i2,a2)') 
390
            read(linestr,'(a12,i4,i2,i2,a1,i2,a4,i6,a1,i2,a2)') 
331
     >           str1,
391
     >           str1,
332
     >           time(1),time(2),time(3),str2,time(4),
392
     >           time(1),time(2),time(3),str2,time(4),
333
     >           str3,itmp1,str3,itmp2,str4
393
     >           str3,itmp1,str3,itmp2,str4
334
            time(5) = 0
394
            time(5) = 0
335
            time(6) = itmp1 * 60 + itmp2
395
            time(6) = itmp1 * 60 + itmp2
336
 
396
 
337
         endif
397
         endif
338
 
398
 
339
c        Skip the empty line and read field names
399
c        Skip the empty line and read field names
340
         read(fid,*)
400
         read(fid,*)
341
         read(fid,'(a500)',end=100) str
401
         read(fid,'(a500)',end=100) str
342
         do i=1,500
402
         do i=1,500
343
            ch(i)=str(i:i)
403
            ch(i)=str(i:i)
344
         enddo
404
         enddo
345
 
405
 
346
c        Split the input string
406
c        Split the input string
347
         isstr=0
407
         isstr=0
348
         nvars=0
408
         nvars=0
349
         do i=1,500
409
         do i=1,500
350
            if ( (isstr.eq.0).and.(ch(i).ne.' ') ) then
410
            if ( (isstr.eq.0).and.(ch(i).ne.' ') ) then
351
               isstr=1
411
               isstr=1
352
               ileft=i
412
               ileft=i
353
            elseif ( (isstr.eq.1).and.(ch(i).eq.' ') ) then
413
            elseif ( (isstr.eq.1).and.(ch(i).eq.' ') ) then
354
               isstr=0
414
               isstr=0
355
               iright=i-1
415
               iright=i-1
356
               nvars=nvars+1
416
               nvars=nvars+1
357
               vars(nvars)=str(ileft:iright)
417
               vars(nvars)=str(ileft:iright)
358
            endif
418
            endif
359
         enddo
419
         enddo
360
 
420
 
361
c        Skip the empty line
421
c        Skip the empty line
362
         read(fid,*,end=100)
422
         read(fid,*,end=100)
363
 
423
 
364
c     Read fortran mode (mode=3)
424
c     Read fortran mode (mode=3)
365
      elseif (mode.eq.3) then
425
      elseif (mode.eq.3) then
366
         read(fid) ntra,ntim,ncol
426
         read(fid) ntra,ntim,ncol
367
         read(fid) time
427
         read(fid) time
368
         read(fid) vars
428
         read(fid) vars
369
 
429
 
370
c     Read IVE netcdf mode (mode=4)
430
c     Read IVE netcdf mode (mode=4)
371
      elseif (mode.eq.4) then
431
      elseif (mode.eq.4) then
372
         call getvars(fid,nvars,vars,ierr)  
432
         call getvars(fid,nvars,vars,ierr)  
373
         varname='BASEDATE'
433
         varname='BASEDATE'
374
         call getdat(fid,varname,0.,0,rtime,ierr)
434
         call getdat(fid,varname,0.,0,rtime,ierr)
375
         do i=1,6
435
         do i=1,6
376
            time(i)=nint(rtime(i))
436
            time(i)=nint(rtime(i))
377
         enddo
437
         enddo
378
      endif
438
      endif
379
 
439
 
380
      return
440
      return
381
 
441
 
382
c     End of file has been reached
442
c     End of file has been reached
383
 100  fid=-fid
443
 100  fid=-fid
384
      return
444
      return
385
 
445
 
386
c     Excetion handling
446
c     Excetion handling
387
 110  print*,'<read_hea>: Unexspected time format.... Stop'
447
 110  print*,'<read_hea>: Unexspected time format.... Stop'
388
      stop
448
      stop
389
   
449
   
390
      end
450
      end
391
      
451
      
392
 
452
 
393
c     ----------------------------------------------------------------
453
c     ----------------------------------------------------------------
394
c     Write header to trajectory file (in ascii mode)
454
c     Write header to trajectory file (in ascii mode)
395
c     ----------------------------------------------------------------
455
c     ----------------------------------------------------------------
396
 
456
 
397
      subroutine write_hea(fid,time,vars,ntra,ntim,ncol,mode)
457
      subroutine write_hea(fid,time,vars,ntra,ntim,ncol,mode)
398
 
458
 
399
      implicit none
459
      implicit none
400
      
460
      
401
c     Declaration of subroutine parameters
461
c     Declaration of subroutine parameters
402
      integer       fid
462
      integer       fid
403
      integer       time(6)
463
      integer       time(6)
404
      integer       ntra,ntim,ncol
464
      integer       ntra,ntim,ncol
405
      character*80  vars(ncol)
465
      character*80  vars(ncol)
406
      integer       mode
466
      integer       mode
407
 
467
 
408
c     Auxiliary variables
468
c     Auxiliary variables
409
      integer       i
469
      integer       i
410
      character*500 str
470
      character*500 str
411
      character*4   str1
471
      character*4   str1
412
      character*2   str2,str3,str4,str5,str6
472
      character*2   str2,str3,str4,str5,str6
413
      integer       vardim(4)
473
      integer       vardim(4)
414
      real          varmin(4),varmax(4),stag(4)
474
      real          varmin(4),varmax(4),stag(4)
415
      real          mdv
475
      real          mdv
416
      integer       ierr
476
      integer       ierr
417
      character*80  varname
477
      character*80  varname
418
      real          rtime(6)
478
      real          rtime(6)
419
      integer       nvars
479
      integer       nvars
420
 
480
 
421
c     Write ascii format (mode=1,2)
481
c     Write ascii format (mode=1,2)
422
      if ( (mode.eq.1).or.(mode.eq.2) ) then
482
      if ( (mode.eq.1).or.(mode.eq.2) ) then
423
 
483
 
424
c        Get the strings for output
484
c        Get the strings for output
425
         write(str1,'(i4)') time(1)
485
         write(str1,'(i4)') time(1)
426
         write(str2,'(i2)') time(2)
486
         write(str2,'(i2)') time(2)
427
         write(str3,'(i2)') time(3)
487
         write(str3,'(i2)') time(3)
428
         write(str4,'(i2)') time(4)
488
         write(str4,'(i2)') time(4)
429
         write(str5,'(i2)') time(5)
489
         write(str5,'(i2)') time(5)
430
         if (time(2).eq. 0) str2(1:1)='0'
490
         if (time(2).eq. 0) str2(1:1)='0'
431
         if (time(3).eq. 0) str3(1:1)='0'
491
         if (time(3).eq. 0) str3(1:1)='0'
432
         if (time(4).eq. 0) str4(1:1)='0'
492
         if (time(4).eq. 0) str4(1:1)='0'
433
         if (time(5).eq. 0) str5(1:1)='0'
493
         if (time(5).eq. 0) str5(1:1)='0'
434
         if (time(2).lt.10) str2(1:1)='0'
494
         if (time(2).lt.10) str2(1:1)='0'
435
         if (time(3).lt.10) str3(1:1)='0'
495
         if (time(3).lt.10) str3(1:1)='0'
436
         if (time(4).lt.10) str4(1:1)='0'
496
         if (time(4).lt.10) str4(1:1)='0'
437
         if (time(5).lt.10) str5(1:1)='0'
497
         if (time(5).lt.10) str5(1:1)='0'
438
 
498
 
439
c        Write the time specification
499
c        Write the time specification
440
         write(fid,'(a15,a4,a2,a2,a1,a2,a2,a13,i8,a4)') 
500
         write(fid,'(a15,a4,a2,a2,a1,a2,a2,a13,i8,a4)') 
441
     >          'Reference date ',
501
     >          'Reference date ',
442
     >           str1,str2,str3,'_',str4,str5,
502
     >           str1,str2,str3,'_',str4,str5,
443
     >          ' / Time range',time(6), ' min'
503
     >          ' / Time range',time(6), ' min'
444
         write(fid,*)
504
         write(fid,*)
445
 
505
 
446
c        Write variable names
506
c        Write variable names
447
         str=''
507
         str=''
448
         do i=1,ncol
508
         do i=1,ncol
449
            str=trim(str)//trim(vars(i))
509
            str=trim(str)//trim(vars(i))
450
         enddo
510
         enddo
451
         write(fid,'(a6,a9,a8,a6,100a10)') (trim(vars(i)),i=1,ncol)
511
         write(fid,'(a6,a9,a8,a6,100a10)') (trim(vars(i)),i=1,ncol)
452
         write(fid,'(a6,a9,a8,a6,100a10)') 
512
         write(fid,'(a6,a9,a8,a6,100a10)') 
453
     >              '------','---------','--------','------',
513
     >              '------','---------','--------','------',
454
     >              ('----------',i=5,ncol)
514
     >              ('----------',i=5,ncol)
455
 
515
 
456
c     Write fortran mode (mode=3)
516
c     Write fortran mode (mode=3)
457
      elseif (mode.eq.3) then
517
      elseif (mode.eq.3) then
458
         write(fid) ntra,ntim,ncol
518
         write(fid) ntra,ntim,ncol
459
         write(fid) time
519
         write(fid) time
460
         write(fid) vars
520
         write(fid) vars
461
 
521
 
462
c     Write IVE netcdf format (mode=4)
522
c     Write IVE netcdf format (mode=4)
463
      elseif (mode.eq.4) then
523
      elseif (mode.eq.4) then
464
         vardim(1)=ntra
524
         vardim(1)=ntra
465
         vardim(2)=1
525
         vardim(2)=1
466
         vardim(3)=1
526
         vardim(3)=1
467
         vardim(4)=1
527
         vardim(4)=1
468
         mdv      =-999.98999
528
         mdv      =-999.98999
469
         do i=2,ncol
529
         do i=2,ncol
470
            call putdef(fid,vars(i),4,mdv,vardim,
530
            call putdef(fid,vars(i),4,mdv,vardim,
471
     >                  varmin,varmax,stag,ierr)
531
     >                  varmin,varmax,stag,ierr)
472
         enddo
532
         enddo
473
         varname='BASEDATE'
533
         varname='BASEDATE'
474
         vardim(1)=6
534
         vardim(1)=6
475
         call putdef(fid,varname,4,mdv,vardim,
535
         call putdef(fid,varname,4,mdv,vardim,
476
     >               varmin,varmax,stag,ierr)
536
     >               varmin,varmax,stag,ierr)
477
         do i=1,6
537
         do i=1,6
478
            rtime(i)=real(time(i))
538
            rtime(i)=real(time(i))
479
         enddo
539
         enddo
480
         call putdat(fid,varname,0.,0,rtime,ierr)
540
         call putdat(fid,varname,0.,0,rtime,ierr)
481
 
541
 
-
 
542
c     Write KML format (mode=5)
-
 
543
      elseif (mode.eq.5) then
-
 
544
 
-
 
545
      write(fid,"(A)") '<?xml version="1.0" encoding="UTF-8"?>'
-
 
546
      write(fid,"(A)") '<kml xmlns="http://www.opengis.net/kml/2.2">'
-
 
547
      write(fid,"(A)") '<Document>'
-
 
548
      write(fid,"(A)") '<name>Paths</name>'
-
 
549
      write(fid,"(A)") '<Style id="yellowLineGreenPoly">'
-
 
550
      write(fid,"(A)") '<LineStyle>'
-
 
551
c      write(fid,*) '<color>7f00ffff</color>'    ! Yellow
-
 
552
      write(fid,"(A)") '<color>500A0A0A</color>'     ! Black
-
 
553
      write(fid,"(A)") '<width>4</width>'
-
 
554
      write(fid,"(A)") '</LineStyle>'
-
 
555
      write(fid,"(A)") '<PolyStyle>'
-
 
556
      write(fid,"(A)") '<color>7f00ff00</color>'
-
 
557
      write(fid,"(A)") '</PolyStyle>'
-
 
558
      write(fid,"(A)") '</Style>'
-
 
559
 
482
      endif
560
      endif
483
 
561
 
484
      end
562
      end
485
      
563
      
486
      
564
      
487
c     ----------------------------------------------------------------
565
c     ----------------------------------------------------------------
488
c     Close a trajectory file
566
c     Close a trajectory file
489
c     ----------------------------------------------------------------
567
c     ----------------------------------------------------------------
490
      
568
      
491
      subroutine close_tra(fid,mode)
569
      subroutine close_tra(fid,mode)
492
 
570
 
493
      implicit none
571
      implicit none
494
      
572
      
495
c     Declaration of subroutine parameters
573
c     Declaration of subroutine parameters
496
      integer      fid
574
      integer      fid
497
      integer      mode
575
      integer      mode
498
      
576
      
499
c     Auxiliary variables
577
c     Auxiliary variables
500
      integer      ierr
578
      integer      ierr
501
 
579
 
502
c     Close file
580
c     Close file
503
      if (mode.eq.1) then
581
      if (mode.eq.1) then
504
         close(abs(fid))
582
         close(abs(fid))
-
 
583
 
505
      elseif (mode.eq.2) then
584
      elseif (mode.eq.2) then
506
         close(abs(fid))
585
         close(abs(fid))
-
 
586
 
507
      elseif (mode.eq.3) then
587
      elseif (mode.eq.3) then
508
         close(fid)
588
         close(fid)
-
 
589
 
509
      elseif (mode.eq.4) then
590
      elseif (mode.eq.4) then
510
         call clscdf(fid,ierr)
591
         call clscdf(fid,ierr)
-
 
592
 
-
 
593
      elseif (mode.eq.5) then
-
 
594
          write(fid,"(A)") '</Document>'
-
 
595
          write(fid,"(A)") '</kml>'
-
 
596
         close(abs(fid))
511
      endif
597
      endif
512
 
598
 
513
      end
599
      end
514
      
600
      
515
 
601
 
516
c     ----------------------------------------------------------------
602
c     ----------------------------------------------------------------
517
c     Determine the mode of a trajectory file
603
c     Determine the mode of a trajectory file
518
c     ----------------------------------------------------------------
604
c     ----------------------------------------------------------------
519
 
605
 
520
      subroutine mode_tra(mode,filename)
606
      subroutine mode_tra(mode,filename)
521
      
607
      
522
      implicit none
608
      implicit none
523
 
609
 
524
c     Declaration of subroutine parameters
610
c     Declaration of subroutine parameters
525
      integer        mode
611
      integer        mode
526
      character*80   filename
612
      character*80   filename
527
 
613
 
528
c     Auxiliary variables
614
c     Auxiliary variables
529
      integer        len
615
      integer        len
530
      character      char0,char1
616
      character      char0,char1,char2,char3,char4
531
 
617
 
532
c     Get mode
618
c     Get mode
533
      mode=-1
619
      mode=-1
534
      
620
      
535
      len  = len_trim(filename)
621
      len  = len_trim(filename)
536
 
622
 
-
 
623
c     Mode specified by number
537
      char0 = filename((len-1):(len-1))
624
      char0 = filename((len-1):(len-1))
538
      char1 = filename(len:len)
625
      char1 = filename(len:len)
539
 
626
 
540
      if ( (char0.eq.'.').and.(char1.eq.'1') ) mode=1
627
      if ( (char0.eq.'.').and.(char1.eq.'1') ) mode=1
541
      if ( (char0.eq.'.').and.(char1.eq.'2') ) mode=2
628
      if ( (char0.eq.'.').and.(char1.eq.'2') ) mode=2
542
      if ( (char0.eq.'.').and.(char1.eq.'3') ) mode=3
629
      if ( (char0.eq.'.').and.(char1.eq.'3') ) mode=3
543
      if ( (char0.eq.'.').and.(char1.eq.'4') ) mode=4
630
      if ( (char0.eq.'.').and.(char1.eq.'4') ) mode=4
-
 
631
      if ( (char0.eq.'.').and.(char1.eq.'5') ) mode=5
-
 
632
 
-
 
633
      if ( mode.gt.0 ) return
-
 
634
 
-
 
635
c     Mode specified by appendix
-
 
636
      char0 = filename((len-3):(len-3))
-
 
637
      char1 = filename((len-2):(len-2))
-
 
638
      char2 = filename((len-1):(len-1))
-
 
639
      char3 = filename(len:len)
-
 
640
      if ( (char1.eq.'.').and.(char2.eq.'l').and.(char3.eq.'s') ) mode=1
-
 
641
      if ( (char1.eq.'.').and.(char2.eq.'t').and.(char3.eq.'i') ) mode=2
-
 
642
      if ( (char1.eq.'.').and.(char2.eq.'d').and.(char3.eq.'u') ) mode=3
-
 
643
      if ( (char1.eq.'.').and.(char2.eq.'n').and.(char3.eq.'c') ) mode=4
-
 
644
 
-
 
645
      if ( (char0.eq.'.').and.(char1.eq.'k').and.
-
 
646
     >                        (char2.eq.'m').and.
-
 
647
     >                        (char3.eq.'l') ) mode = 5
544
 
648
 
545
      end
649
      end
546
 
650
 
547
 
651
 
548
c     ----------------------------------------------------------------
652
c     ----------------------------------------------------------------
549
c     Get dimension of a trajectory file
653
c     Get dimension of a trajectory file
550
c     ----------------------------------------------------------------
654
c     ----------------------------------------------------------------
551
    
655
    
552
      subroutine info_tra(filename,ntra,ntim,ncol,mode)
656
      subroutine info_tra(filename,ntra,ntim,ncol,mode)
553
 
657
 
554
      implicit none
658
      implicit none
555
      
659
      
556
c     Declaration of subroutine parameters
660
c     Declaration of subroutine parameters
557
      integer      fid
661
      integer      fid
558
      character*80 filename
662
      character*80 filename
559
      integer      mode
663
      integer      mode
560
      integer      ntra,ntim,ncol
664
      integer      ntra,ntim,ncol
561
 
665
 
562
c     Auxiliary variables
666
c     Auxiliary variables
563
      integer       vardim(4)
667
      integer       vardim(4)
564
      real          varmin(4),varmax(4),stag(4)
668
      real          varmin(4),varmax(4),stag(4)
565
      real          mdv
669
      real          mdv
566
      character*80  cfn
670
      character*80  cfn
567
      integer       ierr
671
      integer       ierr
568
      integer       i,ndim
672
      integer       i,ndim
569
      character*80  vars(100)
673
      character*80  vars(100)
570
      integer       nvars
674
      integer       nvars
571
      integer       ntimes
675
      integer       ntimes
572
      real          times(100)
676
      real          times(100)
573
      character*500 str
677
      character*500 str
574
      integer       nline0,nline1,nline2
678
      integer       nline0,nline1,nline2
575
      integer       isstr,isok
679
      integer       isstr,isok
576
      character     ch
680
      character     ch
577
 
681
 
578
c     Open file
682
c     Open file
579
      if (mode.eq.1) then
683
      if (mode.eq.1) then
580
         fid=10
684
         fid=10
581
         open(fid,file=filename)
685
         open(fid,file=filename)
582
      elseif (mode.eq.2) then
686
      elseif (mode.eq.2) then
583
         fid=10
687
         fid=10
584
         open(fid,file=filename)
688
         open(fid,file=filename)
585
      elseif (mode.eq.3) then
689
      elseif (mode.eq.3) then
586
         fid=10
690
         fid=10
587
         open(fid,file=filename,form='unformatted')
691
         open(fid,file=filename,form='unformatted')
588
      elseif (mode.eq.4) then
692
      elseif (mode.eq.4) then
589
         call cdfopn(filename,fid,ierr)
693
         call cdfopn(filename,fid,ierr)
590
      endif
694
      endif
591
 
695
 
592
c     Get dimension information
696
c     Get dimension information
593
      if ( (mode.eq.1).or.(mode.eq.2) ) then
697
      if ( (mode.eq.1).or.(mode.eq.2) ) then
594
         read(fid,*)
698
         read(fid,*)
595
         read(fid,*)
699
         read(fid,*)
596
         read(fid,'(a500)') str
700
         read(fid,'(a500)') str
597
         read(fid,*)
701
         read(fid,*)
598
 
702
 
599
c        Get the number of columns
703
c        Get the number of columns
600
         isstr=0
704
         isstr=0
601
         ncol =0
705
         ncol =0
602
         do i=1,500
706
         do i=1,500
603
            ch = str(i:i)
707
            ch = str(i:i)
604
 
708
 
605
            if ( (isstr.eq.0).and.(ch.ne.' ') ) then
709
            if ( (isstr.eq.0).and.(ch.ne.' ') ) then
606
               isstr=1
710
               isstr=1
607
            elseif ( (isstr.eq.1).and.(ch.eq.' ') ) then
711
            elseif ( (isstr.eq.1).and.(ch.eq.' ') ) then
608
               isstr=0
712
               isstr=0
609
               ncol=ncol+1
713
               ncol=ncol+1
610
            endif
714
            endif
611
         enddo
715
         enddo
612
 
716
 
613
c        Get the first data block
717
c        Get the first data block
614
         nline0  = 5
718
         nline0  = 5
615
         nline1  = 5
719
         nline1  = 5
616
         read(fid,*)
720
         read(fid,*)
617
 100     read(fid,'(a500)',end=110) str
721
 100     read(fid,'(a500)',end=110) str
618
         if (str.ne.'') then
722
         if (str.ne.'') then
619
            nline1 = nline1 + 1
723
            nline1 = nline1 + 1
620
            goto 100
724
            goto 100
621
         endif
725
         endif
622
 110     continue
726
 110     continue
623
         
727
         
624
c        Get the total numbers of lines in the data block
728
c        Get the total numbers of lines in the data block
625
         nline2 = nline1
729
         nline2 = nline1
626
 120     read(fid,*,end=130)
730
 120     read(fid,*,end=130)
627
         nline2 = nline2 + 1
731
         nline2 = nline2 + 1
628
         goto 120
732
         goto 120
629
 130     nline2 = nline2 + 1
733
 130     nline2 = nline2 + 1
630
 
734
 
631
c        Set the dimensions
735
c        Set the dimensions
632
         if (mode.eq.1) then
736
         if (mode.eq.1) then
633
            ntim = nline1 - nline0
737
            ntim = nline1 - nline0
634
            ntra = (nline2-nline0+1)/(ntim+1)
738
            ntra = (nline2-nline0+1)/(ntim+1)
635
         else
739
         else
636
            ntra = nline1 - nline0
740
            ntra = nline1 - nline0
637
            ntim = (nline2-nline0+1)/(ntra+1)
741
            ntim = (nline2-nline0+1)/(ntra+1)
638
         endif
742
         endif
639
 
743
 
640
      elseif (mode.eq.3) then
744
      elseif (mode.eq.3) then
641
         read(fid) ntra,ntim,ncol
745
         read(fid) ntra,ntim,ncol
642
 
746
 
643
      elseif (mode.eq.4) then
747
      elseif (mode.eq.4) then
644
         call gettimes(fid,times,ntimes,ierr)
748
         call gettimes(fid,times,ntimes,ierr)
645
         call getvars(fid,nvars,vars,ierr)
749
         call getvars(fid,nvars,vars,ierr)
646
         call getdef(fid,trim(vars(2)),ndim,mdv,vardim,
750
         call getdef(fid,trim(vars(2)),ndim,mdv,vardim,
647
     >               varmin,varmax,stag,ierr)
751
     >               varmin,varmax,stag,ierr)
648
         ntra = vardim(1)
752
         ntra = vardim(1)
649
         ntim = ntimes
753
         ntim = ntimes
650
         ncol = nvars-1
754
         ncol = nvars-1
651
 
755
 
652
      endif
756
      endif
653
 
757
 
654
c     Close file
758
c     Close file
655
      if (mode.eq.1) then
759
      if (mode.eq.1) then
656
         close(fid)
760
         close(fid)
657
      elseif (mode.eq.2) then
761
      elseif (mode.eq.2) then
658
         close(fid)
762
         close(fid)
659
      elseif (mode.eq.3) then
763
      elseif (mode.eq.3) then
660
         close(fid)
764
         close(fid)
661
      elseif (mode.eq.4) then
765
      elseif (mode.eq.4) then
662
         call clscdf(fid,ierr)
766
         call clscdf(fid,ierr)
663
      endif
767
      endif
664
 
768
 
-
 
769
      end
-
 
770
     
-
 
771
 
-
 
772
c     ----------------------------------------------------------------
-
 
773
c     Binary search algorithm
-
 
774
c     ----------------------------------------------------------------
-
 
775
 
-
 
776
      subroutine binary (z,p,ref_z,ref_p)
-
 
777
 
-
 
778
      implicit none
-
 
779
 
-
 
780
c     Declaration of subroutine parameters
-
 
781
      real   z
-
 
782
      real   p
-
 
783
      real   ref_z(3000)
-
 
784
      real   ref_p(3000)
-
 
785
 
-
 
786
c     Auxiliary variables
-
 
787
      integer i0,i1,im
-
 
788
 
-
 
789
c     Binary search
-
 
790
      i0 = 1
-
 
791
      i1 = 3000
-
 
792
 100  continue
-
 
793
        im = (i0 + i1) / 2
-
 
794
        if ( p.lt.ref_p(im) ) then
-
 
795
           i0 = im
-
 
796
        else 
-
 
797
           i1 = im
-
 
798
        endif
-
 
799
      if ( (i1-i0).gt.1 ) goto 100
-
 
800
 
-
 
801
c     Linear interpolation in between
-
 
802
      z = ref_z(i0) + ( p - ref_p(i0) ) / ( ref_p(i1) - ref_p(i0) ) *
-
 
803
     >                ( ref_z(i1) - ref_z(i0) ) 
665
 
804
 
666
      end
805
      end
-
 
806
 
-
 
807
 
-
 
808
 
-
 
809
 
667
      
810