Re: "sign" function

From: Kyle Griffin <ksgriffin2_at_nyahnyahspammersnyahnyah>
Date: Sun Apr 06 2014 - 11:52:52 MDT

You might try something like this...

Given that x = (/time, level, lat, lon/) as you specified in your email -
or honestly, any dimension should work fine

signx = where(x.eq.0,x@_FillValue,x)
signx = x/abs(x)
signx = where(ismissing(signx),0,x)
signx = where(ismissing(x),x@_FillValue,signx)

The middle line alone would work if you knew none of your values were
exactly zero, but would cause divide-by-zero errors if there were zero
values. Since I believe the where function evaluates the entirety of both
the 'true' and 'false' arrays, you would run into the same problem with
doing a x/abs(x) in the where function if you don't set all the zero values
to missing first. This also has a problem, though, in that it will end up
setting all missing values to '0' sign in the third line, but we can fix
that with the fourth line and simply copy the missing value locations out
of the original data.

I haven't tested these lines of code, but I think they should get the job
done.

Let the list know how this works out!

Kyle

----------------------------------------
Kyle S. Griffin
Department of Atmospheric and Oceanic Sciences
University of Wisconsin - Madison
Room 1421
1225 W Dayton St, Madison, WI 53706
Email: ksgriffin2@wisc.edu

On Sun, Apr 6, 2014 at 9:59 AM, Verena Lili <verena.prick@gmail.com> wrote:

> The problem is following:
>
>
> ncl 0>
> ncl 1> function sign( X )
> ncl 2> begin
> ncl 3> if X.eq.0 then
> ncl 4> result = 0
> ncl 5> else
> ncl 6> if X.gt.0 then
> ncl 7> result = 1
> ncl 8> else
> ncl 9> result = -1
> ncl 10> end if
> ncl 11> end if
> ncl 12> return( result )
> ncl 13> end
> ncl 14>
> ncl 15> a=(/-2,-3,-4,4,5,6-1/)
> ncl 16>
> ncl 17>* b=(a^2)*sign(a)*
> *fatal:Conditional statements (if and do while) require SCALAR logical
> values, see all and any functions*
> *fatal:Execute: Error occurred at or near line 11*
>
> *fatal:Execute: Error occurred at or near line 17*
>
> Any subroutine that can work as global "sign" function in ncl and can be
> used for multidimensional arrays? e.g.
>
> X= [time, level, lat, lon]
>
> y= (X^2) * sign (X)
>
> Thank you.
>
>
>
> On Thu, Apr 3, 2014 at 3:30 AM, Verena Lili <verena.prick@gmail.com>wrote:
>
>> Thanks Brian!
>>
>>
>> On Wed, Apr 2, 2014 at 6:50 PM, Brian Medeiros <brianpm@ucar.edu> wrote:
>>
>>> You could write that function:
>>>
>>> function sign( X )
>>> begin
>>> if X.eq.0 then
>>> result = 0
>>> else
>>> if X.gt.0 then
>>> result = 1
>>> else
>>> result = -1
>>> end if
>>> end if
>>> return( result )
>>> end
>>>
>>> .brian
>>>
>>>
>>> On Apr 2, 2014, at 10:40 AM, Verena Lili <verena.prick@gmail.com> wrote:
>>>
>>> Hi NCL,
>>>
>>> Is there any function in NCL similar like `"*sign" *function in Matlab?
>>>
>>> --
>>> Regards,
>>> Dr. Verena.
>>> School of Marine and Atmospheric Sciences
>>> Stony Brook University
>>> homepage: http://www.somas.stonybrook.edu/
>>> _______________________________________________
>>> ncl-talk mailing list
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>
>>>
>>>
>>
>>
>> --
>> Regards,
>> Dr. Verena.
>> School of Marine and Atmospheric Sciences
>> Stony Brook University
>> homepage: http://www.somas.stonybrook.edu/
>>
>
>
>
> --
> Regards,
> Dr. Verena.
> School of Marine and Atmospheric Sciences
> Stony Brook University
> homepage: http://www.somas.stonybrook.edu/
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Sun Apr 06 11:53:25 2014

This archive was generated by hypermail 2.1.8 : Tue Apr 15 2014 - 10:45:19 MDT