Thanks Jonathan and Dave for your help.<div style="font-size: 12pt; font-family: Arial;"><p><br></p><p style="font-size: 12pt; font-family: Arial;">I think I've found a solution to my problem. The function below computes the percentile given by the user. Nothing fancy, but it serves the purpose.</p><div style="font-size: 12pt; font-family: Arial;"><p><br></p><p><span style="font-size: 12pt;">undef("computePercentile")</span></p><p><span style="font-size: 12pt;">function computePercentile (x:numeric, perc:numeric)</span></p><p><span style="font-size: 12pt;">; Compute custom percentile (%) from 2-D field (adapted from the function stat_dispersion)</span></p><p><span style="font-size: 12pt;">;</span></p><p><span style="font-size: 12pt;">local statx, work, nwork, numwork</span></p><p><span style="font-size: 12pt;">begin</span></p><p><span style="font-size: 12pt;">if (.not.all(ismissing(x))) then</span></p><p><span style="font-size: 12pt;"> work = ndtooned(x)</span></p><p><span style="font-size: 12pt;"> nwork = dimsizes(work) </span></p><p><span style="font-size: 12pt;"> qsort(work) ; reordered from lowest to highest</span></p><p><span style="font-size: 12pt;"> numwork = dim_num(.not.ismissing(work))</span></p><p><span style="font-size: 12pt;"> statx = work( toint((perc/100.)*numwork-1) )</span></p><p><span style="font-size: 12pt;">else</span></p><p><span style="font-size: 12pt;"> statx = x@_FillValue</span></p><p><span style="font-size: 12pt;">end if </span></p><p><span style="font-size: 12pt;">return(statx)</span></p><p><span style="font-size: 12pt;">delete(work)</span></p><p><span style="font-size: 12pt;">delete(nwork)</span></p><p><span style="font-size: 12pt;">delete(numwork)</span></p><p><span style="font-size: 12pt; font-family: Arial;">end</span></p><br>Cheers,</div><div style="font-size: 12pt; font-family: Arial;"><p style="font-size: 12pt; font-family: Arial;">Giorgio</p><p style="font-size: 12pt; font-family: Arial;">                  </p><blockquote><br>        <br>   ------ Messaggio Originale ------<br>   Da: dave.allured@noaa.gov<br>   A: g.graffino@tim.it<br>Cc: ncl-talk@ucar.edu<br>   Inviato: martedì 15 marzo 2022 16:50<br>   Oggetto: Re: [ncl-talk] Calculate percentile<br>  <br>        <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"><span class="wt_Email">ncl-talk@mailman.ucar.edu</span><span></span></a>> wrote:<br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex; --darkreader-inline-border-left:#3e4446;" data-darkreader-inline-border-left="">    <div style=""> 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=""> <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> <div>On Mar 15, 2022, at 13:55, Buzan, Jonathan via ncl-talk <<a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank"><span class="wt_Email">ncl-talk@mailman.ucar.edu</span><span></span></a>> wrote:</div> <br> <div> <div style=""> 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=""></span>z = data(:,k,j)</div> <span style=""></span>work = ndtooned(z) <div><span style=""></span>nwork = dimsizes(work) <br> <div><span style=""></span>qsort(work) ; reordered from lowest to highest<br> <div><span style=""></span>mastervar(j,k) = work( min((/nwork-1, nwork*70/100-1 /)) ) ; 70% </div> <div><span style=""></span>delete(z)</div> <div><span style=""></span>delete(work)</div> <div><span style=""></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> <div>On Mar 15, 2022, at 13:25, Giorgio Graffino via ncl-talk <<a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank"><span class="wt_Email">ncl-talk@mailman.ucar.edu</span><span></span></a>> wrote:</div> <div> <p style="font-size: 12.0pt;font-family: Arial;">Dear NCLers,</p> <p style="font-size: 12.0pt;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: 12.0pt;font-family: Arial;">I'm trying to adapt Dennis's suggestion </span><span style="font-family: arial , sans-serif;font-size: 12.0pt;">given here </span><span style="font-size: 12.0pt;">(</span><span style="font-size: 12.0pt;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: 12.0pt;">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: 12.0pt;">at the end of the time period, </span><span style="font-family: arial , sans-serif;font-size: 12.0pt;">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: 12.0pt;"><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: 12.0pt;">),  but for a custom percentile and without detrending the data. </span><span style="font-family: arial , sans-serif;font-size: 12.0pt;">Is there a way to do that? </span><span style="font-family: arial , sans-serif;font-size: 12.0pt;">Here is a snippet of my code:</span></p> <p style="font-size: 12.0pt;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: 12.0pt;"><br> </span></p> <p style="font-size: 12.0pt;font-family: Arial;">temp_num = dim_num_n(.not.ismissing(temp_sort),0) ; (time, lat, lon) -> (lat, lon)</p> <p style="font-size: 12.0pt;font-family: Arial;">do n = 0, ntim-1</p> <p style="font-size: 12.0pt;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: 12.0pt;font-family: Arial;"><span style="font-size: 12.0pt;font-family: Arial;">end do</span></p> <p style="font-size: 12.0pt;font-family: Arial;"><span style="font-family: arial , sans-serif;font-size: 12.0pt;">Thanks,</span></p> <p style="font-size: 12.0pt;font-family: Arial;"><span style="font-family: arial , sans-serif;font-size: 12.0pt;">Giorgio</span></p> _______________________________________________<br> ncl-talk mailing list<br> <a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank"><span class="wt_Email">ncl-talk@mailman.ucar.edu</span><span></span></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> <br>        </blockquote><br>        <br>        </div></div>