;+ ; NAME: ; GuessRecs ; PURPOSE: ; Guess number of records and record length for binary file ; CATEGORY: ; Tricks ; CALLING SEQUENCE: FUNCTION GuessRecs, iu, approx_recl, range, recl=recl ; INPUTS: ; iu scalar; type: integer ; logical unit number of open file ; approx_recl scalar; type: integer ; approximate record length in bytes ; OPTIONAL INPUT PARAMETERS: ; range scalar: type: integer: default: 1 ; range of recordlength tested is approx_recl +/- range ; OUTPUTS: ; Results scalar; type: integer ; guess at # records (-1 if not succesful) ; OPTIONAL OUTPUT PARAMETERS: ; recl = recl scalar: type: integer ; guess at record length in bytes (= file size/# records) ; (-1 if not succesful) ; INCLUDE: @compile_opt.pro ; On error, return to caller ; PROCEDURE: ; The approximate record length specified as input does not include record ; terminators (such as CR or CR+LF combinations). ; If the approximate record length specified is L and the range is R then ; for all values i=[L-R,L+R] the procedures checks whether i+1 or i+2 are factors ; of the file size. The largest i that satisfies this criterion is used to set ; the number of records in the file. The record length returned is the ratio of ; file size and number of records (i.e. it includes the record terminators). ; MODIFICATION HISTORY: ; MAR-2000, Paul Hick (UCSD/CASS) ; SEP-2003, Paul Hick (UCSD/CASS) ; Fixed order in which record lengths are tested. ; approx_recl is tested first. ; Then record lenghts increasingly different from approx_recl: ; approx_recl-1, approx_recl+1, approx_recl-2, approx_recl+2, etc. ; APR-2008, Paul Hick (UCSD/CASS; pphick@ucsd.edu) ; For an empty file now nrec=recl=0 is returned, ; instead of nrec=recl=-1. ;- InitVar, range, 1 isize = (fstat(iu)).size CASE isize GT 0 OF 0: BEGIN nrec = 0L recl = 0 END 1: BEGIN nrec = -1L recl = -1 FOR n=0,range DO BEGIN FOR m=-1,1,2 DO BEGIN i = approx_recl+m*n nrec1 = isize/(i+1) nrec2 = isize/(i+2) CASE isize OF nrec1*(i+1): nrec = nrec1 ; File written by Linux nrec2*(i+2): nrec = nrec2 ; File written by VMS else : ENDCASE IF nrec NE -1 THEN break ENDFOR IF nrec NE -1 THEN break ENDFOR IF nrec NE -1 THEN recl = isize/nrec END ENDCASE RETURN, nrec & END