Re: changing a datatype in a structure [message #48642] |
Thu, 11 May 2006 13:18 |
Mark Hadfield
Messages: 783 Registered: May 1995
|
Senior Member |
|
|
Kenneth Bowman wrote:
> You cannot change the type of a variable inside a structure, but you can add a
> new variable to an existing (anonymous) structure.
>
> IDL> data = {i : LINDGEN(5)}
> IDL> help, data, /str
> ** Structure <215d270>, 1 tags, length=20, data length=20, refs=1:
> I LONG Array[5]
> IDL> data = CREATE_STRUCT(data, 'x', FLOAT(data.i))
> IDL> help, data, /str
> ** Structure <215d330>, 2 tags, length=40, data length=40, refs=1:
> I LONG Array[5]
> X FLOAT Array[5]
> IDL> print, data
> { 0 1 2 3 4
> 0.00000 1.00000 2.00000 3.00000 4.00000
> }
Yes, but what you've actually done is destroy the old one and create a
new one, as the different ID numbers above attest. The difference
doesn't generally matter for structures with small amounts of data but
it certainly can for large ones.
--
Mark Hadfield "Kei puwaha te tai nei, Hoea tahi tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
|
|
|
Re: changing a datatype in a structure [message #48643 is a reply to message #48642] |
Thu, 11 May 2006 13:17  |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
Kenneth Bowman wrote:
> You cannot change the type of a variable inside a structure, but you can add a
> new variable to an existing (anonymous) structure.
>
> IDL> data = {i : LINDGEN(5)}
> IDL> help, data, /str
> ** Structure <215d270>, 1 tags, length=20, data length=20, refs=1:
> I LONG Array[5]
> IDL> data = CREATE_STRUCT(data, 'x', FLOAT(data.i))
> IDL> help, data, /str
> ** Structure <215d330>, 2 tags, length=40, data length=40, refs=1:
> I LONG Array[5]
> X FLOAT Array[5]
> IDL> print, data
> { 0 1 2 3 4
> 0.00000 1.00000 2.00000 3.00000 4.00000
> }
Well, maybe it's just semantics, but in the above you create a new
structure and copy the old structure's data into. So, in effect, you're
"adding" a field to it, but in that case the following changes the type:
IDL> data = { i : indgen(5) }
IDL> help, data, /structure
** Structure <2213490>, 1 tags, length=10, data length=10, refs=1:
I INT Array[5]
IDL> data = { i : float(data.i) }
IDL> help, data, /structure
** Structure <22135b0>, 1 tags, length=20, data length=20, refs=1:
I FLOAT Array[5]
Mike
--
www.michaelgalloy.com
|
|
|
Re: changing a datatype in a structure [message #48648 is a reply to message #48643] |
Thu, 11 May 2006 06:16  |
K. Bowman
Messages: 330 Registered: May 2000
|
Senior Member |
|
|
In article <1147306191.957848.151880@q12g2000cwa.googlegroups.com>,
mark.macauda@gmail.com wrote:
> Ok, here is my current very frustrating problem. I have ALOT of IDL
> work done the general idea is that I read in binary files that are
> stored in very specific format...blah blah, the details aren't
> important, the important part is that rewriting all of that becuase of
> the current problem is not a desirable solution. Here's the problem.
> All the info is read in in structures and the data types are dictated
> by the binary files. One is an array of long 32-bit integers. I have
> discovered that I need to do some calculations on this array, that
> require the data type to be something other than interger. Normally I
> would just use the float command and be on my merry way...but for all
> the other programs to work I need the float arrays to be back in the
> handy dandy structures...is there anyway I can easily change that
> datatype?
You cannot change the type of a variable inside a structure, but you can add a
new variable to an existing (anonymous) structure.
IDL> data = {i : LINDGEN(5)}
IDL> help, data, /str
** Structure <215d270>, 1 tags, length=20, data length=20, refs=1:
I LONG Array[5]
IDL> data = CREATE_STRUCT(data, 'x', FLOAT(data.i))
IDL> help, data, /str
** Structure <215d330>, 2 tags, length=40, data length=40, refs=1:
I LONG Array[5]
X FLOAT Array[5]
IDL> print, data
{ 0 1 2 3 4
0.00000 1.00000 2.00000 3.00000 4.00000
}
Ken Bowman
|
|
|
|
Re: changing a datatype in a structure [message #48653 is a reply to message #48648] |
Wed, 10 May 2006 22:02  |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
mark.macauda@gmail.com wrote:
> Ok, here is my current very frustrating problem. I have ALOT of IDL
> work done the general idea is that I read in binary files that are
> stored in very specific format...blah blah, the details aren't
> important, the important part is that rewriting all of that becuase of
> the current problem is not a desirable solution. Here's the problem.
> All the info is read in in structures and the data types are dictated
> by the binary files. One is an array of long 32-bit integers. I have
> discovered that I need to do some calculations on this array, that
> require the data type to be something other than interger. Normally I
> would just use the float command and be on my merry way...but for all
> the other programs to work I need the float arrays to be back in the
> handy dandy structures...is there anyway I can easily change that
> datatype?
You should have a look at this both functions
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/struct2ptr_struct_dbase.pro.html
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/ptr_struct2struct_dbase.pro.html
struct={A:1,b:FINDGEN(10)}
HELP,struct,/str
** Structure <1052378>, 2 tags, length=44, refs=1:
A INT 1
B FLOAT Array[10]
result=struct2ptr_struct(struct)
HELP,result,/str
** Structure <10551e8>, 2 tags, length=8, refs=1:
A POINTER
B POINTER
*result.a=1.234
result=ptr_struct2struct(result,/free)
HELP,result,/str
** Structure <1056e28>, 2 tags, length=44, refs=1:
A FLOAT 1.234
B FLOAT Array[10]
cheers
Reimar
|
|
|
Re: changing a datatype in a structure [message #48655 is a reply to message #48653] |
Wed, 10 May 2006 18:30  |
Mark Hadfield
Messages: 783 Registered: May 1995
|
Senior Member |
|
|
mark.macauda@gmail.com wrote:
> Ok, here is my current very frustrating problem. I have ALOT of IDL
> work done the general idea is that I read in binary files that are
> stored in very specific format...blah blah, the details aren't
> important, the important part is that rewriting all of that becuase of
> the current problem is not a desirable solution. Here's the problem.
> All the info is read in in structures and the data types are dictated
> by the binary files. One is an array of long 32-bit integers. I have
> discovered that I need to do some calculations on this array, that
> require the data type to be something other than interger. Normally I
> would just use the float command and be on my merry way...but for all
> the other programs to work I need the float arrays to be back in the
> handy dandy structures...is there anyway I can easily change that
> datatype?
Changing the data type or shape of a field in a structure is impossible,
as is adding a new field. The only way to make it look like you've done
so to create a new structure and copy the data from the old one.
Your problem sounds like an obvious application for pointers:
IDL> x = {a: ptr_new(indgen(10))}
IDL> help, *x.a
<PtrHeapVar24115>
INT = Array[10]
IDL> *x.a = float(*x.a)
IDL> help, *x.a
<PtrHeapVar24115>
FLOAT = Array[10]
IDL> ptr_free, x.a
Note that the integer array and the float array that replaces it are
both associated with the same pointer heap variable. When you've
finished with the structure you will have to clean up the pointer
variable, but you don't have to worry about that there's still a copy of
the integer array lurking around somewhere.
--
Mark Hadfield "Kei puwaha te tai nei, Hoea tahi tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
|
|
|