[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Writing formatted text files



Liam Gumley wrote:
> 
> Bernard Puc <bpuc@va.aetc.com> wrote in message
> 38A47BCA.884D0CC@va.aetc.com">news:38A47BCA.884D0CC@va.aetc.com...
> > I'd like to speed-up the following code.  I'm writing comma delimited
> > text files, I'm eliminating whitespace by using strtrim, and am
> > explicitly formatting the string using a format keyword.  Any ideas how
> > to improve this?  I've been playing with various ways of formatting
> > without using a FOR loop but either don't get the desired output or the
> > error message telling me the output string has been truncated to 1024
> > lines.
> >
> > data_to_write = fltarr(3, 100000)
> > FOR j=0, 99999 DO BEGIN
> >   PRINTF,Lun,$
> >     strtrim(string(data_to_write[0,j],format='(f10.3)'),2),',',$
> >     strtrim(string(data_to_write[1,j],format='(f10.3)'),2),',',$
> >     strtrim(string(data_to_write[2,j],format='(f10.3)'),2),$
> >     FORMAT = "(a,a,a,a,a)"
> > ENDFOR
> >
> > Output should look like this:
> >
> > 278.980,-232.018,258.090
> > 278.926,-231.974,258.360
> > 278.889,-231.944,241.240
> > 278.852,-231.913,206.900
> > 278.814,-231.883,156.390
> > 278.776,-231.852,93.680
> > 278.739,-231.821,27.799
> > 278.702,-231.791,-33.771
> > 278.664,-231.760,-84.551
> > 278.626,-231.729,-121.161
> 
> This doesn't get rid of all the whitespace, but it's simple and quick, and
> the output is easy to read:
> 
> arr = findgen(3, 1000) * 0.1
> print, arr, format='(2(f8.3, ","),f8.3)'
> 

Try

print,FORMAT='(2(f0, ","),f0)',arr

It eliminates the whitespace, although you can't control the precision generated
(6 digits after the decimal, by default).  This "natural width" format isn't
documented.

JD

-- 
 J.D. Smith                             |*|      WORK: (607) 255-5842    
 Cornell University Dept. of Astronomy  |*|            (607) 255-6263
 304 Space Sciences Bldg.               |*|       FAX: (607) 255-5875 
 Ithaca, NY 14853                       |*|