[gradsusr] Convert string to numeric?

Jeff Duda jeffduda319 at gmail.com
Sun Aug 6 12:53:01 EDT 2017


Matt,
The 'define' statement doesn't work that way in Grads. "define" only
defines a (possibly) multidimensional field based on the dimension
environment and your data arrays. It is not used to define new script
variables. So how you proceed here depends on what you want to do. But to
get around the immediate issue, don't use "define" or any quotes (since the
use of quotes implies a command issued at the prompt, which many script
functions do not operate in).

Jeff

P.S. I see you responded to another response and got it to work, but I feel
the above information will be helpful in avoiding similar issues in the
future.

On Sun, Aug 6, 2017 at 11:00 AM, Matt Bunkers - NWS Rapid City <
matthew.bunkers at noaa.gov> wrote:

> Hi Jeff,
>
> I think you helped me narrow it down.  Here is a snippet of my code:
>
> say '  BUFawips = ' BUFawips
> rc = valnum(BUFawips)
> say '  rc = ' rc
>
> 'set lat 45.6043'
> 'set lon -103.5469'
> 'd totalQPE/25.4'
> line1=sublin(result,1)
> BUFqpe=subwrd(line1,4)
> say '  BUFqpe = ' BUFqpe
> rc = valnum(BUFqpe)
> say '  rc = ' rc
>
> 'define BUFratio='BUFawips'/'BUFqpe''
> say '  BUFratio = ' BUFratio
> rc = valnum(BUFratio)
> say '  rc = ' rc
>
> and here is the output:
>
>   BUFawips = 2.23
>   rc = 2
>   BUFqpe = 4.96543
>   rc = 2
>   BUFratio = BUFratio
>   rc = 0
>
> So it looks like my last part is where I'm failing.  Any thoughts on what
> I'm doing wrong with my "define" statement?
>
> Thanks,
>
> -- Matt
>
> On Sat, Aug 5, 2017 at 3:01 PM, Jeff Duda <jeffduda319 at gmail.com> wrote:
>
>> Matt,
>> Is it possible you are getting non-numeric characters stored in the
>> variable? You say Grads prints 2.19, but are there any spaces or hidden
>> characters that could be mixed in there? One way to check what's going on
>> is to run valnum(*var*). It should return a value of 2 for your
>> variables. If it returns 0 then it thinks *var* is a string. Once you
>> know that it might help you troubleshoot further. For example, try say
>> "_"var"_" to bracket the variable with underscores. That will tell you
>> pretty quickly if and how many spaces are also located in the variable.
>> That's my guess as to what's going on.
>>
>> Jeff Duda
>>
>> On Sat, Aug 5, 2017 at 3:53 PM, Matt Bunkers - NWS Rapid City <
>> matthew.bunkers at noaa.gov> wrote:
>>
>>> Hi,
>>>
>>> I have a text file with numbers that I'm reading into GrADS and then I
>>> want to perform calculations with them.  However, the numbers I'm reading
>>> won't work and I get an error that says I have non-numeric args to a
>>> numeric operations, even though when I print my variable it shows 2.19.
>>>
>>> I've tried both of these options:
>>>
>>> 'define BUFratio='BUFawips'/'BUFqpe''
>>> BUFratio='BUFawips'/'BUFqpe'
>>>
>>> Is there a command I can use to convert my "2.19" string to a number
>>> that can be used in calculations?
>>>
>>> Thanks,
>>>
>>> -- Matt
>>>
>>>
>>> _______________________________________________
>>> gradsusr mailing list
>>> gradsusr at gradsusr.org
>>> http://gradsusr.org/mailman/listinfo/gradsusr
>>>
>>>
>>
>>
>> --
>> Jeff Duda
>> Post-doctoral research fellow
>> University of Oklahoma School of Meteorology
>>
>> _______________________________________________
>> gradsusr mailing list
>> gradsusr at gradsusr.org
>> http://gradsusr.org/mailman/listinfo/gradsusr
>>
>>
>
> _______________________________________________
> gradsusr mailing list
> gradsusr at gradsusr.org
> http://gradsusr.org/mailman/listinfo/gradsusr
>
>


-- 
Jeff Duda
Post-doctoral research fellow
University of Oklahoma School of Meteorology
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://gradsusr.org/pipermail/gradsusr/attachments/20170806/c925fb07/attachment-0001.html 


More information about the gradsusr mailing list