<div dir="ltr"><div dir="ltr">Georgio, you said you want to compute the 70th percentile month by month.  I recommend a single outer loop over time, and do all the percentile calculations independently inside the time loop.  Sort the data for only one month at a time, excluding missing values as needed; get the 70th percent index within that month; and that gives you the 70th percentile for the current month.<br></div><div dir="ltr"><br></div><div>As Jonathan said, you need to use ndtooned() to reshape the input data to 1-D for input to NCL's sort routines.  Do this independently for each month, inside the time loop.</div><div><br></div><div>A long time ago, NCL's dim_pqsort function was faster than the qsort function, due to internal coding.  If your data set is large, you might want to check on which function is currently faster.</div><div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Mar 15, 2022 at 7:02 AM Buzan, Jonathan via ncl-talk <<a href="mailto:ncl-talk@mailman.ucar.edu">ncl-talk@mailman.ucar.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">



<div style="word-wrap:break-word;line-break:after-white-space">
Watchout, there’s a typo. The indexes need to match the lat and lon.
<div><br>
</div>
<div>mastervar(j,k) = work( min((/nwork-1, nwork*70/100-1 /)) ) ; 70% 
<div>
<div>
<div style="word-wrap:break-word;line-break:after-white-space">
<div>
<div>
<div>Change to:</div>
<div>mastervar(k,j) = work( min((/nwork-1, nwork*70/100-1 /)) ) ; 70% </div>
</div>
</div>
</div>
</div>
<div><br>
<blockquote type="cite">
<div>On Mar 15, 2022, at 13:55, Buzan, Jonathan via ncl-talk <<a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank">ncl-talk@mailman.ucar.edu</a>> wrote:</div>
<br>
<div>
<div style="word-wrap:break-word;line-break:after-white-space">
Hi Giorgio,
<div><br>
</div>
<div>The code for the stat_dispersion is in $NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl</div>
<div>(Search for stat_dispersion for the explicit function)</div>
<div><br>
</div>
<div>Here are some snippets to help. Ultimately, the stat_dispersion operates on 1d fields, and you have to put your data into that format. Highly recommend that you have more than 100 data points in your time dimension or the function will not work.</div>
<div><br>
</div>
<div>mastervar = ; new array that is the stat dispersion by lat by lon</div>
<div>do k = 0, dimsizes(lat)-1</div>
<div>do j = 0, dimsizes(lon)-1</div>
<div><span style="white-space:pre-wrap"></span>z = data(:,k,j)</div>
<span style="white-space:pre-wrap"></span>work = ndtooned(z)
<div><span style="white-space:pre-wrap"></span>nwork = dimsizes(work) <br>
<div><span style="white-space:pre-wrap"></span>qsort(work) ; reordered from lowest to highest<br>
<div><span style="white-space:pre-wrap"></span>mastervar(j,k) = work( min((/nwork-1, nwork*70/100-1 /)) ) ; 70% </div>
<div><span style="white-space:pre-wrap"></span>delete(z)</div>
<div><span style="white-space:pre-wrap"></span>delete(work)</div>
<div><span style="white-space:pre-wrap"></span>delete(nwork)</div>
<div>
<div>end do</div>
<div>
<div>end do</div>
</div>
<div><br></div>
<div>Cheers,</div>
<div>-Jonathan</div>
<div><br></div>
</div>
<div>
<blockquote type="cite">
<div>On Mar 15, 2022, at 13:25, Giorgio Graffino via ncl-talk <<a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank">ncl-talk@mailman.ucar.edu</a>> wrote:</div>
<div>
<p style="font-size:12pt;font-family:Arial">Dear NCLers,</p>
<p style="font-size:12pt;font-family:Arial">I have a time,lat,lon monthly-mean SST field and I want to compute the 70th percentile month by month.
<span style="font-size:12pt;font-family:Arial">I'm trying to adapt Dennis's suggestion
</span><span style="font-family:arial,sans-serif;font-size:12pt">given here
</span><span style="font-size:12pt">(</span><span style="font-size:12pt;font-family:arial,sans-serif"><a href="https://www.ncl.ucar.edu/Support/talk_archives/2013/0954.html" target="_blank">https://www.ncl.ucar.edu/Support/talk_archives/2013/0954.html</a>),
 but I'm ending up with a single 2-D percentile field, calculated across the entire time period.
</span><span style="font-family:arial,sans-serif;font-size:12pt">The problem is that, since it's historical SST, most of the spatial domain is above the threshold
</span><span style="font-family:arial,sans-serif;font-size:12pt">at the end of the time period,
</span><span style="font-family:arial,sans-serif;font-size:12pt">because of global warming.
<br>
<br>
I'd like to have something similar to stat_dispersion (</span><span style="font-family:arial,sans-serif;font-size:12pt"><a href="https://www.ncl.ucar.edu/Document/Functions/Contributed/stat_dispersion.shtml" target="_blank">https://www.ncl.ucar.edu/Document/Functions/Contributed/stat_dispersion.shtml</a></span><span style="font-family:arial,sans-serif;font-size:12pt">),
 but for a custom percentile and without detrending the data. </span><span style="font-family:arial,sans-serif;font-size:12pt">Is there a way to do that?
</span><span style="font-family:arial,sans-serif;font-size:12pt">Here is a snippet of my code:</span></p>
<p style="font-size:12pt;font-family:Arial">temp_sort = dim_pqsort_n(temp,2,0) ; (time, lat, lon) -> (time, lat, lon)<span style="font-family:arial,sans-serif;font-size:12pt"><br>
</span></p>
<p style="font-size:12pt;font-family:Arial">temp_num = dim_num_n(.not.ismissing(temp_sort),0) ; (time, lat, lon) -> (lat, lon)</p>
<p style="font-size:12pt;font-family:Arial">do n = 0, ntim-1</p>
<p style="font-size:12pt;font-family:Arial">temp_warmest30(n,:,:) = where(temp_sort(n,:,:).gt.floor(0.7*temp_num),temp(n,:,:),temp@_FillValue) ; (time, lat, lon) -> (time, lat, lon)</p>
<p style="font-size:12pt;font-family:Arial"><span style="font-size:12pt;font-family:Arial">end do</span></p>
<p style="font-size:12pt;font-family:Arial"><span style="font-family:arial,sans-serif;font-size:12pt">Thanks,</span></p>
<p style="font-size:12pt;font-family:Arial"><span style="font-family:arial,sans-serif;font-size:12pt">Giorgio</span></p>
_______________________________________________<br>
ncl-talk mailing list<br>
<a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank">ncl-talk@mailman.ucar.edu</a><br>
List instructions, subscriber options, unsubscribe:<br>
<a href="https://mailman.ucar.edu/mailman/listinfo/ncl-talk" target="_blank">https://mailman.ucar.edu/mailman/listinfo/ncl-talk</a></div></blockquote></div></div></div></div></div></blockquote></div></div></div></div></blockquote></div></div>