;+ ; NAME: ; READSTRUC ; PURPOSE: ; Read ASCII data into a structure array of known format ; ; CALLING SEQUENCE: ; Result = READSTRUC(filename, struc, format=format) ; ; INPUTS: ; filename = scalar string containing the full path to the ASCII file. ; ; struc = scalar structure defining the structure tags and data field ; formats ; ; OPTIONAL INPUT KEYWORDS: ; format = format code describing each line of the data file. This is ; Required if structure contains strings (otherwise sting length ; is effectively undefined). ; ; skip = integer number of lines to skip at start of data file. ; ; OUTPUT: ; Result = vector structure of length equal to the number of lines ; in the ASCII file. ; ; PROCEDURES USED: ; NUMLINES() ; ; MODIFICATION HISTORY: ; Original version writen 03/2007 by A. Bouchez, Caltech Opt. Obs. ;- FUNCTION READSTRUC, filename, struc, format=format, skip=skip if not KEYWORD_SET(skip) then skip = 0 ;;; Check inputs info = FILE_INFO(filename) if info.exists eq 0 then begin MESSAGE, /info, "File '" + filename + "' does not exist!" RETURN, -1 endif if SIZE(struc, /type) ne 8 then begin MESSAGE, /info, "Second argument must a structure!" RETURN, -1 endif ;;; Create vector structure nl = NUMLINES(filename) - skip vec_struc = REPLICATE(struc, nl) ;;; Open file OPENR, lun, filename, /get_lun ;;; Skip any header lines foo = '' for i=0,skip-1 do READF, lun, foo ;;; Read data if KEYWORD_SET(format) then begin tmp = struc for i=0,nl-1 do begin READF, lun, tmp, f=format vec_struc[i] = tmp endfor endif else $ READF, lun, vec_struc FREE_LUN, lun RETURN, vec_struc END