integer function finddate ( indate, flag, dates, xdates, ndates ) C C searchs for a date in an array of dates. The format of the C dates in both the array and the date is for example, C C 23-DEC-1992 09:47:56.1234 C C return value = index of latest date in array just prior to indate C retrun = -1 if indate preceds dates[1] C C assumptions: C 1) ndates > 1 C 2) the array is time ordered. C C name type dimension description C ------- ---- ---------- --------------------------- C indate c*25 1 date to find in array dates C flag logical .true. use dates C .false. use xdates C dates c*25 ndates array of dates C xdates D.P. ndates same, in DP ch2sec output format C ndates i 1 size of array dates C integer idate data idate/-1/ integer ndates, i1, i2, imid character*25 dates(ndates), sec2ch double precision ch2sec, tin, tcheck, it1, it2, itmid double precision xdates(ndates) external ch2sec logical error, flag tin = ch2sec ( indate, .true., error) i1 = 1 i2 = ndates if (flag) then it1 = ch2sec ( dates(i1), .true., error) it2 = ch2sec ( dates(i2), .true., error) else it1 = xdates(i1) it2 = xdates(i2) end if C C check for date out of range of array C if (tin .lt. it1 ) then idate = -1 goto 9999 else if (tin .ge. it2 ) then idate = ndates goto 9999 end if C C at this point, know that C C dates(1) <= indate < dates(ndate) C C check for only 2 points C if ( ndates .eq. 2 ) then idate = 1 goto 9999 end if C C there are at least 3 points in the array. Narrow it down. C do while ( (i2-i1) .gt. 1) imid = (i2+i1)/2 if (flag) then itmid = ch2sec ( dates(imid), .true., error) else itmid = xdates(imid) end if if (tin .lt. itmid) then i2 = imid else i1 = imid end if end do idate = i1 C C check for repeated time code C 8888 continue i3 = idate+1 if (i3 .gt. ndates) goto 9999 if (flag) then itmid = ch2sec ( dates(i3), .true., error) else itmid = xdates (i3) end if if (itmid .lt. tin) then idate = i3 goto 8888 end if 9999 continue finddate = idate return end C program tester C integer finddate C character*25 dates(11), indate C data dates / '16-APR-1992 23:30', C & '19-APR-1992 15:12', '13-MAY-1992 01:48', '18-JUN-1992 22:20', C & '27-JUL-1992 14:20', '29-AUG-1993 15:03', '2-SEP-1993 03:01', C & '6-OCT-1993 04:27', '11-NOV-1993 19:22', '12-DEC-1993 08:29', C & '13-MAY-1994 05:06'/ C write(6,*) dates C 50 write(6,*) '***' C write(6,*) 'Enter date:' C read(6,100) indate C 100 format (A25) C idate = finddate(indate, dates, 11) C write(6,*) 'idate = ', idate, ' ',dates(idate) C if (.true.) goto 50 C stop C end