Subversion Repositories lagranto.ecmwf

Rev

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

Rev 3 Rev 9
1
      PROGRAM select
1
      PROGRAM select
2
 
2
 
3
c     **************************************************************
3
c     **************************************************************
4
c     * Select trajectories from LSL file                          *
4
c     * Select trajectories from LSL file                          *
5
c     * Michael Sprenger / January, February 2008                  *
5
c     * Michael Sprenger / January, February 2008                  *
6
c     **************************************************************
6
c     **************************************************************
7
 
7
 
8
      implicit none
8
      implicit none
9
 
9
 
10
c     -------------------------------------------------------------- 
10
c     -------------------------------------------------------------- 
11
c     Declaration of parameters
11
c     Declaration of parameters
12
c     --------------------------------------------------------------
12
c     --------------------------------------------------------------
13
 
13
 
14
c     Maximum number of columns per trajectory
14
c     Maximum number of columns per trajectory
15
      integer          maxcol
15
      integer          maxcol
16
      parameter        (maxcol=100)
16
      parameter        (maxcol=100)
17
      
17
      
18
c     Maximum number of commands
18
c     Maximum number of commands
19
      integer          maxcmd
19
      integer          maxcmd
20
      parameter        (maxcmd=10000)
20
      parameter        (maxcmd=10000)
21
 
21
 
22
c     -------------------------------------------------------------- 
22
c     -------------------------------------------------------------- 
23
c     Declaration of variables
23
c     Declaration of variables
24
c     --------------------------------------------------------------
24
c     --------------------------------------------------------------
25
 
25
 
26
c     Input and output files
26
c     Input and output files
27
      character*120     inp_lslfile                  ! Input lsl file
27
      character*120     inp_lslfile                  ! Input lsl file
28
      character*120     out_lslfile                  ! Output lsl file
28
      character*120     out_lslfile                  ! Output lsl file
29
      character*120     inp_criteria                 ! Input file with criteria
29
      character*120     inp_criteria                 ! Input file with criteria
30
      integer           inpmode                      ! Format of input file
30
      integer           inpmode                      ! Format of input file
31
      integer           outmode                      ! Format of output file
31
      integer           outmode                      ! Format of output file
32
      character*80      outformat                    ! Trajectory/Boolean/Index of output   
32
      character*80      outformat                    ! Trajectory/Boolean/Index of output   
33
      character*80      regionf                      ! Name of the regionfile
33
      character*80      regionf                      ! Name of the regionfile
34
 
34
 
35
c     Trajectories
35
c     Trajectories
36
      integer          ntim                          ! Number of times 
36
      integer          ntim                          ! Number of times 
37
      integer          ncol                          ! Number of columns (including time...)
37
      integer          ncol                          ! Number of columns (including time...)
38
      integer          ntrainp,ntraout               ! Number of trajectories
38
      integer          ntrainp,ntraout               ! Number of trajectories
39
      character*80     vars(maxcol)                  ! Names of trajectory columns
39
      character*80     vars(maxcol)                  ! Names of trajectory columns
40
      integer          basetime(6)                   ! Base time of trajectory (first line in lsl)
40
      integer          basetime(6)                   ! Base time of trajectory (first line in lsl)
41
      real,allocatable, dimension (:,:,:) :: trainp  ! Input trajectories
41
      real,allocatable, dimension (:,:,:) :: trainp  ! Input trajectories
42
      real,allocatable, dimension (:,:,:) :: traout  ! Output trajectories
42
      real,allocatable, dimension (:,:,:) :: traout  ! Output trajectories
43
      integer,allocatable,dimension (:)   :: selflag ! Flag for selection
43
      integer,allocatable,dimension (:)   :: selflag ! Flag for selection
44
      real,allocatable, dimension (:)     :: time    ! Times of the trajectory
44
      real,allocatable, dimension (:)     :: time    ! Times of the trajectory
45
      integer,allocatable,dimension (:,:) :: trigger ! Trigger column 
45
      integer,allocatable,dimension (:,:) :: trigger ! Trigger column 
46
      integer          itrigger                      ! Column index for trigger
46
      integer          itrigger                      ! Column index for trigger
47
      character*80     addtrigger                    ! Flag whether to add trigger column
47
      character*80     addtrigger                    ! Flag whether to add trigger column
48
 
48
 
49
c     Command stack
49
c     Command stack
50
      real             cmd(maxcmd)                   ! Decoded slection criterion
50
      real             cmd(maxcmd)                   ! Decoded slection criterion
51
      integer          ncmd                          ! Number of commands
51
      integer          ncmd                          ! Number of commands
52
 
52
 
53
c     Common block for initialisation of polygon check
53
c     Common block for initialisation of polygon check
54
      real    tlonv(2000),vlat_c(2000),vlon_c(2000)
54
      real    tlonv(2000),vlat_c(2000),vlon_c(2000)
55
      real    xlat_c,xlon_c
55
      real    xlat_c,xlon_c
56
      integer ibndry,nv_c
56
      integer ibndry,nv_c
57
      data    ibndry/0/
57
      data    ibndry/0/
58
      common  /spolybndry/vlat_c,vlon_c,nv_c,xlat_c,xlon_c,tlonv,ibndry
58
      common  /spolybndry/vlat_c,vlon_c,nv_c,xlat_c,xlon_c,tlonv,ibndry
59
 
59
 
60
c     Auxiliary variables
60
c     Auxiliary variables
61
      integer          stat                          ! Logical (state) variable
61
      integer          stat                          ! Logical (state) variable
62
      integer          fid,fod,fcr                   ! File identifier for input and output
62
      integer          fid,fod,fcr                   ! File identifier for input and output
63
      integer          i,j,k                         ! Index counter
63
      integer          i,j,k                         ! Index counter
64
      integer          isok                          ! Flag for selected trajectory
64
      integer          isok                          ! Flag for selected trajectory
65
      real             param(1000)                   ! List of parameters
65
      real             param(1000)                   ! List of parameters
66
      integer          nparam                        ! Number of parameters
66
      integer          nparam                        ! Number of parameters
67
      character*80     specialstr                    ! Name of special command
67
      character*80     specialstr                    ! Name of special command
68
      integer          len
68
      integer          len
69
      integer,allocatable,dimension (:) :: trigger1  ! Trigger column 
69
      integer,allocatable,dimension (:) :: trigger1  ! Trigger column 
70
      real,allocatable,dimension (:,:) :: trainp1    ! A single trajectory
70
      real,allocatable,dimension (:,:) :: trainp1    ! A single trajectory
71
      character        ch
71
      character        ch
72
 
72
 
73
c     -------------------------------------------------------------- 
73
c     -------------------------------------------------------------- 
74
c     Preparations
74
c     Preparations
75
c     --------------------------------------------------------------
75
c     --------------------------------------------------------------
76
 
76
 
77
c     Write start message
77
c     Write start message
78
      print*,'========================================================='
78
      print*,'========================================================='
79
      print*,'              *** START OF PROGRAM SELECT ***'
79
      print*,'              *** START OF PROGRAM SELECT ***'
80
      print*
80
      print*
81
 
81
 
82
c     Read parameter file
82
c     Read parameter file
83
      open(10,file='select.param')
83
      open(10,file='select.param')
84
       read(10,*) inp_lslfile
84
       read(10,*) inp_lslfile
85
       read(10,*) out_lslfile
85
       read(10,*) out_lslfile
86
       read(10,*) outformat
86
       read(10,*) outformat
87
       read(10,*) inp_criteria
87
       read(10,*) inp_criteria
88
       read(10,*) ntrainp
88
       read(10,*) ntrainp
89
       read(10,*) ntim
89
       read(10,*) ntim
90
       read(10,*) ncol
90
       read(10,*) ncol
91
       read(10,*) regionf
91
       read(10,*) regionf
92
       read(10,*) addtrigger
92
       read(10,*) addtrigger
93
      close(10)
93
      close(10)
94
      
94
      
95
c     Set the formats of the input and output files
95
c     Set the formats of the input and output files
96
      call mode_tra(inpmode,inp_lslfile)
96
      call mode_tra(inpmode,inp_lslfile)
97
      if (inpmode.eq.-1) inpmode=1
97
      if (inpmode.eq.-1) inpmode=1
98
      call mode_tra(outmode,out_lslfile)
98
      call mode_tra(outmode,out_lslfile)
99
      if ( (outmode.eq.-1).and.(outformat.ne.'startf') ) then
99
      if ( (outmode.eq.-1).and.(outformat.ne.'startf') ) then
100
         outmode=1
100
         outmode=1
101
      endif
101
      endif
102
 
102
 
103
c     Allocate memory for a single trajectory
103
c     Allocate memory for a single trajectory
104
      allocate(trainp(ntrainp,ntim,ncol),stat=stat)
104
      allocate(trainp(ntrainp,ntim,ncol),stat=stat)
105
      if (stat.ne.0) stop '*** error allocating array trainp   ***'
105
      if (stat.ne.0) stop '*** error allocating array trainp   ***'
106
      allocate(time(ntim),stat=stat)
106
      allocate(time(ntim),stat=stat)
107
      if (stat.ne.0) stop '*** error allocating array time     ***'
107
      if (stat.ne.0) stop '*** error allocating array time     ***'
108
      allocate(selflag(ntrainp),stat=stat)
108
      allocate(selflag(ntrainp),stat=stat)
109
      if (stat.ne.0) stop '*** error allocating array selflag  ***'
109
      if (stat.ne.0) stop '*** error allocating array selflag  ***'
110
      allocate(trigger(ntrainp,ntim),stat=stat)
110
      allocate(trigger(ntrainp,ntim),stat=stat)
111
      if (stat.ne.0) stop '*** error allocating array trigger  ***'
111
      if (stat.ne.0) stop '*** error allocating array trigger  ***'
112
      allocate(trigger1(ntim),stat=stat)
112
      allocate(trigger1(ntim),stat=stat)
113
      if (stat.ne.0) stop '*** error allocating array trigger1 ***'
113
      if (stat.ne.0) stop '*** error allocating array trigger1 ***'
114
      allocate(trainp1(ntim,ncol),stat=stat)
114
      allocate(trainp1(ntim,ncol),stat=stat)
115
      if (stat.ne.0) stop '*** error allocating array trainp1  ***'
115
      if (stat.ne.0) stop '*** error allocating array trainp1  ***'
116
 
116
 
117
c     Read the input trajectory file
117
c     Read the input trajectory file
118
      call ropen_tra(fid,inp_lslfile,ntrainp,ntim,ncol,
118
      call ropen_tra(fid,inp_lslfile,ntrainp,ntim,ncol,
119
     >               basetime,vars,inpmode)
119
     >               basetime,vars,inpmode)
120
      call read_tra (fid,trainp,ntrainp,ntim,ncol,inpmode)
120
      call read_tra (fid,trainp,ntrainp,ntim,ncol,inpmode)
121
      call close_tra(fid,inpmode)
121
      call close_tra(fid,inpmode)
122
 
122
 
123
c     Check that first four columns correspond to time,lon,lat,p
123
c     Check that first four columns correspond to time,lon,lat,p
124
      if ( (vars(1).ne.'time' ).or.
124
      if ( (vars(1).ne.'time' ).or.
125
     >     (vars(2).ne.'xpos' ).and.(vars(2).ne.'lon' ).or.
125
     >     (vars(2).ne.'xpos' ).and.(vars(2).ne.'lon' ).or.
126
     >     (vars(3).ne.'ypos' ).and.(vars(3).ne.'lat' ).or.
126
     >     (vars(3).ne.'ypos' ).and.(vars(3).ne.'lat' ).or.
127
     >     (vars(4).ne.'ppos' ).and.(vars(4).ne.'p'   ) )
127
     >     (vars(4).ne.'ppos' ).and.(vars(4).ne.'p'   ) )
128
     >then
128
     >then
129
         print*,' ERROR: problem with input trajectories ...'
129
         print*,' ERROR: problem with input trajectories ...'
130
         stop
130
         stop
131
      endif
131
      endif
132
      vars(1) = 'time'
132
      vars(1) = 'time'
133
      vars(2) = 'lon'
133
      vars(2) = 'lon'
134
      vars(3) = 'lat'
134
      vars(3) = 'lat'
135
      vars(4) = 'p'
135
      vars(4) = 'p'
136
 
136
 
137
c     Get the trajectory times from first trajectory
137
c     Get the trajectory times from first trajectory
138
      do i=1,ntim
138
      do i=1,ntim
139
         time(i)=trainp(1,i,1)
139
         time(i)=trainp(1,i,1)
140
      enddo
140
      enddo
141
 
141
 
142
c     Init the trigger field - first check whether it is already available
142
c     Init the trigger field - first check whether it is already available
143
      itrigger = 0
143
      itrigger = 0
144
      do i=1,ncol
144
      do i=1,ncol
145
         if ( vars(i).eq.'TRIGGER' ) itrigger = i
145
         if ( vars(i).eq.'TRIGGER' ) itrigger = i
146
      enddo
146
      enddo
147
 
147
 
148
      if ( itrigger.ne.0 ) then
148
      if ( itrigger.ne.0 ) then
149
         do i=1,ntrainp
149
         do i=1,ntrainp
150
            do j=1,ntim
150
            do j=1,ntim
151
               trigger(i,j) = nint( trainp(i,j,itrigger) )
151
               trigger(i,j) = nint( trainp(i,j,itrigger) )
152
            enddo
152
            enddo
153
         enddo
153
         enddo
154
      else
154
      else
155
         do i=1,ntrainp
155
         do i=1,ntrainp
156
            do j=1,ntim
156
            do j=1,ntim
157
               trigger(i,j) = 0
157
               trigger(i,j) = 0
158
            enddo
158
            enddo
159
         enddo
159
         enddo
160
      endif
160
      endif
161
 
161
 
162
c     Write some info about the trajectory
162
c     Write some info about the trajectory
163
      print*,'---- INPUT PARAMETERS -----------------------------------'
163
      print*,'---- INPUT PARAMETERS -----------------------------------'
164
      write(*,*) 
164
      write(*,*) 
165
      write(*,*) 'Input file    : ',trim(inp_lslfile)
165
      write(*,*) 'Input file    : ',trim(inp_lslfile)
166
      write(*,*) 'Output file   : ',trim(out_lslfile)
166
      write(*,*) 'Output file   : ',trim(out_lslfile)
167
      write(*,*) 'Output format : ',trim(outformat)
167
      write(*,*) 'Output format : ',trim(outformat)
168
      write(*,*) 'Criteria file : ',trim(inp_criteria)
168
      write(*,*) 'Criteria file : ',trim(inp_criteria)
169
      write(*,*) '# tra         : ',ntrainp
169
      write(*,*) '# tra         : ',ntrainp
170
      write(*,*) '# time        : ',ntim
170
      write(*,*) '# time        : ',ntim
171
      write(*,*) '# col         : ',ncol
171
      write(*,*) '# col         : ',ncol
172
      write(*,*) 'Region file   : ',trim(regionf)
172
      write(*,*) 'Region file   : ',trim(regionf)
173
      write(*,*) 'Add trigger   : ',trim(addtrigger)
173
      write(*,*) 'Add trigger   : ',trim(addtrigger)
174
      
174
      
175
      print*
175
      print*
176
      print*,'---- INPUT TRAJECTORY FILE ------------------------------'
176
      print*,'---- INPUT TRAJECTORY FILE ------------------------------'
177
      print*
177
      print*
178
      write(*,'(1x,a12,i4,a10)') 'Vars       : ',1,trim(vars(1))
178
      write(*,'(1x,a12,i4,a10)') 'Vars       : ',1,trim(vars(1))
179
      do i=2,ncol
179
      do i=2,ncol
180
         write(*,'(1x,a12,i4,a10)') '             ',i,trim(vars(i))
180
         write(*,'(1x,a12,i4,a10)') '             ',i,trim(vars(i))
181
      enddo
181
      enddo
182
      print*
182
      print*
183
      write(*,'(1x,a12,i4,f10.2)') 'Time       : ',1,time(1)
183
      write(*,'(1x,a12,i4,f10.2)') 'Time       : ',1,time(1)
184
      do i=2,ntim
184
      do i=2,ntim
185
         write(*,'(1x,a12,i4,f10.2)') '             ',i,time(i)
185
         write(*,'(1x,a12,i4,f10.2)') '             ',i,time(i)
186
      enddo
186
      enddo
187
      print*
187
      print*
188
      write(*,'(1x,a12,i4,i10)') 'Base date  : ',1,basetime(1)
188
      write(*,'(1x,a12,i4,i10)') 'Base date  : ',1,basetime(1)
189
      write(*,'(1x,a12,i4,i10)') '             ',2,basetime(2)
189
      write(*,'(1x,a12,i4,i10)') '             ',2,basetime(2)
190
      write(*,'(1x,a12,i4,i10)') '             ',3,basetime(3)
190
      write(*,'(1x,a12,i4,i10)') '             ',3,basetime(3)
191
      write(*,'(1x,a12,i4,i10)') '             ',4,basetime(4)
191
      write(*,'(1x,a12,i4,i10)') '             ',4,basetime(4)
192
      write(*,'(1x,a12,i4,i10)') '             ',5,basetime(5)      
192
      write(*,'(1x,a12,i4,i10)') '             ',5,basetime(5)      
193
      print*
193
      print*
194
      write(*,'(1x,a12,i4,i10)') 'Time range : ',6,basetime(6)
194
      write(*,'(1x,a12,i4,i10)') 'Time range : ',6,basetime(6)
195
      print*
195
      print*
196
      if ( itrigger.ne.0 ) then
196
      if ( itrigger.ne.0 ) then
197
         print*,'TRIGGER FIELD FOUND IN COLUMN ',itrigger
197
         print*,'TRIGGER FIELD FOUND IN COLUMN ',itrigger
198
         print*
198
         print*
199
      endif
199
      endif
200
 
200
 
201
c     Read and decode the selection criterion
201
c     Read and decode the selection criterion
202
      fcr = 10
202
      fcr = 10
203
      open(fcr,file=inp_criteria)
203
      open(fcr,file=inp_criteria)
204
      ncmd=maxcmd
204
      ncmd=maxcmd
205
      call decode(fcr,cmd,ncmd,vars,ncol,time,ntim,regionf)
205
      call decode(fcr,cmd,ncmd,vars,ncol,time,ntim,regionf)
206
      close(fcr)
206
      close(fcr)
207
 
207
 
208
      print*
208
      print*
209
      print*,'---- PSEUDO CODE FOR SELECTION --------------------------'
209
      print*,'---- PSEUDO CODE FOR SELECTION --------------------------'
210
      print*
210
      print*
211
      call dumpcode(cmd,ncmd,vars,ncol,time,ntim)
211
      call dumpcode(cmd,ncmd,vars,ncol,time,ntim)
212
 
212
 
213
c     -------------------------------------------------------------- 
213
c     -------------------------------------------------------------- 
214
c     Loop over all trajectories - selection
214
c     Loop over all trajectories - selection
215
c     --------------------------------------------------------------
215
c     --------------------------------------------------------------
216
 
216
 
217
c     Prepare string and parameters for SPECIAL commands
217
c     Prepare string and parameters for SPECIAL commands
218
      if ( cmd(1).eq.0 ) then
218
      if ( cmd(1).eq.0 ) then
219
 
219
 
220
c        Get command string
220
c        Get command string
221
         j   = 2
221
         j   = 2
222
         len = nint(cmd(j))
222
         len = nint(cmd(j))
223
         specialstr = ''
223
         specialstr = ''
224
         do k=1,len
224
         do k=1,len
225
            j = j + 1
225
            j = j + 1
226
            specialstr = trim(specialstr)//char(nint(cmd(j)))
226
            specialstr = trim(specialstr)//char(nint(cmd(j)))
227
         enddo
227
         enddo
228
         
228
         
229
c        Get paramters
229
c        Get paramters
230
         j      = j + 1
230
         j      = j + 1
231
         nparam = nint(cmd(j))
231
         nparam = nint(cmd(j))
232
         do k=1,nparam
232
         do k=1,nparam
233
            j        = j + 1
233
            j        = j + 1
234
            param(k) = cmd(j)
234
            param(k) = cmd(j)
235
         enddo
235
         enddo
236
         
236
         
237
      endif
237
      endif
238
        
238
        
239
c     Init the counter for selected trajectories
239
c     Init the counter for selected trajectories
240
      ntraout = 0
240
      ntraout = 0
241
 
241
 
242
c     Loop over all trajectories
242
c     Loop over all trajectories
243
      do i=1,ntrainp
243
      do i=1,ntrainp
244
 
244
 
245
c       Copy a single trajectory to <trainp1> and <trigger1>
245
c       Copy a single trajectory to <trainp1> and <trigger1>
246
        do j=1,ntim
246
        do j=1,ntim
247
           do k=1,ncol
247
           do k=1,ncol
248
              trainp1(j,k) = trainp(i,j,k)
248
              trainp1(j,k) = trainp(i,j,k)
249
           enddo
249
           enddo
250
           trigger1(j) = trigger(i,j)
250
           trigger1(j) = trigger(i,j)
251
        enddo
251
        enddo
252
 
252
 
253
C	    Skip the trajectory if missing data are found for positions
253
C	    Skip the trajectory if missing data are found for positions
254
        isok = 1
254
        isok = 1
255
        do j=1,ntim
255
        do j=1,ntim
256
           if ( trainp1(j,4).lt.0. ) isok = 0
256
           if ( trainp1(j,4).lt.0. ) isok = 0
257
        enddo
257
        enddo
258
 
258
 
259
c       Decide whether the trajectory is selected (handle SPECIAL commands)
259
c       Decide whether the trajectory is selected (handle SPECIAL commands)
260
        if ( isok.eq.1 ) then
260
        if ( isok.eq.1 ) then
261
           if (cmd(1).ne.0 ) then
261
           if (cmd(1).ne.0 ) then
262
              call select_tra (isok,cmd,ncmd,trainp1,trigger1,ntim,ncol)
262
              call select_tra (isok,cmd,ncmd,trainp1,trigger1,ntim,ncol)
263
 
263
 
264
           else
264
           else
265
              call special    (isok,specialstr,trainp1,ntim,ncol,
265
              call special    (isok,specialstr,trainp1,ntim,ncol,
266
     >                         vars,time,param,nparam)
266
     >                         vars,time,param,nparam)
267
           endif
267
           endif
268
	    endif
268
	    endif
269
 
269
 
270
c       The trigger might be changed in the selection - copy it
270
c       The trigger might be changed in the selection - copy it
271
        do j=1,ntim
271
        do j=1,ntim
272
            trigger(i,j) = trigger1(j)
272
            trigger(i,j) = trigger1(j)
273
        enddo
273
        enddo
274
 
274
 
275
c       Set flag for selected trajectories
275
c       Set flag for selected trajectories
276
        if (isok.eq.1) then
276
        if (isok.eq.1) then
277
           selflag(i)          = 1
277
           selflag(i)          = 1
278
           ntraout             = ntraout + 1
278
           ntraout             = ntraout + 1
279
        else
279
        else
280
           selflag(i)          = 0
280
           selflag(i)          = 0
281
        endif
281
        endif
282
 
282
 
283
      enddo
283
      enddo
284
 
284
 
285
c     -------------------------------------------------------------- 
285
c     -------------------------------------------------------------- 
286
c     Write output trajectories
286
c     Write output trajectories
287
c     --------------------------------------------------------------
287
c     --------------------------------------------------------------
288
 
288
 
289
c     ------ Write output trajectories -----------------------------
289
c     ------ Write output trajectories -----------------------------
290
      if ( outformat.eq.'trajectory' ) then
290
      if ( outformat.eq.'trajectory' ) then
291
 
291
 
292
 
292
 
293
c        Define the trigger field if it is not yet defined
293
c        Define the trigger field if it is not yet defined
294
         if ( ( addtrigger.eq.'-trigger' ).and.(itrigger.eq.0) ) then
294
         if ( ( addtrigger.eq.'-trigger' ).and.(itrigger.eq.0) ) then
295
            ncol       = ncol + 1
295
            ncol       = ncol + 1
296
            vars(ncol) = 'TRIGGER'
296
            vars(ncol) = 'TRIGGER'
297
            itrigger   = ncol
297
            itrigger   = ncol
298
         endif
298
         endif
299
 
299
 
300
c        Allocate memory for output trajectory
300
c        Allocate memory for output trajectory
301
         allocate(traout(ntraout,ntim,ncol),stat=stat)
301
         allocate(traout(ntraout,ntim,ncol),stat=stat)
302
         if (stat.ne.0) stop '*** error allocating array apply   ***'
302
         if (stat.ne.0) stop '*** error allocating array apply   ***'
303
 
303
 
304
c        Set output trajectories
304
c        Set output trajectories
305
         j = 0
305
         j = 0
306
         do i=1,ntrainp
306
         do i=1,ntrainp
307
            if (selflag(i).eq.1) then
307
            if (selflag(i).eq.1) then
308
               j             = j + 1 
308
               j             = j + 1 
309
               traout(j,1:ntim,1:ncol) = trainp(i,1:ntim,1:ncol)
309
               traout(j,1:ntim,1:ncol) = trainp(i,1:ntim,1:ncol)
310
               if ( itrigger.ne.0 ) then
310
               if ( itrigger.ne.0 ) then
311
                  traout(j,1:ntim,itrigger) = real(trigger(i,1:ntim))
311
                  traout(j,1:ntim,itrigger) = real(trigger(i,1:ntim))
312
               endif
312
               endif
313
            endif
313
            endif
314
         enddo
314
         enddo
315
 
315
 
316
c        Write trajectories
316
c        Write trajectories
317
         call wopen_tra(fod,out_lslfile,ntraout,ntim,ncol,
317
         call wopen_tra(fod,out_lslfile,ntraout,ntim,ncol,
318
     >        basetime,vars,outmode)
318
     >        basetime,vars,outmode)
319
         call write_tra(fod,traout,ntraout,ntim,ncol,outmode)
319
         call write_tra(fod,traout,ntraout,ntim,ncol,outmode)
320
         call close_tra(fod,outmode)
320
         call close_tra(fod,outmode)
321
         
321
         
322
c     ------ Write index list -------------------------------------
322
c     ------ Write index list -------------------------------------
323
      elseif ( outformat.eq.'index' ) then
323
      elseif ( outformat.eq.'index' ) then
324
         
324
         
325
         open(10,file=out_lslfile)
325
         open(10,file=out_lslfile)
326
          do i=1,ntrainp
326
          do i=1,ntrainp
327
             if ( selflag(i).eq.1) write(10,*) i
327
             if ( selflag(i).eq.1) write(10,*) i
328
          enddo
328
          enddo
329
         close(10)
329
         close(10)
330
 
330
 
331
c     ------ Write boolean list -----------------------------------
331
c     ------ Write boolean list -----------------------------------
332
      elseif ( outformat.eq.'boolean' ) then
332
      elseif ( outformat.eq.'boolean' ) then
333
         
333
         
334
         open(10,file=out_lslfile)
334
         open(10,file=out_lslfile)
335
          do i=1,ntrainp
335
          do i=1,ntrainp
336
             write(10,'(i1)') selflag(i)
336
             write(10,'(i1)') selflag(i)
337
          enddo
337
          enddo
338
         close(10)
338
         close(10)
339
 
339
 
340
c     ------ Write count -------------------------------------------
340
c     ------ Write count -------------------------------------------
341
      elseif ( outformat.eq.'count' ) then
341
      elseif ( outformat.eq.'count' ) then
342
 
342
 
343
         open(10,file=out_lslfile)
343
         open(10,file=out_lslfile)
344
          write(10,'(i7)') ntraout
344
          write(10,'(i7)') ntraout
345
         close(10)
345
         close(10)
346
 
346
 
347
c     ------ Write starting positions -----------------------------
347
c     ------ Write starting positions -----------------------------
348
      elseif ( outformat.eq.'startf' ) then
348
      elseif ( outformat.eq.'startf' ) then
349
 
349
 
350
c        Allocate memory for output trajectory
350
c        Allocate memory for output trajectory
351
         allocate(traout(ntraout,1,ncol),stat=stat)
351
         allocate(traout(ntraout,1,ncol),stat=stat)
352
         if (stat.ne.0) stop '*** error allocating array apply   ***'
352
         if (stat.ne.0) stop '*** error allocating array apply   ***'
353
 
353
 
354
c        Set output trajectories
354
c        Set output trajectories
355
         j = 0
355
         j = 0
356
         do i=1,ntrainp
356
         do i=1,ntrainp
357
            if (selflag(i).eq.1) then
357
            if (selflag(i).eq.1) then
358
               j             = j + 1 
358
               j             = j + 1 
359
               traout(j,1,:) = trainp(i,1,:)
359
               traout(j,1,:) = trainp(i,1,:)
360
            endif
360
            endif
361
         enddo
361
         enddo
362
 
362
 
363
c        Write trajectories
363
c        Write trajectories
364
         if (outmode.ne.-1) then
364
         if (outmode.ne.-1) then
365
            
365
            
366
           call wopen_tra(fod,out_lslfile,ntraout,1,ncol,
366
           call wopen_tra(fod,out_lslfile,ntraout,1,ncol,
367
     >                    basetime,vars,outmode)
367
     >                    basetime,vars,outmode)
368
           call write_tra(fod,traout,ntraout,1,ncol,outmode)
368
           call write_tra(fod,traout,ntraout,1,ncol,outmode)
369
           call close_tra(fod,outmode)       
369
           call close_tra(fod,outmode)       
370
 
370
 
371
c        Output as a triple list (corresponding to <startf> file)
371
c        Output as a triple list (corresponding to <startf> file)
372
         else
372
         else
373
         
373
         
374
           fid = 10
374
           fid = 10
375
           open(fid,file=out_lslfile)
375
           open(fid,file=out_lslfile)
376
            do i=1,ntraout
376
            do i=1,ntraout
377
              write(fid,'(3f10.3)') traout(i,1,2),   ! longitude
377
              write(fid,'(3f10.3)') traout(i,1,2),   ! longitude
378
     >                            traout(i,1,3),   ! latitude
378
     >                            traout(i,1,3),   ! latitude
379
     >                            traout(i,1,4)    ! pressure
379
     >                            traout(i,1,4)    ! pressure
380
            enddo
380
            enddo
381
           close(fid)
381
           close(fid)
382
 
382
 
383
        endif
383
        endif
384
  
384
  
385
      endif
385
      endif
386
 
386
 
387
c     Write some status information, and end of program message
387
c     Write some status information, and end of program message
388
      print*  
388
      print*  
389
      print*,'---- STATUS INFORMATION --------------------------------'
389
      print*,'---- STATUS INFORMATION --------------------------------'
390
      print*
390
      print*
391
      print*,' # input trajectories  : ',ntrainp
391
      print*,' # input trajectories  : ',ntrainp
392
      print*,' # output trajectories : ',ntraout
392
      print*,' # output trajectories : ',ntraout
393
      print*
393
      print*
394
      print*,'              *** END OF PROGRAM SELECT ***'
394
      print*,'              *** END OF PROGRAM SELECT ***'
395
      print*,'========================================================='
395
      print*,'========================================================='
396
 
396
 
397
      stop
397
      stop
398
      
398
      
399
c     Exception handling
399
c     Exception handling
400
 100  stop 'select: First column in input trajectory must be <time>'
400
 100  stop 'select: First column in input trajectory must be <time>'
401
 101  stop 'select: Input trajectory file is empty'
401
 101  stop 'select: Input trajectory file is empty'
402
 
402
 
403
      end
403
      end
404
 
404
 
405
c     -------------------------------------------------------------- 
405
c     -------------------------------------------------------------- 
406
c     Dump the command list
406
c     Dump the command list
407
c     --------------------------------------------------------------
407
c     --------------------------------------------------------------
408
      
408
      
409
      subroutine dumpcode(out,n,vars,nvars,times,ntimes)
409
      subroutine dumpcode(out,n,vars,nvars,times,ntimes)
410
 
410
 
411
c     Write the command list to screen. The command list is decoded
411
c     Write the command list to screen. The command list is decoded
412
c     by call to <decode>
412
c     by call to <decode>
413
 
413
 
414
      implicit none
414
      implicit none
415
 
415
 
416
c     Declaration of subroutine parameters
416
c     Declaration of subroutine parameters
417
      integer      n
417
      integer      n
418
      real         out(n)
418
      real         out(n)
419
      integer      nvars
419
      integer      nvars
420
      character*80 vars(nvars)
420
      character*80 vars(nvars)
421
      integer      ntimes
421
      integer      ntimes
422
      real         times(ntimes)
422
      real         times(ntimes)
423
 
423
 
424
c     A single command
424
c     A single command
425
      character*80 cmd
425
      character*80 cmd
426
      character*80 var,mode,strtim
426
      character*80 var,mode,strtim
427
      integer      nval
427
      integer      nval
428
      integer      ntim
428
      integer      ntim
429
      integer      ivar,imode,icmd,itime
429
      integer      ivar,imode,icmd,itime
430
 
430
 
431
c     Auxiliary variables
431
c     Auxiliary variables
432
      integer      i,j
432
      integer      i,j
433
 
433
 
434
c     Loop through the complete list
434
c     Loop through the complete list
435
      i=0
435
      i=0
436
 100  if (i.lt.n) then 
436
 100  if (i.lt.n) then 
437
 
437
 
438
         write(*,*) '---------------------------------------'
438
         write(*,*) '---------------------------------------'
439
 
439
 
440
c        Get command
440
c        Get command
441
         i=i+1
441
         i=i+1
442
         icmd=nint(out(i))
442
         icmd=nint(out(i))
443
 
443
 
444
c        Special handling of SPECIAL commands
444
c        Special handling of SPECIAL commands
445
         if ( icmd.eq.0 ) then
445
         if ( icmd.eq.0 ) then
446
 
446
 
447
c           Write 'header' for SPECIAL command
447
c           Write 'header' for SPECIAL command
448
            write(*,'(i5,f15.4,10x,a10)') i,out(i),'SPECIAL'
448
            write(*,'(i5,f15.4,10x,a10)') i,out(i),'SPECIAL'
449
 
449
 
450
c           Write command string
450
c           Write command string
451
            i = i + 1
451
            i = i + 1
452
            icmd = nint(out(i))
452
            icmd = nint(out(i))
453
            write(*,'(i5,f15.4,10x,a10)') i,out(i),'LEN(CMD)'
453
            write(*,'(i5,f15.4,10x,a10)') i,out(i),'LEN(CMD)'
454
            do j=1,icmd
454
            do j=1,icmd
455
               i    = i + 1
455
               i    = i + 1
456
               ivar = nint(out(i))
456
               ivar = nint(out(i))
457
               write(*,'(i5,f15.4,10x,a10)') i,out(i),char(ivar)
457
               write(*,'(i5,f15.4,10x,a10)') i,out(i),char(ivar)
458
            enddo
458
            enddo
459
 
459
 
460
c           Write parameters
460
c           Write parameters
461
            i    = i + 1
461
            i    = i + 1
462
            nval = nint(out(i))
462
            nval = nint(out(i))
463
            write(*,'(i5,f15.4,10x,a10)') i,out(i),'#PARAMETER'
463
            write(*,'(i5,f15.4,10x,a10)') i,out(i),'#PARAMETER'
464
            do j=1,nval
464
            do j=1,nval
465
               i=i+1
465
               i=i+1
466
               if ( var.ne.'INPOLYGON' ) then
466
               if ( var.ne.'INPOLYGON' ) then
467
                  write(*,'(i5,f15.4)') i,out(i)
467
                  write(*,'(i5,f15.4)') i,out(i)
468
               else
468
               else
469
                  write(*,'(i5,f15.4,a2)') i,out(i),char(nint(out(i)))
469
                  write(*,'(i5,f15.4,a2)') i,out(i),char(nint(out(i)))
470
               endif
470
               endif
471
            enddo
471
            enddo
472
 
472
 
473
c           Nothing else to do - exit
473
c           Nothing else to do - exit
474
            goto 200
474
            goto 200
475
            
475
            
476
         endif
476
         endif
477
 
477
 
478
c        Set the command
478
c        Set the command
479
         if (icmd.eq.  1) cmd='GT'
479
         if (icmd.eq.  1) cmd='GT'
480
         if (icmd.eq.  2) cmd='LT'
480
         if (icmd.eq.  2) cmd='LT'
481
         if (icmd.eq.  3) cmd='IN'
481
         if (icmd.eq.  3) cmd='IN'
482
         if (icmd.eq.  4) cmd='OUT'
482
         if (icmd.eq.  4) cmd='OUT'
483
         if (icmd.eq.  5) cmd='EQ'
483
         if (icmd.eq.  5) cmd='EQ'
484
         if (icmd.eq.  6) cmd='TRUE'
484
         if (icmd.eq.  6) cmd='TRUE'
485
         if (icmd.eq.  7) cmd='FALSE'
485
         if (icmd.eq.  7) cmd='FALSE'
486
         if (icmd.eq.  8) cmd='ALL'
486
         if (icmd.eq.  8) cmd='ALL'
487
         if (icmd.eq.  9) cmd='ANY'
487
         if (icmd.eq.  9) cmd='ANY'
488
         if (icmd.eq. 10) cmd='NONE'
488
         if (icmd.eq. 10) cmd='NONE'
489
         if (icmd.eq. -1) cmd='BEGIN'
489
         if (icmd.eq. -1) cmd='BEGIN'
490
         if (icmd.eq. -2) cmd='END'
490
         if (icmd.eq. -2) cmd='END'
491
         if (icmd.eq. -3) cmd='AND'
491
         if (icmd.eq. -3) cmd='AND'
492
         if (icmd.eq. -4) cmd='OR'
492
         if (icmd.eq. -4) cmd='OR'
493
         write(*,'(i5,f15.4,10x,a10)') i,out(i),trim(cmd)
493
         write(*,'(i5,f15.4,10x,a10)') i,out(i),trim(cmd)
494
         if (icmd.lt.0) goto 100
494
         if (icmd.lt.0) goto 100
495
 
495
 
496
c        Get variable
496
c        Get variable
497
         i=i+1
497
         i=i+1
498
         ivar=nint(out(i))
498
         ivar=nint(out(i))
499
         if ( ivar.eq. -1 ) then
499
         if ( ivar.eq. -1 ) then
500
            var = 'DIST'
500
            var = 'DIST'
501
         elseif ( ivar.eq. -2 ) then
501
         elseif ( ivar.eq. -2 ) then
502
            var = 'DIST0'
502
            var = 'DIST0'
503
         elseif ( ivar.eq. -3 ) then
503
         elseif ( ivar.eq. -3 ) then
504
            var = 'INPOLYGON'
504
            var = 'INPOLYGON'
505
         elseif ( ivar.eq. -4 ) then
505
         elseif ( ivar.eq. -4 ) then
506
            var = 'INBOX'
506
            var = 'INBOX'
507
         elseif ( ivar.eq. -5 ) then
507
         elseif ( ivar.eq. -5 ) then
508
            var = 'INCIRCLE'
508
            var = 'INCIRCLE'
509
         elseif ( ivar.eq. -6 ) then
509
         elseif ( ivar.eq. -6 ) then
510
            var = 'INREGION'
510
            var = 'INREGION'
511
         elseif ( ivar.eq. -7 ) then
511
         elseif ( ivar.eq. -7 ) then
512
            var = 'TRIGGER'
512
            var = 'TRIGGER'
-
 
513
         elseif ( ivar.eq. -8 ) then
-
 
514
            var = 'VERT0'
513
         else
515
         else
514
            var=vars(ivar)
516
            var=vars(ivar)
515
         endif
517
         endif
516
         write(*,'(i5,f15.4,10x,a10)') i,out(i),trim(var)
518
         write(*,'(i5,f15.4,10x,a10)') i,out(i),trim(var)
517
 
519
 
518
c        Get variable mode
520
c        Get variable mode
519
         i=i+1
521
         i=i+1
520
         imode=nint(out(i))
522
         imode=nint(out(i))
521
         if (imode.eq.1) mode='VALUE'
523
         if (imode.eq.1) mode='VALUE'
522
         if (imode.eq.2) mode='MEAN'         
524
         if (imode.eq.2) mode='MEAN'         
523
         if (imode.eq.3) mode='MAX'
525
         if (imode.eq.3) mode='MAX'
524
         if (imode.eq.4) mode='MIN'
526
         if (imode.eq.4) mode='MIN'
525
         if (imode.eq.5) mode='VAR'
527
         if (imode.eq.5) mode='VAR'
526
         if (imode.eq.6) mode='SUM'
528
         if (imode.eq.6) mode='SUM'
527
         if (imode.eq.7) mode='CHANGE'
529
         if (imode.eq.7) mode='CHANGE'
528
         if (imode.eq.8) mode='DIFF'
530
         if (imode.eq.8) mode='DIFF'
-
 
531
         if (imode.eq.9) mode='RANGE'
529
         write(*,'(i5,f15.4,10x,a10)') i,out(i),trim(mode)
532
         write(*,'(i5,f15.4,10x,a10)') i,out(i),trim(mode)
530
 
533
 
531
c        Get values
534
c        Get values
532
         i=i+1
535
         i=i+1
533
         nval=nint(out(i))
536
         nval=nint(out(i))
534
         write(*,'(i5,f15.4,10x,a10)') i,out(i),'#PARAMETER'
537
         write(*,'(i5,f15.4,10x,a10)') i,out(i),'#PARAMETER'
535
         do j=1,nval
538
         do j=1,nval
536
            i=i+1
539
            i=i+1
537
 
540
 
538
            if ( var.ne.'INPOLYGON' ) then
541
            if ( var.ne.'INPOLYGON' ) then
539
               write(*,'(i5,f15.4)') i,out(i)
542
               write(*,'(i5,f15.4)') i,out(i)
540
            else
543
            else
541
               write(*,'(i5,f15.4,a2)') i,out(i),char(nint(out(i)))
544
               write(*,'(i5,f15.4,a2)') i,out(i),char(nint(out(i)))
542
            endif
545
            endif
543
         enddo
546
         enddo
544
 
547
 
545
c        Get times
548
c        Get the number of times
546
         i=i+1
549
         i=i+1
547
         ntim=nint(out(i))
550
         ntim=nint(out(i))
-
 
551
 
-
 
552
c        the number of times is variable - depending on TRIGGER
-
 
553
         if ( ntim .eq. -993 ) then
-
 
554
            write(*,'(i5,f15.4,7x,7x,a15)') i,out(i),'TIMES @ TRIGGER'
-
 
555
 
-
 
556
c        Get the detailed list of times
-
 
557
         else
548
         write(*,'(i5,f15.4,7x,7x,a6)') i,out(i),'#TIMES'
558
            write(*,'(i5,f15.4,7x,7x,a6)') i,out(i),'#TIMES'
549
         do j=1,ntim
559
            do j=1,ntim
550
            i=i+1
560
               i=i+1
551
            write(*,'(i5,f15.4,f7.0)') i,out(i),times(nint(out(i)))
561
               write(*,'(i5,f15.4,f7.0)') i,out(i),times(nint(out(i)))
-
 
562
            enddo
552
         enddo
563
         endif
553
 
564
 
554
c        Get time mode
565
c        Get time mode
555
         i=i+1
566
         i=i+1
556
         itime=nint(out(i))
567
         itime=nint(out(i))
557
         if (itime.eq.1) strtim='ALL'
568
         if (itime.eq.1) strtim='ALL'
558
         if (itime.eq.2) strtim='ANY'         
569
         if (itime.eq.2) strtim='ANY'         
559
         if (itime.eq.3) strtim='NONE'
570
         if (itime.eq.3) strtim='NONE'
560
         if (itime.lt.0) strtim='TRIGGER'
571
         if (itime.lt.0) strtim='TRIGGER'
561
 
572
 
562
         if ( strtim.ne.'TRIGGER' ) then
573
         if ( strtim.ne.'TRIGGER' ) then
563
            write(*,'(i5,f15.4,10x,a10)') i,out(i),trim(strtim)
574
            write(*,'(i5,f15.4,10x,a10)') i,out(i),trim(strtim)
564
         else
575
         else
565
            write(*,'(i5,f15.4,10x,a10,a3,i3)') i,out(i),trim(strtim),
576
            write(*,'(i5,f15.4,10x,a10,a3,i3)') i,out(i),trim(strtim),
566
     >                                        ' ->',abs(itime)
577
     >                                        ' ->',abs(itime)
567
         endif
578
         endif
568
 
579
 
569
         goto 100
580
         goto 100
570
         
581
         
571
      endif
582
      endif
572
 
583
 
573
c     Exit point
584
c     Exit point
574
 200  continue
585
 200  continue
575
 
586
 
576
      write(*,*) '---------------------------------------'
587
      write(*,*) '---------------------------------------'
577
 
588
 
578
      end
589
      end
579
         
590
         
580
 
591
 
581
c     -------------------------------------------------------------- 
592
c     -------------------------------------------------------------- 
582
c     Read and decode a selection set
593
c     Read and decode a selection set
583
c     --------------------------------------------------------------
594
c     --------------------------------------------------------------
584
 
595
 
585
      subroutine decode(fid,out,n,vars,nvars,times,ntimes,regionf)
596
      subroutine decode(fid,out,n,vars,nvars,times,ntimes,regionf)
586
 
597
 
587
c     A selection file is opened with file id <fid> and transformed
598
c     A selection file is opened with file id <fid> and transformed
588
c     into a set of commands applied to the trajectories. On input
599
c     into a set of commands applied to the trajectories. On input
589
c     <n> sets the maximum dimension of <out>, on output it gives the
600
c     <n> sets the maximum dimension of <out>, on output it gives the
590
c     total length of the command string. The output is a list of
601
c     total length of the command string. The output is a list of
591
c     commands with the following format:
602
c     commands with the following format:
592
c     
603
c     
593
c        out(i)             = Command 
604
c        out(i)             = Command 
594
c        out(i+1)           = Column index of variable
605
c        out(i+1)           = Column index of variable
595
c        out(i+2)           = Mode for variable
606
c        out(i+2)           = Mode for variable
596
c        out(i+3)           = Number of parameters (n)
607
c        out(i+3)           = Number of parameters (n)
597
c        out(i+4:i+4+n)     = Parameters
608
c        out(i+4:i+4+n)     = Parameters
598
c        out(i+5+n)         = Number of times 
609
c        out(i+5+n)         = Number of times 
599
c        out(i+6+n:i+6+n+m) = List of time indices (m)
610
c        out(i+6+n:i+6+n+m) = List of time indices (m)
600
c        out(i+7+n+m)       = Time mode
611
c        out(i+7+n+m)       = Time mode
601
c     
612
c     
602
c     For SPECIAL commands (to be coded in <special.f>) the format is:
613
c     For SPECIAL commands (to be coded in <special.f>) the format is:
603
c     
614
c     
604
c        out(i)             = Length of command string (n)
615
c        out(i)             = Length of command string (n)
605
c        out(i+1:i+n)       = Command string      
616
c        out(i+1:i+n)       = Command string      
606
c        out(i+n+1)         = Number of parameters (m)
617
c        out(i+n+1)         = Number of parameters (m)
607
c        out(i+n+2:i+n+1+m) = List of parameters
618
c        out(i+n+2:i+n+1+m) = List of parameters
608
c     
619
c     
609
c     The following coding is used
620
c     The following coding is used
610
c      
621
c      
611
c        Command        Variable mode    Time mode
622
c        Command        Variable mode    Time mode
612
c        ----------     -------------    ---------
623
c        ----------     -------------    ---------
613
c        GT       1     VALUE    1       ALL      1
624
c        GT       1     VALUE    1       ALL      1
614
c        LT       2     MEAN     2       ANY      2
625
c        LT       2     MEAN     2       ANY      2
615
c        IN       3     MAX      3       NONE     3
626
c        IN       3     MAX      3       NONE     3
616
c        OUT      4     MIN      4       TRIGGER -i (i the trigger index)
627
c        OUT      4     MIN      4       TRIGGER -i (i the trigger index)
617
c        EQ       5     VAR      5
628
c        EQ       5     VAR      5
618
c        BEGIN   -1     SUM      6
629
c        BEGIN   -1     SUM      6
619
c        END     -2     CHANGE   7 
630
c        END     -2     CHANGE   7 
620
c        AND     -3     DIFF     8
631
c        AND     -3     DIFF     8
621
c        OR      -4     
632
c        OR      -4     RANGE    9
622
c        TRUE     6
633
c        TRUE     6
623
c        FALSE    7
634
c        FALSE    7
624
c        SPECIAL  0
635
c        SPECIAL  0
625
c        ALL      8 (TRIGGER)
636
c        ALL      8 (TRIGGER)
626
c        ANY      9 (TRIGGER)
637
c        ANY      9 (TRIGGER)
627
c        NONE    10 (TRIGGER)
638
c        NONE    10 (TRIGGER)
628
 
639
 
629
 
640
 
630
c     Several "implicit variables" are supported - out(i+1):
641
c     Several "implicit variables" are supported - out(i+1):
631
c     
642
c     
632
c        DIST      -1 : Path length of the trajectory
643
c        DIST      -1 : Path length of the trajectory
633
c        DIST0     -2 : Distance from starting position
644
c        DIST0     -2 : Distance from starting position
634
c        INPOLYGON -3 : Specified polygon region
645
c        INPOLYGON -3 : Specified polygon region
635
c        INBOX     -4 : Longitude/latitude rectangle
646
c        INBOX     -4 : Longitude/latitude rectangle
636
c        INCIRCLE  -5 : Within a specified radius 
647
c        INCIRCLE  -5 : Within a specified radius 
637
c        INREGION  -6 : Within a specified rehion on the region file
648
c        INREGION  -6 : Within a specified rehion on the region file
638
c        TRIGGER   -7 : Trigger field
649
c        TRIGGER   -7 : Trigger field
-
 
650
c        VERT0     -8 : Vertical distance from starting position
639
c         
651
c         
640
c     For the special commands BEGIN, END, AND and OR, only one field
652
c     For the special commands BEGIN, END, AND and OR, only one field
641
c     in <out> is used.
653
c     in <out> is used.
642
 
654
 
643
      implicit none
655
      implicit none
644
 
656
 
645
c     Declaration of subroutine parameters
657
c     Declaration of subroutine parameters
646
      integer      fid
658
      integer      fid
647
      integer      n
659
      integer      n
648
      real         out(n)
660
      real         out(n)
649
      integer      nvars
661
      integer      nvars
650
      character*80 vars(nvars)
662
      character*80 vars(nvars)
651
      integer      ntimes
663
      integer      ntimes
652
      real         times(ntimes)
664
      real         times(ntimes)
653
      character*80 regionf
665
      character*80 regionf
654
 
666
 
655
c     Numerical epsilon
667
c     Numerical epsilon
656
      real         eps
668
      real         eps
657
      parameter    (eps=0.001)
669
      parameter    (eps=0.001)
658
 
670
 
659
c     A single command term
671
c     A single command term
660
      character*80 cmd
672
      character*80 cmd
661
      character*80 var,mode
673
      character*80 var,mode
662
      integer      nval
674
      integer      nval
663
      real         val(1000)
675
      real         val(1000)
664
      integer      ntim
676
      integer      ntim
665
      real         tim(1000)
677
      real         tim(1000)
666
      character*80 tmode
678
      character*80 tmode
667
 
679
 
668
c     Specification of a polygon
680
c     Specification of a polygon
669
      character*80 filename
681
      character*80 filename
670
      integer      pn                             ! Number of entries in lat/lon poly
682
      integer      pn                             ! Number of entries in lat/lon poly
671
      real         latpoly(1000)                  ! List of polygon latitudes
683
      real         latpoly(1000)                  ! List of polygon latitudes
672
      real         lonpoly(1000)                  ! List of polygon longitudes
684
      real         lonpoly(1000)                  ! List of polygon longitudes
673
      real         loninpoly,latinpoly            ! Lon/lat inside polygon
685
      real         loninpoly,latinpoly            ! Lon/lat inside polygon
674
 
686
 
675
c     Specification of a region
687
c     Specification of a region
676
      real         xcorner(4)
688
      real         xcorner(4)
677
      real         ycorner(4)
689
      real         ycorner(4)
678
      integer      iregion
690
      integer      iregion
679
      character*80 string
691
      character*80 string
680
 
692
 
681
c     Transformation to UPN (handling of logical operators)
693
c     Transformation to UPN (handling of logical operators)
682
      integer      nlogical
694
      integer      nlogical
683
      integer      ilogical(n)
695
      integer      ilogical(n)
684
      real         tmp(n)
696
      real         tmp(n)
685
      integer      mlogical
697
      integer      mlogical
686
      integer      isor
698
      integer      isor
687
 
699
 
688
c     Auxiliary variables
700
c     Auxiliary variables
689
      integer      i,j
701
      integer      i,j
690
      integer      j1,j2
702
      integer      j1,j2
691
      integer      flag(ntimes)
703
      integer      flag(ntimes)
692
      integer      count
704
      integer      count
693
      integer      ok
705
      integer      ok
694
      integer      itrigger
706
      integer      itrigger,ttrigger
695
 
707
 
696
c     Common block for initialisation of polygon check
708
c     Common block for initialisation of polygon check
697
      real    tlonv(1000),vlat_c(1000),vlon_c(1000)
709
      real    tlonv(1000),vlat_c(1000),vlon_c(1000)
698
      real    xlat_c,xlon_c
710
      real    xlat_c,xlon_c
699
      integer ibndry,nv_c
711
      integer ibndry,nv_c
700
      common  /spolybndry/vlat_c,vlon_c,nv_c,xlat_c,xlon_c,tlonv,ibndry
712
      common  /spolybndry/vlat_c,vlon_c,nv_c,xlat_c,xlon_c,tlonv,ibndry
701
 
713
 
702
c     ------ Decode single commands ---------------------
714
c     ------ Decode single commands ---------------------
703
 
715
 
704
c     Reset the filename for polygons
716
c     Reset the filename for polygons
705
      filename='nil'
717
      filename='nil'
706
 
718
 
707
c     Reset the counter for logical commands
719
c     Reset the counter for logical commands
708
      nlogical=0
720
      nlogical=0
709
 
721
 
710
c     Loop through all commands
722
c     Loop through all commands
711
 100  continue
723
 100  continue
712
 
724
 
713
c       Read next command (handle special cases)
725
c       Read next command (handle special cases)
714
        read(fid,*) cmd
726
        read(fid,*) cmd
715
 
727
 
716
c       Special handling of SPECIAL commands
728
c       Special handling of SPECIAL commands
717
        if ( cmd.eq.'SPECIAL' ) then
729
        if ( cmd.eq.'SPECIAL' ) then
718
 
730
 
719
c          Set the flag for SPECIAL command
731
c          Set the flag for SPECIAL command
720
           n      = 1
732
           n      = 1
721
           out(n) = 0.
733
           out(n) = 0.
722
 
734
 
723
c          Read the command string
735
c          Read the command string
724
           read(fid,*) var
736
           read(fid,*) var
725
 
737
 
726
c          Add the command string
738
c          Add the command string
727
           n      = n + 1
739
           n      = n + 1
728
           out(n) = len_trim(var)
740
           out(n) = len_trim(var)
729
           do j=1,len_trim(var)
741
           do j=1,len_trim(var)
730
              n      = n + 1
742
              n      = n + 1
731
              out(n) = ichar(var(j:j))
743
              out(n) = ichar(var(j:j))
732
           enddo
744
           enddo
733
 
745
 
734
c          Read the parameters
746
c          Read the parameters
735
           read(fid,*) nval
747
           read(fid,*) nval
736
           read(fid,*) (val(i),i=1,nval)
748
           read(fid,*) (val(i),i=1,nval)
737
 
749
 
738
c          Add the parameters
750
c          Add the parameters
739
           n = n + 1
751
           n = n + 1
740
           out(n)=real(nval)
752
           out(n)=real(nval)
741
           do i=1,nval
753
           do i=1,nval
742
              n=n+1
754
              n=n+1
743
              out(n)=val(i)
755
              out(n)=val(i)
744
           enddo
756
           enddo
745
 
757
 
746
c          Goto exit point - nothing more top do
758
c          Goto exit point - nothing more top do
747
           goto 350
759
           goto 350
748
 
760
 
749
        endif
761
        endif
750
 
762
 
751
c       Handle structure commands
763
c       Handle structure commands
752
        if ( cmd.eq.'BEGIN') then
764
        if ( cmd.eq.'BEGIN') then
753
           out(1)=-1.
765
           out(1)=-1.
754
           n=1
766
           n=1
755
           nlogical=1
767
           nlogical=1
756
           ilogical(1)=n
768
           ilogical(1)=n
757
           goto 200
769
           goto 200
758
        elseif ( cmd.eq.'AND'  ) then
770
        elseif ( cmd.eq.'AND'  ) then
759
           n=n+1
771
           n=n+1
760
           out(n)=-3.
772
           out(n)=-3.
761
           nlogical=nlogical+1
773
           nlogical=nlogical+1
762
           ilogical(nlogical)=n
774
           ilogical(nlogical)=n
763
           goto 200
775
           goto 200
764
        elseif ( cmd.eq.'OR'   ) then
776
        elseif ( cmd.eq.'OR'   ) then
765
           n=n+1
777
           n=n+1
766
           out(n)=-4.
778
           out(n)=-4.
767
           nlogical=nlogical+1
779
           nlogical=nlogical+1
768
           ilogical(nlogical)=n
780
           ilogical(nlogical)=n
769
           goto 200
781
           goto 200
770
        elseif ( cmd.eq.'END'  ) then
782
        elseif ( cmd.eq.'END'  ) then
771
           n=n+1
783
           n=n+1
772
           out(n)=-2.
784
           out(n)=-2.
773
           nlogical=nlogical+1
785
           nlogical=nlogical+1
774
           ilogical(nlogical)=n
786
           ilogical(nlogical)=n
775
           goto 300
787
           goto 300
776
        endif
788
        endif
777
 
789
 
778
c       Read other fields associated with the command
790
c       Read other fields associated with the command
779
        read(fid,*) var,mode
791
        read(fid,*) var,mode
780
 
792
 
781
c       Read parameter
793
c       Read parameter
782
        if ( var.eq.'INPOLYGON' ) then 
794
        if ( var.eq.'INPOLYGON' ) then 
783
           read(fid,*) nval
795
           read(fid,*) nval
784
           read(fid,*) filename
796
           read(fid,*) filename
785
           filename = trim(filename)
797
           filename = trim(filename)
786
        else
798
        else
787
           read(fid,*) nval
799
           read(fid,*) nval
788
           read(fid,*) (val(i),i=1,nval)
800
           read(fid,*) (val(i),i=1,nval)
789
        endif
801
        endif
790
 
802
 
791
c       Read times
803
c       Read times (on request, change to special trigger times)
792
        read(fid,*) ntim
804
        read(fid,*) ntim
-
 
805
        if ( ntim.eq.-993 ) then
-
 
806
           ttrigger = 1
-
 
807
           ntim     = 1
-
 
808
           tim(1)   = -999.
-
 
809
        else
-
 
810
           ttrigger = 0
793
        read(fid,*) (tim(i),i=1,ntim)
811
           read(fid,*) (tim(i),i=1,ntim)
-
 
812
        endif
794
        read(fid,*) tmode
813
        read(fid,*) tmode
795
 
814
 
796
c       Bring CAPITAL "TIME,LAT,LON,P" into "time,lat,lon,p"
815
c       Bring CAPITAL "TIME,LAT,LON,P" into "time,lat,lon,p"
797
        if (var.eq.'TIME') var='time'
816
        if (var.eq.'TIME') var='time'
798
        if (var.eq.'LAT' ) var='lat'
817
        if (var.eq.'LAT' ) var='lat'
799
        if (var.eq.'LON' ) var='lon'
818
        if (var.eq.'LON' ) var='lon'
800
        if (var.eq.'P'   ) var='p'
819
        if (var.eq.'P'   ) var='p'
801
 
820
 
802
c       If the time mode is 'TRIGGER', all times of a trajectory
821
c       If the time mode is 'TRIGGER', all times of a trajectory
803
c       must be considered
822
c       must be considered
804
        if ( tmode.eq.'TRIGGER' ) then
823
        if ( tmode.eq.'TRIGGER' ) then
805
           itrigger = nint(tim(1))
824
           itrigger = nint(tim(1))
806
           ntim     = 1
825
           ntim     = 1
807
           tim(1)   = -999.
826
           tim(1)   = -999.
808
        endif
827
        endif
809
 
828
 
810
c       Special times: transform into real time
829
c       Special times: transform into real time
811
        do i=1,ntim
830
        do i=1,ntim
812
           if ( abs(tim(i)+996.).lt.eps ) tim(i)=times(1)
831
           if ( abs(tim(i)+996.).lt.eps ) tim(i)=times(1)
813
           if ( abs(tim(i)+995.).lt.eps ) tim(i)=times(ntimes)
832
           if ( abs(tim(i)+995.).lt.eps ) tim(i)=times(ntimes)
814
        enddo
833
        enddo
815
 
834
 
816
c       Check whether times are valid 
835
c       Check whether times are valid 
817
        ok=0
836
        ok=0
818
        do i=1,ntim
837
        do i=1,ntim
819
           if ( (abs(tim(i)+994.).gt.eps).and.
838
           if ( (abs(tim(i)+994.).gt.eps).and.
820
     >          (abs(tim(i)+999.).gt.eps) )
839
     >          (abs(tim(i)+999.).gt.eps) )
821
     >     then
840
     >     then
822
              do j=1,ntimes
841
              do j=1,ntimes
823
                 if ( abs(tim(i)-times(j)).lt.eps ) then
842
                 if ( abs(tim(i)-times(j)).lt.eps ) then
824
                    ok=ok+1
843
                    ok=ok+1
825
                 endif
844
                 endif
826
              enddo
845
              enddo
827
           else
846
           else
828
              ok=ok+1
847
              ok=ok+1
829
           endif
848
           endif
830
        enddo
849
        enddo
831
        if (ok.ne.ntim) goto 400
850
        if (ok.ne.ntim) goto 400
832
        
851
        
833
c       Select all times which are included in the criterion
852
c       Select all times which are included in the criterion
834
        do i=1,ntimes
853
        do i=1,ntimes
835
           flag(i)=0
854
           flag(i)=0
836
        enddo
855
        enddo
837
        i=1
856
        i=1
838
 150    if (i.le.ntim) then
857
 150    if (i.le.ntim) then
839
           
858
           
840
c          A list of times
859
c          A list of times
841
           if ( (abs(tim(i)+994.).lt.eps) ) then
860
           if ( (abs(tim(i)+994.).lt.eps) ) then
842
              j1=0
861
              j1=0
843
              do j=1,ntimes
862
              do j=1,ntimes
844
                 if ( abs(tim(i-1)-times(j)).lt.eps ) then
863
                 if ( abs(tim(i-1)-times(j)).lt.eps ) then
845
                    j1=j
864
                    j1=j
846
                 endif
865
                 endif
847
              enddo
866
              enddo
848
              j2=0
867
              j2=0
849
              do j=1,ntimes
868
              do j=1,ntimes
850
                 if ( abs(tim(i+1)-times(j)).lt.eps ) then
869
                 if ( abs(tim(i+1)-times(j)).lt.eps ) then
851
                    j2=j
870
                    j2=j
852
                 endif
871
                 endif
853
              enddo   
872
              enddo   
854
              if ( (j1.eq.0).or.(j2.eq.0) ) goto 400
873
              if ( (j1.eq.0).or.(j2.eq.0) ) goto 400
855
              do j=j1,j2
874
              do j=j1,j2
856
                 flag(j)=1
875
                 flag(j)=1
857
              enddo
876
              enddo
858
              i=i+1
877
              i=i+1
859
 
878
 
860
c          Explicitly given time value
879
c          Explicitly given time value
861
           else
880
           else
862
              do j=1,ntimes
881
              do j=1,ntimes
863
                 if ( abs(tim(i)-times(j)).lt.eps ) then
882
                 if ( abs(tim(i)-times(j)).lt.eps ) then
864
                    flag(j)=1
883
                    flag(j)=1
865
                 endif
884
                 endif
866
              enddo
885
              enddo
867
              
886
              
868
           endif
887
           endif
869
 
888
 
870
           i=i+1
889
           i=i+1
871
           goto 150
890
           goto 150
872
 
891
 
873
        endif
892
        endif
874
 
893
 
875
c       Write command identifier
894
c       Write command identifier
876
        n=n+1
895
        n=n+1
877
        if (cmd.eq.'GT'    ) out(n)= 1.
896
        if (cmd.eq.'GT'    ) out(n)= 1.
878
        if (cmd.eq.'LT'    ) out(n)= 2.
897
        if (cmd.eq.'LT'    ) out(n)= 2.
879
        if (cmd.eq.'IN'    ) out(n)= 3.
898
        if (cmd.eq.'IN'    ) out(n)= 3.
880
        if (cmd.eq.'OUT'   ) out(n)= 4.
899
        if (cmd.eq.'OUT'   ) out(n)= 4.
881
        if (cmd.eq.'EQ'    ) out(n)= 5.
900
        if (cmd.eq.'EQ'    ) out(n)= 5.
882
        if (cmd.eq.'TRUE'  ) out(n)= 6.
901
        if (cmd.eq.'TRUE'  ) out(n)= 6.
883
        if (cmd.eq.'FALSE' ) out(n)= 7.
902
        if (cmd.eq.'FALSE' ) out(n)= 7.
884
        if (cmd.eq.'ALL  ' ) out(n)= 8.
903
        if (cmd.eq.'ALL  ' ) out(n)= 8.
885
        if (cmd.eq.'ANY  ' ) out(n)= 9.
904
        if (cmd.eq.'ANY  ' ) out(n)= 9.
886
        if (cmd.eq.'NONE ' ) out(n)=10.
905
        if (cmd.eq.'NONE ' ) out(n)=10.
887
 
906
 
888
c       Write index for variable - force implicit trigger
907
c       Write index for variable - force implicit trigger
889
        ok=0
908
        ok=0
890
        do j=1,nvars
909
        do j=1,nvars
891
           if (vars(j).eq.var) ok=j
910
           if (vars(j).eq.var) ok=j
892
        enddo
911
        enddo
893
 
912
 
894
        if (var.eq.'TRIGGER') ok = 0
913
        if (var.eq.'TRIGGER') ok = 0
895
        
914
        
896
        if (ok.eq.0) then
915
        if (ok.eq.0) then
897
           if (var.eq.'DIST') then
916
           if (var.eq.'DIST') then
898
              ok = -1
917
              ok = -1
899
           elseif (var.eq.'DIST0') then
918
           elseif (var.eq.'DIST0') then
900
              ok = -2
919
              ok = -2
901
           elseif (var.eq.'INPOLYGON') then
920
           elseif (var.eq.'INPOLYGON') then
902
              ok = -3
921
              ok = -3
903
           elseif (var.eq.'INBOX') then
922
           elseif (var.eq.'INBOX') then
904
              ok = -4
923
              ok = -4
905
           elseif (var.eq.'INCIRCLE') then
924
           elseif (var.eq.'INCIRCLE') then
906
              ok = -5
925
              ok = -5
907
           elseif (var.eq.'INREGION') then
926
           elseif (var.eq.'INREGION') then
908
              ok = -6
927
              ok = -6
909
           elseif (var.eq.'TRIGGER') then
928
           elseif (var.eq.'TRIGGER') then
910
              ok = -7
929
              ok = -7
-
 
930
           elseif (var.eq.'VERT0') then
-
 
931
              ok = -8
911
           else
932
           else
912
             goto 400
933
             goto 400
913
          endif
934
          endif
914
        endif
935
        endif
915
        n=n+1
936
        n=n+1
916
        out(n)=real(ok)
937
        out(n)=real(ok)
917
 
938
 
918
c       Write mode for variable
939
c       Write mode for variable
919
        ok=0
940
        ok=0
920
        if (mode.eq.'VALUE'  ) ok=1
941
        if (mode.eq.'VALUE'  ) ok=1
921
        if (mode.eq.'MEAN'   ) ok=2
942
        if (mode.eq.'MEAN'   ) ok=2
922
        if (mode.eq.'MAX'    ) ok=3
943
        if (mode.eq.'MAX'    ) ok=3
923
        if (mode.eq.'MIN'    ) ok=4
944
        if (mode.eq.'MIN'    ) ok=4
924
        if (mode.eq.'VAR'    ) ok=5
945
        if (mode.eq.'VAR'    ) ok=5
925
        if (mode.eq.'SUM'    ) ok=6
946
        if (mode.eq.'SUM'    ) ok=6
926
        if (mode.eq.'CHANGE' ) ok=7
947
        if (mode.eq.'CHANGE' ) ok=7
927
        if (mode.eq.'DIFF'   ) ok=8
948
        if (mode.eq.'DIFF'   ) ok=8
-
 
949
        if (mode.eq.'RANGE'  ) ok=9
928
        if (ok.eq.0) goto 400
950
        if (ok.eq.0) goto 400
929
        n=n+1
951
        n=n+1
930
        out(n)=real(ok)
952
        out(n)=real(ok)
931
 
953
 
932
c       Write the parameter values: INPOLYGON 
954
c       Write the parameter values: INPOLYGON 
933
        if ( var.eq.'INPOLYGON' ) then
955
        if ( var.eq.'INPOLYGON' ) then
934
 
956
 
935
           n      = n+1
957
           n      = n+1
936
           out(n) = len_trim(filename)
958
           out(n) = len_trim(filename)
937
           do j=1,len_trim(filename)
959
           do j=1,len_trim(filename)
938
              n      = n + 1
960
              n      = n + 1
939
              out(n) = ichar(filename(j:j))
961
              out(n) = ichar(filename(j:j))
940
           enddo
962
           enddo
941
 
963
 
942
c       Write parameter value: INREGION 
964
c       Write parameter value: INREGION 
943
        elseif ( var.eq.'INREGION' ) then
965
        elseif ( var.eq.'INREGION' ) then
944
 
966
 
945
           iregion = nint(val(1))
967
           iregion = nint(val(1))
946
 
968
 
947
           open(fid+1,file=regionf)          
969
           open(fid+1,file=regionf)          
948
 
970
 
949
 50        read(fid+1,*,end=51) string
971
 50        read(fid+1,*,end=51) string
950
 
972
 
951
           if ( string(1:1).ne.'#' ) then
973
           if ( string(1:1).ne.'#' ) then
952
              call regionsplit(string,i,xcorner,ycorner)
974
              call regionsplit(string,i,xcorner,ycorner)
953
              if ( i.eq.iregion ) goto 52
975
              if ( i.eq.iregion ) goto 52
954
           endif
976
           endif
955
 
977
 
956
           goto 50
978
           goto 50
957
           
979
           
958
 51        close(fid+1)        
980
 51        close(fid+1)        
959
 
981
 
960
           print*,' ERROR: region ',iregion,' not found on ',
982
           print*,' ERROR: region ',iregion,' not found on ',
961
     >                                         trim(regionf)
983
     >                                         trim(regionf)
962
           stop
984
           stop
963
 
985
 
964
 52        continue
986
 52        continue
965
           
987
           
966
           n      = n + 1
988
           n      = n + 1
967
           out(n) = 8                   ! Number of parameters
989
           out(n) = 8                   ! Number of parameters
968
           do i=1,4
990
           do i=1,4
969
              n      = n + 1
991
              n      = n + 1
970
              out(n) = xcorner(i)
992
              out(n) = xcorner(i)
971
           enddo
993
           enddo
972
           do i=1,4
994
           do i=1,4
973
              n      = n + 1
995
              n      = n + 1
974
              out(n) = ycorner(i)
996
              out(n) = ycorner(i)
975
           enddo
997
           enddo
976
 
998
 
977
c       Write parameter values: all other cases
999
c       Write parameter values: all other cases
978
        else
1000
        else
979
           n=n+1
1001
           n=n+1
980
           out(n)=real(nval)
1002
           out(n)=real(nval)
981
           do i=1,nval
1003
           do i=1,nval
982
              n=n+1
1004
              n=n+1
983
              out(n)=val(i)
1005
              out(n)=val(i)
984
           enddo
1006
           enddo
985
        endif
1007
        endif
986
 
1008
 
-
 
1009
c       Special time handling: only trigger times are cosidered
-
 
1010
        if ( ttrigger.eq.1 ) then
-
 
1011
           n = n+1
-
 
1012
           out(n)=-993.
-
 
1013
 
987
c       All times are selected
1014
c       All times are selected
988
        if ( abs(tim(1)+999.).lt.eps ) then
1015
        elseif ( abs(tim(1)+999.).lt.eps ) then
989
           n=n+1
1016
           n=n+1
990
           out(n)=real(ntimes)
1017
           out(n)=real(ntimes)
991
           do i=1,ntimes
1018
           do i=1,ntimes
992
              n=n+1
1019
              n=n+1
993
              out(n)=real(i)
1020
              out(n)=real(i)
994
           enddo
1021
           enddo
995
 
1022
 
996
c       A selection of times is given 
1023
c       A selection of times is given 
997
        else
1024
        else
998
           count=0
1025
           count=0
999
           do i=1,ntimes
1026
           do i=1,ntimes
1000
              count=count+flag(i)
1027
              count=count+flag(i)
1001
           enddo
1028
           enddo
1002
           n=n+1
1029
           n=n+1
1003
           out(n)=real(count)
1030
           out(n)=real(count)
1004
           do i=1,ntimes
1031
           do i=1,ntimes
1005
              if (flag(i).eq.1) then
1032
              if (flag(i).eq.1) then
1006
                 n=n+1
1033
                 n=n+1
1007
                 out(n)=real(i)
1034
                 out(n)=real(i)
1008
              endif
1035
              endif
1009
           enddo
1036
           enddo
1010
        endif
1037
        endif
1011
 
1038
 
1012
c       Write the time mode
1039
c       Write the time mode
1013
        if ( tmode.eq.'ALL') then
1040
        if ( tmode.eq.'ALL') then
1014
           n=n+1
1041
           n=n+1
1015
           out(n)=1.
1042
           out(n)=1.
1016
        elseif ( tmode.eq.'ANY') then
1043
        elseif ( tmode.eq.'ANY') then
1017
           n=n+1
1044
           n=n+1
1018
           out(n)=2.
1045
           out(n)=2.
1019
        elseif ( tmode.eq.'NONE') then
1046
        elseif ( tmode.eq.'NONE') then
1020
           n=n+1
1047
           n=n+1
1021
           out(n)=3.
1048
           out(n)=3.
1022
        elseif ( tmode.eq.'TRIGGER') then
1049
        elseif ( tmode.eq.'TRIGGER') then
1023
           n=n+1
1050
           n=n+1
1024
           out(n)=-real(itrigger)
1051
           out(n)=-real(itrigger)
1025
        endif
1052
        endif
1026
           
1053
           
1027
c     End loop: handle single command
1054
c     End loop: handle single command
1028
 200  continue
1055
 200  continue
1029
      goto 100
1056
      goto 100
1030
 
1057
 
1031
c     End loop: loop over all commands 
1058
c     End loop: loop over all commands 
1032
 300  continue
1059
 300  continue
1033
 
1060
 
1034
c     ------ Read polygon file, if requested -----------
1061
c     ------ Read polygon file, if requested -----------
1035
      if ( filename.ne.'nil' ) then
1062
      if ( filename.ne.'nil' ) then
1036
 
1063
 
1037
        print*
1064
        print*
1038
        print*,
1065
        print*,
1039
     >     '---- POLYGON --------------------------------------------'
1066
     >     '---- POLYGON --------------------------------------------'
1040
 
1067
 
1041
           print*
1068
           print*
1042
           print*,'Filename = ',trim(filename)
1069
           print*,'Filename = ',trim(filename)
1043
           print*
1070
           print*
1044
         
1071
         
1045
c        Read list of polygon coordinates from file
1072
c        Read list of polygon coordinates from file
1046
         pn = 0
1073
         pn = 0
1047
         open(fid+1,file=filename)
1074
         open(fid+1,file=filename)
1048
           read(fid+1,*) loninpoly,latinpoly
1075
           read(fid+1,*) loninpoly,latinpoly
1049
           print*,'Inside (lon/lat) =',loninpoly,latinpoly
1076
           print*,'Inside (lon/lat) =',loninpoly,latinpoly
1050
           print*
1077
           print*
1051
 510       continue
1078
 510       continue
1052
              pn = pn + 1
1079
              pn = pn + 1
1053
              read(fid+1,*,end=511) lonpoly(pn),
1080
              read(fid+1,*,end=511) lonpoly(pn),
1054
     >                              latpoly(pn)
1081
     >                              latpoly(pn)
1055
 
1082
 
1056
              print*,pn,lonpoly(pn),latpoly(pn)
1083
              print*,pn,lonpoly(pn),latpoly(pn)
1057
              
1084
              
1058
              goto 510
1085
              goto 510
1059
 511       continue
1086
 511       continue
1060
           pn = pn - 1
1087
           pn = pn - 1
1061
         close(fid+1)
1088
         close(fid+1)
1062
 
1089
 
1063
c        Define the polygon boundaries
1090
c        Define the polygon boundaries
1064
         call DefSPolyBndry(latpoly,lonpoly,pn,latinpoly,loninpoly)
1091
         call DefSPolyBndry(latpoly,lonpoly,pn,latinpoly,loninpoly)
1065
 
1092
 
1066
      endif
1093
      endif
1067
 
1094
 
1068
c     ------ Transform to UPN --------------------------
1095
c     ------ Transform to UPN --------------------------
1069
 
1096
 
1070
c     Check whether logical commands are ok
1097
c     Check whether logical commands are ok
1071
      mlogical=nint(out(ilogical(1)))
1098
      mlogical=nint(out(ilogical(1)))
1072
      if ( mlogical.ne.-1) goto 400
1099
      if ( mlogical.ne.-1) goto 400
1073
      mlogical=nint(out(ilogical(nlogical)))
1100
      mlogical=nint(out(ilogical(nlogical)))
1074
      if ( mlogical.ne.-2) goto 400
1101
      if ( mlogical.ne.-2) goto 400
1075
 
1102
 
1076
c     No transformation necessary if only one command
1103
c     No transformation necessary if only one command
1077
      if (nlogical.eq.2) goto 350
1104
      if (nlogical.eq.2) goto 350
1078
      
1105
      
1079
c     Copy the output to temporary list
1106
c     Copy the output to temporary list
1080
      do i=1,n
1107
      do i=1,n
1081
         tmp(i)=out(i)
1108
         tmp(i)=out(i)
1082
      enddo
1109
      enddo
1083
 
1110
 
1084
c     Set BEGIN statement
1111
c     Set BEGIN statement
1085
      n=1
1112
      n=1
1086
      out(n)=-1.
1113
      out(n)=-1.
1087
 
1114
 
1088
c     Reorder commands and transform to UPN
1115
c     Reorder commands and transform to UPN
1089
      isor=0
1116
      isor=0
1090
      do i=1,nlogical-1
1117
      do i=1,nlogical-1
1091
 
1118
 
1092
c        Get the logical command
1119
c        Get the logical command
1093
         mlogical=nint(out(ilogical(i)))
1120
         mlogical=nint(out(ilogical(i)))
1094
 
1121
 
1095
c        Connecting OR
1122
c        Connecting OR
1096
         if (mlogical.eq.-4) then
1123
         if (mlogical.eq.-4) then
1097
            if (isor.eq.1) then
1124
            if (isor.eq.1) then
1098
               n=n+1
1125
               n=n+1
1099
               out(n)=-4.
1126
               out(n)=-4.
1100
            else
1127
            else
1101
               isor=1
1128
               isor=1
1102
            endif
1129
            endif
1103
         endif
1130
         endif
1104
 
1131
 
1105
c        Put the command onto the stack
1132
c        Put the command onto the stack
1106
         do j=ilogical(i)+1,ilogical(i+1)-1
1133
         do j=ilogical(i)+1,ilogical(i+1)-1
1107
            n=n+1
1134
            n=n+1
1108
            out(n)=tmp(j)
1135
            out(n)=tmp(j)
1109
         enddo
1136
         enddo
1110
 
1137
 
1111
c        Connecting AND operator
1138
c        Connecting AND operator
1112
         if ( mlogical.eq.-3 ) then
1139
         if ( mlogical.eq.-3 ) then
1113
            n=n+1
1140
            n=n+1
1114
            out(n)=-3.
1141
            out(n)=-3.
1115
         endif
1142
         endif
1116
         
1143
         
1117
      enddo
1144
      enddo
1118
      
1145
      
1119
c     Set final connecting OR
1146
c     Set final connecting OR
1120
      if (isor.eq.1) then
1147
      if (isor.eq.1) then
1121
         n=n+1
1148
         n=n+1
1122
         out(n)=-4.
1149
         out(n)=-4.
1123
      endif
1150
      endif
1124
 
1151
 
1125
c     Set END statement
1152
c     Set END statement
1126
      n=n+1
1153
      n=n+1
1127
      out(n)=-2.
1154
      out(n)=-2.
1128
 
1155
 
1129
c     ------ Exit point ---------------------------------
1156
c     ------ Exit point ---------------------------------
1130
 
1157
 
1131
 350  continue
1158
 350  continue
1132
      return
1159
      return
1133
 
1160
 
1134
c     ----- Exception handling --------------------------
1161
c     ----- Exception handling --------------------------
1135
 400  print*,'Invalid selection criterion... Stop'
1162
 400  print*,'Invalid selection criterion... Stop'
1136
      stop
1163
      stop
1137
 
1164
 
1138
      end
1165
      end
1139
 
1166
 
1140
 
1167
 
1141
c     -------------------------------------------------------------- 
1168
c     -------------------------------------------------------------- 
1142
c     Decide whether a trajectory is selected or not
1169
c     Decide whether a trajectory is selected or not
1143
c     --------------------------------------------------------------
1170
c     --------------------------------------------------------------
1144
 
1171
 
1145
      subroutine select_tra (select,cmd,n,tra,trigger,ntim,ncol)
1172
      subroutine select_tra (select,cmd,n,tra,trigger,ntim,ncol)
1146
 
1173
 
1147
c     Decide whether a single trajectory is selected (<select=1>) or
1174
c     Decide whether a single trajectory is selected (<select=1>) or
1148
c     is not selected <select=0> according to the selection criterion
1175
c     is not selected <select=0> according to the selection criterion
1149
c     given in <cmd(ncmd)>. The selection criterion <cmd(ncmd)> is 
1176
c     given in <cmd(ncmd)>. The selection criterion <cmd(ncmd)> is 
1150
c     returned from the call to the subroutine <decode>. The trajectory
1177
c     returned from the call to the subroutine <decode>. The trajectory
1151
c     is given in <tra(ntim,ncol)> where <ntim> is the number of times
1178
c     is given in <tra(ntim,ncol)> where <ntim> is the number of times
1152
c     and <ncol> is the number of columns.
1179
c     and <ncol> is the number of columns.
1153
c     
1180
c     
1154
c     Important note: the structure of <tra(ntim,ncol)> must match to the
1181
c     Important note: the structure of <tra(ntim,ncol)> must match to the
1155
c     call parameter <vars,nvars,times,ntimes> in subroutine <decode>.
1182
c     call parameter <vars,nvars,times,ntimes> in subroutine <decode>.
1156
 
1183
 
1157
      implicit none
1184
      implicit none
1158
 
1185
 
1159
c     Declaration of subroutine parameters
1186
c     Declaration of subroutine parameters
1160
      integer   select
1187
      integer   select
1161
      integer   n
1188
      integer   n
1162
      real      cmd(n)
1189
      real      cmd(n)
1163
      integer   ntim,ncol
1190
      integer   ntim,ncol
1164
      real      tra(ntim,ncol)
1191
      real      tra(ntim,ncol)
1165
      integer   trigger(ntim)
1192
      integer   trigger(ntim)
1166
 
1193
 
1167
c     Numerical epsilon (for test of equality)
1194
c     Numerical epsilon (for test of equality)
1168
      real      eps
1195
      real      eps
1169
      parameter (eps=0.000001)
1196
      parameter (eps=0.000001)
1170
 
1197
 
1171
c     A single command and the associated field
1198
c     A single command and the associated field
1172
      integer   icmd,ivar,imode,itime,nsel,nval
1199
      integer   icmd,ivar,imode,itime,nsel,nval
1173
      integer   time(ntim)
1200
      integer   time(ntim)
1174
      real      param(100)
1201
      real      param(100)
1175
      real      var   (ntim)
1202
      real      var   (ntim)
1176
      integer   intvar(ntim)
1203
      integer   intvar(ntim)
1177
 
1204
 
1178
c     Boolean values for a single time, a single command and build-up
1205
c     Boolean values for a single time, a single command and build-up
1179
      integer   stack(100)
1206
      integer   stack(100)
1180
      integer   nstack
1207
      integer   nstack
1181
      integer   istrue(ntim)
1208
      integer   istrue(ntim)
1182
      integer   decision
1209
      integer   decision
1183
 
1210
 
1184
c     Auxiliary variables
1211
c     Auxiliary variables
1185
      integer   i,j,k
1212
      integer   i,j,k
1186
      real      tmp,mea
1213
      real      tmp,mea
1187
      integer   istack1,istack2
1214
      integer   istack1,istack2
1188
      real      lat0,lon0,lat1,lon1
1215
      real      lat0,lon0,lat1,lon1
1189
      real      length(ntim)
1216
      real      length(ntim)
1190
      integer   flag
1217
      integer   flag
1191
      real      dist
1218
      real      dist
1192
      real      xcorner(4),ycorner(4)
1219
      real      xcorner(4),ycorner(4)
1193
      integer   iparam
1220
      integer   iparam
1194
      character ch
1221
      character ch
-
 
1222
      real      varmin,varmax
-
 
1223
      real      lev0,lev1
1195
 
1224
 
1196
c     Common block for initialisation of polygon check
1225
c     Common block for initialisation of polygon check
1197
      real    tlonv(1000),vlat_c(1000),vlon_c(1000)
1226
      real    tlonv(1000),vlat_c(1000),vlon_c(1000)
1198
      real    xlat_c,xlon_c
1227
      real    xlat_c,xlon_c
1199
      integer ibndry,nv_c
1228
      integer ibndry,nv_c
1200
      common  /spolybndry/vlat_c,vlon_c,nv_c,xlat_c,xlon_c,tlonv,ibndry
1229
      common  /spolybndry/vlat_c,vlon_c,nv_c,xlat_c,xlon_c,tlonv,ibndry
1201
 
1230
 
1202
c     Externals
1231
c     Externals
1203
      real      sdis           ! Spherical distance
1232
      real      sdis           ! Spherical distance
1204
      external  sdis
1233
      external  sdis
1205
      integer   inregion       ! Boolean flag for regions
1234
      integer   inregion       ! Boolean flag for regions
1206
      external  inregion
1235
      external  inregion
1207
 
1236
 
1208
c     Reset the decision stack (with locical values)
1237
c     Reset the decision stack (with locical values)
1209
      nstack=0
1238
      nstack=0
1210
 
1239
 
1211
c     Loop through the complete command list
1240
c     Loop through the complete command list
1212
      i=0
1241
      i=0
1213
 100  if (i.lt.n) then  
1242
 100  if (i.lt.n) then  
1214
 
1243
 
1215
c        --- Get the command -------------------------------
1244
c        --- Get the command -------------------------------
1216
         i=i+1
1245
         i=i+1
1217
         icmd=nint(cmd(i))
1246
         icmd=nint(cmd(i))
1218
 
1247
 
1219
c        --- Handle structural commands (BEGIN, END, AND, OR)
1248
c        --- Handle structural commands (BEGIN, END, AND, OR)
1220
 
1249
 
1221
c        Handle BEGIN statement
1250
c        Handle BEGIN statement
1222
         if ( icmd.eq.-1) then
1251
         if ( icmd.eq.-1) then
1223
            nstack=0
1252
            nstack=0
1224
            goto 200
1253
            goto 200
1225
         endif
1254
         endif
1226
 
1255
 
1227
c        Handle END statement
1256
c        Handle END statement
1228
         if (icmd.eq.-2) then
1257
         if (icmd.eq.-2) then
1229
            goto 300
1258
            goto 300
1230
         endif
1259
         endif
1231
 
1260
 
1232
c        Handle AND statement
1261
c        Handle AND statement
1233
         if (icmd.eq.-3) then
1262
         if (icmd.eq.-3) then
1234
            istack1=stack(nstack)
1263
            istack1=stack(nstack)
1235
            nstack=nstack-1
1264
            nstack=nstack-1
1236
            istack2=stack(nstack)
1265
            istack2=stack(nstack)
1237
            if ((istack1.eq.1).and.(istack2.eq.1)) then
1266
            if ((istack1.eq.1).and.(istack2.eq.1)) then
1238
               stack(nstack)=1
1267
               stack(nstack)=1
1239
            else
1268
            else
1240
               stack(nstack)=0
1269
               stack(nstack)=0
1241
            endif
1270
            endif
1242
            goto 200
1271
            goto 200
1243
         endif
1272
         endif
1244
            
1273
            
1245
c        Handle OR statement
1274
c        Handle OR statement
1246
         if (icmd.eq.-4) then
1275
         if (icmd.eq.-4) then
1247
            istack1=stack(nstack)
1276
            istack1=stack(nstack)
1248
            nstack=nstack-1
1277
            nstack=nstack-1
1249
            istack2=stack(nstack)
1278
            istack2=stack(nstack)
1250
            if ((istack1.eq.1).or.(istack2.eq.1)) then
1279
            if ((istack1.eq.1).or.(istack2.eq.1)) then
1251
               stack(nstack)=1
1280
               stack(nstack)=1
1252
            else
1281
            else
1253
               stack(nstack)=0
1282
               stack(nstack)=0
1254
            endif
1283
            endif
1255
            goto 200
1284
            goto 200
1256
         endif
1285
         endif
1257
 
1286
 
1258
c        --- Get all command details (parameters, modes, times)
1287
c        --- Get all command details (parameters, modes, times)
1259
 
1288
 
1260
c        Get variable (<ivar> gets the column index in <tra>)
1289
c        Get variable (<ivar> gets the column index in <tra>)
1261
         i=i+1
1290
         i=i+1
1262
         ivar=nint(cmd(i))
1291
         ivar=nint(cmd(i))
1263
         
1292
         
1264
c        Get variable mode 
1293
c        Get variable mode 
1265
         i=i+1
1294
         i=i+1
1266
         imode=nint(cmd(i))
1295
         imode=nint(cmd(i))
1267
 
1296
 
1268
c        Get parameter values
1297
c        Get parameter values
1269
         i=i+1
1298
         i=i+1
1270
         nval=nint(cmd(i))
1299
         nval=nint(cmd(i))
1271
         do j=1,nval
1300
         do j=1,nval
1272
            i=i+1
1301
            i=i+1
1273
            param(j)=cmd(i)
1302
            param(j)=cmd(i)
1274
         enddo
1303
         enddo
1275
         
1304
         
1276
c        Get times (<time(j)> gets the row indices of <tra>)
1305
c        Get times (<time(j)> gets the row indices of <tra>)
1277
         i=i+1
1306
         i=i+1
1278
         nsel=nint(cmd(i))
1307
         nsel=nint(cmd(i))
-
 
1308
         if ( nsel .eq. -993 ) then
-
 
1309
            nsel = 0
-
 
1310
            do k=1,ntim
-
 
1311
              if ( trigger(k).ne.0 ) then
-
 
1312
                nsel       = nsel + 1
-
 
1313
                time(nsel) = k
-
 
1314
              endif
-
 
1315
           enddo
-
 
1316
         else
1279
         do j=1,nsel
1317
           do j=1,nsel
1280
            i=i+1
1318
              i=i+1
1281
            time(j)=nint(cmd(i))
1319
              time(j)=nint(cmd(i))
1282
         enddo
1320
           enddo
-
 
1321
         endif
-
 
1322
 
-
 
1323
c        If no times are selected, exit with non-select status
-
 
1324
         if ( nsel.eq.0 ) then
-
 
1325
             stack(1) = 0
-
 
1326
             goto 300
-
 
1327
         endif
1283
 
1328
 
1284
c        Get time mode
1329
c        Get time mode
1285
         i=i+1
1330
         i=i+1
1286
         itime=nint(cmd(i))
1331
         itime=nint(cmd(i))
1287
 
1332
 
1288
c        --- Prepare field values for analysis -----------
1333
c        --- Prepare field values for analysis -----------
1289
 
1334
 
1290
c        Implicit variable: DIST
1335
c        Implicit variable: DIST
1291
         if ( ivar.eq. -1 ) then
1336
         if ( ivar.eq. -1 ) then
1292
            length(1) = 0.
1337
            length(1) = 0.
1293
            do j=2,ntim
1338
            do j=2,ntim
1294
               lon0      = tra(j-1,2)
1339
               lon0      = tra(j-1,2)
1295
               lat0      = tra(j-1,3)
1340
               lat0      = tra(j-1,3)
1296
               lon1      = tra(j  ,2)
1341
               lon1      = tra(j  ,2)
1297
               lat1      = tra(j  ,3)
1342
               lat1      = tra(j  ,3)
1298
               length(j) = length(j-1) + sdis(lon0,lat0,lon1,lat1)
1343
               length(j) = length(j-1) + sdis(lon0,lat0,lon1,lat1)
1299
            enddo
1344
            enddo
1300
            do j=1,nsel
1345
            do j=1,nsel
1301
               var(j) = length( time(j) )
1346
               var(j) = length( time(j) )
1302
            enddo
1347
            enddo
1303
 
1348
 
1304
               
1349
               
1305
c        Implict variable: DIST0
1350
c        Implict variable: DIST0
1306
         elseif ( ivar.eq. -2 ) then
1351
         elseif ( ivar.eq. -2 ) then
1307
            do j=1,nsel
1352
            do j=1,nsel
1308
               lon0   = tra(1      ,2)
1353
               lon0   = tra(1      ,2)
1309
               lat0   = tra(1      ,3)
1354
               lat0   = tra(1      ,3)
1310
               lon1   = tra(time(j),2)
1355
               lon1   = tra(time(j),2)
1311
               lat1   = tra(time(j),3)
1356
               lat1   = tra(time(j),3)
1312
               var(j) = sdis(lon0,lat0,lon1,lat1)
1357
               var(j) = sdis(lon0,lat0,lon1,lat1)
1313
            enddo
1358
            enddo
1314
 
1359
 
1315
c        Implict variable: INPOLYGON
1360
c        Implict variable: INPOLYGON
1316
         elseif ( ivar.eq. -3 ) then
1361
         elseif ( ivar.eq. -3 ) then
1317
            do j=1,nsel
1362
            do j=1,nsel
1318
               lon1   = tra(time(j),2)
1363
               lon1   = tra(time(j),2)
1319
               lat1   = tra(time(j),3)               
1364
               lat1   = tra(time(j),3)               
1320
               call LctPtRelBndry(lat1,lon1,flag)
1365
               call LctPtRelBndry(lat1,lon1,flag)
1321
               if ( (flag.eq.1).or.(flag.eq.2) ) then
1366
               if ( (flag.eq.1).or.(flag.eq.2) ) then
1322
                  var(j) = 1.
1367
                  var(j) = 1.
1323
               else
1368
               else
1324
                  var(j) = 0.
1369
                  var(j) = 0.
1325
               endif
1370
               endif
1326
            enddo
1371
            enddo
1327
 
1372
 
1328
c        Implict variable: INBOX 
1373
c        Implict variable: INBOX 
1329
         elseif ( ivar.eq. -4 ) then
1374
         elseif ( ivar.eq. -4 ) then
1330
            do j=1,nsel
1375
            do j=1,nsel
1331
               lon1   = tra(time(j),2)
1376
               lon1   = tra(time(j),2)
1332
               lat1   = tra(time(j),3)               
1377
               lat1   = tra(time(j),3)               
1333
               if ( ( lon1.ge.param(1) ).and.       ! lonmin
1378
               if ( ( lon1.ge.param(1) ).and.       ! lonmin
1334
     >              ( lon1.le.param(2) ).and.       ! lonmax
1379
     >              ( lon1.le.param(2) ).and.       ! lonmax
1335
     >              ( lat1.ge.param(3) ).and.       ! latmin
1380
     >              ( lat1.ge.param(3) ).and.       ! latmin
1336
     >              ( lat1.le.param(4) ) )          ! latmax
1381
     >              ( lat1.le.param(4) ) )          ! latmax
1337
     >         then
1382
     >         then
1338
                  var(j) = 1
1383
                  var(j) = 1
1339
               else
1384
               else
1340
                  var(j) = 0
1385
                  var(j) = 0
1341
               endif
1386
               endif
1342
            enddo
1387
            enddo
1343
 
1388
 
1344
c        Implict variable: INCIRCLE (lonc=param(1),latc=param(2),radius=param(3))
1389
c        Implict variable: INCIRCLE (lonc=param(1),latc=param(2),radius=param(3))
1345
         elseif ( ivar.eq. -5 ) then
1390
         elseif ( ivar.eq. -5 ) then
1346
            do j=1,nsel
1391
            do j=1,nsel
1347
               lon1   = tra(time(j),2)
1392
               lon1   = tra(time(j),2)
1348
               lat1   = tra(time(j),3)               
1393
               lat1   = tra(time(j),3)               
1349
               
1394
               
1350
               dist = sdis( lon1,lat1,param(1),param(2) ) 
1395
               dist = sdis( lon1,lat1,param(1),param(2) ) 
1351
 
1396
 
1352
               if ( dist.le.param(3) ) then
1397
               if ( dist.le.param(3) ) then
1353
                  var(j) = 1
1398
                  var(j) = 1
1354
               else
1399
               else
1355
                  var(j) = 0
1400
                  var(j) = 0
1356
               endif
1401
               endif
1357
            enddo
1402
            enddo
1358
 
1403
 
1359
c        Implict variable: INREGION (xcorner=param(1..4),ycorner=param(5..8) )
1404
c        Implict variable: INREGION (xcorner=param(1..4),ycorner=param(5..8) )
1360
         elseif ( ivar.eq.-6 ) then
1405
         elseif ( ivar.eq.-6 ) then
1361
            
1406
            
1362
            do j=1,4
1407
            do j=1,4
1363
               xcorner(j) = param(j  )
1408
               xcorner(j) = param(j  )
1364
               ycorner(j) = param(j+4)
1409
               ycorner(j) = param(j+4)
1365
            enddo
1410
            enddo
1366
 
1411
 
1367
            do j=1,nsel
1412
            do j=1,nsel
1368
               lon1   = tra(time(j),2)
1413
               lon1   = tra(time(j),2)
1369
               lat1   = tra(time(j),3)  
1414
               lat1   = tra(time(j),3)  
1370
               var(j) = inregion (lon1,lat1,xcorner,ycorner)               
1415
               var(j) = inregion (lon1,lat1,xcorner,ycorner)               
1371
            enddo
1416
            enddo
1372
 
1417
 
1373
c        Implict variable: TRIGGER
1418
c        Implict variable: TRIGGER
1374
         elseif ( ivar.eq. -7 ) then
1419
         elseif ( ivar.eq. -7 ) then
1375
            do j=1,nsel
1420
            do j=1,nsel
1376
               intvar(j) = trigger( time(j) ) 
1421
               intvar(j) = trigger( time(j) ) 
1377
            enddo
1422
            enddo
-
 
1423
 
-
 
1424
c        Implicit variable: VERT0
-
 
1425
         elseif ( ivar.eq. -8 ) then
-
 
1426
            do j=1,nsel
-
 
1427
               lev0   = tra(1      ,4)
-
 
1428
               lev1   = tra(time(j),4)
-
 
1429
               var(j) = lev0 - lev1
1378
         
1430
            enddo
1379
 
1431
 
1380
c        Explicit variable (column index <ivar>)
1432
c        Explicit variable (column index <ivar>)
1381
         else
1433
         else
1382
            do j=1,nsel
1434
            do j=1,nsel
1383
               var(j) = tra(time(j),ivar)
1435
               var(j) = tra(time(j),ivar)
1384
            enddo
1436
            enddo
1385
 
1437
 
1386
         endif
1438
         endif
1387
 
1439
 
1388
c        Take MEAN of the variable (mean of selected times)
1440
c        Take MEAN of the variable (mean of selected times)
1389
         if (imode.eq.2) then
1441
         if (imode.eq.2) then
1390
            tmp=0.
1442
            tmp=0.
1391
            do j=1,nsel
1443
            do j=1,nsel
1392
               tmp=tmp+var(j)
1444
               tmp=tmp+var(j)
1393
            enddo
1445
            enddo
1394
            var(1)=tmp/real(nsel)
1446
            var(1)=tmp/real(nsel)
1395
            nsel=1
1447
            nsel=1
1396
 
1448
 
1397
c        Take MAX of the variable (maximum of selected times)
1449
c        Take MAX of the variable (maximum of selected times)
1398
         elseif (imode.eq.3) then
1450
         elseif (imode.eq.3) then
1399
            tmp=var(1)
1451
            tmp=var(1)
1400
            do j=2,nsel
1452
            do j=2,nsel
1401
               if (var(j).gt.tmp) tmp=var(j)
1453
               if (var(j).gt.tmp) tmp=var(j)
1402
            enddo
1454
            enddo
1403
            var(1)=tmp
1455
            var(1)=tmp
1404
            nsel=1
1456
            nsel=1
1405
            
1457
            
1406
c        Take MIN of the variable (minimum of selected times)
1458
c        Take MIN of the variable (minimum of selected times)
1407
         elseif (imode.eq.4) then
1459
         elseif (imode.eq.4) then
1408
            tmp=var(1)
1460
            tmp=var(1)
1409
            do j=2,nsel
1461
            do j=2,nsel
1410
               if (var(j).lt.tmp) tmp=var(j)
1462
               if (var(j).lt.tmp) tmp=var(j)
1411
            enddo
1463
            enddo
1412
            var(1)=tmp
1464
            var(1)=tmp
1413
            nsel=1
1465
            nsel=1
1414
 
1466
 
1415
c        Take VAR of the variable (variance over all selected times)
1467
c        Take VAR of the variable (variance over all selected times)
1416
         elseif (imode.eq.5) then
1468
         elseif (imode.eq.5) then
1417
            tmp=0.
1469
            tmp=0.
1418
            do j=1,nsel
1470
            do j=1,nsel
1419
               tmp=tmp+var(j)
1471
               tmp=tmp+var(j)
1420
            enddo
1472
            enddo
1421
            mea=tmp/real(nsel)
1473
            mea=tmp/real(nsel)
1422
            do j=1,nsel
1474
            do j=1,nsel
1423
              tmp=tmp+(var(j)-mea)**2
1475
              tmp=tmp+(var(j)-mea)**2
1424
            enddo
1476
            enddo
1425
            var(1)=1./real(nsel-1)*tmp
1477
            var(1)=1./real(nsel-1)*tmp
1426
            nsel=1
1478
            nsel=1
1427
 
1479
 
1428
c        Take SUM of the variable (sum over all selected times)
1480
c        Take SUM of the variable (sum over all selected times)
1429
         elseif (imode.eq.6) then
1481
         elseif (imode.eq.6) then
1430
            tmp=0.
1482
            tmp=0.
1431
            do j=1,nsel
1483
            do j=1,nsel
1432
               tmp=tmp+var(j)
1484
               tmp=tmp+var(j)
1433
            enddo
1485
            enddo
1434
            var(1)=tmp
1486
            var(1)=tmp
1435
            nsel=1
1487
            nsel=1
1436
            
1488
            
1437
c        Take CHANGE of the variable (change between first and last time)
1489
c        Take CHANGE of the variable (absolute difference between first and last time)
1438
         elseif (imode.eq.7) then
1490
         elseif (imode.eq.7) then
1439
            var(1)=abs(var(1)-var(nsel))
1491
            var(1)=abs(var(1)-var(nsel))
1440
            nsel=1
1492
            nsel=1
1441
 
1493
 
1442
c        Take DIFF of the variable (first minus last time)
1494
c        Take DIFF of the variable (first minus last time)
1443
         elseif (imode.eq.8) then
1495
         elseif (imode.eq.8) then
1444
            var(1)=var(1)-var(nsel)
1496
            var(1)=var(1)-var(nsel)
1445
            nsel=1
1497
            nsel=1
1446
 
1498
 
-
 
1499
c        Take RANGE of the variable
-
 
1500
         elseif (imode.eq.9) then
-
 
1501
            varmax=var(1)
-
 
1502
            varmin=var(1)
-
 
1503
            do j=2,nsel
-
 
1504
               if (var(j).gt.varmax) varmax=var(j)
-
 
1505
               if (var(j).lt.varmin) varmin=var(j)
-
 
1506
            enddo
-
 
1507
            var(1) = varmax - varmin
-
 
1508
            nsel=1
1447
         endif
1509
         endif
1448
 
1510
 
1449
c        --- Apply the operators to the single values ---
1511
c        --- Apply the operators to the single values ---
1450
 
1512
 
1451
         do j=1,nsel
1513
         do j=1,nsel
1452
 
1514
 
1453
c           GT
1515
c           GT
1454
            if (icmd.eq.1) then
1516
            if (icmd.eq.1) then
1455
               if (var(j).gt.param(1)) then
1517
               if (var(j).gt.param(1)) then
1456
                  istrue(j)=1
1518
                  istrue(j)=1
1457
               else
1519
               else
1458
                  istrue(j)=0
1520
                  istrue(j)=0
1459
               endif
1521
               endif
1460
               
1522
               
1461
c           LT
1523
c           LT
1462
            elseif (icmd.eq.2) then
1524
            elseif (icmd.eq.2) then
1463
               if (var(j).lt.param(1)) then
1525
               if (var(j).lt.param(1)) then
1464
                  istrue(j)=1
1526
                  istrue(j)=1
1465
               else
1527
               else
1466
                  istrue(j)=0
1528
                  istrue(j)=0
1467
               endif
1529
               endif
1468
 
1530
 
1469
c           IN
1531
c           IN
1470
            elseif (icmd.eq.3) then
1532
            elseif (icmd.eq.3) then
1471
               if ( (var(j).gt.param(1)).and.
1533
               if ( (var(j).gt.param(1)).and.
1472
     >              (var(j).lt.param(2)) ) 
1534
     >              (var(j).lt.param(2)) ) 
1473
     >         then
1535
     >         then
1474
                  istrue(j)=1
1536
                  istrue(j)=1
1475
               else
1537
               else
1476
                  istrue(j)=0
1538
                  istrue(j)=0
1477
               endif
1539
               endif
1478
 
1540
 
1479
c           OUT
1541
c           OUT
1480
            elseif (icmd.eq.4) then
1542
            elseif (icmd.eq.4) then
1481
               if ( (var(j).lt.param(1)).or.
1543
               if ( (var(j).lt.param(1)).or.
1482
     >              (var(j).gt.param(2)) ) 
1544
     >              (var(j).gt.param(2)) ) 
1483
     >         then
1545
     >         then
1484
                  istrue(j)=1
1546
                  istrue(j)=1
1485
               else
1547
               else
1486
                  istrue(j)=0
1548
                  istrue(j)=0
1487
               endif
1549
               endif
1488
 
1550
 
1489
c           EQ
1551
c           EQ
1490
            elseif (icmd.eq.5) then
1552
            elseif (icmd.eq.5) then
1491
               if (abs(var(j)-param(1)).lt.eps) then
1553
               if (abs(var(j)-param(1)).lt.eps) then
1492
                  istrue(j)=1
1554
                  istrue(j)=1
1493
               else
1555
               else
1494
                  istrue(j)=0
1556
                  istrue(j)=0
1495
               endif
1557
               endif
1496
 
1558
 
1497
c           TRUE
1559
c           TRUE
1498
            elseif (icmd.eq.6) then
1560
            elseif (icmd.eq.6) then
1499
               if (abs(var(j)).lt.eps) then
1561
               if (abs(var(j)).lt.eps) then
1500
                  istrue(j)=0
1562
                  istrue(j)=0
1501
               else
1563
               else
1502
                  istrue(j)=1
1564
                  istrue(j)=1
1503
               endif
1565
               endif
1504
 
1566
 
1505
c           FALSE
1567
c           FALSE
1506
            elseif (icmd.eq.7) then
1568
            elseif (icmd.eq.7) then
1507
               if (abs(var(j)).lt.eps) then
1569
               if (abs(var(j)).lt.eps) then
1508
                  istrue(j)=1
1570
                  istrue(j)=1
1509
               else
1571
               else
1510
                  istrue(j)=0
1572
                  istrue(j)=0
1511
               endif
1573
               endif
1512
 
1574
 
1513
c           ALL
1575
c           ALL
1514
            elseif (icmd.eq.8) then
1576
            elseif (icmd.eq.8) then
1515
               istrue(j) = 1
1577
               istrue(j) = 1
1516
               do k=1,nval
1578
               do k=1,nval
1517
                  iparam = nint(param(k))-1
1579
                  iparam = nint(param(k))-1
1518
                  if (btest(intvar(j),iparam).eqv..false.) then
1580
                  if (btest(intvar(j),iparam).eqv..false.) then
1519
                     istrue(j) = 0
1581
                     istrue(j) = 0
1520
                  endif
1582
                  endif
1521
               enddo
1583
               enddo
1522
               
1584
               
1523
c           ANY
1585
c           ANY
1524
            elseif (icmd.eq.9) then
1586
            elseif (icmd.eq.9) then
1525
               istrue(j) = 0
1587
               istrue(j) = 0
1526
               do k=1,nval
1588
               do k=1,nval
1527
                  iparam = nint(param(k))-1
1589
                  iparam = nint(param(k))-1
1528
                  if (btest(intvar(j),iparam).eqv..true.) then
1590
                  if (btest(intvar(j),iparam).eqv..true.) then
1529
                     istrue(j) = 1
1591
                     istrue(j) = 1
1530
                  endif
1592
                  endif
1531
               enddo
1593
               enddo
1532
 
1594
 
1533
c           NONE
1595
c           NONE
1534
            elseif (icmd.eq.10) then
1596
            elseif (icmd.eq.10) then
1535
               istrue(j) = 1
1597
               istrue(j) = 1
1536
               do k=1,nval
1598
               do k=1,nval
1537
                  iparam = nint(param(k))-1
1599
                  iparam = nint(param(k))-1
1538
                  if (btest(intvar(j),iparam).eqv..true.) then
1600
                  if (btest(intvar(j),iparam).eqv..true.) then
1539
                     istrue(j) = 0
1601
                     istrue(j) = 0
1540
                  endif
1602
                  endif
1541
               enddo
1603
               enddo
1542
   
1604
   
1543
            endif
1605
            endif
1544
 
1606
 
1545
         enddo
1607
         enddo
1546
 
1608
 
1547
c        --- Determine the overall boolean value ----------
1609
c        --- Determine the overall boolean value ----------
1548
            
1610
            
1549
c        ALL
1611
c        ALL
1550
         if (itime.eq.1) then
1612
         if (itime.eq.1) then
1551
            decision=1
1613
            decision=1
1552
            do j=1,nsel
1614
            do j=1,nsel
1553
               if (istrue(j).eq.0) then
1615
               if (istrue(j).eq.0) then
1554
                  decision=0
1616
                  decision=0
1555
                  goto 110
1617
                  goto 110
1556
               endif
1618
               endif
1557
            enddo
1619
            enddo
1558
 110        continue
1620
 110        continue
1559
 
1621
 
1560
c        ANY
1622
c        ANY
1561
         elseif (itime.eq.2) then
1623
         elseif (itime.eq.2) then
1562
            decision=0
1624
            decision=0
1563
            do j=1,nsel
1625
            do j=1,nsel
1564
               if (istrue(j).eq.1) then
1626
               if (istrue(j).eq.1) then
1565
                  decision=1
1627
                  decision=1
1566
                  goto 120
1628
                  goto 120
1567
               endif
1629
               endif
1568
            enddo
1630
            enddo
1569
 120        continue
1631
 120        continue
1570
           
1632
           
1571
c        NONE
1633
c        NONE
1572
         elseif (itime.eq.3) then
1634
         elseif (itime.eq.3) then
1573
            decision=1
1635
            decision=1
1574
            do j=1,nsel
1636
            do j=1,nsel
1575
               if (istrue(j).eq.1) then
1637
               if (istrue(j).eq.1) then
1576
                  decision=0
1638
                  decision=0
1577
                  goto 130
1639
                  goto 130
1578
               endif
1640
               endif
1579
            enddo
1641
            enddo
1580
 130        continue
1642
 130        continue
1581
 
1643
 
1582
c        TRIGGER
1644
c        TRIGGER
1583
         elseif (itime.lt.0) then
1645
         elseif (itime.lt.0) then
1584
            decision=1
1646
            decision=1
1585
            do j=1,nsel
1647
            do j=1,nsel
1586
               if (istrue(j).eq.1) then
1648
               if (istrue(j).eq.1) then
1587
                  trigger(j) = ior( trigger(j), 2**(abs(itime)-1) )
1649
                  trigger(j) = ior( trigger(j), 2**(abs(itime)-1) )
1588
               endif
1650
               endif
1589
            enddo
1651
            enddo
1590
            
1652
            
1591
         endif
1653
         endif
1592
 
1654
 
1593
c        --- Put the new boolean value onto the stack
1655
c        --- Put the new boolean value onto the stack
1594
 
1656
 
1595
         nstack=nstack+1
1657
         nstack=nstack+1
1596
         stack(nstack)=decision
1658
         stack(nstack)=decision
1597
 
1659
 
1598
c        Exit point for loop
1660
c        Exit point for loop
1599
 200     continue
1661
 200     continue
1600
         goto 100
1662
         goto 100
1601
 
1663
 
1602
      endif
1664
      endif
1603
 
1665
 
1604
c     Return the decision (selected or non-selected)
1666
c     Return the decision (selected or non-selected)
1605
 300  continue
1667
 300  continue
1606
 
1668
 
1607
      select=stack(1)
1669
      select=stack(1)
1608
 
1670
 
1609
      end
1671
      end
1610
      
1672
      
1611
 
1673
 
1612
c     --------------------------------------------------------------------------
1674
c     --------------------------------------------------------------------------
1613
c     Split a region string and get corners of the domain
1675
c     Split a region string and get corners of the domain
1614
c     --------------------------------------------------------------------------
1676
c     --------------------------------------------------------------------------
1615
 
1677
 
1616
      subroutine regionsplit(string,iregion,xcorner,ycorner)
1678
      subroutine regionsplit(string,iregion,xcorner,ycorner)
1617
 
1679
 
1618
c     The region string comes either as <lonw,lone,lats,latn> or as <lon1,lat1,
1680
c     The region string comes either as <lonw,lone,lats,latn> or as <lon1,lat1,
1619
c     lon2,lat2,lon3,lat3,lon4,lat4>: split it into ints components and get the
1681
c     lon2,lat2,lon3,lat3,lon4,lat4>: split it into ints components and get the
1620
c     four coordinates for the region
1682
c     four coordinates for the region
1621
      
1683
      
1622
      implicit none
1684
      implicit none
1623
 
1685
 
1624
c     Declaration of subroutine parameters
1686
c     Declaration of subroutine parameters
1625
      character*80    string
1687
      character*80    string
1626
      real            xcorner(4),ycorner(4)
1688
      real            xcorner(4),ycorner(4)
1627
      integer         iregion
1689
      integer         iregion
1628
 
1690
 
1629
c     Local variables
1691
c     Local variables
1630
      integer         i,n
1692
      integer         i,n
1631
      integer         il,ir
1693
      integer         il,ir
1632
      real            subfloat (80)
1694
      real            subfloat (80)
1633
      integer         stat
1695
      integer         stat
1634
      integer         len
1696
      integer         len
1635
 
1697
 
1636
c     ------- Split the string
1698
c     ------- Split the string
1637
      i    = 1
1699
      i    = 1
1638
      n    = 0
1700
      n    = 0
1639
      stat = 0
1701
      stat = 0
1640
      il   = 1
1702
      il   = 1
1641
      len  = len_trim(string)
1703
      len  = len_trim(string)
1642
 
1704
 
1643
 100  continue
1705
 100  continue
1644
 
1706
 
1645
c     Find start of a substring
1707
c     Find start of a substring
1646
      do while ( stat.eq.0 )
1708
      do while ( stat.eq.0 )
1647
         if ( string(i:i).ne.' ' ) then
1709
         if ( string(i:i).ne.' ' ) then
1648
            stat = 1
1710
            stat = 1
1649
            il   = i
1711
            il   = i
1650
         else
1712
         else
1651
            i = i + 1
1713
            i = i + 1
1652
         endif
1714
         endif
1653
      enddo
1715
      enddo
1654
 
1716
 
1655
c     Find end of substring
1717
c     Find end of substring
1656
      do while ( stat.eq.1 )         
1718
      do while ( stat.eq.1 )         
1657
         if ( ( string(i:i).eq.' ' ) .or. ( i.eq.len ) ) then
1719
         if ( ( string(i:i).eq.' ' ) .or. ( i.eq.len ) ) then
1658
            stat = 2
1720
            stat = 2
1659
            ir   = i
1721
            ir   = i
1660
         else
1722
         else
1661
            i    = i + 1
1723
            i    = i + 1
1662
         endif
1724
         endif
1663
      enddo
1725
      enddo
1664
 
1726
 
1665
c     Convert the substring into a number
1727
c     Convert the substring into a number
1666
      if ( stat.eq.2 ) then
1728
      if ( stat.eq.2 ) then
1667
         n = n + 1
1729
         n = n + 1
1668
         read(string(il:ir),*) subfloat(n)
1730
         read(string(il:ir),*) subfloat(n)
1669
         stat = 0
1731
         stat = 0
1670
      endif
1732
      endif
1671
 
1733
 
1672
      if ( i.lt.len ) goto 100
1734
      if ( i.lt.len ) goto 100
1673
 
1735
 
1674
 
1736
 
1675
c     -------- Get the region number
1737
c     -------- Get the region number
1676
      
1738
      
1677
      iregion = nint(subfloat(1))
1739
      iregion = nint(subfloat(1))
1678
 
1740
 
1679
c     -------- Get the corners of the region
1741
c     -------- Get the corners of the region
1680
      
1742
      
1681
      if ( n.eq.5 ) then     ! lonw(2),lone(3),lats(4),latn(5)
1743
      if ( n.eq.5 ) then     ! lonw(2),lone(3),lats(4),latn(5)
1682
 
1744
 
1683
         xcorner(1) = subfloat(2)
1745
         xcorner(1) = subfloat(2)
1684
         ycorner(1) = subfloat(4)
1746
         ycorner(1) = subfloat(4)
1685
 
1747
 
1686
         xcorner(2) = subfloat(3)
1748
         xcorner(2) = subfloat(3)
1687
         ycorner(2) = subfloat(4)
1749
         ycorner(2) = subfloat(4)
1688
 
1750
 
1689
         xcorner(3) = subfloat(3)
1751
         xcorner(3) = subfloat(3)
1690
         ycorner(3) = subfloat(5)
1752
         ycorner(3) = subfloat(5)
1691
         
1753
         
1692
         xcorner(4) = subfloat(2)
1754
         xcorner(4) = subfloat(2)
1693
         ycorner(4) = subfloat(5)
1755
         ycorner(4) = subfloat(5)
1694
        
1756
        
1695
      elseif ( n.eq.9 ) then     ! lon1,lat1,lon2,lat2,lon3,lon4,lat4
1757
      elseif ( n.eq.9 ) then     ! lon1,lat1,lon2,lat2,lon3,lon4,lat4
1696
 
1758
 
1697
         xcorner(1) = subfloat(2)
1759
         xcorner(1) = subfloat(2)
1698
         ycorner(1) = subfloat(3)
1760
         ycorner(1) = subfloat(3)
1699
 
1761
 
1700
         xcorner(2) = subfloat(4)
1762
         xcorner(2) = subfloat(4)
1701
         ycorner(2) = subfloat(5)
1763
         ycorner(2) = subfloat(5)
1702
 
1764
 
1703
         xcorner(3) = subfloat(6)
1765
         xcorner(3) = subfloat(6)
1704
         ycorner(3) = subfloat(7)
1766
         ycorner(3) = subfloat(7)
1705
         
1767
         
1706
         xcorner(4) = subfloat(8)
1768
         xcorner(4) = subfloat(8)
1707
         ycorner(4) = subfloat(9)
1769
         ycorner(4) = subfloat(9)
1708
 
1770
 
1709
      else
1771
      else
1710
         
1772
         
1711
         print*,' ERROR: invalid region specification '
1773
         print*,' ERROR: invalid region specification '
1712
         print*,'     ',trim(string)
1774
         print*,'     ',trim(string)
1713
         stop
1775
         stop
1714
         
1776
         
1715
      endif
1777
      endif
1716
         
1778
         
1717
 
1779
 
1718
      end
1780
      end
1719
 
1781
 
1720
c     --------------------------------------------------------------------------
1782
c     --------------------------------------------------------------------------
1721
c     Decide whether lat/lon point is in or out of region
1783
c     Decide whether lat/lon point is in or out of region
1722
c     --------------------------------------------------------------------------
1784
c     --------------------------------------------------------------------------
1723
      
1785
      
1724
      integer function inregion (lon,lat,xcorner,ycorner)
1786
      integer function inregion (lon,lat,xcorner,ycorner)
1725
      
1787
      
1726
c     Decide whether point (lon/lat) is in the region specified by <xcorner(1..4),
1788
c     Decide whether point (lon/lat) is in the region specified by <xcorner(1..4),
1727
c     ycorner(1..4).
1789
c     ycorner(1..4).
1728
      
1790
      
1729
      implicit none
1791
      implicit none
1730
      
1792
      
1731
c     Declaration of subroutine parameters
1793
c     Declaration of subroutine parameters
1732
      real    lon,lat
1794
      real    lon,lat
1733
      real    xcorner(4),ycorner(4)
1795
      real    xcorner(4),ycorner(4)
1734
 
1796
 
1735
c     Local variables
1797
c     Local variables
1736
      integer flag
1798
      integer flag
1737
      real    xmin,xmax,ymin,ymax
1799
      real    xmin,xmax,ymin,ymax
1738
      integer i
1800
      integer i
1739
 
1801
 
1740
c     Reset the flag
1802
c     Reset the flag
1741
      flag = 0
1803
      flag = 0
1742
 
1804
 
1743
c     Set some boundaries
1805
c     Set some boundaries
1744
      xmax = xcorner(1)
1806
      xmax = xcorner(1)
1745
      xmin = xcorner(1)
1807
      xmin = xcorner(1)
1746
      ymax = ycorner(1)
1808
      ymax = ycorner(1)
1747
      ymin = ycorner(1)
1809
      ymin = ycorner(1)
1748
      do i=2,4
1810
      do i=2,4
1749
        if (xcorner(i).lt.xmin) xmin = xcorner(i)
1811
        if (xcorner(i).lt.xmin) xmin = xcorner(i)
1750
        if (xcorner(i).gt.xmax) xmax = xcorner(i)
1812
        if (xcorner(i).gt.xmax) xmax = xcorner(i)
1751
        if (ycorner(i).lt.ymin) ymin = ycorner(i)
1813
        if (ycorner(i).lt.ymin) ymin = ycorner(i)
1752
        if (ycorner(i).gt.ymax) ymax = ycorner(i)
1814
        if (ycorner(i).gt.ymax) ymax = ycorner(i)
1753
      enddo
1815
      enddo
1754
 
1816
 
1755
c     Do the tests - set flag=1 if all tests pased
1817
c     Do the tests - set flag=1 if all tests pased
1756
      if (lon.lt.xmin) goto 970
1818
      if (lon.lt.xmin) goto 970
1757
      if (lon.gt.xmax) goto 970
1819
      if (lon.gt.xmax) goto 970
1758
      if (lat.lt.ymin) goto 970
1820
      if (lat.lt.ymin) goto 970
1759
      if (lat.gt.ymax) goto 970
1821
      if (lat.gt.ymax) goto 970
1760
      
1822
      
1761
      if ((lon-xcorner(1))*(ycorner(2)-ycorner(1))-
1823
      if ((lon-xcorner(1))*(ycorner(2)-ycorner(1))-
1762
     >    (lat-ycorner(1))*(xcorner(2)-xcorner(1)).gt.0.) goto 970
1824
     >    (lat-ycorner(1))*(xcorner(2)-xcorner(1)).gt.0.) goto 970
1763
      if ((lon-xcorner(2))*(ycorner(3)-ycorner(2))-
1825
      if ((lon-xcorner(2))*(ycorner(3)-ycorner(2))-
1764
     >    (lat-ycorner(2))*(xcorner(3)-xcorner(2)).gt.0.) goto 970
1826
     >    (lat-ycorner(2))*(xcorner(3)-xcorner(2)).gt.0.) goto 970
1765
      if ((lon-xcorner(3))*(ycorner(4)-ycorner(3))-
1827
      if ((lon-xcorner(3))*(ycorner(4)-ycorner(3))-
1766
     >    (lat-ycorner(3))*(xcorner(4)-xcorner(3)).gt.0.) goto 970
1828
     >    (lat-ycorner(3))*(xcorner(4)-xcorner(3)).gt.0.) goto 970
1767
      if ((lon-xcorner(4))*(ycorner(1)-ycorner(4))-
1829
      if ((lon-xcorner(4))*(ycorner(1)-ycorner(4))-
1768
     >    (lat-ycorner(4))*(xcorner(1)-xcorner(4)).gt.0.) goto 970
1830
     >    (lat-ycorner(4))*(xcorner(1)-xcorner(4)).gt.0.) goto 970
1769
 
1831
 
1770
      flag = 1
1832
      flag = 1
1771
 
1833
 
1772
c     Return the value
1834
c     Return the value
1773
 970  continue
1835
 970  continue
1774
      
1836
      
1775
      inregion = flag
1837
      inregion = flag
1776
      
1838
      
1777
      return
1839
      return
1778
      
1840
      
1779
      end
1841
      end
1780
 
1842
 
1781
   
1843
   
1782
c     --------------------------------------------------------------------------
1844
c     --------------------------------------------------------------------------
1783
c     Spherical distance between lat/lon points                                                       
1845
c     Spherical distance between lat/lon points                                                       
1784
c     --------------------------------------------------------------------------
1846
c     --------------------------------------------------------------------------
1785
 
1847
 
1786
      real function sdis(xp,yp,xq,yq)
1848
      real function sdis(xp,yp,xq,yq)
1787
c
1849
c
1788
c     calculates spherical distance (in km) between two points given
1850
c     calculates spherical distance (in km) between two points given
1789
c     by their spherical coordinates (xp,yp) and (xq,yq), respectively.
1851
c     by their spherical coordinates (xp,yp) and (xq,yq), respectively.
1790
c
1852
c
1791
      real      re
1853
      real      re
1792
      parameter (re=6370.)
1854
      parameter (re=6370.)
1793
      real      pi180
1855
      real      pi180
1794
      parameter (pi180=3.14159/180.)
1856
      parameter (pi180=3.14159/180.)
1795
      real      xp,yp,xq,yq,arg
1857
      real      xp,yp,xq,yq,arg
1796
 
1858
 
1797
      arg=sin(pi180*yp)*sin(pi180*yq)+
1859
      arg=sin(pi180*yp)*sin(pi180*yq)+
1798
     >    cos(pi180*yp)*cos(pi180*yq)*cos(pi180*(xp-xq))
1860
     >    cos(pi180*yp)*cos(pi180*yq)*cos(pi180*(xp-xq))
1799
      if (arg.lt.-1.) arg=-1.
1861
      if (arg.lt.-1.) arg=-1.
1800
      if (arg.gt.1.) arg=1.
1862
      if (arg.gt.1.) arg=1.
1801
 
1863
 
1802
      sdis=re*acos(arg)
1864
      sdis=re*acos(arg)
1803
 
1865
 
1804
      end
1866
      end
1805
 
1867
 
1806
 
1868
 
1807
c     ****************************************************************
1869
c     ****************************************************************
1808
c     * Given some spherical polygon S and some point X known to be  *
1870
c     * Given some spherical polygon S and some point X known to be  *
1809
c     * located inside S, these routines will determine if an arbit- *
1871
c     * located inside S, these routines will determine if an arbit- *
1810
c     * -rary point P lies inside S, outside S, or on its boundary.  *
1872
c     * -rary point P lies inside S, outside S, or on its boundary.  *
1811
c     * The calling program must first call DefSPolyBndry to define  *
1873
c     * The calling program must first call DefSPolyBndry to define  *
1812
c     * the boundary of S and the point X. Any subsequent call to    *
1874
c     * the boundary of S and the point X. Any subsequent call to    *
1813
c     * subroutine LctPtRelBndry will determine if some point P lies *
1875
c     * subroutine LctPtRelBndry will determine if some point P lies *
1814
c     * inside or outside S, or on its boundary. (Usually            *
1876
c     * inside or outside S, or on its boundary. (Usually            *
1815
c     * DefSPolyBndry is called once, then LctPrRelBndry is called   *
1877
c     * DefSPolyBndry is called once, then LctPrRelBndry is called   *
1816
c     * many times).                                                 *
1878
c     * many times).                                                 *
1817
c     *                                                              * 
1879
c     *                                                              * 
1818
c     * REFERENCE:            Bevis, M. and Chatelain, J.-L. (1989)  * 
1880
c     * REFERENCE:            Bevis, M. and Chatelain, J.-L. (1989)  * 
1819
c     *                       Maflaematical Geology, vol 21.         *
1881
c     *                       Maflaematical Geology, vol 21.         *
1820
c     * VERSION 1.0                                                  *
1882
c     * VERSION 1.0                                                  *
1821
c     ****************************************************************
1883
c     ****************************************************************
1822
 
1884
 
1823
      Subroutine DefSPolyBndry(vlat,vlon,nv,xlat, xlon)
1885
      Subroutine DefSPolyBndry(vlat,vlon,nv,xlat, xlon)
1824
 
1886
 
1825
c     ****************************************************************
1887
c     ****************************************************************
1826
c     * This mmn entry point is used m define ~e spheric~ polygon S  *
1888
c     * This mmn entry point is used m define ~e spheric~ polygon S  *
1827
c     * and the point X.                                             *
1889
c     * and the point X.                                             *
1828
c     * ARGUMENTS:                                                   *
1890
c     * ARGUMENTS:                                                   *
1829
c     * vlat,vlon (sent) ... vectors containing the latitude and     * 
1891
c     * vlat,vlon (sent) ... vectors containing the latitude and     * 
1830
c     *                      longitude of each vertex of the         *
1892
c     *                      longitude of each vertex of the         *
1831
c     *                      spherical polygon S. The ith.vertex is  *
1893
c     *                      spherical polygon S. The ith.vertex is  *
1832
c     *                      located at [vlat(i),vlon(i)].           *
1894
c     *                      located at [vlat(i),vlon(i)].           *
1833
c     * nv        (sent) ... the number of vertices and sides in the *
1895
c     * nv        (sent) ... the number of vertices and sides in the *
1834
c     *                      spherical polygon S                     *
1896
c     *                      spherical polygon S                     *
1835
c     * xlat,xlon (sent) ... latitude and longitude of some point X  *
1897
c     * xlat,xlon (sent) ... latitude and longitude of some point X  *
1836
c     *                      located inside S. X must not be located *
1898
c     *                      located inside S. X must not be located *
1837
c     *                      on any great circle that includes two   *
1899
c     *                      on any great circle that includes two   *
1838
c     *                      vertices of S.                          *
1900
c     *                      vertices of S.                          *
1839
c     *                                                              *
1901
c     *                                                              *
1840
c     * UNITS AND SIGN CONVENTION:                                   *
1902
c     * UNITS AND SIGN CONVENTION:                                   *
1841
c     *  Latitudes and longitudes are specified in degrees.          *
1903
c     *  Latitudes and longitudes are specified in degrees.          *
1842
c     *  Latitudes are positive to the north and negative to the     *
1904
c     *  Latitudes are positive to the north and negative to the     *
1843
c     *  south.                                                      *
1905
c     *  south.                                                      *
1844
c     *  Longitudes are positive to the east and negative to the     *
1906
c     *  Longitudes are positive to the east and negative to the     *
1845
c     *  west.                                                       *
1907
c     *  west.                                                       *
1846
c     *                                                              * 
1908
c     *                                                              * 
1847
c     * VERTEX ENUMERATION:                                          * 
1909
c     * VERTEX ENUMERATION:                                          * 
1848
c     * The vertices of S should be numbered sequentially around the *
1910
c     * The vertices of S should be numbered sequentially around the *
1849
c     * border of the spherical polygon. Vertex 1 lies between vertex*
1911
c     * border of the spherical polygon. Vertex 1 lies between vertex*
1850
c     * nv and vertex 2. Neighbouring vertices must be seperated by  *
1912
c     * nv and vertex 2. Neighbouring vertices must be seperated by  *
1851
c     * less than 180 degrees. (In order to generate a polygon side  *
1913
c     * less than 180 degrees. (In order to generate a polygon side  *
1852
c     * whose arc length equals or exceeds 180 degrees simply        *
1914
c     * whose arc length equals or exceeds 180 degrees simply        *
1853
c     * introduce an additional (pseudo)vertex). Having chosen       *
1915
c     * introduce an additional (pseudo)vertex). Having chosen       *
1854
c     * vertex 1, the user may number the remaining vertices in      *
1916
c     * vertex 1, the user may number the remaining vertices in      *
1855
c     * either direction. However if the user wishes to use the      *
1917
c     * either direction. However if the user wishes to use the      *
1856
c     * subroutine SPA to determine the area of the polygon S (Bevis *
1918
c     * subroutine SPA to determine the area of the polygon S (Bevis *
1857
c     * & Cambareri, 1987, Math. Geol., v.19, p. 335-346) then he or *
1919
c     * & Cambareri, 1987, Math. Geol., v.19, p. 335-346) then he or *
1858
c     * she must follow the convention whereby in moving around the  *
1920
c     * she must follow the convention whereby in moving around the  *
1859
c     * polygon border in the direction of increasing vertex number  *
1921
c     * polygon border in the direction of increasing vertex number  *
1860
c     * clockwise bends occur at salient vertices. A vertex is       *
1922
c     * clockwise bends occur at salient vertices. A vertex is       *
1861
c     * salient if the interior angle is less than 180 degrees.      *
1923
c     * salient if the interior angle is less than 180 degrees.      *
1862
c     * (In the case of a convex polygon this convention implies     *
1924
c     * (In the case of a convex polygon this convention implies     *
1863
c     * that vertices are numbered in clockwise sequence).           *
1925
c     * that vertices are numbered in clockwise sequence).           *
1864
c     ****************************************************************
1926
c     ****************************************************************
1865
 
1927
 
1866
      implicit none
1928
      implicit none
1867
      
1929
      
1868
      integer mxnv,nv
1930
      integer mxnv,nv
1869
 
1931
 
1870
c     ----------------------------------------------------------------
1932
c     ----------------------------------------------------------------
1871
c     Edit next statement to increase maximum number of vertices that 
1933
c     Edit next statement to increase maximum number of vertices that 
1872
c     may be used to define the spherical polygon S               
1934
c     may be used to define the spherical polygon S               
1873
c     The value of parameter mxnv in subroutine LctPtRelBndry must match
1935
c     The value of parameter mxnv in subroutine LctPtRelBndry must match
1874
c     that of parameter mxnv in this subroutine, as assigned above.
1936
c     that of parameter mxnv in this subroutine, as assigned above.
1875
c     ----------------------------------------------------------------
1937
c     ----------------------------------------------------------------
1876
      parameter (mxnv=2000)
1938
      parameter (mxnv=2000)
1877
 
1939
 
1878
      real  vlat(nv),vlon(nv),xlat,xlon,dellon
1940
      real  vlat(nv),vlon(nv),xlat,xlon,dellon
1879
      real  tlonv(mxnv),vlat_c(mxnv),vlon_c(mxnv),xlat_c,xlon_c
1941
      real  tlonv(mxnv),vlat_c(mxnv),vlon_c(mxnv),xlat_c,xlon_c
1880
      integer i,ibndry,nv_c,ip
1942
      integer i,ibndry,nv_c,ip
1881
 
1943
 
1882
      data ibndry/0/
1944
      data ibndry/0/
1883
      
1945
      
1884
      common/spolybndry/vlat_c,vlon_c,nv_c,xlat_c,xlon_c,tlonv,ibndry
1946
      common/spolybndry/vlat_c,vlon_c,nv_c,xlat_c,xlon_c,tlonv,ibndry
1885
 
1947
 
1886
      if (nv.gt.mxnv) then
1948
      if (nv.gt.mxnv) then
1887
         print *,'nv exceeds maximum allowed value'
1949
         print *,'nv exceeds maximum allowed value'
1888
         print *,'adjust parameter mxnv in subroutine DefSPolyBndry'
1950
         print *,'adjust parameter mxnv in subroutine DefSPolyBndry'
1889
         stop
1951
         stop
1890
      endif
1952
      endif
1891
 
1953
 
1892
      ibndry=1                  ! boundary defined at least once (flag)
1954
      ibndry=1                  ! boundary defined at least once (flag)
1893
      nv_c=nv                   ! copy for named common
1955
      nv_c=nv                   ! copy for named common
1894
      xlat_c=xlat               ! . . . .
1956
      xlat_c=xlat               ! . . . .
1895
      xlon_c=xlon               !
1957
      xlon_c=xlon               !
1896
 
1958
 
1897
      do i=1,nv
1959
      do i=1,nv
1898
         vlat_c(i)=vlat(i)      ! "
1960
         vlat_c(i)=vlat(i)      ! "
1899
         vlon_c(i)=vlon(i)      !
1961
         vlon_c(i)=vlon(i)      !
1900
 
1962
 
1901
         call TrnsfmLon(xlat,xlon,vlat(i),vlon(i),tlonv(i))
1963
         call TrnsfmLon(xlat,xlon,vlat(i),vlon(i),tlonv(i))
1902
 
1964
 
1903
         if (i.gt.1) then
1965
         if (i.gt.1) then
1904
            ip=i-1
1966
            ip=i-1
1905
         else
1967
         else
1906
            ip=nv
1968
            ip=nv
1907
         endif
1969
         endif
1908
         
1970
         
1909
         if ((vlat(i).eq.vlat(ip)).and.(vlon(i).eq.vlon(ip))) then
1971
         if ((vlat(i).eq.vlat(ip)).and.(vlon(i).eq.vlon(ip))) then
1910
            print *,'DefSPolyBndry detects user error:'
1972
            print *,'DefSPolyBndry detects user error:'
1911
            print *,'vertices ',i,' and ',ip,' are not distinct'
1973
            print *,'vertices ',i,' and ',ip,' are not distinct'
1912
            print*,'lat ',i,ip,vlat(i),vlat(ip)
1974
            print*,'lat ',i,ip,vlat(i),vlat(ip)
1913
            print*,'lon ',i,ip,vlon(i),vlon(ip)            
1975
            print*,'lon ',i,ip,vlon(i),vlon(ip)            
1914
            stop
1976
            stop
1915
         endif
1977
         endif
1916
 
1978
 
1917
         if (tlonv(i).eq.tlonv(ip)) then
1979
         if (tlonv(i).eq.tlonv(ip)) then
1918
            print *,'DefSPolyBndry detects user error:'
1980
            print *,'DefSPolyBndry detects user error:'
1919
            print *,'vertices ',i,' & ',ip,' on same gt. circle as X'
1981
            print *,'vertices ',i,' & ',ip,' on same gt. circle as X'
1920
            stop
1982
            stop
1921
         endif
1983
         endif
1922
 
1984
 
1923
         if (vlat(i).eq.(-vlat(ip))) then
1985
         if (vlat(i).eq.(-vlat(ip))) then
1924
            dellon=vlon(i)-vlon(ip)
1986
            dellon=vlon(i)-vlon(ip)
1925
            if (dellon.gt.+180.) dellon=dellon-360.
1987
            if (dellon.gt.+180.) dellon=dellon-360.
1926
            if (dellon.lt.-180.) dellon=dellon-360.
1988
            if (dellon.lt.-180.) dellon=dellon-360.
1927
            if ((dellon.eq.+180.0).or.(dellon.eq.-180.0)) then
1989
            if ((dellon.eq.+180.0).or.(dellon.eq.-180.0)) then
1928
               print *,'DefSPolyBndry detects user error:'
1990
               print *,'DefSPolyBndry detects user error:'
1929
               print *,'vertices ',i,' and ',ip,' are antipodal'
1991
               print *,'vertices ',i,' and ',ip,' are antipodal'
1930
               stop
1992
               stop
1931
            endif
1993
            endif
1932
         endif
1994
         endif
1933
      enddo
1995
      enddo
1934
 
1996
 
1935
      return
1997
      return
1936
      
1998
      
1937
      end
1999
      end
1938
 
2000
 
1939
 
2001
 
1940
c     ****************************************************************
2002
c     ****************************************************************
1941
 
2003
 
1942
      Subroutine LctPtRelBndry(plat,plon,location)
2004
      Subroutine LctPtRelBndry(plat,plon,location)
1943
 
2005
 
1944
c     ****************************************************************
2006
c     ****************************************************************
1945
 
2007
 
1946
c     ****************************************************************
2008
c     ****************************************************************
1947
c     * This routine is used to see if some point P is located       *
2009
c     * This routine is used to see if some point P is located       *
1948
c     * inside, outside or on the boundary of the spherical polygon  *
2010
c     * inside, outside or on the boundary of the spherical polygon  *
1949
c     * S previously defined by a call to subroutine DefSPolyBndry.  *
2011
c     * S previously defined by a call to subroutine DefSPolyBndry.  *
1950
c     * There is a single restriction on point P: it must not be     *
2012
c     * There is a single restriction on point P: it must not be     *
1951
c     * antipodal to the point X defined in the call to DefSPolyBndry*
2013
c     * antipodal to the point X defined in the call to DefSPolyBndry*
1952
c     * (ie.P and X cannot be seperated by exactly 180 degrees).     *
2014
c     * (ie.P and X cannot be seperated by exactly 180 degrees).     *
1953
c     * ARGUMENTS:                                                   *  
2015
c     * ARGUMENTS:                                                   *  
1954
c     * plat,plon (sent)... the latitude and longitude of point P    *
2016
c     * plat,plon (sent)... the latitude and longitude of point P    *
1955
c     * location (returned)... specifies the location of P:          *
2017
c     * location (returned)... specifies the location of P:          *
1956
c     *                        location=0 implies P is outside of S  *
2018
c     *                        location=0 implies P is outside of S  *
1957
c     *                        location=1 implies P is inside of S   *
2019
c     *                        location=1 implies P is inside of S   *
1958
c     *                        location=2 implies P on boundary of S *
2020
c     *                        location=2 implies P on boundary of S *
1959
c     *                        location=3 implies user error (P is   *
2021
c     *                        location=3 implies user error (P is   *
1960
c     *                                     antipodal to X)          *
2022
c     *                                     antipodal to X)          *
1961
c     * UNFfS AND SIGN CONVENTION:                                   * 
2023
c     * UNFfS AND SIGN CONVENTION:                                   * 
1962
c     *  Latitudes and longitudes are specified in degrees.          *
2024
c     *  Latitudes and longitudes are specified in degrees.          *
1963
c     *  Latitudes are positive to the north and negative to the     *
2025
c     *  Latitudes are positive to the north and negative to the     *
1964
c     *  south.                                                      *    
2026
c     *  south.                                                      *    
1965
c     *  Longitudes are positive to the east and negative to the     *
2027
c     *  Longitudes are positive to the east and negative to the     *
1966
c     *  west.                                                       *
2028
c     *  west.                                                       *
1967
c     ****************************************************************
2029
c     ****************************************************************
1968
      
2030
      
1969
      implicit none
2031
      implicit none
1970
      
2032
      
1971
      integer mxnv
2033
      integer mxnv
1972
 
2034
 
1973
c     ----------------------------------------------------------------
2035
c     ----------------------------------------------------------------
1974
c     The statement below must match that in subroutine DefSPolyBndry
2036
c     The statement below must match that in subroutine DefSPolyBndry
1975
c     ----------------------------------------------------------------
2037
c     ----------------------------------------------------------------
1976
 
2038
 
1977
      parameter (mxnv=2000)
2039
      parameter (mxnv=2000)
1978
 
2040
 
1979
      real tlonv(mxnv),vlat_c(mxnv),vlon_c(mxnv),xlat_c,xlon_c
2041
      real tlonv(mxnv),vlat_c(mxnv),vlon_c(mxnv),xlat_c,xlon_c
1980
      real plat,plon,vAlat,vAlon,vBlat,vBlon,tlonA,tlonB,tlonP
2042
      real plat,plon,vAlat,vAlon,vBlat,vBlon,tlonA,tlonB,tlonP
1981
      real tlon_X,tlon_P,tlon_B,dellon
2043
      real tlon_X,tlon_P,tlon_B,dellon
1982
      integer i,ibndry,nv_c,location,icross,ibrngAB,ibrngAP,ibrngPB
2044
      integer i,ibndry,nv_c,location,icross,ibrngAB,ibrngAP,ibrngPB
1983
      integer ibrng_BX,ibrng_BP,istrike
2045
      integer ibrng_BX,ibrng_BP,istrike
1984
 
2046
 
1985
      common/spolybndry/vlat_c,vlon_c,nv_c,xlat_c,xlon_c,tlonv,ibndry
2047
      common/spolybndry/vlat_c,vlon_c,nv_c,xlat_c,xlon_c,tlonv,ibndry
1986
 
2048
 
1987
      if (ibndry.eq.0) then     ! user has never defined the bndry
2049
      if (ibndry.eq.0) then     ! user has never defined the bndry
1988
         print*,'Subroutine LctPtRelBndry detects user error:'
2050
         print*,'Subroutine LctPtRelBndry detects user error:'
1989
         print*,'Subroutine DefSPolyBndry must be called before'
2051
         print*,'Subroutine DefSPolyBndry must be called before'
1990
         print*,'subroutine LctPtRelBndry can be called'
2052
         print*,'subroutine LctPtRelBndry can be called'
1991
         stop
2053
         stop
1992
      endif
2054
      endif
1993
 
2055
 
1994
      if (plat.eq.(-xlat_c)) then
2056
      if (plat.eq.(-xlat_c)) then
1995
         dellon=plon-xlon_c
2057
         dellon=plon-xlon_c
1996
         if (dellon.lt.(-180.)) dellon=dellon+360.
2058
         if (dellon.lt.(-180.)) dellon=dellon+360.
1997
         if (dellon.gt.+180.) dellon=dellon-360.
2059
         if (dellon.gt.+180.) dellon=dellon-360.
1998
         if ((dellon.eq.+180.0).or.(dellon.eq.-180.)) then
2060
         if ((dellon.eq.+180.0).or.(dellon.eq.-180.)) then
1999
            print*,'Warning: LctPtRelBndry detects case P antipodal
2061
            print*,'Warning: LctPtRelBndry detects case P antipodal
2000
     >           to X'
2062
     >           to X'
2001
            print*,'location of P relative to S is undetermined'
2063
            print*,'location of P relative to S is undetermined'
2002
            location=3
2064
            location=3
2003
            return
2065
            return
2004
         endif
2066
         endif
2005
      endif 
2067
      endif 
2006
 
2068
 
2007
      location=0                ! default ( P is outside S)
2069
      location=0                ! default ( P is outside S)
2008
      icross=0                  ! initialize counter
2070
      icross=0                  ! initialize counter
2009
 
2071
 
2010
      if ((plat.eq.xlat_c).and.(plon.eq.xlon_c)) then
2072
      if ((plat.eq.xlat_c).and.(plon.eq.xlon_c)) then
2011
         location=1
2073
         location=1
2012
         return
2074
         return
2013
      endif
2075
      endif
2014
 
2076
 
2015
      
2077
      
2016
      call TrnsfmLon (xlat_c,xlon_c,plat,plon,tlonP)
2078
      call TrnsfmLon (xlat_c,xlon_c,plat,plon,tlonP)
2017
 
2079
 
2018
      do i=1,nv_c              ! start of loop over sides of S 
2080
      do i=1,nv_c              ! start of loop over sides of S 
2019
 
2081
 
2020
         vAlat=vlat_c(i)
2082
         vAlat=vlat_c(i)
2021
         vAlon=vlon_c(i)
2083
         vAlon=vlon_c(i)
2022
         tlonA=tlonv(i)
2084
         tlonA=tlonv(i)
2023
 
2085
 
2024
         if (i.lt.nv_c) then
2086
         if (i.lt.nv_c) then
2025
            vBlat=vlat_c(i+1)
2087
            vBlat=vlat_c(i+1)
2026
            vBlon=vlon_c(i+1)
2088
            vBlon=vlon_c(i+1)
2027
            tlonB=tlonv(i+1)
2089
            tlonB=tlonv(i+1)
2028
         else
2090
         else
2029
            vBlat=vlat_c(1)
2091
            vBlat=vlat_c(1)
2030
            vBlon=vlon_c(1)
2092
            vBlon=vlon_c(1)
2031
            tlonB=tlonv(1)
2093
            tlonB=tlonv(1)
2032
         endif
2094
         endif
2033
         
2095
         
2034
         istrike=0
2096
         istrike=0
2035
         
2097
         
2036
         if (tlonP.eq.tlonA) then
2098
         if (tlonP.eq.tlonA) then
2037
            istrike=1
2099
            istrike=1
2038
         else
2100
         else
2039
            call EastOrWest(tlonA,tlonB,ibrngAB)
2101
            call EastOrWest(tlonA,tlonB,ibrngAB)
2040
            call EastOrWest(tlonA,tlonP,ibrngAP)
2102
            call EastOrWest(tlonA,tlonP,ibrngAP)
2041
            call EastOrWest(tlonP,tlonB,ibrngPB)
2103
            call EastOrWest(tlonP,tlonB,ibrngPB)
2042
            
2104
            
2043
 
2105
 
2044
            if((ibrngAP.eq.ibrngAB).and.(ibrngPB.eq.ibrngAB)) istrike=1
2106
            if((ibrngAP.eq.ibrngAB).and.(ibrngPB.eq.ibrngAB)) istrike=1
2045
         endif
2107
         endif
2046
 
2108
 
2047
         
2109
         
2048
         if (istrike.eq.1) then
2110
         if (istrike.eq.1) then
2049
 
2111
 
2050
            if ((plat.eq.vAlat).and.(plon.eq.vAlon)) then
2112
            if ((plat.eq.vAlat).and.(plon.eq.vAlon)) then
2051
               location=2       ! P lies on a vertex of S
2113
               location=2       ! P lies on a vertex of S
2052
               return
2114
               return
2053
            endif
2115
            endif
2054
            call TrnsfmLon(vAlat,vAlon,xlat_c,xlon_c,tlon_X)
2116
            call TrnsfmLon(vAlat,vAlon,xlat_c,xlon_c,tlon_X)
2055
            call TrnsfmLon(vAlat,vAlon,vBlat,vBlon,tlon_B)
2117
            call TrnsfmLon(vAlat,vAlon,vBlat,vBlon,tlon_B)
2056
            call TrnsfmLon(vAlat,vAlon,plat,plon,tlon_P)
2118
            call TrnsfmLon(vAlat,vAlon,plat,plon,tlon_P)
2057
            
2119
            
2058
            if (tlon_P.eq.tlon_B) then
2120
            if (tlon_P.eq.tlon_B) then
2059
               location=2       ! P lies on side of S
2121
               location=2       ! P lies on side of S
2060
               return 
2122
               return 
2061
            else
2123
            else
2062
               call EastOrWest(tlon_B,tlon_X,ibrng_BX)
2124
               call EastOrWest(tlon_B,tlon_X,ibrng_BX)
2063
               call EastOrWest(tlon_B,tlon_P,ibrng_BP)
2125
               call EastOrWest(tlon_B,tlon_P,ibrng_BP)
2064
               if(ibrng_BX.eq.(-ibrng_BP)) icross=icross+1
2126
               if(ibrng_BX.eq.(-ibrng_BP)) icross=icross+1
2065
            endif
2127
            endif
2066
            
2128
            
2067
         endif
2129
         endif
2068
      enddo                     ! end of loop over the sides of S
2130
      enddo                     ! end of loop over the sides of S
2069
 
2131
 
2070
 
2132
 
2071
c     if the arc XP crosses the boundary S an even number of times then P
2133
c     if the arc XP crosses the boundary S an even number of times then P
2072
c     is in S
2134
c     is in S
2073
 
2135
 
2074
      if (mod(icross,2).eq.0) location=1
2136
      if (mod(icross,2).eq.0) location=1
2075
 
2137
 
2076
      return
2138
      return
2077
 
2139
 
2078
      end
2140
      end
2079
 
2141
 
2080
 
2142
 
2081
c     ****************************************************************
2143
c     ****************************************************************
2082
      
2144
      
2083
      subroutine TrnsfmLon(plat,plon,qlat,qlon,tranlon)
2145
      subroutine TrnsfmLon(plat,plon,qlat,qlon,tranlon)
2084
 
2146
 
2085
c     ****************************************************************
2147
c     ****************************************************************
2086
c     * This subroutine is required by subroutines DefSPolyBndry &   *
2148
c     * This subroutine is required by subroutines DefSPolyBndry &   *
2087
c     * LctPtRelBndry. It finds the 'longitude' of point Q in a      *
2149
c     * LctPtRelBndry. It finds the 'longitude' of point Q in a      *
2088
c     * geographic coordinate system for which point P acts as a     *
2150
c     * geographic coordinate system for which point P acts as a     *
2089
c     * 'north pole'. SENT: plat,plon,qlat,qlon, in degrees.         *
2151
c     * 'north pole'. SENT: plat,plon,qlat,qlon, in degrees.         *
2090
c     * RETURNED: tranlon, in degrees.                               *
2152
c     * RETURNED: tranlon, in degrees.                               *
2091
c     ****************************************************************
2153
c     ****************************************************************
2092
 
2154
 
2093
      implicit none
2155
      implicit none
2094
 
2156
 
2095
      real pi,dtr,plat,plon,qlat,qlon,tranlon,t,b
2157
      real pi,dtr,plat,plon,qlat,qlon,tranlon,t,b
2096
      parameter (pi=3.141592654,dtr=pi/180.0)
2158
      parameter (pi=3.141592654,dtr=pi/180.0)
2097
 
2159
 
2098
      if (plat.eq.90.) then
2160
      if (plat.eq.90.) then
2099
         tranlon=qlon
2161
         tranlon=qlon
2100
      else
2162
      else
2101
         t=sin((qlon-plon)*dtr)*cos(qlat*dtr)
2163
         t=sin((qlon-plon)*dtr)*cos(qlat*dtr)
2102
         b=sin(dtr*qlat)*cos(plat*dtr)-cos(qlat*dtr)*sin(plat*dtr)
2164
         b=sin(dtr*qlat)*cos(plat*dtr)-cos(qlat*dtr)*sin(plat*dtr)
2103
     >    *cos((qlon-plon)*dtr)
2165
     >    *cos((qlon-plon)*dtr)
2104
         tranlon=atan2(t,b)/dtr
2166
         tranlon=atan2(t,b)/dtr
2105
      endif
2167
      endif
2106
 
2168
 
2107
      return
2169
      return
2108
      end
2170
      end
2109
 
2171
 
2110
c     ****************************************************************
2172
c     ****************************************************************
2111
 
2173
 
2112
      subroutine EastOrWest(clon,dlon,ibrng)
2174
      subroutine EastOrWest(clon,dlon,ibrng)
2113
 
2175
 
2114
c     ****************************************************************
2176
c     ****************************************************************
2115
c     * This subroutine is required by subroutine LctPtRelBndry.     *
2177
c     * This subroutine is required by subroutine LctPtRelBndry.     *
2116
c     * This routine determines if in travelling the shortest path   *
2178
c     * This routine determines if in travelling the shortest path   *
2117
c     * from point C (at longitude clon) to point D (at longitude    *
2179
c     * from point C (at longitude clon) to point D (at longitude    *
2118
c     * dlon) one is heading east, west or neither.                  *
2180
c     * dlon) one is heading east, west or neither.                  *
2119
c     * SENT: clon,dlon; in degrees. RETURNED: ibrng                 *
2181
c     * SENT: clon,dlon; in degrees. RETURNED: ibrng                 *
2120
c     * (1=east,-1=west, 0=neither).                                 *
2182
c     * (1=east,-1=west, 0=neither).                                 *
2121
c     ****************************************************************
2183
c     ****************************************************************
2122
 
2184
 
2123
      implicit none
2185
      implicit none
2124
      real clon,dlon,del
2186
      real clon,dlon,del
2125
      integer ibrng
2187
      integer ibrng
2126
      del=dlon-clon
2188
      del=dlon-clon
2127
      if (del.gt.180.) del=del-360.
2189
      if (del.gt.180.) del=del-360.
2128
      if (del.lt.-180.) del=del+360.
2190
      if (del.lt.-180.) del=del+360.
2129
      if ((del.gt.0.0).and.(del.ne.180.)) then
2191
      if ((del.gt.0.0).and.(del.ne.180.)) then
2130
         ibrng=-1               ! (D is west of C)
2192
         ibrng=-1               ! (D is west of C)
2131
      elseif ((del.lt.0.0).and.(del.ne.-180.)) then
2193
      elseif ((del.lt.0.0).and.(del.ne.-180.)) then
2132
         ibrng=+1               ! (D is east of C)
2194
         ibrng=+1               ! (D is east of C)
2133
      else
2195
      else
2134
         ibrng=0                ! (D north or south of C)
2196
         ibrng=0                ! (D north or south of C)
2135
      endif
2197
      endif
2136
      return
2198
      return
2137
      end
2199
      end