data_generator.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 = {'FFX' : '1',
  38. 'xxy' : year,
  39. 'type' : type,
  40. 'st' : state,
  41. 'place' : place,
  42. 'ZZZ' : 'END' }
  43. else:
  44. url = "http://aa.usno.navy.mil/cgi-bin/aa_moonill2.pl"
  45. post_data = {'time':'00',
  46. 'xxy':year,
  47. 'zone':'-05',
  48. 'ZZZ':'END'}
  49. data = urllib.urlencode(post_data)
  50. req = urllib2.Request(url, data)
  51. response = urllib2.urlopen(req)
  52. page = response.read()
  53. data = page.split('\n')
  54. table=[]
  55. for line in data:
  56. if type <5:
  57. if parser.match(line):
  58. table.append(line)
  59. else:
  60. if lparser.match(line):
  61. table.append(line)
  62. return table
  63. ''' End of get_table() '''
  64. def fix_time(str, today, chart):
  65. if str == ' ':
  66. str = None
  67. else:
  68. if chart.dst_start < today < chart.dst_end:
  69. delta=timedelta(hours=1)
  70. else:
  71. delta=timedelta(hours=0)
  72. str= time.strftime("%H:%M", (datetime(*time.strptime(str, "%H%M")[0:5])+delta).timetuple())
  73. return str
  74. ''' End of fix_time() '''
  75. '''### THE HEAVY LIFTING ### '''
  76. sun_table = get_table(year, state, place, 'sun')
  77. moon_table = get_table(year, state, place, 'moon')
  78. civil_tw_table = get_table(year, state, place, 'civil_tw')
  79. naut_tw_table = get_table(year, state, place, 'nautical_tw')
  80. astro_tw_table = get_table(year, state, place, 'astro_tw')
  81. lunar_fraction = get_table(year, '', '', 'lunar_fraction')
  82. month=0
  83. astro_table=[]
  84. ''' Here we go over the table 12 times for each month.
  85. For each month, we keep running till we hit a blank.
  86. Load the day, month and year into the table and go. '''
  87. offset=4
  88. moon_iter=0
  89. civil_iter=0
  90. naut_iter=0
  91. astro_iter=0
  92. lunar_offset=8
  93. lunar_iter=0
  94. try:
  95. chart=AstroChart.objects.get(year__exact=year)
  96. except AstroChart.DoesNotExist:
  97. pass #Add code here to add chart if it doesn't exist
  98. while (month < 12):
  99. month = month +1
  100. for entry in sun_table:
  101. if entry[offset:offset+9] == ' ':
  102. pass
  103. else:
  104. if month < 10:
  105. date = datetime.strptime(year + '0' + str(month)+entry[:2], "%Y%m%d").date()
  106. else:
  107. date = datetime.strptime(year + str(month) + entry[:2], "%Y%m%d").date()
  108. astro_table.append({'date':date, 'sunrise':fix_time(entry[offset:offset+4], date,chart), 'sunset':fix_time(entry[offset+5:offset+9], date,chart)})
  109. for entry in moon_table:
  110. if entry[offset:offset+9] == ' ':
  111. pass
  112. else:
  113. date=astro_table[moon_iter]['date']
  114. astro_table[moon_iter]['moonrise']=fix_time(entry[offset:offset+4],date, chart)
  115. astro_table[moon_iter]['moonset']=fix_time(entry[offset+5:offset+9], date, chart)
  116. moon_iter = moon_iter+1
  117. for entry in civil_tw_table:
  118. if entry[offset:offset+9] == ' ':
  119. pass
  120. else:
  121. date=astro_table[civil_iter]['date']
  122. astro_table[civil_iter]['civil_twilight_am']=fix_time(entry[offset:offset+4], date, chart)
  123. astro_table[civil_iter]['civil_twilight_pm']=fix_time(entry[offset+5:offset+9], date, chart)
  124. civil_iter = civil_iter+1
  125. for entry in naut_tw_table:
  126. if entry[offset:offset+9] == ' ':
  127. pass
  128. else:
  129. date=astro_table[naut_iter]['date']
  130. astro_table[naut_iter]['nautical_twilight_am']=fix_time(entry[offset:offset+4], date, chart)
  131. astro_table[naut_iter]['nautical_twilight_pm']=fix_time(entry[offset+5:offset+9], date, chart)
  132. naut_iter = naut_iter+1
  133. '''
  134. Want astronomical twilight?
  135. for entry in astro_tw_table:
  136. if entry[offset:offset+9] == ' ':
  137. pass
  138. else:
  139. astro_table[astro_iter]['astro_tw_start']=entry[offset:offset+4]
  140. astro_table[astro_iter]['astro_tw_end']=entry[offset+5:offset+9]
  141. astro_iter = astro_iter+1
  142. '''
  143. for entry in lunar_fraction:
  144. if entry[lunar_offset:lunar_offset+4] == ' ':
  145. pass
  146. else:
  147. frac = entry[lunar_offset:lunar_offset+4]
  148. if frac == "1.00":
  149. astro_table[lunar_iter]['moonphase']=2 # FULL
  150. elif frac == "0.00":
  151. astro_table[lunar_iter]['moonphase']=0 # NEW
  152. elif 0.6 > float(frac) > 0.5 and past_phase == "full":
  153. astro_table[lunar_iter]['moonphase']=3 # 3rd Q.
  154. elif 0.6 > float(frac) > 0.5 and past_phase == "new":
  155. astro_table[lunar_iter]['moonphase']=1 # 1st Q.
  156. astro_table[lunar_iter]['moonphase_fraction']=entry[lunar_offset:lunar_offset+4]
  157. lunar_iter = lunar_iter+1
  158. if frac == "1.00":
  159. past_phase = "full"
  160. elif frac=="0.00":
  161. past_phase = "new"
  162. lunar_offset=lunar_offset +9
  163. offset=offset +11
  164. ''' Load the data into the DB '''
  165. for entry in astro_table:
  166. print entry
  167. entry['chart']=chart
  168. day=Day.objects.create(**entry)
  169. day.save()