Re: How to read data with format codes? [message #70071 is a reply to message #70070] |
Sat, 13 March 2010 17:04   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Adam Solomon writes:
> Hi all, I've been pulling my hair out over using readf with a format
> code. So I have a data file with three columns of floats, all
> separated by a |. Here's an example:
>
> | 355.9559189095| -79.4625574877| 0.279817 |
> | 355.5057979042| -79.4067330132| 0.189663 |
> | 359.5215546187| -79.7976864503| 0.161308 |
> | 357.7890741750| -79.6427829568| 0.026100 |
> | 354.7163524970| -79.3099884939| 0.026578 |
> | 354.6329916471| -79.3061286761| 0.025486 |
> | 0.0561720211 | -79.8234642170| 0.025670 |
> | 357.3786228505| -79.5728335169| 0.000030 |
> | 356.7569554568| -79.5138546776| 0.199416 |
> | 355.0388858964| -79.3264538270| 0.190393 |
> | 354.8099356150| -79.2912157538| 0.026006 |
> | 352.5968356937| -79.0184276231| 0.207497 |
> | 352.4362834602| -79.0094360366| 0.217226 |
> | 0.1800716026 | -79.8081762850| 0.091767 |
> | 359.0525340400| -79.7227001135| 0.205676 |
> | 354.7045386126| -79.2652363648| 0.119250 |
>
> So every format code I've used which has even come close to working
> runs into issues at one point or another, probably because of the
> variable spacing. I want to read this file into three arrays, or a
> 3xlots array, or whatever - just any way to read in this data!! It's
> unbelievable that such a simple task is proving to be so difficult.
>
> I won't bore you with all the format codes I've tried (none of which
> have worked) but here's the most recent example:
>
> data=fltarr(3,rows-100);the 100 is so i don't run into EOF issues yet,
> just for testing
> readf,lun,data,format='(3(2x,F0.0))'
>
> The problems with this particular one are that when the first column
> is longer (as in most of these examples), the minus sign doesn't get
> read in the second column, and there may be issues reading the third
> column (not sure; will look into it some more).
>
> What's the appropriate way to read these data in?
If you really want a format code, I'm not your man.
They always totally confuse me. :-)
I would read this "irregular" file like this.
rows = File_Lines('example.dat')
data = DblArr(3,rows)
oneLine = ""
OpenR, lun, 'example.dat', /GET_LUN
FOR j=0,rows-1 DO BEGIN
ReadF, lun, oneLine
parts = StrSplit(oneLine, '|', /EXTRACT)
data[*,j] = Double(parts[1:3])
ENDFOR
Free_Lun, lun
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.")
|
|
|