Yes, I see the problem.
NCL uses "pass-by-reference" for arguments to functions
and procedures. Hence, if you change the value of an argument
inside a function or procedure
*the changed value will be returned ... just like fortran!*
For NCL's *built-in* functions, we *never* change the value of
an input argument. I recommend that all users do the same thing.
If necessary, use temporary variables within a function to
preserve the original values.
Cheers
D
----
function TEST (t)
begin
t = t - 273.15 ; change the argument's value
tc = t ; tc AND t will change
return (tc)
end
begin
T = 300.
TT = TEST( T ) ; T keeps changing
print("T="+T+" TT="+TT)
print("T="+T+" TT="+TT)
Mateus da Silva Teixeira wrote:
>> Mateus da Silva Teixeira wrote:
>>
>>> Dear NCL users,
>>>
>>> I'm having a strange behavior of mixhum_ptrh function and my
>>> user-defined function. Both functions give the specific humidity given
>>> temperature, pressure and relative humidity. I printed their results to
>>> compare them, but I'm getting this:
>>>
>>> ncl 1> p = 1000.
>>> ncl 2> tk = 18. + 273.15
>>> ncl 3> rh = 46.5
>>> ncl 4> print(qSpec(tk,rh,p)+" "+mixhum_ptrh (p, tk, rh, 2))
>>> (0) 5.96583e-05 -0.00771059
>>> ncl 5> print(qSpec(tk,rh,p)+" "+mixhum_ptrh (p, tk, rh, 2))
>>> (0) nan -7.65217e-05
>>> ncl 6> print(qSpec(tk,rh,p)+" "+mixhum_ptrh (p, tk, rh, 2))
>>> (0) -1.73974 -7.72574e-07
>>> ncl 7> print(qSpec(tk,rh,p)+" "+mixhum_ptrh (p, tk, rh, 2))
>>> (0) 1.86416e-06 8.26161e-10
>>>
>>> As you can see, the values isn't the same in successive calls.
>>> I'm using NCL 4.3.0.
>>>
>>> Do someone already seen this?
>>>
>>> Thanks,
>>>
>>> Mateus
>>>
>>>
>>
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.5 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFGgWbitBm/Y0ahARYRAl5zAJ4z23KzCFd3djp/xhDDHnVhKdD7QQCfQS39
> 8esddxboiW2JnyZsnJE2D38=
> =H/Bd
> -----END PGP SIGNATURE-----
>
> ------------------------------------------------------------------------
>
> undef("qSpec")
> ; function that returns specific humidity given
> ; air temperature (Kelvin), relative humidity (%)
> ; and pressure (hPa).
> ;
> ; Mateus Teixeira, jun/2007
> function qSpec(T:float,RH:float,p:float)
> local c0,A,B,epsilon,es,e,r,q
> begin
> ; constants used
> ; c0, A e B were taken from (1994), pag. 117.
> ; it's valid in the range
> ; -35C <= T <= 35C
> c0 = 6.112
> A = 17.67
> B = 243.5
> epsilon = 0.622 ; Rd / Rv
>
> ; some necessary transformations
> T = T-273.15 ; Kelvin -> Celsius
> p = p*100.0 ; hPa -> Pa
> RH = RH/100.0
>
> ; equilibrium (saturation) vapor pressure
> es = c0*exp(A*T/(T+B))
> ; vapor pressure
> e = RH*es
> ; mix ratio
> r = e*epsilon/(p-e)
> ; specific humidity
> q = r/(1.+r)
>
> return(q)
> end
>
--
======================================================
Dennis J. Shea tel: 303-497-1361 |
P.O. Box 3000 fax: 303-497-1333 |
Climate Analysis Section |
Climate & Global Dynamics Div. |
National Center for Atmospheric Research |
Boulder, CO 80307 |
USA email: shea 'at' ucar.edu |
======================================================
_______________________________________________
ncl-talk mailing list
ncl-talk_at_ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Tue Jun 26 2007 - 15:03:40 MDT
This archive was generated by hypermail 2.2.0 : Wed Jun 27 2007 - 09:47:23 MDT