pro findtran,trans,lun,rec_len,num_recs,start_rec,end_rec ;=================================================================== ; ; By S Mayor in November 1995 ; Modified from findtime.pro ; Uses binary search to find starting and ending record numbers of ; a transect. This routine assumes transects only change by one ; when they do. ; Example: 0 0 0 0 0 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 5 etc. ; Not: 0 0 0 0 0 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 6 etc. ; Am not sure how it would handle finding the lower limit of ; transect 2 in the above case. Nor am I sure how it would behave ; if the user requested transect 1. SDM. ; ; INPUT: ; trans..... user-supplied transect number ; lun....... logical unit number ; rec_len... record length ; num_recs.. number of records in the file ; ; OUTPUT: ; rec1...... starting record number of the transect ; rec2...... ending record number of the transect ; ; NOTES: ; Data file must already by open by main calling this routine. ; Simply pass in transect number, logical unit number, ; record length and number of records in the file. ; ;==================================================================== record = assoc(lun,fltarr(rec_len)) ; First find the transect number ; of the first record in the file z=record(0) byteswap = 0b if (z(11) lt 1) then byteswap = 1b if (byteswap) then byteorder,z,/Lswap t1 = z(29) ; Then find the transect number ; of the last record in the file z=record(num_recs-1L) if (byteswap) then byteorder,z,/Lswap t2 = z(29) ; If the transect the user requested is not in ; the file then set start_rec and end_rec to flags ; and return to main. if ((trans lt t1) or (trans gt t2)) then begin start_rec = -1L end_rec = -1L return endif ; Otherwise set the lower and upper limits for the ; search to the first and last record number jl=0L ju=num_recs-1L ; Begin binary-search for any record with ; transect number equal to trans entry: jm=long((jl+ju)/2.) z=record(jm) if (byteswap) then byteorder,z,/Lswap t=z(29) ;print,jl,jm,ju,t if (t eq trans) then goto,search_limits if (t gt trans) then begin ju = jm endif else begin jl = jm endelse goto,entry search_limits: ; Remember these limits to begin ; upper boundary search jl2 = jl jm2 = jm ju2 = ju ; Begin search search for lower ; limit of trans old = jl while (abs(jm-old) ne 1) do begin old = jm z = record(jm) if (byteswap) then byteorder,z,/Lswap t = z(29) ; print,jl,jm,ju,t if (t eq trans) then begin ju = jm endif else begin jl = jm endelse jm = long((ju + jl) / 2.) endwhile ; Because of uncertainty on whether the transect transition ; occurs from jl to jm OR from jm to ju, scan through jl, jm and ju ; and find the first record with a transect number equal to trans y=[jl,jm,ju] x=-1 i=0 while ((x lt trans) and (i le 2)) do begin z=record(y(i)) if (byteswap) then byteorder,z,/Lswap x=z(29) i=i+1 endwhile start_rec = y(i-1) ; Begin search search for upper ; limit of trans old = ju2 while (abs(jm2-old) ne 1) do begin old = jm2 z = record(jm2) if (byteswap) then byteorder,z,/Lswap t = z(29) ; print,jl2,jm2,ju2,t if (t eq trans) then begin jl2 = jm2 endif else begin ju2 = jm2 endelse jm2 = fix((ju2 + jl2) / 2.) endwhile ; Because of uncertainty on whether the transect transition ; occurs from jl2 to jm2 OR from jm2 to ju2, scan through jl2, ; jm2 and ju2 and find the last record with a transect number ; equal to trans y=[jl2,jm2,ju2] x=trans i=0 while ((x eq trans) and (i le 2)) do begin z=record(y(i)) if (byteswap) then byteorder,z,/Lswap x=z(29) i=i+1 endwhile end_rec = y(i-2) ; If trans is the last transect in the file, ; be sure to take the last record if (trans eq t2) then end_rec = num_recs - 1L return end