convert north based clockwise azimuth (0, 360) into x-axis/east based angle (-Pi, Pi)
547057
public double convertAzimuth2xaxisAngle(double azimuth) {
        if (Double.compare(azimuth, 360) > 0 || Double.compare(azimuth, 0) < 0) {
            throw new IllegalArgumentException("Azimuth " + azimuth + " must be in (0, 360)");
        }
        double angleXY = PI_2 - azimuth / 180. * Math.PI;
        if (angleXY < -Math.PI)
            angleXY += 2 * Math.PI;
        if (angleXY > Math.PI)
            angleXY -= 2 * Math.PI;
        return angleXY;
    }
717062
def axis_angle(self):
        
        qw, qx, qy, qz = self.quaternion
        theta = 2 * np.arccos(qw)
        omega = np.array([1,0,0])
        if theta > 0:
            rx = qx / np.sqrt(1.0 - qw**2)
            ry = qy / np.sqrt(1.0 - qw**2)
            rz = qz / np.sqrt(1.0 - qw**2)
            omega = np.array([rx, ry, rz])
        return theta * omega
351987
def from_axis_angle(self, axis, angle):
        ''''''
        ux = axis.x
        uy = axis.y
        uz = axis.z
        ct = cos(angle)
        st = sin(angle)
        self.a.x = ct + (1-ct) * ux**2
        self.a.y = ux*uy*(1-ct) - uz*st
        self.a.z = ux*uz*(1-ct) + uy*st
        self.b.x = uy*ux*(1-ct) + uz*st
        self.b.y = ct + (1-ct) * uy**2
        self.b.z = uy*uz*(1-ct) - ux*st
        self.c.x = uz*ux*(1-ct) - uy*st
        self.c.y = uz*uy*(1-ct) + ux*st
        self.c.z = ct + (1-ct) * uz**2
680586
def calculate_clock_angle(inst):
    
    
    # Calculate clock angle in degrees
    clock_angle = np.degrees(np.arctan2(inst['BY_GSM'], inst['BZ_GSM']))
    clock_angle[clock_angle < 0.0] += 360.0
    inst['clock_angle'] = pds.Series(clock_angle, index=inst.data.index)

    # Calculate magnitude of IMF in Y-Z plane
    inst['BYZ_GSM'] = pds.Series(np.sqrt(inst['BY_GSM']**2 +
                                         inst['BZ_GSM']**2),
                                       index=inst.data.index)

    return
999681
def pt_rotate(pt=(0.0, 0.0), angle=[0.0], center=(0.0, 0.0)):
    '''
    '''
    assert isinstance(pt, tuple)
    l_pt = len(pt)
    assert l_pt > 1
    for i in pt:
        assert isinstance(i, float)
    assert isinstance(angle, list)
    l_angle = len(angle)
    assert l_angle == l_pt-1
    for i in angle:
        assert isinstance(i, float)
        assert abs(i) <= 2*pi
    assert isinstance(center, tuple)
    assert len(center) == l_pt
    for i in center:
        assert isinstance(i, float)

    # Get vector from center to point and use to get relative polar coordinate.
    v_cart = [pt[i] - center[i] for i in range(l_pt)]

    # Length of vector needs to stay constant for new point.
    v_pol_l = [sqrt(v_cart[i]**2 + v_cart[i+1]**2) for i in range(l_angle)]
    v_pol_a = [(atan(v_cart[i+1] / v_cart[i]) if v_cart[i] != 0.0 else pi/2) + pi*int(pt[i] < center[i]) \
               for i in range(l_angle)]

    # Add rotation angle then convert back to cartesian vector.
    n_pol_a = [v_pol_a[i] + angle[i] for i in range(l_angle)]
    n_cart = [v_pol_l[0] * cos(n_pol_a[0])] + [v_pol_l[i] * sin(n_pol_a[i])\
                                               for i in range(l_angle)]

    # Add in the centre offset to get original offset from c.
    r = [n_cart[i] + center[i] for i in range(l_pt)]
    return tuple(r)
659627
private AffineTransform getRotationTransform ()
    {
        double theta;

        switch (_orient) {
            case NORTH:
            default:
                theta = 0.0;
                break;

            case SOUTH:
                theta = Math.PI;
                break;

            case EAST:
                theta = Math.PI*0.5;
                break;

            case WEST:
                theta = -Math.PI*0.5;
                break;

            case NORTHEAST:
                theta = Math.PI*0.25;
                break;

            case NORTHWEST:
                theta = -Math.PI*0.25;
                break;

            case SOUTHEAST:
                theta = Math.PI*0.75;
                break;

            case SOUTHWEST:
                theta = -Math.PI*0.75;
                break;

            case NORTHNORTHEAST:
                theta = -Math.PI*0.125;
                break;

            case NORTHNORTHWEST:
                theta = Math.PI*0.125;
                break;

            case SOUTHSOUTHEAST:
                theta = -Math.PI*0.875;
                break;

            case SOUTHSOUTHWEST:
                theta = Math.PI*0.875;
                break;

            case EASTNORTHEAST:
                theta = -Math.PI*0.375;
                break;

            case EASTSOUTHEAST:
                theta = -Math.PI*0.625;
                break;

            case WESTNORTHWEST:
                theta = Math.PI*0.375;
                break;

            case WESTSOUTHWEST:
                theta = Math.PI*0.625;
                break;
        }

        return AffineTransform.getRotateInstance(
            theta,
            (_ox - _oxoff) + _frames.getWidth(_frameIdx)/2,
            (_oy - _oyoff) + _frames.getHeight(_frameIdx)/2
        );
    }
558206
def symbolic_axis_rotation_matrix(axis, symbolic_theta):
    """"""
    [x, y, z] = axis
    c = sympy.cos(symbolic_theta)
    s = sympy.sin(symbolic_theta)
    return sympy.Matrix([
        [x**2 + (1 - x**2) * c, x * y * (1 - c) - z * s, x * z * (1 - c) + y * s],
        [x * y * (1 - c) + z * s, y ** 2 + (1 - y**2) * c, y * z * (1 - c) - x * s],
        [x * z * (1 - c) - y * s, y * z * (1 - c) + x * s, z**2 + (1 - z**2) * c]
    ])
240261
def _inc_lat(self, latitude, delta):
        """"""
        y = self._convert_latitude(latitude)
        y += delta
        return 360 / pi * atan(
            exp((180 - y * 360 / (2 ** self._zoom) / self._size) * pi / 180)) - 90
1048977
def cart2spher(x, y, z):
    """"""
    hxy = np.hypot(x, y)
    r = np.hypot(hxy, z)
    theta = np.arctan2(z, hxy)
    phi = np.arctan2(y, x)
    return r, theta, phi
843890
def get_orientation(k, i, j):
        from pylocus.basics_angles import from_0_to_2pi
        
        theta_ij = own.abs_angles[i, j]
        theta_ji = own.abs_angles[j, i]

        # complicated
        xi = own.points[i, 0]
        xj = own.points[j, 0]
        yi = own.points[i, 1]
        yj = own.points[j, 1]
        w = np.array([yi - yj, xj - xi])
        test = np.dot(own.points[k, :] - own.points[i, :], w) > 0

        # more elegant
        theta_ik = truth.abs_angles[i, k]
        diff = from_0_to_2pi(theta_ik - theta_ij)
        test2 = (diff > 0 and diff < pi)
        assert (test == test2), "diff: %r, scalar prodcut: %r" % (diff, np.dot(
            own.points[k, :] - own.points[i, :], w))

        thetai_jk = truth.get_theta(i, j, k)
        thetaj_ik = truth.get_theta(j, i, k)
        if test:
            theta_ik = theta_ij + thetai_jk
            theta_jk = theta_ji - thetaj_ik
        else:
            theta_ik = theta_ij - thetai_jk
            theta_jk = theta_ji + thetaj_ik
        theta_ik = from_0_to_2pi(theta_ik)
        theta_jk = from_0_to_2pi(theta_jk)
        return theta_ik, theta_jk
1088628
def from_polar(degrees, magnitude):
        """"""
        vec = Vector2()
        vec.X = math.cos(math.radians(degrees)) * magnitude

        # Negate because y in screen coordinates points down, oppisite from what is
        # expected in traditional mathematics.
        vec.Y = -math.sin(math.radians(degrees)) * magnitude
        return vec
312312
def _calculate_sun_vector(self):
        """"""
        z_axis = Vector3(0., 0., -1.)
        x_axis = Vector3(1., 0., 0.)
        north_vector = Vector3(0., 1., 0.)

        # rotate north vector based on azimuth, altitude, and north
        _sun_vector = north_vector \
            .rotate_around(x_axis, self.altitude_in_radians) \
            .rotate_around(z_axis, self.azimuth_in_radians) \
            .rotate_around(z_axis, math.radians(-1 * self.north_angle))

        _sun_vector.normalize()
        try:
            _sun_vector.flip()
        except AttributeError:
            # euclid3
            _sun_vector = Vector3(-1 * _sun_vector.x,
                                  -1 * _sun_vector.y,
                                  -1 * _sun_vector.z)

        self._sun_vector = _sun_vector
52591
def _polar_to_cartesian(cx, cy, r, theta):
        
        return cx - r * math.sin(theta), cy - r * math.cos(theta)
646314
def bbox(self):
        """"""
        # a(t) = radians(self.theta + self.delta*t)
        #      = (2*pi/360)*(self.theta + self.delta*t)
        # x'=0: ~~~~~~~~~
        # -rx*cos(phi)*sin(a(t)) = ry*sin(phi)*cos(a(t))
        # -(rx/ry)*cot(phi)*tan(a(t)) = 1
        # a(t) = arctan(-(ry/rx)tan(phi)) + pi*k === atan_x
        # y'=0: ~~~~~~~~~~
        # rx*sin(phi)*sin(a(t)) = ry*cos(phi)*cos(a(t))
        # (rx/ry)*tan(phi)*tan(a(t)) = 1
        # a(t) = arctan((ry/rx)*cot(phi))
        # atanres = arctan((ry/rx)*cot(phi)) === atan_y
        # ~~~~~~~~
        # (2*pi/360)*(self.theta + self.delta*t) = atanres + pi*k
        # Therfore, for both x' and y', we have...
        # t = ((atan_{x/y} + pi*k)*(360/(2*pi)) - self.theta)/self.delta
        # for all k s.t. 0 < t < 1
        from math import atan, tan

        if cos(self.phi) == 0:
            atan_x = pi/2
            atan_y = 0
        elif sin(self.phi) == 0:
            atan_x = 0
            atan_y = pi/2
        else:
            rx, ry = self.radius.real, self.radius.imag
            atan_x = atan(-(ry/rx)*tan(self.phi))
            atan_y = atan((ry/rx)/tan(self.phi))

        def angle_inv(ang, k):  # inverse of angle from Arc.derivative()
            return ((ang + pi*k)*(360/(2*pi)) - self.theta)/self.delta

        xtrema = [self.start.real, self.end.real]
        ytrema = [self.start.imag, self.end.imag]

        for k in range(-4, 5):
            tx = angle_inv(atan_x, k)
            ty = angle_inv(atan_y, k)
            if 0 <= tx <= 1:
                xtrema.append(self.point(tx).real)
            if 0 <= ty <= 1:
                ytrema.append(self.point(ty).imag)
        xmin = max(xtrema)
        return min(xtrema), max(xtrema), min(ytrema), max(ytrema)
950679
function (x, y, w, h, options) {
			var start = options.start,
				end = options.end,
				radius = options.r || w || h,
				innerRadius = options.innerR,
				cosStart = mathCos(start),
				sinStart = mathSin(start),
				cosEnd = mathCos(end),
				sinEnd = mathSin(end),
				ret;

			if (end - start === 0) { // no angle, don't show it.
				return ['x'];
			}

			ret = [
				'wa', // clockwise arc to
				x - radius, // left
				y - radius, // top
				x + radius, // right
				y + radius, // bottom
				x + radius * cosStart, // start x
				y + radius * sinStart, // start y
				x + radius * cosEnd, // end x
				y + radius * sinEnd  // end y
			];

			if (options.open && !innerRadius) {
				ret.push(
					'e',
					M,
					x,// - innerRadius,
					y// - innerRadius
				);
			}

			ret.push(
				'at', // anti clockwise arc to
				x - innerRadius, // left
				y - innerRadius, // top
				x + innerRadius, // right
				y + innerRadius, // bottom
				x + innerRadius * cosEnd, // start x
				y + innerRadius * sinEnd, // start y
				x + innerRadius * cosStart, // end x
				y + innerRadius * sinStart, // end y
				'x', // finish path
				'e' // close
			);

			ret.isArc = true;
			return ret;

		}
1344464
def _degrees_to_direction(self, degrees):
        """"""
        try:
            degrees = float(degrees)
        except ValueError:
            return None
        if degrees < 0 or degrees > 360:
            return None
        if degrees <= 11.25 or degrees >= 348.76:
            return "N"
        elif degrees <= 33.75:
            return "NNE"
        elif degrees <= 56.25:
            return "NE"
        elif degrees <= 78.75:
            return "ENE"
        elif degrees <= 101.25:
            return "E"
        elif degrees <= 123.75:
            return "ESE"
        elif degrees <= 146.25:
            return "SE"
        elif degrees <= 168.75:
            return "SSE"
        elif degrees <= 191.25:
            return "S"
        elif degrees <= 213.75:
            return "SSW"
        elif degrees <= 236.25:
            return "SW"
        elif degrees <= 258.75:
            return "WSW"
        elif degrees <= 281.25:
            return "W"
        elif degrees <= 303.75:
            return "WNW"
        elif degrees <= 326.25:
            return "NW"
        elif degrees <= 348.75:
            return "NNW"
        else:
            return None
35588
def _get_azimuth_correction(self, coe, azimuth):
        
        term1 = abs(np.cos(np.radians(2.*azimuth)))
        term2 = abs(np.sin(np.radians(2.*azimuth)))*coe['a5']
        return np.log(np.max(np.hstack((term1, term2))))