fork download
  1. # -*- coding: utf-8 -*-
  2. '''
  3. 星食計算に必要な情報を国立天文台より取得
  4. 2013 waku (http://a...content-available-to-author-only...o.jp/wakusan3/)
  5. '''
  6. from urllib import *
  7. from HTMLParser import HTMLParser
  8. import datetime
  9.  
  10. '''
  11. HTMLパーサー
  12. '''
  13. class KParser(HTMLParser):
  14. hms = "00:00:00"
  15. d1 = None
  16. d2 = None
  17. d3 = None
  18.  
  19. getfl = False
  20.  
  21. def handle_data(self, data):
  22. data = data.strip(" \t\r\n")
  23.  
  24. if data and self.getfl:
  25. if self.d1 and self.d2 and self.d3 is None: self.d3 = data
  26. if self.d1 and self.d2 is None: self.d2 = data
  27. if self.d1 is None: self.d1 = float(data)
  28.  
  29. if data.strip() == self.hms:
  30. self.getfl = True
  31.  
  32. def handle_starttag(self, tag, attrs):
  33. pass
  34.  
  35. def handle_endtag(self, tag):
  36. pass
  37.  
  38. '''
  39. 恒星の視位置を返す
  40. '''
  41. def getStarPos(hip, gtimes, tsys, dms):
  42. spos = []
  43. for dt in gtimes:
  44. mp = KParser()
  45. params = {'stype':3, 's_hip':hip, 'year':dt.year, 'month':dt.month, 'day':dt.day, 'hour':dt.hour, 'min':dt.minute, 'sec':dt.second, 'tsys':tsys, 'dms':dms}
  46.  
  47. r=urlopen('http://e...content-available-to-author-only...c.jp/cgi-bin/koyomi/cande/stars_rhip.cgi',urlencode(params))
  48. mp.hms = "%d:%02d:%02d" % (int(params['hour']), int(params['min']), int(params['sec']))
  49. mp.feed(r.read())
  50. spos.append({'year':dt.year, 'month':dt.month, 'day':dt.day, 'hour':dt.hour, 'min':dt.minute, 'sec':dt.second, 'longitude': mp.d1, 'latitude': mp.d2})
  51.  
  52. return spos
  53.  
  54.  
  55. '''
  56. グリニッジ視恒星時を返す
  57. '''
  58. def getGreenwichTime(gtimes, tsys, dms):
  59. spos = []
  60. for dt in gtimes:
  61. mp = KParser()
  62. params = {'year':dt.year, 'month':dt.month, 'day':dt.day, 'hour':dt.hour, 'min':dt.minute, 'sec':dt.second, 'tsys':tsys, 'dms':dms}
  63.  
  64. r=urlopen('http://e...content-available-to-author-only...c.jp/cgi-bin/koyomi/cande/gst.cgi',urlencode(params))
  65. mp.hms = "%d:%02d:%02d" % (int(params['hour']), int(params['min']), int(params['sec']))
  66. mp.feed(r.read())
  67. spos.append({'year':dt.year, 'month':dt.month, 'day':dt.day, 'hour':dt.hour, 'min':dt.minute, 'sec':dt.second, 'gstime': mp.d1})
  68.  
  69. return spos
  70.  
  71. '''
  72. 月の地心座標を返す
  73. '''
  74. def getMoon(gtimes, tsys, dms):
  75. spos = []
  76. for dt in gtimes:
  77. mp = KParser()
  78. params = {'year':dt.year, 'month':dt.month, 'day':dt.day, 'hour':dt.hour, 'min':dt.minute, 'sec':dt.second, 'tsys':tsys, 'dms':dms, 'body':10}
  79.  
  80. r=urlopen('http://e...content-available-to-author-only...c.jp/cgi-bin/koyomi/cande/moon.cgi',urlencode(params))
  81. mp.hms = "%d:%02d:%02d" % (int(params['hour']), int(params['min']), int(params['sec']))
  82. mp.feed(r.read())
  83. spos.append({'year':dt.year, 'month':dt.month, 'day':dt.day, 'hour':dt.hour, 'min':dt.minute, 'sec':dt.second, 'longitude': mp.d1, 'latitude': mp.d2, 'gdistance': mp.d3})
  84.  
  85. return spos
  86.  
  87.  
  88. '''
  89. 指定した開始時間から終了時間まで指定間隔(sec)で時間を返す
  90. '''
  91. def getDeltaTime(stime, etime, unit):
  92. btime = stime
  93. dtimes = []
  94.  
  95. while btime <= etime:
  96. dtimes.append(btime)
  97. btime = btime + datetime.timedelta(seconds=unit)
  98.  
  99. return dtimes
  100.  
  101. def exportCSV(spos, gtime, moon, fpath):
  102. f = open(fpath, 'w')
  103. f.write("日付,時刻,恒星の視赤経,恒星の視赤緯,グリニッジ恒星時,月の視赤経,月の視赤緯,月の地心距離\n")
  104. for i in range(len(spos)):
  105. f.write(("%4d/%02d/%02d,%2d:%02d:%02d,%s,%s,%s,%s,%s,%s\n") %
  106. (spos[i]['year'], spos[i]['month'], spos[i]['day'],
  107. spos[i]['hour'], spos[i]['min'], spos[i]['sec'],
  108. spos[i]['longitude'],spos[i]['latitude'],
  109. gtime[i]['gstime'],
  110. moon[i]['longitude'],moon[i]['latitude'],moon[i]['gdistance']))
  111.  
  112.  
  113. f.close()
  114.  
  115. if __name__=='__main__':
  116. stime = datetime.datetime(2013,9,13,5,0,0) # 開始時間 (yyyy,m,d,h,M,s)
  117. etime = datetime.datetime(2013,9,13,5,10,0) # 終了時間 (yyyy,m,d,h,M,s)
  118. hip = 88125 # HIP番号
  119.  
  120. tsys = 2 # 0:地球時 1:世界時 2:日本標準時
  121. dms = 1 # 0:(h m s)表示 1:少数表示
  122. save_path = r"occu.csv" # 保存先
  123.  
  124. spos = getStarPos(hip, getDeltaTime(stime, etime, 60), tsys, dms)
  125. gtime = getGreenwichTime(getDeltaTime(stime, etime, 60), tsys, dms)
  126. moon = getMoon(getDeltaTime(stime, etime, 60), tsys, dms)
  127. exportCSV(spos,gtime,moon,save_path)
  128. # your code goes here
Runtime error #stdin #stdout #stderr 0.2s 12232KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 124, in <module>
    spos = getStarPos(hip,  getDeltaTime(stime, etime, 60), tsys, dms)
  File "prog.py", line 47, in getStarPos
    r=urlopen('http://e...content-available-to-author-only...c.jp/cgi-bin/koyomi/cande/stars_rhip.cgi',urlencode(params))
  File "/usr/lib/python2.7/urllib.py", line 88, in urlopen
    return opener.open(url, data)
  File "/usr/lib/python2.7/urllib.py", line 209, in open
    return getattr(self, name)(url, data)
  File "/usr/lib/python2.7/urllib.py", line 344, in open_http
    h.endheaders(data)
  File "/usr/lib/python2.7/httplib.py", line 958, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 818, in _send_output
    self.send(msg)
  File "/usr/lib/python2.7/httplib.py", line 780, in send
    self.connect()
  File "/usr/lib/python2.7/httplib.py", line 761, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python2.7/socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
IOError: [Errno socket error] [Errno -2] Name or service not known