PDA

View Full Version : dynamic typing



hcrisp
12-10-2008, 01:27 PM
How do I convert a vector to a dynamic (variable) data type? Is there a function for this?

I want something like:


WAVE> var = [1.,2.,3.,4.]
WAVE> dvar = convert_fcn(var, /DOUBLE)
WAVE> cvar = convert_fcn(var, /COMPLEX)
WAVE> ; etc.
or

WAVE> var = [1.,2.,3.,4.]
WAVE> dvar = convert_fcn(var, TYPE=5)
WAVE> cvar = convert_fcn(var, TYPE=6)
WAVE> ; etc.

I know I can use DOUBLE(), COMPLEX(), etc., but isn't there a way to do this without coding all the cases? It should handle (at least) float, double, complex, and double complex.

The only one I found is CVT_ARR in the Math library, but it transforms the vector (from n to 1xn) which is unneeded.

donb
12-10-2008, 06:13 PM
There is a routine in the User Library (wave/lib/user) called data_convert.pro, however it ALSO handles conversions between various machines and their respective floating point representations and considers big- vs. little-endian-ness. This is perhaps overkill for your current question.

Unless other folks have another routine that comes to mind, it would be rather easy to write the .pro file you want, and inside that routine you can use a CASE stmt based on the desired output type and then use the conversion routines you mentioned (DOUBLE(), FLOAT(), etc.) for each case condition.

Don B.