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

Home » Public Forums » archive » Re: Extracting variables from ascii files that are not in columnar format
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
Re: Extracting variables from ascii files that are not in columnar format [message #70382] Fri, 09 April 2010 13:31
Gray is currently offline  Gray
Messages: 253
Registered: February 2010
Senior Member
On Apr 9, 3:51 pm, Gray <grayliketheco...@gmail.com> wrote:
> On Apr 9, 3:21 pm, nata <bernat.puigdomen...@gmail.com> wrote:
>
>
>
>
>
>> I'll try something like that
>
>> aux=''
>> OPENR, un$lun, file, /GET_LUN
>
>> WHILE ~EOF(un$lun) DO BEGIN
>
>> READF, un$lun, aux
>
>> aux_str=STRSPLIT(aux,' ',/EXTRACT)
>
>> PRINT, 'label', aux_str[0:1]
>> PRINT, 'values', aux_str[2:*]
>
>> ;; Then you can concatenate, etc...
>
>> ENDWHILE
>
>> FREE_LUN, un$lun
>
> Or, you can read everything at once with file_lines and a string
> array, then loop through the array and strsplit.  If you know that
> every line has the same format, you can use astrolib's gettok to pick
> out the header names.  Printing in columns like that is a little more
> complicated, and requires padding.  Example:
>
> n = file_lines(file)
> data = strarr(n)
> heads = strarr(n)
> openr, unit, file, /get_lin
> readf, unit, data
> close, unit & free_lun, unit
> heads = gettok(data,'=')
> print, heads, format='('+n+'A12)'
> lens = intarr(n)
> for i=0L,n-1 do lens[i] = n_elements(strsplit(data[i],/regex))
> npad = max(lens)
> padded_data = strarr(n,npad)
> for i=0L,n-1 do padded_data[i,0] = strsplit(data[i],/regex)
> print, transpose(padded_data), format='('+n+'A12)'

Oops, forgot /extract in the 2nd strsplit call. Also, you may need to
strtrim n for the format codes.
Re: Extracting variables from ascii files that are not in columnar format [message #70383 is a reply to message #70382] Fri, 09 April 2010 12:51 Go to previous message
Gray is currently offline  Gray
Messages: 253
Registered: February 2010
Senior Member
On Apr 9, 3:21 pm, nata <bernat.puigdomen...@gmail.com> wrote:
> I'll try something like that
>
> aux=''
> OPENR, un$lun, file, /GET_LUN
>
> WHILE ~EOF(un$lun) DO BEGIN
>
> READF, un$lun, aux
>
> aux_str=STRSPLIT(aux,' ',/EXTRACT)
>
> PRINT, 'label', aux_str[0:1]
> PRINT, 'values', aux_str[2:*]
>
> ;; Then you can concatenate, etc...
>
> ENDWHILE
>
> FREE_LUN, un$lun

Or, you can read everything at once with file_lines and a string
array, then loop through the array and strsplit. If you know that
every line has the same format, you can use astrolib's gettok to pick
out the header names. Printing in columns like that is a little more
complicated, and requires padding. Example:

n = file_lines(file)
data = strarr(n)
heads = strarr(n)
openr, unit, file, /get_lin
readf, unit, data
close, unit & free_lun, unit
heads = gettok(data,'=')
print, heads, format='('+n+'A12)'
lens = intarr(n)
for i=0L,n-1 do lens[i] = n_elements(strsplit(data[i],/regex))
npad = max(lens)
padded_data = strarr(n,npad)
for i=0L,n-1 do padded_data[i,0] = strsplit(data[i],/regex)
print, transpose(padded_data), format='('+n+'A12)'
Re: Extracting variables from ascii files that are not in columnar format [message #70384 is a reply to message #70383] Fri, 09 April 2010 12:21 Go to previous message
natha is currently offline  natha
Messages: 482
Registered: October 2007
Senior Member
I'll try something like that

aux=''
OPENR, un$lun, file, /GET_LUN

WHILE ~EOF(un$lun) DO BEGIN

READF, un$lun, aux

aux_str=STRSPLIT(aux,' ',/EXTRACT)

PRINT, 'label', aux_str[0:1]
PRINT, 'values', aux_str[2:*]

;; Then you can concatenate, etc...

ENDWHILE

FREE_LUN, un$lun
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Extracting variables from ascii files that are not in columnar format
Next Topic: warp_tri vs. polywarp/poly_2d

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

Current Time: Thu Oct 09 23:35:09 PDT 2025

Total time taken to generate the page: 0.08100 seconds