Subversion Repositories lagranto.ocean

Rev

Details | Last modification | View Log | RSS feed

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