Re: get_unique_values: Extracting unique values in an array

From: Carl Schreck <cjschrec_at_nyahnyahspammersnyahnyah>
Date: Mon Apr 14 2014 - 08:50:50 MDT

Hi Gabriel,

Thanks, this is a useful script, and something that's missing in NCL right
now. Below is the equivalent that I use, which may or may not be any
faster. The attached file also has a few other array functions that I find
useful from time to time.

    Carl

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Description: Take an array of data, sort it, remove duplicates, and
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
undef ( "remove_duplicates" )
function remove_duplicates( \
  i_data : numeric \
)
begin

  data = ndtooned( i_data )
  qsort(data)
  duplicate = new( dimsizes(data), logical )
  duplicate(0) = False
  do i = 1, dimsizes(data)-1
    duplicate(i) = ( data(i).eq.data(i-1) )
  end do

  uniqueInd = ind(.not.duplicate)
  retVal = data(uniqueInd)
  return(retVal)
end ; remove_duplicates

On Sun, Apr 13, 2014 at 6:38 PM, Gabriel Medeiros <gabriel.abrahao@ufv.br>wrote:

> 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
>
>

-- 
 <http://www.cicsnc.org/>
 <http://www.researcherid.com/rid/B-8711-2011>[image:
http://www.researcherid.com/rid/B-8711-2011]<http://www.researcherid.com/rid/B-8711-2011>
*Carl J. Schreck III, PhD*
*Research Associate*
Cooperative Institute for Climate and Satellites NC <http://cicsnc.org/>
North Carolina State University <http://ncsu.edu/>
NOAA's National Climatic Data Center <http://ncdc.noaa.gov/>
151 Patton Ave, Asheville, NC 28801
e: cjschrec@ncsu.edu
o: +1 828 257 3140
CycloneCenter.org: Help analyze tropical cyclones<http://cyclonecenter.org/>
monitor.cicsnc.org/mjo: See recent MJO
diagnostics<http://monitor.cicsnc.org/mjo/>


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

Received on Mon Apr 14 08:51:44 2014

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