<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body>
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Arial,Helvetica,sans-serif;" dir="ltr">
<p>Thanks Griff and Dennis - this is a useful function and the loop doesn't appear to cause problems for me on a reasonably large data.</p>
<p><br>
</p>
<p>While it is a rather 'specific' function as you say, I suggest it may be useful also to other NCL users. For example, those interested in examining a sequence of dry/wet days in the context of drought and retaining this information as an index.</p>
<p><br>
</p>
<p>Thanks again.</p>
<p>Peter<br>
</p>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Dennis Shea <shea@ucar.edu><br>
<b>Sent:</b> Thursday, March 9, 2017 2:03:11 AM<br>
<b>To:</b> Griffith Young<br>
<b>Cc:</b> Peter Gibson; ncl-talk@ucar.edu<br>
<b>Subject:</b> Re: [ncl-talk] running sequence of 0s and 1s [SEC=UNCLASSIFIED]</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div>Griff is correct that interpreted languages can be slow executing do loops. However, I always suggest doing a simple timing test like below. On my MAC, with, N=10000
<br>
gibson_young: N=10000: ===> 0.03022 seconds<br>
</div>
<div><br>
------------------------------------<br>
undef("gibson_young")<br>
function gibson_young(q[*]:numeric)<br>
begin<br>
nq = dimsizes(q)<br>
qsum = conform(q, 0, -1) ; initialize to 0<br>
<br>
do n=0,nq-1<br>
if (q(n).eq.0) then<br>
t = 0<br>
end if<br>
<br>
if (q(n).eq.1) then<br>
qsum(n) = t<br>
t = t + 1<br>
end if<br>
end do<br>
<br>
return(qsum)<br>
end<br>
;=====================================<br>
</div>
<div>; MAIN<br>
;=====================================<br>
</div>
<div>;<br>
; desired result: 0 0 0 0 1 0 0 1 2 3 4 0 0 1 2 0<br>
q = (/0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0/)<br>
qsum = gibson_young(q)<br>
print(q+" "+qsum)<br>
<br>
;========<br>
<br>
N = 10000<br>
x = random_uniform(-10,10,N)<br>
x = where(x.lt.0, 0, 1)<br>
<br>
tStrt = get_cpu_time()<br>
xsum = gibson_young(x)<br>
print("gibson_young: N="+N+ ": " + (get_cpu_time() - tStrt))<br>
<br>
;print(xsum)<br>
</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Tue, Mar 7, 2017 at 9:43 PM, Dennis Shea <span dir="ltr">
<<a href="mailto:shea@ucar.edu" target="_blank">shea@ucar.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">
<div>Never underestimate Aussie brute force! <br>
</div>
<div><br>
</div>
<div><br>
</div>
<div>==========<br>
undef("gibson_young")<br>
function gibson_young(q[*]:numeric)<br>
local nq, qsum, n, t <br>
begin<br>
nq = dimsizes(q)<br>
qsum = conform(q, 0, -1) ; initialize to 0<br>
<br>
do n=0,nq-1<br>
if (q(n).eq.0) then<span class=""><br>
t = 0<br>
end if<br>
<br>
</span> if (q(n).eq.1) then<br>
qsum(n) = t<span class=""><br>
t = t + 1<br>
end if<br>
end do<br>
<br>
</span> return(qsum)<br>
end<br>
;<br>
; desired result: 0 0 0 0 1 0 0 1 2 3 4 0 0 1 2 0<br>
q = (/0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0/)<br>
qsum = gibson_young(q)<br>
print(q+" "+qsum)<br>
<br>
===<br>
<br>
<br>
<br>
(0) 0 0<br>
(1) 0 0<br>
(2) 0 0<br>
(3) 1 0<br>
(4) 1 1<br>
(5) 0 0<br>
(6) 1 0<br>
(7) 1 1<br>
(8) 1 2<br>
(9) 1 3<br>
(10) 1 4<br>
(11) 0 0<br>
(12) 1 0<br>
(13) 1 1<br>
(14) 1 2<br>
<br>
</div>
</div>
<div class="HOEnZb">
<div class="h5">
<div class="gmail_extra"><br>
<div class="gmail_quote">On Tue, Mar 7, 2017 at 9:19 PM, Griffith Young <span dir="ltr">
<<a href="mailto:griffith.young@bom.gov.au" target="_blank">griffith.young@bom.gov.au</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div link="blue" vlink="purple" lang="EN-AU">
<div class="m_4144413541458315111m_5748270439688318660WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Hello Peter,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Not if you go to the casino and are doubling up your bet.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">This code seems to work...<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">v = {vector or array of 1's and 0's}<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">c = v<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">t = 0<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">do i = 0, dimsizes(v) - 1<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> if v(i) .eq. 0 then<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> c(i) = 0<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> t = 0<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> end if<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> if (v(i) .eq. 1) then<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> c(i) = t<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> t = t + 1<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> end if<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">end do<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Caveat: "Since NCL is an interpreted language, it is best to avoid do loops as much as possible. They can cause considerable slow downs. Small loops should
not be a problem."<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Regards, Griff.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"" lang="EN-US">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"" lang="EN-US">
<a href="mailto:ncl-talk-bounces@ucar.edu" target="_blank">ncl-talk-bounces@ucar.edu</a> [mailto:<a href="mailto:ncl-talk-bounces@ucar.edu" target="_blank">ncl-talk-bounces@ucar.<wbr>edu</a>]
<b>On Behalf Of </b>Dennis Shea<br>
<b>Sent:</b> Wednesday, 8 March 2017 2:59 PM<br>
<b>To:</b> Peter Gibson<br>
<b>Cc:</b> <a href="mailto:ncl-talk@ucar.edu" target="_blank">ncl-talk@ucar.edu</a><br>
<b>Subject:</b> Re: [ncl-talk] running sequence of 0s and 1s<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">Sorry, no. Pretty specialized function ...<u></u><u></u></p>
</div>
<p class="MsoNormal">: 0 0 0 1 1 0 1 1 1 1 1 0 1 1 1 0 ... input: 1st 1 is a flag<u></u><u></u></p>
<div>
<p class="MsoNormal">: 0 0 0 0 1 0 0 1 2 3 4 0 0 1 2 0<u></u><u></u></p>
</div>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">On Tue, Mar 7, 2017 at 8:17 PM, Peter Gibson <<a href="mailto:peter.gibson@unsw.edu.au" target="_blank">peter.gibson@unsw.edu.au</a>> wrote:<u></u><u></u></p>
<div>
<div id="m_4144413541458315111m_5748270439688318660m_-7703457688712157601divtagdefaultwrapper">
<div>
<p class="MsoNormal"><span style="font-family:"Calibri","sans-serif";color:black">Hello,<br>
<br>
Is there a function to calculate the running length of 0/1s in a sequence in NCL?<br>
<br>
for example if I had a vector v : 0 0 0 1 1 0 1 1 1 1 1 0 1 1 1 0 ...<br>
the running sequence would be : 0 0 0 0 1 0 0 1 2 3 4 0 0 1 2 0 ...<br>
<br>
I see the function dim_numrun counts the number of unique sequence lengths which is similar but not exactly what I am after .... <u></u><u></u></span></p>
</div>
<p><span style="font-family:"Calibri","sans-serif";color:black"><u></u> <u></u></span></p>
<p><span style="font-family:"Calibri","sans-serif";color:black">Thanks,<u></u><u></u></span></p>
<p><span style="font-family:"Calibri","sans-serif";color:black">Peter<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
______________________________<wbr>_________________<br>
ncl-talk mailing list<br>
<a href="mailto:ncl-talk@ucar.edu" target="_blank">ncl-talk@ucar.edu</a><br>
List instructions, subscriber options, unsubscribe:<br>
<a href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk" target="_blank">http://mailman.ucar.edu/mailma<wbr>n/listinfo/ncl-talk</a><u></u><u></u></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</body>
</html>