fork download
  1. # -*- coding: utf-8 -*-
  2. '''
  3. 経緯線描画 (日時指定)
  4. Ver 1.11 2013-05-29 update
  5. '''
  6. from tkinter import *
  7. from math import *
  8. from numpy import *
  9. from urllib.request import *
  10. from urllib.parse import *
  11. from html.parser import HTMLParser
  12. import datetime
  13.  
  14. '''
  15. 経緯線描画
  16. '''
  17. class LatLong:
  18. def __init__(self, P,B0,L0,r,width,height,astep,dstep,bg,fc,line,fpath):
  19. self.root = Tk()
  20. self.P = P
  21. self.B0 = B0
  22. self.L0 = L0
  23. self.r = r
  24. self.width = width
  25. self.height = height
  26. self.astep = astep
  27. self.dstep = dstep
  28. self.backcolor = bg
  29. self.forecolor = fc
  30. self.lineweight = line
  31. self.fpath = fpath
  32. self.c0=Canvas(self.root, width=width, height=height, bg=bg)
  33. self.c0.pack()
  34.  
  35. # Z軸回転
  36. def rotZ(self, a, d):
  37. L = cos(radians(d)) * cos(radians(a - self.L0))
  38. M = cos(radians(d)) * sin(radians(a - self.L0))
  39. N = sin(radians(d))
  40. return array([[L], [M], [N]])
  41.  
  42. # Y軸回転
  43. def rotY(self, LMN):
  44. mB0 = radians(self.B0) * -1
  45. res = array([[]])
  46. res = append(res, [[cos(mB0), 0, -1 * sin(mB0)]], axis=1)
  47. res = append(res, [[0, 1, 0]], axis=0)
  48. res = append(res, [[sin(mB0), 0, cos(mB0)]], axis=0)
  49. return dot(res, LMN)
  50.  
  51. # X軸回転
  52. def rotX(self, LMNd):
  53. mP = radians(self.P) * -1
  54. res = array([[]])
  55. res = append(res, [[1, 0, 0]], axis=1)
  56. res = append(res, [[0, cos(mP), sin(mP)]], axis=0)
  57. res = append(res, [[0, -1 * sin(mP), cos(mP)]], axis=0)
  58. return dot(res, LMNd)
  59.  
  60. # XYZ計算
  61. def calXYZ(self, a, d):
  62. LMN = self.rotZ(a,d)
  63. LMNd = self.rotY(LMN)
  64. xyz = self.rotX(LMNd)
  65.  
  66. return sum(xyz[0]),sum(xyz[1]),sum(xyz[2])
  67.  
  68. # 位置補正(XYZ)
  69. def corrPos(self, x, y, z):
  70. x = x
  71. y = y * self.r + self.width / 2
  72. z = self.height / 2 - z * self.r
  73. return x,y,z
  74.  
  75. # 緯線
  76. def Latitude(self):
  77. lpos=[]
  78. for delta in range(-90,90,self.dstep):
  79. for alpha in range(0, 361, 5):
  80. xyz = self.calXYZ(alpha, delta)
  81. x,y,z = self.corrPos(xyz[0], xyz[1], xyz[2])
  82. if x>=0:
  83. lpos.append(y)
  84. lpos.append(z)
  85. else:
  86. if len(lpos)>2:
  87. self.c0.create_line(lpos,fill=self.forecolor,width=self.lineweight,smooth=True)
  88. lpos[:]=[]
  89.  
  90. if len(lpos)>2:
  91. self.c0.create_line(lpos,fill=self.forecolor,width=self.lineweight,smooth=True)
  92. lpos[:]=[]
  93.  
  94. # 経線
  95. def Longitude(self):
  96. lpos=[]
  97. for alpha in range(0,360,self.astep):
  98. for delta in range(-90, 91, 5):
  99. xyz = self.calXYZ(alpha, delta)
  100. x,y,z = self.corrPos(xyz[0], xyz[1], xyz[2])
  101. if x>=0:
  102. lpos.append(y)
  103. lpos.append(z)
  104. if len(lpos)>2:
  105. self.c0.create_line(lpos,fill=self.forecolor,width=self.lineweight,smooth=True)
  106. lpos[:]=[]
  107.  
  108. # 外周描画
  109. def dispOuter(self):
  110. lpos = []
  111. for pos in range(360):
  112. y = cos(radians(pos))
  113. z = sin(radians(pos))
  114. x,y,z = self.corrPos(0, y, z)
  115.  
  116. lpos.append(y)
  117. lpos.append(z)
  118. self.c0.create_line(lpos,fill=self.forecolor,width=self.lineweight,smooth=True)
  119. lpos[:] = []
  120.  
  121. # Save PostScript
  122. def SaveImage(self):
  123. self.c0.postscript(file=self.fpath, width=self.width, height=self.height)
  124.  
  125. # MainLoop
  126. def mainloop(self):
  127. self.root.mainloop()
  128.  
  129.  
  130. '''
  131. 国立天文台よりPB0L0取得
  132. '''
  133. class KHTMLParser(HTMLParser):
  134. hms = "00:00:00"
  135. getfl = False
  136. P = None
  137. B0 = None
  138. L0 = None
  139.  
  140. def handle_data(self, data):
  141. data = data.strip(" \t\r\n")
  142.  
  143. if data and self.getfl:
  144. if self.P and self.B0 and self.L0 is None: self.L0 = float(data)
  145. if self.P and self.B0 is None: self.B0 = float(data)
  146. if self.P is None: self.P = float(data)
  147.  
  148. if data.strip() == self.hms: self.getfl = True
  149.  
  150. def handle_starttag(self, tag, attrs):
  151. pass
  152.  
  153. def handle_endtag(self, tag):
  154. pass
  155.  
  156.  
  157. '''
  158. Main
  159. '''
  160. if __name__=='__main__':
  161. '''
  162. 使用方法
  163.  
  164. クラス初期化
  165. LatLong(P,B0,L0,r,width,height,astep,dstep,backcolor,forecolor,lineweight,fpath)
  166. '''
  167. # PB0L0取得
  168. params = {'year':2013,'month':5,'day':20,'hour':10,'min':30,'sec':00,'tsys':2}
  169.  
  170. r=urlopen("http://e...content-available-to-author-only...c.jp/cgi-bin/koyomi/cande/sun_spin.cgi",urlencode(params).encode("utf-8"))
  171. mp = KHTMLParser()
  172. mp.hms = "%02d:%02d:%02d" % (params['hour'], params['min'], params['sec'])
  173. mp.feed(r.read().decode("euc_jp"))
  174.  
  175. # LatLong起動
  176. latlong = LatLong(mp.P,mp.B0,mp.L0,300, 800, 800, 15, 15, "black", "green", 2.0, r"~/Desktop/ball.ps")
  177.  
  178. latlong.Latitude()
  179. latlong.Longitude()
  180. latlong.dispOuter()
  181. latlong.SaveImage()
  182. latlong.mainloop()
  183.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty