Re: Generic type converter

From: Dave Allured <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Thu May 17 2012 - 21:25:09 MDT

Wei,

Yes I have used the toxxx functions. Those are flexible about the
type of the input argument. I need to be flexible about the type of
the function output. Following one of my examples, "data_type" is not
known in advance, and it changes for different iterations within the
same program run:

   y = type_convert (x, data_type)

Therefore I can not hard code any of the toxxx functions unless they
are enumerated within a group of "if data_type" statements. In this
particular application, the type of "x" can not be propagated either,
because it is the result of separate calculations of unrelated data
type.

--Dave

On Thu, May 17, 2012 at 8:49 PM, Wei Huang <huangwei@ucar.edu> wrote:
> Dave,
>
> Have you tried the toxxx functions?
> where xxx can be: double, float, string, int, short, long, etc.
>
> for example, y = tofloat(x) will convert any NCL type of x which
> can be converted to float to y as float.
>
> On May 17, 2012, at 4:26 PM, Dave Allured wrote:
>
>> All,
>>
>> To support some generic data processing, I would like to find a
>> numeric type converter that takes a numeric (or double) input, and
>> converts to a numeric type specified by a string "type" argument such
>> as argument 2 of the "new" function.  For example:
>>
>>   y = type_convert (x, "float")
>>
>> Or more generically, what I am really trying to do:
>>
>>   y = type_convert (x, data_type)
>>
>> Where "data_type" is a string containing the name of any legal NCL data type.
>>
>> The following does not work for some output types, because NCL checks
>> for loss of precision in assignments, a valuable feature.
>>
>>   y = new (1, data_type)
>>   y = x
>>
>> Does anyone know a nice way to do this without a stack of if
>> statements?  I did not find anything like this on the type converters
>> page.  Thank you.
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu May 17 21:25:19 2012

This archive was generated by hypermail 2.1.8 : Fri May 25 2012 - 08:35:50 MDT