comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Read an ASCII file in a table based on delimeter line string
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Read an ASCII file in a table based on delimeter line string [message #93455] Thu, 21 July 2016 08:36 Go to next message
burkina is currently offline  burkina
Messages: 32
Registered: February 2005
Member
Hi everyone,

suppose I have a file like this one (there will be no regularity in the real ones!):

a 1
b 2
c 3
####
d 4
e 5
f 6
g 7
####
h 8
####

I want to read in IDL in separated arrays:

x[0]=[a,b,c]
x[1]=[d,e,f,g]
x[2]=[h]

y[0]=[1,2,3]
y[1]=[4,5,6,7]
y[2]=[8]

or a table t[2,8], or something equivalent.
In practice, I would like to use something like readcol but with IDL understanding the reading should stop every time it encounters a delimiter (#### in this case), and then start again separating the arrays for each set.

How can I do that?

Thanks,

Stefano
Re: Read an ASCII file in a table based on delimeter line string [message #93457 is a reply to message #93455] Thu, 21 July 2016 11:17 Go to previous messageGo to next message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
On Thursday, July 21, 2016 at 11:36:56 AM UTC-4, burkina wrote:
> Hi everyone,
>
> suppose I have a file like this one (there will be no regularity in the real ones!):
...
> I want to read in IDL in separated arrays:
>
> x[0]=[a,b,c]
> x[1]=[d,e,f,g]
> x[2]=[h]
>
> y[0]=[1,2,3]
> y[1]=[4,5,6,7]
> y[2]=[8]
>
> or a table t[2,8], or something equivalent.
> In practice, I would like to use something like readcol but with IDL understanding the reading should stop every time it encounters a delimiter (#### in this case), and then start again separating the arrays for each set.

It's not going to be really easy, or efficient. It's not hard for someone who has programming experience, though.

Step one is deciding on what kind of data structure you want. The IDL arrays you selected do not support varying dimensions so you can't use that type of data structure. IDL lists might be a better approach, if you want to keep similar semantics.

Step two is deciding how to parse the data from an open file unit. Think of how you would do that as a human. The following script may help. I didn't test it in any way, so who knows if it will work.

openr, unit, 'myfile.dat', /get_lun
output = [] ;; Initialize output
while eof(unit) EQ 0 do begin
;; Initialize X and Y to empty arrays
x = []
y = []
s = '' ;; Initialize dummy string
readf, unit, s ;; Read one line from input file

;; s is now one line from the input file

;; Begin parsing
if s EQ '###' then begin ;; It is a delimiter
output = [output, {x: x, y: y}

endif else begin ;; Or else parse it into data
x1 = '' & y1 = 0L ;; Initialize dummy variables

;; NOTE: this will change based on the actual format of your data file
reads, s, x1, y1, format='(A1,I)'

;; Append to existing list
x = [x, x1]
y = [y, y1]
endelse

endwhile
free_lun, unit
Re: Read an ASCII file in a table based on delimeter line string [message #93461 is a reply to message #93457] Fri, 22 July 2016 03:09 Go to previous message
burkina is currently offline  burkina
Messages: 32
Registered: February 2005
Member
On Thursday, 21 July 2016 20:17:50 UTC+2, Craig Markwardt wrote:
> On Thursday, July 21, 2016 at 11:36:56 AM UTC-4, burkina wrote:
>> Hi everyone,
>>
>> suppose I have a file like this one (there will be no regularity in the real ones!):
> ...
>> I want to read in IDL in separated arrays:
>>
>> x[0]=[a,b,c]
>> x[1]=[d,e,f,g]
>> x[2]=[h]
>>
>> y[0]=[1,2,3]
>> y[1]=[4,5,6,7]
>> y[2]=[8]
>>
>> or a table t[2,8], or something equivalent.
>> In practice, I would like to use something like readcol but with IDL understanding the reading should stop every time it encounters a delimiter (#### in this case), and then start again separating the arrays for each set.
>
> It's not going to be really easy, or efficient. It's not hard for someone who has programming experience, though.
>
> Step one is deciding on what kind of data structure you want. The IDL arrays you selected do not support varying dimensions so you can't use that type of data structure. IDL lists might be a better approach, if you want to keep similar semantics.
>
> Step two is deciding how to parse the data from an open file unit. Think of how you would do that as a human. The following script may help. I didn't test it in any way, so who knows if it will work.
>
> openr, unit, 'myfile.dat', /get_lun
> output = [] ;; Initialize output
> while eof(unit) EQ 0 do begin
> ;; Initialize X and Y to empty arrays
> x = []
> y = []
> s = '' ;; Initialize dummy string
> readf, unit, s ;; Read one line from input file
>
> ;; s is now one line from the input file
>
> ;; Begin parsing
> if s EQ '###' then begin ;; It is a delimiter
> output = [output, {x: x, y: y}
>
> endif else begin ;; Or else parse it into data
> x1 = '' & y1 = 0L ;; Initialize dummy variables
>
> ;; NOTE: this will change based on the actual format of your data file
> reads, s, x1, y1, format='(A1,I)'
>
> ;; Append to existing list
> x = [x, x1]
> y = [y, y1]
> endelse
>
> endwhile
> free_lun, unit

Great!
The key information was the suggestion to use IDL lists.
I modified your script, introducing lists for x and y, and now I have exactly what I wanted: each listx element can be easily plotted against its respective listy element, without knowing anything a priori on the array lengths.

Thanks!

Stefano
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Poltting with TVSCL
Next Topic: File units (de)allocation in IDL - what is the proper way?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 11:37:50 PDT 2025

Total time taken to generate the page: 0.00444 seconds