# [ncl-talk] Count Unique Values

Alan Brammer abrammer at albany.edu
Fri Nov 21 08:22:57 MST 2014

```Hey Gus,

There was talk on here a while back about a unique function.  Anne Melsom posted some code I believe. May help to search back if desired.  My function isn’t as elegant as that one but it does work on strings, where as the other was numeric based.

I have my own unique function written which I posted below.  You could then run back through that unique array to add a count to it.

;; Unique function : Written by Alan Brammer 2013
;; Accepts numeric or string array and simple loop through to build new array
undef("unique")
function unique(x[*])
local x1, x2, ct,i,nm
begin
x1 = x
x2 = new( dimsizes(x1), typeof(x1))
do i=0, dimsizes(x1)-1
if( .not.any( x2.eq.x1(i) ) )
x2(i) = x1(i)
end if
end do

nm = ind(.not.ismissing(x2))
if(all(ismissing(nm)))
print("Fail; Whole Array is missing for some reason")
return(False)
end if

return(x2(nm))
end

;;;;;
;;; the below may work, typed out and untested in an email client.
;;;;;

dates = (/ array of dates /)
date_unique = unique(dates)
date_count = new( dimsizes(date_unique), integer)

do i=0, dimsizes(date_unique)-1
date_count(i)  = num( dates .eq. date_unique(i) )
end do

Good Luck,
Alan.
--
Alan Brammer
PhD Candidate — African Easterly Waves and TC genesis
University At Albany, NY
www.atmos.albany.edu/student/abrammer

From: Ghassan J. Alaka, Jr. <gusalaka at atmos.colostate.edu>
Reply: Ghassan J. Alaka, Jr. <gusalaka at atmos.colostate.edu>>
Date: November 21, 2014 at 08:57:52
To: ncl-talk at ucar.edu <ncl-talk at ucar.edu>>
Subject:  [ncl-talk] Count Unique Values

Good morning,

I have an array of time stamps, for example:

"2014070312"
"2014070312"
"2014070400"
"2014070400"
"2014070400"
"2014070412"
"2014070412"
"2014070412"
"2014070412"
"2014070500"

I'd like to know if there is a simple way (or built-in NCL function) that will allow me to count the number of unique values in the above array. In this example, I would like the count to be "4".

Hopefully there is an easy solution!

Best,
Gus Alaka

--
Ghassan J. Alaka, Jr., Ph.D. Candidate
Department of Atmospheric Science
1371 Campus Delivery
Fort Collins, CO 80523-1371

E: gusalaka at atmos.colostate.edu
P: (970) 491-1487
Web: http://maloney.atmos.colostate.edu/galaka/index.php
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20141121/6fddba1f/attachment.html
```