def test_unit_conversion_ra_dec_to_cartesian_functions(self): from astrocalc.coords import unit_conversion converter = unit_conversion( log=log ) x, y, z = converter.ra_dec_to_cartesian( ra=23.454676456, dec=-3.454676456 ) print(x, y, z) from astrocalc.coords import unit_conversion converter = unit_conversion( log=log ) x, y, z = converter.ra_dec_to_cartesian( ra="23 45 21.23232", dec=-3.454676456 ) print(x, y, z) from astrocalc.coords import unit_conversion converter = unit_conversion( log=log ) cartesians = converter.ra_dec_to_cartesian( ra="23 45 21.23232", dec="+01:58:5.45341" ) print(cartesians)
def test_unit_conversion_ra_dec_to_cartesian_functions(self): from astrocalc.coords import unit_conversion converter = unit_conversion( log=log ) x, y, z = converter.ra_dec_to_cartesian( ra=23.454676456, dec=-3.454676456 ) print x, y, z from astrocalc.coords import unit_conversion converter = unit_conversion( log=log ) x, y, z = converter.ra_dec_to_cartesian( ra="23 45 21.23232", dec=-3.454676456 ) print x, y, z from astrocalc.coords import unit_conversion converter = unit_conversion( log=log ) cartesians = converter.ra_dec_to_cartesian( ra="23 45 21.23232", dec="+01:58:5.45341" ) print cartesians
def __init__( self, log, ra, dec, url="http://skyserver.sdss.org/dr12/en/tools/search/x_sql.aspx" ): self.log = log log.debug("instansiating a new 'check_coverage' object") self.ra = ra self.dec = dec self.url = url # xt-self-arg-tmpx # Initial Actions converter = unit_conversion( log=self.log ) self.raDeg = converter.ra_sexegesimal_to_decimal( ra=ra ) self.decDeg = converter.dec_sexegesimal_to_decimal( dec=dec ) return None
def intersect(self, ra, dec, radius, inclusive=True, convertCoordinates=True): """*return IDs of all triangles contained within and/or intersecting a circle centered on a given ra and dec* **Key Arguments** - ``ra`` -- RA of central point in decimal degrees or sexagesimal - ``dec`` -- DEC of central point in decimal degrees or sexagesimal - ``radius`` -- radius of circle in degrees - ``inclusive`` -- include IDs of triangles that intersect the circle as well as those completely inclosed by the circle. Default *True* - ``convertCoordinates`` -- convert the corrdinates passed to intersect. Default *True* - **Return** - ``trixelArray`` -- a numpy array of the match trixel IDs **Usage** To return the trixels overlapping a circle with a 10 arcsec radius centred at 23:25:53.56, +26:54:23.9 ```python overlappingTrixels = mesh16.intersect( ra="23:25:53.56", dec="+26:54:23.9", radius=10 / (60 * 60), inclusive=True ) ``` Or to return the trixels completing enclosed by a circle with a 1 degree radius centred at 23:25:53.56, +26:54:23.9 ```python overlappingTrixels = mesh16.intersect( ra="23:25:53.56", dec="+26:54:23.9", radius=1, inclusive=False ) ``` """ # CONVERT RA AND DEC DECIMAL DEGREES if convertCoordinates == True: converter = unit_conversion(log=self.log) ra = converter.ra_sexegesimal_to_decimal(ra=ra) dec = converter.dec_sexegesimal_to_decimal(dec=dec) if inclusive: inc = 1 else: inc = 0 return super(HTM, self).intersect(ra, dec, radius, inc)
def __init__( self, log, ra, dec, tableName, radiusArcsec, colMaps, dbConn=False, nearestOnly=False, physicalSearch=False, upperMagnitudeLimit=False, lowerMagnitudeLimit=False, magnitudeLimitFilter=False ): self.log = log log.debug("instansiating a new 'conesearcher' object") self.dbConn = dbConn self.radius = radiusArcsec self.tableName = tableName self.nearestOnly = nearestOnly self.colMaps = colMaps self.physicalSearch = physicalSearch self.upperMagnitudeLimit = upperMagnitudeLimit self.lowerMagnitudeLimit = lowerMagnitudeLimit self.magnitudeLimitFilter = magnitudeLimitFilter # xt-self-arg-tmpx # CONVERT RA AND DEC TO DEGREES if not isinstance(ra, list): ra = [ra] dec = [dec] self.ra = [] self.dec = [] converter = unit_conversion( log=self.log ) try: float(ra[0]) convert = False except: convert = True if convert == True: for r, d in zip(ra, dec): self.ra.append(converter.ra_sexegesimal_to_decimal( ra=r )) self.dec.append(converter.dec_sexegesimal_to_decimal( dec=d )) else: self.ra = ra self.dec = dec return None
def __init__( self, log, ra, dec, northArcsec, eastArcsec, settings=False, ): self.log = log log.debug("instansiating a new 'translate' object") self.settings = settings self.ra = ra self.dec = dec self.north = northArcsec / 3600. self.east = eastArcsec / 3600. # xt-self-arg-tmpx # CONSTANTS self.pi = (4 * math.atan(1.0)) self.DEG_TO_RAD_FACTOR = self.pi / 180.0 self.RAD_TO_DEG_FACTOR = 180.0 / self.pi # INITIAL ACTIONS # CONVERT RA AND DEC INTO DECIMAL DEGREES converter = unit_conversion(log=log) self.ra = converter.ra_sexegesimal_to_decimal(ra=self.ra) self.dec = converter.dec_sexegesimal_to_decimal(dec=self.dec) return None
def __init__( self, log, ra, dec, searchRadius, galaxyType=False ): self.log = log log.debug("instansiating a new 'sdss_square_search' object") self.ra = ra self.dec = dec self.searchRadius = searchRadius self.galaxyType = galaxyType # xt-self-arg-tmpx # Variable Data Atrributes self.sdssUrl = 'http://skyserver.sdss3.org/public/en/tools/search/x_sql.aspx' # Initial Actions # convert ra and dec to decimal degrees (if required) converter = unit_conversion( log=log ) self.ra = float(converter.ra_sexegesimal_to_decimal( ra=self.ra )) self.dec = float(converter.dec_sexegesimal_to_decimal( dec=self.dec )) self._calculate_search_limits() self._build_sql_query() return None
def _download_sdss_image( self): """*download sdss image* """ self.log.info('starting the ``_download_sdss_image`` method') opt = "" if self.grid: opt += "G" if self.label: opt += "L" if self.photocat: opt += "P" if self.speccat: opt += "S" if self.invertColors: opt += "I" if len(opt): opt = "opt=%(opt)s&" % locals() width = self.pixelWidth scale = (self.arcminWidth * 60.) / width converter = unit_conversion( log=self.log ) ra = converter.ra_sexegesimal_to_decimal( ra=self.ra ) dec = converter.dec_sexegesimal_to_decimal( dec=self.dec ) url = """http://skyservice.pha.jhu.edu/DR12/ImgCutout/getjpeg.aspx?ra=%(ra)s&dec=%(dec)s&scale=%(scale)s&%(opt)sPhotoObjs=on&width=%(width)s&height=%(width)s""" % locals( ) from fundamentals.download import multiobject_download localUrls = multiobject_download( urlList=[url], downloadDirectory=self.downloadDirectory, log=self.log, timeStamp=False, timeout=180, concurrentDownloads=10, resetFilename=[self.filename], credentials=False, # { 'username' : "...", "password", "..." } longTime=True, indexFilenames=False ) print url self.log.info('completed the ``_download_sdss_image`` method') return None
def intersect(self, ra, dec, radius, inclusive=True): """*return IDs of all triangles contained within and/or intersecting a circle centered on a given ra and dec* **Key Arguments:** - ``ra`` -- RA of central point in decimal degrees or sexagesimal - ``dec`` -- DEC of central point in decimal degrees or sexagesimal - ``radius`` -- radius of circle in degrees - ``inclusive`` -- include IDs of triangles that intersect the circle as well as those completely inclosed by the circle. Default *True* **Return:** - ``trixelArray`` -- a numpy array of the match trixel IDs **Usage:** To return the trixels overlapping a circle with a 10 arcsec radius centred at 23:25:53.56, +26:54:23.9 .. code-block:: python overlappingTrixels = mesh16.intersect( ra="23:25:53.56", dec="+26:54:23.9", radius=10 / (60 * 60), inclusive=True ) Or to return the trixels completing enclosed by a circle with a 1 degree radius centred at 23:25:53.56, +26:54:23.9 .. code-block:: python overlappingTrixels = mesh16.intersect( ra="23:25:53.56", dec="+26:54:23.9", radius=1, inclusive=False ) """ # CONVERT RA AND DEC DECIMAL DEGREES converter = unit_conversion( log=self.log ) ra = converter.ra_sexegesimal_to_decimal( ra=ra ) dec = converter.dec_sexegesimal_to_decimal( dec=dec ) if inclusive: inc = 1 else: inc = 0 return super(HTM, self).intersect(ra, dec, radius, inc)
def _download_sdss_image(self): """*download sdss image* """ self.log.debug('starting the ``_download_sdss_image`` method') opt = "" if self.grid: opt += "G" if self.label: opt += "L" if self.photocat: opt += "P" if self.speccat: opt += "S" if self.invertColors: opt += "I" if len(opt): opt = "opt=%(opt)s&" % locals() width = self.pixelWidth scale = old_div((self.arcminWidth * 60.), width) converter = unit_conversion(log=self.log) ra = converter.ra_sexegesimal_to_decimal(ra=self.ra) dec = converter.dec_sexegesimal_to_decimal(dec=self.dec) url = """http://skyservice.pha.jhu.edu/DR12/ImgCutout/getjpeg.aspx?ra=%(ra)s&dec=%(dec)s&scale=%(scale)s&%(opt)sPhotoObjs=on&width=%(width)s&height=%(width)s""" % locals( ) from fundamentals.download import multiobject_download localUrls = multiobject_download( urlList=[url], downloadDirectory=self.downloadDirectory, log=self.log, timeStamp=False, timeout=180, concurrentDownloads=10, resetFilename=[self.filename], credentials=False, # { 'username' : "...", "password", "..." } longTime=True, indexFilenames=False) print(url) self.log.debug('completed the ``_download_sdss_image`` method') return None
def _generate_sdss_object_name(self): """ *generate sdss object names for the results* **Key Arguments** # - **Return** - None .. todo:: """ self.log.debug('starting the ``_generate_sdss_object_name`` method') converter = unit_conversion(log=self.log) # Names should be of the format `SDSS JHHMMSS.ss+DDMMSS.s` # where the coordinates are truncated, not rounded. for row in self.results: raSex = converter.ra_decimal_to_sexegesimal(ra=row["ra"], delimiter=":") decSex = converter.dec_decimal_to_sexegesimal(dec=row["dec"], delimiter=":") raSex = raSex.replace(":", "")[:9] decSex = decSex.replace(":", "")[:9] sdssName = "SDSS J%(raSex)s%(decSex)s" % locals() row["sdss_name"] = sdssName wordType = [ "unknown", "cosmic_ray", "defect", "galaxy", "ghost", "knownobj", "star", "trail", "sky", "notatype", ] numberType = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] row["type"] = wordType[row["type"]] self.log.debug('completed the ``_generate_sdss_object_name`` method') return None
def _generate_sdss_object_name( self): """ *generate sdss object names for the results* **Key Arguments:** # - **Return:** - None .. todo:: """ self.log.info('starting the ``_generate_sdss_object_name`` method') converter = unit_conversion( log=self.log ) # Names should be of the format `SDSS JHHMMSS.ss+DDMMSS.s` # where the coordinates are truncated, not rounded. for row in self.results: raSex = converter.ra_decimal_to_sexegesimal( ra=row["ra"], delimiter=":" ) decSex = converter.dec_decimal_to_sexegesimal( dec=row["dec"], delimiter=":" ) raSex = raSex.replace(":", "")[:9] decSex = decSex.replace(":", "")[:9] sdssName = "SDSS J%(raSex)s%(decSex)s" % locals() row["sdss_name"] = sdssName wordType = ["unknown", "cosmic_ray", "defect", "galaxy", "ghost", "knownobj", "star", "trail", "sky", "notatype", ] numberType = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] row["type"] = wordType[row["type"]] self.log.info('completed the ``_generate_sdss_object_name`` method') return None
def __init__(self, log, ra, dec, searchRadius, galaxyType=False): self.log = log log.debug("instansiating a new 'sdss_square_search' object") self.ra = ra self.dec = dec self.searchRadius = searchRadius self.galaxyType = galaxyType # xt-self-arg-tmpx # Variable Data Atrributes self.sdssUrl = 'http://skyserver.sdss.org/dr16/en/tools/search/x_sql.aspx' # Initial Actions # convert ra and dec to decimal degrees (if required) converter = unit_conversion(log=log) self.ra = float(converter.ra_sexegesimal_to_decimal(ra=self.ra)) self.dec = float(converter.dec_sexegesimal_to_decimal(dec=self.dec)) self._calculate_search_limits() self._build_sql_query() return None
def __init__( self, log, ra, dec, northArcsec, eastArcsec, settings=False, ): self.log = log log.debug("instansiating a new 'translate' object") self.settings = settings self.ra = ra self.dec = dec self.north = northArcsec / 3600. self.east = eastArcsec / 3600. # xt-self-arg-tmpx # CONSTANTS self.pi = (4 * math.atan(1.0)) self.DEG_TO_RAD_FACTOR = self.pi / 180.0 self.RAD_TO_DEG_FACTOR = 180.0 / self.pi # INITIAL ACTIONS # CONVERT RA AND DEC INTO DECIMAL DEGREES converter = unit_conversion( log=log ) self.ra = converter.ra_sexegesimal_to_decimal( ra=self.ra ) self.dec = converter.dec_sexegesimal_to_decimal( dec=self.dec ) return None
def get( self): """*Calulate the angular separation between two locations on the sky* Input precision should be respected. **Key Arguments:** **Return:** - ``angularSeparation`` -- total angular separation between coordinates (arcsec) - ``north`` -- north-south separation between coordinates (arcsec) - ``east`` -- east-west separation between coordinates (arcsec) See main class usage for details. """ self.log.info('starting the ``get_angular_separation`` method') from astrocalc.coords import unit_conversion # CONSTANTS pi = (4 * math.atan(1.0)) DEG_TO_RAD_FACTOR = pi / 180.0 RAD_TO_DEG_FACTOR = 180.0 / pi converter = unit_conversion( log=self.log ) dec1 = converter.dec_sexegesimal_to_decimal( dec=self.dec1 ) dec2 = converter.dec_sexegesimal_to_decimal( dec=self.dec2 ) ra1 = converter.ra_sexegesimal_to_decimal( ra=self.ra1 ) ra2 = converter.ra_sexegesimal_to_decimal( ra=self.ra2 ) # PRECISION TEST precision = 100 vals = [dec1, dec2, ra1, ra2] for v in vals: thisLen = len(repr(v * 3600.).split(".")[-1]) if thisLen < precision: precision = thisLen angularSeparation = None aa = (90.0 - dec1) * DEG_TO_RAD_FACTOR bb = (90.0 - dec2) * DEG_TO_RAD_FACTOR cc = (ra1 - ra2) * DEG_TO_RAD_FACTOR one = math.cos(aa) * math.cos(bb) two = math.sin(aa) * math.sin(bb) * math.cos(cc) # Because acos() returns NaN outside the ranges of -1 to +1 # we need to check this. Double precision decimal places # can give values like 1.0000000000002 which will throw an # exception. three = one + two if (three > 1.0): three = 1.0 if (three < -1.0): three = -1.0 # BE CAREFUL WITH PRECISION PROPAGATION thisVal = math.acos(three) angularSeparation = float(thisVal) * RAD_TO_DEG_FACTOR * 3600.0 # Now work out N-S, E-W separations (object 1 relative to 2) north = -(dec1 - dec2) * 3600.0 east = -(ra1 - ra2) * \ math.cos((dec1 + dec2) * DEG_TO_RAD_FACTOR / 2.) * 3600.0 angularSeparation = "%0.*f" % (precision, angularSeparation) north = "%0.*f" % (precision, north) east = "%0.*f" % (precision, east) self.log.info('completed the ``get_angular_separation`` method') return angularSeparation, north, east
def coordinates_to_array(log, ra, dec): """*Convert a single value RA, DEC or list of RA and DEC to numpy arrays* **Key Arguments** - ``ra`` -- list, numpy array or single ra value - ``dec`` --list, numpy array or single dec value - ``log`` -- logger **Return** - ``raArray`` -- input RAs as a numpy array of decimal degree values - ``decArray`` -- input DECs as a numpy array of decimal degree values **Usage** .. todo:: add usage info create a sublime snippet for usage ```python ra, dec = coordinates_to_array( log=log, ra=ra, dec=dec ) ``` """ log.debug('starting the ``coordinates_to_array`` function') if isinstance(ra, np.ndarray) and isinstance(dec, np.ndarray): return ra, dec # ASTROCALC UNIT CONVERTER OBJECT converter = unit_conversion(log=log) # CONVERT RA AND DEC TO NUMPY ARRAYS if isinstance(ra, float): pass elif isinstance(ra, ("".__class__, u"".__class__)): try: ra = float(ra) except: ra = converter.ra_sexegesimal_to_decimal(ra=ra) elif isinstance(ra, list): try: ra = np.array(ra).astype(np.float) except: raList = [] raList[:] = [converter.ra_sexegesimal_to_decimal(ra=r) for r in ra] ra = raList if isinstance(dec, float): pass elif isinstance(dec, ("".__class__, u"".__class__)): try: dec = float(dec) except: dec = converter.dec_sexegesimal_to_decimal(dec=dec) elif isinstance(dec, list): try: dec = np.array(dec).astype(np.float) except: decList = [] decList[:] = [ converter.dec_sexegesimal_to_decimal(dec=d) for d in dec ] dec = decList raArray = np.array(ra, dtype='f8', ndmin=1, copy=False) decArray = np.array(dec, dtype='f8', ndmin=1, copy=False) log.debug('completed the ``coordinates_to_array`` function') return raArray, decArray
def _do_ned_namesearch_queries_and_add_resulting_metadata_to_database( self, batchCount): """*Query NED via name searcha and add result metadata to database* **Key Arguments:** - ``batchCount`` - the index number of the batch sent to NED (only needed for printing to STDOUT to give user idea of progress) *Usage:* .. code-block:: python numberSources = stream._do_ned_namesearch_queries_and_add_resulting_metadata_to_database(batchCount=10) """ self.log.debug( 'starting the ``_do_ned_namesearch_queries_and_add_resulting_metadata_to_database`` method' ) # ASTROCALC UNIT CONVERTER OBJECT converter = unit_conversion(log=self.log) tableName = self.dbTableName # QUERY NED WITH BATCH totalCount = len(self.theseIds) print "requesting metadata from NED for %(totalCount)s galaxies (batch %(batchCount)s)" % locals( ) # QUERY THE ONLINE NED DATABASE USING NEDDY'S NAMESEARCH METHOD search = namesearch(log=self.log, names=self.theseIds, quiet=True) results = search.get() print "results returned from ned -- starting to add to database" % locals( ) # CLEAN THE RETURNED DATA AND UPDATE DATABASE totalCount = len(results) count = 0 sqlQuery = "" dictList = [] for thisDict in results: thisDict["tableName"] = tableName count += 1 for k, v in thisDict.iteritems(): if not v or len(v) == 0: thisDict[k] = "null" if k in ["major_diameter_arcmin", "minor_diameter_arcmin" ] and (":" in v or "?" in v or "<" in v): thisDict[k] = v.replace(":", "").replace("?", "").replace("<", "") if isinstance(v, str) and '"' in v: thisDict[k] = v.replace('"', '\\"') if "Input name not" not in thisDict[ "input_note"] and "Same object as" not in thisDict[ "input_note"]: try: thisDict["raDeg"] = converter.ra_sexegesimal_to_decimal( ra=thisDict["ra"]) thisDict["decDeg"] = converter.dec_sexegesimal_to_decimal( dec=thisDict["dec"]) except: name = thisDict["input_name"] self.log.warning( "Could not convert the RA & DEC for the %(name)s NED source" % locals()) continue thisDict["eb_v"] = thisDict["eb-v"] thisDict["ned_name"] = thisDict["input_name"] row = {} for k in [ "redshift_quality", "redshift", "hierarchy", "object_type", "major_diameter_arcmin", "morphology", "magnitude_filter", "ned_notes", "eb_v", "raDeg", "radio_morphology", "activity_type", "minor_diameter_arcmin", "decDeg", "redshift_err", "ned_name" ]: if thisDict[k] == "null": row[k] = None else: row[k] = thisDict[k] dictList.append(row) self.add_data_to_database_table( dictList=dictList, createStatement="""SET SESSION sql_mode="";""") theseIds = ("\", \"").join(self.theseIds) sqlQuery = u""" update %(tableName)s set download_error = 1 where ned_name in ("%(theseIds)s"); """ % locals() writequery( log=self.log, sqlQuery=sqlQuery, dbConn=self.cataloguesDbConn, ) print "%(count)s/%(totalCount)s galaxy metadata batch entries added to database" % locals( ) if count < totalCount: # Cursor up one line and clear line sys.stdout.write("\x1b[1A\x1b[2K") sqlQuery = u""" update tcs_helper_catalogue_tables_info set last_updated = now() where table_name = "%(tableName)s" """ % locals() writequery( log=self.log, sqlQuery=sqlQuery, dbConn=self.cataloguesDbConn, ) self.log.debug( 'completed the ``_do_ned_namesearch_queries_and_add_resulting_metadata_to_database`` method' ) return None
def _update_ned_query_history(self): """*Update the database helper table to give details of the ned cone searches performed* *Usage:* .. code-block:: python stream._update_ned_query_history() """ self.log.debug('starting the ``_update_ned_query_history`` method') myPid = self.myPid # ASTROCALC UNIT CONVERTER OBJECT converter = unit_conversion(log=self.log) # UPDATE THE DATABASE HELPER TABLE TO GIVE DETAILS OF THE NED CONE # SEARCHES PERFORMED dataList = [] for i, coord in enumerate(self.coordinateList): if isinstance(coord, str): ra = coord.split(" ")[0] dec = coord.split(" ")[1] elif isinstance(coord, tuple) or isinstance(coord, list): ra = coord[0] dec = coord[1] dataList.append({ "raDeg": ra, "decDeg": dec, "arcsecRadius": self.radiusArcsec }) if len(dataList) == 0: return None # CREATE TABLE IF NOT EXIST createStatement = """CREATE TABLE IF NOT EXISTS `tcs_helper_ned_query_history` ( `primaryId` bigint(20) NOT NULL AUTO_INCREMENT, `raDeg` double DEFAULT NULL, `decDeg` double DEFAULT NULL, `dateCreated` datetime DEFAULT CURRENT_TIMESTAMP, `dateLastModified` datetime DEFAULT CURRENT_TIMESTAMP, `updated` varchar(45) DEFAULT '0', `arcsecRadius` int(11) DEFAULT NULL, `dateQueried` datetime DEFAULT CURRENT_TIMESTAMP, `htm16ID` bigint(20) DEFAULT NULL, `htm13ID` int(11) DEFAULT NULL, `htm10ID` int(11) DEFAULT NULL, PRIMARY KEY (`primaryId`), KEY `idx_htm16ID` (`htm16ID`), KEY `dateQueried` (`dateQueried`), KEY `dateHtm16` (`dateQueried`,`htm16ID`), KEY `idx_htm10ID` (`htm10ID`), KEY `idx_htm13ID` (`htm13ID`) ) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; """ writequery(log=self.log, sqlQuery=createStatement, dbConn=self.cataloguesDbConn) # USE dbSettings TO ACTIVATE MULTIPROCESSING insert_list_of_dictionaries_into_database_tables( dbConn=self.cataloguesDbConn, log=self.log, dictList=dataList, dbTableName="tcs_helper_ned_query_history", uniqueKeyList=[], dateModified=True, batchSize=10000, replace=True, dbSettings=self.settings["database settings"]["static catalogues"]) # INDEX THE TABLE FOR LATER SEARCHES add_htm_ids_to_mysql_database_table( raColName="raDeg", declColName="decDeg", tableName="tcs_helper_ned_query_history", dbConn=self.cataloguesDbConn, log=self.log, primaryIdColumnName="primaryId") self.log.debug('completed the ``_update_ned_query_history`` method') return None
def test_unit_conversion_deg_to_sex_functions(self): kwargs = {} kwargs["log"] = log kwargs["settings"] = settings # xt-kwarg_key_and_value from astrocalc.coords import unit_conversion converter = unit_conversion( log=log ) ra = converter.ra_decimal_to_sexegesimal( ra="-23.454676456", delimiter="/" ) print ra ra = converter.ra_decimal_to_sexegesimal( ra="63.454676456" ) print ra ra = converter.ra_decimal_to_sexegesimal( ra="0.454676456" ) print ra ra = converter.ra_decimal_to_sexegesimal( ra="345.454" ) print ra ra = converter.ra_decimal_to_sexegesimal( ra="345" ) print ra from astrocalc.coords import unit_conversion converter = unit_conversion( log=log ) dec = converter.dec_decimal_to_sexegesimal( dec="-23.454676456", delimiter="/" ) print dec dec = converter.dec_decimal_to_sexegesimal( dec="-83.454676456", delimiter=":" ) print dec dec = converter.dec_decimal_to_sexegesimal( dec="-3.454676456", delimiter=":" ) print dec dec = converter.dec_decimal_to_sexegesimal( dec="50", delimiter=":" ) print dec
def get(self): """*Calulate the angular separation between two locations on the sky* Input precision should be respected. **Key Arguments** **Return** - ``angularSeparation`` -- total angular separation between coordinates (arcsec) - ``north`` -- north-south separation between coordinates (arcsec) - ``east`` -- east-west separation between coordinates (arcsec) See main class usage for details. """ self.log.debug('starting the ``get_angular_separation`` method') from astrocalc.coords import unit_conversion # CONSTANTS pi = (4 * math.atan(1.0)) DEG_TO_RAD_FACTOR = old_div(pi, 180.0) RAD_TO_DEG_FACTOR = old_div(180.0, pi) converter = unit_conversion(log=self.log) dec1 = converter.dec_sexegesimal_to_decimal(dec=self.dec1) dec2 = converter.dec_sexegesimal_to_decimal(dec=self.dec2) ra1 = converter.ra_sexegesimal_to_decimal(ra=self.ra1) ra2 = converter.ra_sexegesimal_to_decimal(ra=self.ra2) # PRECISION TEST precision = 100 vals = [dec1, dec2, ra1, ra2] for v in vals: thisLen = len(repr(v * 3600.).split(".")[-1]) if thisLen < precision: precision = thisLen angularSeparation = None aa = (90.0 - dec1) * DEG_TO_RAD_FACTOR bb = (90.0 - dec2) * DEG_TO_RAD_FACTOR cc = (ra1 - ra2) * DEG_TO_RAD_FACTOR one = math.cos(aa) * math.cos(bb) two = math.sin(aa) * math.sin(bb) * math.cos(cc) # Because acos() returns NaN outside the ranges of -1 to +1 # we need to check this. Double precision decimal places # can give values like 1.0000000000002 which will throw an # exception. three = one + two if (three > 1.0): three = 1.0 if (three < -1.0): three = -1.0 # BE CAREFUL WITH PRECISION PROPAGATION thisVal = math.acos(three) angularSeparation = float(thisVal) * RAD_TO_DEG_FACTOR * 3600.0 # Now work out N-S, E-W separations (object 1 relative to 2) north = -(dec1 - dec2) * 3600.0 east = -(ra1 - ra2) * \ math.cos((dec1 + dec2) * DEG_TO_RAD_FACTOR / 2.) * 3600.0 angularSeparation = "%0.*f" % (precision, angularSeparation) north = "%0.*f" % (precision, north) east = "%0.*f" % (precision, east) self.log.debug('completed the ``get_angular_separation`` method') return angularSeparation, north, east
def import_new_ps1_pointings( self, recent=False): """ *Import any new PS1 GW pointings from the ps1gw database into the ``ps1_pointings`` table of the Ligo-Virgo Waves database* **Key Arguments:** - ``recent`` -- only sync the most recent 2 months of data (speeds things up) **Return:** - None **Usage:** .. code-block:: python # IMPORT NEW PS1 POINTINGS FROM PS1 GW DATABASE INTO LIGO-VIRGO # WAVES DATABASE from breaker import update_ps1_atlas_footprint_tables dbUpdater = update_ps1_atlas_footprint_tables( log=log, settings=settings ) dbUpdater.import_new_ps1_pointings() """ self.log.debug('starting the ``import_new_ps1_pointings`` method') if recent: mjd = mjdnow( log=self.log ).get_mjd() recent = mjd - 62 recent = " and mjd_obs > %(recent)s " % locals() else: recent = "" # SELECT ALL OF THE POINTING INFO REQUIRED FROM THE ps1gw DATABASE tables = ["ps1_warp_stack_diff_skycells", "ps1_stack_stack_diff_skycells"] filenameMatch = ["ws", "ss"] for t, f in zip(tables, filenameMatch): sqlQuery = u""" SELECT imageid, ppsub_input, filename, m.exptime exp_time, TRUNCATE(mjd_obs, 8) mjd, LEFT(fpa_filter, 1) AS filter, IF(deteff_counts < 200, m.zero_pt + m.deteff_magref+2.5*log(10,exptime), m.zero_pt + m.deteff_magref + m.deteff_calculated_offset+2.5*log(10,exptime)) AS limiting_mag FROM tcs_cmf_metadata m where filename like "%%.%(f)s.%%" %(recent)s """ % locals() rows = readquery( log=self.log, sqlQuery=sqlQuery, dbConn=self.ps1gwDbConn, quiet=False ) # TIDY RESULTS BEFORE IMPORT entries = [] converter = unit_conversion( log=self.log ) for row in rows: e = {} e["exp_time"] = row["exp_time"] e["mjd"] = row["mjd"] e["filter"] = row["filter"] e["ps1_exp_id"] = row["imageid"] e["limiting_mag"] = row["limiting_mag"] e["filename"] = row["filename"] e["skycell_id"] = (".").join(row["filename"].split(".")[0:5]) e["target_image"] = row["ppsub_input"] entries.append(e) # ADD THE NEW RESULTS TO THE ps1_pointings TABLE insert_list_of_dictionaries_into_database_tables( dbConn=self.ligo_virgo_wavesDbConn, log=self.log, dictList=entries, dbTableName=t, uniqueKeyList=["filename"], dateModified=False, batchSize=2500, replace=True ) print "PS1 skycells synced between `tcs_cmf_metadata` and `%(t)s` database tables" % locals() self.log.debug('completed the ``import_new_ps1_pointings`` method') return None
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* """ from astrocalc.coords import unit_conversion # setup the command-line util settings su = tools( arguments=arguments, docString=__doc__, logLevel="CRITICAL", options_first=True, projectName="astrocalc" ) arguments, settings, log, dbConn = su.setup() # tab completion for raw_input readline.set_completer_delims(' \t\n;') readline.parse_and_bind("tab: complete") readline.set_completer(tab_complete) # 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,)) # set options interactively if user requests if "interactiveFlag" in locals() and interactiveFlag: # load previous settings moduleDirectory = os.path.dirname(__file__) + "/resources" pathToPickleFile = "%(moduleDirectory)s/previousSettings.p" % locals() try: with open(pathToPickleFile): pass previousSettingsExist = True except: previousSettingsExist = False previousSettings = {} if previousSettingsExist: previousSettings = pickle.load(open(pathToPickleFile, "rb")) # x-raw-input # x-boolean-raw-input # x-raw-input-with-default-value-from-previous-settings # save the most recently used requests pickleMeObjects = [] pickleMe = {} theseLocals = locals() for k in pickleMeObjects: pickleMe[k] = theseLocals[k] pickle.dump(pickleMe, open(pathToPickleFile, "wb")) # CALL FUNCTIONS/OBJECTS if coordflip: if cartesianFlag: converter = unit_conversion( log=log ) x, y, z = converter.ra_dec_to_cartesian( ra="23 45 21.23232", dec="+01:58:5.45341" ) print x, y, z return try: ra = float(ra) dec = float(dec) degree = True except Exception, e: degree = False if degree is True: converter = unit_conversion( log=log ) try: ra = converter.ra_decimal_to_sexegesimal( ra=ra, delimiter=":" ) dec = converter.dec_decimal_to_sexegesimal( dec=dec, delimiter=":" ) except Exception, e: print e sys.exit(0) print ra, dec
def _create_dictionary_of_IFS(self): """*Generate the list of dictionaries containing all the rows in the IFS stream* **Return** - ``dictList`` - a list of dictionaries containing all the rows in the IFS stream **Usage** ```python from sherlock.imports import IFS stream = IFS( log=log, settings=settings ) dictList = stream._create_dictionary_of_IFS() ``` """ self.log.debug('starting the ``_create_dictionary_of_IFS`` method') # GRAB THE CONTENT OF THE IFS CSV try: response = requests.get(url=self.settings["ifs galaxies url"], ) thisData = response.content thisData = str(thisData).split("\n") status_code = response.status_code except requests.exceptions.RequestException: print('HTTP Request failed') sys.exit(0) dictList = [] columns = ["name", "raDeg", "decDeg", "z"] for line in thisData: thisDict = {} line = line.strip() line = line.replace("\t", " ") values = line.split("|") if len(values) > 3: thisDict["name"] = values[0].strip() # ASTROCALC UNIT CONVERTER OBJECT converter = unit_conversion(log=self.log) try: raDeg = converter.ra_sexegesimal_to_decimal( ra=values[1].strip()) thisDict["raDeg"] = raDeg decDeg = converter.dec_sexegesimal_to_decimal( dec=values[2].strip()) thisDict["decDeg"] = decDeg except: name = thisDict["name"] self.log.warning( 'Could not convert the coordinates for IFS source %(name)s. Skipping import of this source.' % locals()) continue try: z = float(values[3].strip()) if z > 0.: thisDict["z"] = float(values[3].strip()) else: thisDict["z"] = None except: thisDict["z"] = None dictList.append(thisDict) self.log.debug('completed the ``_create_dictionary_of_IFS`` method') return dictList
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="breaker" ) 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,)) if init: from os.path import expanduser home = expanduser("~") filepath = home + "/.config/breaker/breaker.yaml" 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 if not far: far = 1e-5 if gwid and gwid[:2] == "GW": for g in settings["gravitational waves"]: if settings["gravitational waves"][g]["human-name"] == gwid.strip(): gwid = g if wFlag and wFlag[:2] == "GW": for g in settings["gravitational waves"]: if settings["gravitational waves"][g]["human-name"] == wFlag.strip(): wFlag = g # CALL FUNCTIONS/OBJECTS if update: pointingsFlag = True if nopointingsFlag: pointingsFlag = False u = update_ps1_atlas_footprint_tables( log=log, settings=settings, updateNed=updateNedFlag, updateAll=allFlag, updatePointings=pointingsFlag ) u.get() if plot and history: p = plot_wave_observational_timelines( log=log, settings=settings, plotType="history" ) p.get() if plot and timeline: if not pFlag: pFlag = "mercator" if fFlag: filters = list(fFlag) else: filters = False p = plot_wave_observational_timelines( log=log, settings=settings, gwid=wFlag, plotType="timeline", allPlots=allFlag, telescope=tFlag, projection=pFlag, filters=filters, probabilityCut=True ) p.get() if plot and sources: p = plot_wave_matched_source_maps( log=log, settings=settings, gwid=gwid ) p.get() if faker: f = generate_faker_catalogue( log=log, settings=settings, ps1ExpId=ps1ExpId, gwid=False ) f.get() if stats: s = survey_footprint( log=log, settings=settings, gwid=gwid, telescope=telescope ) s.get() if listen and inLastNMins: timeNowMjd = mjdNow( log=log ).get_mjd() startMJD = float(timeNowMjd) - float(inLastNMins) / (60 * 60 * 24.) this = mlisten( log=log, settings=settings, #label="EM_READY | EM_Selected | ADVOK", label="", farThreshold=far, startMJD=float(startMJD), endMJD=float(timeNowMjd) + 1. ) this.get_maps() if listen and mjdStart: this = mlisten( log=log, settings=settings, # label="EM_READY | EM_Selected | ADVOK", label="", farThreshold=far, startMJD=float(mjdStart), endMJD=float(mjdEnd) ) this.get_maps() if listen and daemonFlag: if sec: daemon = float(sec) else: daemon = True this = mlisten( log=log, settings=settings, # label="EM_READY | EM_Selected | ADVOK", label="", farThreshold=far, daemon=daemon ) this.get_maps() if skymap: if exposuresFlag: databaseConnRequired = True else: databaseConnRequired = False plotter = plot_wave_observational_timelines( log=log, settings=settings, databaseConnRequired=databaseConnRequired ) if exposuresFlag: plotParameters, ps1Transients, ps1Pointings, atlasPointings, atlasTransients = plotter.get_gw_parameters_from_settings( gwid=gwid, inFirstDays=(0, 31) ) else: ps1Transients = [] atlasTransients = [] ps1Pointings = [] atlasPointings = [] ps1Transients = [] atlasTransients = [] if not cFlag: cFlag = 0. else: cFlag = float(cFlag) if defaultoutputFlag: outputDirectory = False else: outputDirectory = "." plotter.generate_probability_plot( gwid=gwid, ps1Transients=ps1Transients, atlasTransients=atlasTransients, ps1Pointings=ps1Pointings, atlasPointings=atlasPointings, pathToProbMap=pathToLVMap, fileFormats=["pdf", "png"], outputDirectory=outputDirectory, projection="mollweide", plotType="timeline", folderName="all_sky_plots", fitsImage=False, allSky=True, center=cFlag ) plotter.generate_probability_plot( gwid=gwid, pathToProbMap=pathToLVMap, fileFormats=["pdf", "png"], outputDirectory=outputDirectory, projection="cartesian", plotType="timeline", folderName="all_sky_plots", fitsImage=True, allSky=True, center=cFlag ) if contour: from breaker.transients import annotator an = annotator( log=log, settings=settings, gwid=gwid ) from astrocalc.coords import unit_conversion # ASTROCALC UNIT CONVERTER OBJECT converter = unit_conversion( log=log ) ra = converter.ra_sexegesimal_to_decimal( ra=ra ) dec = converter.dec_sexegesimal_to_decimal( dec=dec ) transients = {"cl": (ra, dec)} transientNames, probs = an.annotate(transients) percentage = probs[0] print "The transient lies within the inner %(percentage)s%% likelihood contour of event %(gwid)s" % locals() 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 test_unit_conversion_deg_to_sex_functions(self): kwargs = {} kwargs["log"] = log kwargs["settings"] = settings # xt-kwarg_key_and_value from astrocalc.coords import unit_conversion converter = unit_conversion( log=log ) ra = converter.ra_decimal_to_sexegesimal( ra="-23.454676456", delimiter="/" ) print(ra) ra = converter.ra_decimal_to_sexegesimal( ra="63.454676456" ) print(ra) ra = converter.ra_decimal_to_sexegesimal( ra="0.454676456" ) print(ra) ra = converter.ra_decimal_to_sexegesimal( ra="345.454" ) print(ra) ra = converter.ra_decimal_to_sexegesimal( ra="345" ) print(ra) from astrocalc.coords import unit_conversion converter = unit_conversion( log=log ) dec = converter.dec_decimal_to_sexegesimal( dec="-23.454676456", delimiter="/" ) print(dec) dec = converter.dec_decimal_to_sexegesimal( dec="-83.454676456", delimiter=":" ) print(dec) dec = converter.dec_decimal_to_sexegesimal( dec="-3.454676456", delimiter=":" ) print(dec) dec = converter.dec_decimal_to_sexegesimal( dec="50", delimiter=":" ) print(dec)
def coordinates_to_array( log, ra, dec): """*Convert a single value RA, DEC or list of RA and DEC to numpy arrays* **Key Arguments:** - ``ra`` -- list, numpy array or single ra value - ``dec`` --list, numpy array or single dec value - ``log`` -- logger **Return:** - ``raArray`` -- input RAs as a numpy array of decimal degree values - ``decArray`` -- input DECs as a numpy array of decimal degree values **Usage:** .. todo:: add usage info create a sublime snippet for usage .. code-block:: python ra, dec = coordinates_to_array( log=log, ra=ra, dec=dec ) """ log.info('starting the ``coordinates_to_array`` function') if isinstance(ra, np.ndarray) and isinstance(dec, np.ndarray): return ra, dec # ASTROCALC UNIT CONVERTER OBJECT converter = unit_conversion( log=log ) # CONVERT RA AND DEC TO NUMPY ARRAYS if isinstance(ra, float): pass elif isinstance(ra, str): try: ra = float(ra) except: ra = converter.ra_sexegesimal_to_decimal(ra=ra) elif isinstance(ra, list): try: ra = np.array(ra).astype(np.float) except: raList = [] raList[:] = [converter.ra_sexegesimal_to_decimal(ra=r) for r in ra] ra = raList if isinstance(dec, float): pass elif isinstance(dec, str): try: dec = float(dec) except: dec = converter.dec_sexegesimal_to_decimal(dec=dec) elif isinstance(dec, list): try: dec = np.array(dec).astype(np.float) except: decList = [] decList[:] = [ converter.dec_sexegesimal_to_decimal(dec=d) for d in dec] dec = decList raArray = np.array(ra, dtype='f8', ndmin=1, copy=False) decArray = np.array(dec, dtype='f8', ndmin=1, copy=False) log.info('completed the ``coordinates_to_array`` function') return raArray, decArray
ra = converter.ra_decimal_to_sexegesimal( ra=ra, delimiter=":" ) dec = converter.dec_decimal_to_sexegesimal( dec=dec, delimiter=":" ) except Exception, e: print e sys.exit(0) print ra, dec else: converter = unit_conversion( log=log ) try: ra = converter.ra_sexegesimal_to_decimal( ra=ra ) dec = converter.dec_sexegesimal_to_decimal( dec=dec ) except Exception, e: print e sys.exit(0) print ra, dec if sep: from astrocalc.coords import separations
def _query_ned_and_add_results_to_database( self, batchCount): """ query ned and add results to database **Key Arguments:** - ``batchCount`` - the index number of the batch sent to NED .. todo :: - update key arguments values and definitions with defaults - update return values and definitions - update usage examples and text - update docstring text - check sublime snippet exists - clip any useful text to docs mindmap - regenerate the docs and check redendering of this docstring """ self.log.debug( 'starting the ``_query_ned_and_add_results_to_database`` method') tableName = self.dbTableName # ASTROCALC UNIT CONVERTER OBJECT converter = unit_conversion( log=self.log ) # QUERY NED WITH BATCH totalCount = len(self.theseIds) print "requesting metadata from NED for %(totalCount)s galaxies (batch %(batchCount)s)" % locals() search = namesearch( log=self.log, names=self.theseIds.keys(), quiet=True ) results = search.get() print "results returned from ned -- starting to add to database" % locals() # CLEAN THE RETURNED DATA AND UPDATE DATABASE totalCount = len(results) count = 0 sqlQuery = "" dictList = [] colList = ["redshift_quality", "redshift", "hierarchy", "object_type", "major_diameter_arcmin", "morphology", "magnitude_filter", "ned_notes", "eb_v", "raDeg", "radio_morphology", "activity_type", "minor_diameter_arcmin", "decDeg", "redshift_err", "in_ned"] if not len(results): for k, v in self.theseIds.iteritems(): dictList.append({ "in_ned": 0, "primaryID": v }) for thisDict in results: thisDict["tableName"] = tableName count += 1 for k, v in thisDict.iteritems(): if not v or len(v) == 0: thisDict[k] = "null" if k in ["major_diameter_arcmin", "minor_diameter_arcmin"] and (":" in v or "?" in v or "<" in v): thisDict[k] = v.replace(":", "").replace( "?", "").replace("<", "") if isinstance(v, str) and '"' in v: thisDict[k] = v.replace('"', '\\"') if "Input name not" not in thisDict["input_note"] and "Same object as" not in thisDict["input_note"]: if thisDict["ra"] != "null" and thisDict["dec"] != "null": thisDict["raDeg"] = converter.ra_sexegesimal_to_decimal( ra=thisDict["ra"] ) thisDict["decDeg"] = converter.dec_sexegesimal_to_decimal( dec=thisDict["dec"] ) else: thisDict["raDeg"] = None thisDict["decDeg"] = None thisDict["in_ned"] = 1 thisDict["eb_v"] = thisDict["eb-v"] row = {} row["primary_ned_id"] = thisDict["input_name"] try: row["primaryID"] = self.theseIds[thisDict["input_name"]] for c in colList: if thisDict[c] == "null": row[c] = None else: row[c] = thisDict[c] dictList.append(row) except: g = thisDict["input_name"] self.log.error( "Cannot find database table %(tableName)s primaryID for '%(g)s'\n\n" % locals()) dictList.append({ "in_ned": 0, "primary_ned_id": thisDict["input_name"] }) else: dictList.append({ "primary_ned_id": thisDict["input_name"], "in_ned": 0, "primaryID": self.theseIds[thisDict["input_name"]] }) self.log.debug( 'completed the ``_query_ned_and_add_results_to_database`` method') return dictList
def test_unit_conversion_sexe_to_deg_function(self): kwargs = {} kwargs["log"] = log kwargs["settings"] = settings # xt-kwarg_key_and_value # DEC FIRST from astrocalc.coords import unit_conversion converter = unit_conversion( log=log ) dec = converter.dec_sexegesimal_to_decimal( dec="-23:45:21.23232" ) print dec dec = converter.dec_sexegesimal_to_decimal( dec="+1:58:05.45341" ) print dec dec = converter.dec_sexegesimal_to_decimal( dec="+01:58:5.45341" ) print dec dec = converter.dec_sexegesimal_to_decimal( dec="01:58:05" ) print dec dec = converter.dec_sexegesimal_to_decimal( dec="12.3234234234" ) print dec dec = converter.dec_sexegesimal_to_decimal( dec="-34.3234234234" ) print dec # TEST RA NOW ra = converter.ra_sexegesimal_to_decimal( ra="23:45:21.23232" ) print ra # TEST RA NOW ra = converter.ra_sexegesimal_to_decimal( ra="23h45m21.23232s" ) print ra # TEST RA NOW ra = converter.ra_sexegesimal_to_decimal( ra="23 45 21.23232" ) print ra # TEST RA NOW ra = converter.ra_sexegesimal_to_decimal( ra="2 04 21.23232" ) print ra # TEST RA NOW ra = converter.ra_sexegesimal_to_decimal( ra="04:45 21" ) print ra ra = converter.ra_sexegesimal_to_decimal( ra="12.3234234234" ) print ra ra = converter.ra_sexegesimal_to_decimal( ra="334.3234234234" ) print ra
def test_unit_conversion_sexe_to_deg_function(self): kwargs = {} kwargs["log"] = log kwargs["settings"] = settings # xt-kwarg_key_and_value # DEC FIRST from astrocalc.coords import unit_conversion converter = unit_conversion( log=log ) dec = converter.dec_sexegesimal_to_decimal( dec="-23:45:21.23232" ) print(dec) dec = converter.dec_sexegesimal_to_decimal( dec="+1:58:05.45341" ) print(dec) dec = converter.dec_sexegesimal_to_decimal( dec="+01:58:5.45341" ) print(dec) dec = converter.dec_sexegesimal_to_decimal( dec="01:58:05" ) print(dec) dec = converter.dec_sexegesimal_to_decimal( dec="12.3234234234" ) print(dec) dec = converter.dec_sexegesimal_to_decimal( dec="-34.3234234234" ) print(dec) # TEST RA NOW ra = converter.ra_sexegesimal_to_decimal( ra="23:45:21.23232" ) print(ra) # TEST RA NOW ra = converter.ra_sexegesimal_to_decimal( ra="23h45m21.23232s" ) print(ra) # TEST RA NOW ra = converter.ra_sexegesimal_to_decimal( ra="23 45 21.23232" ) print(ra) # TEST RA NOW ra = converter.ra_sexegesimal_to_decimal( ra="2 04 21.23232" ) print(ra) # TEST RA NOW ra = converter.ra_sexegesimal_to_decimal( ra="04:45 21" ) print(ra) ra = converter.ra_sexegesimal_to_decimal( ra="12.3234234234" ) print(ra) ra = converter.ra_sexegesimal_to_decimal( ra="334.3234234234" ) print(ra)
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* """ from astrocalc.coords import unit_conversion # setup the command-line util settings su = tools(arguments=arguments, docString=__doc__, logLevel="CRITICAL", options_first=True, projectName="astrocalc", defaultSettingsFile=True) arguments, settings, log, dbConn = su.setup() # tab completion for raw_input readline.set_completer_delims(' \t\n;') readline.parse_and_bind("tab: complete") readline.set_completer(tab_complete) # UNPACK REMAINING CL ARGUMENTS USING `EXEC` TO SETUP THE VARIABLE NAMES # AUTOMATICALLY a = {} for arg, val in list(arguments.items()): if arg[0] == "-": varname = arg.replace("-", "") + "Flag" else: varname = arg.replace("<", "").replace(">", "") a[varname] = val if arg == "--dbConn": dbConn = val a["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, )) # set options interactively if user requests if "interactiveFlag" in a and a["interactiveFlag"]: # load previous settings moduleDirectory = os.path.dirname(__file__) + "/resources" pathToPickleFile = "%(moduleDirectory)s/previousSettings.p" % locals() try: with open(pathToPickleFile): pass previousSettingsExist = True except: previousSettingsExist = False previousSettings = {} if previousSettingsExist: previousSettings = pickle.load(open(pathToPickleFile, "rb")) # x-raw-input # x-boolean-raw-input # x-raw-input-with-default-value-from-previous-settings # save the most recently used requests pickleMeObjects = [] pickleMe = {} theseLocals = locals() for k in pickleMeObjects: pickleMe[k] = theseLocals[k] pickle.dump(pickleMe, open(pathToPickleFile, "wb")) coordflip = a["coordflip"] sep = a["sep"] timeflip = a["timeflip"] trans = a["trans"] now = a["now"] dist = a["dist"] ra = a["ra"] ra1 = a["ra1"] ra2 = a["ra2"] dec = a["dec"] dec1 = a["dec1"] dec2 = a["dec2"] datetime = a["datetime"] north = a["north"] east = a["east"] distVal = a["distVal"] hVal = a["hcFlag"] OmegaMatter = a["wmFlag"] OmegaVacuum = a["wvFlag"] mpcFlag = a["mpcFlag"] redshiftFlag = a["redshiftFlag"] cartesianFlag = a["cartesianFlag"] # CALL FUNCTIONS/OBJECTS if coordflip: if cartesianFlag: converter = unit_conversion(log=log) x, y, z = converter.ra_dec_to_cartesian(ra="23 45 21.23232", dec="+01:58:5.45341") print(x, y, z) return try: ra = float(ra) dec = float(dec) degree = True except Exception as e: degree = False if degree is True: converter = unit_conversion(log=log) try: ra = converter.ra_decimal_to_sexegesimal(ra=ra, delimiter=":") dec = converter.dec_decimal_to_sexegesimal(dec=dec, delimiter=":") except Exception as e: print(e) sys.exit(0) print(ra, dec) else: converter = unit_conversion(log=log) try: ra = converter.ra_sexegesimal_to_decimal(ra=ra) dec = converter.dec_sexegesimal_to_decimal(dec=dec) except Exception as e: print(e) sys.exit(0) print(ra, dec) if sep: from astrocalc.coords import separations calculator = separations( log=log, ra1=ra1, dec1=dec1, ra2=ra2, dec2=dec2, ) angularSeparation, north, east = calculator.get() print("""%(angularSeparation)s arcsec (%(north)s N, %(east)s E)""" % locals()) if timeflip: try: inputMjd = float(datetime) if datetime[0] not in ["0", "1", "2"]: inputMjd = True else: inputMjd = False except: inputMjd = False from astrocalc.times import conversions converter = conversions(log=log) if inputMjd == False: try: mjd = converter.ut_datetime_to_mjd(utDatetime=datetime) print(mjd) except Exception as e: print(e) else: try: utDate = converter.mjd_to_ut_datetime(mjd=datetime) print(utDate) except Exception as e: print(e) if trans: # TRANSLATE COORDINATES ACROSS SKY from astrocalc.coords import translate newRa, newDec = translate(log=log, ra=ra, dec=dec, northArcsec=float(north), eastArcsec=float(east)).get() from astrocalc.coords import unit_conversion converter = unit_conversion(log=log) ra = converter.ra_decimal_to_sexegesimal(ra=newRa, delimiter=":") dec = converter.dec_decimal_to_sexegesimal(dec=newDec, delimiter=":") print("%(newRa)s, %(newDec)s (%(ra)s, %(dec)s)" % locals()) if now: from astrocalc.times import now mjd = now(log=log).get_mjd() print(mjd) if dist and redshiftFlag: from astrocalc.distances import converter c = converter(log=log) if not hcFlag: hcFlag = 70. if not wmFlag: wmFlag = 0.3 if not wvFlag: wvFlag = 0.7 dists = c.redshift_to_distance(z=float(distVal), WM=float(wmFlag), WV=float(wvFlag), H0=float(hcFlag)) print("Distance Modulus: " + str(dists["dmod"]) + " mag") print("Luminousity Distance: " + str(dists["dl_mpc"]) + " Mpc") print("Angular Size Scale: " + str(dists["da_scale"]) + " kpc/arcsec") print("Angular Size Distance: " + str(dists["da_mpc"]) + " Mpc") print("Comoving Radial Distance: " + str(dists["dcmr_mpc"]) + " Mpc") if dist and mpcFlag: from astrocalc.distances import converter c = converter(log=log) z = c.distance_to_redshift(mpc=float(distVal)) print("z = %(z)s" % locals()) 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 ``sky-tile-pinpointer.py`` is run as a single script from the cl* """ # MAKE SURE HEALPIX SMALL ENOUGH TO MATCH FOOTPRINTS CORRECTLY nside = 1024 pi = (4 * math.atan(1.0)) DEG_TO_RAD_FACTOR = pi / 180.0 RAD_TO_DEG_FACTOR = 180.0 / pi # SETUP THE COMMAND-LINE UTIL SETTINGS su = tools(arguments=arguments, docString=__doc__, logLevel="WARNING", options_first=False, projectName=False) 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, )) tileSide = float(tileSide) # CONVERT RA AND DEC # ASTROCALC UNIT CONVERTER OBJECT converter = unit_conversion(log=log) ra = converter.ra_sexegesimal_to_decimal(ra=ra) dec = converter.dec_sexegesimal_to_decimal(dec=dec) # THE SKY-LOCATION AS A HEALPIXEL ID pinpoint = hp.ang2pix(nside, theta=ra, phi=dec, lonlat=True) matchedTileIds = [] with open(pathToTileList, 'rb') as csvFile: csvReader = csv.DictReader(csvFile, dialect='excel', delimiter=',', quotechar='"') for row in csvReader: row["DEC"] = float(row["DEC"]) row["RA"] = float(row["RA"]) decCorners = (row["DEC"] - tileSide / 2, row["DEC"] + tileSide / 2) corners = [] for d in decCorners: if d > 90.: d = 180. - d elif d < -90.: d = -180 - d raCorners = (row["RA"] - (tileSide / 2) / np.cos(d * DEG_TO_RAD_FACTOR), row["RA"] + (tileSide / 2) / np.cos(d * DEG_TO_RAD_FACTOR)) for r in raCorners: if r > 360.: r = 720. - r elif r < 0.: r = 360. + r corners.append(hp.ang2vec(r, d, lonlat=True)) # FLIP CORNERS 3 & 4 SO HEALPY UNDERSTANDS POLYGON SHAPE corners = [corners[0], corners[1], corners[3], corners[2]] # RETURN HEALPIXELS IN EXPOSURE AREA expPixels = hp.query_polygon(nside, np.array(corners)) if pinpoint in expPixels: # CALCULATE SEPARATION IN ARCSEC calculator = separations( log=log, ra1=ra, dec1=dec, ra2=row["RA"], dec2=row["DEC"], ) angularSeparation, north, east = calculator.get() angularSeparation = float(angularSeparation) / 3600 north = float(north) / 3600 east = float(east) / 3600 matchedTileIds.append( row["EXPID"] + ": %(angularSeparation)1.4f deg from center (%(north)1.4f N, %(east)1.4f E) " % locals()) csvFile.close() for i in matchedTileIds: print i return