data_generator.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import sys, os
  2. sys.path.append("/var/lib/django")
  3. from django.core.management import setup_environ
  4. from pbp_com import settings
  5. setup_environ(settings)
  6. import re
  7. import urllib
  8. import urllib2
  9. from BeautifulSoup import BeautifulSoup
  10. import time
  11. from datetime import datetime, timedelta
  12. import dateutil
  13. from almanac.models import Day, AstroChart
  14. year = "2014"
  15. place = "Stonington"
  16. state = "ME"
  17. def get_table(year="", state="", place="", type=""):
  18. parser = re.compile("\d\d ")
  19. """A special parser for lunar fraction tables """
  20. lparser = re.compile(" \d\d ")
  21. if type == "sun":
  22. type = 0
  23. elif type == "moon":
  24. type = 1
  25. elif type == "civil_tw":
  26. type = 2
  27. elif type == "nautical_tw":
  28. type = 3
  29. elif type == "astro_tw":
  30. type = 4
  31. elif type == "lunar_fraction":
  32. type = 5
  33. else:
  34. type = 0
  35. if type < 5:
  36. url = "http://aa.usno.navy.mil/cgi-bin/aa_rstablew.pl"
  37. post_data = {
  38. "FFX": "1",
  39. "xxy": year,
  40. "type": type,
  41. "st": state,
  42. "place": place,
  43. "ZZZ": "END",
  44. }
  45. else:
  46. url = "http://aa.usno.navy.mil/cgi-bin/aa_moonill2.pl"
  47. post_data = {"time": "00", "xxy": year, "zone": "-05", "ZZZ": "END"}
  48. data = urllib.urlencode(post_data)
  49. req = urllib2.Request(url, data)
  50. response = urllib2.urlopen(req)
  51. page = response.read()
  52. data = page.split("\n")
  53. table = []
  54. for line in data:
  55. if type < 5:
  56. if parser.match(line):
  57. table.append(line)
  58. else:
  59. if lparser.match(line):
  60. table.append(line)
  61. return table
  62. """ End of get_table() """
  63. def fix_time(str, today, chart):
  64. if str == " ":
  65. str = None
  66. else:
  67. if chart.dst_start < today < chart.dst_end:
  68. delta = timedelta(hours=1)
  69. else:
  70. delta = timedelta(hours=0)
  71. str = time.strftime(
  72. "%H:%M", (datetime(*time.strptime(str, "%H%M")[0:5]) + delta).timetuple()
  73. )
  74. return str
  75. """ End of fix_time() """
  76. """### THE HEAVY LIFTING ### """
  77. sun_table = get_table(year, state, place, "sun")
  78. moon_table = get_table(year, state, place, "moon")
  79. civil_tw_table = get_table(year, state, place, "civil_tw")
  80. naut_tw_table = get_table(year, state, place, "nautical_tw")
  81. astro_tw_table = get_table(year, state, place, "astro_tw")
  82. lunar_fraction = get_table(year, "", "", "lunar_fraction")
  83. month = 0
  84. astro_table = []
  85. """ Here we go over the table 12 times for each month.
  86. For each month, we keep running till we hit a blank.
  87. Load the day, month and year into the table and go. """
  88. offset = 4
  89. moon_iter = 0
  90. civil_iter = 0
  91. naut_iter = 0
  92. astro_iter = 0
  93. lunar_offset = 8
  94. lunar_iter = 0
  95. try:
  96. chart = AstroChart.objects.get(year__exact=year)
  97. except AstroChart.DoesNotExist:
  98. pass # Add code here to add chart if it doesn't exist
  99. while month < 12:
  100. month = month + 1
  101. for entry in sun_table:
  102. if entry[offset : offset + 9] == " ":
  103. pass
  104. else:
  105. if month < 10:
  106. date = datetime.strptime(
  107. year + "0" + str(month) + entry[:2], "%Y%m%d"
  108. ).date()
  109. else:
  110. date = datetime.strptime(year + str(month) + entry[:2], "%Y%m%d").date()
  111. astro_table.append(
  112. {
  113. "date": date,
  114. "sunrise": fix_time(entry[offset : offset + 4], date, chart),
  115. "sunset": fix_time(entry[offset + 5 : offset + 9], date, chart),
  116. }
  117. )
  118. for entry in moon_table:
  119. if entry[offset : offset + 9] == " ":
  120. pass
  121. else:
  122. date = astro_table[moon_iter]["date"]
  123. astro_table[moon_iter]["moonrise"] = fix_time(
  124. entry[offset : offset + 4], date, chart
  125. )
  126. astro_table[moon_iter]["moonset"] = fix_time(
  127. entry[offset + 5 : offset + 9], date, chart
  128. )
  129. moon_iter = moon_iter + 1
  130. for entry in civil_tw_table:
  131. if entry[offset : offset + 9] == " ":
  132. pass
  133. else:
  134. date = astro_table[civil_iter]["date"]
  135. astro_table[civil_iter]["civil_twilight_am"] = fix_time(
  136. entry[offset : offset + 4], date, chart
  137. )
  138. astro_table[civil_iter]["civil_twilight_pm"] = fix_time(
  139. entry[offset + 5 : offset + 9], date, chart
  140. )
  141. civil_iter = civil_iter + 1
  142. for entry in naut_tw_table:
  143. if entry[offset : offset + 9] == " ":
  144. pass
  145. else:
  146. date = astro_table[naut_iter]["date"]
  147. astro_table[naut_iter]["nautical_twilight_am"] = fix_time(
  148. entry[offset : offset + 4], date, chart
  149. )
  150. astro_table[naut_iter]["nautical_twilight_pm"] = fix_time(
  151. entry[offset + 5 : offset + 9], date, chart
  152. )
  153. naut_iter = naut_iter + 1
  154. """
  155. Want astronomical twilight?
  156. for entry in astro_tw_table:
  157. if entry[offset:offset+9] == ' ':
  158. pass
  159. else:
  160. astro_table[astro_iter]['astro_tw_start']=entry[offset:offset+4]
  161. astro_table[astro_iter]['astro_tw_end']=entry[offset+5:offset+9]
  162. astro_iter = astro_iter+1
  163. """
  164. for entry in lunar_fraction:
  165. if entry[lunar_offset : lunar_offset + 4] == " ":
  166. pass
  167. else:
  168. frac = entry[lunar_offset : lunar_offset + 4]
  169. if frac == "1.00":
  170. astro_table[lunar_iter]["moonphase"] = 2 # FULL
  171. elif frac == "0.00":
  172. astro_table[lunar_iter]["moonphase"] = 0 # NEW
  173. elif 0.6 > float(frac) > 0.5 and past_phase == "full":
  174. astro_table[lunar_iter]["moonphase"] = 3 # 3rd Q.
  175. elif 0.6 > float(frac) > 0.5 and past_phase == "new":
  176. astro_table[lunar_iter]["moonphase"] = 1 # 1st Q.
  177. astro_table[lunar_iter]["moonphase_fraction"] = entry[
  178. lunar_offset : lunar_offset + 4
  179. ]
  180. lunar_iter = lunar_iter + 1
  181. if frac == "1.00":
  182. past_phase = "full"
  183. elif frac == "0.00":
  184. past_phase = "new"
  185. lunar_offset = lunar_offset + 9
  186. offset = offset + 11
  187. """ Load the data into the DB """
  188. for entry in astro_table:
  189. print entry
  190. entry["chart"] = chart
  191. day = Day.objects.create(**entry)
  192. day.save()