[gradsusr] How to convert a string to a real number?

Jeffrey Duda jdduda at iastate.edu
Wed Jan 5 14:44:53 EST 2011


Qinglan,
In your script, try_loop_p2.gs, change the line

SW1=subwrd(SWDOWN.1,1)

to

SW1=subwrd(result,4)

That fixes the errors on lines 9 and 12.

I get two text files, SW1.txt and SW2.txt.  SW1.txt contains the single
value 359.505.  SW2.txt contains the single value 360.505.  Is this what you
want?

Jeff

On Wed, Jan 5, 2011 at 8:39 AM, qinglan <qinglan at hku.hk> wrote:

>   Dear Jeff and other Grads users,
>
> First of all, really appreciate your help and time.
>
> In order to make my question more clearly and simple, I have reduced the
> data size and simplified the code. I attach the .dat file, .ctl file and .gs
> file, as well as fprintf.gs file. There is no problem running the attached
> try_loop_p2.gs. The output SW1.txt is 359.505, which should be a string,
> not a value. My problem is how to transfer SW1 to a number.
>
> Still in try_loop_p2.gs, if remove the first * marks in lines 9 and 12 (or
> lines 10 and 13), error occurs. The error information is “Syntax Error” or
> “Non-numeric args to numeric operation ”.
>
> My problem is how to make SW2=SW1+1/ or SW3=SWDOWN+1 work.
>
> Thank you very much!
>
> Qinglan Li
>
>  ------------------------------
> *From:* gradsusr-bounces at gradsusr.org [gradsusr-bounces at gradsusr.org] On
> Behalf Of Jeffrey Duda [jdduda at iastate.edu]
> *Sent:* Wednesday, January 05, 2011 1:46 PM
>
> *To:* GrADS Users Forum
> *Subject:* Re: [gradsusr] How to convert a string to a real number?
>
>  Qinglan,
> Are you sure you're using the subwrd function correctly?  I don't think you
> are.  If you want to get the value of SWDOWN.1 and SWDOWN.2, do something
> like
>
> (set environment commands)
> 'd SWDOWN.1'
> SW1 = subwrd(result,1)  !make sure this is the correct one...depending on
> how your control file is setup, you might get "Notice: interpolation will be
> made", then the value, in which case you'll need to do a sublin command
> first, then the subwrd command
>
> I'm pretty sure that's the source of your error.  Here's an example from
> another script I use (vgeo.gs):
>
> 'q dims'
> xline=sublin(result,2)
> xdim1=subwrd(xline,11)
> xdim2=subwrd(xline,13)
> yline=sublin(result,3)
> ydim1=subwrd(yline,11)
> ydim2=subwrd(yline,13)
> ...
> ...
> ...
> x3 = xdim1 + xdim2
> say xdim1 "+" xdim2 "=" x3
> ...
> ...
>
> *The output is:*
> ga-> run vgeo.gs
> 1+301=302
>
> Try making those changes and see if it works then.
>
> Jeff
>
> P.S. Alternately, you can try using the maskout function to define a new
> variable for SWDOWN which is undefined where your LU_index variable is 1.
> Such a command could be like:
> 'define SWDOWNmask = maskout(SWDOWN.1,LU_index - 1.5)'
> This would make sure not to mask out SWDOWN.1 when LU_index was 2 or
> greater (I'm assuming the values of LU_index are positive integers only).
>
>
> On Tue, Jan 4, 2011 at 9:19 PM, qinglan <qinglan at hku.hk> wrote:
>
>>   Dear all,
>>
>> Thanks for the response, but the reply has not solved my problem. Let me
>> make my question more specific. I would like to calculate the average of the
>> SWDOWN (short wave radiation) over certain land type, here referring to
>> rural land type (LU_index =not 1).
>>
>> The following is my original code.  First of all, I want summate all the
>> values of SWDOWN for all the grids with LU_index!=1 and LU_index!=1, then
>> divided by the total number of nrr.  tim, m and n might be larger, here
>> for simplifying purpose, setting them for only one loop each.
>>
>> The problem is the outputs of SWDOWN.1 or SWDOWN.2 are not a single value,
>> the program treat SWDOWN.1 as a vector, even the t, x, y (tim, m, n) are
>> fixed to one value.
>>
>> Firstly, there is an error for this sentence
>>
>>       SWDOWN_nrr1=SWDOWN_nrr1+SWDOWN.1
>>
>> Or I try to output SWDOWN.1 to a single value
>>
>>      SW1=subwrd(SWDOWN.1,1)
>>
>>  the program treats SW1 as a string, not a value, so
>>
>> SW5=SW1+SW2               makes mistakes, as well as
>> SWDOWN_nrr1=SWDOWN_nrr1+'SW1'
>>
>> Possible solutions for my problems are:  convert the output of SWDOWN.1
>> to a single value, which I can use for summation.      SWDOWN_nrr1=SWDOWN_nrr1+SWDOWN.1;
>>
>>
>> or convert a string to a value, which I can also use it for summation.
>> SWDOWN_nrr1=SWDOWN_nrr1+'SW1'
>>
>>
>>
>> Thank you very much!
>>
>> Qinglan Li
>>
>>
>>
>>
>>
>>  'reinit'
>>
>> 'open Small_Nest3_Moisture_2000July23-27_00_UTC_2000_land_AH100.ctl'
>>
>> 'open
>> Small_Nest3_Moisture_2000July23-27_00_UTC_1980_land_AH100_changed_0.7profile.ctl'
>>
>> tim=25
>>
>> while(tim<=25)
>>
>> SWDOWN_rr1=0
>>
>> SWDOWN_rr2=0
>>
>>   'set t 'tim''
>>
>> nrr=0
>>
>> SWDOWN_nrr1=0
>>
>> SWDOWN_nrr2=0
>>
>> m=1
>>
>> while(m<=1)
>>
>> n=1
>>
>> while(n<=1)
>>
>>   'set x 'm''
>>
>>   'set y 'n''
>>
>>   LU1=LU_index.1
>>
>>   LU2=LU_index.2
>>
>>   SW1=subwrd(SWDOWN.1,1)
>>
>>   SW2=subwrd(SWDOWN.2,1)
>>
>>   SW5=SW1+SW2                                   ********error
>>
>>   SW3=1.1
>>
>>   SW4=2.2
>>
>>   if (LU1!=1 & LU2!=1)
>>
>>       nrr=nrr+1
>>
>>       SWDOWN_nrr1=SWDOWN_nrr1+'SW1'            ******error
>>
>>       SWDOWN_nrr2=SWDOWN_nrr2+'SW2'           ******error
>>
>>   endif
>>
>>   n=n+1
>>
>> endwhile
>>
>>   m=m+1
>>
>> endwhile
>>
>> SWDOWN_rr1=SWDOWN_nrr1/nrr
>>
>> SWDOWN_rr2=SWDOWN_nrr2/nrr
>>
>> *'d SWDOWN_rr1'
>>
>> *'d SWDOWN_rr2'
>>
>>  'd 'SWDOWN_nrr1''
>>
>>  'd 'SWDOWN_nrr2''
>>
>>  'fprintf 'LU1' LU1.txt'
>>
>>  'd 'LU2''
>>
>>  'fprintf 'SW1' SW1.txt'
>>
>>  'fprintf 'SW5' SW5.txt'
>>
>>  'fprintf 'nrr' nrr.txt'
>>
>>  'fprintf 'SWDOWN_nrr2' SWDOWN_nrr2.txt'
>>
>>   tim=tim+1
>>
>> endwhile
>>
>>  ------------------------------
>> *From:* gradsusr-bounces at gradsusr.org [gradsusr-bounces at gradsusr.org] On
>> Behalf Of Jeffrey Duda [jdduda at iastate.edu]
>> *Sent:* Wednesday, January 05, 2011 3:34 AM
>> *To:* GrADS Users Forum
>> *Subject:* Re: [gradsusr] How to convert a string to a real number?
>>
>>    As far as I'm aware, you shouldn't have to explicitly convert in grads
>> scripting.  Just use your quotes correctly.  Here's an example from a script
>> I wrote to compute theta-e:
>>
>> These constants are set as variables within the grads script only (i.e.,
>> they don't exist in the command window...I can't display Rv or Rd etc.).
>> Thus they're regarded as constants or strings.
>>
>> Rv = 461.5
>> Rd = 287.05
>> Cp = 1005
>> lv = 2.5e6
>>
>> In the actual theta-e computation below, notice how the variables are used
>> between single quotes so as not to be a part of a command that would
>> otherwise be issued in the command window.  This is all you should have to
>> do to use a string as a number or vice versa.
>>
>>   'tempK = tc + 273'
>>   'es = 6.11*exp((2.5e6/461.5)*((1/273) - (1/tempK)))'
>>   'e = qvapor*((p+pb)/100) / (qvapor + 0.622)'
>>   'equivt = tempK + 'Lv'*qvapor/'Cp'
>>   'thetae = equivt*pow(1000/slp,'Rd'/'Cp')'
>>
>>
>> I hope this helps, but ask more questions if it doesn't.
>>
>> Jeff Duda
>>
>> On Tue, Jan 4, 2011 at 1:24 PM, Murphy, Darren W <dwmurphy at nps.edu>wrote:
>>
>>> I’d like to second this question.  I am having the same issue.  I think
>>> the Grads literature says that some commands can use a number as a string
>>> for calculations.  However, when I try and use my number “string”  in a max
>>> or min Grads function, it throws an error.
>>>
>>> Thanks,
>>> Darren
>>>
>>>
>>> On 1/4/11 6:45 AM, "qinglan" <qinglan at hku.hk> wrote:
>>>
>>>  Dear All,
>>>
>>> Is there anybody know how to convert a string to a real number in Grads?
>>> For example, if my string is ‘300.5’, I want to get the value of 300.5,
>>> which is a real number, how to do that?
>>>
>>> Really appreciate your help!
>>> Best regards,
>>> Qinglan Li
>>>
>>>
>>> ------------------------------
>>> _______________________________________________
>>> 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
>> Iowa State University
>> Meteorology Graduate Student
>> 3134 Agronomy Hall
>> www.meteor.iastate.edu/~jdduda <http://www.meteor.iastate.edu/%7Ejdduda>
>>
>> _______________________________________________
>> gradsusr mailing list
>> gradsusr at gradsusr.org
>> http://gradsusr.org/mailman/listinfo/gradsusr
>>
>>
>
>
> --
> Jeff Duda
> Iowa State University
> Meteorology Graduate Student
> 3134 Agronomy Hall
> www.meteor.iastate.edu/~jdduda <http://www.meteor.iastate.edu/%7Ejdduda>
>
> _______________________________________________
> gradsusr mailing list
> gradsusr at gradsusr.org
> http://gradsusr.org/mailman/listinfo/gradsusr
>
>


-- 
Jeff Duda
Iowa State University
Meteorology Graduate Student
3134 Agronomy Hall
www.meteor.iastate.edu/~jdduda
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://gradsusr.org/pipermail/gradsusr/attachments/20110105/7b10e077/attachment-0003.html 


More information about the gradsusr mailing list