def test_orbfit_ephemeris_function(self): from rockfinder import orbfit_ephemeris this = orbfit_ephemeris(log=log, settings=settings, objectId=1, mjd=57916., verbose=True) print this from rockfinder import orbfit_ephemeris this = orbfit_ephemeris(log=log, settings=settings, objectId=1, mjd=[57916., 57916.1, 57916.2]) print this
def test_orbfit_ephemeris_function02(self): from rockfinder import orbfit_ephemeris this = orbfit_ephemeris(log=log, settings=settings, objectId=range(1500), mjd=57916., verbose=True) print this
def test_orbfit_ephemeris_function02(self): from rockfinder import orbfit_ephemeris this = orbfit_ephemeris( log=log, settings=settings, objectId=range(1500), mjd=57916., verbose=True ) print this
def test_orbfit_ephemeris_function(self): from rockfinder import orbfit_ephemeris this = orbfit_ephemeris( log=log, settings=settings, objectId=1, mjd=57916., verbose=True ) print this from rockfinder import orbfit_ephemeris this = orbfit_ephemeris( log=log, settings=settings, objectId=1, mjd=[57916., 57916.1, 57916.2] ) print this
def test_orbfit_ephemeris_w_custom_oe_file_function(self): from rockfinder import orbfit_ephemeris this = orbfit_ephemeris(log=log, settings=settings, objectId=1, mjd=57916., verbose=True, astorbPath=pathToInputDir + "/astorb-partial.dat") print this from rockfinder import orbfit_ephemeris this = orbfit_ephemeris(log=log, settings=settings, objectId=1, mjd=[57916., 57916.1, 57916.2], astorbPath=pathToInputDir + "/astorb-partial.dat") print this
def test_orbfit_ephemeris_function_exception(self): from rockfinder import orbfit_ephemeris try: this = orbfit_ephemeris(log=log, settings=settings, fakeKey="break the code") this.get() assert False except Exception, e: assert True print str(e)
def test_orbfit_ephemeris_w_custom_oe_file_function(self): from rockfinder import orbfit_ephemeris this = orbfit_ephemeris( log=log, settings=settings, objectId=1, mjd=57916., verbose=True, astorbPath=pathToInputDir + "/astorb-partial.dat" ) print this from rockfinder import orbfit_ephemeris this = orbfit_ephemeris( log=log, settings=settings, objectId=1, mjd=[57916., 57916.1, 57916.2], astorbPath=pathToInputDir + "/astorb-partial.dat" ) print this
def test_orbfit_ephemeris_function_exception(self): from rockfinder import orbfit_ephemeris try: this = orbfit_ephemeris( log=log, settings=settings, fakeKey="break the code" ) this.get() assert False except Exception, e: assert True print str(e)
def main(arguments=None): """ *The main function used when ``cl_utils.py`` is run as a single script from the cl, or when installed as a cl command* """ # setup the command-line util settings su = tools( arguments=arguments, docString=__doc__, logLevel="WARNING", options_first=False, projectName="rockfinder", defaultSettingsFile=True ) arguments, settings, log, dbConn = su.setup() # unpack remaining cl arguments using `exec` to setup the variable names # automatically for arg, val in arguments.iteritems(): if arg[0] == "-": varname = arg.replace("-", "") + "Flag" else: varname = arg.replace("<", "").replace(">", "") if isinstance(val, str) or isinstance(val, unicode): exec(varname + " = '%s'" % (val,)) else: exec(varname + " = %s" % (val,)) if arg == "--dbConn": dbConn = val log.debug('%s = %s' % (varname, val,)) ## START LOGGING ## startTime = times.get_now_sql_datetime() log.info( '--- STARTING TO RUN THE cl_utils.py AT %s' % (startTime,)) # CALL FUNCTIONS/OBJECTS if init: from os.path import expanduser home = expanduser("~") filepath = home + "/.config/rockfinder/rockfinder.yaml" cmd = """open %(filepath)s""" % locals() p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) try: cmd = """open %(filepath)s""" % locals() p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) except: pass try: cmd = """start %(filepath)s""" % locals() p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) except: pass return if where and orbfitFlag: from rockfinder import orbfit_ephemeris eph = orbfit_ephemeris( log=log, objectId=objectId, mjd=mjd, obscode=obscode, settings=settings, verbose=extraFlag ) else: from rockfinder import jpl_horizons_ephemeris eph = jpl_horizons_ephemeris( log=log, objectId=objectId, mjd=mjd, obscode=obscode, verbose=extraFlag ) dataSet = list_of_dictionaries( log=log, listOfDictionaries=eph ) # xfundamentals-render-list-of-dictionaries output = dataSet.table(filepath=None) if csv: output = dataSet.csv(filepath=None) elif json: output = dataSet.json(filepath=None) elif yaml: output = dataSet.yaml(filepath=None) elif md: output = dataSet.markdown(filepath=None) elif rst: output = dataSet.reST(filepath=None) print output if "dbConn" in locals() and dbConn: dbConn.commit() dbConn.close() ## FINISH LOGGING ## endTime = times.get_now_sql_datetime() runningTime = times.calculate_time_difference(startTime, endTime) log.info('-- FINISHED ATTEMPT TO RUN THE cl_utils.py AT %s (RUNTIME: %s) --' % (endTime, runningTime, )) return
def main(arguments=None): """ *The main function used when ``cl_utils.py`` is run as a single script from the cl, or when installed as a cl command* """ # setup the command-line util settings su = tools(arguments=arguments, docString=__doc__, logLevel="WARNING", options_first=False, projectName="rockfinder", defaultSettingsFile=True) arguments, settings, log, dbConn = su.setup() # unpack remaining cl arguments using `exec` to setup the variable names # automatically for arg, val in arguments.iteritems(): if arg[0] == "-": varname = arg.replace("-", "") + "Flag" else: varname = arg.replace("<", "").replace(">", "") if isinstance(val, str) or isinstance(val, unicode): exec(varname + " = '%s'" % (val, )) else: exec(varname + " = %s" % (val, )) if arg == "--dbConn": dbConn = val log.debug('%s = %s' % ( varname, val, )) ## START LOGGING ## startTime = times.get_now_sql_datetime() log.info('--- STARTING TO RUN THE cl_utils.py AT %s' % (startTime, )) # CALL FUNCTIONS/OBJECTS if init: from os.path import expanduser home = expanduser("~") filepath = home + "/.config/rockfinder/rockfinder.yaml" cmd = """open %(filepath)s""" % locals() p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) try: cmd = """open %(filepath)s""" % locals() p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) except: pass try: cmd = """start %(filepath)s""" % locals() p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) except: pass return if where and orbfitFlag: from rockfinder import orbfit_ephemeris eph = orbfit_ephemeris(log=log, objectId=objectId, mjd=mjd, obscode=obscode, settings=settings, verbose=extraFlag) else: from rockfinder import jpl_horizons_ephemeris eph = jpl_horizons_ephemeris(log=log, objectId=objectId, mjd=mjd, obscode=obscode, verbose=extraFlag) dataSet = list_of_dictionaries(log=log, listOfDictionaries=eph) # xfundamentals-render-list-of-dictionaries output = dataSet.table(filepath=None) if csv: output = dataSet.csv(filepath=None) elif json: output = dataSet.json(filepath=None) elif yaml: output = dataSet.yaml(filepath=None) elif md: output = dataSet.markdown(filepath=None) elif rst: output = dataSet.reST(filepath=None) print(output) if "dbConn" in locals() and dbConn: dbConn.commit() dbConn.close() ## FINISH LOGGING ## endTime = times.get_now_sql_datetime() runningTime = times.calculate_time_difference(startTime, endTime) log.info( '-- FINISHED ATTEMPT TO RUN THE cl_utils.py AT %s (RUNTIME: %s) --' % ( endTime, runningTime, )) return
class orbfitPositions(): """ *The worker class for the orbfitPositions module* **Key Arguments:** - ``log`` -- logger - ``settings`` -- the settings dictionary - ``dev_flag`` -- use the dev_flag column in the database to select out specific ATLAS exposures to work with. Default *False* **Usage:** To setup your logger, settings and database connections, please use the ``fundamentals`` package (`see tutorial here <http://fundamentals.readthedocs.io/en/latest/#tutorial>`_). To initiate an orbfitPositions object, generate the positions for moving objects that already have PyEphem positions and finally add the results to the `orbfit_positions` database table, use the following: .. code-block:: python from rockAtlas.positions import orbfitPositions oe = orbfitPositions( log=log, settings=settings ) oe.get() """ # INITIALISATION def __init__(self, log, settings=False, dev_flag=False): self.log = log log.debug("instansiating a new 'orbfitPositions' object") self.settings = settings self.dev_flag = dev_flag # xt-self-arg-tmpx # INITIAL ACTIONS # SETUP ALL DATABASE CONNECTIONS from rockAtlas import database db = database(log=log, settings=settings) dbConns, dbVersions = db.connect() self.atlas3DbConn = dbConns["atlas3"] self.atlas4DbConn = dbConns["atlas4"] self.atlasMoversDBConn = dbConns["atlasMovers"] return None def get(self, singleExposure=False): """ *get the orbfitPositions object* **Key Arguments:** - ``singleExposure`` -- only execute fot a single exposure (useful for debugging) **Return:** - None **Usage:** See class docstring """ self.log.info('starting the ``get`` method') if singleExposure: batchSize = 1 else: batchSize = int(self.settings["orbfit"]["batch size"]) exposureCount = 1 while exposureCount > 0: expsoureObjects, astorbString, exposureCount = self._get_exposures_requiring_orbfit_positions( batchSize=batchSize) if exposureCount: orbfitPositions = self._get_orbfit_positions( expsoureObjects, astorbString) self._add_orbfit_eph_to_database(orbfitPositions, expsoureObjects) if singleExposure: exposureCount = 0 self.log.info('completed the ``get`` method') return None def _get_exposures_requiring_orbfit_positions(self, batchSize=25): """*get next batch of exposures requiring orbfit positions* **Key Arguments:** - ``batchSize`` -- number of exposures to process per batch **Return:** - ``expsoureObjects`` -- a dictionary with exposure names as keys and a dictionary of exposure ra, dec and moving object names/mpc number as values - ``astorbString`` -- as string of the object orbital elements to be used later for orbfit ephemerides """ self.log.info( 'starting the ``_get_exposures_requiring_orbfit_positions`` method' ) if self.dev_flag == True: dev_flag = " and dev_flag = 1" else: dev_flag = "" sqlQuery = u""" select count(*) as count from atlas_exposures where pyephem_positions = 1 and orbfit_positions = 0 and local_data = 1 %(dev_flag)s order by pyephem_mjd asc """ % locals() rows = readquery(log=self.log, sqlQuery=sqlQuery, dbConn=self.atlasMoversDBConn, quiet=False) exposureCount = rows[0]["count"] if batchSize > 1: text = "%(batchSize)s exposures" % locals() else: text = "%(batchSize)s exposure" % locals() if exposureCount: print "There are currently %(exposureCount)s ATLAS exposures with pyephem positions needing tightened - processing the next %(text)s with orbfit" % locals( ) else: print "There are no more ATLAS exposures requiring to be processed with orbfit" return {}, '', exposureCount sqlQuery = u""" SELECT a.expname, a.mjd, o.primaryId, a.raDeg, a.decDeg, p.object_name, p.mpc_number, o.astorb_string, a.mjd+a.exp_time/(2*3600*24) as `mjd_mid` FROM atlas_exposures a, pyephem_positions p, orbital_elements o WHERE a.expname=p.expname %(dev_flag)s and p.object_name=o.name and a.expname IN (SELECT * FROM (SELECT expname FROM atlas_exposures WHERE pyephem_positions = 1 AND orbfit_positions = 0 and local_data = 1 order by mjd LIMIT %(batchSize)s) AS a); """ % locals() objects = readquery(log=self.log, sqlQuery=sqlQuery, dbConn=self.atlasMoversDBConn, quiet=False) expsoureObjects = {} astorbDict = {} for o in objects: if o["expname"] not in expsoureObjects: expsoureObjects[o["expname"]] = { "ra": o["raDeg"], "dec": o["decDeg"], "mjd": o["mjd_mid"], "objects": [[o["mpc_number"], o["object_name"], o["primaryId"]]] } else: expsoureObjects[o["expname"]]["objects"].append( [o["mpc_number"], o["object_name"], o["primaryId"]]) if o["object_name"] not in astorbDict: astorbDict[o["object_name"]] = o["astorb_string"] astorbString = ("\n").join(astorbDict.values()) self.log.info( 'completed the ``_get_exposures_requiring_orbfit_positions`` method' ) return expsoureObjects, astorbString, exposureCount def _get_orbfit_positions(self, expsoureObjects, astorbString): """* get orbfit positions* **Key Arguments:** - ``expsoureObjects`` -- a dictionary with exposure names as keys and a dictionary of exposure ra, dec and moving object names/mpc number as values - ``astorbString`` -- as string of the object orbital elements to be used with orbfit **Return:** - ``orbfitMatches`` -- all of the ephemerides generated by Orbfit that match against the exact ATLAS exposure footprint """ self.log.info('starting the ``_get_orbfit_positions`` method') # WRITE THE CUSTOM ORBFIT DATABASE TO FILE now = datetime.now() astorbFilepath = now.strftime("/tmp/astorb-%Y%m%dt%H%M%S%f.dat") try: self.log.debug("attempting to open the file %s" % (astorbFilepath, )) writeFile = codecs.open(astorbFilepath, encoding='utf-8', mode='w') except IOError, e: message = 'could not open the file %s' % (astorbFilepath, ) self.log.critical(message) raise IOError(message) writeFile.write(astorbString) writeFile.close() orbfitMatches = [] for expname, v in expsoureObjects.iteritems(): raDeg = v["ra"] decDeg = v["dec"] mjd = v["mjd"] objects = {} for o in v["objects"]: if o[0]: objects[o[0]] = [o[0], o[1], o[2]] else: objects[o[1].replace(" ", "")] = [o[0], o[1], o[2]] if expname[:2] == "02": obscode = "T05" elif expname[:2] == "01": obscode = "T08" orbfitEph = orbfit_ephemeris(log=self.log, obscode=obscode, objectId=objects.keys(), mjd=mjd, settings=self.settings, verbose=True, astorbPath=astorbFilepath) for o in orbfitEph: o["expname"] = expname o["mpc_number"] = objects[o["object_name"]][0] o["orbital_elements_id"] = objects[o["object_name"]][2] o["object_name"] = objects[o["object_name"]][1] matchedEph = self._match_objects_against_atlas_footprint( orbfitEph=orbfitEph, ra=raDeg, dec=decDeg) orbfitMatches += matchedEph try: os.remove(astorbFilepath) except: pass self.log.info('completed the ``_get_orbfit_positions`` method') return orbfitMatches