Reading complicated ASCII data [message #71527] |
Tue, 29 June 2010 06:05  |
Tone M R
Messages: 2 Registered: June 2010
|
Junior Member |
|
|
Hi!
I've been racking my brains and the web for the best part of a day,
but have not managed to find anything useful to solve my problem,
which is this:
I've got an automatically generated .txt file of rainfall measurements
which I need to read. I'm having trouble with the format of the file,
which looks more or less like this:
------------------------------------------------------------ -----
[block of not-so-interesting information]
Date Jan Feb Mar Apr May Jun Jul Aug Sep
Oct Nov Dec
1 0.5 1.4 . 4.7 . .
0.1 . . . .
2 0.6 0.3 3.9 . . . . .
4.0 . .
3 5.8 1.6 4.9 0.1 3.1 3.4 4.4 0.2 0.9
1.4 .
4 2.0 5.1 1.9 0.2 0.5 6.7 3.3 . 1.1
0.1 .
5 6.8 0.6 9.7 . 2.7 0.8 1.6 2.4
0.7 . .
... and so forth, for an entire year. - a 13x31 table of floats.
[new block of non-helpful stuff]
[new block of data for another year]
------------------------------------------------------------ ------------
etc..., for a total of ten years.
The table of figures is actually in straight columns, a column per
month, with a dot wherever a measurement is zero. (There are also
blank spaces at the bottom of each table, for dates such as feb 30th.)
I've managed to work around the headers and identify where a table
starts, and what I wanted to do was to read the entire thing into a
nice structure array I've prepared. However, when using READF, IDL
stops when trying to convert a dot to a float (understandably), and I
haven't managed to solve it with a format code. I have thought about
using STRSPLIT and WHERE to replace them, but then I have to go one
line at a time, and I was rather hoping to make something a little
more elegant.
Does anyone see a way around these dots?
|
|
|
Re: Reading complicated ASCII data [message #71600 is a reply to message #71527] |
Thu, 01 July 2010 01:35  |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
Tone M R schrieb:
> Hi!
>
> I've been racking my brains and the web for the best part of a day,
> but have not managed to find anything useful to solve my problem,
> which is this:
>
> I've got an automatically generated .txt file of rainfall measurements
> which I need to read. I'm having trouble with the format of the file,
> which looks more or less like this:
> ------------------------------------------------------------ -----
> [block of not-so-interesting information]
>
> Date Jan Feb Mar Apr May Jun Jul Aug Sep
> Oct Nov Dec
> 1 0.5 1.4 . 4.7 . .
> 0.1 . . . .
> 2 0.6 0.3 3.9 . . . . .
> 4.0 . .
> 3 5.8 1.6 4.9 0.1 3.1 3.4 4.4 0.2 0.9
> 1.4 .
> 4 2.0 5.1 1.9 0.2 0.5 6.7 3.3 . 1.1
> 0.1 .
> 5 6.8 0.6 9.7 . 2.7 0.8 1.6 2.4
> 0.7 . .
> ... and so forth, for an entire year. - a 13x31 table of floats.
>
> [new block of non-helpful stuff]
>
> [new block of data for another year]
> ------------------------------------------------------------ ------------
> etc..., for a total of ten years.
>
> The table of figures is actually in straight columns, a column per
> month, with a dot wherever a measurement is zero. (There are also
> blank spaces at the bottom of each table, for dates such as feb 30th.)
> I've managed to work around the headers and identify where a table
> starts, and what I wanted to do was to read the entire thing into a
> nice structure array I've prepared. However, when using READF, IDL
> stops when trying to convert a dot to a float (understandably), and I
> haven't managed to solve it with a format code. I have thought about
> using STRSPLIT and WHERE to replace them, but then I have to go one
> line at a time, and I was rather hoping to make something a little
> more elegant.
>
> Does anyone see a way around these dots?
>
a=read_data_file('data.txt',/vst)
IDL> help,a,/str
** Structure <a26218>, 4 tags, length=768, data length=693, refs=1:
FILE STRING 'data.txt'
SEPARATOR STRING ' '
DATA STRUCT -> <Anonymous> Array[5]
HEADER STRING Array[1]
IDL> help,a.data,/str
** Structure <7f0fd8>, 12 tags, length=144, data length=129, refs=2:
VAR0 BYTE 1
VAR1 FLOAT 0.500000
VAR2 FLOAT 1.40000
VAR3 STRING '.'
VAR4 FLOAT 4.70000
VAR5 STRING '.'
VAR6 STRING '.'
VAR7 FLOAT 0.100000
VAR8 STRING '.'
VAR9 STRING '.'
VAR10 STRING '.'
VAR11 STRING '.'
IDL> print, a.data.var0
1 2 3 4 5
http://www.fz-juelich.de/icg/icg-1/idl_icglib/idl_source/idl _html/dbase/read_data_file_dbase.pro.html
it may need postprocessing if the first entry was a string.
e.g. print, float(a.data.var8)
0.00000 0.00000 0.200000 0.00000 2.40000
cheers
Reimar
|
|
|