# [ncl-talk] optimization problem

Marston Johnston shejo284 at gmail.com
Tue Jul 26 06:05:04 MDT 2016

Hi,

I'm wondering if there is a way to speed up the do loops in the code below?
I'm not sure it can be done, but I'm not an expert.

;***** 2D histogram ******
; x and y axis
darr = ispan(-80,80,1)
sarr = new(dimsizes(darr),string)
sarr = " "
char = ispan(150,300,1)

; histogram bins
hist5m4 = new((/dimsizes(darr),dimsizes(char)/),float)
hist5m4 at _FillValue = default_fillvalue("float")
hist5m4!0 = "diff"
hist5m4!1 = "Tb"
hist5m4&diff = darr
hist5m4&Tb = char
ndarr = dimsizes(darr)
nchar = dimsizes(char)
hist5m4 = 0.0
hist5m3 = hist5m4
hist5m3 = 0.0
hist4m3 = hist5m4
hist4m3 = 0.0

tb5m4 = mhs(4,:,:) - mhs(3,:,:)
copy_VarMeta(mhs(3,:,:),tb5m4)
tb5m3 = mhs(4,:,:) - mhs(2,:,:)
copy_VarMeta(mhs(2,:,:),tb5m3)
tb4m3 = mhs(3,:,:) - mhs(2,:,:)
copy_VarMeta(mhs(2,:,:),tb5m3)

mhs5 := round(ndtooned(mhs(4,:,:)),0)
mhs4 := round(ndtooned(mhs(3,:,:)),0)
mhs3 := round(ndtooned(mhs(2,:,:)),0)

tb5m4 := round(ndtooned(tb5m4),0)
tb5m3 := round(ndtooned(tb5m3),0)
tb4m3 := round(ndtooned(tb4m3),0)

; print(darr(y)+" <= DARR:CH5AR => "+ch5ar(x))
; Fill the bins
do x = 0, nchar - 1
do y = 0, ndarr - 1
tmp := ind(mhs5.eq.char(x) .and. tb5m4.eq.darr(y))
if(dimsizes(tmp).gt.1) then
hist5m4(y,x) = hist5m4(y,x) + (dimsizes(tmp) - 1)
end if
tmp := ind(mhs5.eq.char(x) .and. tb5m3.eq.darr(y))
if(dimsizes(tmp).gt.1) then
hist5m3(y,x) = hist5m3(y,x) + (dimsizes(tmp) - 1)
end if
tmp := ind(mhs4.eq.char(x) .and. tb4m3.eq.darr(y))
if(dimsizes(tmp).gt.1) then
hist4m3(y,x) = hist4m3(y,x) + (dimsizes(tmp) - 1)
end if
end do
end do

/M
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Marston S. Johnston, PhD
Department of Earth Sciences
University of Gothenburg, Sweden
Email: marston.johnston at gu.se
Phone: +46-31-7864901
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Only the fruitful thing is true!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20160726/5dba7318/attachment.html