     func sunPosition(jd: Double) -> [Double] {
         
         let D = jd - 2451545
-        let g = fixangle(357.529 + 0.98560028 * D)
-        let q = fixangle(280.459 + 0.98564736 * D)
-        let L = fixangle(q + (1.915 * dsin(g)) + (0.020 * dsin(2 * g)))
-        
-        // double R = 1.00014 - 0.01671 * [self dcos:g] - 0.00014 * [self dcos:
+        let g = Double.fixAngle(357.529 + 0.98560028 * D)
+        let q = Double.fixAngle(280.459 + 0.98564736 * D)
+        let L = Double.fixAngle(q + (1.915 * Double.dSin(g)) + (0.020 * Double.dSin(2 * g)))
+        
+        // double R = 1.00014 - 0.01671 * [self dCos:g] - 0.00014 * [self dCos:
         // (2*g)]
         let e = 23.439 - (0.00000036 * D)
-        let d = darcsin(dsin(e) * dsin(L))
-        var RA = darctan2((dcos(e) * dsin(L)), x: dcos(L))/15.0
-        RA = fixhour(RA)
+        let d = Double.dArcSin(Double.dSin(e) * Double.dSin(L))
+        var RA = Double.dArcTan2((Double.dCos(e) * Double.dSin(L)), x: Double.dCos(L))/15.0
+        RA = Double.fixHour(RA)
         let EqT = q/15.0 - RA
         let sPosition: [Double] = [d,EqT]
         return sPosition

    // the night portion used for adjusting times in higher latitudes
    func nightPortion(angle: Double) -> Double {
        var calc : Double = 0.0
        if (adjustHighLats == AdjustingMethods.angleBased){
            calc = (angle)/60.0
        }else if (adjustHighLats == AdjustingMethods.midNight){
            calc = 0.5
        }else if (adjustHighLats == AdjustingMethods.oneSeventh){
            calc = 0.14286
        }
        return calc
    }
    
    // convert hours to day portions
    func dayPortion(var times: [Double]) -> [Double] {
        for i in 0...6{
            times[i] /= 24
        }
        return times
    }
    
    // Tune timings for adjustments
    // Set time offsets
    func tune(offsetTimes: [Double]) {
        for i in 0...offsets.count{ // offsetTimes length
            // should be 7 in order
            // of Fajr, Sunrise,

        
    }
    
    public func tuneTimes(times: [Double]) -> [Double] {
        
        var aTimes : [Double] = []
        for i in 0..<times.count {
            aTimes.append(times[i]+offsets[i]/60.0)
        }
        
        return aTimes
    }
    
    
    
}







extension Double {
    // ---------------------- Trigonometric Functions -----------------------
    // range reduce angle in degrees.
    static func fixAngle(a: Double) -> Double {
        
        let reduceAngle = a - (360 * (floor(a / 360.0)))
        let angle = reduceAngle < 0 ? (reduceAngle + 360) : reduceAngle
        
        return angle
    }
    
    // range reduce hours to 0..23
    static func fixHour(a: Double) -> Double {
        
        let reduceHour = a - 24.0 * floor(a / 24.0)
        let fixHour = reduceHour < 0 ? (reduceHour + 24) : reduceHour
        
        return fixHour
    }
    
    // radian to degree
    static func radiansToDegrees(alpha: Double) -> Double {
        return ((alpha * 180.0) / M_PI)
    }
    
    // deree to radian
    static func degreesToRadians(alpha: Double) -> Double {
        return ((alpha * M_PI) / 180.0)
    }
    
    // degree sin
    static func dSin(d: Double) -> Double {
        return (sin(degreesToRadians(d)))
    }
    
    // degree cos
    static func dCos(d: Double) -> Double {
        return (cos(degreesToRadians(d)))
    }
    
    // degree tan
    static func dTan(d:Double) -> Double {
        return (tan(degreesToRadians(d)))
    }
    
    // degree arcsin
    static func dArcSin(x: Double) -> Double {
        return radiansToDegrees(asin(x))
    }
    
    // degree arccos
    static func dArcCos(x: Double) -> Double {
        return radiansToDegrees(acos(x))
    }
    
    // degree arctan
    static func dArcTan(x: Double) -> Double {
        return radiansToDegrees(atan(x))
    }
    
    // degree arctan2
    static func dArcTan2(y: Double, x: Double) -> Double {
        return radiansToDegrees(atan2(y, x))
    }
    
    // degree arccot
    static func dArcCot(x: Double) -> Double{
        return radiansToDegrees(atan2(1.0, x))
    }
}
