FUNCTION AngleRange, A, pi=pi, monotonic=monotonic, degrees=degrees ;+ ; NAME: ; AngleRange ; PURPOSE: ; Map angles into the range [0,360) or (-180,+180] ; CATEGORY: ; sat/idl/toolbox/math ; CALLING SEQUENCE: ; R = AngleRange(A, [/pi, /degrees]) ; INPUTS: ; Angles scalar or array; type: any ; angles ; OPTIONAL INPUT PARAMETERS: ; /pi by default the angles are mapped to [0,360) ; if /pi is set then the angles are mapped to (-180,+180] ; /degrees if set then in- and output angles are in degrees (default: radians) ; OUTPUTS: ; R same size and type as input array ; angles mapped to specified range ; INCLUDE: @compile_opt.pro ; On error, return to caller ; CALLS: ; InitVar, ToRadians, IsType, AngleRange ; PROCEDURE: ; The output interval is open on the right side for mapping to [0,360) ; (i.e. 360 is not included). ; The output interval is open on the left side for mapping to (-180,+180] ; (i.e. -180 is not included) ; MODIFICATION HISTORY: ; MAR-2000, Paul Hick (UCSD/CASS; pphick@ucsd.edu) ;- InitVar, pi, /key rpd = ToRadians(degrees=degrees) onepi = !dpi IF NOT IsType(A, /double) THEN BEGIN rpd = float(rpd ) onepi = float(onepi) ENDIF twopi = 2*onepi B = A*rpd ; Convert to radians B = B mod twopi ; To range (-2*pi,2*pi) CASE pi OF 0: B = B+twopi*(B LT 0) ; To range [0,2*pi) 1: B = B+twopi*(fix(B LE -onepi)-fix(B GT onepi)) ; To range (-pi,pi] ENDCASE IF IsType(monotonic,/defined) THEN BEGIN sz = size(B) nn = sz[sz[0]+2] ; # elements IF nn LE 1 THEN BEGIN ; do nothing ENDIF ELSE IF sz[0] EQ 1 THEN BEGIN ; 1-dim array dB = (B-shift(B,1))[1:*] ; Difference with next neighbour dB = AngleRange(dB) ;FOR i=0,sz[1]-2 do B[i+1] = B[i]+dB[i] FOR i=monotonic,sz[1]-2 DO B[i+1] = B[i]+dB[i] FOR i=monotonic,1,-1 DO B[i-1] = B[i]-dB[i-1] ENDIF ELSE IF sz[0] GT 1 THEN BEGIN ; Multi-dim array B = reform(B, sz[1], nn/sz[1], /overwrite) dB = (B-shift(B,1,0))[1:*,*] ; Difference with next neighbour dB = AngleRange(dB) ;for i=0,sz[1]-2 do B[i+1,*] = B[i,*]+dB[i,*] FOR i=monotonic,sz[1]-2 DO B[i+1,*] = B[i,*]+dB[i ,*] FOR i=monotonic,1,-1 DO B[i-1,*] = B[i,*]-dB[i-1,*] B = reform(B,sz[1:sz[0]], /overwrite) ENDIF ENDIF RETURN, B/rpd & END