get_unique_values: Extracting unique values in an array

From: Gabriel Medeiros <gabriel.abrahao_at_nyahnyahspammersnyahnyah>
Date: Sun Apr 13 2014 - 16:38:06 MDT

Hello,

I have seen a couple of threads on how to get all the unique values in an
array, such as a discrete variable. This is a little clumsy and processor
consuming function but works, so i thought about sharing with you. The
attached file can be loaded or just add this to your script:

; Gabriel Abrahao - gabriel.abrahao@ufv.br
; Gets all the unique values that occur in var
undef("get_unique_values")
function get_unique_values(var)
begin
var1d = ndtooned(var)
printVarSummary(var1d)
levels = new(1,typeof(var1d))
do i=0,dimsizes(var1d)-1
        if (ismissing(levels(0)) .eq. True) then
                if (ismissing(var1d(i)) .ne. True) then
                        levels(0) = var1d(i)
                end if
        else
                if (any(levels.eq.var1d(i)) .or.
ismissing(var1d(i)).eq.True) then
                else
                        dummy = new(dimsizes(levels) + 1, typeof(levels))
                        dummy(0:dimsizes(levels)-1) = levels
                        dummy(dimsizes(dummy)-1) = var1d(i)
                        delete(levels)
                        levels = dummy
                        delete(dummy)
                end if
        end if
end do

return(levels)
end

Hope it's useful, and please let me know if you can improve it,

Gabriel Abrahao
Grupo de pesquisa em Interação Atmosfera - Biosfera
Universidade Federal de Viçosa

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk

Received on Sun Apr 13 16:38:20 2014

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