Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.stsci.edu/spst/UnixTransition/doc/orbit_util.py
Дата изменения: Fri Feb 28 14:46:10 2014
Дата индексирования: Sat Mar 1 15:15:30 2014
Кодировка:

Поисковые слова: п п п п р п р п р п р п р п р п
#
# Module orbit_util
#
#***********************************************************************
"""
**PURPOSE** --
A module for dealing with orbit files.

**DEVELOPER** --
Don Chance

**MODIFICATION HISTORY** --
Initial implemetation 09/12/00
Fixed constructor so it works when the orbit file is not on disk. drc
10/23/00
Added method for computing the beta angle. 5/20/05 mdr
Updated for move to python 2.4.1 8/17/05 drc
Add function to get the latest orbit file. 7/12/07 drc
Updated for DB changes with SPSS 49.7. 1/15/09 drc
Fix get_latest_extrap_orbit_file 8/25/09 drc
"""
#***********************************************************************
import time_util, stpydb, string, spss_sys_util, re

__version__ = "8/25/09"

def get_latest_orbit_file():
"""Return the latest normal orbit file.
"""
spss_db = spss_sys_util.get_environ_variable("SPSS_DB")[0]
dbconnection = stpydb.stpydb(dbmsName=spss_db)
result = {}
dbconnection.query("select file_name, create_date")
dbconnection.query("from orbit_file_c where file_type = 'N' having create_date=max(create_date)")
while dbconnection.execute(result):
pass
return orbit_file(result['file_name'])

def get_latest_extrap_orbit_file():
"""Return the latest extrapolated orbit file.
"""
spss_db = spss_sys_util.get_environ_variable("SPSS_DB")[0]
dbconnection = stpydb.stpydb(dbmsName=spss_db)
result = {}
dbconnection.query("select file_name, create_date")
dbconnection.query("from orbit_file_c")
dbconnection.query("where file_name like 'OR_____X_'")
dbconnection.query("and file_type = 'X'")
dbconnection.query("having create_date=max(create_date)")
while dbconnection.execute(result):
pass
return orbit_file(result['file_name'])

class orbit_file:
"""A class dealing with orbit files.
"""
def __init__(self, name, ver=1):
"""Orbit file object constructor

If the 'name' parameter contains alphanumeric characters followed
by a colon followed by a number, part before the colon is
interpreted as the orbit file name and the part after the colon as
the orbit file version. The 'ver' parameter will be ignored if a
version is given in 'name'.

If the 'name' parameter does not contain a colon, then 'name' will
be the orbit file name and the version number will be the 'ver'
parameter with a default of 1.
"""
if re.findall("(\w+):(\d*)",name):
[(name, ver)] = re.findall("(\w+):(\d*)",name)
self.orbit_file_name = string.upper(string.strip(name))
self.orbit_file_version = int(ver)
self.db = spss_sys_util.get_environ_variable("SPSS_DB")[0]
self.get_orbitdata()
if self.orbitdata['file_name']:
self.get_fitdata()

def __repr__(self):
"""The string representation of an orbit file object.

Returns the orbit file name and version.
"""
return self.orbit_file_name + ":%i" % self.orbit_file_version

def __getattr__(self, name):
if name == 'period':
return self.get_period()
elif name == 'fit_name':
return self.orbitdata['st_fit']
else:
raise AttributeError("'orbit_file' object has no attribute '%s'" % str(name))


def __nonzero__(self):
return True

def exists(self):
"""Check for the existance of the orbit file.

Checks the SPSSORB directory for the existance of the proper
.dat file. Returns TRUE (1) if the file is found, FALSE (0) otherwise.
"""
if spss_sys_util.resolver("SPSSORB",string.lower(self.orbit_file_name) +
".dat_%i" % self.orbit_file_version):
return 1
else:
return 0


def get_period(self):
"""Returns the orbital period as a delta_time object.

The orbital period is derived from the g2 field in the fit_cat relation.
"""
self.__dict__['period'] = time_util.delta_time(3600*0.1/self.fitdata['g2'])
return self.__dict__['period']

def get_fitdata(self):
"""Populate 'fitdata' with a dictionary containing all the information
from the relatation fit_cat relevant to this orbit file.
"""
dbconnection = stpydb.stpydb(dbmsName=self.db)
result = {}
dbconnection.query("select * from fit_cat where")
dbconnection.query("fit_name = @name")
dbconnection.query("order by create_date asc")

dbconnection.setParam("name", self.fit_name)

while dbconnection.execute(result):
pass

self.fitdata = result

def get_orbitdata(self):
"""Populate 'orbitdata' with a dictionary containing all the information
from the relation orbit_file_c relevant to this orbit file.
"""
dbconnection = stpydb.stpydb(dbmsName=self.db)
result = {}
dbconnection.query("select * from orbit_file_c where")
dbconnection.query("file_name = @name and")
dbconnection.query("version_num = @ver")

dbconnection.setParam([["name", self.orbit_file_name],
["ver", self.orbit_file_version]])
while dbconnection.execute(result):
pass

self.orbitdata = result

def get_ra_northpoint(self,t):
"""returns right ascension of the northpoint for the given time.
"""
if not self.fitdata:
self.get_fitdata()

fit_start_time = time_util.spss_time(self.fitdata['fit_start'])
t = time_util.spss_time(t)

#self.fitdata['f3_1'] is right ascension of ascending node at fit_start.
#self.fitdata['f3_2'] is the change in the right ascension of
#ascending node wrt time.

ra_north_point = (self.fitdata['f3_1'] + 90.0 +
self.fitdata['f3_2']*(float(t-fit_start_time)))%360

return ra_north_point

def compute_beta_angle(self, t):
"""Returns the orbit beta angle for the given time
Note: though this routine will return an answer that is likely reasonable,
the resulting beta angle should only be relied upon if t is within
the timespan of the orbit file.
"""

import math

if not self.fitdata:
self.get_fitdata()
# end if

epoch = time_util.spss_time(self.fitdata['fit_start'])
t = time_util.spss_time(t)

# Now get the sun ra and dec
if spss_sys_util.on_mac():
# If on a Mac, then SPSS is not available so use PyEphem
import ephem

solar_time = t.strftime('%Y/%m/%d %H:%M:%S')
sunephem = ephem.Sun()
sunephem.compute(solar_time)

sun_ra = float(sunephem.ra)*180.0/math.pi
sun_dec = float(sunephem.dec)*180.0/math.pi
else:
# Assume SPSS is available...PyEphem doesn't build on Solaris
sendt = t + 1

cmdstr = 'cheby -display sun -start=%s -end=%s -delta=2 -device=table' % (t, sendt)
status, result = spss_sys_util.command(cmdstr)

position = string.split(string.split(result, '\n')[8])
sun_ra = float(position[1])
sun_dec = float(position[2])
# end if

s_ra_rad = sun_ra * math.pi / 180.0
s_dec_rad = sun_dec * math.pi / 180.0

# Delta time between the time-of-interest and the epoch of the fit
dtime = float(t - epoch)

# Get the RA of the Ascending Node Crossing at the time-of-interest
raan = self.fitdata['f3_1'] + self.fitdata['f3_2']*dtime + self.fitdata['f3_3']*dtime**2
raan_rad = raan * math.pi / 180.0

# Get the orbit inclination at the time-of-interest
incl = self.fitdata['f4_1'] + self.fitdata['f4_2']*dtime + self.fitdata['f4_3']*dtime**2
incl_rad = incl * math.pi /180.0

# Compute the beta angle at the time-of-interest
beta_rad = math.asin((math.cos(s_dec_rad) * math.sin(incl_rad) * \
math.sin(raan_rad - s_ra_rad)) + \
(math.sin(s_dec_rad) * math.cos(incl_rad)))
beta = beta_rad * 180.0 / math.pi

return beta

def get_north_point_times(self, begin_time, end_time, step_size=3600):
"""Return a list of times when the RA of north point HST's orbit is
the same as the RA of the sun.
"""
import ephem, math
begin_time = time_util.spss_time(begin_time)
end_time = time_util.spss_time(end_time)
sunephem = ephem.Sun()
t = begin_time - step_size
np_times = []
diffs = []
while t < (end_time + step_size):
sunephem.compute(t.strftime('%Y/%m/%d %H:%M:%S'))
diffs.append(abs(float(sunephem.ra)*180.0/math.pi - self.get_ra_northpoint(t)))

if len(diffs) > 2 and diffs[-2] < diffs[-3] and diffs[-2] < diffs[-1]:
np_times.append(t)

t += step_size
return np_times