PRO rdcols,file,length,rowvec0,rowvec1,rowvec2,rowvec3,rowvec4 $ ,rowvec5,rowvec6,rowvec7,rowvec8,rowvec9 ;+ ; NAME: ; RDCOLS ; ; PURPOSE: ; Reading general ASCII files of multicolumn tabulated data, ; with or without a numeric "header line" specifying the number ; of rows and columns in the file. ; ; CATEGORY: ; ; CALLING SEQUENCE: ; RDCOLS, FILENAME, N, COL1, COL2 [, COL3 [, COL4 ...[, COL9]...]] ; ; INPUTS: ; FILENAME: An unambiguous path to the file to be read. ; ; KEYWORD PARAMETERS: ; None. ; ; OUTPUTS: ; N: The number of rows of data found in the file, ; excluding a possible numeric "header line." ; COL1: A FLOAT row vector containing the elements of the ; first column of the file, excluding the first ; element of the numeric "header line" if one was ; found. ; COL2: A FLOAT row vector containing the elements of the ; second column of the file, excluding the second ; element of the numeric "header line" if one was ; found. ; COL3: As above for the third column of the file, if one ; is present. ; COL4: As above for the fourth column of the file, if one ; is present. ; .... ; COL9: As above for the ninth column of the file, if one ; is present. ; ; Note that all arguments beyond COL2 are optional. If fewer ; COL# variables are specified than the number of columns ; contained in the file, the file columns for which variables ; are not specified will not be returned. If more COL# variables ; are specified than the number of columns contained in the ; file, the variables for which columns are not found will be ; returned UNDEFINED. ; ; COMMON BLOCKS: ; None. ; ; SIDE EFFECTS: ; None. ; ; RESTRICTIONS: ; Excluding a possible numeric "header line", all rows in ; the data file must have the same number of entries. All ; entries must be numeric; strings will cause type conversion ; errors. ; ; PROCEDURE: ; The number of carriage returns (ASCII 10B characters) in ; the file is read using the NLINES function. If the file ; is of non-zero length, the first line is read and the number ; of words is determined. If two words are found, it is ; possible a numeric "header line", so each entry is compared ; with the total number of lines in the file returned by ; NLINES. If a match is found, the header line is used to ; dimension the FLOAT array into which the file will be read, ; otherwise the number of words in the first line, and the ; number of lines found, are used. The file is opened, read ; into the FLOAT array, and closed. The array is transposed ; to convert columns into rows, and vectors corresponding to ; the individual rows in the array are created until the ; array is exhausted. ; ; MODIFICATION HISTORY: ; Written 1992(?) by DPS. ; Modified 1993/5/12 by DPS to handle data files with more ; than 2 entries per line. If extra entries exist, they are ; returned in rowvec3, 4, and 5, otherwise these vectors are ; returned full of zeroes. ; Modified 1994/4/22 by DPS to increase the total number of ; row vectors that can be returned to 10. They are now ; numbered rowvec0 ... rowvec9. ; ;- r=nlines(file) IF r EQ 0 THEN BEGIN MESSAGE,'File contains no data',/INFORMATIONAL length=0 rowvec0=[0] rowvec1=[0] RETURN ENDIF rdfile,file,1,1,s s=s(0) c=nwrds(s) CASE c OF 1: BEGIN p0=FLOAT(getwrd(s,0)) IF p0 NE r-1 THEN MESSAGE,"Can't interpret first line of file" rdfile,file,2,2,s s=s(0) p1=nwrds(s) dimfirst=1B END 2: BEGIN READS,s,p0,p1 dimfirst=((p0 EQ r-1) OR (p1 EQ r-1)) END ELSE: dimfirst=0B ENDCASE OPENR,unit,file,/GET_LUN IF dimfirst THEN BEGIN r=r-1 IF r EQ p0 THEN c=p1 ELSE c=p0 READF,unit,s ; skip the line containing the dimensions ENDIF d=FLTARR(c,r) READF,unit,d CLOSE,unit FREE_LUN,unit d=TRANSPOSE(d) rowvec0=d(*,0) & rowvec1=d(*,1) length=r IF c GE 3 THEN rowvec2=d(*,2) IF c GE 4 THEN rowvec3=d(*,3) IF c GE 5 THEN rowvec4=d(*,4) IF c GE 6 THEN rowvec5=d(*,5) IF c GE 7 THEN rowvec6=d(*,6) IF c GE 8 THEN rowvec7=d(*,7) IF c GE 9 THEN rowvec8=d(*,8) IF c GE 10 THEN rowvec9=d(*,9) RETURN END