Subversion Repositories lagranto.ocean

Rev

Details | Last modification | View Log | RSS feed

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