; ; generic file reading routine for general data files ; that were written by write_dat.pro ; ; Tom Woods ; ; Version 1.0 1994 Initial procedure ; 2.0 1996 Updated to handle array of structures ; ; INPUT: filename ; ; OUTPUT: data from the file (array of data or array of structure) ; function read_dat, filename, status=status status = -1 data = -1 if n_params(0) lt 1 then begin filename = ' ' read, 'Enter filename to read ? ', filename if filename eq '' then return, data endif get_lun, lun openit = 0 on_ioerror, openerror openr,lun, filename numbers = '0123456789' ; ; read header comment lines first ; also check for STRUCTURE and FORMAT keywords too ; on_ioerror, readerror openit = 1 incomments = 1 sinput = '' form = '' struct = '' while incomments do begin readf,lun,sinput sinput = strtrim(sinput,1) sinputcaps = strupcase(sinput) if strpos( sinputcaps, 'STRUCT' ) eq 0 then begin startpos = strpos( sinputcaps, '{' ) endpos = strpos( sinputcaps, '}' ) if (startpos lt 0) or (endpos lt 0) or $ (endpos lt startpos) then begin print, 'read_dat: Error in STRUCT keyword definition !' endif else begin struct = strmid( sinput, startpos, endpos-startpos+1 ) endelse endif if strpos( sinputcaps, 'FORMAT' ) eq 0 then begin startpos = strpos( sinputcaps, '(' ) endpos = strpos( sinputcaps, ')' ) if (startpos lt 0) or (endpos lt 0) or $ (endpos lt startpos) then begin print, 'read_dat: Error in FORMAT keyword definition !' endif else begin form = strmid( sinput, startpos, endpos-startpos+1 ) endelse endif if strpos( numbers, strmid(sinput,0,1) ) ge 0 then $ incomments = 0 endwhile ; ; read number of lines ; nlines = fix( sinput ) n = strlen(sinput) k = 1 innumber = 1 numberstr = '0123456789 /.,<>?;:\|=+-_)(*&^%$#@!~' while innumber do begin if strpos( numberstr, strmid(sinput,k,1) ) lt 0 then begin if k lt n-1 then print, ' ', filename, ' : ', strmid(sinput,k,n-k) innumber = 0 endif else begin k = k + 1 if k ge n-2 then innumber = 0 endelse endwhile ; ; read number of columns ; readf,lun,sinput sinput = strtrim(sinput,1) ncolumns = fix( sinput ) n = strlen(sinput) k = 1 innumber = 1 while innumber do begin if strpos( numberstr, strmid(sinput,k,1) ) lt 0 then begin if k lt n-1 then print, ' ', filename, ' : ', strmid(sinput,k,n-k) innumber = 0 endif else begin k = k + 1 if k ge n-2 then innumber = 0 endelse endwhile form = '$' + form ; ; read array of numbers if structure is not defined ; if strlen(struct) le 2 then begin data = dblarr(ncolumns, nlines) ; ; read data (using format if it exists) ; if strlen(form) le 2 then readf,lun,data else $ readf,lun,form,data status = 0 goto, cleanup endif else begin ; ; read array of structure ; acmd = execute( 'temp = ' + struct ) data = replicate( temp, nlines ) ; ; read data (using format if it exists) ; if strlen(form) le 2 then readf,lun,data else $ readf,lun,form,data ; ; clean up any string data ; ntags = n_tags(data) tnames = tag_names(data) for i=0,ntags-1 do begin asize = size( data.(i) ) if asize(asize(0)+1) eq 7 then begin print, 'read_dat: Compressing strings for DATA.' + tnames(i) data.(i) = strtrim( data.(i), 2 ) endif endfor status = 0 goto, cleanup endelse openerror: print, 'ERROR: READ_DAT() could not open ' + filename goto,cleanup readerror: print, 'ERROR: READ_DAT() had a read error for ' + filename cleanup: print, ' ' on_ioerror, NULL if openit then close,lun free_lun, lun return, data end