[ncl-talk] Using NCL function get_unique_values, but maintain original array order

Alan Brammer abrammer at albany.edu
Wed May 31 08:05:23 MDT 2017


Hi Alan, 

I made some edits to the hidden NCL function which does the work for get_unique_values(x) to both speed the function up and also to implement Hoffman Cheungs idea about assigning an index to unsort the unique array.   You can load this at the top of your script and it will replace the official functions,  get_unique_values(x) will be faster and I added a get_unique_values_nosort(x) same use as the original but it will return an unsorted array of unique values (i.e. the first instance of each unique value).  

I’m not a developer, so this is not an official function and may likely be changed if incorporated in future releases.  That said any problems let me know. 


Alan.








##############################
Alan Brammer,
Post-Doc Researcher

Department of Atmospheric and Environmental Sciences,
University at Albany, State University of New York, Albany, NY, 12222
abrammer at albany.edu
##############################

> On 27 May 2017, at 08:05, Hoffman Cheung <hoffmancheung at gmail.com> wrote:
> 
> Hi Mary,
> 
> If one wants to get the indices of an unsorted array after sorting by qsort, one can manually assign a unique index for the array before reordering.
> 
> For example,
> 
> a = (/6,8,10,2,4/)
> a!0 = "index"
> a&index = (/0,1,2,3,4/)
> 
> qsort(a)
> 
> print(a + " " + a&index) 
> 
> The result is: 
> 2 3
> 4 4 
> 6 0
> 8 1 
> 10 2
> 
> Is it possible to modify the function get_unique_index in such a way that it can also return the associated coordinate variable?
> If it is possible, this would be easy to get the index of the unsorted array (for 1-D and multiple-dimensional array).
> 
> Best Regards,
> Hoffman
> 
> 
> 
> 
> 2017-05-26 18:18 GMT+02:00 Mary Haley <haley at ucar.edu <mailto:haley at ucar.edu>>:
> Hi Alan,
> 
> That's a good question, and I'm afraid we don't have a function to do that. The problem is that the function has to sort the values in order to effectively find the unique elements, and it doesn't bother unsorting them when done. This would add a potentially expensive extra step that people may not want. However, I do see the value of not sorting the values.
> 
> I've attached a function that unsorts the values after you call get_unique_values. If you have a lot of values, this may add noticeable time to execute. Also, this will currently only work on a 1D array.
> 
> I created a ticket for this, in case we get more requests for this and need to create a more elegant solution (NCL-2618).
> 
> --Mary
> 
> 
> 
> 
> On Thu, May 25, 2017 at 11:33 AM, Alan Rhoades <alan.m.rhoades at gmail.com <mailto:alan.m.rhoades at gmail.com>> wrote:
> Hello,
> 
> I'm currently using this nifty NCL function get_unique_values to sort through an array and extract unique strings and/or floats from in-situ station data.
> 
> My problem is that the extracted values are then sorted alphabetically and/or lowest to highest.  
> 
> Is it possible to use this function (or a similar) and maintain the ordering from the original array which the strings/floats were extracted from?
> 
> Best,
> 
> AR
> 
> -- 
> 
> Alan Rhoades, Ph.D. | Lawrence Berkeley National Laboratories
> Hydroclimate Uncertainty Postdoctoral Fellow | Project Hyperion <http://climate.ucdavis.edu/hyperion/>
> arhoades at lbl.gov <mailto:arhoades at lbl.gov> | alan.m.rhoades at gmail.com <mailto:alan.m.rhoades at gmail.com>
> Website <http://alanrhoades.weebly.com/> | Research Gate <https://www.researchgate.net/profile/Alan_Rhoades> | Google Scholar <https://scholar.google.gr/citations?user=AVFLiFsAAAAJ&hl=en> | LinkedIn <https://www.linkedin.com/pub/alan-rhoades/22/5bb/52a> 
>  <https://scholar.google.gr/citations?user=AVFLiFsAAAAJ&hl=en>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk <http://mailman.ucar.edu/mailman/listinfo/ncl-talk>
> 
> 
> 
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk <http://mailman.ucar.edu/mailman/listinfo/ncl-talk>
> 
> 
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> 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/20170531/fec08062/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: unique.ncl
Type: application/octet-stream
Size: 3696 bytes
Desc: not available
Url : http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170531/fec08062/attachment.obj 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170531/fec08062/attachment-0001.html 


More information about the ncl-talk mailing list