;+ ; 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, phasein, yin, phaseout, yout ; INPUTS: ; phasein : input phase ; yin : input associated array ; OUTPUTS: ; phaseout : 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, phasein, yin, phaseout, yout n = n_elements(phasein) pmin = min(phasein) ; p from 0 to 2 !pi offset = 2 * !pi * floor(pmin / (2*!pi) + 1) phase = (phasein + offset) mod (2*!pi) ; find the phase where crosses 0 indx = where(shift(phase,-1)-phase lt 0, nneg) if nneg gt 0 then begin if indx[0] eq n-1 then begin phaseout = phase yout = yin endif else begin phaseout = [phase[indx[0]+1:n-1],phase[0:indx[0]]] yout = [yin[indx[0]+1:n-1],yin[0:indx[0]]] endelse endif else begin phaseout = phase yout = yin endelse end