4字节 经纬度_Swift4 经纬度计算日出日落时间
import UIKitclassPGSun: NSObject {structSunriseset {
var sunrise= "07:00" //日出
var sunset = "18:30" //日落
init(sunrise:String, sunset:String) {
self.sunrise=sunrise
self.sunset=sunset
}
}
var start: Double= 0.0var sRA: Double= 0.0var sdec: Double= 0.0var sr: Double= 0.0var lon: Double= 0.0var end: Double= 0.0
private let Inv360: Double = 1.0 / 360.0
private let Degrad: Double = .pi / 180.0
private let Radge: Double = 180.0 /.pi///根據經緯度計算日出和日落時間 -誤差2分鐘///
///- Parameters:///- Returns: Sunriseset
class func sun(longitude:Double, latitude:Double)->Sunriseset{
let sun=PGSun()returnsun.getSunTimeAtDate(d: Date(), longitude: longitude, latitude: latitude)
}
func getSunTimeAtDate(d:Date , longitude:Double ,latitude:Double)->Sunriseset{
let xcts: Int=days(since_2000_Jan_0: d)
let dic=getSunTime(xcts, lon: longitude, lat: latitude)returndic
}
func getSunTime(_ day: Int, lon longitude: Double, lat latitude: Double)->Sunriseset {
_= self.sunRiset(day, long:longitude, lat:latitude ,altit: -35.0 / 60.0, limb:1, trise:self.start, tset:self.end)
let sunrise=toLocalTime(self.start)
let sunset=toLocalTime(self.end)
let sun=Sunriseset(sunrise: sunrise, sunset: sunset)returnsun
}
func toLocalTime(_ utTime:Double)->String {
var hour=Int(floor(utTime))
var temp= utTime -Double(hour)
hour+= 8;
temp= temp * 60;
let minute=Int(floor(temp))return String(format: "%02d:%02d", hour,minute)
}
func days(since_2000_Jan_0 d: Date)->Int {//距離2000-01-01的天數
let dateStr = "2000-01-01"let dateFormatter=DateFormatter()
dateFormatter.dateFormat= "yyyy-MM-dd"let date: Date? =dateFormatter.date(from: dateStr)
var time: TimeInterval? =nilif let aDate =date {
time=d.timeIntervalSince(aDate)
}
let days= (Int(time ?? 0)) / (3600 * 24)returndays
}
func sunRiset(_ day:Int,long longitude:Double, lat:Double ,altit:Double, limb upper_limb:Int, trise:Double, tset:Double)->Int{
var altit=altit
var d= 0.0/*Days since 2000 Jan 0.0 (negative before)*/
//以歷元2000.0起算的日數。
var sradius = 0.0 /*Sun's apparent radius*/
//太陽視半徑,約16分(受日地距離、大氣折射等諸多影響)
var t= 0.0 /*Diurnal arc*/
//周日弧,太陽一天在天上走過的弧長。
var tsouth= 0.0 /*Time when Sun is at south*/var sidtime= 0.0 /*Local sidereal time*/
//當地恒星時,即地球的真實自轉周期。比平均太陽日(日常時間)長3分56秒。
var rc= 0; /*Return cde from function - usually 0*/
/*Compute d of 12h local mean solar time*/d= Double(day)/*Days_since_2000_Jan_0(date)*/ + 0.5 - longitude / 360.0;//計算觀測地當日中午時刻對應2000.0起算的日數。
/*Compute local sideral time of this moment*/sidtime= self.revolution(self.GMST0(d) + 180.0 +longitude)//計算同時刻的當地恒星時(以角度為單位)。以格林尼治為基準,用經度差校正。
/*Compute Sun's RA + Decl at this moment*/self.Sun_RA_dec(d, ra:self.sRA, dec:self.sdec, r:self.sr);//Sun_RA_dec(d, sRA,sdec,sr);//計算同時刻太陽赤經赤緯。
/*Compute time when Sun is at south - in hours UT*/tsouth= 12.0 - self.rev180(sidtime - self.sRA) / 15.0;//tsouth = 12.0 - Rev180(sidtime - sRA) / 15.0;//計算太陽日的正午時刻,以世界時(格林尼治平太陽時)的小時計。
/*Compute the Sun's apparent radius, degrees*/sradius= 0.2666 /self.sr;//太陽視半徑。0.2666是一天文單位處的太陽視半徑(角度)。
/*Do correction to upper limb, if necessary*/
if upper_limb != 0{
altit-=sradius;//如果要用上邊緣,就要扣除一個視半徑。
}/*Compute the diurnal arc that the Sun traverses to reach*/
//計算周日弧。直接利用球面三角公式。如果碰到極晝極夜問題,同前處理。
/*the specified altitide altit:*/var cost:Double= 0cost= (self.sind(altit) - self.sind(lat)*self.sind(self.sdec))/(self.cosd(lat) *self.cosd(self.sdec));//cost = (Sind(altit) - Sind(lat) * Sind(sdec)) ///(Cosd(lat) * Cosd(sdec));
if (cost >= 1.0)
{
rc= -1;
t= 0.0;
}else{if (cost <= -1.0)
{
rc= +1;
t= 12.0; /*Sun always above altit*/}else{//t = Acosd(cost) / 15.0; /* The diurnal arc, hours */
t = self.acosd(cost) / 15.0;
}
}/*Store rise and set times - in hours UT*/self.start= tsouth -t;
self.end= tsouth +t;returnrc;
}
func Sun_RA_dec(_ d: Double, ra RA: Double, dec: Double, r: Double) {
var obl_ecl: Double= 0var x: Double= 0var y: Double= 0var z: Double= 0sunpos(d, lon: lon, r: r)//計算太陽的黃道坐標。
x = sr *cosd(lon)
y= sr *sind(lon)//計算太陽的直角坐標。
obl_ecl = 23.4393 - 3.563E-7 *d//黃赤交角,同前。
z = y *sind(obl_ecl)
y= y *cosd(obl_ecl)//把太陽的黃道坐標轉換成赤道坐標(暫改用直角坐標)。
sRA =atan2d(y, x: x)
sdec= atan2d(z, x: sqrt(x * x + y *y))//最后轉成赤道坐標。顯然太陽的位置是由黃道坐標方便地直接確定的,但必須轉換到赤//道坐標里才能結合地球的自轉確定我們需要的白晝長度。
}
func sunpos(_ d: Double, lon: Double, r: Double) {
var lon=lon
var M: Double= 0var w: Double= 0var e: Double= 0var E: Double= 0var x: Double= 0var y: Double= 0var v: Double= 0 //真近點角,太陽在任意時刻的真實近點角。
M= revolution(356.0470 + 0.9856002585 *d)//自變量的組成:2000.0時刻太陽黃經為356.0470度,此后每天約推進一度(360度/365天
w = 282.9404 + 4.70935E-5 * d //近日點的平均黃經。
e= 0.016709 - 1.151E-9 * d //地球公轉橢圓軌道離心率的時間演化。以上公式和黃赤交角公式一樣,不必深究。
E= M + e * Double(Radge) * sind(M) * (1.0 + e *cosd(M))
x= cosd(E) -e
y= sqrt(1.0 - e * e) *sind(E)
sr= sqrt(x * x + y *y)
v=atan2d(y, x: x)
lon= v +w
self.lon=lonif lon >= 360.0{
lon-= 360.0self.lon=lon
}
}
func revolution(_ x: Double)->Double {return x - 360.0 * floor(x *Double(Inv360))
}
func rev180(_ x: Double)->Double {return x - 360.0 * floor(x * Double(Inv360) + 0.5)
}
func GMST0(_ d: Double)->Double {
var sidtim0: Double
sidtim0= revolution((180.0 + 356.0470 + 282.9404) + (0.9856002585 + 4.70935E-5) *d)returnsidtim0
}
func sind(_ x: Double)->Double {return sin(x *Double(Degrad))
}
func cosd(_ x: Double)->Double {return cos(x *Double(Degrad))
}
func acosd(_ x: Double)->Double {return Double(Radge *acos(x))
}
func atan2d(_ y: Double, x: Double)->Double {return Double(Radge *atan2(y, x))
}
}
總結
以上是生活随笔為你收集整理的4字节 经纬度_Swift4 经纬度计算日出日落时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot 建readme_经
- 下一篇: layui table工具栏点击时间_l