Re: Common block access in DLM [message #39431] |
Wed, 19 May 2004 05:08 |
Haje Korth
Messages: 651 Registered: May 1997
|
Senior Member |
|
|
Rick,
thanks for the further information. And thanks for the advice on how to
define global vars in C. Since obviously no COMMON or similar statement is
required, it would have probably taken me forever to figure this out. (Just
as it took forever to find out the the argument after "switch" required
parantheses around it. :-) ).
Cheers,
Haje
"Rick Towler" <tsehai@comcast.net> wrote in message
news:_Auqc.73594$536.11940230@attbi_s03...
>
> "Haje Korth" wrote...
>
>> thanks for the example, this is more than I expected. Regarding my
> project,
>> I ported a FORTRAN library for geophysical coordinate transformations
and
>> magnetic field calculation from FORTRAN to IDL usind a DLM, basically
IDL
>> <-> DLM <-> C <-> FORTRAN, taking Ronn's exercises a little further.
>> Unfortunately I only have Ronn's first edition book, so I do not know if
> he
>> expanded on this in the second edition.
>
> I can't say. I know that he updated the text to include the new keyword
API
> and added the C++ section. I'm sure he did more, those were just the two
> most compelling updates for me.
>
>
>> Anyway, everything works as
>> expected, but having to supply year, day of year, hour minute second for
>> every call is nerve wrecking, and blows up the call to so many
parameters
>> that the IDL code looks confusing. I will give the global variables a
try.
>
> That's a good place to start. Then if you need the ability to run
multiple
> instances you can go the C++ route. I could dig up an example if you
like.
>
> Also, have you checked out Stein Vidar Hagfors Haugan's dlmform? It might
> be able to handle the tedium for you.
>
> http://www.astro.uio.no/~steinhh/idl/additions.html
> http://www.astro.uio.no/~steinhh/idl/dlmform.html
>
>
>> How do setup such a global variable in
>> C? Is this just regular C language, or is there some fancy handling
> required
>> with the IDL external API? (I am pretty much C newby, but once I now
this,
> I
>> think I can handle the interfacing with FORTRAN.)
>
> Plain old regular C. Simply define it before your first function and it
> will be available globally. I too am fumbling thru the dark when it comes
> to C/C++ but luckily there are enough people in the group that can help us
> when we get stuck. :)
>
> Good luck!
>
> -Rick
>
>
|
|
|
Re: Common block access in DLM [message #39437 is a reply to message #39431] |
Tue, 18 May 2004 13:44  |
Rick Towler
Messages: 821 Registered: August 1998
|
Senior Member |
|
|
"Haje Korth" wrote...
> thanks for the example, this is more than I expected. Regarding my
project,
> I ported a FORTRAN library for geophysical coordinate transformations and
> magnetic field calculation from FORTRAN to IDL usind a DLM, basically IDL
> <-> DLM <-> C <-> FORTRAN, taking Ronn's exercises a little further.
> Unfortunately I only have Ronn's first edition book, so I do not know if
he
> expanded on this in the second edition.
I can't say. I know that he updated the text to include the new keyword API
and added the C++ section. I'm sure he did more, those were just the two
most compelling updates for me.
> Anyway, everything works as
> expected, but having to supply year, day of year, hour minute second for
> every call is nerve wrecking, and blows up the call to so many parameters
> that the IDL code looks confusing. I will give the global variables a try.
That's a good place to start. Then if you need the ability to run multiple
instances you can go the C++ route. I could dig up an example if you like.
Also, have you checked out Stein Vidar Hagfors Haugan's dlmform? It might
be able to handle the tedium for you.
http://www.astro.uio.no/~steinhh/idl/additions.html
http://www.astro.uio.no/~steinhh/idl/dlmform.html
> How do setup such a global variable in
> C? Is this just regular C language, or is there some fancy handling
required
> with the IDL external API? (I am pretty much C newby, but once I now this,
I
> think I can handle the interfacing with FORTRAN.)
Plain old regular C. Simply define it before your first function and it
will be available globally. I too am fumbling thru the dark when it comes
to C/C++ but luckily there are enough people in the group that can help us
when we get stuck. :)
Good luck!
-Rick
|
|
|
Re: Common block access in DLM [message #39438 is a reply to message #39437] |
Tue, 18 May 2004 12:53  |
Haje Korth
Messages: 651 Registered: May 1997
|
Senior Member |
|
|
Rick
thanks for the example, this is more than I expected. Regarding my project,
I ported a FORTRAN library for geophysical coordinate transformations and
magnetic field calculation from FORTRAN to IDL usind a DLM, basically IDL
<-> DLM <-> C <-> FORTRAN, taking Ronn's exercises a little further.
Unfortunately I only have Ronn's first edition book, so I do not know if he
expanded on this in the second edition. Anyway, everything works as
expected, but having to supply year, day of year, hour minute second for
every call is nerve wrecking, and blows up the call to so many parameters
that the IDL code looks confusing. I will give the global variables a try.
Here a very stupid question though: How do setup such a global variable in
C? Is this just regular C language, or is there some fancy handling required
with the IDL external API? (I am pretty much C newby, but once I now this, I
think I can handle the interfacing with FORTRAN.)
Thanks,
Haje
"Rick Towler" <rtowler@u.washington.edu> wrote in message
news:c8dhm8$ooo$1@nntp6.u.washington.edu...
>
> "Haje Korth" wrote...
>
>> I have a routine that sets up coordinate transformations that are
>> subsequently used by calls to other procedures in the DLM. Rather than
>> calling this setup routine for every procedure call, it would be more
>> eficcient to establish the transformation matrices, store them in an IDL
>> common block, and access this common block over and over. However, this
>> scenario requires to be able to create IDL common block from C and have
> read
>> and write access to it. Therefore my question: Does anybody know whether
> it
>> is possible to access an IDL common block from C in a DLM? If anyone
could
>> point me to the right functions in the IDL external API, I would be very
>> thankful.
>
> Hi Haje,
>
> Sounds like you have something interesting brewing...
>
> I can't tell you how to do it with common blocks but if I am reading you
> correctly, you have a couple of other options:
>
> Use a global variable in your DLM. If you need to access this on the IDL
> side, then write your "init" function to return the transform back to IDL
> too and keep a copy there. Simple, if not very elegant. The downside is
> that you can only store one instance of your transform
>
>
> I used to do this quite a bit but have since started creating little C++
> classes to store this type of data so I could have multiple instances that
> didn't clash. And when I mean little, I *mean* little:
>
>
> class HajeTransform
> {
>
> public:
>
> float transform[4][4];
>
> };
>
>
> Then in your init function you set up your transform and return the
pointer
> to your HajeTransform object back to IDL:
>
>
> char *cptr;
> IDL_MEMINT dims[1];
> IDL_VPTR result;
> HajeTransform *hTransform = new HajeTransform;
>
> // do what you do to set up your transform
> (*hTransform).transform[0][0] = <insert stuff here>
> .
> .
> .
>
> // Return ptr to HajeTrans object back to IDL
> dims[0] = sizeof(hTransform);
> cptr = IDL_MakeTempArray(IDL_TYP_BYTE,1,dims,IDL_ARR_INI_NOP, &result);
> memcpy(cptr, &hTransform, dims[0]);
>
> return result;
>
>
> This pointer will serve as your instance ID, all subsequent calls to your
> DLM functions will require this ID so you can dereference the pointer and
> access your object members.
>
> Assuming your first argument is the pointer value:
>
>
> HajeTransform *hTransform;
>
> // obtain the pointer to the HajeTransform object
> IDL_ENSURE_ARRAY(argv[0]);
> memcpy(&hTransform, argv[0]->value.arr->data, sizeof(hTransform));
>
> // Now you can access your HajeTransform object
> (*hTransform).transform[0][0] = ....
>
>
>
> You will need to add a cleanup routine to free your object when you are
> done:
>
> // The SAFE_DELETE macro used in the function below is defined as:
> #define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }):
>
>
> void IDL_CDECL HajeTransform_Cleanup(int argc, IDL_VPTR *argv)
> {
>
> /*
> Frees memory associated with the HajeTransform object.
> */
>
> IDL_MEMINT dims[1];
> HajeTransform *hTransform;
>
> IDL_ENSURE_ARRAY(argv[0]);
>
> dims[0] = sizeof(hTransform);
> memcpy(&hTransform, argv[0]->value.arr->data, dims[0]);
>
> SAFE_DELETE(hTransform);
>
> }
>
>
> The details of passing pointers back and forth has been covered before
> (Nigel just posted on this subject again today). There have been other
> posts regarding compiling C++ dlms and Ronn's new version of his "Calling
C
> from IDL" now covers C++ too.
>
> Hope this helps!
>
> -Rick
>
>
|
|
|
Re: Common block access in DLM [message #39443 is a reply to message #39438] |
Tue, 18 May 2004 10:36  |
Rick Towler
Messages: 821 Registered: August 1998
|
Senior Member |
|
|
"Haje Korth" wrote...
> I have a routine that sets up coordinate transformations that are
> subsequently used by calls to other procedures in the DLM. Rather than
> calling this setup routine for every procedure call, it would be more
> eficcient to establish the transformation matrices, store them in an IDL
> common block, and access this common block over and over. However, this
> scenario requires to be able to create IDL common block from C and have
read
> and write access to it. Therefore my question: Does anybody know whether
it
> is possible to access an IDL common block from C in a DLM? If anyone could
> point me to the right functions in the IDL external API, I would be very
> thankful.
Hi Haje,
Sounds like you have something interesting brewing...
I can't tell you how to do it with common blocks but if I am reading you
correctly, you have a couple of other options:
Use a global variable in your DLM. If you need to access this on the IDL
side, then write your "init" function to return the transform back to IDL
too and keep a copy there. Simple, if not very elegant. The downside is
that you can only store one instance of your transform
I used to do this quite a bit but have since started creating little C++
classes to store this type of data so I could have multiple instances that
didn't clash. And when I mean little, I *mean* little:
class HajeTransform
{
public:
float transform[4][4];
};
Then in your init function you set up your transform and return the pointer
to your HajeTransform object back to IDL:
char *cptr;
IDL_MEMINT dims[1];
IDL_VPTR result;
HajeTransform *hTransform = new HajeTransform;
// do what you do to set up your transform
(*hTransform).transform[0][0] = <insert stuff here>
.
.
.
// Return ptr to HajeTrans object back to IDL
dims[0] = sizeof(hTransform);
cptr = IDL_MakeTempArray(IDL_TYP_BYTE,1,dims,IDL_ARR_INI_NOP, &result);
memcpy(cptr, &hTransform, dims[0]);
return result;
This pointer will serve as your instance ID, all subsequent calls to your
DLM functions will require this ID so you can dereference the pointer and
access your object members.
Assuming your first argument is the pointer value:
HajeTransform *hTransform;
// obtain the pointer to the HajeTransform object
IDL_ENSURE_ARRAY(argv[0]);
memcpy(&hTransform, argv[0]->value.arr->data, sizeof(hTransform));
// Now you can access your HajeTransform object
(*hTransform).transform[0][0] = ....
You will need to add a cleanup routine to free your object when you are
done:
// The SAFE_DELETE macro used in the function below is defined as:
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }):
void IDL_CDECL HajeTransform_Cleanup(int argc, IDL_VPTR *argv)
{
/*
Frees memory associated with the HajeTransform object.
*/
IDL_MEMINT dims[1];
HajeTransform *hTransform;
IDL_ENSURE_ARRAY(argv[0]);
dims[0] = sizeof(hTransform);
memcpy(&hTransform, argv[0]->value.arr->data, dims[0]);
SAFE_DELETE(hTransform);
}
The details of passing pointers back and forth has been covered before
(Nigel just posted on this subject again today). There have been other
posts regarding compiling C++ dlms and Ronn's new version of his "Calling C
from IDL" now covers C++ too.
Hope this helps!
-Rick
|
|
|