#!/usr/bin/env python import ephem import time import os import sys from MmdDb import Db from MmdLocation import Location def createJavaArray (point_list, name): astring = '''var {0} = new Array (\n'''.format (name) astring = '{0}\tnew Array ('.format (astring) last = 'west' for point in point_list: if point[0][0] == '-': if last == 'east': astring = astring[:-1] astring = '{0}\n\t\t),\n\tnew Array ('.format (astring) last = 'west' else: last = 'east' astring = '{0}\n\t\tnew OpenLayers.LonLat ({1}, {2}).transform (from, to),'.format (astring, point[0], point[1]) if astring[-1] == ',': astring = astring[:-1] astring = '{0}\n\t\t)\n\t);'.format (astring) return astring def dms2degdec (lonlat): ''' convert a position argument from Deg:Min:Sec.Frac to Deg.Frac and round the result to 5 decimal points ''' parts = lonlat.split (':') if len (parts) != 3: return 0.0 degrees = float (parts[0]) fraction = (float (parts[1]) * 60.0 + float (parts[2])) / 3600 if parts[0][0] == '-': return round (degrees - fraction, 5) return round (degrees + fraction, 5) class Satellite: def __init__ (self, location = False): self.name = 'RADIOSCAF-B/ARISSAT-1' self.tle_filename = '/usr/local/mmd/tles/current' self.db = Db () self.initSatellite (location) def getCurrentSSP (self): ssp = self._loadCurrentSSP () return ssp['timestamp'], ssp['longitude'], ssp['latitude'] def getTrajectoryAsJavaArray (self, name, minutes = 180): astring = '''var {0} = new Array (\n'''.format (name) astring = '{0}\tnew Array ('.format (astring) last = 'west' for row in self._loadTrajectory (minutes): if str (row['longitude'])[0] == '-': if last == 'east': astring = astring[:-1] astring = '{0}\n\t\t),\n\tnew Array ('.format (astring) last = 'west' else: last = 'east' astring = '{0}\n\t\tnew OpenLayers.LonLat ({1}, {2}).transform (from, to),'.format ( astring, row['longitude'], row['latitude']) if astring[-1] == ',': astring = astring[:-1] astring = '{0}\n\t\t)\n\t);'.format (astring) return astring def _loadTrajectory (self, minutes): return self.db.satelliteLoadTrajectory (time.strftime ('%s'), minutes) def _loadCurrentSSP (self): return self.db.satelliteLoadCurrentSSP (time.strftime ('%s')) def initSatellite (self, location = False): if not self._loadTLE (): return False if location: loc = location else: loc = Location () sat = ephem.readtle (self._tle[0], self._tle[1], self._tle[2]) obs = ephem.Observer () obs.long, obs.lat, obs.elevation = loc.longitude, loc.latitude, loc.altitude sat.compute (obs) self.longitude = dms2degdec (sat.sublong.__str__ ()) self.latitude = dms2degdec (sat.sublat.__str__ ()) self.altitude = round (float (sat.elevation.__str__ ()), 1) def _computeTrajectory (self): ''' computes 5 hours of future trajectory data, starting at the latest timestamp found in data base ''' if not self._loadTLE (): return False sat = ephem.readtle (self._tle[0], self._tle[1], self._tle[2]) graz = ephem.Observer () graz.long, graz.lat, graz.elevation = 15.44226, 47.06576, 376 latest = self.db.satelliteGetLatestSSP () if latest: timestamp = int (latest['timestamp']) + 60 else: timestamp = int (time.strftime ('%s')) + 60 while timestamp % 60 != 0: timestamp += 1 for i in range (60 * 5): graz.date = time.strftime ('%Y/%m/%d %H:%M:%S', time.gmtime (timestamp)) sat.compute (graz) self.db.satelliteInsertNewSSP (timestamp, dms2degdec (sat.sublong.__str__ ()), dms2degdec (sat.sublat.__str__ ())) timestamp += 60 return True def _loadTLE (self): try: tle = open (self.tle_filename, 'r') except IOError: return False self._tle = [] count = 0 for line in tle.readlines (): self._tle.append (line) count += 1 if count != 3: return False return True def cronJob (self): if self._updateTLEFile (): self._updateTrajectory () return self._computeTrajectory () def _updateTLEFile (self): return False def _updateTrajectory (self): self.db.satelliteDeleteObsoleteSSPs (time.strftime ('%s')) def getLatestSSP (self): return self.db.satelliteGetLatestSSP () if __name__ == "__main__": satellite = Satellite () try: assert satellite.cronJob (), 'executing cronjob failed' except AssertionError as e: print 'Error: {0}'.format (e) sys.exit (1) sys.exit (0) # vim: tw=0 ts=2 # EOF