index arrays of structures [message #59229] |
Sat, 15 March 2008 13:53  |
raghuram
Messages: 32 Registered: February 2008
|
Member |
|
|
Hi,
With reference to my recent posting (IDL batch indexing), i understood
that i could structures. However, i haven't been able to figure out
one step.
Here is my code as of now:
; I want to search for all the images(files) in the directory trials,
read them into the structure named data, and access each file within
the structure.
pro strcutres
dir1='D:\trials'
cd,dir1
files=FILE_SEARCH('*[^\.{hdr}]', /QUOTE,count=numfiles)
i=0
while i lt numfiles do begin
named=files(i)
print,named
data={ID:'a',sizes:fltarr(2179,761)}
data=replicate(data,numfiles)
data.sizes=findgen(numfiles)
openr,1,named
readu,1,data[i].sizes
close,1
i=i+1
endwhile
end
ERROR message- READU: Expression must be named variable in this
context: <FLOAT Array[2179, 761]>.
I am getting the error here because it seems like i am not able to
read in the LUN 1 or named, into data[i].sizes.
Where am i going wrong ?
Raghu
|
|
|
Re: index arrays of structures [message #59295 is a reply to message #59229] |
Mon, 17 March 2008 13:31  |
raghuram
Messages: 32 Registered: February 2008
|
Member |
|
|
On Mar 17, 10:57 am, "R.G. Stockwell" <notha...@noemail.com> wrote:
> "Raghu" <raghuram.narasim...@gmail.com> wrote in message
>
> news:3db3001e-8c15-45a8-814c-9ffc92f43df8@e10g2000prf.google groups.com...
>
>
>
>
>
>> Hi,
>
>> With reference to my recent posting (IDL batch indexing), i understood
>> that i could structures. However, i haven't been able to figure out
>> one step.
>
>> Here is my code as of now:
>> ; I want to search for all the images(files) in the directory trials,
>> read them into the structure named data, and access each file within
>> the structure.
>
>> pro strcutres
>> dir1='D:\trials'
>> cd,dir1
>> files=FILE_SEARCH('*[^\.{hdr}]', /QUOTE,count=numfiles)
>> i=0
>> while i lt numfiles do begin
>> named=files(i)
>> print,named
>> data={ID:'a',sizes:fltarr(2179,761)}
>> data=replicate(data,numfiles)
>> data.sizes=findgen(numfiles)
>> openr,1,named
>> readu,1,data[i].sizes
>> close,1
>> i=i+1
>> endwhile
>> end
>
>> ERROR message- READU: Expression must be named variable in this
>> context: <FLOAT Array[2179, 761]>.
>
>> I am getting the error here because it seems like i am not able to
>> read in the LUN 1 or named, into data[i].sizes.
>
>> Where am i going wrong ?
>
>> Raghu
>
> I'm not sure if it has been explained, but your error message
> is telling you that readu,1,data[i].sizes does now work,
> because data[i].sizes is an expression. It is similar to
> readu,1,x + 4
> or
> readu, 1, abs(array)
>
> in that IDL executes the data[i].sizes almost like it was a
> function, and it returns the array you request (i.e. the 'sizes'
> field of the fourth element of the data array).
>
> Just read it directly into an array, the assign the array to the
> structure in a second line.
>
> The fact that your filename is called "named" and the error message
> says you need a "named array" is just a coincedence.
>
> ALSO, look at the line:
> data.sizes=findgen(numfiles)
>
> I am not sure this is doing what you want it to do.
>
> Cheers,
> bob- Hide quoted text -
>
> - Show quoted text -
Hi,
Yea, the findgen statement is not doing anything. Initially, i thought
i'd use that to generate an array of the number of files i need. But
it assigns values to it and i don't want that. I wanted something like
an empty list.
Thanks for the explaination of the named variable. I'm clear now.
Raghu
|
|
|
Re: index arrays of structures [message #59296 is a reply to message #59229] |
Mon, 17 March 2008 10:57  |
R.G. Stockwell
Messages: 363 Registered: July 1999
|
Senior Member |
|
|
"Raghu" <raghuram.narasimhan@gmail.com> wrote in message
news:3db3001e-8c15-45a8-814c-9ffc92f43df8@e10g2000prf.google groups.com...
> Hi,
>
> With reference to my recent posting (IDL batch indexing), i understood
> that i could structures. However, i haven't been able to figure out
> one step.
>
> Here is my code as of now:
> ; I want to search for all the images(files) in the directory trials,
> read them into the structure named data, and access each file within
> the structure.
>
> pro strcutres
> dir1='D:\trials'
> cd,dir1
> files=FILE_SEARCH('*[^\.{hdr}]', /QUOTE,count=numfiles)
> i=0
> while i lt numfiles do begin
> named=files(i)
> print,named
> data={ID:'a',sizes:fltarr(2179,761)}
> data=replicate(data,numfiles)
> data.sizes=findgen(numfiles)
> openr,1,named
> readu,1,data[i].sizes
> close,1
> i=i+1
> endwhile
> end
>
> ERROR message- READU: Expression must be named variable in this
> context: <FLOAT Array[2179, 761]>.
>
> I am getting the error here because it seems like i am not able to
> read in the LUN 1 or named, into data[i].sizes.
>
> Where am i going wrong ?
>
> Raghu
I'm not sure if it has been explained, but your error message
is telling you that readu,1,data[i].sizes does now work,
because data[i].sizes is an expression. It is similar to
readu,1,x + 4
or
readu, 1, abs(array)
in that IDL executes the data[i].sizes almost like it was a
function, and it returns the array you request (i.e. the 'sizes'
field of the fourth element of the data array).
Just read it directly into an array, the assign the array to the
structure in a second line.
The fact that your filename is called "named" and the error message
says you need a "named array" is just a coincedence.
ALSO, look at the line:
data.sizes=findgen(numfiles)
I am not sure this is doing what you want it to do.
Cheers,
bob
|
|
|
Re: index arrays of structures [message #59298 is a reply to message #59229] |
Mon, 17 March 2008 09:29  |
raghuram
Messages: 32 Registered: February 2008
|
Member |
|
|
On Mar 17, 8:03 am, David Fanning <n...@dfanning.com> wrote:
> Reimar Bauer writes:
>> Have to say first that is not a solution for your current problem. But
>> sometimes you like to know about this too
>
>> You can directly read into pointers
>> this examples assumes files of equal filesize (it reads now only one
>> file, but you can use [i] for [0] )
>
>> a={data:replicate(ptr_new(bytarr(20996)),4)}
>> openr,lun,'example.bin',/get
>> readu,lun,*(a.data)[0]
>> free_lun,lun
>
>> print, (*(a.data)[0])[0:10] ; prints the first bytes
>
>> You can make the structure more complex if you have to read for example
>> records of byte, float and other types. Then you have to define a
>> structure with placeholders for these types. You can replicate this
>> structure to the number of records. And then the whole file could be
>> read at once.
>
> With all due respect to Reimar, this is NOT the kind
> of thing you should be laying on a guy who is struggling
> to understand the basics of programming! Give the
> guy a break. Simple, simple, simple. He can learn the
> rest later. :-)
>
> 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.")- Hide quoted text -
>
> - Show quoted text -
Well,
The only way i am going to look at this is that there are many
possible solutions to a problem, some simple and some complicated.
True. I am new to programming and am still learning. This is a good
chance for me to learn about structures,pointers, automation etc. and
relate them to my work. I would never learn to use these concepts
unless i apply them somewhere. As long as i learn something new
everyday, i'm adding something. Thats what matters. Thanks for all
your replies and suggestions. I will try them all, understand them,
and use them such that the code works.
Thanks,
Raghu
|
|
|
Re: index arrays of structures [message #59301 is a reply to message #59229] |
Mon, 17 March 2008 08:03  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Reimar Bauer writes:
> Have to say first that is not a solution for your current problem. But
> sometimes you like to know about this too
>
> You can directly read into pointers
> this examples assumes files of equal filesize (it reads now only one
> file, but you can use [i] for [0] )
>
> a={data:replicate(ptr_new(bytarr(20996)),4)}
> openr,lun,'example.bin',/get
> readu,lun,*(a.data)[0]
> free_lun,lun
>
> print, (*(a.data)[0])[0:10] ; prints the first bytes
>
> You can make the structure more complex if you have to read for example
> records of byte, float and other types. Then you have to define a
> structure with placeholders for these types. You can replicate this
> structure to the number of records. And then the whole file could be
> read at once.
With all due respect to Reimar, this is NOT the kind
of thing you should be laying on a guy who is struggling
to understand the basics of programming! Give the
guy a break. Simple, simple, simple. He can learn the
rest later. :-)
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: index arrays of structures [message #59302 is a reply to message #59229] |
Mon, 17 March 2008 07:44  |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
Raghu schrieb:
> Hi,
>
> With reference to my recent posting (IDL batch indexing), i understood
> that i could structures. However, i haven't been able to figure out
> one step.
>
> Here is my code as of now:
> ; I want to search for all the images(files) in the directory trials,
> read them into the structure named data, and access each file within
> the structure.
>
> pro strcutres
> dir1='D:\trials'
> cd,dir1
> files=FILE_SEARCH('*[^\.{hdr}]', /QUOTE,count=numfiles)
> i=0
> while i lt numfiles do begin
> named=files(i)
> print,named
> data={ID:'a',sizes:fltarr(2179,761)}
> data=replicate(data,numfiles)
> data.sizes=findgen(numfiles)
> openr,1,named
> readu,1,data[i].sizes
> close,1
> i=i+1
> endwhile
> end
>
> ERROR message- READU: Expression must be named variable in this
> context: <FLOAT Array[2179, 761]>.
>
> I am getting the error here because it seems like i am not able to
> read in the LUN 1 or named, into data[i].sizes.
>
> Where am i going wrong ?
>
> Raghu
Have to say first that is not a solution for your current problem. But
sometimes you like to know about this too
You can directly read into pointers
this examples assumes files of equal filesize (it reads now only one
file, but you can use [i] for [0] )
a={data:replicate(ptr_new(bytarr(20996)),4)}
openr,lun,'example.bin',/get
readu,lun,*(a.data)[0]
free_lun,lun
print, (*(a.data)[0])[0:10] ; prints the first bytes
You can make the structure more complex if you have to read for example
records of byte, float and other types. Then you have to define a
structure with placeholders for these types. You can replicate this
structure to the number of records. And then the whole file could be
read at once.
cheers
Reimar
|
|
|
Re: index arrays of structures [message #59311 is a reply to message #59229] |
Sun, 16 March 2008 12:29  |
Vince Hradil
Messages: 574 Registered: December 1999
|
Senior Member |
|
|
Raghu wrote:
> On Mar 16, 5:49 am, Vince Hradil <hrad...@yahoo.com> wrote:
>> Raghu wrote:
>>> On Mar 15, 7:31�pm, Vince Hradil <hrad...@yahoo.com> wrote:
>>>> On Mar 15, 4:07 pm, Vince Hradil <hrad...@yahoo.com> wrote:
>>
>>>> > On Mar 15, 3:53 pm, Raghu <raghuram.narasim...@gmail.com> wrote:
>>
>>>> > > Hi,
>>
>>>> > > With reference to my recent posting (IDL batch indexing), i understood
>>>> > > that i could structures. However, i haven't been able to figure out
>>>> > > one step.
>>
>>>> > > Here is my code as of now:
>>>> > > ; I want to search for all the images(files) in the directory trials,
>>>> > > read them into the structure named data, and access each file within
>>>> > > the structure.
>>
>>>> > > pro strcutres
>>>> > > dir1='D:\trials'
>>>> > > cd,dir1
>>>> > > files=FILE_SEARCH('*[^\.{hdr}]', /QUOTE,count=numfiles)
>>>> > > i=0
>>>> > > while i lt numfiles do begin
>>>> > > named=files(i)
>>>> > > print,named
>>>> > > data={ID:'a',sizes:fltarr(2179,761)}
>>>> > > data=replicate(data,numfiles)
>>>> > > data.sizes=findgen(numfiles)
>>>> > > openr,1,named
>>>> > > readu,1,data[i].sizes
>>>> > > close,1
>>>> > > i=i+1
>>>> > > endwhile
>>>> > > end
>>
>>>> > > ERROR message- READU: Expression must be named variable in this
>>>> > > context: <FLOAT � � Array[2179, 761]>.
>>
>>>> > > I am getting the error here because it seems like i am not able to
>>>> > > read in the LUN 1 or named, into data[i].sizes.
>>
>>>> > > Where am i going wrong ?
>>
>>>> > > Raghu
>>
>>>> > Hoo boy - here you go.
>>
>>>> > 1-Yes, you need to use a named variable to readu
>>>> > 2-You already defined "sizes" (weird name, by the way, how about
>>>> > "values"), when you made the structure.
>>
>>>> > How about this:
>>
>>>> > pro strcutres
>>>> > dir1='D:\trials'
>>>> > cd,dir1
>>>> > files=FILE_SEARCH('*[^\.{hdr}]', /QUOTE,count=numfiles)
>>
>>>> > data={ID:'a',values:fltarr(2179,761)}
>>>> > data=replicate(data,numfiles)
>>>> > tempval = fltarr(2179,761)
>>
>>>> > for i=0L, numfiles-1 do begin
>>>> > named=files(i)
>>>> > print,named
>>
>>>> > openr,1,named
>>>> > readu,1,tempval
>>>> > free_lun,1
>>>> > data[i].values = tempval
>>
>>>> > endfor
>>
>>>> > return
>>>> > end
>>
>>>> BTW - why do you want to use a structure again? �The above code makes
>>>> all the data.id's = 'a'.
>>
>>>> Why can't you just read the data into a matrix? �If you want the
>>>> structure to contain the filename then add data[i].id = named inside
>>>> the loop.- Hide quoted text -
>>
>>>> - Show quoted text -
>>
>>> Hi,
>>
>>> Are you asking why i'm using structures in the first place ?
>>> If yes, my initial idea (from python lessons) was to create something
>>> like an empty array and then read in each file into this empty array
>>> by concatenation. That way each element would have a unique ID which i
>>> could use to access them.
>>> But it seems like i can't create an empty 2-d array of a certain size
>>> and number of files to be read ('numfiles' in my case).
>>> When you say create a matrix, is this what you meant ?
>>
>>> I got the idea of using structures only from responses in my previous
>>> emails on 'batch indexing'.
>>
>>> -R
>>
>> I guess I'm still confused. I was suggesting just doing:
>> data = fltarr(nx,ny,numfiles)
>>
>> then for each files[i] the image is data[*,*,i]
>>
>> It's unique simply by the index.- Hide quoted text -
>>
>> - Show quoted text -
>
> Hi Vince,
>
> I combined the ideas and it seems to have worked.
> The file_search method is storing the filenames in a string array. So,
> if i read them all one by one into data[*,*,numfiles-1], it is
> placing each files[i] into the corresponding location in data[*,*,i].
> This way i am able to access each file, and then compute the mean, for
> e.g.
>
> Here's the code:
>
> pro structures_simple
>
> dir1='D:\trials'
> cd,dir1
> files=FILE_SEARCH('*[^\.{hdr}]', /QUOTE,count=numfiles)
> print,files
> print,numfiles
>
> data=fltarr(2179,761,numfiles)
>
> tempval = fltarr(2179,761)
> for i=0L, numfiles-1 do begin
> named=files(i)
>
> openr,1,named
> readu,1,tempval
> close,1
> data[*,*,i]=tempval
> avg=mean(data[*,*,i],/nan)
> print,avg
> help,data[*,*,i]
> endfor
>
> return
>
> end
>
> What do you think ?
>
> -Raghu
Yes. That's what kind of what I was thinking. Of course, if you just
want the average, then there is no need to use the nx by ny by
numfiles array, just the tempval.
tempval = fltarr(nx,ny,/nozero)
for i=0l, numfiles-1 do begin
openr, lun, files[i], /get_lun
readu, lun, tempval
free_lun, lun
print, files[i], ' ', mean(tempval,/nan)
endfor
|
|
|
Re: index arrays of structures [message #59312 is a reply to message #59229] |
Sun, 16 March 2008 12:19  |
raghuram
Messages: 32 Registered: February 2008
|
Member |
|
|
On Mar 16, 5:49 am, Vince Hradil <hrad...@yahoo.com> wrote:
> Raghu wrote:
>> On Mar 15, 7:31�pm, Vince Hradil <hrad...@yahoo.com> wrote:
>>> On Mar 15, 4:07 pm, Vince Hradil <hrad...@yahoo.com> wrote:
>
>>>> On Mar 15, 3:53 pm, Raghu <raghuram.narasim...@gmail.com> wrote:
>
>>>> > Hi,
>
>>>> > With reference to my recent posting (IDL batch indexing), i understood
>>>> > that i could structures. However, i haven't been able to figure out
>>>> > one step.
>
>>>> > Here is my code as of now:
>>>> > ; I want to search for all the images(files) in the directory trials,
>>>> > read them into the structure named data, and access each file within
>>>> > the structure.
>
>>>> > pro strcutres
>>>> > dir1='D:\trials'
>>>> > cd,dir1
>>>> > files=FILE_SEARCH('*[^\.{hdr}]', /QUOTE,count=numfiles)
>>>> > i=0
>>>> > while i lt numfiles do begin
>>>> > named=files(i)
>>>> > print,named
>>>> > data={ID:'a',sizes:fltarr(2179,761)}
>>>> > data=replicate(data,numfiles)
>>>> > data.sizes=findgen(numfiles)
>>>> > openr,1,named
>>>> > readu,1,data[i].sizes
>>>> > close,1
>>>> > i=i+1
>>>> > endwhile
>>>> > end
>
>>>> > ERROR message- READU: Expression must be named variable in this
>>>> > context: <FLOAT � � Array[2179, 761]>.
>
>>>> > I am getting the error here because it seems like i am not able to
>>>> > read in the LUN 1 or named, into data[i].sizes.
>
>>>> > Where am i going wrong ?
>
>>>> > Raghu
>
>>>> Hoo boy - here you go.
>
>>>> 1-Yes, you need to use a named variable to readu
>>>> 2-You already defined "sizes" (weird name, by the way, how about
>>>> "values"), when you made the structure.
>
>>>> How about this:
>
>>>> pro strcutres
>>>> dir1='D:\trials'
>>>> cd,dir1
>>>> files=FILE_SEARCH('*[^\.{hdr}]', /QUOTE,count=numfiles)
>
>>>> data={ID:'a',values:fltarr(2179,761)}
>>>> data=replicate(data,numfiles)
>>>> tempval = fltarr(2179,761)
>
>>>> for i=0L, numfiles-1 do begin
>>>> named=files(i)
>>>> print,named
>
>>>> openr,1,named
>>>> readu,1,tempval
>>>> free_lun,1
>>>> data[i].values = tempval
>
>>>> endfor
>
>>>> return
>>>> end
>
>>> BTW - why do you want to use a structure again? �The above code makes
>>> all the data.id's = 'a'.
>
>>> Why can't you just read the data into a matrix? �If you want the
>>> structure to contain the filename then add data[i].id = named inside
>>> the loop.- Hide quoted text -
>
>>> - Show quoted text -
>
>> Hi,
>
>> Are you asking why i'm using structures in the first place ?
>> If yes, my initial idea (from python lessons) was to create something
>> like an empty array and then read in each file into this empty array
>> by concatenation. That way each element would have a unique ID which i
>> could use to access them.
>> But it seems like i can't create an empty 2-d array of a certain size
>> and number of files to be read ('numfiles' in my case).
>> When you say create a matrix, is this what you meant ?
>
>> I got the idea of using structures only from responses in my previous
>> emails on 'batch indexing'.
>
>> -R
>
> I guess I'm still confused. I was suggesting just doing:
> data = fltarr(nx,ny,numfiles)
>
> then for each files[i] the image is data[*,*,i]
>
> It's unique simply by the index.- Hide quoted text -
>
> - Show quoted text -
Hi Vince,
I combined the ideas and it seems to have worked.
The file_search method is storing the filenames in a string array. So,
if i read them all one by one into data[*,*,numfiles-1], it is
placing each files[i] into the corresponding location in data[*,*,i].
This way i am able to access each file, and then compute the mean, for
e.g.
Here's the code:
pro structures_simple
dir1='D:\trials'
cd,dir1
files=FILE_SEARCH('*[^\.{hdr}]', /QUOTE,count=numfiles)
print,files
print,numfiles
data=fltarr(2179,761,numfiles)
tempval = fltarr(2179,761)
for i=0L, numfiles-1 do begin
named=files(i)
openr,1,named
readu,1,tempval
close,1
data[*,*,i]=tempval
avg=mean(data[*,*,i],/nan)
print,avg
help,data[*,*,i]
endfor
return
end
What do you think ?
-Raghu
|
|
|