readf,1,format= ...... - Idl-pvwave
This is a discussion on readf,1,format= ...... - Idl-pvwave ; hi
i have a question
i have in a txt file some data
48883 149.55579541 12.44483936
.... .... ....
when i read a entire line
line=''
readf,1,line
print,line
i get a good output
48883 149.55579541 12.44483936
but when i read ...
-
readf,1,format= ......
hi
i have a question
i have in a txt file some data
48883 149.55579541 12.44483936
.... .... ....
when i read a entire line
line=''
readf,1,line
print,line
i get a good output
48883 149.55579541 12.44483936
but when i read data like this
ttab = MAKE_ARRAY(nlines,3,/DOUBLE)
....
FMTp='(I5,2X,D13.8,2X,D13.8)'
for i=1,nlines do begin
READF,1,FORMAT=FMT,l1,l2,l3
ttab[i-1,0]=l1 & ttab[i-1,1]=l2 & ttab[i-1,2]=l3
endfor
and i print them
print,FORMAT=FMTp,ttab[0,0], ttab[0,1],ttab[0,2]
i get
48883 149.55580139 12.44483948
and this is not the same it should be
48883 149.55579541 12.44483936
why ?
i use idl 6.1 6.2
Wojtek
-
Re: readf,1,format= ......
woopik@interia.pl writes:
> i have a question
> i have in a txt file some data
> 48883 149.55579541 12.44483936
> .... .... ....
> when i read a entire line
> line=''
> readf,1,line
> print,line
> i get a good output
> 48883 149.55579541 12.44483936
>
> but when i read data like this
> ttab = MAKE_ARRAY(nlines,3,/DOUBLE)
> ...
> FMTp='(I5,2X,D13.8,2X,D13.8)'
> for i=1,nlines do begin
> READF,1,FORMAT=FMT,l1,l2,l3
> ttab[i-1,0]=l1 & ttab[i-1,1]=l2 & ttab[i-1,2]=l3
> endfor
> and i print them
> print,FORMAT=FMTp,ttab[0,0], ttab[0,1],ttab[0,2]
> i get
> 48883 149.55580139 12.44483948
> and this is not the same it should be
> 48883 149.55579541 12.44483936
>
> why ?
Well, if this code is correct, I would say because you didn't
use the same format on your READF command as you did on your
PRINT command. So, I think the numbers were read in as floats,
not doubles. If I use the same format both places, no worries. :-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
-
Re: readf,1,format= ......
> I would say because you didn't use the same format on your
> READF command as you did on your PRINT command.
i dont understand your tip 
i am using the same FMTp format for both
Wojtek
-
-
Re: readf,1,format= ......
sorry a type error but nothing change when i use FMTp for both
(FMT is quite similar)
function read_local ,file
FMT=
'(I5,2X,D13.8,2X,D13.8,3X,F8.2,3X,F8.2,3X,F7.2,3X,F7.2,3X,F6.2,3X,F6.2,3X,F6.2,3X,F6.2,4X,F6.2,3X,F6.2,3X,F6.2,2X,F6.2)'
spawn, ['wc', '-l', file], result, /noshell
nlines = long(result(0))
ttab = MAKE_ARRAY(nlines-5,15,/DOUBLE)
OPENR,1,file
line=''
readf,1,line
for i=1,nlines-5 do begin
READF,1,FORMAT=FMT, n,ra,dec, l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12
ttab[i-1,0]=n & ttab[i-1,1]=ra & ttab[i-1,2]=dec
ttab[i-1,3]=l1 & ttab[i-1,4]=l2 & ttab[i-1,5]=l3
ttab[i-1,6]=l4 & ttab[i-1,7]=l5 & ttab[i-1,8]=l6
ttab[i-1,9]=l7 & ttab[i-1,10]=l8 & ttab[i-1,11]=l9
ttab[i-1,12]=l10 & ttab[i-1,13]=l11 & ttab[i-1,14]=l12
endfor
CLOSE , 1
print,FORMAT=FMT,ttab[0,*]
RETURN,ttab
end
-
Re: readf,1,format= ......
On Dec 9, 8:50 am, woo...@interia.pl wrote:
> sorry a type error but nothing change when i use FMTp for both
> (FMT is quite similar)
>
> function read_local ,file
> FMT=
> '(I5,2X,D13.8,2X,D13.8,3X,F8.2,3X,F8.2,3X,F7.2,3X,F7.2,3X,F6.2,3X,F6.2,3X,F6.2,3X,F6.2,4X,F6.2,3X,F6.2,3X,F6.2,2X,F6.2)'
> spawn, ['wc', '-l', file], result, /noshell
> nlines = long(result(0))
> ttab = MAKE_ARRAY(nlines-5,15,/DOUBLE)
> OPENR,1,file
> line=''
> readf,1,line
> for i=1,nlines-5 do begin
> READF,1,FORMAT=FMT, n,ra,dec, l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12
> ttab[i-1,0]=n & ttab[i-1,1]=ra & ttab[i-1,2]=dec
> ttab[i-1,3]=l1 & ttab[i-1,4]=l2 & ttab[i-1,5]=l3
> ttab[i-1,6]=l4 & ttab[i-1,7]=l5 & ttab[i-1,8]=l6
> ttab[i-1,9]=l7 & ttab[i-1,10]=l8 & ttab[i-1,11]=l9
> ttab[i-1,12]=l10 & ttab[i-1,13]=l11 & ttab[i-1,14]=l12
> endfor
> CLOSE , 1
> print,FORMAT=FMT,ttab[0,*]
> RETURN,ttab
> end
1) I would avoid using l (el) in variable names - hard to read
2) I would use file_lines to count the number of lines
3) I would define a structure and read each line in separately:
ttab = replicate({n:0,ra:0.0D,dec:0.0D,array:fltarr(12)},nlines)
for i=0L, nlines-5 do readf, lun, ttab[i]
Hope this helps!
-
Re: readf,1,format= ......
woopik@interia.pl writes:
> sorry a type error but nothing change when i use FMTp for both
> (FMT is quite similar)
>
> function read_local ,file
> FMT=
> '(I5,2X,D13.8,2X,D13.8,3X,F8.2,3X,F8.2,3X,F7.2,3X,F7.2,3X,F6.2,3X,F6.2,3X,F6.2,3X,F6.2,4X,F6.2,3X,F6.2,3X,F6.2,2X,F6.2)'
> spawn, ['wc', '-l', file], result, /noshell
> nlines = long(result(0))
> ttab = MAKE_ARRAY(nlines-5,15,/DOUBLE)
> OPENR,1,file
> line=''
> readf,1,line
> for i=1,nlines-5 do begin
> READF,1,FORMAT=FMT, n,ra,dec, l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12
> ttab[i-1,0]=n & ttab[i-1,1]=ra & ttab[i-1,2]=dec
> ttab[i-1,3]=l1 & ttab[i-1,4]=l2 & ttab[i-1,5]=l3
> ttab[i-1,6]=l4 & ttab[i-1,7]=l5 & ttab[i-1,8]=l6
> ttab[i-1,9]=l7 & ttab[i-1,10]=l8 & ttab[i-1,11]=l9
> ttab[i-1,12]=l10 & ttab[i-1,13]=l11 & ttab[i-1,14]=l12
> endfor
> CLOSE , 1
> print,FORMAT=FMT,ttab[0,*]
> RETURN,ttab
> end
Well, this is most interesting. There is a mismatch between
your variables (n, ra, dec, etc.) and your FORMAT statement.
In other words, ra and dec, since they are undeclared in the
program, are by default floats. Your format statement is going
to try to read them as doubles. The "rules" of input state that
IDL "tries to do what you want to do". Here, it thinks what you
are trying to do, is stuff a double into a float, and it does
this correctly. The variable ttab is a double, but it has been
stuffed with a floating value (from ra and dec).
The solution is to declare the variables on your READF
statement to be what you want to read into. That is, doubles.
However, I completely agree with Vince that what you REALLY
want to do is read this data in another way, rather than
line by line, practically the worst way to read data in IDL
(ala READ_ASCII). I think I would take his structure advice.
That will also solve your problem here.
Cheers,
David
P.S. Here is the program I used to test your program with the
data you sent yesterday:
function read_local ,file
FMT='(I5,2X,D13.8,2X,D13.8)'
nlines = File_lines(file)
ttab = MAKE_ARRAY(3,nlines,/DOUBLE)
n = 0L
ra = 0.0D0
dec = 0.0D0
OPENR,1,file
for I=0,nlines-1 do begin
READF,1,FORMAT=FMT, n,ra,dec
ttab[0,I]=n & ttab[1,I]=ra & ttab[2,I]=dec
endfor
CLOSE , 1
print,FORMAT=FMT,ttab
RETURN,ttab
end
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
-
Re: readf,1,format= ......
strange but true , now its works
thx a lot 
Wojtek
> In other words, ra and dec, since they are undeclared in the
> program, are by default floats.
> n = 0L
> ra = 0.0D0
> dec = 0.0D0
-
Re: readf,1,format= ......
bu if this works fine
--> ra = 0.0D0
i have second question
what i should do when i have in file
variables in a scientific notation
and i want read and store them
in array
for ex 5.4195140971e-04 ?
Wojtek
-
Re: readf,1,format= ......
woopik@interia.pl writes:
> bu if this works fine
> --> ra = 0.0D0
> i have second question
>
> what i should do when i have in file
> variables in a scientific notation
> and i want read and store them
> in array
> for ex 5.4195140971e-04 ?
>
>
IDL won't be confused by this. :-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Similar Threads
-
By Application Development in forum Idl-pvwave
Replies: 8
Last Post: 08-21-2007, 03:14 AM
-
By Application Development in forum Idl-pvwave
Replies: 5
Last Post: 04-10-2007, 12:30 PM
-
By Application Development in forum XML SOAP
Replies: 0
Last Post: 06-11-2006, 01:21 PM
-
By Application Development in forum XML SOAP
Replies: 0
Last Post: 06-09-2006, 07:58 AM
-
By Application Development in forum CSharp
Replies: 0
Last Post: 06-08-2006, 05:25 PM