fork download
  1. # -*- coding: utf-8 -*-
  2. '''
  3. 経緯線描画 (日時指定)
  4. Ver 1.12 2013-05-30 update
  5. '''
  6. from Tkinter import *
  7. from math import *
  8. from numpy import *
  9. from urllib import *
  10. from HTMLParser import HTMLParser
  11. import datetime
  12.  
  13. '''
  14. 経緯線描画
  15. '''
  16. class LatLong:
  17. def __init__(self, P,B0,L0,r,width,height,astep,dstep,bg,fc,line,fpath):
  18. self.root = Tk()
  19. self.P = P
  20. self.B0 = B0
  21. self.L0 = L0
  22. self.r = r
  23. self.width = width
  24. self.height = height
  25. self.astep = astep
  26. self.dstep = dstep
  27. self.backcolor = bg
  28. self.forecolor = fc
  29. self.lineweight = line
  30. self.fpath = fpath
  31. self.c0=Canvas(self.root, width=width, height=height, bg=bg)
  32. self.c0.pack()
  33.  
  34. # Z軸回転
  35. def rotZ(self, a, d):
  36. L = cos(radians(d)) * cos(radians(a - self.L0))
  37. M = cos(radians(d)) * sin(radians(a - self.L0))
  38. N = sin(radians(d))
  39. return array([[L], [M], [N]])
  40.  
  41. # Y軸回転
  42. def rotY(self, LMN):
  43. mB0 = radians(self.B0) * -1
  44. res = array([[]])
  45. res = append(res, [[cos(mB0), 0, -1 * sin(mB0)]], axis=1)
  46. res = append(res, [[0, 1, 0]], axis=0)
  47. res = append(res, [[sin(mB0), 0, cos(mB0)]], axis=0)
  48. return dot(res, LMN)
  49.  
  50. # X軸回転
  51. def rotX(self, LMNd):
  52. mP = radians(self.P) * -1
  53. res = array([[]])
  54. res = append(res, [[1, 0, 0]], axis=1)
  55. res = append(res, [[0, cos(mP), sin(mP)]], axis=0)
  56. res = append(res, [[0, -1 * sin(mP), cos(mP)]], axis=0)
  57. return dot(res, LMNd)
  58.  
  59. # XYZ計算
  60. def calXYZ(self, a, d):
  61. LMN = self.rotZ(a,d)
  62. LMNd = self.rotY(LMN)
  63. xyz = self.rotX(LMNd)
  64.  
  65. return sum(xyz[0]),sum(xyz[1]),sum(xyz[2])
  66.  
  67. # 位置補正(XYZ)
  68. def corrPos(self, x, y, z):
  69. x = x
  70. y = y * self.r + self.width / 2
  71. z = self.height / 2 - z * self.r
  72. return x,y,z
  73.  
  74. # 緯線
  75. def Latitude(self):
  76. lpos=[]
  77. for delta in range(-90,90,self.dstep):
  78. for alpha in range(0, 361, 5):
  79. xyz = self.calXYZ(alpha, delta)
  80. x,y,z = self.corrPos(xyz[0], xyz[1], xyz[2])
  81. if x>=0:
  82. lpos.append(y)
  83. lpos.append(z)
  84. else:
  85. if len(lpos)>2:
  86. self.c0.create_line(lpos,fill=self.forecolor,width=self.lineweight,smooth=True)
  87. lpos[:]=[]
  88.  
  89. if len(lpos)>2:
  90. self.c0.create_line(lpos,fill=self.forecolor,width=self.lineweight,smooth=True)
  91. lpos[:]=[]
  92.  
  93. # 経線
  94. def Longitude(self):
  95. lpos=[]
  96. for alpha in range(0,360,self.astep):
  97. for delta in range(-90, 91, 5):
  98. xyz = self.calXYZ(alpha, delta)
  99. x,y,z = self.corrPos(xyz[0], xyz[1], xyz[2])
  100. if x>=0:
  101. lpos.append(y)
  102. lpos.append(z)
  103. if len(lpos)>2:
  104. self.c0.create_line(lpos,fill=self.forecolor,width=self.lineweight,smooth=True)
  105. lpos[:]=[]
  106.  
  107. # 外周描画
  108. def dispOuter(self):
  109. lpos = []
  110. for pos in range(360):
  111. y = cos(radians(pos))
  112. z = sin(radians(pos))
  113. x,y,z = self.corrPos(0, y, z)
  114.  
  115. lpos.append(y)
  116. lpos.append(z)
  117. self.c0.create_line(lpos,fill=self.forecolor,width=self.lineweight,smooth=True)
  118. lpos[:] = []
  119.  
  120. # Save PostScript
  121. def SaveImage(self):
  122. self.c0.postscript(file=self.fpath, width=self.width, height=self.height)
  123.  
  124. # MainLoop
  125. def mainloop(self):
  126. self.root.mainloop()
  127.  
  128.  
  129. '''
  130. 国立天文台よりPB0L0取得
  131. '''
  132. class KHTMLParser(HTMLParser):
  133. hms = "00:00:00"
  134. getfl = False
  135. P = None
  136. B0 = None
  137. L0 = None
  138.  
  139. def handle_data(self, data):
  140. data = data.strip(" \t\r\n")
  141.  
  142. if data and self.getfl:
  143. if self.P and self.B0 and self.L0 is None: self.L0 = float(data)
  144. if self.P and self.B0 is None: self.B0 = float(data)
  145. if self.P is None: self.P = float(data)
  146.  
  147. if data.strip() == self.hms: self.getfl = True
  148.  
  149. def handle_starttag(self, tag, attrs):
  150. pass
  151.  
  152. def handle_endtag(self, tag):
  153. pass
  154.  
  155.  
  156. '''
  157. Main
  158. '''
  159. if __name__=='__main__':
  160. '''
  161. 使用方法
  162.  
  163. クラス初期化
  164. LatLong(P,B0,L0,r,width,height,astep,dstep,backcolor,forecolor,lineweight,fpath)
  165. '''
  166. # PB0L0取得
  167. params = {'year':2013,'month':05,'day':20,'hour':10,'min':30,'sec':00}
  168.  
  169. r=urlopen('http://e...content-available-to-author-only...c.jp/cgi-bin/koyomi/cande/sun_spin.cgi',urlencode(params))
  170. mp = KHTMLParser()
  171. mp.hms = "%02d:%02d:%02d" % (int(params['hour']), int(params['min']), int(params['sec']))
  172. mp.feed(r.read())
  173.  
  174. # LatLong起動
  175. latlong = LatLong(mp.P,mp.B0,mp.L0,300, 800, 800, 15, 15, "black", "green", 2.0, r"~/Desktop/ball.ps")
  176.  
  177. latlong.Latitude()
  178. latlong.Longitude()
  179. latlong.dispOuter()
  180. latlong.SaveImage()
  181. latlong.mainloop()
  182. # -*- coding: utf-8 -*-
  183. '''
  184. 経緯線描画 (日時指定)
  185. Ver 1.11 2013-05-29 update
  186. '''
  187. from Tkinter import *
  188. from math import *
  189. from numpy import *
  190. from urllib import *
  191. from HTMLParser import HTMLParser
  192. import datetime
  193.  
  194. '''
  195. 経緯線描画
  196. '''
  197. class LatLong:
  198. def __init__(self, P,B0,L0,r,width,height,astep,dstep,bg,fc,line,fpath):
  199. self.root = Tk()
  200. self.P = P
  201. self.B0 = B0
  202. self.L0 = L0
  203. self.r = r
  204. self.width = width
  205. self.height = height
  206. self.astep = astep
  207. self.dstep = dstep
  208. self.backcolor = bg
  209. self.forecolor = fc
  210. self.lineweight = line
  211. self.fpath = fpath
  212. self.c0=Canvas(self.root, width=width, height=height, bg=bg)
  213. self.c0.pack()
  214.  
  215. # Z軸回転
  216. def rotZ(self, a, d):
  217. L = cos(radians(d)) * cos(radians(a - self.L0))
  218. M = cos(radians(d)) * sin(radians(a - self.L0))
  219. N = sin(radians(d))
  220. return array([[L], [M], [N]])
  221.  
  222. # Y軸回転
  223. def rotY(self, LMN):
  224. mB0 = radians(self.B0) * -1
  225. res = array([[]])
  226. res = append(res, [[cos(mB0), 0, -1 * sin(mB0)]], axis=1)
  227. res = append(res, [[0, 1, 0]], axis=0)
  228. res = append(res, [[sin(mB0), 0, cos(mB0)]], axis=0)
  229. return dot(res, LMN)
  230.  
  231. # X軸回転
  232. def rotX(self, LMNd):
  233. mP = radians(self.P) * -1
  234. res = array([[]])
  235. res = append(res, [[1, 0, 0]], axis=1)
  236. res = append(res, [[0, cos(mP), sin(mP)]], axis=0)
  237. res = append(res, [[0, -1 * sin(mP), cos(mP)]], axis=0)
  238. return dot(res, LMNd)
  239.  
  240. # XYZ計算
  241. def calXYZ(self, a, d):
  242. LMN = self.rotZ(a,d)
  243. LMNd = self.rotY(LMN)
  244. xyz = self.rotX(LMNd)
  245.  
  246. return sum(xyz[0]),sum(xyz[1]),sum(xyz[2])
  247.  
  248. # 位置補正(XYZ)
  249. def corrPos(self, x, y, z):
  250. x = x
  251. y = y * self.r + self.width / 2
  252. z = self.height / 2 - z * self.r
  253. return x,y,z
  254.  
  255. # 緯線
  256. def Latitude(self):
  257. lpos=[]
  258. for delta in range(-90,90,self.dstep):
  259. for alpha in range(0, 361, 5):
  260. xyz = self.calXYZ(alpha, delta)
  261. x,y,z = self.corrPos(xyz[0], xyz[1], xyz[2])
  262. if x>=0:
  263. lpos.append(y)
  264. lpos.append(z)
  265. else:
  266. if len(lpos)>2:
  267. self.c0.create_line(lpos,fill=self.forecolor,width=self.lineweight,smooth=True)
  268. lpos[:]=[]
  269.  
  270. if len(lpos)>2:
  271. self.c0.create_line(lpos,fill=self.forecolor,width=self.lineweight,smooth=True)
  272. lpos[:]=[]
  273.  
  274. # 経線
  275. def Longitude(self):
  276. lpos=[]
  277. for alpha in range(0,360,self.astep):
  278. for delta in range(-90, 91, 5):
  279. xyz = self.calXYZ(alpha, delta)
  280. x,y,z = self.corrPos(xyz[0], xyz[1], xyz[2])
  281. if x>=0:
  282. lpos.append(y)
  283. lpos.append(z)
  284. if len(lpos)>2:
  285. self.c0.create_line(lpos,fill=self.forecolor,width=self.lineweight,smooth=True)
  286. lpos[:]=[]
  287.  
  288. # 外周描画
  289. def dispOuter(self):
  290. lpos = []
  291. for pos in range(360):
  292. y = cos(radians(pos))
  293. z = sin(radians(pos))
  294. x,y,z = self.corrPos(0, y, z)
  295.  
  296. lpos.append(y)
  297. lpos.append(z)
  298. self.c0.create_line(lpos,fill=self.forecolor,width=self.lineweight,smooth=True)
  299. lpos[:] = []
  300.  
  301. # Save PostScript
  302. def SaveImage(self):
  303. self.c0.postscript(file=self.fpath, width=self.width, height=self.height)
  304.  
  305. # MainLoop
  306. def mainloop(self):
  307. self.root.mainloop()
  308.  
  309.  
  310. '''
  311. 国立天文台よりPB0L0取得
  312. '''
  313. class KHTMLParser(HTMLParser):
  314. hms = "00:00:00"
  315. getfl = False
  316. P = None
  317. B0 = None
  318. L0 = None
  319.  
  320. def handle_data(self, data):
  321. data = data.strip(" \t\r\n")
  322.  
  323. if data and self.getfl:
  324. if self.P and self.B0 and self.L0 is None: self.L0 = float(data)
  325. if self.P and self.B0 is None: self.B0 = float(data)
  326. if self.P is None: self.P = float(data)
  327.  
  328. if data.strip() == self.hms: self.getfl = True
  329.  
  330. def handle_starttag(self, tag, attrs):
  331. pass
  332.  
  333. def handle_endtag(self, tag):
  334. pass
  335.  
  336.  
  337. '''
  338. Main
  339. '''
  340. if __name__=='__main__':
  341. '''
  342. 使用方法
  343.  
  344. クラス初期化
  345. LatLong(P,B0,L0,r,width,height,astep,dstep,backcolor,forecolor,lineweight,fpath)
  346. '''
  347. # PB0L0取得
  348. params = {'year':2013,'month':05,'day':20,'hour':10,'min':30,'sec':00, 'tsys':2}
  349.  
  350. r=urlopen('http://e...content-available-to-author-only...c.jp/cgi-bin/koyomi/cande/sun_spin.cgi',urlencode(params))
  351. mp = KHTMLParser()
  352. mp.hms = "%02d:%02d:%02d" % (int(params['hour']), int(params['min']), int(params['sec']))
  353. mp.feed(r.read())
  354.  
  355. # LatLong起動
  356. latlong = LatLong(mp.P,mp.B0,mp.L0,300, 800, 800, 15, 15, "black", "green", 2.0, r"~/Desktop/ball.ps")
  357.  
  358. latlong.Latitude()
  359. latlong.Longitude()
  360. latlong.dispOuter()
  361. latlong.SaveImage()
  362. latlong.mainloop()
  363.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty