Subversion Repositories lagranto.ecmwf

Rev

Rev 9 | Go to most recent revision | 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.'ppos' ).and.(vars(4).ne.'p'   ) )
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) = 'p'
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,f10.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
         else
514
            var=vars(ivar)
515
         endif
516
         write(*,'(i5,f15.4,10x,a10)') i,out(i),trim(var)
517
 
518
c        Get variable mode
519
         i=i+1
520
         imode=nint(out(i))
521
         if (imode.eq.1) mode='VALUE'
522
         if (imode.eq.2) mode='MEAN'         
523
         if (imode.eq.3) mode='MAX'
524
         if (imode.eq.4) mode='MIN'
525
         if (imode.eq.5) mode='VAR'
526
         if (imode.eq.6) mode='SUM'
527
         if (imode.eq.7) mode='CHANGE'
528
         if (imode.eq.8) mode='DIFF'
529
         write(*,'(i5,f15.4,10x,a10)') i,out(i),trim(mode)
530
 
531
c        Get values
532
         i=i+1
533
         nval=nint(out(i))
534
         write(*,'(i5,f15.4,10x,a10)') i,out(i),'#PARAMETER'
535
         do j=1,nval
536
            i=i+1
537
 
538
            if ( var.ne.'INPOLYGON' ) then
539
               write(*,'(i5,f15.4)') i,out(i)
540
            else
541
               write(*,'(i5,f15.4,a2)') i,out(i),char(nint(out(i)))
542
            endif
543
         enddo
544
 
545
c        Get times
546
         i=i+1
547
         ntim=nint(out(i))
548
         write(*,'(i5,f15.4,7x,7x,a6)') i,out(i),'#TIMES'
549
         do j=1,ntim
550
            i=i+1
551
            write(*,'(i5,f15.4,f7.0)') i,out(i),times(nint(out(i)))
552
         enddo
553
 
554
c        Get time mode
555
         i=i+1
556
         itime=nint(out(i))
557
         if (itime.eq.1) strtim='ALL'
558
         if (itime.eq.2) strtim='ANY'         
559
         if (itime.eq.3) strtim='NONE'
560
         if (itime.lt.0) strtim='TRIGGER'
561
 
562
         if ( strtim.ne.'TRIGGER' ) then
563
            write(*,'(i5,f15.4,10x,a10)') i,out(i),trim(strtim)
564
         else
565
            write(*,'(i5,f15.4,10x,a10,a3,i3)') i,out(i),trim(strtim),
566
     >                                        ' ->',abs(itime)
567
         endif
568
 
569
         goto 100
570
 
571
      endif
572
 
573
c     Exit point
574
 200  continue
575
 
576
      write(*,*) '---------------------------------------'
577
 
578
      end
579
 
580
 
581
c     -------------------------------------------------------------- 
582
c     Read and decode a selection set
583
c     --------------------------------------------------------------
584
 
585
      subroutine decode(fid,out,n,vars,nvars,times,ntimes,regionf)
586
 
587
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
589
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
591
c     commands with the following format:
592
c     
593
c        out(i)             = Command 
594
c        out(i+1)           = Column index of variable
595
c        out(i+2)           = Mode for variable
596
c        out(i+3)           = Number of parameters (n)
597
c        out(i+4:i+4+n)     = Parameters
598
c        out(i+5+n)         = Number of times 
599
c        out(i+6+n:i+6+n+m) = List of time indices (m)
600
c        out(i+7+n+m)       = Time mode
601
c     
602
c     For SPECIAL commands (to be coded in <special.f>) the format is:
603
c     
604
c        out(i)             = Length of command string (n)
605
c        out(i+1:i+n)       = Command string      
606
c        out(i+n+1)         = Number of parameters (m)
607
c        out(i+n+2:i+n+1+m) = List of parameters
608
c     
609
c     The following coding is used
610
c      
611
c        Command        Variable mode    Time mode
612
c        ----------     -------------    ---------
613
c        GT       1     VALUE    1       ALL      1
614
c        LT       2     MEAN     2       ANY      2
615
c        IN       3     MAX      3       NONE     3
616
c        OUT      4     MIN      4       TRIGGER -i (i the trigger index)
617
c        EQ       5     VAR      5
618
c        BEGIN   -1     SUM      6
619
c        END     -2     CHANGE   7 
620
c        AND     -3     DIFF     8
621
c        OR      -4     
622
c        TRUE     6
623
c        FALSE    7
624
c        SPECIAL  0
625
c        ALL      8 (TRIGGER)
626
c        ANY      9 (TRIGGER)
627
c        NONE    10 (TRIGGER)
628
 
629
 
630
c     Several "implicit variables" are supported - out(i+1):
631
c     
632
c        DIST      -1 : Path length of the trajectory
633
c        DIST0     -2 : Distance from starting position
634
c        INPOLYGON -3 : Specified polygon region
635
c        INBOX     -4 : Longitude/latitude rectangle
636
c        INCIRCLE  -5 : Within a specified radius 
637
c        INREGION  -6 : Within a specified rehion on the region file
638
c        TRIGGER   -7 : Trigger field
639
c         
640
c     For the special commands BEGIN, END, AND and OR, only one field
641
c     in <out> is used.
642
 
643
      implicit none
644
 
645
c     Declaration of subroutine parameters
646
      integer      fid
647
      integer      n
648
      real         out(n)
649
      integer      nvars
650
      character*80 vars(nvars)
651
      integer      ntimes
652
      real         times(ntimes)
653
      character*80 regionf
654
 
655
c     Numerical epsilon
656
      real         eps
657
      parameter    (eps=0.001)
658
 
659
c     A single command term
660
      character*80 cmd
661
      character*80 var,mode
662
      integer      nval
663
      real         val(1000)
664
      integer      ntim
665
      real         tim(1000)
666
      character*80 tmode
667
 
668
c     Specification of a polygon
669
      character*80 filename
670
      integer      pn                             ! Number of entries in lat/lon poly
671
      real         latpoly(1000)                  ! List of polygon latitudes
672
      real         lonpoly(1000)                  ! List of polygon longitudes
673
      real         loninpoly,latinpoly            ! Lon/lat inside polygon
674
 
675
c     Specification of a region
676
      real         xcorner(4)
677
      real         ycorner(4)
678
      integer      iregion
679
      character*80 string
680
 
681
c     Transformation to UPN (handling of logical operators)
682
      integer      nlogical
683
      integer      ilogical(n)
684
      real         tmp(n)
685
      integer      mlogical
686
      integer      isor
687
 
688
c     Auxiliary variables
689
      integer      i,j
690
      integer      j1,j2
691
      integer      flag(ntimes)
692
      integer      count
693
      integer      ok
694
      integer      itrigger
695
 
696
c     Common block for initialisation of polygon check
697
      real    tlonv(1000),vlat_c(1000),vlon_c(1000)
698
      real    xlat_c,xlon_c
699
      integer ibndry,nv_c
700
      common  /spolybndry/vlat_c,vlon_c,nv_c,xlat_c,xlon_c,tlonv,ibndry
701
 
702
c     ------ Decode single commands ---------------------
703
 
704
c     Reset the filename for polygons
705
      filename='nil'
706
 
707
c     Reset the counter for logical commands
708
      nlogical=0
709
 
710
c     Loop through all commands
711
 100  continue
712
 
713
c       Read next command (handle special cases)
714
        read(fid,*) cmd
715
 
716
c       Special handling of SPECIAL commands
717
        if ( cmd.eq.'SPECIAL' ) then
718
 
719
c          Set the flag for SPECIAL command
720
           n      = 1
721
           out(n) = 0.
722
 
723
c          Read the command string
724
           read(fid,*) var
725
 
726
c          Add the command string
727
           n      = n + 1
728
           out(n) = len_trim(var)
729
           do j=1,len_trim(var)
730
              n      = n + 1
731
              out(n) = ichar(var(j:j))
732
           enddo
733
 
734
c          Read the parameters
735
           read(fid,*) nval
736
           read(fid,*) (val(i),i=1,nval)
737
 
738
c          Add the parameters
739
           n = n + 1
740
           out(n)=real(nval)
741
           do i=1,nval
742
              n=n+1
743
              out(n)=val(i)
744
           enddo
745
 
746
c          Goto exit point - nothing more top do
747
           goto 350
748
 
749
        endif
750
 
751
c       Handle structure commands
752
        if ( cmd.eq.'BEGIN') then
753
           out(1)=-1.
754
           n=1
755
           nlogical=1
756
           ilogical(1)=n
757
           goto 200
758
        elseif ( cmd.eq.'AND'  ) then
759
           n=n+1
760
           out(n)=-3.
761
           nlogical=nlogical+1
762
           ilogical(nlogical)=n
763
           goto 200
764
        elseif ( cmd.eq.'OR'   ) then
765
           n=n+1
766
           out(n)=-4.
767
           nlogical=nlogical+1
768
           ilogical(nlogical)=n
769
           goto 200
770
        elseif ( cmd.eq.'END'  ) then
771
           n=n+1
772
           out(n)=-2.
773
           nlogical=nlogical+1
774
           ilogical(nlogical)=n
775
           goto 300
776
        endif
777
 
778
c       Read other fields associated with the command
779
        read(fid,*) var,mode
780
 
781
c       Read parameter
782
        if ( var.eq.'INPOLYGON' ) then 
783
           read(fid,*) nval
784
           read(fid,*) filename
785
           filename = trim(filename)
786
        else
787
           read(fid,*) nval
788
           read(fid,*) (val(i),i=1,nval)
789
        endif
790
 
791
c       Read times
792
        read(fid,*) ntim
793
        read(fid,*) (tim(i),i=1,ntim)
794
        read(fid,*) tmode
795
 
796
c       Bring CAPITAL "TIME,LAT,LON,P" into "time,lat,lon,p"
797
        if (var.eq.'TIME') var='time'
798
        if (var.eq.'LAT' ) var='lat'
799
        if (var.eq.'LON' ) var='lon'
800
        if (var.eq.'P'   ) var='p'
801
 
802
c       If the time mode is 'TRIGGER', all times of a trajectory
803
c       must be considered
804
        if ( tmode.eq.'TRIGGER' ) then
805
           itrigger = nint(tim(1))
806
           ntim     = 1
807
           tim(1)   = -999.
808
        endif
809
 
810
c       Special times: transform into real time
811
        do i=1,ntim
812
           if ( abs(tim(i)+996.).lt.eps ) tim(i)=times(1)
813
           if ( abs(tim(i)+995.).lt.eps ) tim(i)=times(ntimes)
814
        enddo
815
 
816
c       Check whether times are valid 
817
        ok=0
818
        do i=1,ntim
819
           if ( (abs(tim(i)+994.).gt.eps).and.
820
     >          (abs(tim(i)+999.).gt.eps) )
821
     >     then
822
              do j=1,ntimes
823
                 if ( abs(tim(i)-times(j)).lt.eps ) then
824
                    ok=ok+1
825
                 endif
826
              enddo
827
           else
828
              ok=ok+1
829
           endif
830
        enddo
831
        if (ok.ne.ntim) goto 400
832
 
833
c       Select all times which are included in the criterion
834
        do i=1,ntimes
835
           flag(i)=0
836
        enddo
837
        i=1
838
 150    if (i.le.ntim) then
839
 
840
c          A list of times
841
           if ( (abs(tim(i)+994.).lt.eps) ) then
842
              j1=0
843
              do j=1,ntimes
844
                 if ( abs(tim(i-1)-times(j)).lt.eps ) then
845
                    j1=j
846
                 endif
847
              enddo
848
              j2=0
849
              do j=1,ntimes
850
                 if ( abs(tim(i+1)-times(j)).lt.eps ) then
851
                    j2=j
852
                 endif
853
              enddo   
854
              if ( (j1.eq.0).or.(j2.eq.0) ) goto 400
855
              do j=j1,j2
856
                 flag(j)=1
857
              enddo
858
              i=i+1
859
 
860
c          Explicitly given time value
861
           else
862
              do j=1,ntimes
863
                 if ( abs(tim(i)-times(j)).lt.eps ) then
864
                    flag(j)=1
865
                 endif
866
              enddo
867
 
868
           endif
869
 
870
           i=i+1
871
           goto 150
872
 
873
        endif
874
 
875
c       Write command identifier
876
        n=n+1
877
        if (cmd.eq.'GT'    ) out(n)= 1.
878
        if (cmd.eq.'LT'    ) out(n)= 2.
879
        if (cmd.eq.'IN'    ) out(n)= 3.
880
        if (cmd.eq.'OUT'   ) out(n)= 4.
881
        if (cmd.eq.'EQ'    ) out(n)= 5.
882
        if (cmd.eq.'TRUE'  ) out(n)= 6.
883
        if (cmd.eq.'FALSE' ) out(n)= 7.
884
        if (cmd.eq.'ALL  ' ) out(n)= 8.
885
        if (cmd.eq.'ANY  ' ) out(n)= 9.
886
        if (cmd.eq.'NONE ' ) out(n)=10.
887
 
888
c       Write index for variable - force implicit trigger
889
        ok=0
890
        do j=1,nvars
891
           if (vars(j).eq.var) ok=j
892
        enddo
893
 
894
        if (var.eq.'TRIGGER') ok = 0
895
 
896
        if (ok.eq.0) then
897
           if (var.eq.'DIST') then
898
              ok = -1
899
           elseif (var.eq.'DIST0') then
900
              ok = -2
901
           elseif (var.eq.'INPOLYGON') then
902
              ok = -3
903
           elseif (var.eq.'INBOX') then
904
              ok = -4
905
           elseif (var.eq.'INCIRCLE') then
906
              ok = -5
907
           elseif (var.eq.'INREGION') then
908
              ok = -6
909
           elseif (var.eq.'TRIGGER') then
910
              ok = -7
911
           else
912
             goto 400
913
          endif
914
        endif
915
        n=n+1
916
        out(n)=real(ok)
917
 
918
c       Write mode for variable
919
        ok=0
920
        if (mode.eq.'VALUE'  ) ok=1
921
        if (mode.eq.'MEAN'   ) ok=2
922
        if (mode.eq.'MAX'    ) ok=3
923
        if (mode.eq.'MIN'    ) ok=4
924
        if (mode.eq.'VAR'    ) ok=5
925
        if (mode.eq.'SUM'    ) ok=6
926
        if (mode.eq.'CHANGE' ) ok=7
927
        if (mode.eq.'DIFF'   ) ok=8
928
        if (ok.eq.0) goto 400
929
        n=n+1
930
        out(n)=real(ok)
931
 
932
c       Write the parameter values: INPOLYGON 
933
        if ( var.eq.'INPOLYGON' ) then
934
 
935
           n      = n+1
936
           out(n) = len_trim(filename)
937
           do j=1,len_trim(filename)
938
              n      = n + 1
939
              out(n) = ichar(filename(j:j))
940
           enddo
941
 
942
c       Write parameter value: INREGION 
943
        elseif ( var.eq.'INREGION' ) then
944
 
945
           iregion = nint(val(1))
946
 
947
           open(fid+1,file=regionf)          
948
 
949
 50        read(fid+1,*,end=51) string
950
 
951
           if ( string(1:1).ne.'#' ) then
952
              call regionsplit(string,i,xcorner,ycorner)
953
              if ( i.eq.iregion ) goto 52
954
           endif
955
 
956
           goto 50
957
 
958
 51        close(fid+1)        
959
 
960
           print*,' ERROR: region ',iregion,' not found on ',
961
     >                                         trim(regionf)
962
           stop
963
 
964
 52        continue
965
 
966
           n      = n + 1
967
           out(n) = 8                   ! Number of parameters
968
           do i=1,4
969
              n      = n + 1
970
              out(n) = xcorner(i)
971
           enddo
972
           do i=1,4
973
              n      = n + 1
974
              out(n) = ycorner(i)
975
           enddo
976
 
977
c       Write parameter values: all other cases
978
        else
979
           n=n+1
980
           out(n)=real(nval)
981
           do i=1,nval
982
              n=n+1
983
              out(n)=val(i)
984
           enddo
985
        endif
986
 
987
c       All times are selected
988
        if ( abs(tim(1)+999.).lt.eps ) then
989
           n=n+1
990
           out(n)=real(ntimes)
991
           do i=1,ntimes
992
              n=n+1
993
              out(n)=real(i)
994
           enddo
995
 
996
c       A selection of times is given 
997
        else
998
           count=0
999
           do i=1,ntimes
1000
              count=count+flag(i)
1001
           enddo
1002
           n=n+1
1003
           out(n)=real(count)
1004
           do i=1,ntimes
1005
              if (flag(i).eq.1) then
1006
                 n=n+1
1007
                 out(n)=real(i)
1008
              endif
1009
           enddo
1010
        endif
1011
 
1012
c       Write the time mode
1013
        if ( tmode.eq.'ALL') then
1014
           n=n+1
1015
           out(n)=1.
1016
        elseif ( tmode.eq.'ANY') then
1017
           n=n+1
1018
           out(n)=2.
1019
        elseif ( tmode.eq.'NONE') then
1020
           n=n+1
1021
           out(n)=3.
1022
        elseif ( tmode.eq.'TRIGGER') then
1023
           n=n+1
1024
           out(n)=-real(itrigger)
1025
        endif
1026
 
1027
c     End loop: handle single command
1028
 200  continue
1029
      goto 100
1030
 
1031
c     End loop: loop over all commands 
1032
 300  continue
1033
 
1034
c     ------ Read polygon file, if requested -----------
1035
      if ( filename.ne.'nil' ) then
1036
 
1037
        print*
1038
        print*,
1039
     >     '---- POLYGON --------------------------------------------'
1040
 
1041
           print*
1042
           print*,'Filename = ',trim(filename)
1043
           print*
1044
 
1045
c        Read list of polygon coordinates from file
1046
         pn = 0
1047
         open(fid+1,file=filename)
1048
           read(fid+1,*) loninpoly,latinpoly
1049
           print*,'Inside (lon/lat) =',loninpoly,latinpoly
1050
           print*
1051
 510       continue
1052
              pn = pn + 1
1053
              read(fid+1,*,end=511) lonpoly(pn),
1054
     >                              latpoly(pn)
1055
 
1056
              print*,pn,lonpoly(pn),latpoly(pn)
1057
 
1058
              goto 510
1059
 511       continue
1060
           pn = pn - 1
1061
         close(fid+1)
1062
 
1063
c        Define the polygon boundaries
1064
         call DefSPolyBndry(latpoly,lonpoly,pn,latinpoly,loninpoly)
1065
 
1066
      endif
1067
 
1068
c     ------ Transform to UPN --------------------------
1069
 
1070
c     Check whether logical commands are ok
1071
      mlogical=nint(out(ilogical(1)))
1072
      if ( mlogical.ne.-1) goto 400
1073
      mlogical=nint(out(ilogical(nlogical)))
1074
      if ( mlogical.ne.-2) goto 400
1075
 
1076
c     No transformation necessary if only one command
1077
      if (nlogical.eq.2) goto 350
1078
 
1079
c     Copy the output to temporary list
1080
      do i=1,n
1081
         tmp(i)=out(i)
1082
      enddo
1083
 
1084
c     Set BEGIN statement
1085
      n=1
1086
      out(n)=-1.
1087
 
1088
c     Reorder commands and transform to UPN
1089
      isor=0
1090
      do i=1,nlogical-1
1091
 
1092
c        Get the logical command
1093
         mlogical=nint(out(ilogical(i)))
1094
 
1095
c        Connecting OR
1096
         if (mlogical.eq.-4) then
1097
            if (isor.eq.1) then
1098
               n=n+1
1099
               out(n)=-4.
1100
            else
1101
               isor=1
1102
            endif
1103
         endif
1104
 
1105
c        Put the command onto the stack
1106
         do j=ilogical(i)+1,ilogical(i+1)-1
1107
            n=n+1
1108
            out(n)=tmp(j)
1109
         enddo
1110
 
1111
c        Connecting AND operator
1112
         if ( mlogical.eq.-3 ) then
1113
            n=n+1
1114
            out(n)=-3.
1115
         endif
1116
 
1117
      enddo
1118
 
1119
c     Set final connecting OR
1120
      if (isor.eq.1) then
1121
         n=n+1
1122
         out(n)=-4.
1123
      endif
1124
 
1125
c     Set END statement
1126
      n=n+1
1127
      out(n)=-2.
1128
 
1129
c     ------ Exit point ---------------------------------
1130
 
1131
 350  continue
1132
      return
1133
 
1134
c     ----- Exception handling --------------------------
1135
 400  print*,'Invalid selection criterion... Stop'
1136
      stop
1137
 
1138
      end
1139
 
1140
 
1141
c     -------------------------------------------------------------- 
1142
c     Decide whether a trajectory is selected or not
1143
c     --------------------------------------------------------------
1144
 
1145
      subroutine select_tra (select,cmd,n,tra,trigger,ntim,ncol)
1146
 
1147
c     Decide whether a single trajectory is selected (<select=1>) or
1148
c     is not selected <select=0> according to the selection criterion
1149
c     given in <cmd(ncmd)>. The selection criterion <cmd(ncmd)> is 
1150
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
1152
c     and <ncol> is the number of columns.
1153
c     
1154
c     Important note: the structure of <tra(ntim,ncol)> must match to the
1155
c     call parameter <vars,nvars,times,ntimes> in subroutine <decode>.
1156
 
1157
      implicit none
1158
 
1159
c     Declaration of subroutine parameters
1160
      integer   select
1161
      integer   n
1162
      real      cmd(n)
1163
      integer   ntim,ncol
1164
      real      tra(ntim,ncol)
1165
      integer   trigger(ntim)
1166
 
1167
c     Numerical epsilon (for test of equality)
1168
      real      eps
1169
      parameter (eps=0.000001)
1170
 
1171
c     A single command and the associated field
1172
      integer   icmd,ivar,imode,itime,nsel,nval
1173
      integer   time(ntim)
1174
      real      param(100)
1175
      real      var   (ntim)
1176
      integer   intvar(ntim)
1177
 
1178
c     Boolean values for a single time, a single command and build-up
1179
      integer   stack(100)
1180
      integer   nstack
1181
      integer   istrue(ntim)
1182
      integer   decision
1183
 
1184
c     Auxiliary variables
1185
      integer   i,j,k
1186
      real      tmp,mea
1187
      integer   istack1,istack2
1188
      real      lat0,lon0,lat1,lon1
1189
      real      length(ntim)
1190
      integer   flag
1191
      real      dist
1192
      real      xcorner(4),ycorner(4)
1193
      integer   iparam
1194
      character ch
1195
 
1196
c     Common block for initialisation of polygon check
1197
      real    tlonv(1000),vlat_c(1000),vlon_c(1000)
1198
      real    xlat_c,xlon_c
1199
      integer ibndry,nv_c
1200
      common  /spolybndry/vlat_c,vlon_c,nv_c,xlat_c,xlon_c,tlonv,ibndry
1201
 
1202
c     Externals
1203
      real      sdis           ! Spherical distance
1204
      external  sdis
1205
      integer   inregion       ! Boolean flag for regions
1206
      external  inregion
1207
 
1208
c     Reset the decision stack (with locical values)
1209
      nstack=0
1210
 
1211
c     Loop through the complete command list
1212
      i=0
1213
 100  if (i.lt.n) then  
1214
 
1215
c        --- Get the command -------------------------------
1216
         i=i+1
1217
         icmd=nint(cmd(i))
1218
 
1219
c        --- Handle structural commands (BEGIN, END, AND, OR)
1220
 
1221
c        Handle BEGIN statement
1222
         if ( icmd.eq.-1) then
1223
            nstack=0
1224
            goto 200
1225
         endif
1226
 
1227
c        Handle END statement
1228
         if (icmd.eq.-2) then
1229
            goto 300
1230
         endif
1231
 
1232
c        Handle AND statement
1233
         if (icmd.eq.-3) then
1234
            istack1=stack(nstack)
1235
            nstack=nstack-1
1236
            istack2=stack(nstack)
1237
            if ((istack1.eq.1).and.(istack2.eq.1)) then
1238
               stack(nstack)=1
1239
            else
1240
               stack(nstack)=0
1241
            endif
1242
            goto 200
1243
         endif
1244
 
1245
c        Handle OR statement
1246
         if (icmd.eq.-4) then
1247
            istack1=stack(nstack)
1248
            nstack=nstack-1
1249
            istack2=stack(nstack)
1250
            if ((istack1.eq.1).or.(istack2.eq.1)) then
1251
               stack(nstack)=1
1252
            else
1253
               stack(nstack)=0
1254
            endif
1255
            goto 200
1256
         endif
1257
 
1258
c        --- Get all command details (parameters, modes, times)
1259
 
1260
c        Get variable (<ivar> gets the column index in <tra>)
1261
         i=i+1
1262
         ivar=nint(cmd(i))
1263
 
1264
c        Get variable mode 
1265
         i=i+1
1266
         imode=nint(cmd(i))
1267
 
1268
c        Get parameter values
1269
         i=i+1
1270
         nval=nint(cmd(i))
1271
         do j=1,nval
1272
            i=i+1
1273
            param(j)=cmd(i)
1274
         enddo
1275
 
1276
c        Get times (<time(j)> gets the row indices of <tra>)
1277
         i=i+1
1278
         nsel=nint(cmd(i))
1279
         do j=1,nsel
1280
            i=i+1
1281
            time(j)=nint(cmd(i))
1282
         enddo
1283
 
1284
c        Get time mode
1285
         i=i+1
1286
         itime=nint(cmd(i))
1287
 
1288
c        --- Prepare field values for analysis -----------
1289
 
1290
c        Implicit variable: DIST
1291
         if ( ivar.eq. -1 ) then
1292
            length(1) = 0.
1293
            do j=2,ntim
1294
               lon0      = tra(j-1,2)
1295
               lat0      = tra(j-1,3)
1296
               lon1      = tra(j  ,2)
1297
               lat1      = tra(j  ,3)
1298
               length(j) = length(j-1) + sdis(lon0,lat0,lon1,lat1)
1299
            enddo
1300
            do j=1,nsel
1301
               var(j) = length( time(j) )
1302
            enddo
1303
 
1304
 
1305
c        Implict variable: DIST0
1306
         elseif ( ivar.eq. -2 ) then
1307
            do j=1,nsel
1308
               lon0   = tra(1      ,2)
1309
               lat0   = tra(1      ,3)
1310
               lon1   = tra(time(j),2)
1311
               lat1   = tra(time(j),3)
1312
               var(j) = sdis(lon0,lat0,lon1,lat1)
1313
            enddo
1314
 
1315
c        Implict variable: INPOLYGON
1316
         elseif ( ivar.eq. -3 ) then
1317
            do j=1,nsel
1318
               lon1   = tra(time(j),2)
1319
               lat1   = tra(time(j),3)               
1320
               call LctPtRelBndry(lat1,lon1,flag)
1321
               if ( (flag.eq.1).or.(flag.eq.2) ) then
1322
                  var(j) = 1.
1323
               else
1324
                  var(j) = 0.
1325
               endif
1326
            enddo
1327
 
1328
c        Implict variable: INBOX 
1329
         elseif ( ivar.eq. -4 ) then
1330
            do j=1,nsel
1331
               lon1   = tra(time(j),2)
1332
               lat1   = tra(time(j),3)               
1333
               if ( ( lon1.ge.param(1) ).and.       ! lonmin
1334
     >              ( lon1.le.param(2) ).and.       ! lonmax
1335
     >              ( lat1.ge.param(3) ).and.       ! latmin
1336
     >              ( lat1.le.param(4) ) )          ! latmax
1337
     >         then
1338
                  var(j) = 1
1339
               else
1340
                  var(j) = 0
1341
               endif
1342
            enddo
1343
 
1344
c        Implict variable: INCIRCLE (lonc=param(1),latc=param(2),radius=param(3))
1345
         elseif ( ivar.eq. -5 ) then
1346
            do j=1,nsel
1347
               lon1   = tra(time(j),2)
1348
               lat1   = tra(time(j),3)               
1349
 
1350
               dist = sdis( lon1,lat1,param(1),param(2) ) 
1351
 
1352
               if ( dist.le.param(3) ) then
1353
                  var(j) = 1
1354
               else
1355
                  var(j) = 0
1356
               endif
1357
            enddo
1358
 
1359
c        Implict variable: INREGION (xcorner=param(1..4),ycorner=param(5..8) )
1360
         elseif ( ivar.eq.-6 ) then
1361
 
1362
            do j=1,4
1363
               xcorner(j) = param(j  )
1364
               ycorner(j) = param(j+4)
1365
            enddo
1366
 
1367
            do j=1,nsel
1368
               lon1   = tra(time(j),2)
1369
               lat1   = tra(time(j),3)  
1370
               var(j) = inregion (lon1,lat1,xcorner,ycorner)               
1371
            enddo
1372
 
1373
c        Implict variable: TRIGGER
1374
         elseif ( ivar.eq. -7 ) then
1375
            do j=1,nsel
1376
               intvar(j) = trigger( time(j) ) 
1377
            enddo
1378
 
1379
 
1380
c        Explicit variable (column index <ivar>)
1381
         else
1382
            do j=1,nsel
1383
               var(j) = tra(time(j),ivar)
1384
            enddo
1385
 
1386
         endif
1387
 
1388
c        Take MEAN of the variable (mean of selected times)
1389
         if (imode.eq.2) then
1390
            tmp=0.
1391
            do j=1,nsel
1392
               tmp=tmp+var(j)
1393
            enddo
1394
            var(1)=tmp/real(nsel)
1395
            nsel=1
1396
 
1397
c        Take MAX of the variable (maximum of selected times)
1398
         elseif (imode.eq.3) then
1399
            tmp=var(1)
1400
            do j=2,nsel
1401
               if (var(j).gt.tmp) tmp=var(j)
1402
            enddo
1403
            var(1)=tmp
1404
            nsel=1
1405
 
1406
c        Take MIN of the variable (minimum of selected times)
1407
         elseif (imode.eq.4) then
1408
            tmp=var(1)
1409
            do j=2,nsel
1410
               if (var(j).lt.tmp) tmp=var(j)
1411
            enddo
1412
            var(1)=tmp
1413
            nsel=1
1414
 
1415
c        Take VAR of the variable (variance over all selected times)
1416
         elseif (imode.eq.5) then
1417
            tmp=0.
1418
            do j=1,nsel
1419
               tmp=tmp+var(j)
1420
            enddo
1421
            mea=tmp/real(nsel)
1422
            do j=1,nsel
1423
              tmp=tmp+(var(j)-mea)**2
1424
            enddo
1425
            var(1)=1./real(nsel-1)*tmp
1426
            nsel=1
1427
 
1428
c        Take SUM of the variable (sum over all selected times)
1429
         elseif (imode.eq.6) then
1430
            tmp=0.
1431
            do j=1,nsel
1432
               tmp=tmp+var(j)
1433
            enddo
1434
            var(1)=tmp
1435
            nsel=1
1436
 
1437
c        Take CHANGE of the variable (change between first and last time)
1438
         elseif (imode.eq.7) then
1439
            var(1)=abs(var(1)-var(nsel))
1440
            nsel=1
1441
 
1442
c        Take DIFF of the variable (first minus last time)
1443
         elseif (imode.eq.8) then
1444
            var(1)=var(1)-var(nsel)
1445
            nsel=1
1446
 
1447
         endif
1448
 
1449
c        --- Apply the operators to the single values ---
1450
 
1451
         do j=1,nsel
1452
 
1453
c           GT
1454
            if (icmd.eq.1) then
1455
               if (var(j).gt.param(1)) then
1456
                  istrue(j)=1
1457
               else
1458
                  istrue(j)=0
1459
               endif
1460
 
1461
c           LT
1462
            elseif (icmd.eq.2) then
1463
               if (var(j).lt.param(1)) then
1464
                  istrue(j)=1
1465
               else
1466
                  istrue(j)=0
1467
               endif
1468
 
1469
c           IN
1470
            elseif (icmd.eq.3) then
1471
               if ( (var(j).gt.param(1)).and.
1472
     >              (var(j).lt.param(2)) ) 
1473
     >         then
1474
                  istrue(j)=1
1475
               else
1476
                  istrue(j)=0
1477
               endif
1478
 
1479
c           OUT
1480
            elseif (icmd.eq.4) then
1481
               if ( (var(j).lt.param(1)).or.
1482
     >              (var(j).gt.param(2)) ) 
1483
     >         then
1484
                  istrue(j)=1
1485
               else
1486
                  istrue(j)=0
1487
               endif
1488
 
1489
c           EQ
1490
            elseif (icmd.eq.5) then
1491
               if (abs(var(j)-param(1)).lt.eps) then
1492
                  istrue(j)=1
1493
               else
1494
                  istrue(j)=0
1495
               endif
1496
 
1497
c           TRUE
1498
            elseif (icmd.eq.6) then
1499
               if (abs(var(j)).lt.eps) then
1500
                  istrue(j)=0
1501
               else
1502
                  istrue(j)=1
1503
               endif
1504
 
1505
c           FALSE
1506
            elseif (icmd.eq.7) then
1507
               if (abs(var(j)).lt.eps) then
1508
                  istrue(j)=1
1509
               else
1510
                  istrue(j)=0
1511
               endif
1512
 
1513
c           ALL
1514
            elseif (icmd.eq.8) then
1515
               istrue(j) = 1
1516
               do k=1,nval
1517
                  iparam = nint(param(k))-1
1518
                  if (btest(intvar(j),iparam).eqv..false.) then
1519
                     istrue(j) = 0
1520
                  endif
1521
               enddo
1522
 
1523
c           ANY
1524
            elseif (icmd.eq.9) then
1525
               istrue(j) = 0
1526
               do k=1,nval
1527
                  iparam = nint(param(k))-1
1528
                  if (btest(intvar(j),iparam).eqv..true.) then
1529
                     istrue(j) = 1
1530
                  endif
1531
               enddo
1532
 
1533
c           NONE
1534
            elseif (icmd.eq.10) then
1535
               istrue(j) = 1
1536
               do k=1,nval
1537
                  iparam = nint(param(k))-1
1538
                  if (btest(intvar(j),iparam).eqv..true.) then
1539
                     istrue(j) = 0
1540
                  endif
1541
               enddo
1542
 
1543
            endif
1544
 
1545
         enddo
1546
 
1547
c        --- Determine the overall boolean value ----------
1548
 
1549
c        ALL
1550
         if (itime.eq.1) then
1551
            decision=1
1552
            do j=1,nsel
1553
               if (istrue(j).eq.0) then
1554
                  decision=0
1555
                  goto 110
1556
               endif
1557
            enddo
1558
 110        continue
1559
 
1560
c        ANY
1561
         elseif (itime.eq.2) then
1562
            decision=0
1563
            do j=1,nsel
1564
               if (istrue(j).eq.1) then
1565
                  decision=1
1566
                  goto 120
1567
               endif
1568
            enddo
1569
 120        continue
1570
 
1571
c        NONE
1572
         elseif (itime.eq.3) then
1573
            decision=1
1574
            do j=1,nsel
1575
               if (istrue(j).eq.1) then
1576
                  decision=0
1577
                  goto 130
1578
               endif
1579
            enddo
1580
 130        continue
1581
 
1582
c        TRIGGER
1583
         elseif (itime.lt.0) then
1584
            decision=1
1585
            do j=1,nsel
1586
               if (istrue(j).eq.1) then
1587
                  trigger(j) = ior( trigger(j), 2**(abs(itime)-1) )
1588
               endif
1589
            enddo
1590
 
1591
         endif
1592
 
1593
c        --- Put the new boolean value onto the stack
1594
 
1595
         nstack=nstack+1
1596
         stack(nstack)=decision
1597
 
1598
c        Exit point for loop
1599
 200     continue
1600
         goto 100
1601
 
1602
      endif
1603
 
1604
c     Return the decision (selected or non-selected)
1605
 300  continue
1606
 
1607
      select=stack(1)
1608
 
1609
      end
1610
 
1611
 
1612
c     --------------------------------------------------------------------------
1613
c     Split a region string and get corners of the domain
1614
c     --------------------------------------------------------------------------
1615
 
1616
      subroutine regionsplit(string,iregion,xcorner,ycorner)
1617
 
1618
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
1620
c     four coordinates for the region
1621
 
1622
      implicit none
1623
 
1624
c     Declaration of subroutine parameters
1625
      character*80    string
1626
      real            xcorner(4),ycorner(4)
1627
      integer         iregion
1628
 
1629
c     Local variables
1630
      integer         i,n
1631
      integer         il,ir
1632
      real            subfloat (80)
1633
      integer         stat
1634
      integer         len
1635
 
1636
c     ------- Split the string
1637
      i    = 1
1638
      n    = 0
1639
      stat = 0
1640
      il   = 1
1641
      len  = len_trim(string)
1642
 
1643
 100  continue
1644
 
1645
c     Find start of a substring
1646
      do while ( stat.eq.0 )
1647
         if ( string(i:i).ne.' ' ) then
1648
            stat = 1
1649
            il   = i
1650
         else
1651
            i = i + 1
1652
         endif
1653
      enddo
1654
 
1655
c     Find end of substring
1656
      do while ( stat.eq.1 )         
1657
         if ( ( string(i:i).eq.' ' ) .or. ( i.eq.len ) ) then
1658
            stat = 2
1659
            ir   = i
1660
         else
1661
            i    = i + 1
1662
         endif
1663
      enddo
1664
 
1665
c     Convert the substring into a number
1666
      if ( stat.eq.2 ) then
1667
         n = n + 1
1668
         read(string(il:ir),*) subfloat(n)
1669
         stat = 0
1670
      endif
1671
 
1672
      if ( i.lt.len ) goto 100
1673
 
1674
 
1675
c     -------- Get the region number
1676
 
1677
      iregion = nint(subfloat(1))
1678
 
1679
c     -------- Get the corners of the region
1680
 
1681
      if ( n.eq.5 ) then     ! lonw(2),lone(3),lats(4),latn(5)
1682
 
1683
         xcorner(1) = subfloat(2)
1684
         ycorner(1) = subfloat(4)
1685
 
1686
         xcorner(2) = subfloat(3)
1687
         ycorner(2) = subfloat(4)
1688
 
1689
         xcorner(3) = subfloat(3)
1690
         ycorner(3) = subfloat(5)
1691
 
1692
         xcorner(4) = subfloat(2)
1693
         ycorner(4) = subfloat(5)
1694
 
1695
      elseif ( n.eq.9 ) then     ! lon1,lat1,lon2,lat2,lon3,lon4,lat4
1696
 
1697
         xcorner(1) = subfloat(2)
1698
         ycorner(1) = subfloat(3)
1699
 
1700
         xcorner(2) = subfloat(4)
1701
         ycorner(2) = subfloat(5)
1702
 
1703
         xcorner(3) = subfloat(6)
1704
         ycorner(3) = subfloat(7)
1705
 
1706
         xcorner(4) = subfloat(8)
1707
         ycorner(4) = subfloat(9)
1708
 
1709
      else
1710
 
1711
         print*,' ERROR: invalid region specification '
1712
         print*,'     ',trim(string)
1713
         stop
1714
 
1715
      endif
1716
 
1717
 
1718
      end
1719
 
1720
c     --------------------------------------------------------------------------
1721
c     Decide whether lat/lon point is in or out of region
1722
c     --------------------------------------------------------------------------
1723
 
1724
      integer function inregion (lon,lat,xcorner,ycorner)
1725
 
1726
c     Decide whether point (lon/lat) is in the region specified by <xcorner(1..4),
1727
c     ycorner(1..4).
1728
 
1729
      implicit none
1730
 
1731
c     Declaration of subroutine parameters
1732
      real    lon,lat
1733
      real    xcorner(4),ycorner(4)
1734
 
1735
c     Local variables
1736
      integer flag
1737
      real    xmin,xmax,ymin,ymax
1738
      integer i
1739
 
1740
c     Reset the flag
1741
      flag = 0
1742
 
1743
c     Set some boundaries
1744
      xmax = xcorner(1)
1745
      xmin = xcorner(1)
1746
      ymax = ycorner(1)
1747
      ymin = ycorner(1)
1748
      do i=2,4
1749
        if (xcorner(i).lt.xmin) xmin = xcorner(i)
1750
        if (xcorner(i).gt.xmax) xmax = xcorner(i)
1751
        if (ycorner(i).lt.ymin) ymin = ycorner(i)
1752
        if (ycorner(i).gt.ymax) ymax = ycorner(i)
1753
      enddo
1754
 
1755
c     Do the tests - set flag=1 if all tests pased
1756
      if (lon.lt.xmin) goto 970
1757
      if (lon.gt.xmax) goto 970
1758
      if (lat.lt.ymin) goto 970
1759
      if (lat.gt.ymax) goto 970
1760
 
1761
      if ((lon-xcorner(1))*(ycorner(2)-ycorner(1))-
1762
     >    (lat-ycorner(1))*(xcorner(2)-xcorner(1)).gt.0.) goto 970
1763
      if ((lon-xcorner(2))*(ycorner(3)-ycorner(2))-
1764
     >    (lat-ycorner(2))*(xcorner(3)-xcorner(2)).gt.0.) goto 970
1765
      if ((lon-xcorner(3))*(ycorner(4)-ycorner(3))-
1766
     >    (lat-ycorner(3))*(xcorner(4)-xcorner(3)).gt.0.) goto 970
1767
      if ((lon-xcorner(4))*(ycorner(1)-ycorner(4))-
1768
     >    (lat-ycorner(4))*(xcorner(1)-xcorner(4)).gt.0.) goto 970
1769
 
1770
      flag = 1
1771
 
1772
c     Return the value
1773
 970  continue
1774
 
1775
      inregion = flag
1776
 
1777
      return
1778
 
1779
      end
1780
 
1781
 
1782
c     --------------------------------------------------------------------------
1783
c     Spherical distance between lat/lon points                                                       
1784
c     --------------------------------------------------------------------------
1785
 
1786
      real function sdis(xp,yp,xq,yq)
1787
c
1788
c     calculates spherical distance (in km) between two points given
1789
c     by their spherical coordinates (xp,yp) and (xq,yq), respectively.
1790
c
1791
      real      re
1792
      parameter (re=6370.)
1793
      real      pi180
1794
      parameter (pi180=3.14159/180.)
1795
      real      xp,yp,xq,yq,arg
1796
 
1797
      arg=sin(pi180*yp)*sin(pi180*yq)+
1798
     >    cos(pi180*yp)*cos(pi180*yq)*cos(pi180*(xp-xq))
1799
      if (arg.lt.-1.) arg=-1.
1800
      if (arg.gt.1.) arg=1.
1801
 
1802
      sdis=re*acos(arg)
1803
 
1804
      end
1805
 
1806
 
1807
c     ****************************************************************
1808
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- *
1810
c     * -rary point P lies inside S, outside S, or on its boundary.  *
1811
c     * The calling program must first call DefSPolyBndry to define  *
1812
c     * the boundary of S and the point X. Any subsequent call to    *
1813
c     * subroutine LctPtRelBndry will determine if some point P lies *
1814
c     * inside or outside S, or on its boundary. (Usually            *
1815
c     * DefSPolyBndry is called once, then LctPrRelBndry is called   *
1816
c     * many times).                                                 *
1817
c     *                                                              * 
1818
c     * REFERENCE:            Bevis, M. and Chatelain, J.-L. (1989)  * 
1819
c     *                       Maflaematical Geology, vol 21.         *
1820
c     * VERSION 1.0                                                  *
1821
c     ****************************************************************
1822
 
1823
      Subroutine DefSPolyBndry(vlat,vlon,nv,xlat, xlon)
1824
 
1825
c     ****************************************************************
1826
c     * This mmn entry point is used m define ~e spheric~ polygon S  *
1827
c     * and the point X.                                             *
1828
c     * ARGUMENTS:                                                   *
1829
c     * vlat,vlon (sent) ... vectors containing the latitude and     * 
1830
c     *                      longitude of each vertex of the         *
1831
c     *                      spherical polygon S. The ith.vertex is  *
1832
c     *                      located at [vlat(i),vlon(i)].           *
1833
c     * nv        (sent) ... the number of vertices and sides in the *
1834
c     *                      spherical polygon S                     *
1835
c     * xlat,xlon (sent) ... latitude and longitude of some point X  *
1836
c     *                      located inside S. X must not be located *
1837
c     *                      on any great circle that includes two   *
1838
c     *                      vertices of S.                          *
1839
c     *                                                              *
1840
c     * UNITS AND SIGN CONVENTION:                                   *
1841
c     *  Latitudes and longitudes are specified in degrees.          *
1842
c     *  Latitudes are positive to the north and negative to the     *
1843
c     *  south.                                                      *
1844
c     *  Longitudes are positive to the east and negative to the     *
1845
c     *  west.                                                       *
1846
c     *                                                              * 
1847
c     * VERTEX ENUMERATION:                                          * 
1848
c     * The vertices of S should be numbered sequentially around the *
1849
c     * border of the spherical polygon. Vertex 1 lies between vertex*
1850
c     * nv and vertex 2. Neighbouring vertices must be seperated by  *
1851
c     * less than 180 degrees. (In order to generate a polygon side  *
1852
c     * whose arc length equals or exceeds 180 degrees simply        *
1853
c     * introduce an additional (pseudo)vertex). Having chosen       *
1854
c     * vertex 1, the user may number the remaining vertices in      *
1855
c     * either direction. However if the user wishes to use the      *
1856
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 *
1858
c     * she must follow the convention whereby in moving around the  *
1859
c     * polygon border in the direction of increasing vertex number  *
1860
c     * clockwise bends occur at salient vertices. A vertex is       *
1861
c     * salient if the interior angle is less than 180 degrees.      *
1862
c     * (In the case of a convex polygon this convention implies     *
1863
c     * that vertices are numbered in clockwise sequence).           *
1864
c     ****************************************************************
1865
 
1866
      implicit none
1867
 
1868
      integer mxnv,nv
1869
 
1870
c     ----------------------------------------------------------------
1871
c     Edit next statement to increase maximum number of vertices that 
1872
c     may be used to define the spherical polygon S               
1873
c     The value of parameter mxnv in subroutine LctPtRelBndry must match
1874
c     that of parameter mxnv in this subroutine, as assigned above.
1875
c     ----------------------------------------------------------------
1876
      parameter (mxnv=2000)
1877
 
1878
      real  vlat(nv),vlon(nv),xlat,xlon,dellon
1879
      real  tlonv(mxnv),vlat_c(mxnv),vlon_c(mxnv),xlat_c,xlon_c
1880
      integer i,ibndry,nv_c,ip
1881
 
1882
      data ibndry/0/
1883
 
1884
      common/spolybndry/vlat_c,vlon_c,nv_c,xlat_c,xlon_c,tlonv,ibndry
1885
 
1886
      if (nv.gt.mxnv) then
1887
         print *,'nv exceeds maximum allowed value'
1888
         print *,'adjust parameter mxnv in subroutine DefSPolyBndry'
1889
         stop
1890
      endif
1891
 
1892
      ibndry=1                  ! boundary defined at least once (flag)
1893
      nv_c=nv                   ! copy for named common
1894
      xlat_c=xlat               ! . . . .
1895
      xlon_c=xlon               !
1896
 
1897
      do i=1,nv
1898
         vlat_c(i)=vlat(i)      ! "
1899
         vlon_c(i)=vlon(i)      !
1900
 
1901
         call TrnsfmLon(xlat,xlon,vlat(i),vlon(i),tlonv(i))
1902
 
1903
         if (i.gt.1) then
1904
            ip=i-1
1905
         else
1906
            ip=nv
1907
         endif
1908
 
1909
         if ((vlat(i).eq.vlat(ip)).and.(vlon(i).eq.vlon(ip))) then
1910
            print *,'DefSPolyBndry detects user error:'
1911
            print *,'vertices ',i,' and ',ip,' are not distinct'
1912
            print*,'lat ',i,ip,vlat(i),vlat(ip)
1913
            print*,'lon ',i,ip,vlon(i),vlon(ip)            
1914
            stop
1915
         endif
1916
 
1917
         if (tlonv(i).eq.tlonv(ip)) then
1918
            print *,'DefSPolyBndry detects user error:'
1919
            print *,'vertices ',i,' & ',ip,' on same gt. circle as X'
1920
            stop
1921
         endif
1922
 
1923
         if (vlat(i).eq.(-vlat(ip))) then
1924
            dellon=vlon(i)-vlon(ip)
1925
            if (dellon.gt.+180.) dellon=dellon-360.
1926
            if (dellon.lt.-180.) dellon=dellon-360.
1927
            if ((dellon.eq.+180.0).or.(dellon.eq.-180.0)) then
1928
               print *,'DefSPolyBndry detects user error:'
1929
               print *,'vertices ',i,' and ',ip,' are antipodal'
1930
               stop
1931
            endif
1932
         endif
1933
      enddo
1934
 
1935
      return
1936
 
1937
      end
1938
 
1939
 
1940
c     ****************************************************************
1941
 
1942
      Subroutine LctPtRelBndry(plat,plon,location)
1943
 
1944
c     ****************************************************************
1945
 
1946
c     ****************************************************************
1947
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  *
1949
c     * S previously defined by a call to subroutine DefSPolyBndry.  *
1950
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*
1952
c     * (ie.P and X cannot be seperated by exactly 180 degrees).     *
1953
c     * ARGUMENTS:                                                   *  
1954
c     * plat,plon (sent)... the latitude and longitude of point P    *
1955
c     * location (returned)... specifies the location of P:          *
1956
c     *                        location=0 implies P is outside of S  *
1957
c     *                        location=1 implies P is inside of S   *
1958
c     *                        location=2 implies P on boundary of S *
1959
c     *                        location=3 implies user error (P is   *
1960
c     *                                     antipodal to X)          *
1961
c     * UNFfS AND SIGN CONVENTION:                                   * 
1962
c     *  Latitudes and longitudes are specified in degrees.          *
1963
c     *  Latitudes are positive to the north and negative to the     *
1964
c     *  south.                                                      *    
1965
c     *  Longitudes are positive to the east and negative to the     *
1966
c     *  west.                                                       *
1967
c     ****************************************************************
1968
 
1969
      implicit none
1970
 
1971
      integer mxnv
1972
 
1973
c     ----------------------------------------------------------------
1974
c     The statement below must match that in subroutine DefSPolyBndry
1975
c     ----------------------------------------------------------------
1976
 
1977
      parameter (mxnv=2000)
1978
 
1979
      real tlonv(mxnv),vlat_c(mxnv),vlon_c(mxnv),xlat_c,xlon_c
1980
      real plat,plon,vAlat,vAlon,vBlat,vBlon,tlonA,tlonB,tlonP
1981
      real tlon_X,tlon_P,tlon_B,dellon
1982
      integer i,ibndry,nv_c,location,icross,ibrngAB,ibrngAP,ibrngPB
1983
      integer ibrng_BX,ibrng_BP,istrike
1984
 
1985
      common/spolybndry/vlat_c,vlon_c,nv_c,xlat_c,xlon_c,tlonv,ibndry
1986
 
1987
      if (ibndry.eq.0) then     ! user has never defined the bndry
1988
         print*,'Subroutine LctPtRelBndry detects user error:'
1989
         print*,'Subroutine DefSPolyBndry must be called before'
1990
         print*,'subroutine LctPtRelBndry can be called'
1991
         stop
1992
      endif
1993
 
1994
      if (plat.eq.(-xlat_c)) then
1995
         dellon=plon-xlon_c
1996
         if (dellon.lt.(-180.)) dellon=dellon+360.
1997
         if (dellon.gt.+180.) dellon=dellon-360.
1998
         if ((dellon.eq.+180.0).or.(dellon.eq.-180.)) then
1999
            print*,'Warning: LctPtRelBndry detects case P antipodal
2000
     >           to X'
2001
            print*,'location of P relative to S is undetermined'
2002
            location=3
2003
            return
2004
         endif
2005
      endif 
2006
 
2007
      location=0                ! default ( P is outside S)
2008
      icross=0                  ! initialize counter
2009
 
2010
      if ((plat.eq.xlat_c).and.(plon.eq.xlon_c)) then
2011
         location=1
2012
         return
2013
      endif
2014
 
2015
 
2016
      call TrnsfmLon (xlat_c,xlon_c,plat,plon,tlonP)
2017
 
2018
      do i=1,nv_c              ! start of loop over sides of S 
2019
 
2020
         vAlat=vlat_c(i)
2021
         vAlon=vlon_c(i)
2022
         tlonA=tlonv(i)
2023
 
2024
         if (i.lt.nv_c) then
2025
            vBlat=vlat_c(i+1)
2026
            vBlon=vlon_c(i+1)
2027
            tlonB=tlonv(i+1)
2028
         else
2029
            vBlat=vlat_c(1)
2030
            vBlon=vlon_c(1)
2031
            tlonB=tlonv(1)
2032
         endif
2033
 
2034
         istrike=0
2035
 
2036
         if (tlonP.eq.tlonA) then
2037
            istrike=1
2038
         else
2039
            call EastOrWest(tlonA,tlonB,ibrngAB)
2040
            call EastOrWest(tlonA,tlonP,ibrngAP)
2041
            call EastOrWest(tlonP,tlonB,ibrngPB)
2042
 
2043
 
2044
            if((ibrngAP.eq.ibrngAB).and.(ibrngPB.eq.ibrngAB)) istrike=1
2045
         endif
2046
 
2047
 
2048
         if (istrike.eq.1) then
2049
 
2050
            if ((plat.eq.vAlat).and.(plon.eq.vAlon)) then
2051
               location=2       ! P lies on a vertex of S
2052
               return
2053
            endif
2054
            call TrnsfmLon(vAlat,vAlon,xlat_c,xlon_c,tlon_X)
2055
            call TrnsfmLon(vAlat,vAlon,vBlat,vBlon,tlon_B)
2056
            call TrnsfmLon(vAlat,vAlon,plat,plon,tlon_P)
2057
 
2058
            if (tlon_P.eq.tlon_B) then
2059
               location=2       ! P lies on side of S
2060
               return 
2061
            else
2062
               call EastOrWest(tlon_B,tlon_X,ibrng_BX)
2063
               call EastOrWest(tlon_B,tlon_P,ibrng_BP)
2064
               if(ibrng_BX.eq.(-ibrng_BP)) icross=icross+1
2065
            endif
2066
 
2067
         endif
2068
      enddo                     ! end of loop over the sides of S
2069
 
2070
 
2071
c     if the arc XP crosses the boundary S an even number of times then P
2072
c     is in S
2073
 
2074
      if (mod(icross,2).eq.0) location=1
2075
 
2076
      return
2077
 
2078
      end
2079
 
2080
 
2081
c     ****************************************************************
2082
 
2083
      subroutine TrnsfmLon(plat,plon,qlat,qlon,tranlon)
2084
 
2085
c     ****************************************************************
2086
c     * This subroutine is required by subroutines DefSPolyBndry &   *
2087
c     * LctPtRelBndry. It finds the 'longitude' of point Q in a      *
2088
c     * geographic coordinate system for which point P acts as a     *
2089
c     * 'north pole'. SENT: plat,plon,qlat,qlon, in degrees.         *
2090
c     * RETURNED: tranlon, in degrees.                               *
2091
c     ****************************************************************
2092
 
2093
      implicit none
2094
 
2095
      real pi,dtr,plat,plon,qlat,qlon,tranlon,t,b
2096
      parameter (pi=3.141592654,dtr=pi/180.0)
2097
 
2098
      if (plat.eq.90.) then
2099
         tranlon=qlon
2100
      else
2101
         t=sin((qlon-plon)*dtr)*cos(qlat*dtr)
2102
         b=sin(dtr*qlat)*cos(plat*dtr)-cos(qlat*dtr)*sin(plat*dtr)
2103
     >    *cos((qlon-plon)*dtr)
2104
         tranlon=atan2(t,b)/dtr
2105
      endif
2106
 
2107
      return
2108
      end
2109
 
2110
c     ****************************************************************
2111
 
2112
      subroutine EastOrWest(clon,dlon,ibrng)
2113
 
2114
c     ****************************************************************
2115
c     * This subroutine is required by subroutine LctPtRelBndry.     *
2116
c     * This routine determines if in travelling the shortest path   *
2117
c     * from point C (at longitude clon) to point D (at longitude    *
2118
c     * dlon) one is heading east, west or neither.                  *
2119
c     * SENT: clon,dlon; in degrees. RETURNED: ibrng                 *
2120
c     * (1=east,-1=west, 0=neither).                                 *
2121
c     ****************************************************************
2122
 
2123
      implicit none
2124
      real clon,dlon,del
2125
      integer ibrng
2126
      del=dlon-clon
2127
      if (del.gt.180.) del=del-360.
2128
      if (del.lt.-180.) del=del+360.
2129
      if ((del.gt.0.0).and.(del.ne.180.)) then
2130
         ibrng=-1               ! (D is west of C)
2131
      elseif ((del.lt.0.0).and.(del.ne.-180.)) then
2132
         ibrng=+1               ! (D is east of C)
2133
      else
2134
         ibrng=0                ! (D north or south of C)
2135
      endif
2136
      return
2137
      end