COSMOS v7.655  COSMOSv7655
(AirShowerMC)
kdwhereis.f
Go to the documentation of this file.
1 ! test kdwhereis
2 !
3 !
4 ! implicit none
5 !c real*8 xa(5)/-1., 0., 1.33, 1.33, 9./
6 ! real*8 xa(5)/9.d0, 1.33d0, 1.33d0, 0.d0, -1.d0/
7 ! real*8 x
8 ! integer ios, loc
9 ! do while(1)
10 ! write(*,*) xa
11 ! write(*, *) 'enter x'
12 ! read(*, *, iostat=ios) x
13 ! if(ios .ne. 0) stop
14 ! call kdwhereis(x, 5, xa, 1, loc)
15 ! if(loc .eq. 0 .or. loc .eq. 5) then
16 ! write(*, *) " out of range"
17 ! endif
18 ! write(*, *) " loc=", loc, " xa =", xa(loc), xa(loc+1)
19 ! enddo
20 ! end
21 ! --------------------------------------------------------------------
22 ! find location of a given double value in a sorted given double
23 ! array.
24 !
25 ! *********************************
26  subroutine kdwhereis(x, in, a, step, loc)
27 ! *********************************
28 ! x : real*8. input. given double value.
29 ! in : integer. input. number of data in a.
30 ! a : real*8 a(step, in). input array.
31 ! a(1, 1), a(1, 2), a(1, 3)... are examined.
32 ! step: integer. input. see above. give 1 for one dim. array.
33 ! loc: integer. ouput. a(1,loc) <= x < a(1,loc+1) if a is ascending
34 ! (if a(1,in) = x, loc= in)
35 ! a(1, loc) > x >= a(1, loc+1) if a is dscending
36 !c (if a(1,1) = x, loc = 0)
37 ! if loc=0 or loc =in; x is out of range.
38 
39  implicit none
40  integer in, loc, step
41  real*8 x, a(step, in)
42 
43  logical ascending
44  integer i1, i2, im
45 
46  i1 = 0 ! lower and
47  i2 = in + 1 ! upper bound
48 
49  ascending = a(1, in) .gt. a(1, 1)
50  do while (i2 - i1 .gt. 1)
51  im = (i1 + i2)/2
52  if( ascending .eqv. x .ge. a(1, im) ) then
53  i1 = im
54  else
55  i2 = im
56  endif
57  enddo
58  loc = i1
59  end
60 !
61 ! *********************************
62  subroutine kwhereis(x, in, a, step, loc)
63 ! *********************************
64 ! x : real*4. input. given value.
65 ! in : integer. input. number of data in a.
66 ! a : real*4 a(step, in). input array.
67 ! a(1, 1), a(1, 2), a(1, 3)... are examined.
68 ! step: integer. input. see above. give 1 for one dim. array.
69 ! loc: integer. ouput. a(1,loc) <= x < a(1,loc+1) if a is ascending
70 ! (if a(1,in) = x, loc= in)
71 ! a(1, loc) > x >= a(1, loc+1) if a is dscending
72 !c (if a(1,1) = x, loc = 0)
73 ! if loc=0 or loc =in; x is out of range.
74 
75  implicit none
76  integer in, loc, step
77  real*4 x, a(step, in)
78 
79  logical ascending
80  integer i1, i2, im
81 
82  i1 = 0 ! lower and
83  i2 = in + 1 ! upper bound
84 
85  ascending = a(1, in) .gt. a(1, 1)
86  do while (i2 - i1 .gt. 1)
87  im = (i1 + i2)/2
88  if( ascending .eqv. x .ge. a(1, im) ) then
89  i1 = im
90  else
91  i2 = im
92  endif
93  enddo
94  loc = i1
95  end
subroutine kdwhereis(x, in, a, step, loc)
Definition: kdwhereis.f:27
subroutine kwhereis(x, in, a, step, loc)
Definition: kdwhereis.f:63