Re: how read a file of complex? [message #10630 is a reply to message #10628] |
Fri, 02 January 1998 00:00  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
Nando Iavarone (iavarone@axpba0.ba.infn.it) provides the
first interesting problem of the New Year when he writes:
> I have a data-file containing a matrix. Each element of matrix has the
> form(real part, imaginary part)[1] with each one LONG.
>
> 1) How create a matrix with structure [1] to read in a single pass the
> file?
> [Slow code clipped.]
For an m-by-n matrix of real-imaginary pairs of long integers,
how about something like this:
FUNCTION Read_It, filename, m, n
; Read the data all at once.
data = LonArr(m*n*2)
OpenR, lun, filename, /Get_Lun
ReadU, lun, data
Free_Lun, data
; Reform to align real data in column next to imaginary data.
colData = Reform(data, 2, m*n)
; Create vectors of real and imaginary values.
real = Reform(colData[0,*])
imag = Reform(colData[1,*])
; Make complex numbers out of real and imaginary parts.
complexData = Complex(real, imag)
; Reformat the complex data into a matrix and return.
RETURN, Reform(complexData, m, n)
END
Call this function like this:
myData = Read_It('mydata.dat', 300, 500)
I've taken more steps then necessary here (as someone is
sure to point out), but I tried to give you the sense of
how this might be done by taking advantage of IDL's array
operations instead of loops.
Cheers,
David
-----------------------------------------------------------
David Fanning, Ph.D.
Fanning Software Consulting
E-Mail: davidf@dfanning.com
Phone: 970-221-0438
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|