Subversion Repositories lagranto.icon

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 michaesp 1
      PROGRAM trace
2
 
3
C     ********************************************************************
4
C     *                                                                  *
5
C     * Trace fields along trajectories                                  *
6
C     *                                                                  *
7
C     * Heini Wernli       first version:       April 1993               *
8
C     * Michael Sprenger   major upgrade:       2008-2009                *
9
C     *                                                                  *
10
C     ********************************************************************
11
 
12
      implicit none
13
 
14
c     --------------------------------------------------------------------
15
c     Declaration of parameters
16
c     --------------------------------------------------------------------
17
 
18
c     Maximum number of levels for input files
19
      integer   nlevmax
20
      parameter (nlevmax=100)
21
 
22
c     Maximum number of input files (dates, length of trajectories)
23
      integer   ndatmax
24
      parameter (ndatmax=500)
25
 
26
c     Numerical epsilon (for float comparison)
27
      real      eps
28
      parameter (eps=0.001)
29
 
30
c     Conversion factors
31
      real      pi180                                   ! deg -> rad
32
      parameter (pi180=3.14159/180.)
33
      real      deg2km                                  ! deg -> km (at equator)
34
      parameter (deg2km=111.)
35
 
36
c     Prefix for primary and secondary fields
37
      character charp
38
      character chars
39
      parameter (charp='P')
40
      parameter (chars='S')
41
 
42
c     --------------------------------------------------------------------
43
c     Declaration of variables
44
c     --------------------------------------------------------------------
45
 
46
c     Input and output format for trajectories (see iotra.f)
47
      integer   inpmode
48
      integer   outmode
49
 
50
c     Input parameters
51
      character*80                           inpfile         ! Input trajectory file
52
      character*80                           outfile         ! Output trajectory file
53
      integer                                ntra            ! Number of trajectories
54
      integer                                ncol            ! Number of columns (including time, lon, lat, p)
55
      integer                                ntim            ! Number of times per trajectory
56
      integer                                ntrace0         ! Number of trace variables
57
      character*80                           tvar0(200)      ! Tracing variable (with mode specification)
58
      character*80                           tvar(200)       ! Tracing variable name (only the variable)
59
      character*1                            tfil(200)       ! Filename prefix 
60
      real                                   fac(200)        ! Scaling factor 
61
      real                                   shift_val(200)  ! Shift in space and time relative to trajectory position
62
      character*80                           shift_dir(200)  ! Direction of shift
63
      integer                                vector(200)     ! Flag for vectorial transformation
64
      integer                                nvector         ! Counter of vectorial variables
65
      integer                                compfl(200)     ! Computation flag (1=compute)
66
      integer                                numdat          ! Number of input files
67
      character*13                           dat(ndatmax)    ! Dates of input files
68
      real                                   timeinc         ! Time increment between input files
69
      real                                   tst             ! Time shift of start relative to first data file
70
      real                                   ten             ! Time shift of end relatiev to first data file  
71
      character*20                           startdate       ! First time/date on trajectory
72
      character*20                           enddate         ! Last time/date on trajectory
73
      integer                                ntrace1         ! Count trace and additional variables
74
      character*80                           timecheck       ! Either 'yes' or 'no'
75
 
76
c     Trajectories
77
      real,allocatable, dimension (:,:,:) :: trainp          ! Input trajectories (ntra,ntim,ncol)
78
      real,allocatable, dimension (:,:,:) :: traint          ! Internal trajectories (ntra,ntim,ncol+ntrace1)
79
      real,allocatable, dimension (:,:,:) :: traout          ! Output trajectories (ntra,ntim,ncol+ntrace0)
80
      integer                                reftime(6)      ! Reference date
81
      character*80                           varsinp(100)    ! Field names for input trajectory
82
      character*80                           varsint(100)    ! Field names for internal trajectory
83
      character*80                           varsout(100)    ! Field names for output trajectory
84
      integer                                fid,fod         ! File identifier for inp and out trajectories
85
      real                                   x0,y0,z0        ! Position of air parcel (physical space)
86
      real                                   reltpos0        ! Relative time of air parcel
87
      real                                   xind,yind,zind  ! Position of air parcel (grid space)
88
      integer                                fbflag          ! Flag for forward (1) or backward (-1) trajectories
89
      integer                                fok(100)        ! Flag whether field is ready
90
 
91
c     Meteorological fields
92
      real,allocatable, dimension (:)     :: zbt0,zbt1       ! Surface pressure
93
      real,allocatable, dimension (:)     :: z3t0,z3t1       ! 3d-pressure
94
      real,allocatable, dimension (:)     :: f3t0,f3t1       ! 3d field for tracing 
95
      character*80                           svars(100)      ! List of variables on S file
96
      character*80                           pvars(100)      ! List of variables on P file
97
      integer                                n_svars         ! Number of variables on S file
98
      integer                                n_pvars         ! Number of variables on P file
99
 
100
c     Grid description
101
      real                                   pollon,pollat   ! Longitude/latitude of pole
102
      real                                   polgam          ! Grid rotation
103
      real                                   xmin,xmax       ! Zonal grid extension
104
      real                                   ymin,ymax       ! Meridional grid extension
105
      integer                                nx,ny,nz        ! Grid dimensions
106
      real                                   dx,dy           ! Horizontal grid resolution
107
      integer                                hem             ! Flag for hemispheric domain
108
      integer                                per             ! Flag for periodic domain
109
      real                                   stagz           ! Vertical staggering
110
      real                                   mdv             ! Missing data value
111
 
112
c     Auxiliary variables
113
      integer                                i,j,k,l,n
114
      real                                   rd
115
      character*80                           filename,varname
116
      real                                   time0,time1,reltpos
117
      integer                                itime0,itime1
118
      integer                                stat
119
      real                                   tstart
120
      integer                                iloaded0,iloaded1
121
      real                                   f0
122
      real                                   frac
123
      real                                   tload,tfrac
124
      integer                                isok
125
      character                              ch
126
      integer                                ind
127
      integer                                ind1,ind2,ind3,ind4,ind5
128
      integer                                ind6,ind7,ind8,ind9,ind0
129
      integer                                noutside
130
      real                                   delta
131
      integer                                itrace0
132
      character*80                           string
133
      real                                   lon,lat,rlon,rlat
134
      integer                                ipoint
135
      character*80                           name
136
      real                                   urot,vrot,u,v
137
 
138
c     Externals 
139
      real                                   int_index4
140
      external                               int_index4
141
 
142
      real                                   lmtolms 
143
      real                                   phtophs    
144
      real                                   lmstolm
145
      real                                   phstoph        
146
      external                               lmtolms,phtophs
147
      external                               lmstolm,phstoph
148
      real                                   phi2phirot
149
      real                                   phirot2phi
150
      real                                   rla2rlarot
151
      real                                   rlarot2rla
152
      external                               phi2phirot,phirot2phi
153
      external                               rla2rlarot,rlarot2rla
154
 
155
c     --------------------------------------------------------------------
156
c     Start of program, Read parameters, get grid parameters
157
c     --------------------------------------------------------------------
158
 
159
c     Write start message
160
      print*,'========================================================='
161
      print*,'              *** START OF PROGRAM TRACE ***'
162
      print*
163
 
164
c     Read parameters
165
      open(10,file='trace.param')
166
       read(10,*) inpfile
167
       read(10,*) outfile
168
       read(10,*) startdate
169
       read(10,*) enddate 
170
       read(10,*) fbflag
171
       read(10,*) numdat
172
       if ( fbflag.eq.1) then
173
          do i=1,numdat
174
             read(10,'(a)') dat(i)
175
          enddo
176
       else
177
          do i=numdat,1,-1
178
             read(10,'(a)') dat(i)
179
          enddo
180
       endif
181
       read(10,*) timeinc
182
       read(10,*) tst
183
       read(10,*) ten
184
       read(10,*) ntra
185
       read(10,*) ntim
186
       read(10,*) ncol
187
       read(10,*) ntrace0
188
       do i=1,ntrace0
189
          read(10,*) tvar(i), fac(i), compfl(i), tfil(i)
190
       enddo
191
       read(10,*) n_pvars
192
       do i=1,n_pvars
193
          read(10,*) pvars(i)
194
       enddo
195
       read(10,*) n_svars
196
       do i=1,n_svars
197
          read(10,*) svars(i)
198
       enddo
199
       read(10,*) timecheck
200
      close(10)
201
 
202
c     Remove commented tracing fields
203
      itrace0 = 1
204
      do while ( itrace0.le.ntrace0) 
205
         string = tvar(itrace0)
206
         if ( string(1:1).eq.'#' ) then
207
            do i=itrace0,ntrace0-1
208
               tvar(i)   = tvar(i+1)
209
               fac(i)    = fac(i+1)
210
               compfl(i) = compfl(i+1)
211
               tfil(i)   = tfil(i+1)
212
            enddo
213
            ntrace0 = ntrace0 - 1
214
         else
215
            itrace0 = itrace0 + 1
216
         endif
217
      enddo
218
 
219
c     Check whether it is a vectorial variable - if yes, split it into
220
c     its two components and mark it as vectorial
221
      nvector = 0
222
      i       = 1
223
      do while ( i.le.ntrace0 )
224
          name   = tvar(i)
225
          ipoint = 0
226
          do j=1,80
227
             if ( name(j:j).eq.'.' ) ipoint = j
228
     	  enddo
229
     	  if ( ipoint.ne.0 ) then
230
             nvector   = nvector + 1
231
     	  	 tvar(i)   = trim ( name(1:ipoint-1) )
232
     	  	 vector(i) = nvector
233
     	  	 do j=i+1,ntrace0
234
     	  	 	tvar  (j+1) = tvar  (j)
235
     	  	    fac   (j+1) = fac   (j)
236
     	  	    compfl(j+1) = compfl(j)
237
     	  	    tfil  (j+1) = tfil  (j)
238
     	  	 enddo
239
     	  	 tvar  (i+1) = trim( name(ipoint+1:80) )
240
     	  	 fac   (i+1) = fac   (i)
241
     	  	 compfl(i+1) = compfl(i)
242
     	  	 tfil  (i+1) = tfil  (i)
243
     	  	 vector(i+1) = nvector
244
     	  	 ntrace0     = ntrace0 + 1
245
     	  	 i = i + 1
246
     	  else
247
     	     vector(i) = 0
248
     	  endif
249
     	  i = i + 1
250
      enddo
251
 
252
c     Save the tracing variable (including all mode specifications)
253
      do i=1,ntrace0
254
         tvar0(i) = tvar(i)
255
      enddo
256
 
257
c     Set the formats of the input and output files
258
      call mode_tra(inpmode,inpfile)
259
      if (inpmode.eq.-1) inpmode=1
260
      call mode_tra(outmode,outfile)
261
      if (outmode.eq.-1) outmode=1
262
 
263
C     Convert time shifts <tst,ten> from <hh.mm> into fractional time
264
      call hhmm2frac(tst,frac)
265
      tst = frac
266
      call hhmm2frac(ten,frac)
267
      ten = frac
268
 
269
c     Set the time for the first data file (depending on forward/backward mode)
270
      if (fbflag.eq.1) then
271
        tstart = -tst
272
      else
273
        tstart = tst
274
      endif
275
 
276
c     Read the constant grid parameters (nx,ny,nz,xmin,xmax,ymin,ymax,pollon,pollat)
277
c     The negative <-fid> of the file identifier is used as a flag for parameter retrieval
278
      filename = charp//dat(1)
279
      varname  = 'ICONCONST'
280
      nx       = 1
281
      ny       = 1
282
      nz       = 1
283
      call input_open (fid,filename)
284
      call input_grid (-fid,varname,xmin,xmax,ymin,ymax,dx,dy,nx,ny,
285
     >                 tload,pollon,pollat,polgam,rd,rd,nz,rd,timecheck)
286
      call input_close(fid)
287
 
288
C     Allocate memory for some meteorological arrays
289
      allocate(zbt0(nx*ny),stat=stat)
290
      if (stat.ne.0) print*,'*** error allocating array zbt0 ***'   ! Surface height
291
      allocate(zbt1(nx*ny),stat=stat)
292
      if (stat.ne.0) print*,'*** error allocating array zbt1 ***'
293
      allocate(z3t0(nx*ny*nz),stat=stat)
294
      if (stat.ne.0) print*,'*** error allocating array z3t0 ***'   ! model level height
295
      allocate(z3t1(nx*ny*nz),stat=stat)
296
      if (stat.ne.0) print*,'*** error allocating array z3t1 ***'
297
      allocate(f3t0(nx*ny*nz),stat=stat)
298
      if (stat.ne.0) print*,'*** error allocating array f3t0 ***'   ! Tracing field
299
      allocate(f3t1(nx*ny*nz),stat=stat)
300
      if (stat.ne.0) print*,'*** error allocating array f3t1 ***'
301
 
302
C     Get memory for trajectory arrays
303
      allocate(trainp(ntra,ntim,ncol),stat=stat)
304
      if (stat.ne.0) print*,'*** error allocating array tra      ***' 
305
 
306
c     Set the flags for periodic domains
307
      if ( abs(xmax-xmin-360.).lt.eps ) then
308
         per = 1
309
      elseif ( abs(xmax-xmin-360.+dx).lt.eps ) then
310
         per = 2
311
      else
312
         per = 0
313
      endif
314
 
315
C     Set logical flag for periodic data set (hemispheric or not)
316
      hem = 0
317
      if (per.eq.0.) then
318
         delta=xmax-xmin-360.
319
         if (abs(delta+dx).lt.eps) then               ! Program aborts: arrays must be closed
320
            print*,' ERROR: arrays must be closed... Stop'
321
        else if (abs(delta).lt.eps) then              ! Periodic and hemispheric
322
           hem=1
323
           per=360.
324
        endif
325
      else                                            ! Periodic and hemispheric
326
         hem=1
327
      endif
328
 
329
c     Write some status information on input parameters
330
      print*,'---- INPUT PARAMETERS -----------------------------------'
331
      print*
332
      print*,'  Input trajectory file  : ',trim(inpfile)
333
      print*,'  Output trajectory file : ',trim(outfile)
334
      print*,'  Format of input file   : ',inpmode
335
      print*,'  Format of output file  : ',outmode
336
      print*,'  Forward/backward       : ',fbflag
337
      print*,'  #tra                   : ',ntra
338
      print*,'  #col                   : ',ncol
339
      print*,'  #tim                   : ',ntim
340
      print*,'  No time check          : ',trim(timecheck)
341
      do i=1,ntrace0
342
         if (compfl(i).eq.0) then
343
            write(*,'(a20,a10,f10.2,a3,1x,a1,a20,i2)')
344
     >                 '   Tracing field          : ',
345
     >                 trim(tvar(i)), fac(i), ' 0 ', tfil(i),
346
     >                 ' -> vector/scalar : ',vector(i)
347
         else
348
            write(*,'(a20,a10,f10.2,a3,1x,a1,a20,i2)')
349
     >                 '   Tracing field          : ',
350
     >                 trim(tvar(i)), fac(i), ' 1 ', tfil(i),
351
     >                 ' -> vector/scalar : ',vector(i)
352
         endif
353
      enddo
354
 
355
c     Write some status information on input data files (netCDF)
356
      print*
357
      print*,'---- INPUT DATA FILES -----------------------------------'
358
      print*
359
      call frac2hhmm(tstart,tload)
360
      print*,'  Time of 1st data file  : ',tload
361
      print*,'  #input files           : ',numdat
362
      print*,'  time increment         : ',timeinc
363
      call frac2hhmm(tst,tload)
364
      print*,'  Shift of start         : ',tload
365
      call frac2hhmm(ten,tload)
366
      print*,'  Shift of end           : ',tload
367
      print*,'  First/last input file  : ',trim(dat(1)),
368
     >                                     ' ... ',
369
     >                                     trim(dat(numdat))
370
      print*,'  Primary variables      : ',trim(pvars(1))
371
      do i=2,n_pvars
372
         print*,'                         : ',trim(pvars(i))
373
      enddo
374
      if ( n_svars.ge.1 ) then
375
         print*,'  Secondary variables    : ',trim(svars(1))
376
         do i=2,n_svars
377
            print*,'                         : ',trim(svars(i))
378
         enddo
379
      endif
380
 
381
c     Write some status information on grid parameters
382
      print*
383
      print*,'---- CONSTANT GRID PARAMETERS ---------------------------'
384
      print*
385
      print*,'  xmin,xmax     : ',xmin,xmax
386
      print*,'  ymin,ymax     : ',ymin,ymax
387
      print*,'  dx,dy         : ',dx,dy
388
      print*,'  pollon,pollat : ',pollon,pollat
389
      print*,'  polgam        : ',polgam
390
      print*,'  nx,ny,nz      : ',nx,ny,nz
391
      print*,'  per, hem      : ',per,hem
392
      print*
393
 
394
c     --------------------------------------------------------------------
395
c     Load the input trajectories
396
c     --------------------------------------------------------------------
397
 
398
c     Read the input trajectory file
399
      call ropen_tra(fid,inpfile,ntra,ntim,ncol,reftime,varsinp,inpmode)
400
      call read_tra (fid,trainp,ntra,ntim,ncol,inpmode)
401
      call close_tra(fid,inpmode)
402
 
403
c     Coordinate rotation
404
      do i=1,ntra
405
         do j=1,ntim
406
 
407
            lon = trainp(i,j,2)
408
            lat = trainp(i,j,3)
409
 
410
            if ( abs(polgam).gt.eps ) then
411
 
412
              rlat = phi2phirot ( lat, lon, pollat, pollon )
413
              rlon = rla2rlarot ( lat, lon, pollat, pollon, polgam )
414
 
415
            else
416
 
417
              if ( abs(pollat-90.).gt.eps) then
418
                 rlon = lmtolms(lat,lon,pollat,pollon)
419
                 rlat = phtophs(lat,lon,pollat,pollon)
420
              else
421
                 rlon = lon
422
                 rlat = lat
423
              endif
424
 
425
            endif
426
 
427
            trainp(i,j,2) = rlon
428
            trainp(i,j,3) = rlat
429
 
430
         enddo
431
 
432
      enddo
433
 
434
c     Check that first four columns correspond to time,lon,lat,p
435
      if ( (varsinp(1).ne.'time' ).or.
436
     >     (varsinp(2).ne.'xpos' ).and.(varsinp(2).ne.'lon' ).or.
437
     >     (varsinp(3).ne.'ypos' ).and.(varsinp(3).ne.'lat' ).or.
438
     >     (varsinp(4).ne.'zpos' ).and.(varsinp(4).ne.'z'   ) )
439
     >then
440
         print*,' ERROR: problem with input trajectories ...'
441
         stop
442
      endif
443
      varsinp(1) = 'time'
444
      varsinp(2) = 'lon'
445
      varsinp(3) = 'lat'
446
      varsinp(4) = 'z'
447
 
448
c     Write some status information of the input trajectories
449
      print*,'---- INPUT TRAJECTORIES ---------------------------------'
450
      print*
451
      print*,' Start date             : ',trim(startdate)
452
      print*,' End date               : ',trim(enddate)
453
      print*,' Reference time (year)  : ',reftime(1)
454
      print*,'                (month) : ',reftime(2)
455
      print*,'                (day)   : ',reftime(3)
456
      print*,'                (hour)  : ',reftime(4)
457
      print*,'                (min)   : ',reftime(5)
458
      print*,' Time range (min)       : ',reftime(6)
459
      do i=1,ncol
460
         print*,' Var                    :',i,trim(varsinp(i))
461
      enddo
462
      print*
463
 
464
c     Run a simple consistency check
465
      call do_timecheck(dat,numdat,timeinc,fbflag,reftime)
466
 
467
c     --------------------------------------------------------------------
468
c     Check dependencies for trace fields which must be calculated
469
c     --------------------------------------------------------------------
470
 
471
c     Set the counter for extra fields
472
      ntrace1 = ntrace0
473
 
474
c     Loop over all tracing variables
475
      i = 1
476
      do while (i.le.ntrace1)
477
 
478
c        Skip fields which must be available on the input files
479
         if (i.le.ntrace0) then
480
            if (compfl(i).eq.0) goto 100
481
         endif
482
 
483
c        Get the dependencies for potential temperature (TH)
484
         if ( tvar(i).eq.'TH' ) then
485
            varname='P'                                   ! P
486
            call add2list(varname,tvar,ntrace1)
487
            varname='T'                                   ! T
488
            call add2list(varname,tvar,ntrace1)
489
 
490
c        Get the dependencies for potential temperature (TH)
491
         elseif ( tvar(i).eq.'RHO' ) then
492
            varname='P'                                   ! P
493
            call add2list(varname,tvar,ntrace1)
494
            varname='T'                                   ! T
495
            call add2list(varname,tvar,ntrace1)
496
 
497
c        Get the dependencies for relative humidity (RH)
498
         elseif ( tvar(i).eq.'RH' ) then
499
            varname='P'                                   ! P
500
            call add2list(varname,tvar,ntrace1)
501
            varname='T'                                   ! T
502
            call add2list(varname,tvar,ntrace1)
503
            varname='QV'                                  ! QV
504
            call add2list(varname,tvar,ntrace1)
505
 
506
c        Get the dependencies for equivalent potential temperature (THE)
507
         elseif ( tvar(i).eq.'THE' ) then
508
            varname='P'                                   ! P
509
            call add2list(varname,tvar,ntrace1)
510
            varname='T'                                   ! T
511
            call add2list(varname,tvar,ntrace1)
512
            varname='QV'                                  ! QV
513
            call add2list(varname,tvar,ntrace1)
514
 
515
c        Get the dependencies for latent heating rate (LHR)
516
         elseif ( tvar(i).eq.'LHR' ) then
517
            varname='P'                                   ! P
518
            call add2list(varname,tvar,ntrace1)
519
            varname='T'                                   ! T
520
            call add2list(varname,tvar,ntrace1)
521
            varname='QV'                                  ! QV
522
            call add2list(varname,tvar,ntrace1)
523
            varname='W'                                   ! W
524
            call add2list(varname,tvar,ntrace1)
525
            varname='RH'                                  ! RH
526
            call add2list(varname,tvar,ntrace1)
527
 
528
c        Get the dependencies for wind speed (VEL)
529
         elseif ( tvar(i).eq.'VEL' ) then
530
            varname='U'                                   ! U
531
            call add2list(varname,tvar,ntrace1)
532
            varname='V'                                   ! V
533
            call add2list(varname,tvar,ntrace1)
534
 
535
c        Get the dependencies for wind direction (DIR)
536
         elseif ( tvar(i).eq.'DIR' ) then
537
            varname='U'                                   ! U
538
            call add2list(varname,tvar,ntrace1)
539
            varname='V'                                   ! V
540
            call add2list(varname,tvar,ntrace1)
541
 
542
c        Get the dependencies for du/dx (DUDX)
543
         elseif ( tvar(i).eq.'DUDX' ) then
544
            varname='U:+1DLON'                            ! U:+1DLON
545
            call add2list(varname,tvar,ntrace1)
546
            varname='U:-1DLON'                            ! U:-1DLON
547
            call add2list(varname,tvar,ntrace1)
548
 
549
c        Get the dependencies for dv(dx (DVDX)
550
         elseif ( tvar(i).eq.'DVDX' ) then
551
            varname='V:+1DLON'                            ! V:+1DLON
552
            call add2list(varname,tvar,ntrace1)
553
            varname='V:-1DLON'                            ! V:-1DLON
554
            call add2list(varname,tvar,ntrace1)
555
 
556
c        Get the dependencies for du/dy (DUDY)
557
         elseif ( tvar(i).eq.'DUDY' ) then
558
            varname='U:+1DLAT'                            ! U:+1DLAT
559
            call add2list(varname,tvar,ntrace1)
560
            varname='U:-1DLAT'                            ! U:-1DLAT
561
            call add2list(varname,tvar,ntrace1)
562
 
563
c        Get the dependencies for dv/dy (DVDY)
564
         elseif ( tvar(i).eq.'DVDY' ) then
565
            varname='V:+1DLAT'                            ! V:+1DLAT
566
            call add2list(varname,tvar,ntrace1)
567
            varname='V:-1DLAT'                            ! V:-1DLAT
568
            call add2list(varname,tvar,ntrace1)
569
 
570
c        Get the dependencies for du/dz (DUDZ)
571
         elseif ( tvar(i).eq.'DUDZ' ) then
572
            varname='U:+1DZ'                              ! U:+1DZ
573
            call add2list(varname,tvar,ntrace1)
574
            varname='U:-1DZ'                              ! U:-1DZ
575
            call add2list(varname,tvar,ntrace1)
576
            varname='Z:+1DZ'                              ! Z:+1DZ
577
            call add2list(varname,tvar,ntrace1)
578
            varname='Z:-1DZ'                              ! Z:-1DZ
579
            call add2list(varname,tvar,ntrace1)
580
 
581
c        Get the dependencies for dv/dz (DVDZ)
582
         elseif ( tvar(i).eq.'DVDZ' ) then
583
            varname='V:+1DZ'                              ! V:+1DZ
584
            call add2list(varname,tvar,ntrace1)
585
            varname='V:-1DZ'                              ! V:-1DZ
586
            call add2list(varname,tvar,ntrace1)
587
            varname='Z:+1DZ'                              ! Z:+1DZ
588
            call add2list(varname,tvar,ntrace1)
589
            varname='Z:-1DZ'                              ! Z:-1DZ
590
            call add2list(varname,tvar,ntrace1)
591
 
592
c        Get the dependencies for dt/dx (DTDX)
593
         elseif ( tvar(i).eq.'DTDX' ) then
594
            varname='T:+1DLON'                            ! T:+1DLON
595
            call add2list(varname,tvar,ntrace1)
596
            varname='T:-1DLON'                            ! T:-1DLON
597
            call add2list(varname,tvar,ntrace1)
598
 
599
c        Get the dependencies for dth/dy (DTHDY)
600
         elseif ( tvar(i).eq.'DTHDY' ) then
601
            varname='T:+1DLAT'                            ! T:+1DLAT
602
            call add2list(varname,tvar,ntrace1)
603
            varname='T:-1DLAT'                            ! T:-1DLAT
604
            call add2list(varname,tvar,ntrace1)
605
            varname='P'                                   ! P
606
            call add2list(varname,tvar,ntrace1)
607
 
608
c        Get the dependencies for dth/dx (DTHDX)
609
         elseif ( tvar(i).eq.'DTHDX' ) then
610
            varname='T:+1DLON'                            ! T:+1DLON
611
            call add2list(varname,tvar,ntrace1)
612
            varname='T:-1DLON'                            ! T:-1DLON
613
            call add2list(varname,tvar,ntrace1)
614
            varname='P'                                   ! P
615
            call add2list(varname,tvar,ntrace1)
616
 
617
c        Get the dependencies for dt/dy (DTDY)
618
         elseif ( tvar(i).eq.'DTDY' ) then
619
            varname='T:+1DLAT'                            ! T:+1DLON
620
            call add2list(varname,tvar,ntrace1)
621
            varname='T:-1DLAT'                            ! T:-1DLON
622
            call add2list(varname,tvar,ntrace1)
623
 
624
c        Get the dependencies for dt/dz (DTDZ)
625
         elseif ( tvar(i).eq.'DTDZ' ) then
626
            varname='T:+1DZ'                              ! T:+1DZ
627
            call add2list(varname,tvar,ntrace1)
628
            varname='T:-1DZ'                              ! T:-1DZ
629
            call add2list(varname,tvar,ntrace1)
630
            varname='Z:+1DZ'                              ! Z:+1DZ
631
            call add2list(varname,tvar,ntrace1)
632
            varname='Z:-1DZ'                              ! Z:-1DZ
633
            call add2list(varname,tvar,ntrace1)
634
 
635
c        Get the dependencies for dth/dz (DTHDZ)
636
         elseif ( tvar(i).eq.'DTHDZ' ) then
637
            varname='T:+1DZ'                              ! T:+1DZ
638
            call add2list(varname,tvar,ntrace1)
639
            varname='T:-1DZ'                              ! T:-1DZ
640
            call add2list(varname,tvar,ntrace1)
641
            varname='T'                                   ! T
642
            call add2list(varname,tvar,ntrace1)
643
            varname='Z:+1DZ'                              ! Z:+1DZ
644
            call add2list(varname,tvar,ntrace1)
645
            varname='Z:-1DZ'                              ! Z:-1DZ
646
            call add2list(varname,tvar,ntrace1)
647
            varname='P'                                   ! P
648
            call add2list(varname,tvar,ntrace1)
649
 
650
c        Get the dependencies for squared Brunt Vaiäläa frequency (NSQ)
651
         elseif ( tvar(i).eq.'NSQ' ) then
652
            varname='DTHDZ'                                ! DTHDZ
653
            call add2list(varname,tvar,ntrace1)
654
            varname='TH'                                   ! TH
655
            call add2list(varname,tvar,ntrace1)
656
 
657
c        Get the dependencies for relative vorticity (RELVORT)
658
         elseif ( tvar(i).eq.'RELVORT' ) then
659
            varname='U'                                    ! U
660
            call add2list(varname,tvar,ntrace1)
661
            varname='DUDY'                                 ! DUDY
662
            call add2list(varname,tvar,ntrace1)
663
            varname='DVDX'                                 ! DVDX
664
            call add2list(varname,tvar,ntrace1)
665
 
666
c        Get the dependencies for relative vorticity (ABSVORT)
667
         elseif ( tvar(i).eq.'ABSVORT' ) then
668
            varname='U'                                    ! U
669
            call add2list(varname,tvar,ntrace1)
670
            varname='DUDY'                                 ! DUDY
671
            call add2list(varname,tvar,ntrace1)
672
            varname='DVDX'                                 ! DVDX
673
            call add2list(varname,tvar,ntrace1)
674
 
675
c        Get the dependencies for divergence (DIV)
676
         elseif ( tvar(i).eq.'DIV' ) then
677
            varname='V'                                    ! U
678
            call add2list(varname,tvar,ntrace1)
679
            varname='DUDX'                                 ! DUDX
680
            call add2list(varname,tvar,ntrace1)
681
            varname='DVDY'                                 ! DVDY
682
            call add2list(varname,tvar,ntrace1)
683
 
684
c        Get the dependencies for deformation (DEF)
685
         elseif ( tvar(i).eq.'DEF' ) then
686
            call add2list(varname,tvar,ntrace1)
687
            varname='DUDX'                                 ! DUDX
688
            call add2list(varname,tvar,ntrace1)
689
            varname='DVDY'                                 ! DVDY
690
            call add2list(varname,tvar,ntrace1)
691
            varname='DUDY'                                 ! DUDY
692
            call add2list(varname,tvar,ntrace1)
693
            varname='DVDX'                                 ! DVDX
694
            call add2list(varname,tvar,ntrace1)
695
 
696
c        Get the dependencies for potential vorticity (PV)
697
         elseif ( tvar(i).eq.'PV' ) then
698
            varname='ABSVORT'                              ! ABSVORT
699
            call add2list(varname,tvar,ntrace1)
700
            varname='DTHDZ'                                ! DTHDZ
701
            call add2list(varname,tvar,ntrace1)
702
            varname='DUDZ'                                 ! DUDZ
703
            call add2list(varname,tvar,ntrace1)
704
            varname='DVDZ'                                 ! DVDZ
705
            call add2list(varname,tvar,ntrace1)
706
            varname='DTHDX'                                ! DTHDX
707
            call add2list(varname,tvar,ntrace1)
708
            varname='DTHDY'                                ! DTHDY
709
            call add2list(varname,tvar,ntrace1)
710
            varname='RHO'                                  ! RHO
711
            call add2list(varname,tvar,ntrace1)
712
 
713
c        Get the dependencies for Richardson number (RI)
714
         elseif ( tvar(i).eq.'RI' ) then
715
            varname='DUDZ'                                 ! DUDZ
716
            call add2list(varname,tvar,ntrace1)
717
            varname='DVDZ'                                 ! DVDZ
718
            call add2list(varname,tvar,ntrace1)
719
            varname='NSQ'                                  ! NSQ
720
            call add2list(varname,tvar,ntrace1)
721
 
722
c        Get the dependencies for Ellrod&Knapp's turbulence index (TI)
723
         elseif ( tvar(i).eq.'TI' ) then
724
            varname='DEF'                                  ! DEF
725
            call add2list(varname,tvar,ntrace1)       
726
            varname='DUDZ'                                 ! DUDZ
727
            call add2list(varname,tvar,ntrace1)       
728
            varname='DVDZ'                                 ! DVDZ
729
            call add2list(varname,tvar,ntrace1)       
730
 
731
         endif
732
 
733
c        Exit point for handling additional fields
734
 100     continue
735
         i = i + 1
736
 
737
      enddo
738
 
739
c     Save the full variable name (including shift specification)
740
      do i=1,ncol
741
         varsint(i)      = varsinp(i)
742
      enddo
743
      do i=1,ntrace1
744
         varsint(i+ncol) = tvar(i)
745
      enddo
746
 
747
c     Split the variable name and set flags
748
      do i=1,ntrace1
749
 
750
c        Set the scaling factor and the source
751
         if ( i.gt.ntrace0 ) then
752
            fac(i)  = 1.
753
            tfil(i) = '-'
754
         endif
755
 
756
c        Set the base variable name, the shift and the direction
757
         call splitvar(tvar(i),shift_val(i),shift_dir(i) )
758
 
759
c        Set the prefix of the file name
760
         if ( (tfil(i).eq.'-').or.(tfil(i).eq.'*') ) then
761
           if ( ( compfl(i).eq.0 ).or.(i.gt.ntrace0) ) then
762
            do j=1,n_pvars
763
               if ( tvar(i).eq.pvars(j) ) tfil(i)=charp
764
            enddo
765
            do j=1,n_svars
766
               if ( tvar(i).eq.svars(j) ) tfil(i)=chars
767
            enddo
768
           endif
769
         endif
770
 
771
c        Set the computational flag
772
         if ( tvar(i).eq.'Z' ) then
773
            compfl(i) = 0
774
            tfil(i)   = charp
775
         elseif ( tfil(i).ne.'*' ) then
776
            compfl(i) = 0
777
         else
778
            compfl(i) = 1
779
         endif
780
 
781
      enddo
782
 
783
c     Check whether the shift modes are supported
784
      do i=1,ntrace1
785
         if ( ( shift_dir(i).ne.'nil'     ).and.
786
     >        ( shift_dir(i).ne.'DLON'    ).and.
787
     >        ( shift_dir(i).ne.'DLAT'    ).and.
788
     >        ( shift_dir(i).ne.'DZ'      ).and.
789
     >        ( shift_dir(i).ne.'M'       ).and.
790
     >        ( shift_dir(i).ne.'KM(LON)' ).and.
791
     >        ( shift_dir(i).ne.'KM(LAT)' ).and.
792
     >        ( shift_dir(i).ne.'H'       ).and.
793
     >        ( shift_dir(i).ne.'MIN'     ).and.
794
     >        ( shift_dir(i).ne.'INDZ'    ) )
795
     >   then
796
            print*,' ERROR: shift mode ',trim(shift_dir(i)),
797
     >             ' not supported'
798
            stop
799
         endif
800
      enddo
801
 
802
c     Write status information
803
      print*
804
      print*,'---- COMPLETE TABLE FOR TRACING -------------------------'   
805
      print*
806
      do i=1,ntrace1
807
         if ( ( shift_dir(i).ne.'nil' ) ) then
808
            write(*,'(i4,a4,a8,f10.2,a8,3x,a1,i5)') 
809
     >           i,' : ',trim(tvar(i)),shift_val(i),trim(shift_dir(i)),
810
     >           tfil(i),compfl(i)
811
         else
812
            write(*,'(i4,a4,a8,10x,8x,3x,a1,i5)') 
813
     >           i,' : ',trim(tvar(i)),tfil(i),compfl(i)
814
         endif
815
      enddo
816
      if ( i.eq.ntrace0 ) print*
817
 
818
c     --------------------------------------------------------------------
819
c     Prepare the internal and output trajectories
820
c     --------------------------------------------------------------------
821
 
822
c     Allocate memory for internal trajectories
823
      allocate(traint(ntra,ntim,ncol+ntrace1),stat=stat)
824
      if (stat.ne.0) print*,'*** error allocating array traint   ***'
825
 
826
c     Copy input to output trajectory
827
      do i=1,ntra
828
         do j=1,ntim
829
            do k=1,ncol
830
               traint(i,j,k)=trainp(i,j,k)
831
            enddo
832
         enddo
833
      enddo
834
 
835
c     Set the flags for ready fields/colums - at begin only read-in fields are ready
836
      do i=1,ncol
837
         fok(i) = 1
838
      enddo
839
      do i=ncol+1,ntrace1
840
         fok(i) = 0
841
      enddo
842
 
843
c     --------------------------------------------------------------------
844
c     Trace the fields (fields available on input files)
845
c     --------------------------------------------------------------------
846
 
847
      print*
848
      print*,'---- TRACING FROM PRIMARY AND SECONDARY DATA FILES ------'     
849
 
850
c     Loop over all tracing fields
851
      do i=1,ntrace1
852
 
853
C         Skip fields which must be computed (compfl=1), will be handled later
854
          if (compfl(i).ne.0)  goto 110
855
 
856
c         Write some status information
857
          print*
858
          print*,' Now tracing             : ',
859
     >         trim(tvar(i)),shift_val(i),trim(shift_dir(i)),
860
     >         compfl(i),' ',trim(tfil(i))
861
 
862
c         Set the flag for ready field/column
863
          fok(ncol+i) = 1
864
 
865
c         Reset flags for load manager
866
          iloaded0 = -1
867
          iloaded1 = -1
868
 
869
c         Reset the counter for fields outside domain
870
          noutside = 0
871
 
872
c         Loop over all times
873
          do j=1,ntim
874
 
875
c            Convert trajectory time from hh.mm to fractional time
876
             call hhmm2frac(trainp(1,j,1),tfrac)
877
 
878
c            Shift time if requested
879
             if ( shift_dir(i).eq.'H' ) then
880
                tfrac = tfrac + shift_val(i)
881
             elseif ( shift_dir(i).eq.'MIN' ) then
882
                tfrac = tfrac + shift_val(i)/60.
883
             endif
884
 
885
c            Get the times which are needed
886
             itime0   = int(abs(tfrac-tstart)/timeinc) + 1
887
             time0    = tstart + fbflag * real(itime0-1) * timeinc
888
             itime1   = itime0 + 1
889
             time1    = time0 + fbflag * timeinc
890
             if ( itime1.gt.numdat ) then
891
                itime1 = itime0
892
                time1  = time0
893
             endif
894
 
895
             print*,tfrac,time0,time1
896
 
897
c            Load manager: Check whether itime0 can be copied from itime1
898
             if ( itime0.eq.iloaded1 ) then
899
                f3t0     = f3t1
900
                z3t0     = z3t1
901
                zbt0     = zbt1
902
                iloaded0 = itime0
903
             endif
904
 
905
c            Load manager: Check whether itime1 can be copied from itime0
906
             if ( itime1.eq.iloaded0 ) then
907
                f3t1     = f3t0
908
                z3t1     = z3t0
909
                zbt1     = zbt0
910
                iloaded1 = itime1
911
             endif
912
 
913
c            Load manager:  Load first time (tracing variable and grid)
914
             if ( itime0.ne.iloaded0 ) then
915
 
916
                filename = tfil(i)//dat(itime0)
917
                call frac2hhmm(time0,tload)
918
                varname  = tvar(i) 
919
                write(*,'(a23,a20,a3,a5,f7.2)') 
920
     >               '    ->  loading          : ',
921
     >               trim(filename),'  ',trim(varname),tload
922
                call input_open (fid,filename)
923
                call input_wind 
924
     >               (fid,varname,f3t0,tload,stagz,mdv,
925
     >               xmin,xmax,ymin,ymax,dx,dy,nx,ny,nz,timecheck)     
926
                call input_grid      
927
     >          (fid,varname,xmin,xmax,ymin,ymax,dx,dy,nx,ny,
928
     >          tload,pollon,pollat,polgam,z3t0,zbt0,nz,stagz,timecheck)
929
                call input_close(fid)
930
 
931
                iloaded0 = itime0
932
 
933
             endif
934
 
935
c            Load manager: Load second time (tracing variable and grid)
936
             if ( itime1.ne.iloaded1 ) then
937
 
938
                filename = tfil(i)//dat(itime1)
939
                call frac2hhmm(time1,tload)
940
                varname  = tvar(i) 
941
                write(*,'(a23,a20,a3,a5,f7.2)') 
942
     >               '    ->  loading          : ',
943
     >               trim(filename),'  ',trim(varname),tload
944
                call input_open (fid,filename)
945
                call input_wind 
946
     >               (fid,varname,f3t1,tload,stagz,mdv,
947
     >               xmin,xmax,ymin,ymax,dx,dy,nx,ny,nz,timecheck)
948
                call input_grid      
949
     >          (fid,varname,xmin,xmax,ymin,ymax,dx,dy,nx,ny,
950
     >          tload,pollon,pollat,polgam,z3t1,zbt1,nz,stagz,timecheck)
951
                call input_close(fid)
952
 
953
                iloaded1 = itime1
954
 
955
             endif
956
 
957
c            Loop over all trajectories
958
             do k=1,ntra
959
 
960
c               Set the horizontal position where to interpolate to
961
                x0       = traint(k,j,2)                          ! Longitude
962
                y0       = traint(k,j,3)                          ! Latitude
963
 
964
c               Set the vertical position where to interpolate to
965
                if ( nz.gt.1 ) then
966
                   z0 = traint(k,j,4)                             ! Pressure (3D tracing)
967
                else
968
                   z0 = 0.                                        ! Lowest level (2D tracing)
969
                endif
970
 
971
c               Set negative pressures to mdv
972
c                if (z0.lt.0.) traint(k,j,4) = mdv
973
 
974
c               Set the relative time
975
                call hhmm2frac(traint(k,j,1),tfrac)
976
                reltpos0 = fbflag * (tfrac-time0)/timeinc    
977
 
978
c               Make adjustments depending on the shift flag
979
                if ( shift_dir(i).eq.'DLON' ) then                         ! DLON
980
                   x0 = x0 + shift_val(i)
981
 
982
                elseif  ( shift_dir(i).eq.'DLAT' ) then                    ! DLAT
983
                   y0 = y0 + shift_val(i)
984
 
985
                elseif ( shift_dir(i).eq.'KM(LON)' ) then                  ! KM(LON)
986
                   x0 = x0 + shift_val(i)/deg2km * 1./cos(y0*pi180)
987
 
988
                elseif ( shift_dir(i).eq.'KM(LAT)' ) then                  ! KM(LAT)
989
                   y0 = y0 + shift_val(i)/deg2km 
990
 
991
                elseif ( shift_dir(i).eq.'M' ) then                        ! M
992
                   z0 = z0 + shift_val(i)
993
 
994
                elseif ( shift_dir(i).eq.'DZ' ) then                       ! DZ
995
                   call get_index4 (xind,yind,zind,x0,y0,z0,reltpos0,
996
     >                              z3t0,z3t1,zbt0,zbt1,3,
997
     >                              nx,ny,nz,xmin,ymin,dx,dy,mdv)
998
                   zind = zind + shift_val(i)
999
                   z0   = int_index4(z3t0,z3t1,nx,ny,nz,
1000
     >                              xind,yind,zind,reltpos0,mdv)
1001
 
1002
                elseif ( shift_dir(i).eq.'INDZ' ) then                      ! INDZ
1003
                   z0   = int_index4(z3t0,z3t1,nx,ny,nz,
1004
     >                            xind,yind,shift_val(i),reltpos0,mdv)
1005
 
1006
                endif
1007
 
1008
c               Handle periodic boundaries in zonal direction
1009
                if ( (x0.gt.xmax).and.(per.ne.0) ) x0 = x0 - 360.
1010
                if ( (x0.lt.xmin).and.(per.ne.0) ) x0 = x0 + 360.
1011
 
1012
c               Handle pole problems for hemispheric data (taken from caltra.f)
1013
                if ((hem.eq.1).and.(y0.gt.90.)) then
1014
                   y0=180.-y0
1015
                   x0=x0+per/2.
1016
                endif
1017
                if ((hem.eq.1).and.(y0.lt.-90.)) then
1018
                   y0=-180.-y0
1019
                   x0=x0+per/2.
1020
                endif
1021
                if (y0.gt.89.99) then
1022
                   y0=89.99
1023
                endif
1024
 
1025
C               Get the index where to interpolate (x0,y0,p0)
1026
                if ( (abs(x0-mdv).gt.eps).and.
1027
     >               (abs(x0-mdv).gt.eps) )
1028
     >          then           
1029
                   call get_index4 (xind,yind,zind,x0,y0,z0,reltpos0,
1030
     >                              z3t0,z3t1,zbt0,zbt1,3,
1031
     >                              nx,ny,nz,xmin,ymin,dx,dy,mdv)
1032
                else
1033
                   xind = mdv
1034
                   yind = mdv
1035
                   zind = mdv
1036
                endif
1037
 
1038
c               If vertical index is between surface and first level - > set to first level
1039
                if ( zind.lt.1. ) zind = 1.
1040
 
1041
c               Do the interpolation
1042
                if ( (xind.ge.1.).and.(xind.le.real(nx)).and.
1043
     >               (yind.ge.1.).and.(yind.le.real(ny)).and.
1044
     >               (zind.ge.1.).and.(zind.le.real(nz)) )
1045
     >          then           
1046
                   f0 = int_index4(f3t0,f3t1,nx,ny,nz,
1047
     >                             xind,yind,zind,reltpos0,mdv)
1048
                elseif (noutside.lt.10) then
1049
                   print*,' ',trim(tvar(i)),' @ ',x0,y0,z0,'outside'
1050
                   f0       = mdv
1051
                   noutside = noutside + 1
1052
                elseif (noutside.eq.10) then
1053
                   print*,' ...more than 10 outside...'
1054
                   f0       = mdv
1055
                   noutside = noutside + 1
1056
                else
1057
                   f0       = mdv
1058
                endif
1059
 
1060
c               Save the new field
1061
                if ( abs(f0-mdv).gt.eps) then
1062
                   traint(k,j,ncol+i) = f0
1063
                else
1064
                   traint(k,j,ncol+i) = mdv
1065
                endif
1066
 
1067
             enddo
1068
 
1069
          enddo
1070
 
1071
c         Exit point for loop over all tracing variables
1072
 110      continue
1073
 
1074
       enddo
1075
 
1076
c     --------------------------------------------------------------------
1077
c     Calculate additional fields along the trajectories
1078
c     --------------------------------------------------------------------
1079
 
1080
      print*
1081
      print*,'---- CALCULATE ADDITIONAL FIELDS FROM TRAJECTORY TABLE --'
1082
 
1083
c     Loop over all tracing fields
1084
      do i=ntrace1,1,-1
1085
 
1086
C         Skip fields which must not be computed (compfl=0)
1087
          if (compfl(i).eq.0) goto 120
1088
 
1089
c         Write some status information
1090
          print*
1091
          write(*,'(a10,f10.2,a5,i3,3x,a2)')
1092
     >         trim(tvar(i)),shift_val(i),trim(shift_dir(i)),
1093
     >         compfl(i),trim(tfil(i))
1094
 
1095
c         Loop over trajectories and times
1096
          do j=1,ntra
1097
          do k=1,ntim
1098
 
1099
c            Potential temperature (TH)
1100
             if  ( varsint(i+ncol).eq.'TH' ) then
1101
 
1102
                varname='T'
1103
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1104
                varname='P'
1105
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1106
 
1107
                call calc_TH  (traint(j,k,ncol+i), traint(j,k,ind1), 
1108
     >                                             traint(j,k,ind2) )
1109
 
1110
c            Density (RHO)
1111
             elseif  ( varsint(i+ncol).eq.'RHO' ) then
1112
 
1113
                varname='T'
1114
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1115
                varname='P'
1116
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1117
 
1118
                call calc_RHO (traint(j,k,ncol+i), traint(j,k,ind1), 
1119
     >                                             traint(j,k,ind2) )
1120
 
1121
c            Relative humidity (RH)
1122
             elseif  ( varsint(i+ncol).eq.'RH' ) then
1123
 
1124
                varname='T'
1125
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1126
                varname='P'
1127
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1128
                varname='QV'
1129
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)               
1130
 
1131
                call calc_RH (traint(j,k,ncol+i), traint(j,k,ind1), 
1132
     >                                            traint(j,k,ind2),
1133
     >                                            traint(j,k,ind3) )
1134
 
1135
c            Equivalent potential temperature (THE)
1136
             elseif  ( varsint(i+ncol).eq.'THE' ) then
1137
 
1138
                varname='T'
1139
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1140
                varname='P'
1141
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1142
                varname='QV'
1143
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)                
1144
 
1145
                call calc_THE (traint(j,k,ncol+i), traint(j,k,ind1), 
1146
     >                                             traint(j,k,ind2),
1147
     >                                             traint(j,k,ind3) )
1148
 
1149
c            Latent heating rate (LHR)
1150
             elseif  ( varsint(i+ncol).eq.'LHR' ) then
1151
 
1152
                varname='T'
1153
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1154
                varname='P'
1155
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1156
                varname='QV'
1157
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)                
1158
                varname='W'
1159
                call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)                
1160
                varname='RH'
1161
                call list2ind (ind5,varname,varsint,fok,ncol+ntrace1)                
1162
 
1163
                call calc_LHR (traint(j,k,ncol+i), traint(j,k,ind1), 
1164
     >                                             traint(j,k,ind2),
1165
     >                                             traint(j,k,ind3),
1166
     >                                             traint(j,k,ind4),
1167
     >                                             traint(j,k,ind5) )
1168
 
1169
c            Wind speed (VEL)
1170
             elseif  ( varsint(i+ncol).eq.'VEL' ) then
1171
 
1172
                varname='U'
1173
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1174
                varname='V'
1175
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1176
 
1177
                call calc_VEL (traint(j,k,ncol+i), traint(j,k,ind1), 
1178
     >                                             traint(j,k,ind2) )
1179
 
1180
c            Wind direction (DIR)
1181
             elseif  ( varsint(i+ncol).eq.'DIR' ) then
1182
 
1183
                varname='U'
1184
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1185
                varname='V'
1186
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1187
 
1188
                call calc_DIR (traint(j,k,ncol+i), traint(j,k,ind1), 
1189
     >                                             traint(j,k,ind2) )
1190
 
1191
c            Zonal gradient of U (DUDX)
1192
             elseif  ( varsint(i+ncol).eq.'DUDX' ) then
1193
 
1194
                varname='U:+1DLON'
1195
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1196
                varname='U:-1DLON'
1197
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1198
                varname='lat'
1199
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1200
 
1201
                call calc_DUDX (traint(j,k,ncol+i), traint(j,k,ind1), 
1202
     >                                              traint(j,k,ind2),
1203
     >                                              traint(j,k,ind3) )
1204
 
1205
c            Zonal gradient of V (DVDX)
1206
             elseif  ( varsint(i+ncol).eq.'DVDX' ) then
1207
 
1208
                varname='V:+1DLON'
1209
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1210
                varname='V:-1DLON'
1211
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1212
                varname='lat'
1213
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1214
 
1215
                call calc_DVDX (traint(j,k,ncol+i), traint(j,k,ind1), 
1216
     >                                              traint(j,k,ind2),
1217
     >                                              traint(j,k,ind3) )
1218
 
1219
c            Zonal gradient of T (DTDX)
1220
             elseif  ( varsint(i+ncol).eq.'DVDX' ) then
1221
 
1222
                varname='T:+1DLON'
1223
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1224
                varname='T:-1DLON'
1225
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1226
                varname='lat'
1227
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1228
 
1229
                call calc_DTDX (traint(j,k,ncol+i), traint(j,k,ind1), 
1230
     >                                              traint(j,k,ind2),
1231
     >                                              traint(j,k,ind3) )
1232
 
1233
c            Zonal gradient of TH (DTHDX)
1234
             elseif  ( varsint(i+ncol).eq.'DTHDX' ) then
1235
 
1236
                varname='T:+1DLON'
1237
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1238
                varname='T:-1DLON'
1239
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1240
                varname='P'
1241
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1242
                varname='lat'
1243
                call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1244
 
1245
                call calc_DTHDX (traint(j,k,ncol+i), traint(j,k,ind1), 
1246
     >                                               traint(j,k,ind2),
1247
     >                                               traint(j,k,ind3),
1248
     >                                               traint(j,k,ind4) )
1249
 
1250
c            Meridional gradient of U (DUDY)
1251
             elseif  ( varsint(i+ncol).eq.'DUDY' ) then
1252
 
1253
                varname='U:+1DLAT'
1254
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1255
                varname='U:-1DLAT'
1256
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1257
 
1258
                call calc_DUDY (traint(j,k,ncol+i), traint(j,k,ind1), 
1259
     >                                              traint(j,k,ind2) )
1260
 
1261
c            Meridional gradient of V (DVDY)
1262
             elseif  ( varsint(i+ncol).eq.'DVDY' ) then
1263
 
1264
                varname='V:+1DLAT'
1265
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1266
                varname='V:-1DLAT'
1267
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1268
 
1269
                call calc_DVDY (traint(j,k,ncol+i), traint(j,k,ind1), 
1270
     >                                              traint(j,k,ind2) )
1271
 
1272
c            Meridional gradient of T (DTDY)
1273
             elseif  ( varsint(i+ncol).eq.'DTDY' ) then
1274
 
1275
                varname='T:+1DLAT'
1276
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1277
                varname='T:-1DLAT'
1278
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1279
 
1280
                call calc_DTDY (traint(j,k,ncol+i), traint(j,k,ind1), 
1281
     >                                              traint(j,k,ind2) )
1282
 
1283
c            Meridional gradient of TH (DTHDY)
1284
             elseif  ( varsint(i+ncol).eq.'DTHDY' ) then
1285
 
1286
                varname='T:+1DLAT'
1287
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1288
                varname='T:-1DLAT'
1289
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1290
                varname='P'
1291
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1292
 
1293
                call calc_DTDY (traint(j,k,ncol+i), traint(j,k,ind1), 
1294
     >                                              traint(j,k,ind2),
1295
     >                                              traint(j,k,ind3) )
1296
 
1297
 
1298
c            Vertical wind shear DU/DZ (DUDZ)
1299
             elseif  ( varsint(i+ncol).eq.'DUDZ' ) then
1300
 
1301
                varname='U:+1DZ'
1302
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1303
                varname='U:-1DZ'
1304
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1305
                varname='Z:+1DZ'
1306
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1) 
1307
                varname='Z:-1DZ'
1308
                call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1309
 
1310
                call calc_DUDZ (traint(j,k,ncol+i), traint(j,k,ind1),
1311
     >                                              traint(j,k,ind2),
1312
     >                                              traint(j,k,ind3),
1313
     >                                              traint(j,k,ind4) )
1314
 
1315
c            Vertical wind shear DV/DZ (DVDZ)
1316
             elseif  ( varsint(i+ncol).eq.'DVDZ' ) then
1317
 
1318
                varname='V:+1DZ'
1319
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1320
                varname='V:-1DZ'
1321
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1322
                varname='Z:+1DZ'
1323
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1) 
1324
                varname='Z:-1DZ'
1325
                call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1326
 
1327
                call calc_DVDZ (traint(j,k,ncol+i), traint(j,k,ind1),
1328
     >                                              traint(j,k,ind2),
1329
     >                                              traint(j,k,ind3),
1330
     >                                              traint(j,k,ind4) )
1331
 
1332
c            Vertical derivative of T (DTDZ)
1333
             elseif  ( varsint(i+ncol).eq.'DTDZ' ) then
1334
 
1335
                varname='T:+1DZ'
1336
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1337
                varname='T:-1DZ'
1338
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1339
                varname='Z:+1DZ'
1340
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1) 
1341
                varname='Z:-1DZ'
1342
                call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1343
 
1344
                call calc_DTDZ (traint(j,k,ncol+i), traint(j,k,ind1),
1345
     >                                              traint(j,k,ind2),
1346
     >                                              traint(j,k,ind3),
1347
     >                                              traint(j,k,ind4) )
1348
 
1349
c            Vertical derivative of TH (DTHDZ)
1350
             elseif  ( varsint(i+ncol).eq.'DTHDZ' ) then
1351
 
1352
                varname='T:+1DZ'
1353
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1354
                varname='T:-1DZ'
1355
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1356
                varname='Z:+1DZ'
1357
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1) 
1358
                varname='Z:-1DZ'
1359
                call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1360
                varname='P'
1361
                call list2ind (ind5,varname,varsint,fok,ncol+ntrace1)
1362
                varname='T'
1363
                call list2ind (ind6,varname,varsint,fok,ncol+ntrace1)
1364
 
1365
                call calc_DTHDZ (traint(j,k,ncol+i), traint(j,k,ind1),
1366
     >                                               traint(j,k,ind2),
1367
     >                                               traint(j,k,ind3),
1368
     >                                               traint(j,k,ind4),
1369
     >                                               traint(j,k,ind5),
1370
     >                                               traint(j,k,ind6) )
1371
 
1372
c            Squared Brunt-Vaisäla frequency (NSQ)
1373
             elseif  ( varsint(i+ncol).eq.'NSQ' ) then
1374
 
1375
                varname='DTHDZ'
1376
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1377
                varname='TH'
1378
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1379
 
1380
                call calc_NSQ (traint(j,k,ncol+i), traint(j,k,ind1), 
1381
     >                                             traint(j,k,ind2) )
1382
 
1383
c            Relative vorticity (RELVORT)
1384
             elseif  ( varsint(i+ncol).eq.'RELVORT' ) then
1385
 
1386
                varname='DUDY'
1387
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1388
                varname='DVDX'
1389
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1390
                varname='U'
1391
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1392
                varname='lat'
1393
                call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1394
 
1395
                call calc_RELVORT (traint(j,k,ncol+i), traint(j,k,ind1), 
1396
     >                                                 traint(j,k,ind2),
1397
     >                                                 traint(j,k,ind3),
1398
     >                                                 traint(j,k,ind4))
1399
 
1400
c            Absolute vorticity (ABSVORT)
1401
             elseif  ( varsint(i+ncol).eq.'ABSVORT' ) then
1402
 
1403
                varname='DUDY'
1404
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1405
                varname='DVDX'
1406
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1407
                varname='U'
1408
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1409
                varname='lat'
1410
                call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1411
                varname='lon'
1412
                call list2ind (ind5,varname,varsint,fok,ncol+ntrace1)
1413
 
1414
                call calc_ABSVORT (traint(j,k,ncol+i), traint(j,k,ind1), 
1415
     >                                                 traint(j,k,ind2),
1416
     >                                                 traint(j,k,ind3),
1417
     >                                                 traint(j,k,ind4),
1418
     >                                                 traint(j,k,ind5),
1419
     >                                                 pollon,pollat  )
1420
 
1421
c            Divergence (DIV)
1422
             elseif  ( varsint(i+ncol).eq.'DIV' ) then
1423
 
1424
                varname='DUDX'
1425
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1426
                varname='DVDY'
1427
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1428
                varname='V'
1429
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1430
                varname='lat'
1431
                call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1432
 
1433
                call calc_DIV (traint(j,k,ncol+i), traint(j,k,ind1), 
1434
     >                                             traint(j,k,ind2),
1435
     >                                             traint(j,k,ind3),
1436
     >                                             traint(j,k,ind4))
1437
 
1438
c            Deformation (DEF)
1439
             elseif  ( varsint(i+ncol).eq.'DEF' ) then
1440
 
1441
                varname='DUDX'
1442
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1443
                varname='DVDX'
1444
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1445
                varname='DUDY'
1446
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1447
                varname='DVDY'
1448
                call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1449
 
1450
                call calc_DEF (traint(j,k,ncol+i), traint(j,k,ind1), 
1451
     >                                             traint(j,k,ind2),
1452
     >                                             traint(j,k,ind3),
1453
     >                                             traint(j,k,ind4))
1454
 
1455
c            Potential Vorticity (PV)
1456
             elseif  ( varsint(i+ncol).eq.'PV' ) then
1457
 
1458
                varname='ABSVORT'
1459
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1460
                varname='DTHDZ'
1461
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1462
                varname='DUDZ'
1463
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1464
                varname='DVDZ'
1465
                call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1466
                varname='DTHDX'
1467
                call list2ind (ind5,varname,varsint,fok,ncol+ntrace1)
1468
                varname='DTHDY'
1469
                call list2ind (ind6,varname,varsint,fok,ncol+ntrace1)
1470
                varname='RHO'
1471
                call list2ind (ind7,varname,varsint,fok,ncol+ntrace1)
1472
 
1473
                call calc_PV (traint(j,k,ncol+i), traint(j,k,ind1), 
1474
     >                                            traint(j,k,ind2),
1475
     >                                            traint(j,k,ind3),
1476
     >                                            traint(j,k,ind4),
1477
     >                                            traint(j,k,ind5),
1478
     >                                            traint(j,k,ind6),
1479
     >                                            traint(j,k,ind7) )
1480
 
1481
c            Richardson number (RI)
1482
             elseif  ( varsint(i+ncol).eq.'RI' ) then
1483
 
1484
                varname='DUDZ'
1485
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1486
                varname='DVDZ'
1487
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1488
                varname='NSQ'
1489
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1490
 
1491
                call calc_RI (traint(j,k,ncol+i), traint(j,k,ind1), 
1492
     >                                            traint(j,k,ind2),
1493
     >                                            traint(j,k,ind3) )
1494
 
1495
c            Ellrod and Knapp's turbulence idicator (TI)
1496
             elseif  ( varsint(i+ncol).eq.'TI' ) then
1497
 
1498
                varname='DEF'
1499
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1500
                varname='DUDZ'
1501
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1502
                varname='DVDZ'
1503
                call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1504
 
1505
                call calc_TI (traint(j,k,ncol+i), traint(j,k,ind1), 
1506
     >                                            traint(j,k,ind2),
1507
     >                                            traint(j,k,ind3) )
1508
 
1509
c            Spherical distance from starting position (DIST0)
1510
             elseif  ( varsint(i+ncol).eq.'DIST0' ) then
1511
 
1512
                varname='lon'
1513
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1514
                varname='lat'
1515
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1516
 
1517
                call calc_DIST0 (traint(j,k,ncol+i), traint(j,k,ind1), 
1518
     >                                               traint(j,k,ind2),
1519
     >                                               traint(j,1,ind1), 
1520
     >                                               traint(j,1,ind2) )
1521
 
1522
c            Spherical distance length of trajectory (DIST)
1523
             elseif  ( varsint(i+ncol).eq.'DIST' ) then
1524
 
1525
                varname='lon'
1526
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1527
                varname='lat'
1528
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1529
 
1530
                if ( k.eq.1 ) then
1531
                   traint(j,k,ncol+i) = 0.
1532
                else
1533
                   call calc_DIST0 (delta, traint(j,k  ,ind1), 
1534
     >                                     traint(j,k  ,ind2),
1535
     >                                     traint(j,k-1,ind1), 
1536
     >                                     traint(j,k-1,ind2) )
1537
                   traint(j,k,ncol+i) = traint(j,k-1,ncol+i) + delta
1538
                endif
1539
 
1540
c            Heading of the trajectory (HEAD)
1541
             elseif  ( varsint(i+ncol).eq.'HEAD' ) then
1542
 
1543
                varname='lon'
1544
                call list2ind (ind1,varname,varsint,fok,ncol+ntrace1) 
1545
                varname='lat'
1546
                call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1547
 
1548
                if (k.eq.ntim) then
1549
                   traint(j,k,ncol+i) = mdv
1550
                else
1551
                   call calc_HEAD (traint(j,k,ncol+i), 
1552
     >                                         traint(j,k  ,ind1), 
1553
     >                                         traint(j,k  ,ind2),
1554
     >                                         traint(j,k+1,ind1), 
1555
     >                                         traint(j,k+1,ind2) )
1556
                endif
1557
 
1558
 
1559
c            Invalid tracing variable
1560
             else
1561
 
1562
                print*,' ERROR: invalid tracing variable ',
1563
     >                   trim(varsint(i+ncol))
1564
                stop
1565
 
1566
 
1567
             endif
1568
 
1569
c         End loop over all trajectories and times   
1570
          enddo
1571
          enddo
1572
 
1573
c         Set the flag for a ready field/column
1574
          fok(ncol+i) = 1
1575
 
1576
 
1577
c         Exit point for loop over all tracing fields
1578
 120      continue
1579
 
1580
       enddo
1581
 
1582
c     --------------------------------------------------------------------
1583
c     Write output to output trajectory file
1584
c     --------------------------------------------------------------------
1585
 
1586
c     Write status information
1587
      print*
1588
      print*,'---- WRITE OUTPUT TRAJECTORIES --------------------------'   
1589
      print*
1590
 
1591
c     Allocate memory for internal trajectories
1592
      allocate(traout(ntra,ntim,ncol+ntrace0),stat=stat)
1593
      if (stat.ne.0) print*,'*** error allocating array traout   ***'
1594
 
1595
c     Copy input to output trajectory (apply scaling of output)
1596
      do i=1,ntra
1597
         do j=1,ntim
1598
            do k=1,ncol+ntrace0
1599
               if ( k.le.ncol ) then
1600
                  traout(i,j,k) = traint(i,j,k)
1601
               elseif ( abs(traint(i,j,k)-mdv).gt.eps ) then
1602
                  traout(i,j,k) = fac(k-ncol) * traint(i,j,k)
1603
               else
1604
                  traout(i,j,k) = mdv
1605
               endif
1606
            enddo
1607
         enddo
1608
      enddo
1609
 
1610
c     Set the variable names for output trajectory
1611
      do i=1,ncol+ntrace0
1612
         varsout(i)      = varsint(i)
1613
      enddo  
1614
 
1615
c     Coordinate rotation
1616
      print*,' Rotation (rot-lon/lat -> true-lon/lat )'
1617
      print*
1618
      do i=1,ntra
1619
         do j=1,ntim
1620
 
1621
            rlon = traout(i,j,2)
1622
            rlat = traout(i,j,3)
1623
 
1624
            if ( abs(polgam).gt.eps ) then
1625
 
1626
               lon = rlarot2rla (rlat, rlon, pollat, pollon, polgam)
1627
               lat = phirot2phi (rlat, rlon, pollat, pollon, polgam)
1628
 
1629
            else
1630
 
1631
               if ( abs(pollat-90.).gt.eps) then
1632
                  lon = lmstolm(rlat,rlon,pollat,pollon)
1633
                  lat = phstoph(rlat,rlon,pollat,pollon)
1634
               else
1635
                  lon = rlon
1636
                  lat = rlat
1637
               endif
1638
 
1639
            endif
1640
 
1641
            traout(i,j,2) = lon
1642
            traout(i,j,3) = lat
1643
 
1644
         enddo
1645
      enddo
1646
 
1647
c	  Vector rotation - if requested
1648
      do n=1,nvector
1649
 
1650
        ind0 = 0
1651
        ind1 = 0
1652
      	do j=1,ntrace0
1653
	      if ( (vector(j).eq.n).and.(ind0.eq.0) ) ind0 = j + ncol
1654
	      if ( (vector(j).eq.n).and.(ind0.ne.0) ) ind1 = j + ncol
1655
	    enddo
1656
 
1657
        print*,' Rotation : ',trim(varsout(ind0)),'.',
1658
     >                    	  trim(varsout(ind1)),ind0,ind1
1659
 
1660
	    do i=1,ntra
1661
	       do j=1,ntim
1662
	    	  urot = traout(i,j,ind0)
1663
	    	  vrot = traout(i,j,ind1)
1664
	    	  rlon = traout(i,j,2   )
1665
	    	  rlat = traout(i,j,3   )
1666
	    	  call uvrot2uv (urot,vrot,rlat,rlon,pollat,pollon,u,v)
1667
              traout(i,j,ind0) = u
1668
              traout(i,j,ind1) = v
1669
            enddo
1670
         enddo
1671
 
1672
      enddo
1673
 
1674
 
1675
c     Write trajectories
1676
      call wopen_tra(fod,outfile,ntra,ntim,ncol+ntrace0,
1677
     >               reftime,varsout,outmode)
1678
      call write_tra(fod,traout ,ntra,ntim,ncol+ntrace0,outmode)
1679
      call close_tra(fod,outmode)
1680
 
1681
c     Write some status information, and end of program message
1682
      print*  
1683
      print*,'---- STATUS INFORMATION --------------------------------'
1684
      print*
1685
      print*,' ok'
1686
      print*
1687
      print*,'              *** END OF PROGRAM TRACE ***'
1688
      print*,'========================================================='
1689
 
1690
 
1691
      end 
1692
 
1693
 
1694
 
1695
c     ******************************************************************
1696
c     * SUBROUTINE SECTION                                             *
1697
c     ******************************************************************
1698
 
1699
c     ------------------------------------------------------------------
1700
c     Add a variable to the list if not yet included in this list
1701
c     ------------------------------------------------------------------
1702
 
1703
      subroutine add2list (varname,list,nlist)
1704
 
1705
      implicit none
1706
 
1707
c     Declaration of subroutine parameters
1708
      character*80    varname
1709
      character*80    list(200)
1710
      integer         nlist
1711
 
1712
c     Auxiliray variables
1713
      integer i,j
1714
      integer isok
1715
 
1716
c     Expand the list, if necessary
1717
      isok = 0
1718
      do i=1,nlist      
1719
         if ( list(i).eq.varname ) isok = 1
1720
      enddo
1721
      if ( isok.eq.0 ) then
1722
         nlist       = nlist + 1
1723
         list(nlist) = varname
1724
      endif
1725
 
1726
c     Check for too large number of fields
1727
      if ( nlist.ge.200) then
1728
         print*,' ERROR: too many additional fields for tracing ...'
1729
         stop
1730
      endif
1731
 
1732
      end
1733
 
1734
 
1735
c     ------------------------------------------------------------------
1736
c     Get the index of a variable in the list
1737
c     ------------------------------------------------------------------
1738
 
1739
      subroutine list2ind (ind,varname,list,fok,nlist)
1740
 
1741
      implicit none
1742
 
1743
c     Declaration of subroutine parameters
1744
      integer         ind
1745
      character*80    varname
1746
      character*80    list(200)
1747
      integer         fok(200)
1748
      integer         nlist
1749
 
1750
c     Auxiliray variables
1751
      integer i,j
1752
      integer isok   
1753
 
1754
c     Get the index - error message if not found
1755
      ind = 0
1756
      do i=1,nlist
1757
         if ( varname.eq.list(i) ) then
1758
            ind = i
1759
            goto 100
1760
         endif
1761
      enddo
1762
 
1763
      if ( ind.eq.0) then
1764
         print*
1765
         print*,' ERROR: cannot find ',trim(varname),' in list ...'
1766
         do i=1,nlist
1767
            print*,i,trim(list(i))
1768
         enddo
1769
         print*
1770
         stop
1771
      endif
1772
 
1773
c     Exit point
1774
 100  continue
1775
 
1776
c     Check whether the field/column is ready
1777
      if ( fok(ind).eq.0 ) then
1778
         print*
1779
         print*,' ERROR: unresolved dependence : ',trim(list(ind))
1780
         print*
1781
         stop
1782
      endif
1783
 
1784
      end
1785
 
1786
 
1787
c     ------------------------------------------------------------------
1788
c     Split the variable name into name, shift and direction 
1789
c     ------------------------------------------------------------------
1790
 
1791
      subroutine splitvar (tvar,shift_val,shift_dir)
1792
 
1793
      implicit none
1794
 
1795
c     Declaration of subroutine parameters
1796
      character*80 tvar
1797
      real         shift_val
1798
      character*80 shift_dir
1799
 
1800
c     Auxiliary variables
1801
      integer      i,j
1802
      integer      icolon,inumber
1803
      character*80 name
1804
      character    ch
1805
 
1806
c     Save variable name
1807
      name = tvar
1808
 
1809
c     Search for colon
1810
      icolon=0
1811
      do i=1,80
1812
         if ( (name(i:i).eq.':').and.(icolon.ne.0) ) goto 100
1813
         if ( (name(i:i).eq.':').and.(icolon.eq.0) ) icolon=i
1814
      enddo
1815
 
1816
c     If there is a colon, split the variable name
1817
      if ( icolon.ne.0 ) then
1818
 
1819
         tvar = name(1:(icolon-1))
1820
 
1821
         do i=icolon+1,80
1822
            ch = name(i:i)
1823
            if ( ( ch.ne.'0' ).and. ( ch.ne.'1' ).and.( ch.ne.'2' ).and.
1824
     >           ( ch.ne.'3' ).and. ( ch.ne.'4' ).and.( ch.ne.'5' ).and.
1825
     >           ( ch.ne.'6' ).and. ( ch.ne.'7' ).and.( ch.ne.'8' ).and.
1826
     >           ( ch.ne.'9' ).and. ( ch.ne.'+' ).and.( ch.ne.'-' ).and.
1827
     >           ( ch.ne.'.' ).and. ( ch.ne.' ' )  )
1828
     >      then
1829
               inumber = i
1830
               exit
1831
            endif
1832
         enddo
1833
 
1834
         read(name( (icolon+1):(inumber-1) ),*) shift_val
1835
 
1836
         shift_dir = name(inumber:80)
1837
 
1838
      else
1839
 
1840
         shift_dir = 'nil'
1841
         shift_val = 0.
1842
 
1843
      endif
1844
 
1845
      return
1846
 
1847
c     Error handling
1848
 100  continue
1849
 
1850
      print*,' ERROR: cannot split variable name ',trim(tvar)
1851
      stop
1852
 
1853
      end
1854
 
1855
 
1856
 
1857
 
1858
 
1859
 
1860
 
1861
 
1862