Subversion Repositories lagranto.ecmwf

Rev

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

Rev 15 Rev 39
1
PROGRAM trace
1
PROGRAM trace
2
 
2
 
3
  ! ********************************************************************
3
  ! ********************************************************************
4
  ! *                                                                  *
4
  ! *                                                                  *
5
  ! * Trace fields along trajectories                                  *
5
  ! * Trace fields along trajectories                                  *
6
  ! *                                                                  *
6
  ! *                                                                  *
7
  ! * April 1993: First version (Heini Wernli)                         *
7
  ! * April 1993: First version (Heini Wernli)                         *
8
  ! * 2008-2009 : Major upgrades (Michael Sprenger)                    *
8
  ! * 2008-2009 : Major upgrades (Michael Sprenger)                    *
9
  ! * Mar 2012: Clustering option (Bojan Skerlak)                      *  
9
  ! * Mar 2012: Clustering option (Bojan Skerlak)                      *  
10
  ! * Nov 2012: Circle options (")                                     *
10
  ! * Nov 2012: Circle options (")                                     *
11
  ! * Jul 2013: user-defined PV,TH @ clustering mode (")               *
11
  ! * Jul 2013: user-defined PV,TH @ clustering mode (")               *
12
  ! *                                                                  *
12
  ! *                                                                  *
13
  ! ********************************************************************
13
  ! ********************************************************************
14
 
14
 
15
  implicit none
15
  implicit none
16
 
16
 
17
  ! --------------------------------------------------------------------
17
  ! --------------------------------------------------------------------
18
  ! Declaration of parameters
18
  ! Declaration of parameters
19
  ! --------------------------------------------------------------------
19
  ! --------------------------------------------------------------------
20
 
20
 
21
  ! Maximum number of levels for input files
21
  ! Maximum number of levels for input files
22
  integer :: nlevmax
22
  integer :: nlevmax
23
  parameter (nlevmax=100)
23
  parameter (nlevmax=100)
24
 
24
 
25
  ! Maximum number of input files (dates, length of trajectories)
25
  ! Maximum number of input files (dates, length of trajectories)
26
  integer :: ndatmax
26
  integer :: ndatmax
27
  parameter (ndatmax=500)
27
  parameter (ndatmax=500)
28
 
28
 
29
  ! Numerical epsilon (for float comparison)
29
  ! Numerical epsilon (for float comparison)
30
  real :: eps
30
  real :: eps
31
  parameter (eps=0.001)
31
  parameter (eps=0.001)
32
 
32
 
33
  ! Conversion factors
33
  ! Conversion factors
34
  real :: pi180                                   ! deg -> rad
34
  real :: pi180                                   ! deg -> rad
35
  parameter (pi180=3.14159/180.)
35
  parameter (pi180=3.14159/180.)
36
  real :: deg2km                                  ! deg -> km (at equator)
36
  real :: deg2km                                  ! deg -> km (at equator)
37
  parameter (deg2km=111.)
37
  parameter (deg2km=111.)
38
  real :: pir
38
  real :: pir
39
  parameter (pir=255032235.95489)                 ! 2*Pi*R^2 Bojan
39
  parameter (pir=255032235.95489)                 ! 2*Pi*R^2 Bojan
40
 
40
 
41
  ! Prefix for primary and secondary fields
41
  ! Prefix for primary and secondary fields
42
  character :: charp
42
  character :: charp
43
  character :: chars
43
  character :: chars
44
  parameter (charp='P')
44
  parameter (charp='P')
45
  parameter (chars='S')
45
  parameter (chars='S')
46
 
46
 
47
  ! --------------------------------------------------------------------
47
  ! --------------------------------------------------------------------
48
  ! Declaration of variables
48
  ! Declaration of variables
49
  ! --------------------------------------------------------------------
49
  ! --------------------------------------------------------------------
50
 
50
 
51
  ! Input and output format for trajectories (see iotra.f)
51
  ! Input and output format for trajectories (see iotra.f)
52
  integer :: inpmode
52
  integer :: inpmode
53
  integer :: outmode
53
  integer :: outmode
54
 
54
 
55
  ! Input parameters
55
  ! Input parameters
56
  character(len=80) :: inpfile         ! Input trajectory file
56
  character(len=80) :: inpfile         ! Input trajectory file
57
  character(len=80) :: outfile         ! Output trajectory file
57
  character(len=80) :: outfile         ! Output trajectory file
58
  integer :: ntra                      ! Number of trajectories
58
  integer :: ntra                      ! Number of trajectories
59
  integer :: ncol                      ! Number of columns (including time, lon, lat, p)
59
  integer :: ncol                      ! Number of columns (including time, lon, lat, p)
60
  integer :: ntim                      ! Number of times per trajectory
60
  integer :: ntim                      ! Number of times per trajectory
61
  integer :: ntrace0                   ! Number of trace variables
61
  integer :: ntrace0                   ! Number of trace variables
62
  character(len=80) :: tvar0(200)      ! Tracing variable (with mode specification)
62
  character(len=80) :: tvar0(200)      ! Tracing variable (with mode specification)
63
  character(len=80) :: tvar(200)       ! Tracing variable name (only the variable)
63
  character(len=80) :: tvar(200)       ! Tracing variable name (only the variable)
64
  character(len=80) :: tfil(200)       ! Filename prefix
64
  character(len=80) :: tfil(200)       ! Filename prefix
65
  real :: fac(200)                     ! Scaling factor
65
  real :: fac(200)                     ! Scaling factor
66
  real :: shift_val(200)               ! Shift in space and time relative to trajectory position
66
  real :: shift_val(200)               ! Shift in space and time relative to trajectory position
67
  character(len=80) :: shift_dir(200)  ! Direction of shift
67
  character(len=80) :: shift_dir(200)  ! Direction of shift
-
 
68
  character(len=80) :: shift_rel(200)  ! Operator/relator for variable
68
  integer :: compfl(200)               ! Computation flag (1=compute)
69
  integer :: compfl(200)               ! Computation flag (1=compute)
69
  integer :: numdat                    ! Number of input files
70
  integer :: numdat                    ! Number of input files
70
  character(len=11) :: dat(ndatmax)    ! Dates of input files
71
  character(len=11) :: dat(ndatmax)    ! Dates of input files
71
  real :: timeinc                      ! Time increment between input files
72
  real :: timeinc                      ! Time increment between input files
72
  real :: tst                          ! Time shift of start relative to first data file
73
  real :: tst                          ! Time shift of start relative to first data file
73
  real :: ten                          ! Time shift of end relatiev to first data file
74
  real :: ten                          ! Time shift of end relatiev to first data file
74
  character(len=20) :: startdate       ! First time/date on trajectory
75
  character(len=20) :: startdate       ! First time/date on trajectory
75
  character(len=20) :: enddate         ! Last time/date on trajectory
76
  character(len=20) :: enddate         ! Last time/date on trajectory
76
  integer :: ntrace1                   ! Count trace and additional variables
77
  integer :: ntrace1                   ! Count trace and additional variables
77
  character(len=80) :: timecheck       ! Either 'yes' or 'no'
78
  character(len=80) :: timecheck       ! Either 'yes' or 'no'
78
  character(len=80) :: intmode         ! Interpolation mode ('normal', 'nearest') Bojan ('clustering','circle_avg','circle_max','circle_min')
79
  character(len=80) :: intmode         ! Interpolation mode ('normal', 'nearest') Bojan ('clustering','circle_avg','circle_max','circle_min')
79
 
80
 
80
  ! Trajectories
81
  ! Trajectories
81
  real,allocatable, dimension (:,:,:) :: trainp          ! Input trajectories (ntra,ntim,ncol)
82
  real,allocatable, dimension (:,:,:) :: trainp          ! Input trajectories (ntra,ntim,ncol)
82
  real,allocatable, dimension (:,:,:) :: traint          ! Internal trajectories (ntra,ntim,ncol+ntrace1)
83
  real,allocatable, dimension (:,:,:) :: traint          ! Internal trajectories (ntra,ntim,ncol+ntrace1)
83
  real,allocatable, dimension (:,:,:) :: traout          ! Output trajectories (ntra,ntim,ncol+ntrace0)
84
  real,allocatable, dimension (:,:,:) :: traout          ! Output trajectories (ntra,ntim,ncol+ntrace0)
84
  integer :: reftime(6)                                  ! Reference date
85
  integer :: reftime(6)                                  ! Reference date
85
  character(len=80) :: varsinp(200)                      ! Field names for input trajectory
86
  character(len=80) :: varsinp(200)                      ! Field names for input trajectory
86
  character(len=80) :: varsint(200)                      ! Field names for internal trajectory
87
  character(len=80) :: varsint(200)                      ! Field names for internal trajectory
87
  character(len=80) :: varsout(200)                      ! Field names for output trajectory
88
  character(len=80) :: varsout(200)                      ! Field names for output trajectory
88
  integer :: fid,fod                                     ! File identifier for inp and out trajectories
89
  integer :: fid,fod                                     ! File identifier for inp and out trajectories
89
  real :: x0,y0,p0                                       ! Position of air parcel (physical space)
90
  real :: x0,y0,p0                                       ! Position of air parcel (physical space)
90
  real :: reltpos0                                       ! Relative time of air parcel
91
  real :: reltpos0                                       ! Relative time of air parcel
91
  real :: xind,yind,pind                                 ! Position of air parcel (grid space)
92
  real :: xind,yind,pind                                 ! Position of air parcel (grid space)
92
  integer :: fbflag                                      ! Flag for forward (1) or backward (-1) trajectories
93
  integer :: fbflag                                      ! Flag for forward (1) or backward (-1) trajectories
93
  integer :: fok(200)                                    ! Flag whether field is ready
94
  integer :: fok(200)                                    ! Flag whether field is ready
94
 
95
 
95
  ! Meteorological fields
96
  ! Meteorological fields
96
  real,allocatable, dimension (:)     :: spt0,spt1       ! Surface pressure
97
  real,allocatable, dimension (:)     :: spt0,spt1       ! Surface pressure
97
  real,allocatable, dimension (:)     :: p3t0,p3t1       ! 3d-pressure
98
  real,allocatable, dimension (:)     :: p3t0,p3t1       ! 3d-pressure
98
  real,allocatable, dimension (:)     :: f3t0,f3t1       ! 3d field for tracing
99
  real,allocatable, dimension (:)     :: f3t0,f3t1       ! 3d field for tracing
99
  character(len=80) :: svars(100)                        ! List of variables on S file
100
  character(len=80) :: svars(100)                        ! List of variables on S file
100
  character(len=80) :: pvars(100)                        ! List of variables on P file
101
  character(len=80) :: pvars(100)                        ! List of variables on P file
101
  integer :: n_svars                                     ! Number of variables on S file
102
  integer :: n_svars                                     ! Number of variables on S file
102
  integer :: n_pvars                                     ! Number of variables on P file
103
  integer :: n_pvars                                     ! Number of variables on P file
103
 
104
 
104
  ! Grid description
105
  ! Grid description
105
  real :: pollon,pollat   ! Longitude/latitude of pole
106
  real :: pollon,pollat   ! Longitude/latitude of pole
106
  real :: ak(100)         ! Vertical layers and levels
107
  real :: ak(100)         ! Vertical layers and levels
107
  real :: bk(100)
108
  real :: bk(100)
108
  real :: xmin,xmax       ! Zonal grid extension
109
  real :: xmin,xmax       ! Zonal grid extension
109
  real :: ymin,ymax       ! Meridional grid extension
110
  real :: ymin,ymax       ! Meridional grid extension
110
  integer :: nx,ny,nz     ! Grid dimensions
111
  integer :: nx,ny,nz     ! Grid dimensions
111
  real :: dx,dy           ! Horizontal grid resolution
112
  real :: dx,dy           ! Horizontal grid resolution
112
  integer :: hem          ! Flag for hemispheric domain
113
  integer :: hem          ! Flag for hemispheric domain
113
  integer :: per          ! Flag for periodic domain
114
  integer :: per          ! Flag for periodic domain
114
  real :: stagz           ! Vertical staggering
115
  real :: stagz           ! Vertical staggering
115
  real :: mdv             ! Missing data value
116
  real :: mdv             ! Missing data value
116
 
117
 
117
  ! Auxiliary variables
118
  ! Auxiliary variables
118
  integer :: i,j,k,l,m,n
119
  integer :: i,j,k,l,m,n
119
  real :: rd
120
  real :: rd
120
  character(len=80) :: filename,varname
121
  character(len=80) :: filename,varname
121
  real :: time0,time1,reltpos
122
  real :: time0,time1,reltpos
122
  integer :: itime0,itime1
123
  integer :: itime0,itime1
123
  integer :: stat
124
  integer :: stat
124
  real :: tstart
125
  real :: tstart
125
  integer :: iloaded0,iloaded1
126
  integer :: iloaded0,iloaded1
126
  real :: f0
127
  real :: f0
127
  real :: frac
128
  real :: frac
128
  real :: tload,tfrac
129
  real :: tload,tfrac
129
  integer :: isok
130
  integer :: isok
130
  character :: ch
131
  character :: ch
131
  integer :: ind
132
  integer :: ind
132
  integer :: ind1,ind2,ind3,ind4,ind5
133
  integer :: ind1,ind2,ind3,ind4,ind5
133
  integer :: ind6,ind7,ind8,ind9,ind0
134
  integer :: ind6,ind7,ind8,ind9,ind0
134
  integer :: noutside
135
  integer :: noutside
135
  real :: delta
136
  real :: delta
136
  integer :: itrace0
137
  integer :: itrace0
137
  character(len=80) :: string
138
  character(len=80) :: string
138
  integer err_c1,err_c2,err_c3
139
  integer err_c1,err_c2,err_c3
139
 
140
 
140
  ! Bojan
141
  ! Bojan
141
  real    :: dist,circlesum,circlemax,circlemin,circleavg,radius       ! distance (great circle), sum/max/min/avg in circle, radius of circle
142
  real    :: dist,circlesum,circlemax,circlemin,circleavg,radius       ! distance (great circle), sum/max/min/avg in circle, radius of circle
142
  integer :: ist,jst,kst,sp,ml,mr,nd,nu                                ! ijk in stack, sp=stack counter, ml (left), mr (right), nd (down), nu (up)
143
  integer :: ist,jst,kst,sp,ml,mr,nd,nu                                ! ijk in stack, sp=stack counter, ml (left), mr (right), nd (down), nu (up)
143
  integer :: lci,lcj,xindb,xindf                                       ! label count i and j, xind back, xind forward
144
  integer :: lci,lcj,xindb,xindf                                       ! label count i and j, xind back, xind forward
144
  integer :: yindb,yindf,pindb,pindf                                   ! yind back, yind forward, pind back, pind forward
145
  integer :: yindb,yindf,pindb,pindf                                   ! yind back, yind forward, pind back, pind forward
145
  integer :: pvpos,thpos                                               ! position of variables PV and TH in trajectory
146
  integer :: pvpos,thpos                                               ! position of variables PV and TH in trajectory
146
  real    :: tropo_pv,tropo_th                                         ! values of PV and TH at dynamical tropopause
147
  real    :: tropo_pv,tropo_th                                         ! values of PV and TH at dynamical tropopause
147
  integer, allocatable, dimension (:)   :: stackx,stacky               ! lon/lat of stack
148
  integer, allocatable, dimension (:)   :: stackx,stacky               ! lon/lat of stack
148
  integer, allocatable, dimension (:)   :: lblcount                    ! counter for label
149
  integer, allocatable, dimension (:)   :: lblcount                    ! counter for label
149
  integer, allocatable, dimension (:,:) :: connect                     ! array that keeps track of the visited grid points
150
  integer, allocatable, dimension (:,:) :: connect                     ! array that keeps track of the visited grid points
150
  real, allocatable, dimension (:)      :: lbl                         ! label
151
  real, allocatable, dimension (:)      :: lbl                         ! label
151
  real, allocatable, dimension (:)      :: circlelon,circlelat         ! value of f, lon and lat in circle
152
  real, allocatable, dimension (:)      :: circlelon,circlelat         ! value of f, lon and lat in circle
152
  real, allocatable, dimension (:)      :: circlef,circlearea          ! value of f, lon and lat in circle
153
  real, allocatable, dimension (:)      :: circlef,circlearea          ! value of f, lon and lat in circle
153
  real, allocatable, dimension (:)      :: longrid,latgrid             ! arrays of longitude and latitude of grid points
154
  real, allocatable, dimension (:)      :: longrid,latgrid             ! arrays of longitude and latitude of grid points
154
  ! Bojan
155
  ! Bojan
155
 
156
 
156
  ! Externals
157
  ! Externals
157
  real :: int_index4
158
  real :: int_index4
158
  external                               int_index4
159
  external                               int_index4
159
  real :: sdis ! Bojan: need function sdis (calculates great circle distance)
160
  real :: sdis ! Bojan: need function sdis (calculates great circle distance)
160
  external                               sdis ! Bojan: need function sdis
161
  external                               sdis ! Bojan: need function sdis
161
 
162
 
162
  ! --------------------------------------------------------------------
163
  ! --------------------------------------------------------------------
163
  ! Start of program, Read parameters, get grid parameters
164
  ! Start of program, Read parameters, get grid parameters
164
  ! --------------------------------------------------------------------
165
  ! --------------------------------------------------------------------
165
 
166
 
166
  ! Write start message
167
  ! Write start message
167
  print*,'========================================================='
168
  print*,'========================================================='
168
  print*,'              *** START OF PROGRAM TRACE ***'
169
  print*,'              *** START OF PROGRAM TRACE ***'
169
  print*
170
  print*
170
 
171
 
171
  ! Read parameters
172
  ! Read parameters
172
  open(10,file='trace.param')
173
  open(10,file='trace.param')
173
   read(10,*) inpfile
174
   read(10,*) inpfile
174
   read(10,*) outfile
175
   read(10,*) outfile
175
   read(10,*) startdate
176
   read(10,*) startdate
176
   read(10,*) enddate
177
   read(10,*) enddate
177
   read(10,*) fbflag
178
   read(10,*) fbflag
178
   read(10,*) numdat
179
   read(10,*) numdat
179
   if ( fbflag.eq.1) then
180
   if ( fbflag.eq.1) then
180
      do i=1,numdat
181
      do i=1,numdat
181
         read(10,'(a11)') dat(i)
182
         read(10,'(a11)') dat(i)
182
      enddo
183
      enddo
183
   else
184
   else
184
      do i=numdat,1,-1
185
      do i=numdat,1,-1
185
         read(10,'(a11)') dat(i)
186
         read(10,'(a11)') dat(i)
186
      enddo
187
      enddo
187
   endif
188
   endif
188
   read(10,*) timeinc
189
   read(10,*) timeinc
189
   read(10,*) tst
190
   read(10,*) tst
190
   read(10,*) ten
191
   read(10,*) ten
191
   read(10,*) ntra
192
   read(10,*) ntra
192
   read(10,*) ntim
193
   read(10,*) ntim
193
   read(10,*) ncol
194
   read(10,*) ncol
194
   read(10,*) ntrace0
195
   read(10,*) ntrace0
195
   do i=1,ntrace0
196
   do i=1,ntrace0
196
      read(10,*) tvar(i), fac(i), compfl(i), tfil(i)
197
      read(10,*) tvar(i), fac(i), compfl(i), tfil(i)
197
   enddo
198
   enddo
198
   read(10,*) n_pvars
199
   read(10,*) n_pvars
199
   do i=1,n_pvars
200
   do i=1,n_pvars
200
      read(10,*) pvars(i)
201
      read(10,*) pvars(i)
201
   enddo
202
   enddo
202
   read(10,*) n_svars
203
   read(10,*) n_svars
203
   do i=1,n_svars
204
   do i=1,n_svars
204
      read(10,*) svars(i)
205
      read(10,*) svars(i)
205
   enddo
206
   enddo
206
   read(10,*) timecheck
207
   read(10,*) timecheck
207
   read(10,*) intmode
208
   read(10,*) intmode
208
   read(10,*) radius
209
   read(10,*) radius
209
   read(10,*) tropo_pv
210
   read(10,*) tropo_pv
210
   read(10,*) tropo_th
211
   read(10,*) tropo_th
211
  close(10)
212
  close(10)
212
 
213
 
213
  ! Bojan: error if radius < 0
214
  ! Bojan: error if radius < 0
214
  if (((intmode .eq. "circle_avg") .or. (intmode .eq. "circle_min") .or. (intmode .eq. "circle_max")) .and. (radius .lt. 0)) then
215
  if (((intmode .eq. "circle_avg") .or. (intmode .eq. "circle_min") .or. (intmode .eq. "circle_max")) .and. (radius .lt. 0)) then
215
     print*,'ERROR (circle): radius < 0!'
216
     print*,'ERROR (circle): radius < 0!'
216
     stop
217
     stop
217
  endif
218
  endif
218
 
219
 
219
  ! Remove commented tracing fields
220
  ! Remove commented tracing fields
220
  itrace0 = 1
221
  itrace0 = 1
221
  do while ( itrace0.le.ntrace0)
222
  do while ( itrace0.le.ntrace0)
222
     string = tvar(itrace0)
223
     string = tvar(itrace0)
223
     if ( string(1:1).eq.'#' ) then
224
     if ( string(1:1).eq.'#' ) then
224
        do i=itrace0,ntrace0-1
225
        do i=itrace0,ntrace0-1
225
           tvar(i)   = tvar(i+1)
226
           tvar(i)   = tvar(i+1)
226
           fac(i)    = fac(i+1)
227
           fac(i)    = fac(i+1)
227
           compfl(i) = compfl(i+1)
228
           compfl(i) = compfl(i+1)
228
           tfil(i)   = tfil(i+1)
229
           tfil(i)   = tfil(i+1)
229
        enddo
230
        enddo
230
        ntrace0 = ntrace0 - 1
231
        ntrace0 = ntrace0 - 1
231
     else
232
     else
232
        itrace0 = itrace0 + 1
233
        itrace0 = itrace0 + 1
233
     endif
234
     endif
234
  enddo
235
  enddo
235
 
236
 
236
  ! Save the tracing variable (including all mode specifications)
237
  ! Save the tracing variable (including all mode specifications)
237
  do i=1,ntrace0
238
  do i=1,ntrace0
238
     tvar0(i) = tvar(i)
239
     tvar0(i) = tvar(i)
239
  enddo
240
  enddo
240
 
241
 
241
  ! Set the formats of the input and output files
242
  ! Set the formats of the input and output files
242
  call mode_tra(inpmode,inpfile)
243
  call mode_tra(inpmode,inpfile)
243
  if (inpmode.eq.-1) inpmode=1
244
  if (inpmode.eq.-1) inpmode=1
244
  call mode_tra(outmode,outfile)
245
  call mode_tra(outmode,outfile)
245
  if (outmode.eq.-1) outmode=1
246
  if (outmode.eq.-1) outmode=1
246
 
247
 
247
  ! Convert time shifts <tst,ten> from <hh.mm> into fractional time
248
  ! Convert time shifts <tst,ten> from <hh.mm> into fractional time
248
  call hhmm2frac(tst,frac)
249
  call hhmm2frac(tst,frac)
249
  tst = frac
250
  tst = frac
250
  call hhmm2frac(ten,frac)
251
  call hhmm2frac(ten,frac)
251
  ten = frac
252
  ten = frac
252
 
253
 
253
  ! Set the time for the first data file (depending on forward/backward mode)
254
  ! Set the time for the first data file (depending on forward/backward mode)
254
  if (fbflag.eq.1) then
255
  if (fbflag.eq.1) then
255
    tstart = -tst
256
    tstart = -tst
256
  else
257
  else
257
    tstart = tst
258
    tstart = tst
258
  endif
259
  endif
259
 
260
 
260
  ! Read the constant grid parameters (nx,ny,nz,xmin,xmax,ymin,ymax,pollon,pollat)
261
  ! Read the constant grid parameters (nx,ny,nz,xmin,xmax,ymin,ymax,pollon,pollat)
261
  ! The negative <-fid> of the file identifier is used as a flag for parameter retrieval
262
  ! The negative <-fid> of the file identifier is used as a flag for parameter retrieval
262
  filename = charp//dat(1)
263
  filename = charp//dat(1)
263
  varname  = 'U'
264
  varname  = 'U'
264
  call input_open (fid,filename)
265
  call input_open (fid,filename)
265
  call input_grid (-fid,varname,xmin,xmax,ymin,ymax,dx,dy,nx,ny,tstart,pollon,pollat,rd,rd,nz,rd,rd,rd,timecheck)
266
  call input_grid (-fid,varname,xmin,xmax,ymin,ymax,dx,dy,nx,ny,tstart,pollon,pollat,rd,rd,nz,rd,rd,rd,timecheck)
266
  call input_close(fid)
267
  call input_close(fid)
267
 
268
 
268
  ! Allocate memory for some meteorological arrays
269
  ! Allocate memory for some meteorological arrays
269
  allocate(spt0(nx*ny),stat=stat)
270
  allocate(spt0(nx*ny),stat=stat)
270
  if (stat.ne.0) print*,'*** error allocating array spt0 ***'   ! Surface pressure
271
  if (stat.ne.0) print*,'*** error allocating array spt0 ***'   ! Surface pressure
271
  allocate(spt1(nx*ny),stat=stat)
272
  allocate(spt1(nx*ny),stat=stat)
272
  if (stat.ne.0) print*,'*** error allocating array spt1 ***'
273
  if (stat.ne.0) print*,'*** error allocating array spt1 ***'
273
  allocate(p3t0(nx*ny*nz),stat=stat)
274
  allocate(p3t0(nx*ny*nz),stat=stat)
274
  if (stat.ne.0) print*,'*** error allocating array p3t0 ***'   ! Pressure
275
  if (stat.ne.0) print*,'*** error allocating array p3t0 ***'   ! Pressure
275
  allocate(p3t1(nx*ny*nz),stat=stat)
276
  allocate(p3t1(nx*ny*nz),stat=stat)
276
  if (stat.ne.0) print*,'*** error allocating array p3t1 ***'
277
  if (stat.ne.0) print*,'*** error allocating array p3t1 ***'
277
  allocate(f3t0(nx*ny*nz),stat=stat)
278
  allocate(f3t0(nx*ny*nz),stat=stat)
278
  if (stat.ne.0) print*,'*** error allocating array p3t0 ***'   ! Tracing field
279
  if (stat.ne.0) print*,'*** error allocating array p3t0 ***'   ! Tracing field
279
  allocate(f3t1(nx*ny*nz),stat=stat)
280
  allocate(f3t1(nx*ny*nz),stat=stat)
280
  if (stat.ne.0) print*,'*** error allocating array p3t1 ***'
281
  if (stat.ne.0) print*,'*** error allocating array p3t1 ***'
281
 
282
 
282
  ! Get memory for trajectory arrays
283
  ! Get memory for trajectory arrays
283
  allocate(trainp(ntra,ntim,ncol),stat=stat)
284
  allocate(trainp(ntra,ntim,ncol),stat=stat)
284
  if (stat.ne.0) print*,'*** error allocating array tra      ***'
285
  if (stat.ne.0) print*,'*** error allocating array tra      ***'
285
 
286
 
286
  ! Bojan
287
  ! Bojan
287
  ! allocate memory for clustering mode 
288
  ! allocate memory for clustering mode 
288
  if (intmode .eq. 'clustering') then
289
  if (intmode .eq. 'clustering') then
289
     allocate(lbl(8),stat=stat)
290
     allocate(lbl(8),stat=stat)
290
     if (stat.ne.0) print*,'*** error allocating array lbl      ***'
291
     if (stat.ne.0) print*,'*** error allocating array lbl      ***'
291
     allocate(lblcount(5),stat=stat)
292
     allocate(lblcount(5),stat=stat)
292
     if (stat.ne.0) print*,'*** error allocating array lblcount ***'
293
     if (stat.ne.0) print*,'*** error allocating array lblcount ***'
293
  endif
294
  endif
294
  ! allocate memory for circle mode
295
  ! allocate memory for circle mode
295
  if ( (intmode.eq.'circle_avg') .or. (intmode.eq.'circle_min') .or. (intmode.eq.'circle_max') ) then
296
  if ( (intmode.eq.'circle_avg') .or. (intmode.eq.'circle_min') .or. (intmode.eq.'circle_max') ) then
296
     allocate(connect(nx,ny),stat=stat)
297
     allocate(connect(nx,ny),stat=stat)
297
     if (stat.ne.0) print*,'*** error allocating connect ***'
298
     if (stat.ne.0) print*,'*** error allocating connect ***'
298
     allocate(stackx(nx*ny),stat=stat)
299
     allocate(stackx(nx*ny),stat=stat)
299
     if (stat.ne.0) print*,'*** error allocating stackx ***'
300
     if (stat.ne.0) print*,'*** error allocating stackx ***'
300
     allocate(stacky(nx*ny),stat=stat)
301
     allocate(stacky(nx*ny),stat=stat)
301
     if (stat.ne.0) print*,'*** error allocating stacky ***'
302
     if (stat.ne.0) print*,'*** error allocating stacky ***'
302
     allocate(circlelon(nx*ny),stat=stat)
303
     allocate(circlelon(nx*ny),stat=stat)
303
     if (stat.ne.0) print*,'*** error allocating circlelon ***'
304
     if (stat.ne.0) print*,'*** error allocating circlelon ***'
304
     allocate(circlelat(nx*ny),stat=stat)
305
     allocate(circlelat(nx*ny),stat=stat)
305
     if (stat.ne.0) print*,'*** error allocating circlelat ***'
306
     if (stat.ne.0) print*,'*** error allocating circlelat ***'
306
     allocate(circlef(nx*ny),stat=stat)
307
     allocate(circlef(nx*ny),stat=stat)
307
     if (stat.ne.0) print*,'*** error allocating circlef ***'
308
     if (stat.ne.0) print*,'*** error allocating circlef ***'
308
     allocate(circlearea(nx*ny),stat=stat)
309
     allocate(circlearea(nx*ny),stat=stat)
309
     if (stat.ne.0) print*,'*** error allocating circlearea ***'
310
     if (stat.ne.0) print*,'*** error allocating circlearea ***'
310
     allocate(longrid(nx),stat=stat)
311
     allocate(longrid(nx),stat=stat)
311
     if (stat.ne.0) print*,'*** error allocating longrid ***'
312
     if (stat.ne.0) print*,'*** error allocating longrid ***'
312
     allocate(latgrid(ny),stat=stat)
313
     allocate(latgrid(ny),stat=stat)
313
     if (stat.ne.0) print*,'*** error allocating latgrid ***'
314
     if (stat.ne.0) print*,'*** error allocating latgrid ***'
314
     do m=1,nx
315
     do m=1,nx
315
        longrid(m)=xmin+dx*(m-1)
316
        longrid(m)=xmin+dx*(m-1)
316
     enddo
317
     enddo
317
     do n=1,ny
318
     do n=1,ny
318
        latgrid(n)=ymin+dy*(n-1)
319
        latgrid(n)=ymin+dy*(n-1)
319
     enddo
320
     enddo
320
  endif
321
  endif
321
  ! Bojan
322
  ! Bojan
322
 
323
 
323
  ! Set the flags for periodic domains
324
  ! Set the flags for periodic domains
324
  if ( abs(xmax-xmin-360.).lt.eps ) then
325
  if ( abs(xmax-xmin-360.).lt.eps ) then
325
     per = 1
326
     per = 1
326
  elseif ( abs(xmax-xmin-360.+dx).lt.eps ) then
327
  elseif ( abs(xmax-xmin-360.+dx).lt.eps ) then
327
     per = 2
328
     per = 2
328
  else
329
  else
329
     per = 0
330
     per = 0
330
  endif
331
  endif
331
 
332
 
332
  ! Set logical flag for periodic data set (hemispheric or not)
333
  ! Set logical flag for periodic data set (hemispheric or not)
333
  hem = 0
334
  hem = 0
334
  if (per.eq.0.) then
335
  if (per.eq.0.) then
335
     delta=xmax-xmin-360.
336
     delta=xmax-xmin-360.
336
     if (abs(delta+dx).lt.eps) then               ! Program aborts: arrays must be closed
337
     if (abs(delta+dx).lt.eps) then               ! Program aborts: arrays must be closed
337
        print*,' ERROR: arrays must be closed... Stop'
338
        print*,' ERROR: arrays must be closed... Stop'
338
     else if (abs(delta).lt.eps) then ! Periodic and hemispheric
339
     else if (abs(delta).lt.eps) then ! Periodic and hemispheric
339
       hem=1
340
       hem=1
340
       per=360.
341
       per=360.
341
    endif
342
    endif
342
  else                                            ! Periodic and hemispheric
343
  else                                            ! Periodic and hemispheric
343
     hem=1
344
     hem=1
344
  endif
345
  endif
345
 
346
 
346
  ! Write some status information
347
  ! Write some status information
347
  print*,'---- INPUT PARAMETERS -----------------------------------'
348
  print*,'---- INPUT PARAMETERS -----------------------------------'
348
  print*
349
  print*
349
  print*,'  Input trajectory file  : ',trim(inpfile)
350
  print*,'  Input trajectory file  : ',trim(inpfile)
350
  print*,'  Output trajectory file : ',trim(outfile)
351
  print*,'  Output trajectory file : ',trim(outfile)
351
  print*,'  Format of input file   : ',inpmode
352
  print*,'  Format of input file   : ',inpmode
352
  print*,'  Format of output file  : ',outmode
353
  print*,'  Format of output file  : ',outmode
353
  print*,'  Forward/backward       : ',fbflag
354
  print*,'  Forward/backward       : ',fbflag
354
  print*,'  #tra                   : ',ntra
355
  print*,'  #tra                   : ',ntra
355
  print*,'  #col                   : ',ncol
356
  print*,'  #col                   : ',ncol
356
  print*,'  #tim                   : ',ntim
357
  print*,'  #tim                   : ',ntim
357
  print*,'  No time check          : ',trim(timecheck)
358
  print*,'  No time check          : ',trim(timecheck)
358
  print*,'  Interpolation mode     : ',trim(intmode)
359
  print*,'  Interpolation mode     : ',trim(intmode)
359
  ! Bojan
360
  ! Bojan
360
  if (trim(intmode) .eq. "clustering") then
361
  if (trim(intmode) .eq. "clustering") then
361
     print*,'  Tropopause PV [pvu]    : ',tropo_pv
362
     print*,'  Tropopause PV [pvu]    : ',tropo_pv
362
     print*,'  Tropopause TH [K]      : ',tropo_th
363
     print*,'  Tropopause TH [K]      : ',tropo_th
363
  endif
364
  endif
364
  do i=1,ntrace0
365
  do i=1,ntrace0
365
     if (compfl(i).eq.0) then
366
     if (compfl(i).eq.0) then
366
        print*,'  Tracing field          : ', trim(tvar(i)), fac(i), ' 0 ', tfil(i)
367
        print*,'  Tracing field          : ', trim(tvar(i)), fac(i), ' 0 ', tfil(i)
367
     else
368
     else
368
        print*,'  Tracing field          : ', trim(tvar(i)), fac(i), '  1 ', tfil(i)
369
        print*,'  Tracing field          : ', trim(tvar(i)), fac(i), '  1 ', tfil(i)
369
     endif
370
     endif
370
  enddo
371
  enddo
371
  print*
372
  print*
372
  print*,'---- INPUT DATA FILES -----------------------------------'
373
  print*,'---- INPUT DATA FILES -----------------------------------'
373
  print*
374
  print*
374
  call frac2hhmm(tstart,tload)
375
  call frac2hhmm(tstart,tload)
375
  print*,'  Time of 1st data file  : ',tload
376
  print*,'  Time of 1st data file  : ',tload
376
  print*,'  #input files           : ',numdat
377
  print*,'  #input files           : ',numdat
377
  print*,'  time increment         : ',timeinc
378
  print*,'  time increment         : ',timeinc
378
  call frac2hhmm(tst,tload)
379
  call frac2hhmm(tst,tload)
379
  print*,'  Shift of start         : ',tload
380
  print*,'  Shift of start         : ',tload
380
  call frac2hhmm(ten,tload)
381
  call frac2hhmm(ten,tload)
381
  print*,'  Shift of end           : ',tload
382
  print*,'  Shift of end           : ',tload
382
  print*,'  First/last input file  : ',trim(dat(1)), ' ... ', trim(dat(numdat))
383
  print*,'  First/last input file  : ',trim(dat(1)), ' ... ', trim(dat(numdat))
383
  print*,'  Primary variables      : ',trim(pvars(1))
384
  print*,'  Primary variables      : ',trim(pvars(1))
384
  do i=2,n_pvars
385
  do i=2,n_pvars
385
     print*,'                         : ',trim(pvars(i))
386
     print*,'                         : ',trim(pvars(i))
386
  enddo
387
  enddo
387
  if ( n_svars.ge.1 ) then
388
  if ( n_svars.ge.1 ) then
388
     print*,'  Secondary variables    : ',trim(svars(1))
389
     print*,'  Secondary variables    : ',trim(svars(1))
389
     do i=2,n_svars
390
     do i=2,n_svars
390
        print*,'                         : ',trim(svars(i))
391
        print*,'                         : ',trim(svars(i))
391
     enddo
392
     enddo
392
  endif
393
  endif
393
  print*
394
  print*
394
  print*,'---- CONSTANT GRID PARAMETERS ---------------------------'
395
  print*,'---- CONSTANT GRID PARAMETERS ---------------------------'
395
  print*
396
  print*
396
  print*,'  xmin,xmax     : ',xmin,xmax
397
  print*,'  xmin,xmax     : ',xmin,xmax
397
  print*,'  ymin,ymax     : ',ymin,ymax
398
  print*,'  ymin,ymax     : ',ymin,ymax
398
  print*,'  dx,dy         : ',dx,dy
399
  print*,'  dx,dy         : ',dx,dy
399
  print*,'  pollon,pollat : ',pollon,pollat
400
  print*,'  pollon,pollat : ',pollon,pollat
400
  print*,'  nx,ny,nz      : ',nx,ny,nz
401
  print*,'  nx,ny,nz      : ',nx,ny,nz
401
  print*,'  per, hem      : ',per,hem
402
  print*,'  per, hem      : ',per,hem
402
  print*
403
  print*
403
 
404
 
404
  ! --------------------------------------------------------------------
405
  ! --------------------------------------------------------------------
405
  ! Load the input trajectories
406
  ! Load the input trajectories
406
  ! --------------------------------------------------------------------
407
  ! --------------------------------------------------------------------
407
 
408
 
408
  ! Read the input trajectory file
409
  ! Read the input trajectory file
409
  call ropen_tra(fid,inpfile,ntra,ntim,ncol,reftime,varsinp,inpmode)
410
  call ropen_tra(fid,inpfile,ntra,ntim,ncol,reftime,varsinp,inpmode)
410
  call read_tra (fid,trainp,ntra,ntim,ncol,inpmode)
411
  call read_tra (fid,trainp,ntra,ntim,ncol,inpmode)
411
  call close_tra(fid,inpmode)
412
  call close_tra(fid,inpmode)
412
 
413
 
413
  ! Check that first four columns correspond to time,lon,lat,p
414
  ! Check that first four columns correspond to time,lon,lat,p
414
  if ( (varsinp(1).ne.'time' ).or. &
415
  if ( (varsinp(1).ne.'time' ).or. &
415
       (varsinp(2).ne.'xpos' ).and.(varsinp(2).ne.'lon' ).or. &
416
       (varsinp(2).ne.'xpos' ).and.(varsinp(2).ne.'lon' ).or. &
416
       (varsinp(3).ne.'ypos' ).and.(varsinp(3).ne.'lat' ).or. &
417
       (varsinp(3).ne.'ypos' ).and.(varsinp(3).ne.'lat' ).or. &
417
       (varsinp(4).ne.'ppos' ).and.(varsinp(4).ne.'p'   ) ) then
418
       (varsinp(4).ne.'ppos' ).and.(varsinp(4).ne.'p'   ) ) then
418
     print*,' ERROR: problem with input trajectories ...'
419
     print*,' ERROR: problem with input trajectories ...'
419
     stop
420
     stop
420
  endif
421
  endif
421
  varsinp(1) = 'time'
422
  varsinp(1) = 'time'
422
  varsinp(2) = 'lon'
423
  varsinp(2) = 'lon'
423
  varsinp(3) = 'lat'
424
  varsinp(3) = 'lat'
424
  varsinp(4) = 'p'
425
  varsinp(4) = 'p'
425
 
426
 
426
  ! Write some status information of the input trajectories
427
  ! Write some status information of the input trajectories
427
  print*,'---- INPUT TRAJECTORIES ---------------------------------'
428
  print*,'---- INPUT TRAJECTORIES ---------------------------------'
428
  print*
429
  print*
429
  print*,' Start date             : ',trim(startdate)
430
  print*,' Start date             : ',trim(startdate)
430
  print*,' End date               : ',trim(enddate)
431
  print*,' End date               : ',trim(enddate)
431
  print*,' Reference time (year)  : ',reftime(1)
432
  print*,' Reference time (year)  : ',reftime(1)
432
  print*,'                (month) : ',reftime(2)
433
  print*,'                (month) : ',reftime(2)
433
  print*,'                (day)   : ',reftime(3)
434
  print*,'                (day)   : ',reftime(3)
434
  print*,'                (hour)  : ',reftime(4)
435
  print*,'                (hour)  : ',reftime(4)
435
  print*,'                (min)   : ',reftime(5)
436
  print*,'                (min)   : ',reftime(5)
436
  print*,' Time range (min)       : ',reftime(6)
437
  print*,' Time range (min)       : ',reftime(6)
437
  do i=1,ncol
438
  do i=1,ncol
438
     print*,' Var                    :',i,trim(varsinp(i))
439
     print*,' Var                    :',i,trim(varsinp(i))
439
  enddo
440
  enddo
440
  print*
441
  print*
441
 
442
 
442
  ! Check that first time is 0 - otherwise the tracing will produce
443
  ! Check that first time is 0 - otherwise the tracing will produce
443
  ! wrong results because later in the code only absolute times are
444
  ! wrong results because later in the code only absolute times are
444
  ! considered: <itime0   = int(abs(tfrac-tstart)/timeinc) + 1>. This
445
  ! considered: <itime0   = int(abs(tfrac-tstart)/timeinc) + 1>. This
445
  ! will be changed in a future version.
446
  ! will be changed in a future version.
446
  if ( abs( trainp(1,1,1) ).gt.eps ) then
447
  if ( abs( trainp(1,1,1) ).gt.eps ) then
447
     print*,' ERROR: First time of trajectory must be 0, i.e. '
448
     print*,' ERROR: First time of trajectory must be 0, i.e. '
448
     print*,'     correspond to the reference date. Otherwise'
449
     print*,'     correspond to the reference date. Otherwise'
449
     print*,'     the tracing will give wrong results... STOP'
450
     print*,'     the tracing will give wrong results... STOP'
450
     stop
451
     stop
451
  endif
452
  endif
452
 
453
 
453
  ! --------------------------------------------------------------------
454
  ! --------------------------------------------------------------------
454
  ! Check dependencies for trace fields which must be calculated
455
  ! Check dependencies for trace fields which must be calculated
455
  ! --------------------------------------------------------------------
456
  ! --------------------------------------------------------------------
456
 
457
 
457
  ! Set the counter for extra fields
458
  ! Set the counter for extra fields
458
  ntrace1 = ntrace0
459
  ntrace1 = ntrace0
459
 
460
 
460
  ! Loop over all tracing variables
461
  ! Loop over all tracing variables
461
  i = 1
462
  i = 1
462
  do while (i.le.ntrace1)
463
  do while (i.le.ntrace1)
463
 
464
 
464
     ! Skip fields which must be available on the input files
465
     ! Skip fields which must be available on the input files
465
     if (i.le.ntrace0) then
466
     if (i.le.ntrace0) then
466
        if (compfl(i).eq.0) goto 100
467
        if (compfl(i).eq.0) goto 100
467
     endif
468
     endif
468
 
469
 
469
     ! Get the dependencies for potential temperature (TH)
470
     ! Get the dependencies for potential temperature (TH)
470
     if ( tvar(i).eq.'TH' ) then
471
     if ( tvar(i).eq.'TH' ) then
471
        varname='P'                                   ! P
472
        varname='P'                                   ! P
472
        call add2list(varname,tvar,ntrace1)
473
        call add2list(varname,tvar,ntrace1)
473
        varname='T'                                   ! T
474
        varname='T'                                   ! T
474
        call add2list(varname,tvar,ntrace1)
475
        call add2list(varname,tvar,ntrace1)
475
 
476
 
476
     ! Get the dependencies for potential temperature (TH)
477
     ! Get the dependencies for potential temperature (TH)
477
     elseif ( tvar(i).eq.'RHO' ) then
478
     elseif ( tvar(i).eq.'RHO' ) then
478
        varname='P'                                   ! P
479
        varname='P'                                   ! P
479
        call add2list(varname,tvar,ntrace1)
480
        call add2list(varname,tvar,ntrace1)
480
        varname='T'                                   ! T
481
        varname='T'                                   ! T
481
        call add2list(varname,tvar,ntrace1)
482
        call add2list(varname,tvar,ntrace1)
482
 
483
 
483
     ! Get the dependencies for relative humidity (RH)
484
     ! Get the dependencies for relative humidity (RH)
484
     elseif ( tvar(i).eq.'RH' ) then
485
     elseif ( tvar(i).eq.'RH' ) then
485
        varname='P'                                   ! P
486
        varname='P'                                   ! P
486
        call add2list(varname,tvar,ntrace1)
487
        call add2list(varname,tvar,ntrace1)
487
        varname='T'                                   ! T
488
        varname='T'                                   ! T
488
        call add2list(varname,tvar,ntrace1)
489
        call add2list(varname,tvar,ntrace1)
489
        varname='Q'                                   ! Q
490
        varname='Q'                                   ! Q
490
        call add2list(varname,tvar,ntrace1)
491
        call add2list(varname,tvar,ntrace1)
491
 
492
 
492
     ! Get the dependencies for equivalent potential temperature (THE)
493
     ! Get the dependencies for equivalent potential temperature (THE)
493
     elseif ( tvar(i).eq.'THE' ) then
494
     elseif ( tvar(i).eq.'THE' ) then
494
        varname='P'                                   ! P
495
        varname='P'                                   ! P
495
        call add2list(varname,tvar,ntrace1)
496
        call add2list(varname,tvar,ntrace1)
496
        varname='T'                                   ! T
497
        varname='T'                                   ! T
497
        call add2list(varname,tvar,ntrace1)
498
        call add2list(varname,tvar,ntrace1)
498
        varname='Q'                                   ! Q
499
        varname='Q'                                   ! Q
499
        call add2list(varname,tvar,ntrace1)
500
        call add2list(varname,tvar,ntrace1)
500
 
501
 
501
     ! Get the dependencies for latent heating rate (LHR)
502
     ! Get the dependencies for latent heating rate (LHR)
502
     elseif ( tvar(i).eq.'LHR' ) then
503
     elseif ( tvar(i).eq.'LHR' ) then
503
        varname='P'                                   ! P
504
        varname='P'                                   ! P
504
        call add2list(varname,tvar,ntrace1)
505
        call add2list(varname,tvar,ntrace1)
505
        varname='T'                                   ! T
506
        varname='T'                                   ! T
506
        call add2list(varname,tvar,ntrace1)
507
        call add2list(varname,tvar,ntrace1)
507
        varname='Q'                                   ! Q
508
        varname='Q'                                   ! Q
508
        call add2list(varname,tvar,ntrace1)
509
        call add2list(varname,tvar,ntrace1)
509
        varname='OMEGA'                               ! OMEGA
510
        varname='OMEGA'                               ! OMEGA
510
        call add2list(varname,tvar,ntrace1)
511
        call add2list(varname,tvar,ntrace1)
511
        varname='RH'                                  ! RH
512
        varname='RH'                                  ! RH
512
        call add2list(varname,tvar,ntrace1)
513
        call add2list(varname,tvar,ntrace1)
513
 
514
 
514
     ! Get the dependencies for wind speed (VEL)
515
     ! Get the dependencies for wind speed (VEL)
515
     elseif ( tvar(i).eq.'VEL' ) then
516
     elseif ( tvar(i).eq.'VEL' ) then
516
        varname='U'                                   ! U
517
        varname='U'                                   ! U
517
        call add2list(varname,tvar,ntrace1)
518
        call add2list(varname,tvar,ntrace1)
518
        varname='V'                                   ! V
519
        varname='V'                                   ! V
519
        call add2list(varname,tvar,ntrace1)
520
        call add2list(varname,tvar,ntrace1)
520
 
521
 
521
     ! Get the dependencies for wind direction (DIR)
522
     ! Get the dependencies for wind direction (DIR)
522
     elseif ( tvar(i).eq.'DIR' ) then
523
     elseif ( tvar(i).eq.'DIR' ) then
523
        varname='U'                                   ! U
524
        varname='U'                                   ! U
524
        call add2list(varname,tvar,ntrace1)
525
        call add2list(varname,tvar,ntrace1)
525
        varname='V'                                   ! V
526
        varname='V'                                   ! V
526
        call add2list(varname,tvar,ntrace1)
527
        call add2list(varname,tvar,ntrace1)
527
 
528
 
528
     ! Get the dependencies for du/dx (DUDX)
529
     ! Get the dependencies for du/dx (DUDX)
529
     elseif ( tvar(i).eq.'DUDX' ) then
530
     elseif ( tvar(i).eq.'DUDX' ) then
530
        varname='U:+1DLON'                            ! U:+1DLON
531
        varname='U:+1DLON'                            ! U:+1DLON
531
        call add2list(varname,tvar,ntrace1)
532
        call add2list(varname,tvar,ntrace1)
532
        varname='U:-1DLON'                            ! U:-1DLON
533
        varname='U:-1DLON'                            ! U:-1DLON
533
        call add2list(varname,tvar,ntrace1)
534
        call add2list(varname,tvar,ntrace1)
534
 
535
 
535
     ! Get the dependencies for dv(dx (DVDX)
536
     ! Get the dependencies for dv(dx (DVDX)
536
     elseif ( tvar(i).eq.'DVDX' ) then
537
     elseif ( tvar(i).eq.'DVDX' ) then
537
        varname='V:+1DLON'                            ! V:+1DLON
538
        varname='V:+1DLON'                            ! V:+1DLON
538
        call add2list(varname,tvar,ntrace1)
539
        call add2list(varname,tvar,ntrace1)
539
        varname='V:-1DLON'                            ! V:-1DLON
540
        varname='V:-1DLON'                            ! V:-1DLON
540
        call add2list(varname,tvar,ntrace1)
541
        call add2list(varname,tvar,ntrace1)
541
 
542
 
542
     ! Get the dependencies for du/dy (DUDY)
543
     ! Get the dependencies for du/dy (DUDY)
543
     elseif ( tvar(i).eq.'DUDY' ) then
544
     elseif ( tvar(i).eq.'DUDY' ) then
544
        varname='U:+1DLAT'                            ! U:+1DLAT
545
        varname='U:+1DLAT'                            ! U:+1DLAT
545
        call add2list(varname,tvar,ntrace1)
546
        call add2list(varname,tvar,ntrace1)
546
        varname='U:-1DLAT'                            ! U:-1DLAT
547
        varname='U:-1DLAT'                            ! U:-1DLAT
547
        call add2list(varname,tvar,ntrace1)
548
        call add2list(varname,tvar,ntrace1)
548
 
549
 
549
     ! Get the dependencies for dv/dy (DVDY)
550
     ! Get the dependencies for dv/dy (DVDY)
550
     elseif ( tvar(i).eq.'DVDY' ) then
551
     elseif ( tvar(i).eq.'DVDY' ) then
551
        varname='V:+1DLAT'                            ! V:+1DLAT
552
        varname='V:+1DLAT'                            ! V:+1DLAT
552
        call add2list(varname,tvar,ntrace1)
553
        call add2list(varname,tvar,ntrace1)
553
        varname='V:-1DLAT'                            ! V:-1DLAT
554
        varname='V:-1DLAT'                            ! V:-1DLAT
554
        call add2list(varname,tvar,ntrace1)
555
        call add2list(varname,tvar,ntrace1)
555
 
556
 
556
     ! Get the dependencies for du/dp (DUDP)
557
     ! Get the dependencies for du/dp (DUDP)
557
     elseif ( tvar(i).eq.'DUDP' ) then
558
     elseif ( tvar(i).eq.'DUDP' ) then
558
        varname='U:+1DP'                              ! U:+1DP
559
        varname='U:+1DP'                              ! U:+1DP
559
        call add2list(varname,tvar,ntrace1)
560
        call add2list(varname,tvar,ntrace1)
560
        varname='U:-1DP'                              ! U:-1DP
561
        varname='U:-1DP'                              ! U:-1DP
561
        call add2list(varname,tvar,ntrace1)
562
        call add2list(varname,tvar,ntrace1)
562
        varname='P:+1DP'                              ! P:+1DP
563
        varname='P:+1DP'                              ! P:+1DP
563
        call add2list(varname,tvar,ntrace1)
564
        call add2list(varname,tvar,ntrace1)
564
        varname='P:-1DP'                              ! P:-1DP
565
        varname='P:-1DP'                              ! P:-1DP
565
        call add2list(varname,tvar,ntrace1)
566
        call add2list(varname,tvar,ntrace1)
566
 
567
 
567
     ! Get the dependencies for dv/dp (DVDP)
568
     ! Get the dependencies for dv/dp (DVDP)
568
     elseif ( tvar(i).eq.'DVDP' ) then
569
     elseif ( tvar(i).eq.'DVDP' ) then
569
        varname='V:+1DP'                              ! V:+1DP
570
        varname='V:+1DP'                              ! V:+1DP
570
        call add2list(varname,tvar,ntrace1)
571
        call add2list(varname,tvar,ntrace1)
571
        varname='V:-1DP'                              ! V:-1DP
572
        varname='V:-1DP'                              ! V:-1DP
572
        call add2list(varname,tvar,ntrace1)
573
        call add2list(varname,tvar,ntrace1)
573
        varname='P:+1DP'                              ! P:+1DP
574
        varname='P:+1DP'                              ! P:+1DP
574
        call add2list(varname,tvar,ntrace1)
575
        call add2list(varname,tvar,ntrace1)
575
        varname='P:-1DP'                              ! P:-1DP
576
        varname='P:-1DP'                              ! P:-1DP
576
        call add2list(varname,tvar,ntrace1)
577
        call add2list(varname,tvar,ntrace1)
577
 
578
 
578
     ! Get the dependencies for dt/dx (DTDX)
579
     ! Get the dependencies for dt/dx (DTDX)
579
     elseif ( tvar(i).eq.'DTDX' ) then
580
     elseif ( tvar(i).eq.'DTDX' ) then
580
        varname='T:+1DLON'                            ! T:+1DLON
581
        varname='T:+1DLON'                            ! T:+1DLON
581
        call add2list(varname,tvar,ntrace1)
582
        call add2list(varname,tvar,ntrace1)
582
        varname='T:-1DLON'                            ! T:-1DLON
583
        varname='T:-1DLON'                            ! T:-1DLON
583
        call add2list(varname,tvar,ntrace1)
584
        call add2list(varname,tvar,ntrace1)
584
 
585
 
585
     ! Get the dependencies for dth/dy (DTHDY)
586
     ! Get the dependencies for dth/dy (DTHDY)
586
     elseif ( tvar(i).eq.'DTHDY' ) then
587
     elseif ( tvar(i).eq.'DTHDY' ) then
587
        varname='T:+1DLAT'                            ! T:+1DLON
588
        varname='T:+1DLAT'                            ! T:+1DLON
588
        call add2list(varname,tvar,ntrace1)
589
        call add2list(varname,tvar,ntrace1)
589
        varname='T:-1DLAT'                            ! T:-1DLON
590
        varname='T:-1DLAT'                            ! T:-1DLON
590
        call add2list(varname,tvar,ntrace1)
591
        call add2list(varname,tvar,ntrace1)
591
        varname='P'                                   ! P
592
        varname='P'                                   ! P
592
        call add2list(varname,tvar,ntrace1)
593
        call add2list(varname,tvar,ntrace1)
593
 
594
 
594
     ! Get the dependencies for dth/dx (DTHDX)
595
     ! Get the dependencies for dth/dx (DTHDX)
595
     elseif ( tvar(i).eq.'DTHDX' ) then
596
     elseif ( tvar(i).eq.'DTHDX' ) then
596
        varname='T:+1DLON'                            ! T:+1DLON
597
        varname='T:+1DLON'                            ! T:+1DLON
597
        call add2list(varname,tvar,ntrace1)
598
        call add2list(varname,tvar,ntrace1)
598
        varname='T:-1DLON'                            ! T:-1DLON
599
        varname='T:-1DLON'                            ! T:-1DLON
599
        call add2list(varname,tvar,ntrace1)
600
        call add2list(varname,tvar,ntrace1)
600
        varname='P'                                   ! P
601
        varname='P'                                   ! P
601
        call add2list(varname,tvar,ntrace1)
602
        call add2list(varname,tvar,ntrace1)
602
 
603
 
603
     ! Get the dependencies for dt/dy (DTDY)
604
     ! Get the dependencies for dt/dy (DTDY)
604
     elseif ( tvar(i).eq.'DTDY' ) then
605
     elseif ( tvar(i).eq.'DTDY' ) then
605
        varname='T:+1DLAT'                            ! T:+1DLON
606
        varname='T:+1DLAT'                            ! T:+1DLON
606
        call add2list(varname,tvar,ntrace1)
607
        call add2list(varname,tvar,ntrace1)
607
        varname='T:-1DLAT'                            ! T:-1DLON
608
        varname='T:-1DLAT'                            ! T:-1DLON
608
        call add2list(varname,tvar,ntrace1)
609
        call add2list(varname,tvar,ntrace1)
609
 
610
 
610
     ! Get the dependencies for dt/dp (DTDP)
611
     ! Get the dependencies for dt/dp (DTDP)
611
     elseif ( tvar(i).eq.'DTDP' ) then
612
     elseif ( tvar(i).eq.'DTDP' ) then
612
        varname='T:+1DP'                              ! T:+1DP
613
        varname='T:+1DP'                              ! T:+1DP
613
        call add2list(varname,tvar,ntrace1)
614
        call add2list(varname,tvar,ntrace1)
614
        varname='T:-1DP'                              ! T:-1DP
615
        varname='T:-1DP'                              ! T:-1DP
615
        call add2list(varname,tvar,ntrace1)
616
        call add2list(varname,tvar,ntrace1)
616
        varname='P:+1DP'                              ! P:+1DP
617
        varname='P:+1DP'                              ! P:+1DP
617
        call add2list(varname,tvar,ntrace1)
618
        call add2list(varname,tvar,ntrace1)
618
        varname='P:-1DP'                              ! P:-1DP
619
        varname='P:-1DP'                              ! P:-1DP
619
        call add2list(varname,tvar,ntrace1)
620
        call add2list(varname,tvar,ntrace1)
620
 
621
 
621
     ! Get the dependencies for dth/dp (DTHDP)
622
     ! Get the dependencies for dth/dp (DTHDP)
622
     elseif ( tvar(i).eq.'DTHDP' ) then
623
     elseif ( tvar(i).eq.'DTHDP' ) then
623
        varname='T:+1DP'                              ! T:+1DP
624
        varname='T:+1DP'                              ! T:+1DP
624
        call add2list(varname,tvar,ntrace1)
625
        call add2list(varname,tvar,ntrace1)
625
        varname='T:-1DP'                              ! T:-1DP
626
        varname='T:-1DP'                              ! T:-1DP
626
        call add2list(varname,tvar,ntrace1)
627
        call add2list(varname,tvar,ntrace1)
627
        varname='T'                                   ! T
628
        varname='T'                                   ! T
628
        call add2list(varname,tvar,ntrace1)
629
        call add2list(varname,tvar,ntrace1)
629
        varname='P:+1DP'                              ! P:+1DP
630
        varname='P:+1DP'                              ! P:+1DP
630
        call add2list(varname,tvar,ntrace1)
631
        call add2list(varname,tvar,ntrace1)
631
        varname='P:-1DP'                              ! P:-1DP
632
        varname='P:-1DP'                              ! P:-1DP
632
        call add2list(varname,tvar,ntrace1)
633
        call add2list(varname,tvar,ntrace1)
633
        varname='P'                                   ! P
634
        varname='P'                                   ! P
634
        call add2list(varname,tvar,ntrace1)
635
        call add2list(varname,tvar,ntrace1)
635
 
636
 
636
     ! Get the dependencies for squared Brunt Vaiäläa frequency (NSQ)
637
     ! Get the dependencies for squared Brunt Vaiäläa frequency (NSQ)
637
     elseif ( tvar(i).eq.'NSQ' ) then
638
     elseif ( tvar(i).eq.'NSQ' ) then
638
        varname='DTHDP'                                ! DTHDP
639
        varname='DTHDP'                                ! DTHDP
639
        call add2list(varname,tvar,ntrace1)
640
        call add2list(varname,tvar,ntrace1)
640
        varname='TH'                                   ! TH
641
        varname='TH'                                   ! TH
641
        call add2list(varname,tvar,ntrace1)
642
        call add2list(varname,tvar,ntrace1)
642
        varname='RHO'                                  ! RHO
643
        varname='RHO'                                  ! RHO
643
        call add2list(varname,tvar,ntrace1)
644
        call add2list(varname,tvar,ntrace1)
644
 
645
 
645
     ! Get the dependencies for relative vorticity (RELVORT)
646
     ! Get the dependencies for relative vorticity (RELVORT)
646
     elseif ( tvar(i).eq.'RELVORT' ) then
647
     elseif ( tvar(i).eq.'RELVORT' ) then
647
        varname='U'                                    ! U
648
        varname='U'                                    ! U
648
        call add2list(varname,tvar,ntrace1)
649
        call add2list(varname,tvar,ntrace1)
649
        varname='DUDY'                                 ! DUDY
650
        varname='DUDY'                                 ! DUDY
650
        call add2list(varname,tvar,ntrace1)
651
        call add2list(varname,tvar,ntrace1)
651
        varname='DVDX'                                 ! DVDX
652
        varname='DVDX'                                 ! DVDX
652
        call add2list(varname,tvar,ntrace1)
653
        call add2list(varname,tvar,ntrace1)
653
 
654
 
654
     ! Get the dependencies for relative vorticity (ABSVORT)
655
     ! Get the dependencies for relative vorticity (ABSVORT)
655
     elseif ( tvar(i).eq.'ABSVORT' ) then
656
     elseif ( tvar(i).eq.'ABSVORT' ) then
656
        varname='U'                                    ! U
657
        varname='U'                                    ! U
657
        call add2list(varname,tvar,ntrace1)
658
        call add2list(varname,tvar,ntrace1)
658
        varname='DUDY'                                 ! DUDY
659
        varname='DUDY'                                 ! DUDY
659
        call add2list(varname,tvar,ntrace1)
660
        call add2list(varname,tvar,ntrace1)
660
        varname='DVDX'                                 ! DVDX
661
        varname='DVDX'                                 ! DVDX
661
        call add2list(varname,tvar,ntrace1)
662
        call add2list(varname,tvar,ntrace1)
662
 
663
 
663
     ! Get the dependencies for divergence (DIV)
664
     ! Get the dependencies for divergence (DIV)
664
     elseif ( tvar(i).eq.'DIV' ) then
665
     elseif ( tvar(i).eq.'DIV' ) then
665
        varname='V'                                    ! U
666
        varname='V'                                    ! U
666
        call add2list(varname,tvar,ntrace1)
667
        call add2list(varname,tvar,ntrace1)
667
        varname='DUDX'                                 ! DUDX
668
        varname='DUDX'                                 ! DUDX
668
        call add2list(varname,tvar,ntrace1)
669
        call add2list(varname,tvar,ntrace1)
669
        varname='DVDY'                                 ! DVDY
670
        varname='DVDY'                                 ! DVDY
670
        call add2list(varname,tvar,ntrace1)
671
        call add2list(varname,tvar,ntrace1)
671
 
672
 
672
     ! Get the dependencies for deformation (DEF)
673
     ! Get the dependencies for deformation (DEF)
673
     elseif ( tvar(i).eq.'DEF' ) then
674
     elseif ( tvar(i).eq.'DEF' ) then
674
        call add2list(varname,tvar,ntrace1)
675
        call add2list(varname,tvar,ntrace1)
675
        varname='DUDX'                                 ! DUDX
676
        varname='DUDX'                                 ! DUDX
676
        call add2list(varname,tvar,ntrace1)
677
        call add2list(varname,tvar,ntrace1)
677
        varname='DVDY'                                 ! DVDY
678
        varname='DVDY'                                 ! DVDY
678
        call add2list(varname,tvar,ntrace1)
679
        call add2list(varname,tvar,ntrace1)
679
        varname='DUDY'                                 ! DUDY
680
        varname='DUDY'                                 ! DUDY
680
        call add2list(varname,tvar,ntrace1)
681
        call add2list(varname,tvar,ntrace1)
681
        varname='DVDX'                                 ! DVDX
682
        varname='DVDX'                                 ! DVDX
682
        call add2list(varname,tvar,ntrace1)
683
        call add2list(varname,tvar,ntrace1)
683
 
684
 
684
     ! Get the dependencies for potential vorticity (PV)
685
     ! Get the dependencies for potential vorticity (PV)
685
     elseif ( tvar(i).eq.'PV' ) then
686
     elseif ( tvar(i).eq.'PV' ) then
686
        varname='ABSVORT'                              ! ABSVORT
687
        varname='ABSVORT'                              ! ABSVORT
687
        call add2list(varname,tvar,ntrace1)
688
        call add2list(varname,tvar,ntrace1)
688
        varname='DTHDP'                                ! DTHDP
689
        varname='DTHDP'                                ! DTHDP
689
        call add2list(varname,tvar,ntrace1)
690
        call add2list(varname,tvar,ntrace1)
690
        varname='DUDP'                                 ! DUDP
691
        varname='DUDP'                                 ! DUDP
691
        call add2list(varname,tvar,ntrace1)
692
        call add2list(varname,tvar,ntrace1)
692
        varname='DVDP'                                 ! DVDP
693
        varname='DVDP'                                 ! DVDP
693
        call add2list(varname,tvar,ntrace1)
694
        call add2list(varname,tvar,ntrace1)
694
        varname='DTHDX'                                ! DTHDX
695
        varname='DTHDX'                                ! DTHDX
695
        call add2list(varname,tvar,ntrace1)
696
        call add2list(varname,tvar,ntrace1)
696
        varname='DTHDY'                                ! DTHDY
697
        varname='DTHDY'                                ! DTHDY
697
        call add2list(varname,tvar,ntrace1)
698
        call add2list(varname,tvar,ntrace1)
698
 
699
 
699
     ! Get the dependencies for Richardson number (RI)
700
     ! Get the dependencies for Richardson number (RI)
700
     elseif ( tvar(i).eq.'RI' ) then
701
     elseif ( tvar(i).eq.'RI' ) then
701
        varname='DUDP'                                 ! DUDP
702
        varname='DUDP'                                 ! DUDP
702
        call add2list(varname,tvar,ntrace1)
703
        call add2list(varname,tvar,ntrace1)
703
        varname='DVDP'                                 ! DVDP
704
        varname='DVDP'                                 ! DVDP
704
        call add2list(varname,tvar,ntrace1)
705
        call add2list(varname,tvar,ntrace1)
705
        varname='NSQ'                                  ! NSQ
706
        varname='NSQ'                                  ! NSQ
706
        call add2list(varname,tvar,ntrace1)
707
        call add2list(varname,tvar,ntrace1)
707
        varname='RHO'                                  ! RHO
708
        varname='RHO'                                  ! RHO
708
        call add2list(varname,tvar,ntrace1)
709
        call add2list(varname,tvar,ntrace1)
709
 
710
 
710
     ! Get the dependencies for Ellrod&Knapp's turbulence index (TI)
711
     ! Get the dependencies for Ellrod&Knapp's turbulence index (TI)
711
     elseif ( tvar(i).eq.'TI' ) then
712
     elseif ( tvar(i).eq.'TI' ) then
712
        varname='DEF'                                  ! DEF
713
        varname='DEF'                                  ! DEF
713
        call add2list(varname,tvar,ntrace1)
714
        call add2list(varname,tvar,ntrace1)
714
        varname='DUDP'                                 ! DUDP
715
        varname='DUDP'                                 ! DUDP
715
        call add2list(varname,tvar,ntrace1)
716
        call add2list(varname,tvar,ntrace1)
716
        varname='DVDP'                                 ! DVDP
717
        varname='DVDP'                                 ! DVDP
717
        call add2list(varname,tvar,ntrace1)
718
        call add2list(varname,tvar,ntrace1)
718
        varname='RHO'                                  ! RHO
719
        varname='RHO'                                  ! RHO
719
        call add2list(varname,tvar,ntrace1)
720
        call add2list(varname,tvar,ntrace1)
720
 
721
 
721
     endif
722
     endif
722
 
723
 
723
     ! Exit point for handling additional fields
724
     ! Exit point for handling additional fields
724
 100   continue
725
 100   continue
725
     i = i + 1
726
     i = i + 1
726
 
727
 
727
  enddo
728
  enddo
728
 
729
 
729
  ! Save the full variable name (including shift specification)
730
  ! Save the full variable name (including shift specification)
730
  do i=1,ncol
731
  do i=1,ncol
731
     varsint(i)      = varsinp(i)
732
     varsint(i)      = varsinp(i)
732
  enddo
733
  enddo
733
  do i=1,ntrace1
734
  do i=1,ntrace1
734
     varsint(i+ncol) = tvar(i)
735
     varsint(i+ncol) = tvar(i)
735
  enddo
736
  enddo
736
 
737
 
737
  ! Bojan: check that PV and TH are on trajectory
738
  ! Bojan: check that PV and TH are on trajectory
738
  if (intmode .eq. 'clustering') then
739
  if (intmode .eq. 'clustering') then
739
     pvpos=-1
740
     pvpos=-1
740
     thpos=-1
741
     thpos=-1
741
     do i=1,ncol+ntrace1
742
     do i=1,ncol+ntrace1
742
        if (varsint(i) .eq. 'TH') then
743
        if (varsint(i) .eq. 'TH') then
743
        thpos=i
744
        thpos=i
744
        print*,'Clustering: Found TH at position:',thpos
745
        print*,'Clustering: Found TH at position:',thpos
745
        endif
746
        endif
746
        if (varsint(i) .eq. 'PV') then
747
        if (varsint(i) .eq. 'PV') then
747
        pvpos=i
748
        pvpos=i
748
        print*,'Clustering: Found PV at position:',pvpos
749
        print*,'Clustering: Found PV at position:',pvpos
749
        endif
750
        endif
750
     enddo
751
     enddo
751
     if (thpos .eq. -1) then
752
     if (thpos .eq. -1) then
752
        print*,'WARNING (clustering): Did not find TH'
753
        print*,'WARNING (clustering): Did not find TH'
753
        stop
754
        stop
754
     endif
755
     endif
755
     if (pvpos .eq. -1) then
756
     if (pvpos .eq. -1) then
756
        print*,'WARNING (clustering): Did not find PV'
757
        print*,'WARNING (clustering): Did not find PV'
757
        stop
758
        stop
758
     endif
759
     endif
759
  endif
760
  endif
760
  ! Bojan
761
  ! Bojan
761
 
762
 
762
  ! Split the tracing variables
763
  ! Split the tracing variables
763
  do i=1,ntrace0
764
  do i=1,ntrace0
764
    call splitvar(tvar(i),shift_val(i),shift_dir(i) )
765
    call splitvar(tvar(i),shift_val(i),shift_dir(i),shift_rel(i) )
765
  enddo
766
  enddo
766
 
767
 
767
 
768
 
768
  ! Split the variable name and set flags
769
  ! Split the variable name and set flags
769
  do i=ntrace0+1,ntrace1
770
  do i=ntrace0+1,ntrace1
770
 
771
 
771
     ! Set the scaling factor
772
     ! Set the scaling factor
772
     fac(i) = 1.
773
     fac(i) = 1.
773
 
774
 
774
     ! Set the base variable name, the shift and the direction
775
     ! Set the base variable name, the shift and the direction
775
     call splitvar(tvar(i),shift_val(i),shift_dir(i) )
776
     call splitvar(tvar(i),shift_val(i),shift_dir(i),shift_rel(i) )
776
 
777
 
777
     ! Set the prefix of the file name for additional fields
778
     ! Set the prefix of the file name for additional fields
778
     tfil(i)='*'
779
     tfil(i)='*'
779
     do j=1,n_pvars
780
     do j=1,n_pvars
780
        if ( tvar(i).eq.pvars(j) ) tfil(i)=charp
781
        if ( tvar(i).eq.pvars(j) ) tfil(i)=charp
781
     enddo
782
     enddo
782
     do j=1,n_svars
783
     do j=1,n_svars
783
        if ( tvar(i).eq.svars(j) ) tfil(i)=chars
784
        if ( tvar(i).eq.svars(j) ) tfil(i)=chars
784
     enddo
785
     enddo
785
 
786
 
786
     ! Set the computational flag
787
     ! Set the computational flag
787
     if ( (tvar(i).eq.'P'   ).or. &
788
     if ( (tvar(i).eq.'P'   ).or. &
788
          (tvar(i).eq.'PLAY').or. &
789
          (tvar(i).eq.'PLAY').or. &
789
          (tvar(i).eq.'PLEV') ) then
790
          (tvar(i).eq.'PLEV') ) then
790
        compfl(i) = 0
791
        compfl(i) = 0
791
        tfil(i)   = charp
792
        tfil(i)   = charp
792
     elseif ( ( tfil(i).eq.charp ).or.( tfil(i).eq.chars ) ) then
793
     elseif ( ( tfil(i).eq.charp ).or.( tfil(i).eq.chars ) ) then
793
        compfl(i) = 0
794
        compfl(i) = 0
794
     else
795
     else
795
        compfl(i) = 1
796
        compfl(i) = 1
796
     endif
797
     endif
797
 
798
 
798
  enddo
799
  enddo
799
 
800
 
800
  ! Check whether the shift modes are supported
801
  ! Check whether the shift modes are supported
801
  do i=1,ntrace1
802
  do i=1,ntrace1
802
     if ( ( shift_dir(i).ne.'nil'     ).and. &
803
     if ( ( shift_dir(i).ne.'nil'     ).and. &
803
          ( shift_dir(i).ne.'DLON'    ).and. &
804
          ( shift_dir(i).ne.'DLON'    ).and. &
804
          ( shift_dir(i).ne.'DLAT'    ).and. &
805
          ( shift_dir(i).ne.'DLAT'    ).and. &
805
          ( shift_dir(i).ne.'DP'      ).and. &
806
          ( shift_dir(i).ne.'DP'      ).and. &
806
          ( shift_dir(i).ne.'HPA'     ).and. &
807
          ( shift_dir(i).ne.'HPA'     ).and. &
807
          ( shift_dir(i).ne.'HPA(ABS)').and. &
808
          ( shift_dir(i).ne.'HPA(ABS)').and. &
808
          ( shift_dir(i).ne.'KM(LON)' ).and. &
809
          ( shift_dir(i).ne.'KM(LON)' ).and. &
809
          ( shift_dir(i).ne.'KM(LAT)' ).and. &
810
          ( shift_dir(i).ne.'KM(LAT)' ).and. &
810
          ( shift_dir(i).ne.'H'       ).and. &
811
          ( shift_dir(i).ne.'H'       ).and. &
811
          ( shift_dir(i).ne.'MIN'     ).and. &
812
          ( shift_dir(i).ne.'MIN'     ).and. &
-
 
813
          ( shift_dir(i).ne.'INDP'    ).and. & 
-
 
814
          ( shift_dir(i).ne.'PMIN'    ).and. &
812
          ( shift_dir(i).ne.'INDP'    ) ) then
815
          ( shift_dir(i).ne.'PMAX'    ) ) then
813
        print*,' ERROR: shift mode ',trim(shift_dir(i)), ' not supported'
816
        print*,' ERROR: shift mode ',trim(shift_dir(i)), ' not supported'
814
        stop
817
        stop
815
     endif
818
     endif
816
  enddo
819
  enddo
817
 
820
 
818
  ! Write status information
821
  ! Write status information
819
  print*
822
  print*
820
  print*,'---- COMPLETE TABLE FOR TRACING -------------------------'
823
  print*,'---- COMPLETE TABLE FOR TRACING -------------------------'
821
  print*
824
  print*
822
  do i=1,ntrace1
825
  do i=1,ntrace1
823
     if ( ( shift_dir(i).ne.'nil' ) ) then
826
     if ( ( shift_dir(i).ne.'nil' ) ) then
824
        write(*,'(i4,a4,a8,f10.2,a8,3x,a4,i5)') i,' : ',trim(tvar(i)), &
827
        write(*,'(i4,a4,a8,f10.2,a8,a8,3x,a4,i5)') i,' : ',trim(tvar(i)), &
825
             shift_val(i),trim(shift_dir(i)),tfil(i),compfl(i)
828
             shift_val(i),trim(shift_dir(i)),trim(shift_rel(i)),tfil(i),compfl(i)
826
     else
829
     else
827
        write(*,'(i4,a4,a8,10x,8x,3x,a4,i5)') &
830
        write(*,'(i4,a4,a8,10x,8x,3x,a4,i5)') &
828
             i,' : ',trim(tvar(i)),tfil(i),compfl(i)
831
             i,' : ',trim(tvar(i)),tfil(i),compfl(i)
829
     endif
832
     endif
830
  enddo
833
  enddo
831
 
834
 
832
  ! --------------------------------------------------------------------
835
  ! --------------------------------------------------------------------
833
  ! Prepare the internal and output trajectories
836
  ! Prepare the internal and output trajectories
834
  ! --------------------------------------------------------------------
837
  ! --------------------------------------------------------------------
835
 
838
 
836
  ! Allocate memory for internal trajectories
839
  ! Allocate memory for internal trajectories
837
  allocate(traint(ntra,ntim,ncol+ntrace1),stat=stat)
840
  allocate(traint(ntra,ntim,ncol+ntrace1),stat=stat)
838
  if (stat.ne.0) print*,'*** error allocating array traint   ***'
841
  if (stat.ne.0) print*,'*** error allocating array traint   ***'
839
 
842
 
840
  ! Copy input to output trajectory
843
  ! Copy input to output trajectory
841
  do i=1,ntra
844
  do i=1,ntra
842
     do j=1,ntim
845
     do j=1,ntim
843
        do k=1,ncol
846
        do k=1,ncol
844
           traint(i,j,k)=trainp(i,j,k)
847
           traint(i,j,k)=trainp(i,j,k)
845
        enddo
848
        enddo
846
     enddo
849
     enddo
847
  enddo
850
  enddo
848
 
851
 
849
  ! Set the flags for ready fields/colums - at begin only read-in fields are ready
852
  ! Set the flags for ready fields/colums - at begin only read-in fields are ready
850
  do i=1,ncol
853
  do i=1,ncol
851
     fok(i) = 1
854
     fok(i) = 1
852
  enddo
855
  enddo
853
  do i=ncol+1,ntrace1
856
  do i=ncol+1,ntrace1
854
     fok(i) = 0
857
     fok(i) = 0
855
  enddo
858
  enddo
856
 
859
 
857
  ! --------------------------------------------------------------------
860
  ! --------------------------------------------------------------------
858
  ! Trace the fields (fields available on input files)
861
  ! Trace the fields (fields available on input files)
859
  ! --------------------------------------------------------------------
862
  ! --------------------------------------------------------------------
860
 
863
 
861
  print*
864
  print*
862
  print*,'---- TRACING FROM PRIMARY AND SECONDARY DATA FILES ------'
865
  print*,'---- TRACING FROM PRIMARY AND SECONDARY DATA FILES ------'
863
 
866
 
864
  ! Loop over all tracing fields
867
  ! Loop over all tracing fields
865
  do i=1,ntrace1
868
  do i=1,ntrace1
866
 
869
 
867
      ! Skip fields which must be computed (compfl=1), will be handled later
870
      ! Skip fields which must be computed (compfl=1), will be handled later
868
      if (compfl(i).ne.0)  goto 110
871
      if (compfl(i).ne.0)  goto 110
869
 
872
 
870
      ! Write some status information
873
      ! Write some status information
871
      print*
874
      print*
872
      print*,' Now tracing             : ', trim(tvar(i)),shift_val(i),trim(shift_dir(i)),compfl(i),' ',trim(tfil(i))
875
      print*,' Now tracing             : ', trim(tvar(i)),shift_val(i),trim(shift_dir(i)),compfl(i),' ',trim(tfil(i))
873
 
876
 
874
      ! Set the flag for ready field/column
877
      ! Set the flag for ready field/column
875
      fok(ncol+i) = 1
878
      fok(ncol+i) = 1
876
 
879
 
877
      ! Reset flags for load manager
880
      ! Reset flags for load manager
878
      iloaded0 = -1
881
      iloaded0 = -1
879
      iloaded1 = -1
882
      iloaded1 = -1
880
 
883
 
881
      ! Reset the counter for fields outside domain
884
      ! Reset the counter for fields outside domain
882
      noutside = 0
885
      noutside = 0
883
      err_c1   = 0
886
      err_c1   = 0
884
      err_c2   = 0
887
      err_c2   = 0
885
      err_c3   = 0
888
      err_c3   = 0
886
 
889
 
887
      ! Loop over all times
890
      ! Loop over all times
888
      do j=1,ntim
891
      do j=1,ntim
889
 
892
 
890
         ! Convert trajectory time from hh.mm to fractional time
893
         ! Convert trajectory time from hh.mm to fractional time
891
         call hhmm2frac(trainp(1,j,1),tfrac)
894
         call hhmm2frac(trainp(1,j,1),tfrac)
892
 
895
 
893
         ! Shift time if requested
896
         ! Shift time if requested
894
         if ( shift_dir(i).eq.'H' ) then
897
         if ( shift_dir(i).eq.'H' ) then
895
            tfrac = tfrac + shift_val(i)
898
            tfrac = tfrac + shift_val(i)
896
         elseif ( shift_dir(i).eq.'MIN' ) then
899
         elseif ( shift_dir(i).eq.'MIN' ) then
897
            tfrac = tfrac + shift_val(i)/60.
900
            tfrac = tfrac + shift_val(i)/60.
898
         endif
901
         endif
899
 
902
 
900
         ! Get the times which are needed
903
         ! Get the times which are needed
901
         itime0   = int(abs(tfrac-tstart)/timeinc) + 1
904
         itime0   = int(abs(tfrac-tstart)/timeinc) + 1
902
         time0    = tstart + fbflag * real(itime0-1) * timeinc
905
         time0    = tstart + fbflag * real(itime0-1) * timeinc
903
         itime1   = itime0 + 1
906
         itime1   = itime0 + 1
904
         time1    = time0 + fbflag * timeinc
907
         time1    = time0 + fbflag * timeinc
905
         if ( itime1.gt.numdat ) then
908
         if ( itime1.gt.numdat ) then
906
            itime1 = itime0
909
            itime1 = itime0
907
            time1  = time0
910
            time1  = time0
908
         endif
911
         endif
909
 
912
 
910
         ! Load manager: Check whether itime0 can be copied from itime1
913
         ! Load manager: Check whether itime0 can be copied from itime1
911
         if ( itime0.eq.iloaded1 ) then
914
         if ( itime0.eq.iloaded1 ) then
912
            f3t0     = f3t1
915
            f3t0     = f3t1
913
            p3t0     = p3t1
916
            p3t0     = p3t1
914
            spt0     = spt1
917
            spt0     = spt1
915
            iloaded0 = itime0
918
            iloaded0 = itime0
916
         endif
919
         endif
917
 
920
 
918
         ! Load manager: Check whether itime1 can be copied from itime0
921
         ! Load manager: Check whether itime1 can be copied from itime0
919
         if ( itime1.eq.iloaded0 ) then
922
         if ( itime1.eq.iloaded0 ) then
920
            f3t1     = f3t0
923
            f3t1     = f3t0
921
            p3t1     = p3t0
924
            p3t1     = p3t0
922
            spt1     = spt0
925
            spt1     = spt0
923
            iloaded1 = itime1
926
            iloaded1 = itime1
924
         endif
927
         endif
925
 
928
 
926
         ! Load manager:  Load first time (tracing variable and grid)
929
         ! Load manager:  Load first time (tracing variable and grid)
927
         if ( itime0.ne.iloaded0 ) then
930
         if ( itime0.ne.iloaded0 ) then
928
 
931
 
929
            filename = trim(tfil(i))//trim(dat(itime0))
932
            filename = trim(tfil(i))//trim(dat(itime0))
930
            call frac2hhmm(time0,tload)
933
            call frac2hhmm(time0,tload)
931
            varname  = tvar(i)
934
            varname  = tvar(i)
932
            write(*,'(a23,a20,a3,a5,f7.2)') '    ->  loading          : ', trim(filename),'  ',trim(varname),tload
935
            write(*,'(a23,a20,a3,a5,f7.2)') '    ->  loading          : ', trim(filename),'  ',trim(varname),tload
933
            call input_open (fid,filename)
936
            call input_open (fid,filename)
-
 
937
            mdv = -999.
934
            call input_wind &
938
            call input_wind &
935
                 (fid,varname,f3t0,tload,stagz,mdv, &
939
                 (fid,varname,f3t0,tload,stagz,mdv, &
936
                 xmin,xmax,ymin,ymax,dx,dy,nx,ny,nz,timecheck)
940
                 xmin,xmax,ymin,ymax,dx,dy,nx,ny,nz,timecheck)
937
            call input_grid &
941
            call input_grid &
938
                 (fid,varname,xmin,xmax,ymin,ymax,dx,dy,nx,ny, &
942
                 (fid,varname,xmin,xmax,ymin,ymax,dx,dy,nx,ny, &
939
                 tload,pollon,pollat,p3t0,spt0,nz,ak,bk,stagz, &
943
                 tload,pollon,pollat,p3t0,spt0,nz,ak,bk,stagz, &
940
                 timecheck)
944
                 timecheck)
941
            call input_close(fid)
945
            call input_close(fid)
942
 
946
 
943
            iloaded0 = itime0
947
            iloaded0 = itime0
944
 
948
 
945
         endif
949
         endif
946
 
950
 
947
         ! Load manager: Load second time (tracing variable and grid)
951
         ! Load manager: Load second time (tracing variable and grid)
948
         if ( itime1.ne.iloaded1 ) then
952
         if ( itime1.ne.iloaded1 ) then
949
 
953
 
950
            filename = trim(tfil(i))//trim(dat(itime1))
954
            filename = trim(tfil(i))//trim(dat(itime1))
951
            call frac2hhmm(time1,tload)
955
            call frac2hhmm(time1,tload)
952
            varname  = tvar(i)
956
            varname  = tvar(i)
953
            write(*,'(a23,a20,a3,a5,f7.2)') '    ->  loading          : ', trim(filename),'  ',trim(varname),tload
957
            write(*,'(a23,a20,a3,a5,f7.2)') '    ->  loading          : ', trim(filename),'  ',trim(varname),tload
954
            call input_open (fid,filename)
958
            call input_open (fid,filename)
955
            call input_wind &
959
            call input_wind &
956
                 (fid,varname,f3t1,tload,stagz,mdv, &
960
                 (fid,varname,f3t1,tload,stagz,mdv, &
957
                 xmin,xmax,ymin,ymax,dx,dy,nx,ny,nz,timecheck)
961
                 xmin,xmax,ymin,ymax,dx,dy,nx,ny,nz,timecheck)
958
            call input_grid &
962
            call input_grid &
959
                 (fid,varname,xmin,xmax,ymin,ymax,dx,dy,nx,ny, &
963
                 (fid,varname,xmin,xmax,ymin,ymax,dx,dy,nx,ny, &
960
                 tload,pollon,pollat,p3t1,spt1,nz,ak,bk,stagz, &
964
                 tload,pollon,pollat,p3t1,spt1,nz,ak,bk,stagz, &
961
                 timecheck)
965
                 timecheck)
962
            call input_close(fid)
966
            call input_close(fid)
963
 
967
 
964
            iloaded1 = itime1
968
            iloaded1 = itime1
965
 
969
 
966
         endif
970
         endif
967
 
971
 
968
         ! Loop over all trajectories
972
         ! Loop over all trajectories
969
         do k=1,ntra
973
         do k=1,ntra
970
 
974
 
971
            ! Set the horizontal position where to interpolate to
975
            ! Set the horizontal position where to interpolate to
972
            x0       = traint(k,j,2)                          ! Longitude
976
            x0       = traint(k,j,2)                          ! Longitude
973
            y0       = traint(k,j,3)                          ! Latitude
977
            y0       = traint(k,j,3)                          ! Latitude
974
 
978
 
975
            ! Set the vertical position where to interpolate to
979
            ! Set the vertical position where to interpolate to
976
            if ( nz.gt.1 ) then
980
            if ( nz.gt.1 ) then
977
               p0 = traint(k,j,4)                             ! Pressure (3D tracing)
981
               p0 = traint(k,j,4)                             ! Pressure (3D tracing)
978
            else
982
            else
979
               p0 = 1050.                                     ! Lowest level (2D tracing)
983
               p0 = 1050.                                     ! Lowest level (2D tracing)
980
            endif
984
            endif
981
 
985
 
982
            ! Set negative pressures to mdv
986
            ! Set negative pressures to mdv
983
            if (p0.lt.0.) then
987
            if (p0.lt.0.) then
984
	        f0 = mdv
988
	        f0 = mdv
985
                goto 109
989
                goto 109
986
            endif
990
            endif
987
 
991
 
988
            ! Set the relative time
992
            ! Set the relative time
989
            call hhmm2frac(traint(k,j,1),tfrac)
993
            call hhmm2frac(traint(k,j,1),tfrac)
990
            reltpos0 = fbflag * (tfrac-time0)/timeinc
994
            reltpos0 = fbflag * (tfrac-time0)/timeinc
991
 
995
 
992
            ! Make adjustments depending on the shift flag
996
            ! Make adjustments depending on the shift flag
993
            if ( shift_dir(i).eq.'DLON' ) then                         ! DLON
997
            if ( shift_dir(i).eq.'DLON' ) then                         ! DLON
994
               x0 = x0 + shift_val(i)
998
               x0 = x0 + shift_val(i)
995
 
999
 
996
            elseif  ( shift_dir(i).eq.'DLAT' ) then                    ! DLAT
1000
            elseif  ( shift_dir(i).eq.'DLAT' ) then                    ! DLAT
997
               y0 = y0 + shift_val(i)
1001
               y0 = y0 + shift_val(i)
998
 
1002
 
999
            elseif ( shift_dir(i).eq.'KM(LON)' ) then                  ! KM(LON)
1003
            elseif ( shift_dir(i).eq.'KM(LON)' ) then                  ! KM(LON)
1000
               x0 = x0 + shift_val(i)/deg2km * 1./cos(y0*pi180)
1004
               x0 = x0 + shift_val(i)/deg2km * 1./cos(y0*pi180)
1001
 
1005
 
1002
            elseif ( shift_dir(i).eq.'KM(LAT)' ) then                  ! KM(LAT)
1006
            elseif ( shift_dir(i).eq.'KM(LAT)' ) then                  ! KM(LAT)
1003
               y0 = y0 + shift_val(i)/deg2km
1007
               y0 = y0 + shift_val(i)/deg2km
1004
 
1008
 
1005
            elseif ( shift_dir(i).eq.'HPA' ) then                      ! HPA
1009
            elseif ( shift_dir(i).eq.'HPA' ) then                      ! HPA
1006
               p0 = p0 + shift_val(i)
1010
               p0 = p0 + shift_val(i)
1007
 
1011
 
1008
            elseif ( shift_dir(i).eq.'HPA(ABS)' ) then                 ! HPA(ABS)
1012
            elseif ( shift_dir(i).eq.'HPA(ABS)' ) then                 ! HPA(ABS)
1009
               p0 = shift_val(i)
1013
               p0 = shift_val(i)
1010
 
1014
 
1011
            elseif ( shift_dir(i).eq.'DP' ) then                       ! DP
1015
            elseif ( shift_dir(i).eq.'DP' ) then                       ! DP
1012
               call get_index4 (xind,yind,pind,x0,y0,p0,reltpos0, &
1016
               call get_index4 (xind,yind,pind,x0,y0,p0,reltpos0, &
1013
                    p3t0,p3t1,spt0,spt1,3,nx,ny,nz,xmin,ymin,dx,dy,mdv)
1017
                    p3t0,p3t1,spt0,spt1,3,nx,ny,nz,xmin,ymin,dx,dy,mdv)
1014
               pind = pind - shift_val(i)
1018
               pind = pind - shift_val(i)
1015
               p0   = int_index4(p3t0,p3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
1019
               p0   = int_index4(p3t0,p3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
1016
 
1020
 
1017
            elseif ( shift_dir(i).eq.'INDP' ) then
1021
            elseif ( shift_dir(i).eq.'INDP' ) then
1018
               p0   = int_index4(p3t0,p3t1,nx,ny,nz,xind,yind,shift_val(i),reltpos0,mdv)
1022
               p0   = int_index4(p3t0,p3t1,nx,ny,nz,xind,yind,shift_val(i),reltpos0,mdv)
1019
 
1023
 
1020
            endif
1024
            endif
1021
 
1025
 
1022
            ! Handle periodic boundaries in zonal direction
1026
            ! Handle periodic boundaries in zonal direction
1023
            if ( (x0.gt.xmax).and.(per.ne.0) ) x0 = x0 - 360.
1027
            if ( (x0.gt.xmax).and.(per.ne.0) ) x0 = x0 - 360.
1024
            if ( (x0.lt.xmin).and.(per.ne.0) ) x0 = x0 + 360.
1028
            if ( (x0.lt.xmin).and.(per.ne.0) ) x0 = x0 + 360.
1025
 
1029
 
1026
            ! Handle pole problems for hemispheric data (taken from caltra.f)
1030
            ! Handle pole problems for hemispheric data (taken from caltra.f)
1027
            if ((hem.eq.1).and.(y0.gt.90.)) then
1031
            if ((hem.eq.1).and.(y0.gt.90.)) then
1028
               print*,'WARNING: y0>90 ',y0,' => setting to 180-y0 ',180.-y0
1032
               print*,'WARNING: y0>90 ',y0,' => setting to 180-y0 ',180.-y0
1029
               y0=180.-y0
1033
               y0=180.-y0
1030
               x0=x0+per/2.
1034
               x0=x0+per/2.
1031
            endif
1035
            endif
1032
            if ((hem.eq.1).and.(y0.lt.-90.)) then
1036
            if ((hem.eq.1).and.(y0.lt.-90.)) then
1033
               print*,'WARNING: y0<-90 ',y0,' => setting to -180-y0 ',-180.-y0
1037
               print*,'WARNING: y0<-90 ',y0,' => setting to -180-y0 ',-180.-y0
1034
               y0=-180.-y0
1038
               y0=-180.-y0
1035
               x0=x0+per/2.
1039
               x0=x0+per/2.
1036
            endif
1040
            endif
1037
 
1041
 
1038
            ! Get the index where to interpolate (x0,y0,p0)
1042
            ! Get the index where to interpolate (x0,y0,p0)
1039
            if ( (abs(x0-mdv).gt.eps).and. (abs(y0-mdv).gt.eps) ) then
1043
            if ( (abs(x0-mdv).gt.eps).and. (abs(y0-mdv).gt.eps) ) then
1040
               call get_index4 (xind,yind,pind,x0,y0,p0,reltpos0, &
1044
               call get_index4 (xind,yind,pind,x0,y0,p0,reltpos0, &
1041
                    p3t0,p3t1,spt0,spt1,3,nx,ny,nz,xmin,ymin,dx,dy,mdv)
1045
                    p3t0,p3t1,spt0,spt1,3,nx,ny,nz,xmin,ymin,dx,dy,mdv)
1042
            else
1046
            else
1043
               xind = mdv
1047
               xind = mdv
1044
               yind = mdv
1048
               yind = mdv
1045
               pind = mdv
1049
               pind = mdv
1046
            endif
1050
            endif
1047
 
1051
 
1048
           ! Check if point is within grid (keep indices if ok)
1052
           ! Check if point is within grid (keep indices if ok)
1049
            if ( (xind.ge.1.).and.(xind.le.real(nx)).and. &
1053
            if ( (xind.ge.1.).and.(xind.le.real(nx)).and. &
1050
                 (yind.ge.1.).and.(yind.le.real(ny)).and. &
1054
                 (yind.ge.1.).and.(yind.le.real(ny)).and. &
1051
                 (pind.ge.1.).and.(pind.le.real(nz)) ) then
1055
                 (pind.ge.1.).and.(pind.le.real(nz)) ) then
1052
		 xind = xind
1056
		 xind = xind
1053
		 yind = yind
1057
		 yind = yind
1054
		 pind = pind
1058
		 pind = pind
1055
 
1059
 
1056
            ! Check if pressure is outside, but rest okay => adjust to lowest or highest level
1060
            ! Check if pressure is outside, but rest okay => adjust to lowest or highest level
1057
            elseif ( (xind.ge.1.).and.(xind.le.real(nx)).and. (yind.ge.1.).and.(yind.le.real(ny)) ) then ! only vertical problem
1061
            elseif ( (xind.ge.1.).and.(xind.le.real(nx)).and. (yind.ge.1.).and.(yind.le.real(ny)) ) then ! only vertical problem
1058
 
1062
 
1059
               if ( pind.gt.nz ) then ! pressure too low, index too high
1063
               if ( pind.gt.nz ) then ! pressure too low, index too high
1060
                 err_c1 = err_c1 + 1
1064
                 err_c1 = err_c1 + 1
1061
                 if ( err_c1.lt.10 ) then
1065
                 if ( err_c1.lt.10 ) then
1062
                    write(*,'(Af5.3A)') ' WARNING: pressure too low (pind = ',pind,') => adjusted to highest level (pind=nz.)'
1066
                    write(*,'(a,f7.3,a)') ' WARNING: pressure too low (pind = ',pind,') => adjusted to highest level (pind=nz.)'
1063
                    print*,'(x0,y0,p0)=',x0,y0,p0
1067
                    print*,'(x0,y0,p0)=',x0,y0,p0
1064
                    pind = real(nz)
1068
                    pind = real(nz)
1065
                 elseif ( err_c1.eq.10 ) then
1069
                 elseif ( err_c1.eq.10 ) then
1066
                    print*,' WARNING: more pressures too low -> adjusted to highest level '
1070
                    print*,' WARNING: more pressures too low -> adjusted to highest level '
1067
                    pind = real(nz)
1071
                    pind = real(nz)
1068
                 else
1072
                 else
1069
                    pind = real(nz)
1073
                    pind = real(nz)
1070
                 endif
1074
                 endif
1071
                 
1075
                 
1072
               elseif (pind.lt.1.) then ! pressure too high, index too low
1076
               elseif (pind.lt.1.) then ! pressure too high, index too low
1073
                 err_c2 = err_c2 + 1
1077
                 err_c2 = err_c2 + 1
1074
                 if ( err_c2.lt.10 ) then
1078
                 if ( err_c2.lt.10 ) then
1075
                    write(*,'(Af5.3A)') ' WARNING: pressure too high (pind = ',pind,') => adjusted to lowest level, (pind=1.)'
1079
                    write(*,'(a,f7.3,a)') ' WARNING: pressure too high (pind = ',pind,') => adjusted to lowest level, (pind=1.)'
1076
                    print*,'(x0,y0,p0)=',x0,y0,p0
1080
                    print*,'(x0,y0,p0)=',x0,y0,p0
1077
                    pind = 1.
1081
                    pind = 1.
1078
                 elseif ( err_c2.eq.10 ) then
1082
                 elseif ( err_c2.eq.10 ) then
1079
                    print*,' WARNING: more pressures too high -> adjusted to lowest level '
1083
                    print*,' WARNING: more pressures too high -> adjusted to lowest level '
1080
                    pind = 1.
1084
                    pind = 1.
1081
                 else
1085
                 else
1082
                    pind = 1.
1086
                    pind = 1.
1083
                 endif
1087
                 endif
1084
 
1088
 
1085
              endif
1089
              endif
1086
 
1090
 
1087
            ! Grid point is outside!
1091
            ! Grid point is outside!
1088
            else
1092
            else
1089
               
1093
               
1090
               err_c3 = err_c3 + 1
1094
               err_c3 = err_c3 + 1
1091
               if ( err_c3.lt.10 ) then
1095
               if ( err_c3.lt.10 ) then
1092
                  print*,'ERROR: point is outside grid (horizontally)'
1096
                  print*,'ERROR: point is outside grid (horizontally)'
1093
                  print*,'   Trajectory # ',k
1097
                  print*,'   Trajectory # ',k
1094
                  print*,'   Position     ',x0,y0,p0
1098
                  print*,'   Position     ',x0,y0,p0
1095
                  print*,'  (xind,yind):  ',xind,yind
1099
                  print*,'  (xind,yind):  ',xind,yind
1096
                  xind          = mdv
1100
                  xind          = mdv
1097
                  yind          = mdv
1101
                  yind          = mdv
1098
                  pind          = mdv
1102
                  pind          = mdv
1099
                  traint(k,j,2) = mdv  
1103
                  traint(k,j,2) = mdv  
1100
                  traint(k,j,3) = mdv  
1104
                  traint(k,j,3) = mdv  
1101
                  traint(k,j,4) = mdv  
1105
                  traint(k,j,4) = mdv  
1102
               elseif ( err_c3.eq.10 ) then
1106
               elseif ( err_c3.eq.10 ) then
1103
                  print*,'ERROR: more points outside grid (horizontally)'
1107
                  print*,'ERROR: more points outside grid (horizontally)'
1104
                  xind          = mdv
1108
                  xind          = mdv
1105
                  yind          = mdv
1109
                  yind          = mdv
1106
                  pind          = mdv
1110
                  pind          = mdv
1107
                  traint(k,j,2) = mdv  
1111
                  traint(k,j,2) = mdv  
1108
                  traint(k,j,3) = mdv  
1112
                  traint(k,j,3) = mdv  
1109
                  traint(k,j,4) = mdv  
1113
                  traint(k,j,4) = mdv  
1110
               else
1114
               else
1111
                  xind          = mdv
1115
                  xind          = mdv
1112
                  yind          = mdv
1116
                  yind          = mdv
1113
                  pind          = mdv
1117
                  pind          = mdv
1114
                  traint(k,j,2) = mdv  
1118
                  traint(k,j,2) = mdv  
1115
                  traint(k,j,3) = mdv  
1119
                  traint(k,j,3) = mdv  
1116
                  traint(k,j,4) = mdv  
1120
                  traint(k,j,4) = mdv  
1117
               endif
1121
               endif
1118
 
1122
 
1119
            endif
1123
            endif
1120
 
1124
 
1121
            ! ------------------------ NEAREST mode ------------------------------- 
1125
            ! ------------------------ NEAREST mode ------------------------------- 
1122
            ! Interpolate to nearest grid point
1126
            ! Interpolate to nearest grid point
1123
            if ( intmode.eq.'nearest') then
1127
            if ( intmode.eq.'nearest') then
1124
 
1128
 
1125
               xind = real( nint(xind) )
1129
               xind = real( nint(xind) )
1126
               yind = real( nint(yind) )
1130
               yind = real( nint(yind) )
1127
               pind = real( nint(pind) )
1131
               pind = real( nint(pind) )
1128
 
1132
 
1129
               if ( xind.lt.1.  ) xind = 1.
1133
               if ( xind.lt.1.  ) xind = 1.
1130
               if ( xind.gt.nx  ) xind = real(nx)
1134
               if ( xind.gt.nx  ) xind = real(nx)
1131
               if ( yind.lt.1.  ) yind = 1.
1135
               if ( yind.lt.1.  ) yind = 1.
1132
               if ( yind.gt.ny  ) yind = real(ny)
1136
               if ( yind.gt.ny  ) yind = real(ny)
1133
 
1137
 
1134
               if ( pind.lt.1.  ) pind = 1.
1138
               if ( pind.lt.1.  ) pind = 1.
1135
               if ( pind.gt.nz  ) pind = real(nz)
1139
               if ( pind.gt.nz  ) pind = real(nz)
1136
 
1140
 
1137
               if ( abs(reltpos0).ge.eps ) then
1141
               if ( abs(reltpos0).ge.eps ) then
1138
                  print*,'ERROR (nearest): reltpos != 0',reltpos0
1142
                  print*,'ERROR (nearest): reltpos != 0',reltpos0
1139
                  stop
1143
                  stop
1140
               endif
1144
               endif
1141
 
1145
 
1142
               ! interpolate
1146
               ! interpolate
1143
               f0 = int_index4(f3t0,f3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
1147
               f0 = int_index4(f3t0,f3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
1144
 
1148
 
1145
            ! ------------------------ end NEAREST mode ------------------------------- 
1149
            ! ------------------------ end NEAREST mode ------------------------------- 
1146
 
1150
 
1147
            ! ------------------------ CLUSTERING mode ------------------------------- Bojan
1151
            ! ------------------------ CLUSTERING mode ------------------------------- Bojan
1148
            elseif (intmode.eq.'clustering') then
1152
            elseif (intmode.eq.'clustering') then
1149
               if (varname.ne.'LABEL' ) then
1153
               if (varname.ne.'LABEL' ) then
1150
                  print*,'ERROR (clustering): varname is not LABEL'
1154
                  print*,'ERROR (clustering): varname is not LABEL'
1151
                  stop
1155
                  stop
1152
               endif
1156
               endif
1153
 
1157
 
1154
            ! Get indices of box around the point
1158
            ! Get indices of box around the point
1155
            xindb=floor(xind)
1159
            xindb=floor(xind)
1156
            xindf=ceiling(xind)
1160
            xindf=ceiling(xind)
1157
            yindb=floor(yind)
1161
            yindb=floor(yind)
1158
            yindf=ceiling(yind)
1162
            yindf=ceiling(yind)
1159
            pindb=floor(pind)
1163
            pindb=floor(pind)
1160
            pindf=ceiling(pind)
1164
            pindf=ceiling(pind)
1161
 
1165
 
1162
            ! Make sure all points are within grid
1166
            ! Make sure all points are within grid
1163
            if ( xindb.lt.1 ) xindb = 1
1167
            if ( xindb.lt.1 ) xindb = 1
1164
            if ( xindf.lt.1 ) xindf = 1
1168
            if ( xindf.lt.1 ) xindf = 1
1165
            if ( xindb.gt.nx ) xindb = nx
1169
            if ( xindb.gt.nx ) xindb = nx
1166
            if ( xindf.gt.nx ) xindf = nx
1170
            if ( xindf.gt.nx ) xindf = nx
1167
            if ( yindb.lt.1 ) yindb = 1
1171
            if ( yindb.lt.1 ) yindb = 1
1168
            if ( yindf.lt.1 ) yindf = 1
1172
            if ( yindf.lt.1 ) yindf = 1
1169
            if ( yindb.gt.ny ) yindb = ny
1173
            if ( yindb.gt.ny ) yindb = ny
1170
            if ( yindf.gt.ny ) yindf = ny
1174
            if ( yindf.gt.ny ) yindf = ny
1171
            if ( pindb.lt.1 ) pindb = 1
1175
            if ( pindb.lt.1 ) pindb = 1
1172
            if ( pindf.lt.1 ) pindf = 1
1176
            if ( pindf.lt.1 ) pindf = 1
1173
            if ( pindb.gt.nz ) pindb = nz
1177
            if ( pindb.gt.nz ) pindb = nz
1174
            if ( pindf.gt.nz ) pindf = nz
1178
            if ( pindf.gt.nz ) pindf = nz
1175
 
1179
 
1176
            ! Shift one point if they are equal
1180
            ! Shift one point if they are equal
1177
            if ( xindb.eq.xindf ) then
1181
            if ( xindb.eq.xindf ) then
1178
               if ( xindf.eq.nx ) then
1182
               if ( xindf.eq.nx ) then
1179
                  xindb=nx-1
1183
                  xindb=nx-1
1180
               else
1184
               else
1181
                  xindf=xindb+1
1185
                  xindf=xindb+1
1182
               endif
1186
               endif
1183
            endif
1187
            endif
1184
            if ( yindb.eq.yindf ) then
1188
            if ( yindb.eq.yindf ) then
1185
               if ( yindf.eq.ny ) then
1189
               if ( yindf.eq.ny ) then
1186
                  yindb=ny-1
1190
                  yindb=ny-1
1187
               else
1191
               else
1188
                  yindf=yindb+1
1192
                  yindf=yindb+1
1189
               endif
1193
               endif
1190
            endif
1194
            endif
1191
            if ( pindb.eq.pindf ) then
1195
            if ( pindb.eq.pindf ) then
1192
               if ( pindf.eq.nz ) then
1196
               if ( pindf.eq.nz ) then
1193
                  pindb=nz-1
1197
                  pindb=nz-1
1194
               else
1198
               else
1195
                  pindf=pindb+1
1199
                  pindf=pindb+1
1196
               endif
1200
               endif
1197
            endif
1201
            endif
1198
            ! Give warnings and stop if problems occur
1202
            ! Give warnings and stop if problems occur
1199
            if ( xindb.eq.xindf ) then
1203
            if ( xindb.eq.xindf ) then
1200
               print*,'ERROR (clustering): xindb=xindf'
1204
               print*,'ERROR (clustering): xindb=xindf'
1201
               print*,xind,xindb,xindf
1205
               print*,xind,xindb,xindf
1202
               stop
1206
               stop
1203
            endif
1207
            endif
1204
            if ( yindb.eq.yindf ) then
1208
            if ( yindb.eq.yindf ) then
1205
               print*,'ERROR (clustering): yindb=yindf'
1209
               print*,'ERROR (clustering): yindb=yindf'
1206
               print*,yind,yindb,yindf
1210
               print*,yind,yindb,yindf
1207
               stop
1211
               stop
1208
            endif
1212
            endif
1209
            if ( pindb.eq.pindf ) then
1213
            if ( pindb.eq.pindf ) then
1210
               print*,'ERROR (clustering): pindb=pindf'
1214
               print*,'ERROR (clustering): pindb=pindf'
1211
               print*,pind,pindb,pindf
1215
               print*,pind,pindb,pindf
1212
               stop
1216
               stop
1213
            endif
1217
            endif
1214
            if ( ( xindb.lt.1 ).or.( xindf.gt.nx ) ) then
1218
            if ( ( xindb.lt.1 ).or.( xindf.gt.nx ) ) then
1215
               print*,'ERROR (clustering): xindb/f outside'
1219
               print*,'ERROR (clustering): xindb/f outside'
1216
               print*,xind,xindb,xindf
1220
               print*,xind,xindb,xindf
1217
               stop
1221
               stop
1218
            endif
1222
            endif
1219
            if ( ( yindb.lt.1 ).or.( yindf.gt.ny ) ) then
1223
            if ( ( yindb.lt.1 ).or.( yindf.gt.ny ) ) then
1220
               print*,'ERROR (clustering): yindb/f outside'
1224
               print*,'ERROR (clustering): yindb/f outside'
1221
               print*,yind,yindb,yindf
1225
               print*,yind,yindb,yindf
1222
               stop
1226
               stop
1223
            endif
1227
            endif
1224
            if ( ( pindb.lt.1 ).or.( pindf.gt.nz ) ) then
1228
            if ( ( pindb.lt.1 ).or.( pindf.gt.nz ) ) then
1225
               print*,'ERROR (clustering): pindb/f outside'
1229
               print*,'ERROR (clustering): pindb/f outside'
1226
               print*,pind,pindb,pindf
1230
               print*,pind,pindb,pindf
1227
               stop
1231
               stop
1228
            endif
1232
            endif
1229
            if ( abs(reltpos0).ge.eps ) then
1233
            if ( abs(reltpos0).ge.eps ) then
1230
               print*,'ERROR (clustering): reltpos != 0',reltpos0
1234
               print*,'ERROR (clustering): reltpos != 0',reltpos0
1231
               stop
1235
               stop
1232
            endif
1236
            endif
1233
 
1237
 
1234
            ! Get Value in Box
1238
            ! Get Value in Box
1235
            lblcount=(/0,0,0,0,0/)
1239
            lblcount=(/0,0,0,0,0/)
1236
 
1240
 
1237
            lbl(1) = f3t0( xindb + nx*(yindb-1) + nx*ny*(pindb-1) )
1241
            lbl(1) = f3t0( xindb + nx*(yindb-1) + nx*ny*(pindb-1) )
1238
            lbl(2) = f3t0( xindf + nx*(yindb-1) + nx*ny*(pindb-1) )
1242
            lbl(2) = f3t0( xindf + nx*(yindb-1) + nx*ny*(pindb-1) )
1239
            lbl(3) = f3t0( xindb + nx*(yindf-1) + nx*ny*(pindb-1) )
1243
            lbl(3) = f3t0( xindb + nx*(yindf-1) + nx*ny*(pindb-1) )
1240
            lbl(4) = f3t0( xindf + nx*(yindf-1) + nx*ny*(pindb-1) )
1244
            lbl(4) = f3t0( xindf + nx*(yindf-1) + nx*ny*(pindb-1) )
1241
            lbl(5) = f3t0( xindb + nx*(yindb-1) + nx*ny*(pindf-1) )
1245
            lbl(5) = f3t0( xindb + nx*(yindb-1) + nx*ny*(pindf-1) )
1242
            lbl(6) = f3t0( xindf + nx*(yindb-1) + nx*ny*(pindf-1) )
1246
            lbl(6) = f3t0( xindf + nx*(yindb-1) + nx*ny*(pindf-1) )
1243
            lbl(7) = f3t0( xindb + nx*(yindf-1) + nx*ny*(pindf-1) )
1247
            lbl(7) = f3t0( xindb + nx*(yindf-1) + nx*ny*(pindf-1) )
1244
            lbl(8) = f3t0( xindf + nx*(yindf-1) + nx*ny*(pindf-1) )
1248
            lbl(8) = f3t0( xindf + nx*(yindf-1) + nx*ny*(pindf-1) )
1245
 
1249
 
1246
            ! Count the number of times every label appears
1250
            ! Count the number of times every label appears
1247
            do lci=1,5
1251
            do lci=1,5
1248
               do lcj=1,8
1252
               do lcj=1,8
1249
                  if ( abs(lbl(lcj)-lci).lt.eps ) then
1253
                  if ( abs(lbl(lcj)-lci).lt.eps ) then
1250
                     lblcount(lci)=lblcount(lci)+1
1254
                     lblcount(lci)=lblcount(lci)+1
1251
                  endif
1255
                  endif
1252
               enddo
1256
               enddo
1253
            enddo
1257
            enddo
1254
 
1258
 
1255
            ! Set to -9 to detect if no label was assigned in the end
1259
            ! Set to -9 to detect if no label was assigned in the end
1256
            f0=-9
1260
            f0=-9
1257
 
1261
 
1258
            ! Stratosphere (PV)
1262
            ! Stratosphere (PV)
1259
            if ( abs(traint(k,j,pvpos)) .ge. tropo_pv ) then
1263
            if ( abs(traint(k,j,pvpos)) .ge. tropo_pv ) then
1260
               if ( (lblcount(2).ge.lblcount(3)).and. (lblcount(2).ge.lblcount(5)) ) then
1264
               if ( (lblcount(2).ge.lblcount(3)).and. (lblcount(2).ge.lblcount(5)) ) then
1261
                  f0=2
1265
                  f0=2
1262
               elseif ( lblcount(3).ge.lblcount(5) ) then
1266
               elseif ( lblcount(3).ge.lblcount(5) ) then
1263
                  f0=3
1267
                  f0=3
1264
               elseif ( lblcount(5).gt.lblcount(3) ) then
1268
               elseif ( lblcount(5).gt.lblcount(3) ) then
1265
                  f0=5
1269
                  f0=5
1266
               endif
1270
               endif
1267
            endif
1271
            endif
1268
 
1272
 
1269
            ! Troposphere (PV)
1273
            ! Troposphere (PV)
1270
            if ( abs(traint(k,j,pvpos)) .lt. tropo_pv ) then
1274
            if ( abs(traint(k,j,pvpos)) .lt. tropo_pv ) then
1271
               if ( lblcount(1).ge.lblcount(4) ) then
1275
               if ( lblcount(1).ge.lblcount(4) ) then
1272
               f0=1
1276
               f0=1
1273
               elseif ( lblcount(4).gt.lblcount(1) ) then
1277
               elseif ( lblcount(4).gt.lblcount(1) ) then
1274
               f0=4
1278
               f0=4
1275
               endif
1279
               endif
1276
            endif
1280
            endif
1277
 
1281
 
1278
            ! Stratosphere (TH)
1282
            ! Stratosphere (TH)
1279
            if ( traint(k,j,thpos) .ge. tropo_th ) then
1283
            if ( traint(k,j,thpos) .ge. tropo_th ) then
1280
               f0=2
1284
               f0=2
1281
            endif
1285
            endif
1282
 
1286
 
1283
            if (f0.eq.-9) then
1287
            if (f0.eq.-9) then
1284
               print*,'ERROR (Clustering): No label assigned!'
1288
               print*,'ERROR (Clustering): No label assigned!'
1285
               stop
1289
               stop
1286
            endif
1290
            endif
1287
            ! ------------------------ end CLUSTERING mode -------------------------------
1291
            ! ------------------------ end CLUSTERING mode -------------------------------
1288
 
1292
 
1289
            ! ------------------------ CIRCLE modes ------------------------------- Bojan
1293
            ! ------------------------ CIRCLE modes ------------------------------- Bojan
1290
            ! elseif (not clustering but one of the possible circle modes)
1294
            ! elseif (not clustering but one of the possible circle modes)
1291
            elseif ( (intmode.eq.'circle_avg') .or. (intmode.eq.'circle_min') .or. (intmode.eq.'circle_max') ) then
1295
            elseif ( (intmode.eq.'circle_avg') .or. (intmode.eq.'circle_min') .or. (intmode.eq.'circle_max') ) then
1292
 
1296
 
1293
            ! reset arrays for this point
1297
            ! reset arrays for this point
1294
            connect=0
1298
            connect=0
1295
            stackx=0
1299
            stackx=0
1296
            stacky=0
1300
            stacky=0
1297
            circlelon=0
1301
            circlelon=0
1298
            circlelat=0
1302
            circlelat=0
1299
            circlef=0
1303
            circlef=0
1300
            circlearea=0
1304
            circlearea=0
1301
 
1305
 
1302
            ! Get indices of one coarse grid point within search radius (nint=round to next integer)
1306
            ! Get indices of one coarse grid point within search radius (nint=round to next integer)
1303
            if ( sdis(x0,y0,longrid(nint(xind)),latgrid(nint(yind))) .gt. radius) then
1307
            if ( sdis(x0,y0,longrid(nint(xind)),latgrid(nint(yind))) .gt. radius) then
1304
               print*,'ERROR (circle): Search radius is too small... (1). r =',radius
1308
               print*,'ERROR (circle): Search radius is too small... (1). r =',radius
1305
               print*,'Distance to nint grid point (minimum search radius)=',sdis(x0,y0,longrid(nint(xind)),latgrid(nint(yind)))
1309
               print*,'Distance to nint grid point (minimum search radius)=',sdis(x0,y0,longrid(nint(xind)),latgrid(nint(yind)))
1306
               stop
1310
               stop
1307
            endif
1311
            endif
1308
 
1312
 
1309
            ! Initialize stack with nint(xind),nint(yind)
1313
            ! Initialize stack with nint(xind),nint(yind)
1310
            kst=0 ! counts the number of points in circle
1314
            kst=0 ! counts the number of points in circle
1311
            stackx(1)=nint(xind)
1315
            stackx(1)=nint(xind)
1312
            stacky(1)=nint(yind)
1316
            stacky(1)=nint(yind)
1313
            sp=1 ! stack counter
1317
            sp=1 ! stack counter
1314
            do while (sp.ne.0)
1318
            do while (sp.ne.0)
1315
 
1319
 
1316
            ! Get an element from stack
1320
            ! Get an element from stack
1317
             ist=stackx(sp)
1321
             ist=stackx(sp)
1318
             jst=stacky(sp)
1322
             jst=stacky(sp)
1319
             sp=sp-1
1323
             sp=sp-1
1320
 
1324
 
1321
            ! Get distance from reference point
1325
            ! Get distance from reference point
1322
             dist=sdis(x0,y0,longrid(ist),latgrid(jst))
1326
             dist=sdis(x0,y0,longrid(ist),latgrid(jst))
1323
 
1327
 
1324
            ! Check whether distance is smaller than search radius: connected
1328
            ! Check whether distance is smaller than search radius: connected
1325
             if (dist.lt.radius) then
1329
             if (dist.lt.radius) then
1326
 
1330
 
1327
            ! Increase total stack index
1331
            ! Increase total stack index
1328
              kst=kst+1
1332
              kst=kst+1
1329
              circlelon(kst)=longrid(ist)
1333
              circlelon(kst)=longrid(ist)
1330
              circlelat(kst)=latgrid(jst)
1334
              circlelat(kst)=latgrid(jst)
1331
  
1335
  
1332
            ! Interpolate field to position of point (interpolation in time!) 
1336
            ! Interpolate field to position of point (interpolation in time!) 
1333
              circlef(kst) = int_index4(f3t0,f3t1,nx,ny,nz,real(ist),real(jst),pind,reltpos0,mdv)
1337
              circlef(kst) = int_index4(f3t0,f3t1,nx,ny,nz,real(ist),real(jst),pind,reltpos0,mdv)
1334
 
1338
 
1335
            ! Calculate area of point (for circle_avg mode only)
1339
            ! Calculate area of point (for circle_avg mode only)
1336
              if ( intmode .eq. 'circle_avg' ) then
1340
              if ( intmode .eq. 'circle_avg' ) then
1337
                 circlearea(kst) = pir/(nx-1)*(sin(pi180*abs(circlelat(kst)))-sin(pi180*(abs(circlelat(kst))-dy)))
1341
                 circlearea(kst) = pir/(nx-1)*(sin(pi180*abs(circlelat(kst)))-sin(pi180*(abs(circlelat(kst))-dy)))
1338
              endif
1342
              endif
1339
 
1343
 
1340
            ! Mark this point as visited
1344
            ! Mark this point as visited
1341
              connect(ist,jst)=1
1345
              connect(ist,jst)=1
1342
 
1346
 
1343
            ! Get coordinates of neighbouring points and implement periodicity
1347
            ! Get coordinates of neighbouring points and implement periodicity
1344
              mr=ist+1
1348
              mr=ist+1
1345
              if (mr.gt.nx) mr=1
1349
              if (mr.gt.nx) mr=1
1346
              ml=ist-1
1350
              ml=ist-1
1347
              if (ml.lt.1) ml=nx
1351
              if (ml.lt.1) ml=nx
1348
              nu=jst+1
1352
              nu=jst+1
1349
              if (nu.gt.ny) nu=ny
1353
              if (nu.gt.ny) nu=ny
1350
              nd=jst-1
1354
              nd=jst-1
1351
              if (nd.lt.1) nd=1
1355
              if (nd.lt.1) nd=1
1352
 
1356
 
1353
            ! Update stack with neighbouring points
1357
            ! Update stack with neighbouring points
1354
              if (connect(mr,jst).ne. 1) then
1358
              if (connect(mr,jst).ne. 1) then
1355
                 connect(mr,jst)=1
1359
                 connect(mr,jst)=1
1356
                 sp=sp+1
1360
                 sp=sp+1
1357
                 stackx(sp)=mr
1361
                 stackx(sp)=mr
1358
                 stacky(sp)=jst
1362
                 stacky(sp)=jst
1359
              endif
1363
              endif
1360
              if (connect(ml,jst).ne. 1) then
1364
              if (connect(ml,jst).ne. 1) then
1361
                 connect(ml,jst)=1
1365
                 connect(ml,jst)=1
1362
                 sp=sp+1
1366
                 sp=sp+1
1363
                 stackx(sp)=ml
1367
                 stackx(sp)=ml
1364
                 stacky(sp)=jst
1368
                 stacky(sp)=jst
1365
              endif
1369
              endif
1366
              if (connect(ist,nd).ne. 1) then
1370
              if (connect(ist,nd).ne. 1) then
1367
                 connect(ist,nd)=1
1371
                 connect(ist,nd)=1
1368
                 sp=sp+1
1372
                 sp=sp+1
1369
                 stackx(sp)=ist
1373
                 stackx(sp)=ist
1370
                 stacky(sp)=nd
1374
                 stacky(sp)=nd
1371
              endif
1375
              endif
1372
              if (connect(ist,nu).ne. 1) then
1376
              if (connect(ist,nu).ne. 1) then
1373
                 connect(ist,nu)=1
1377
                 connect(ist,nu)=1
1374
                 sp=sp+1
1378
                 sp=sp+1
1375
                 stackx(sp)=ist
1379
                 stackx(sp)=ist
1376
                 stacky(sp)=nu
1380
                 stacky(sp)=nu
1377
              endif
1381
              endif
1378
 
1382
 
1379
             endif ! endif radius is smaller => end of updating stack
1383
             endif ! endif radius is smaller => end of updating stack
1380
 
1384
 
1381
            end do ! end working on stack 
1385
            end do ! end working on stack 
1382
 
1386
 
1383
            if (kst.ge.1) then
1387
            if (kst.ge.1) then
1384
               ! Choose output depending on intmode
1388
               ! Choose output depending on intmode
1385
               if ( intmode .eq. 'circle_avg' ) then
1389
               if ( intmode .eq. 'circle_avg' ) then
1386
                  ! calculate area-weighted average of f in circle
1390
                  ! calculate area-weighted average of f in circle
1387
                  circlesum=0.
1391
                  circlesum=0.
1388
                  do l=1,kst
1392
                  do l=1,kst
1389
                     circlesum=circlesum+circlef(l)*circlearea(l)
1393
                     circlesum=circlesum+circlef(l)*circlearea(l)
1390
                  enddo
1394
                  enddo
1391
                  circleavg=circlesum/sum(circlearea(1:kst))
1395
                  circleavg=circlesum/sum(circlearea(1:kst))
1392
                  !print*,'area-weighted average of f in circle=',circleavg
1396
                  !print*,'area-weighted average of f in circle=',circleavg
1393
                  f0=circleavg
1397
                  f0=circleavg
1394
               elseif ( intmode .eq. 'circle_min' ) then
1398
               elseif ( intmode .eq. 'circle_min' ) then
1395
                  ! calculate minimum in circle
1399
                  ! calculate minimum in circle
1396
                  circlemin=circlef(1)
1400
                  circlemin=circlef(1)
1397
                  do l=1,kst
1401
                  do l=1,kst
1398
                     if (circlef(l) .lt. circlemin) then
1402
                     if (circlef(l) .lt. circlemin) then
1399
                        circlemin=circlef(l)
1403
                        circlemin=circlef(l)
1400
                     endif
1404
                     endif
1401
                  enddo
1405
                  enddo
1402
                  !print*,'minimum of f in circle=',circlemin       
1406
                  !print*,'minimum of f in circle=',circlemin       
1403
                  f0=circlemin
1407
                  f0=circlemin
1404
               elseif ( intmode .eq. 'circle_max' ) then             
1408
               elseif ( intmode .eq. 'circle_max' ) then             
1405
                  ! calculate maximum in circle
1409
                  ! calculate maximum in circle
1406
                  circlemax=circlef(1)
1410
                  circlemax=circlef(1)
1407
                  do l=1,kst
1411
                  do l=1,kst
1408
                     if (circlef(l) .gt. circlemax) then
1412
                     if (circlef(l) .gt. circlemax) then
1409
                        circlemax=circlef(l)
1413
                        circlemax=circlef(l)
1410
                     endif
1414
                     endif
1411
                  enddo
1415
                  enddo
1412
                  !print*,'maximum of f in circle=',circlemax
1416
                  !print*,'maximum of f in circle=',circlemax
1413
                  f0=circlemax
1417
                  f0=circlemax
1414
               else 
1418
               else 
1415
                  print*,'ERROR (circle): intmode not valid!'
1419
                  print*,'ERROR (circle): intmode not valid!'
1416
                  stop
1420
                  stop
1417
               endif
1421
               endif
1418
            else
1422
            else
1419
               print*,'ERROR (circle): Search radius is too small... (2). r =',radius
1423
               print*,'ERROR (circle): Search radius is too small... (2). r =',radius
1420
               stop
1424
               stop
1421
            endif
1425
            endif
1422
 
1426
 
1423
            ! ------------------------ end CIRCLE modes -------------------------------
1427
            ! ------------------------ end CIRCLE modes -------------------------------
1424
 
1428
 
1425
            ! ------------------------ NORMAL mode -------------------------------
1429
            ! ------------------------ NORMAL mode -------------------------------
1426
            else ! not clustering nor circle: NORMAL mode
1430
            else ! not clustering nor circle: NORMAL mode
1427
 
1431
 
1428
            ! Check if point is within grid
1432
            ! Check if point is within grid
1429
!            if ( (xind.ge.1.).and.(xind.le.real(nx)).and. &
1433
!            if ( (xind.ge.1.).and.(xind.le.real(nx)).and. &
1430
!                 (yind.ge.1.).and.(yind.le.real(ny)).and. &
1434
!                 (yind.ge.1.).and.(yind.le.real(ny)).and. &
1431
!                 (pind.ge.1.).and.(pind.le.real(nz)) ) then
1435
!                 (pind.ge.1.).and.(pind.le.real(nz)) ) then
1432
!
1436
!
1433
            ! Do the interpolation: everthing is ok
1437
            ! Do the interpolation: everthing is ok
-
 
1438
            if ( ( shift_dir(i).ne.'PMIN' ).and.( shift_dir(i).ne.'PMAX' ) ) then
1434
               f0 = int_index4(f3t0,f3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
1439
               f0 = int_index4(f3t0,f3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
-
 
1440
               
-
 
1441
            elseif  ( shift_dir(i).eq.'PMIN' ) then
-
 
1442
               f0 = int_index4(f3t0,f3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
-
 
1443
               
-
 
1444
               ! Handle > and < operator
-
 
1445
               if ( (shift_rel(i).eq.'>').and.(f0.gt.shift_val(i)) ) then
-
 
1446
                 do while ( (f0.gt.shift_val(i)).and.(pind.lt.nz) )
-
 
1447
                     pind = pind + 0.1
-
 
1448
                     f0 = int_index4(f3t0,f3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
-
 
1449
                 enddo
-
 
1450
                 f0 = int_index4(p3t0,p3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
-
 
1451
               
-
 
1452
               elseif ( (shift_rel(i).eq.'<').and.(f0.lt.shift_val(i)) ) then
-
 
1453
                 do while ( (f0.lt.shift_val(i)).and.(pind.lt.nz) )
-
 
1454
                     pind = pind + 0.1
-
 
1455
                     f0 = int_index4(f3t0,f3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
-
 
1456
                 enddo
-
 
1457
                 f0 = int_index4(p3t0,p3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
-
 
1458
                 
-
 
1459
              else
-
 
1460
                 f0 = mdv
-
 
1461
              endif
-
 
1462
              
-
 
1463
              
-
 
1464
           elseif  ( shift_dir(i).eq.'PMAX' ) then
-
 
1465
               f0 = int_index4(f3t0,f3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
-
 
1466
               
-
 
1467
               ! Handle > and < operator
-
 
1468
               if ( (shift_rel(i).eq.'>').and.(f0.gt.shift_val(i)) ) then
-
 
1469
                 do while ( (f0.gt.shift_val(i)).and.(pind.gt.1) )
-
 
1470
                     pind = pind - 0.1
-
 
1471
                     f0 = int_index4(f3t0,f3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
-
 
1472
                 enddo
-
 
1473
                 f0 = int_index4(p3t0,p3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
-
 
1474
              
-
 
1475
               elseif ( (shift_rel(i).eq.'<').and.(f0.lt.shift_val(i)) ) then
-
 
1476
                 do while ( (f0.lt.shift_val(i)).and.(pind.gt.1) )
-
 
1477
                     pind = pind - 0.1
-
 
1478
                     f0 = int_index4(f3t0,f3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
-
 
1479
                 enddo
-
 
1480
                 f0 = int_index4(p3t0,p3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
-
 
1481
              
-
 
1482
              else
-
 
1483
                 f0 = mdv
-
 
1484
              endif
-
 
1485
               
-
 
1486
          endif  
1435
 
1487
 
1436
!            ! Check if pressure is outside, but rest okay: adjust to lowest or highest level
1488
!            ! Check if pressure is outside, but rest okay: adjust to lowest or highest level
1437
!            elseif ( (xind.ge.1.).and.(xind.le.real(nx)).and. (yind.ge.1.).and.(yind.le.real(ny)) ) then ! only vertical problem
1489
!            elseif ( (xind.ge.1.).and.(xind.le.real(nx)).and. (yind.ge.1.).and.(yind.le.real(ny)) ) then ! only vertical problem
1438
!               if ( pind.gt.nz ) then ! pressure too low, index too high
1490
!               if ( pind.gt.nz ) then ! pressure too low, index too high
1439
!                 pind = real(nz)
1491
!                 pind = real(nz)
1440
!                 print*,' Warning: pressure too low -> adjusted to highest level, pind=nz.'
1492
!                 print*,' Warning: pressure too low -> adjusted to highest level, pind=nz.'
1441
!                 print*,'(x0,y0,p0)=',x0,y0,p0
1493
!                 print*,'(x0,y0,p0)=',x0,y0,p0
1442
!               elseif (pind.lt.1.) then ! pressure too high, index too low
1494
!               elseif (pind.lt.1.) then ! pressure too high, index too low
1443
!                 pind = 1.
1495
!                 pind = 1.
1444
!                 print*,' Warning: pressure too high -> adjusted to lowest level, pind=1.'
1496
!                 print*,' Warning: pressure too high -> adjusted to lowest level, pind=1.'
1445
!                 print*,'(x0,y0,p0)=',x0,y0,p0
1497
!                 print*,'(x0,y0,p0)=',x0,y0,p0
1446
!              endif
1498
!              endif
1447
!              f0 = int_index4(f3t0,f3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
1499
!              f0 = int_index4(f3t0,f3t1,nx,ny,nz,xind,yind,pind,reltpos0,mdv)
1448
 
1500
 
1449
!            ! Less than 10 outside
1501
!            ! Less than 10 outside
1450
!            elseif (noutside.lt.10) then
1502
!            elseif (noutside.lt.10) then
1451
!               print*,' ',trim(tvar(i)),' @ ',x0,y0,p0,'outside'
1503
!               print*,' ',trim(tvar(i)),' @ ',x0,y0,p0,'outside'
1452
!               f0       = mdv
1504
!               f0       = mdv
1453
!               noutside = noutside + 1
1505
!               noutside = noutside + 1
1454
!
1506
!
1455
!            ! More than 10 outside
1507
!            ! More than 10 outside
1456
!            elseif (noutside.eq.10) then
1508
!            elseif (noutside.eq.10) then
1457
!               print*,' ...more than 10 outside...'
1509
!               print*,' ...more than 10 outside...'
1458
!               f0       = mdv
1510
!               f0       = mdv
1459
!               noutside = noutside + 1
1511
!               noutside = noutside + 1
1460
 
1512
 
1461
!            ! Else (not everything okay and also not 'tolerated cases') set to missing data
1513
!            ! Else (not everything okay and also not 'tolerated cases') set to missing data
1462
!            else
1514
!            else
1463
!               f0       = mdv
1515
!               f0       = mdv
1464
!            endif
1516
!            endif
1465
 
1517
 
1466
            ! ------------------------ end NORMAL mode -------------------------------
1518
            ! ------------------------ end NORMAL mode -------------------------------
1467
            endif ! end if nearest case
1519
            endif ! end if nearest case
1468
 
1520
 
1469
           ! Exit for loop over all trajectories and times -save interpolated value
1521
           ! Exit for loop over all times -save interpolated value
1470
 109        continue
1522
 109        continue
1471
 
1523
 
1472
            ! Save the new field
1524
            ! Save the new field
1473
            if ( abs(f0-mdv).gt.eps) then
1525
            if ( abs(f0-mdv).gt.eps) then
1474
               traint(k,j,ncol+i) = f0
1526
               traint(k,j,ncol+i) = f0
1475
            else
1527
            else
1476
               traint(k,j,ncol+i) = mdv
1528
               traint(k,j,ncol+i) = mdv
1477
            endif
1529
            endif
1478
 
1530
 
1479
         enddo ! end loop over all trajectories
1531
         enddo ! end loop over all trajectories
1480
 
1532
 
1481
      enddo ! end loop over all times
1533
      enddo ! end loop over all times
1482
 
1534
 
1483
      ! Exit point for loop over all tracing variables
1535
      ! Exit point for loop over all tracing variables
1484
 110   continue
1536
 110   continue
1485
 
1537
 
1486
   enddo ! end loop over all variables
1538
   enddo ! end loop over all variables
1487
 
1539
 
1488
  ! --------------------------------------------------------------------
1540
  ! --------------------------------------------------------------------
1489
  ! Calculate additional fields along the trajectories
1541
  ! Calculate additional fields along the trajectories
1490
  ! --------------------------------------------------------------------
1542
  ! --------------------------------------------------------------------
1491
 
1543
 
1492
  print*
1544
  print*
1493
  print*,'---- CALCULATE ADDITIONAL FIELDS FROM TRAJECTORY TABLE --'
1545
  print*,'---- CALCULATE ADDITIONAL FIELDS FROM TRAJECTORY TABLE --'
1494
 
1546
 
1495
  ! Loop over all tracing fields
1547
  ! Loop over all tracing fields
1496
  do i=ntrace1,1,-1
1548
  do i=ntrace1,1,-1
1497
 
1549
 
1498
      ! Skip fields which must not be computed (compfl=0)
1550
      ! Skip fields which must not be computed (compfl=0)
1499
      if (compfl(i).eq.0) goto 120
1551
      if (compfl(i).eq.0) goto 120
1500
 
1552
 
1501
      ! Write some status information
1553
      ! Write some status information
1502
      print*
1554
      print*
1503
      write(*,'(a10,f10.2,a5,i3,3x,a2)') &
1555
      write(*,'(a10,f10.2,a5,i3,3x,a2)') &
1504
           trim(tvar(i)),shift_val(i),trim(shift_dir(i)),compfl(i),trim(tfil(i))
1556
           trim(tvar(i)),shift_val(i),trim(shift_dir(i)),compfl(i),trim(tfil(i))
1505
 
1557
 
1506
      ! Loop over trajectories and times
1558
      ! Loop over trajectories and times
1507
      do j=1,ntra
1559
      do j=1,ntra
1508
      do k=1,ntim
1560
      do k=1,ntim
1509
 
1561
 
1510
         ! Potential temperature (TH)
1562
         ! Potential temperature (TH)
1511
         if  ( varsint(i+ncol).eq.'TH' ) then
1563
         if  ( varsint(i+ncol).eq.'TH' ) then
1512
 
1564
 
1513
            varname='T'
1565
            varname='T'
1514
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1566
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1515
            varname='p'
1567
            varname='p'
1516
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1568
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1517
 
1569
 
1518
            call calc_TH  (traint(j,k,ncol+i), traint(j,k,ind1), &
1570
            call calc_TH  (traint(j,k,ncol+i), traint(j,k,ind1), &
1519
                 traint(j,k,ind2) )
1571
                 traint(j,k,ind2) )
1520
 
1572
 
1521
         ! Density (RHO)
1573
         ! Density (RHO)
1522
         elseif  ( varsint(i+ncol).eq.'RHO' ) then
1574
         elseif  ( varsint(i+ncol).eq.'RHO' ) then
1523
 
1575
 
1524
            varname='T'
1576
            varname='T'
1525
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1577
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1526
            varname='p'
1578
            varname='p'
1527
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1579
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1528
 
1580
 
1529
            call calc_RHO (traint(j,k,ncol+i), traint(j,k,ind1), &
1581
            call calc_RHO (traint(j,k,ncol+i), traint(j,k,ind1), &
1530
                 traint(j,k,ind2) )
1582
                 traint(j,k,ind2) )
1531
 
1583
 
1532
         ! Relative humidity (RH)
1584
         ! Relative humidity (RH)
1533
         elseif  ( varsint(i+ncol).eq.'RH' ) then
1585
         elseif  ( varsint(i+ncol).eq.'RH' ) then
1534
 
1586
 
1535
            varname='T'
1587
            varname='T'
1536
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1588
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1537
            varname='p'
1589
            varname='p'
1538
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1590
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1539
            varname='Q'
1591
            varname='Q'
1540
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1592
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1541
 
1593
 
1542
            call calc_RH (traint(j,k,ncol+i), traint(j,k,ind1), &
1594
            call calc_RH (traint(j,k,ncol+i), traint(j,k,ind1), &
1543
                 traint(j,k,ind2),traint(j,k,ind3) )
1595
                 traint(j,k,ind2),traint(j,k,ind3) )
1544
 
1596
 
1545
         ! Equivalent potential temperature (THE)
1597
         ! Equivalent potential temperature (THE)
1546
         elseif  ( varsint(i+ncol).eq.'THE' ) then
1598
         elseif  ( varsint(i+ncol).eq.'THE' ) then
1547
 
1599
 
1548
            varname='T'
1600
            varname='T'
1549
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1601
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1550
            varname='p'
1602
            varname='p'
1551
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1603
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1552
            varname='Q'
1604
            varname='Q'
1553
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1605
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1554
 
1606
 
1555
            call calc_THE (traint(j,k,ncol+i), traint(j,k,ind1), &
1607
            call calc_THE (traint(j,k,ncol+i), traint(j,k,ind1), &
1556
                 traint(j,k,ind2),traint(j,k,ind3) )
1608
                 traint(j,k,ind2),traint(j,k,ind3) )
1557
 
1609
 
1558
         ! Latent heating rate (LHR)
1610
         ! Latent heating rate (LHR)
1559
         elseif  ( varsint(i+ncol).eq.'LHR' ) then
1611
         elseif  ( varsint(i+ncol).eq.'LHR' ) then
1560
 
1612
 
1561
            varname='T'
1613
            varname='T'
1562
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1614
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1563
            varname='p'
1615
            varname='p'
1564
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1616
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1565
            varname='Q'
1617
            varname='Q'
1566
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1618
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1567
            varname='OMEGA'
1619
            varname='OMEGA'
1568
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1620
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1569
            varname='RH'
1621
            varname='RH'
1570
            call list2ind (ind5,varname,varsint,fok,ncol+ntrace1)
1622
            call list2ind (ind5,varname,varsint,fok,ncol+ntrace1)
1571
 
1623
 
1572
            call calc_LHR (traint(j,k,ncol+i), traint(j,k,ind1), &
1624
            call calc_LHR (traint(j,k,ncol+i), traint(j,k,ind1), &
1573
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4),traint(j,k,ind5) )
1625
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4),traint(j,k,ind5) )
1574
 
1626
 
1575
         ! Wind speed (VEL)
1627
         ! Wind speed (VEL)
1576
         elseif  ( varsint(i+ncol).eq.'VEL' ) then
1628
         elseif  ( varsint(i+ncol).eq.'VEL' ) then
1577
 
1629
 
1578
            varname='U'
1630
            varname='U'
1579
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1631
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1580
            varname='V'
1632
            varname='V'
1581
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1633
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1582
 
1634
 
1583
            call calc_VEL (traint(j,k,ncol+i), traint(j,k,ind1), &
1635
            call calc_VEL (traint(j,k,ncol+i), traint(j,k,ind1), &
1584
                 traint(j,k,ind2) )
1636
                 traint(j,k,ind2) )
1585
 
1637
 
1586
         ! Wind direction (DIR)
1638
         ! Wind direction (DIR)
1587
         elseif  ( varsint(i+ncol).eq.'DIR' ) then
1639
         elseif  ( varsint(i+ncol).eq.'DIR' ) then
1588
 
1640
 
1589
            varname='U'
1641
            varname='U'
1590
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1642
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1591
            varname='V'
1643
            varname='V'
1592
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1644
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1593
 
1645
 
1594
            call calc_DIR (traint(j,k,ncol+i), traint(j,k,ind1), &
1646
            call calc_DIR (traint(j,k,ncol+i), traint(j,k,ind1), &
1595
                 traint(j,k,ind2) )
1647
                 traint(j,k,ind2) )
1596
 
1648
 
1597
         ! Zonal gradient of U (DUDX)
1649
         ! Zonal gradient of U (DUDX)
1598
         elseif  ( varsint(i+ncol).eq.'DUDX' ) then
1650
         elseif  ( varsint(i+ncol).eq.'DUDX' ) then
1599
 
1651
 
1600
            varname='U:+1DLON'
1652
            varname='U:+1DLON'
1601
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1653
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1602
            varname='U:-1DLON'
1654
            varname='U:-1DLON'
1603
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1655
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1604
            varname='lat'
1656
            varname='lat'
1605
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1657
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1606
 
1658
 
1607
            call calc_DUDX (traint(j,k,ncol+i), traint(j,k,ind1), &
1659
            call calc_DUDX (traint(j,k,ncol+i), traint(j,k,ind1), &
1608
                 traint(j,k,ind2),traint(j,k,ind3) )
1660
                 traint(j,k,ind2),traint(j,k,ind3) )
1609
 
1661
 
1610
         ! Zonal gradient of V (DVDX)
1662
         ! Zonal gradient of V (DVDX)
1611
         elseif  ( varsint(i+ncol).eq.'DVDX' ) then
1663
         elseif  ( varsint(i+ncol).eq.'DVDX' ) then
1612
 
1664
 
1613
            varname='V:+1DLON'
1665
            varname='V:+1DLON'
1614
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1666
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1615
            varname='V:-1DLON'
1667
            varname='V:-1DLON'
1616
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1668
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1617
            varname='lat'
1669
            varname='lat'
1618
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1670
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1619
 
1671
 
1620
            call calc_DVDX (traint(j,k,ncol+i), traint(j,k,ind1), &
1672
            call calc_DVDX (traint(j,k,ncol+i), traint(j,k,ind1), &
1621
                 traint(j,k,ind2),traint(j,k,ind3) )
1673
                 traint(j,k,ind2),traint(j,k,ind3) )
1622
 
1674
 
1623
         ! Zonal gradient of T (DTDX)
1675
         ! Zonal gradient of T (DTDX)
1624
         elseif  ( varsint(i+ncol).eq.'DVDX' ) then
1676
         elseif  ( varsint(i+ncol).eq.'DVDX' ) then
1625
 
1677
 
1626
            varname='T:+1DLON'
1678
            varname='T:+1DLON'
1627
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1679
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1628
            varname='T:-1DLON'
1680
            varname='T:-1DLON'
1629
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1681
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1630
            varname='lat'
1682
            varname='lat'
1631
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1683
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1632
 
1684
 
1633
            call calc_DTDX (traint(j,k,ncol+i), traint(j,k,ind1), &
1685
            call calc_DTDX (traint(j,k,ncol+i), traint(j,k,ind1), &
1634
                 traint(j,k,ind2),traint(j,k,ind3) )
1686
                 traint(j,k,ind2),traint(j,k,ind3) )
1635
 
1687
 
1636
         ! Zonal gradient of TH (DTHDX)
1688
         ! Zonal gradient of TH (DTHDX)
1637
         elseif  ( varsint(i+ncol).eq.'DTHDX' ) then
1689
         elseif  ( varsint(i+ncol).eq.'DTHDX' ) then
1638
 
1690
 
1639
            varname='T:+1DLON'
1691
            varname='T:+1DLON'
1640
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1692
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1641
            varname='T:-1DLON'
1693
            varname='T:-1DLON'
1642
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1694
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1643
            varname='P'
1695
            varname='P'
1644
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1696
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1645
            varname='lat'
1697
            varname='lat'
1646
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1698
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1647
 
1699
 
1648
            call calc_DTHDX (traint(j,k,ncol+i), traint(j,k,ind1), &
1700
            call calc_DTHDX (traint(j,k,ncol+i), traint(j,k,ind1), &
1649
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4) )
1701
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4) )
1650
 
1702
 
1651
         ! Meridional gradient of U (DUDY)
1703
         ! Meridional gradient of U (DUDY)
1652
         elseif  ( varsint(i+ncol).eq.'DUDY' ) then
1704
         elseif  ( varsint(i+ncol).eq.'DUDY' ) then
1653
 
1705
 
1654
            varname='U:+1DLAT'
1706
            varname='U:+1DLAT'
1655
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1707
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1656
            varname='U:-1DLAT'
1708
            varname='U:-1DLAT'
1657
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1709
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1658
 
1710
 
1659
            call calc_DUDY (traint(j,k,ncol+i), traint(j,k,ind1), &
1711
            call calc_DUDY (traint(j,k,ncol+i), traint(j,k,ind1), &
1660
                 traint(j,k,ind2) )
1712
                 traint(j,k,ind2) )
1661
 
1713
 
1662
         ! Meridional gradient of V (DVDY)
1714
         ! Meridional gradient of V (DVDY)
1663
         elseif  ( varsint(i+ncol).eq.'DVDY' ) then
1715
         elseif  ( varsint(i+ncol).eq.'DVDY' ) then
1664
 
1716
 
1665
            varname='V:+1DLAT'
1717
            varname='V:+1DLAT'
1666
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1718
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1667
            varname='V:-1DLAT'
1719
            varname='V:-1DLAT'
1668
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1720
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1669
 
1721
 
1670
            call calc_DVDY (traint(j,k,ncol+i), traint(j,k,ind1), &
1722
            call calc_DVDY (traint(j,k,ncol+i), traint(j,k,ind1), &
1671
                 traint(j,k,ind2) )
1723
                 traint(j,k,ind2) )
1672
 
1724
 
1673
         ! Meridional gradient of T (DTDY)
1725
         ! Meridional gradient of T (DTDY)
1674
         elseif  ( varsint(i+ncol).eq.'DTDY' ) then
1726
         elseif  ( varsint(i+ncol).eq.'DTDY' ) then
1675
 
1727
 
1676
            varname='T:+1DLAT'
1728
            varname='T:+1DLAT'
1677
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1729
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1678
            varname='T:-1DLAT'
1730
            varname='T:-1DLAT'
1679
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1731
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1680
 
1732
 
1681
            call calc_DTDY (traint(j,k,ncol+i), traint(j,k,ind1), &
1733
            call calc_DTDY (traint(j,k,ncol+i), traint(j,k,ind1), &
1682
                 traint(j,k,ind2) )
1734
                 traint(j,k,ind2) )
1683
 
1735
 
1684
         ! Meridional gradient of TH (DTHDY)
1736
         ! Meridional gradient of TH (DTHDY)
1685
         elseif  ( varsint(i+ncol).eq.'DTHDY' ) then
1737
         elseif  ( varsint(i+ncol).eq.'DTHDY' ) then
1686
 
1738
 
1687
            varname='T:+1DLAT'
1739
            varname='T:+1DLAT'
1688
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1740
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1689
            varname='T:-1DLAT'
1741
            varname='T:-1DLAT'
1690
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1742
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1691
            varname='P'
1743
            varname='P'
1692
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1744
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1693
 
1745
 
1694
            call calc_DTDY (traint(j,k,ncol+i), traint(j,k,ind1), &
1746
            call calc_DTDY (traint(j,k,ncol+i), traint(j,k,ind1), &
1695
                 traint(j,k,ind2),traint(j,k,ind3) )
1747
                 traint(j,k,ind2),traint(j,k,ind3) )
1696
 
1748
 
1697
 
1749
 
1698
         ! Vertical wind shear DU/DP (DUDP)
1750
         ! Vertical wind shear DU/DP (DUDP)
1699
         elseif  ( varsint(i+ncol).eq.'DUDP' ) then
1751
         elseif  ( varsint(i+ncol).eq.'DUDP' ) then
1700
 
1752
 
1701
            varname='U:+1DP'
1753
            varname='U:+1DP'
1702
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1754
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1703
            varname='U:-1DP'
1755
            varname='U:-1DP'
1704
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1756
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1705
            varname='P:+1DP'
1757
            varname='P:+1DP'
1706
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1758
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1707
            varname='P:-1DP'
1759
            varname='P:-1DP'
1708
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1760
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1709
 
1761
 
1710
            call calc_DUDP (traint(j,k,ncol+i), traint(j,k,ind1), &
1762
            call calc_DUDP (traint(j,k,ncol+i), traint(j,k,ind1), &
1711
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4) )
1763
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4) )
1712
 
1764
 
1713
         ! Vertical wind shear DV/DP (DVDP)
1765
         ! Vertical wind shear DV/DP (DVDP)
1714
         elseif  ( varsint(i+ncol).eq.'DVDP' ) then
1766
         elseif  ( varsint(i+ncol).eq.'DVDP' ) then
1715
 
1767
 
1716
            varname='V:+1DP'
1768
            varname='V:+1DP'
1717
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1769
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1718
            varname='V:-1DP'
1770
            varname='V:-1DP'
1719
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1771
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1720
            varname='P:+1DP'
1772
            varname='P:+1DP'
1721
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1773
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1722
            varname='P:-1DP'
1774
            varname='P:-1DP'
1723
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1775
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1724
 
1776
 
1725
            call calc_DVDP (traint(j,k,ncol+i), traint(j,k,ind1), &
1777
            call calc_DVDP (traint(j,k,ncol+i), traint(j,k,ind1), &
1726
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4) )
1778
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4) )
1727
 
1779
 
1728
         ! Vertical derivative of T (DTDP)
1780
         ! Vertical derivative of T (DTDP)
1729
         elseif  ( varsint(i+ncol).eq.'DTDP' ) then
1781
         elseif  ( varsint(i+ncol).eq.'DTDP' ) then
1730
 
1782
 
1731
            varname='T:+1DP'
1783
            varname='T:+1DP'
1732
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1784
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1733
            varname='T:-1DP'
1785
            varname='T:-1DP'
1734
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1786
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1735
            varname='P:+1DP'
1787
            varname='P:+1DP'
1736
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1788
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1737
            varname='P:-1DP'
1789
            varname='P:-1DP'
1738
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1790
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1739
 
1791
 
1740
            call calc_DTDP (traint(j,k,ncol+i), traint(j,k,ind1), &
1792
            call calc_DTDP (traint(j,k,ncol+i), traint(j,k,ind1), &
1741
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4) )
1793
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4) )
1742
 
1794
 
1743
         ! Vertical derivative of TH (DTHDP)
1795
         ! Vertical derivative of TH (DTHDP)
1744
         elseif  ( varsint(i+ncol).eq.'DTHDP' ) then
1796
         elseif  ( varsint(i+ncol).eq.'DTHDP' ) then
1745
 
1797
 
1746
            varname='T:+1DP'
1798
            varname='T:+1DP'
1747
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1799
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1748
            varname='T:-1DP'
1800
            varname='T:-1DP'
1749
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1801
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1750
            varname='P:+1DP'
1802
            varname='P:+1DP'
1751
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1803
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1752
            varname='P:-1DP'
1804
            varname='P:-1DP'
1753
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1805
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1754
            varname='P'
1806
            varname='P'
1755
            call list2ind (ind5,varname,varsint,fok,ncol+ntrace1)
1807
            call list2ind (ind5,varname,varsint,fok,ncol+ntrace1)
1756
            varname='T'
1808
            varname='T'
1757
            call list2ind (ind6,varname,varsint,fok,ncol+ntrace1)
1809
            call list2ind (ind6,varname,varsint,fok,ncol+ntrace1)
1758
 
1810
 
1759
            call calc_DTHDP (traint(j,k,ncol+i), traint(j,k,ind1), &
1811
            call calc_DTHDP (traint(j,k,ncol+i), traint(j,k,ind1), &
1760
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4),traint(j,k,ind5),traint(j,k,ind6) )
1812
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4),traint(j,k,ind5),traint(j,k,ind6) )
1761
 
1813
 
1762
         ! Squared Brunt-Vaisäla frequency (NSQ)
1814
         ! Squared Brunt-Vaisäla frequency (NSQ)
1763
         elseif  ( varsint(i+ncol).eq.'NSQ' ) then
1815
         elseif  ( varsint(i+ncol).eq.'NSQ' ) then
1764
 
1816
 
1765
            varname='DTHDP'
1817
            varname='DTHDP'
1766
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1818
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1767
            varname='TH'
1819
            varname='TH'
1768
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1820
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1769
            varname='RHO'
1821
            varname='RHO'
1770
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1822
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1771
 
1823
 
1772
            call calc_NSQ (traint(j,k,ncol+i), traint(j,k,ind1), &
1824
            call calc_NSQ (traint(j,k,ncol+i), traint(j,k,ind1), &
1773
                 traint(j,k,ind2),traint(j,k,ind3))
1825
                 traint(j,k,ind2),traint(j,k,ind3))
1774
 
1826
 
1775
         ! Relative vorticity (RELVORT)
1827
         ! Relative vorticity (RELVORT)
1776
         elseif  ( varsint(i+ncol).eq.'RELVORT' ) then
1828
         elseif  ( varsint(i+ncol).eq.'RELVORT' ) then
1777
 
1829
 
1778
            varname='DUDY'
1830
            varname='DUDY'
1779
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1831
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1780
            varname='DVDX'
1832
            varname='DVDX'
1781
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1833
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1782
            varname='U'
1834
            varname='U'
1783
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1835
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1784
            varname='lat'
1836
            varname='lat'
1785
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1837
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1786
 
1838
 
1787
            call calc_RELVORT (traint(j,k,ncol+i), traint(j,k,ind1), &
1839
            call calc_RELVORT (traint(j,k,ncol+i), traint(j,k,ind1), &
1788
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4))
1840
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4))
1789
 
1841
 
1790
         ! Absolute vorticity (ABSVORT)
1842
         ! Absolute vorticity (ABSVORT)
1791
         elseif  ( varsint(i+ncol).eq.'ABSVORT' ) then
1843
         elseif  ( varsint(i+ncol).eq.'ABSVORT' ) then
1792
 
1844
 
1793
            varname='DUDY'
1845
            varname='DUDY'
1794
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1846
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1795
            varname='DVDX'
1847
            varname='DVDX'
1796
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1848
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1797
            varname='U'
1849
            varname='U'
1798
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1850
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1799
            varname='lat'
1851
            varname='lat'
1800
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1852
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1801
 
1853
 
1802
            call calc_ABSVORT (traint(j,k,ncol+i), traint(j,k,ind1), &
1854
            call calc_ABSVORT (traint(j,k,ncol+i), traint(j,k,ind1), &
1803
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4))
1855
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4))
1804
 
1856
 
1805
         ! Divergence (DIV)
1857
         ! Divergence (DIV)
1806
         elseif  ( varsint(i+ncol).eq.'DIV' ) then
1858
         elseif  ( varsint(i+ncol).eq.'DIV' ) then
1807
 
1859
 
1808
            varname='DUDX'
1860
            varname='DUDX'
1809
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1861
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1810
            varname='DVDY'
1862
            varname='DVDY'
1811
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1863
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1812
            varname='V'
1864
            varname='V'
1813
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1865
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1814
            varname='lat'
1866
            varname='lat'
1815
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1867
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1816
 
1868
 
1817
            call calc_DIV (traint(j,k,ncol+i), traint(j,k,ind1), &
1869
            call calc_DIV (traint(j,k,ncol+i), traint(j,k,ind1), &
1818
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4))
1870
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4))
1819
 
1871
 
1820
         ! Deformation (DEF)
1872
         ! Deformation (DEF)
1821
         elseif  ( varsint(i+ncol).eq.'DEF' ) then
1873
         elseif  ( varsint(i+ncol).eq.'DEF' ) then
1822
 
1874
 
1823
            varname='DUDX'
1875
            varname='DUDX'
1824
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1876
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1825
            varname='DVDX'
1877
            varname='DVDX'
1826
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1878
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1827
            varname='DUDY'
1879
            varname='DUDY'
1828
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1880
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1829
            varname='DVDY'
1881
            varname='DVDY'
1830
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1882
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1831
 
1883
 
1832
            call calc_DEF (traint(j,k,ncol+i), traint(j,k,ind1), &
1884
            call calc_DEF (traint(j,k,ncol+i), traint(j,k,ind1), &
1833
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4))
1885
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4))
1834
 
1886
 
1835
         ! Potential Vorticity (PV)
1887
         ! Potential Vorticity (PV)
1836
         elseif  ( varsint(i+ncol).eq.'PV' ) then
1888
         elseif  ( varsint(i+ncol).eq.'PV' ) then
1837
 
1889
 
1838
            varname='ABSVORT'
1890
            varname='ABSVORT'
1839
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1891
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1840
            varname='DTHDP'
1892
            varname='DTHDP'
1841
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1893
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1842
            varname='DUDP'
1894
            varname='DUDP'
1843
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1895
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1844
            varname='DVDP'
1896
            varname='DVDP'
1845
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1897
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1846
            varname='DTHDX'
1898
            varname='DTHDX'
1847
            call list2ind (ind5,varname,varsint,fok,ncol+ntrace1)
1899
            call list2ind (ind5,varname,varsint,fok,ncol+ntrace1)
1848
            varname='DTHDY'
1900
            varname='DTHDY'
1849
            call list2ind (ind6,varname,varsint,fok,ncol+ntrace1)
1901
            call list2ind (ind6,varname,varsint,fok,ncol+ntrace1)
1850
 
1902
 
1851
            call calc_PV (traint(j,k,ncol+i), traint(j,k,ind1), &
1903
            call calc_PV (traint(j,k,ncol+i), traint(j,k,ind1), &
1852
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4),traint(j,k,ind5),traint(j,k,ind6) )
1904
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4),traint(j,k,ind5),traint(j,k,ind6) )
1853
 
1905
 
1854
         ! Richardson number (RI)
1906
         ! Richardson number (RI)
1855
         elseif  ( varsint(i+ncol).eq.'RI' ) then
1907
         elseif  ( varsint(i+ncol).eq.'RI' ) then
1856
 
1908
 
1857
            varname='DUDP'
1909
            varname='DUDP'
1858
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1910
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1859
            varname='DVDP'
1911
            varname='DVDP'
1860
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1912
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1861
            varname='NSQ'
1913
            varname='NSQ'
1862
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1914
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1863
            varname='RHO'
1915
            varname='RHO'
1864
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1916
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1865
 
1917
 
1866
            call calc_RI (traint(j,k,ncol+i), traint(j,k,ind1), &
1918
            call calc_RI (traint(j,k,ncol+i), traint(j,k,ind1), &
1867
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4) )
1919
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4) )
1868
 
1920
 
1869
         ! Ellrod and Knapp's turbulence idicator (TI)
1921
         ! Ellrod and Knapp's turbulence idicator (TI)
1870
         elseif  ( varsint(i+ncol).eq.'TI' ) then
1922
         elseif  ( varsint(i+ncol).eq.'TI' ) then
1871
 
1923
 
1872
            varname='DEF'
1924
            varname='DEF'
1873
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1925
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1874
            varname='DUDP'
1926
            varname='DUDP'
1875
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1927
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1876
            varname='DVDP'
1928
            varname='DVDP'
1877
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1929
            call list2ind (ind3,varname,varsint,fok,ncol+ntrace1)
1878
            varname='RHO'
1930
            varname='RHO'
1879
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1931
            call list2ind (ind4,varname,varsint,fok,ncol+ntrace1)
1880
 
1932
 
1881
            call calc_TI (traint(j,k,ncol+i), traint(j,k,ind1), &
1933
            call calc_TI (traint(j,k,ncol+i), traint(j,k,ind1), &
1882
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4) )
1934
                 traint(j,k,ind2),traint(j,k,ind3),traint(j,k,ind4) )
1883
 
1935
 
1884
         ! Spherical distance from starting position (DIST0)
1936
         ! Spherical distance from starting position (DIST0)
1885
         elseif  ( varsint(i+ncol).eq.'DIST0' ) then
1937
         elseif  ( varsint(i+ncol).eq.'DIST0' ) then
1886
 
1938
 
1887
            varname='lon'
1939
            varname='lon'
1888
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1940
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1889
            varname='lat'
1941
            varname='lat'
1890
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1942
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1891
 
1943
 
1892
            call calc_DIST0 (traint(j,k,ncol+i), traint(j,k,ind1), &
1944
            call calc_DIST0 (traint(j,k,ncol+i), traint(j,k,ind1), &
1893
                 traint(j,k,ind2),traint(j,1,ind1),traint(j,1,ind2) )
1945
                 traint(j,k,ind2),traint(j,1,ind1),traint(j,1,ind2) )
1894
 
1946
 
1895
         ! Spherical distance length of trajectory (DIST)
1947
         ! Spherical distance length of trajectory (DIST)
1896
         elseif  ( varsint(i+ncol).eq.'DIST' ) then
1948
         elseif  ( varsint(i+ncol).eq.'DIST' ) then
1897
 
1949
 
1898
            varname='lon'
1950
            varname='lon'
1899
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1951
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1900
            varname='lat'
1952
            varname='lat'
1901
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1953
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1902
 
1954
 
1903
            if ( k.eq.1 ) then
1955
            if ( k.eq.1 ) then
1904
               traint(j,k,ncol+i) = 0.
1956
               traint(j,k,ncol+i) = 0.
1905
            else
1957
            else
1906
               call calc_DIST0 (delta, traint(j,k  ,ind1), &
1958
               call calc_DIST0 (delta, traint(j,k  ,ind1), &
1907
                   traint(j,k  ,ind2),traint(j,k-1,ind1),traint(j,k-1,ind2) )
1959
                   traint(j,k  ,ind2),traint(j,k-1,ind1),traint(j,k-1,ind2) )
1908
               traint(j,k,ncol+i) = traint(j,k-1,ncol+i) + delta
1960
               traint(j,k,ncol+i) = traint(j,k-1,ncol+i) + delta
1909
            endif
1961
            endif
1910
 
1962
 
1911
         ! Heading of the trajectory (HEAD)
1963
         ! Heading of the trajectory (HEAD)
1912
         elseif  ( varsint(i+ncol).eq.'HEAD' ) then
1964
         elseif  ( varsint(i+ncol).eq.'HEAD' ) then
1913
 
1965
 
1914
            varname='lon'
1966
            varname='lon'
1915
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1967
            call list2ind (ind1,varname,varsint,fok,ncol+ntrace1)
1916
            varname='lat'
1968
            varname='lat'
1917
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1969
            call list2ind (ind2,varname,varsint,fok,ncol+ntrace1)
1918
 
1970
 
1919
            if (k.eq.ntim) then
1971
            if (k.eq.ntim) then
1920
               traint(j,k,ncol+i) = mdv
1972
               traint(j,k,ncol+i) = mdv
1921
            else
1973
            else
1922
               call calc_HEAD (traint(j,k,ncol+i), &
1974
               call calc_HEAD (traint(j,k,ncol+i), &
1923
                    traint(j,k  ,ind1),traint(j,k  ,ind2),traint(j,k+1,ind1),traint(j,k+1,ind2) )
1975
                    traint(j,k  ,ind1),traint(j,k  ,ind2),traint(j,k+1,ind1),traint(j,k+1,ind2) )
1924
            endif
1976
            endif
1925
 
1977
 
1926
 
1978
 
1927
         ! Invalid tracing variable
1979
         ! Invalid tracing variable
1928
         else
1980
         else
1929
 
1981
 
1930
            print*,' ERROR: invalid tracing variable ', trim(varsint(i+ncol))
1982
            print*,' ERROR: invalid tracing variable ', trim(varsint(i+ncol))
1931
            stop
1983
            stop
1932
 
1984
 
1933
 
1985
 
1934
         endif
1986
         endif
1935
 
1987
 
1936
      ! End loop over all trajectories and times
1988
      ! End loop over all trajectories and times
1937
      enddo
1989
      enddo
1938
      enddo
1990
      enddo
1939
 
1991
 
1940
      ! Set the flag for a ready field/column
1992
      ! Set the flag for a ready field/column
1941
      fok(ncol+i) = 1
1993
      fok(ncol+i) = 1
1942
 
1994
 
1943
 
1995
 
1944
      ! Exit point for loop over all tracing fields
1996
      ! Exit point for loop over all tracing fields
1945
 120   continue
1997
 120   continue
1946
 
1998
 
1947
   enddo
1999
   enddo
1948
 
2000
 
1949
  ! --------------------------------------------------------------------
2001
  ! --------------------------------------------------------------------
1950
  ! Write output to output trajectory file
2002
  ! Write output to output trajectory file
1951
  ! --------------------------------------------------------------------
2003
  ! --------------------------------------------------------------------
1952
 
2004
 
1953
  ! Write status information
2005
  ! Write status information
1954
  print*
2006
  print*
1955
  print*,'---- WRITE OUTPUT TRAJECTORIES --------------------------'
2007
  print*,'---- WRITE OUTPUT TRAJECTORIES --------------------------'
1956
  print*
2008
  print*
1957
 
2009
 
1958
  ! Allocate memory for internal trajectories
2010
  ! Allocate memory for internal trajectories
1959
  allocate(traout(ntra,ntim,ncol+ntrace0),stat=stat)
2011
  allocate(traout(ntra,ntim,ncol+ntrace0),stat=stat)
1960
  if (stat.ne.0) print*,'*** error allocating array traout   ***'
2012
  if (stat.ne.0) print*,'*** error allocating array traout   ***'
1961
 
2013
 
1962
  ! Copy input to output trajectory (apply scaling of output)
2014
  ! Copy input to output trajectory (apply scaling of output)
1963
  do i=1,ntra
2015
  do i=1,ntra
1964
     do j=1,ntim
2016
     do j=1,ntim
1965
        do k=1,ncol+ntrace0
2017
        do k=1,ncol+ntrace0
1966
           if ( k.le.ncol ) then
2018
           if ( k.le.ncol ) then
1967
              traout(i,j,k) = traint(i,j,k)
2019
              traout(i,j,k) = traint(i,j,k)
1968
           elseif ( abs(traint(i,j,k)-mdv).gt.eps ) then
2020
           elseif ( abs(traint(i,j,k)-mdv).gt.eps ) then
1969
              traout(i,j,k) = fac(k-ncol) * traint(i,j,k)
2021
              traout(i,j,k) = fac(k-ncol) * traint(i,j,k)
1970
           else
2022
           else
1971
              traout(i,j,k) = mdv
2023
              traout(i,j,k) = mdv
1972
           endif
2024
           endif
1973
        enddo
2025
        enddo
1974
     enddo
2026
     enddo
1975
  enddo
2027
  enddo
1976
 
2028
 
1977
  ! Set the variable names for output trajectory
2029
  ! Set the variable names for output trajectory
1978
  do i=1,ncol+ntrace0
2030
  do i=1,ncol+ntrace0
1979
     varsout(i)      = varsint(i)
2031
        varsout(i)      = varsint(i)
1980
  enddo
2032
  enddo
-
 
2033
  do i=1,ntrace0
-
 
2034
     if ( (shift_dir(i).eq.'PMIN').or.(shift_dir(i).eq.'PMAX') ) then
-
 
2035
        varsout(ncol+i) = trim(tvar(i))//':'//trim(shift_dir(i))
-
 
2036
     endif
-
 
2037
  enddo
1981
 
2038
  
1982
  ! Write trajectories
2039
  ! Write trajectories
1983
  call wopen_tra(fod,outfile,ntra,ntim,ncol+ntrace0, &
2040
  call wopen_tra(fod,outfile,ntra,ntim,ncol+ntrace0, &
1984
       reftime,varsout,outmode)
2041
       reftime,varsout,outmode)
1985
  call write_tra(fod,traout ,ntra,ntim,ncol+ntrace0,outmode)
2042
  call write_tra(fod,traout ,ntra,ntim,ncol+ntrace0,outmode)
1986
  call close_tra(fod,outmode)
2043
  call close_tra(fod,outmode)
1987
 
2044
 
1988
  ! Write some status information, and end of program message
2045
  ! Write some status information, and end of program message
1989
  print*
2046
  print*
1990
  print*,'---- STATUS INFORMATION --------------------------------'
2047
  print*,'---- STATUS INFORMATION --------------------------------'
1991
  print*
2048
  print*
1992
  print*,' ok'
2049
  print*,' ok'
1993
  print*
2050
  print*
1994
  print*,'              *** END OF PROGRAM TRACE ***'
2051
  print*,'              *** END OF PROGRAM TRACE ***'
1995
  print*,'========================================================='
2052
  print*,'========================================================='
1996
 
2053
 
1997
 
2054
 
1998
end program trace
2055
end program trace
1999
 
2056
 
2000
 
2057
 
2001
 
2058
 
2002
! ******************************************************************
2059
! ******************************************************************
2003
! * SUBROUTINE SECTION                                             *
2060
! * SUBROUTINE SECTION                                             *
2004
! ******************************************************************
2061
! ******************************************************************
2005
 
2062
 
2006
! ------------------------------------------------------------------
2063
! ------------------------------------------------------------------
2007
! Add a variable to the list if not yet included in this list
2064
! Add a variable to the list if not yet included in this list
2008
! ------------------------------------------------------------------
2065
! ------------------------------------------------------------------
2009
 
2066
 
2010
subroutine add2list (varname,list,nlist)
2067
subroutine add2list (varname,list,nlist)
2011
 
2068
 
2012
  implicit none
2069
  implicit none
2013
 
2070
 
2014
  ! Declaration of subroutine parameters
2071
  ! Declaration of subroutine parameters
2015
  character(len=80) :: varname
2072
  character(len=80) :: varname
2016
  character(len=80) :: list(200)
2073
  character(len=80) :: list(200)
2017
  integer :: nlist
2074
  integer :: nlist
2018
 
2075
 
2019
  ! Auxiliray variables
2076
  ! Auxiliray variables
2020
  integer :: i,j
2077
  integer :: i,j
2021
  integer :: isok
2078
  integer :: isok
2022
 
2079
 
2023
  ! Expand the list, if necessary
2080
  ! Expand the list, if necessary
2024
  isok = 0
2081
  isok = 0
2025
  do i=1,nlist
2082
  do i=1,nlist
2026
     if ( list(i).eq.varname ) isok = 1
2083
     if ( list(i).eq.varname ) isok = 1
2027
  enddo
2084
  enddo
2028
  if ( isok.eq.0 ) then
2085
  if ( isok.eq.0 ) then
2029
     nlist       = nlist + 1
2086
     nlist       = nlist + 1
2030
     list(nlist) = varname
2087
     list(nlist) = varname
2031
  endif
2088
  endif
2032
 
2089
 
2033
  ! Check for too large number of fields
2090
  ! Check for too large number of fields
2034
  if ( nlist.ge.200) then
2091
  if ( nlist.ge.200) then
2035
     print*,' ERROR: too many additional fields for tracing ...'
2092
     print*,' ERROR: too many additional fields for tracing ...'
2036
     stop
2093
     stop
2037
  endif
2094
  endif
2038
 
2095
 
2039
end subroutine add2list
2096
end subroutine add2list
2040
 
2097
 
2041
 
2098
 
2042
! ------------------------------------------------------------------
2099
! ------------------------------------------------------------------
2043
! Get the index of a variable in the list
2100
! Get the index of a variable in the list
2044
! ------------------------------------------------------------------
2101
! ------------------------------------------------------------------
2045
 
2102
 
2046
subroutine list2ind (ind,varname,list,fok,nlist)
2103
subroutine list2ind (ind,varname,list,fok,nlist)
2047
 
2104
 
2048
  implicit none
2105
  implicit none
2049
 
2106
 
2050
  ! Declaration of subroutine parameters
2107
  ! Declaration of subroutine parameters
2051
  integer :: ind
2108
  integer :: ind
2052
  character(len=80) :: varname
2109
  character(len=80) :: varname
2053
  character(len=80) :: list(200)
2110
  character(len=80) :: list(200)
2054
  integer :: fok(200)
2111
  integer :: fok(200)
2055
  integer :: nlist
2112
  integer :: nlist
2056
 
2113
 
2057
  ! Auxiliray variables
2114
  ! Auxiliray variables
2058
  integer :: i,j
2115
  integer :: i,j
2059
  integer :: isok
2116
  integer :: isok
2060
 
2117
 
2061
  ! Get the index - error message if not found
2118
  ! Get the index - error message if not found
2062
  ind = 0
2119
  ind = 0
2063
  do i=1,nlist
2120
  do i=1,nlist
2064
     if ( varname.eq.list(i) ) then
2121
     if ( varname.eq.list(i) ) then
2065
        ind = i
2122
        ind = i
2066
        goto 100
2123
        goto 100
2067
     endif
2124
     endif
2068
  enddo
2125
  enddo
2069
 
2126
 
2070
  if ( ind.eq.0) then
2127
  if ( ind.eq.0) then
2071
     print*
2128
     print*
2072
     print*,' ERROR: cannot find ',trim(varname),' in list ...'
2129
     print*,' ERROR: cannot find ',trim(varname),' in list ...'
2073
     do i=1,nlist
2130
     do i=1,nlist
2074
        print*,i,trim(list(i))
2131
        print*,i,trim(list(i))
2075
     enddo
2132
     enddo
2076
     print*
2133
     print*
2077
     stop
2134
     stop
2078
  endif
2135
  endif
2079
 
2136
 
2080
  ! Exit point
2137
  ! Exit point
2081
 100   continue
2138
 100   continue
2082
 
2139
 
2083
  ! Check whether the field/column is ready
2140
  ! Check whether the field/column is ready
2084
  if ( fok(ind).eq.0 ) then
2141
  if ( fok(ind).eq.0 ) then
2085
     print*
2142
     print*
2086
     print*,' ERROR: unresolved dependence : ',trim(list(ind))
2143
     print*,' ERROR: unresolved dependence : ',trim(list(ind))
2087
     print*
2144
     print*
2088
     stop
2145
     stop
2089
  endif
2146
  endif
2090
 
2147
 
2091
end subroutine list2ind
2148
end subroutine list2ind
2092
 
2149
 
2093
 
2150
 
2094
! ------------------------------------------------------------------
2151
! ------------------------------------------------------------------
2095
! Split the variable name into name, shift and direction
2152
! Split the variable name into name, shift and direction
2096
! ------------------------------------------------------------------
2153
! ------------------------------------------------------------------
2097
 
2154
 
2098
subroutine splitvar (tvar,shift_val,shift_dir)
2155
subroutine splitvar (tvar,shift_val,shift_dir,shift_rel)
2099
 
2156
 
2100
  implicit none
2157
  implicit none
2101
 
2158
 
2102
  ! Declaration of subroutine parameters
2159
  ! Declaration of subroutine parameters
2103
  character(len=80) :: tvar
2160
  character(len=80) :: tvar
2104
  real :: shift_val
2161
  real :: shift_val
2105
  character(len=80) :: shift_dir
2162
  character(len=80) :: shift_dir
-
 
2163
  character(len=80) :: shift_rel
2106
 
2164
 
2107
  ! Auxiliary variables
2165
  ! Auxiliary variables
2108
  integer :: i,j
2166
  integer :: i,j
2109
  integer :: icolon,inumber
2167
  integer :: icolon,inumber,irelator
2110
  character(len=80) :: name
2168
  character(len=80) :: name
2111
  character :: ch
2169
  character :: ch
2112
  integer      isabsval
2170
  integer      isabsval
2113
 
2171
 
2114
  ! Save variable name
2172
  ! Save variable name
2115
  name = tvar
2173
  name = tvar
-
 
2174
  shift_rel = 'nil'
-
 
2175
  shift_dir = 'nil'
2116
 
2176
 
2117
  ! Search for colon
2177
  ! Search for colon
2118
  icolon=0
2178
  icolon=0
2119
  do i=1,80
2179
  do i=1,80
2120
     if ( (name(i:i).eq.':').and.(icolon.ne.0) ) goto 100
2180
     if ( (name(i:i).eq.':').and.(icolon.ne.0) ) goto 100
2121
     if ( (name(i:i).eq.':').and.(icolon.eq.0) ) icolon=i
2181
     if ( (name(i:i).eq.':').and.(icolon.eq.0) ) icolon=i
2122
  enddo
2182
  enddo
2123
 
2183
 
2124
  ! If there is a colon, split the variable name
2184
  ! If there is a colon, split the variable name
2125
  if ( icolon.ne.0 ) then
2185
  if ( icolon.ne.0 ) then
2126
 
2186
 
2127
     tvar = name(1:(icolon-1))
2187
     tvar = name(1:(icolon-1))
2128
 
2188
 
2129
     ! Get the index for number
2189
     ! Get the index for number
2130
     do i=icolon+1,80
2190
     do i=icolon+1,80
2131
        ch = name(i:i)
2191
        ch = name(i:i)
2132
        if ( ( ch.ne.'0' ).and. ( ch.ne.'1' ).and.( ch.ne.'2' ).and. &
2192
        if ( ( ch.ne.'0' ).and. ( ch.ne.'1' ).and.( ch.ne.'2' ).and. &
2133
             ( ch.ne.'3' ).and. ( ch.ne.'4' ).and.( ch.ne.'5' ).and. &
2193
             ( ch.ne.'3' ).and. ( ch.ne.'4' ).and.( ch.ne.'5' ).and. &
2134
             ( ch.ne.'6' ).and. ( ch.ne.'7' ).and.( ch.ne.'8' ).and. &
2194
             ( ch.ne.'6' ).and. ( ch.ne.'7' ).and.( ch.ne.'8' ).and. &
2135
             ( ch.ne.'9' ).and. ( ch.ne.'+' ).and.( ch.ne.'-' ).and. &
2195
             ( ch.ne.'9' ).and. ( ch.ne.'+' ).and.( ch.ne.'-' ).and. &
2136
             ( ch.ne.'.' ).and. ( ch.ne.' ' )  ) then
2196
             ( ch.ne.'.' ).and. ( ch.ne.' ' )  ) then
2137
           inumber = i
2197
           inumber = i
2138
           exit
2198
           exit
2139
        endif
2199
        endif
2140
     enddo
2200
     enddo
2141
 
2201
 
-
 
2202
     ! If the variable name is e.g. PMIN:UMF>0, the variable to be read
-
 
2203
     ! is UMF, the value is 0, and the direction is 'PMIN'
-
 
2204
     shift_rel = 'nil'
-
 
2205
     if ( (tvar.eq.'PMIN').or.(tvar.eq.'PMAX') ) then
-
 
2206
         shift_dir = tvar
-
 
2207
         irelator = 0
-
 
2208
         do i=icolon+1,80
-
 
2209
            ch = name(i:i)
-
 
2210
            if ( (ch.eq.'>').or.(ch.eq.'<') ) then
-
 
2211
               irelator = i
-
 
2212
            endif
-
 
2213
         enddo
-
 
2214
         if ( irelator.eq.0 ) then
-
 
2215
             print*,' ERROR: dont know how to interpret ',trim(name)
-
 
2216
             stop
-
 
2217
         endif
-
 
2218
         tvar = name(icolon+1:irelator-1)
-
 
2219
         read(name( (irelator+1):80 ),*) shift_val
-
 
2220
         shift_rel = name(irelator:irelator)
-
 
2221
           
-
 
2222
         goto 90
-
 
2223
  
-
 
2224
     endif
-
 
2225
    
2142
     ! Get the number
2226
     ! Get the number
2143
     read(name( (icolon+1):(inumber-1) ),*) shift_val
2227
     read(name( (icolon+1):(inumber-1) ),*) shift_val
2144
 
2228
 
2145
     ! Decide whether it is a shift relatiev to trajectory or absolute value
2229
     ! Decide whether it is a shift relatiev to trajectory or absolute value
2146
     ! If the number starts with + or -, it is relative to the trajectory
2230
     ! If the number starts with + or -, it is relative to the trajectory
2147
     isabsval = 1
2231
     isabsval = 1
2148
     do i=icolon+1,inumber-1
2232
     do i=icolon+1,inumber-1
2149
       ch = name(i:i)
2233
       ch = name(i:i)
2150
       if ( (ch.eq.'+').or.(ch.eq.'-') ) isabsval = 0
2234
       if ( (ch.eq.'+').or.(ch.eq.'-') ) isabsval = 0
2151
     enddo
2235
     enddo
2152
 
2236
 
2153
     ! Get the unit/shift axis
2237
     ! Get the unit/shift axis
2154
     shift_dir = name(inumber:80)
2238
     shift_dir = name(inumber:80)
2155
     if ( isabsval.eq.1 ) then
2239
     if ( isabsval.eq.1 ) then
2156
       shift_dir=trim(shift_dir)//'(ABS)'
2240
       shift_dir=trim(shift_dir)//'(ABS)'
2157
     endif
2241
     endif
2158
 
2242
 
2159
  else
2243
  else
2160
 
2244
 
2161
     shift_dir = 'nil'
2245
     shift_dir = 'nil'
2162
     shift_val = 0.
2246
     shift_val = 0.
2163
 
2247
 
2164
  endif
2248
  endif
2165
 
2249
 
-
 
2250
 90 continue
-
 
2251
 
2166
  return
2252
    return
2167
 
2253
 
2168
  ! Error handling
2254
  ! Error handling
2169
 100   continue
2255
 100   continue
2170
 
2256
 
2171
  print*,' ERROR: cannot split variable name ',trim(tvar)
2257
  print*,' ERROR: cannot split variable name ',trim(tvar)
2172
  stop
2258
  stop
2173
 
2259
 
2174
end subroutine splitvar
2260
end subroutine splitvar