def test_Astral_Nighttime(): a = Astral() l = a['London'] d = datetime.date(2016, 1, 6) start, end = a.night_utc(d, l.latitude, l.longitude) cstart = datetime.datetime(2016, 1, 6, 18, 10, 0, tzinfo=pytz.UTC) cend = datetime.datetime(2016, 1, 7, 6, 2, 0, tzinfo=pytz.UTC) assert datetime_almost_equal(start, cstart, 300) assert datetime_almost_equal(end, cend, 300)
def main(): srtdb = sqlite3.connect( '../srtdatabase/srtdata.db' ) # establish a connection and cursor into the database srtdb.row_factory = sqlite3.Row cur = srtdb.cursor() config = cur.execute("SELECT * FROM CONFIG").fetchone() a = Astral() today = datetime.date.today() nighttimes = a.night_utc(today, config['lat'], config['lon']) print(nighttimes[0].isoformat() + ' ' + nighttimes[1].isoformat()) dusk = nighttimes[0].timestamp() dawn = nighttimes[1].timestamp() schedule = Schedule(dusk, dawn) cur.execute("DELETE FROM SCHEDULE") # cur.execute("INSERT INTO SOURCES VALUES (?,?,?)", ('sigma octantis', '21h8m47s', '-88d57m23s')) # cur.execute("INSERT INTO SCANIDS VALUES (?,?,?)", (-30, 'invalidtest', 'submitted')) # cur.execute("INSERT INTO SCANPARAMS VALUES (?,?,?,?,?,?,?,?,?)", (-30, 'track', 'sigma octantis', '21h8m47s', '-88d57m23s', '1h0m0s', 1500, 1510, 10)) # cur.execute("UPDATE CONFIG SET LAT = ?, LON = ?", (44.45, -93.16)) # cur.execute("INSERT INTO SOURCES VALUES (?,?,?)", ('polaris', '2h31m49s', '89d15m50s')) # cur.execute("INSERT INTO SCANIDS VALUES (?,?,?)", (-20, 'scheduletest', 'submitted')) # cur.execute("INSERT INTO SCANPARAMS VALUES (?,?,?,?,?,?,?,?,?)", (-20, 'track', 'polaris', '2h31m49s', '89d15m50s', '0h30m0s', 1500, 1510, 10)) # srtdb.commit() ntp = NTPTime() schedule.schedulescan(-20, ntp.getcurrenttime()) schedule.schedulescan(-30, ntp.getcurrenttime()) for block in schedule.schedule: print( str(block.scanid) + ' ' + str(block.starttime) + ' ' + str(block.endtime))
def main(): srtdb = sqlite3.connect(database_location) # establish a connection and cursor into the database srtdb.row_factory = sqlite3.Row cur = srtdb.cursor() telescope = Scan() # initialize a Scan object for running scans config = cur.execute("SELECT * FROM CONFIG").fetchone() # fetch config data from the db a = Astral() # initalize an Astral object for computer day and night times today = datetime.date.today() # get today's date daytimes = a.daylight_utc(today, config['lat'], config['lon']) # compute date and night times in UTC using the Astral object nighttimes = a.night_utc(today, config['lat'], config['lon']) print('Daystart: ' + daytimes[0].isoformat() + ', Dayend: ' + daytimes[1].isoformat()) print('Nightstart:' + nighttimes[0].isoformat() + ', Nightend: ' + nighttimes[1].isoformat()) dusk = nighttimes[0].timestamp() # get unix times for day and night dawn = nighttimes[1].timestamp() sunrise = daytimes[0].timestamp() sunset = daytimes[1].timestamp() dayschedule = Schedule.Schedule(sunrise + 7200, sunset - 7200) # instantiate day and night schedules nightschedule = Schedule.Schedule(dusk, dawn) currentscanid = None ### attempt to reschedule scans in SCHEDULE after restart ### schedule = cur.execute("SELECT * FROM SCHEDULE").fetchall() # get the schedule from the db cur.execute("DELETE FROM SCHEDULE") # delete all entries in SCHEDULE for scan in schedule: # attempt to reschedule all scheduled scans scanparams = cur.execute("SELECT * FROM SCANPARAMS WHERE ID = ?", (scan['id'],)).fetchone() curtime = ntp.getcurrenttime() if scanparams['source'] == 'sun': # if source is the sun, schedule during the day status = dayschedule.schedulescan(scanparams['id'], curtime) else: # if source is not the sun, schedule at night status = nightschedule.schedulescan(scanparams['id'], curtime) if status != 'scheduled': today = datetime.date.today() cur.execute("INSERT INTO SCANHISTORY VALUES (?,?,?,?,?,?)", (newscan['id'], scanparams['name'], scanparams['type'], today.day, today.month, today.year)) cur.execute("UPDATE SCANIDS SET STATUS = ? WHERE ID = ?", (status, newscan['id'])) srtdb.commit() ### MAIN EXECUTION LOOP FOR TELESCOPE-SIDE CODE ### while True: time.sleep(0.5) # sleep for half a second to reduce looping speed ### set whether it is day or night ### curtime = ntp.getcurrenttime() if curtime >= dusk and curtime <= dawn: currentschedule = nightschedule else: currentschedule = dayschedule ### create new schedules if current schedules are completed ### config = cur.execute("SELECT * FROM CONFIG").fetchone() # get config data from the db newday = datetime.date.today() # get today's date if newday.day != today.day or newday.month != today.month or newday.year != today.year: # if the day has changed, create new day schedule print('it\'s a new day! setting up new daytime schedule') today = newday daytimes = a.daylight_utc(today, config['lat'], config['lon']) sunrise = Time(daytimes[0], format = 'datetime', scale = 'utc').unix sunset = Time(daytimes[1], format = 'datetime', scale = 'utc').unix dayschedule = Schedule(sunrise + 7200, sunset - 7200) if curtime > dawn: # if the night is over, create new night schedule print('good morning! setting up new nighttime schedule') nighttimes = a.night_utc(today, config['lat'], config['lon']) dusk = Time(nighttimes[0], format = 'datetime', scale = 'utc').unix dawn = Time(nighttimes[1], format = 'datetime', scale = 'utc').unix nightschedule = Schedule(dusk, dawn) ### remove cancelled scans from schedules ### print('might be time to cancel some scans') cancelscans(dayschedule) cancelscans(nightschedule) ### insert a new scan into the schedule ### print('is there a new scan to add?') newscan = cur.execute("SELECT * FROM SCANIDS WHERE STATUS = ?", ('submitted',)).fetchone() if newscan != None: # check if a scan has been submitted print('yes there is!') scanparams = cur.execute("SELECT * FROM SCANPARAMS WHERE ID = ?", (newscan['id'],)).fetchone() if scanparams['source'] == 'sun': # if source is the sun, schedule during the day status = dayschedule.schedulescan(scanparams['id'], curtime) else: # if source is not the sun, schedule at night status = nightschedule.schedulescan(scanparams['id'], curtime) if status != 'scheduled': today = datetime.date.today() cur.execute("INSERT INTO SCANHISTORY VALUES (?,?,?,?,?,?)", (newscan['id'], scanparams['name'], scanparams['type'], today.day, today.month, today.year)) cur.execute("UPDATE SCANIDS SET STATUS = ? WHERE ID = ?", (status, newscan['id'])) srtdb.commit() ### remove current scan from the schedule when it finishes ### print('did the current scan finish running?') if currentscanid != None: # check to see if a scan is supposed to be running currentscan = cur.execute("SELECT * FROM SCANIDS WHERE ID = ?", (currentscanid,)).fetchone() if currentscan == None or currentscan['status'] != 'running': # if scan is not running anymore, remove from schedule and reset currentscanid print('yes it did!') cur.execute("DELETE FROM SCHEDULE WHERE ID = ?", (currentscanid,)) srtdb.commit() currentschedule.deschedulescan(currentscanid) currentscanid = None ### run the next scan in the schedule ### print('time to run a scan?') currentscanid = runscan(currentschedule)