; Read an array from a file. ; The file is assumed to be columns ("cols" of them) and the game is ; to find how many rows there are ; If /fbin, assume fortran binary ; /cont - if io error encountered, return first line of file rather than ; crashing out ; /cutto - only applicable to ascii files; pass through "cut -b1-" function readfromfile,filename1,cols=cols,remove=remove,cont=cont,fbin=fbin,lines1=lines1,bin=bin,noisy=noisy,ignore=ignore,cutto=cutto @comm_error if (not keyword_set(ignore)) then ignore=0 on_ioerror, weasel ; File may be http:// - omit check in this case if (strpos(filename1(0),"http://") eq 0) then begin spawn,'echo /tmp/wmc-readhttpfromfile-$$',filename & filename=filename(0) spawn,'perl -MLWP::Simple -e "getprint '''+filename1(0)+'''" > '+filename endif else begin ; Check we have only 1 file if (n_elements(filename1) ne 1) then message,'error - want one string for filename - got: '+shtstr(n_elements(filename)) ; Expand the name with findfile filename=findfile(filename1) ; Check again! if (n_elements(filename) ne 1) then message,'error - want one string for filename - got: '+shtstr(n_elements(filename)) filename=filename(0) if (strlen(filename) eq 0) then begin message,'file ('+filename1+') does not exist',cont=keyword_Set(cont) return,-1 endif endelse ; ; Fortran binary (/fbin) ; if (keyword_set(fbin)) then begin words=0l openr,lun,/get,filename readu,lun,words data=fltarr(words/4) readu,lun,data endif else begin lines=0l; words=0l; ; ; Binary (/bin) ; if (keyword_Set(bin)) then begin spawn,'wc -c '+filename,text ; spawn,['wc','-c',filename],text,/noshell reads,text,words words=words/4 lines=words ; ; Text otherwise ; endif else begin if (keyword_set(cutto)) then begin spawn,'echo /tmp/wmc-readfromfile-$$',file & file=file(0) spawn,'cut -b1-'+shtstr(cutto)+' '+filename+' > '+file endif else $ file=filename spawn,'wc -wl '+file,text reads,text,lines,words if (words lt lines) then begin junk=lines lines=words words=junk endif endelse if (keyword_set(lines1)) then lines=lines1 if (lines eq 0) then begin message,'File '+filename+' is empty',cont=keyword_Set(cont) return,-1 endif if (keyword_set(noisy)) then print,'Opening '+filename+' (file) '+' to read '+shtstr(words)+ ' words' openr,lun,file,/get if (keyword_Set(bin)) then begin data=fltarr(words) readu,lun,data endif else begin if (not keyword_Set(cols)) then cols=words/lines if (ignore eq 0) then lines=words/cols data=fltarr(cols,lines-ignore) junk=string(0) for i=0,ignore-1 do begin readf,lun,junk if (keyword_set(noisy)) then print,'Ignoring: ',junk endfor readf,lun,data endelse endelse free_lun,lun if (keyword_Set(remove)) then spawn,'rm '+filename if (keyword_set(cutto)) then spawn,'rm '+file return,data weasel: ; print,!error_state rewind,lun a=string(1) & readf,lun,a & free_lun,lun if (keyword_set(cont)) then begin return,a endif else begin help,data print,!error_state message,'Error reading array from file - is it data?',/cont print,'First line in file:' print,a return,data endelse end