;+ ; NAME: ; reorder_by_phase ; PURPOSE: (one line) ; reorder phase and associated array in increasing phase ; DESCRIPTION: ; reorder phase and associated array in increasing phase ; CATEGORY: ; Util ; CALLING SEQUENCE: ; reorder_by_phase, pin, yin, pout, yout ; INPUTS: ; pin : input phase ; yin : input associated array ; OUTPUTS: ; pout : phase, between 0 and 2 pi, sorted ; yout : output associated array, sorted to match pout ; MODIFICATION HISTORY: ; Written 2012 Oct 16, by Leslie Young, SwRI ; 2016 Mar 24 LAY. Modified for inclusion in layoung library. ;- pro reorder_by_phase, pin, yin, pout, yout n = n_elements(pin) pmin = min(pin) ; p from 0 to 2 !pi offset = 2 * !pi * floor(pmin / (2*!pi) + 1) p = (pin + offset) mod (2*!pi) ; find the phase where crosses 0 indx = where(shift(p,-1)-p lt 0, nneg) if nneg gt 0 then begin if indx[0] eq n-1 then begin pout = p yout = yin endif else begin pout = [p[indx[0]+1:n-1],p[0:indx[0]]] yout = [yin[indx[0]+1:n-1],yin[0:indx[0]]] endelse endif else begin pout = p yout = yin endelse end