C+ C NAME: C ArrR4Median C PURPOSE: C Get median of all elements in real*4 array C CALLING SEQUENCE: function ArrR4Median(N,A,W,M,B) C INPUTS: C N integer abs(N) is # elements in A C if N < 0 then bad values in A are ignored C A(*) real array C W real bin width C OUTPUTS: C M integer # elements contributing to median C =N if N > 0 C =N-(# bad values) if N < 0 C B(M) real M valid elements of A in sorted order C CALLS: C SortR4, BadR4 C SEE ALSO: C Array_Info C PROCEDURE: C Array A and array B can be the same array, i.e. the following is legal: C call ArrR4Median(N,A,M,A) C After completion M=N (if N>0) and A will be sorted into ascending order. C- integer N real A(*) integer M real B(*) bad = BadR4() ! Move all good values in peds to the beginning of the array prior to sorting. M = 0 do I=1,abs(N) if (N .gt. 0 .or. A(I) .ne. bad) then M = M+1 B(M) = A(I) end if end do if (M .eq. 0) then ArrR4Median = bad else ! Sort array B. Array B(M/2) is the first estimate of the median. call SortR4(1,M,1,M,B) I = max(M/2,1) ArrR4Median = B(I) if (W .ne. bad) then ! Get index of first element in B with value B(M/2) klow = I-1 do while (klow .ge. 1 .and. B(klow) .eq. ArrR4Median) klow = klow-1 end do klow = klow+1 ! Get index of element after last element in B with value B(M/2) khigh = I+1 do while (khigh .le. M .and. B(khigh) .eq. ArrR4Median) khigh = khigh+1 end do ! khigh-klow is number of points with value B(M/2). ! The correction changes the median by up to +/-0.5. ArrR4Median = ArrR4Median+(float(I-klow)/float(khigh-klow)-0.5)*W end if end if return end