This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdss_finder.py
80 lines (71 loc) · 3.3 KB
/
sdss_finder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
#Object catalog
from astroquery.simbad import Simbad
#Coordinates and astornomical units
from astropy import units as u
from astropy.coordinates import SkyCoord
import astropy.coordinates as coord
#Url stuff
from urllib.parse import urlencode
from urllib.request import urlretrieve
class SDSS:
def get_SDDS_image(self, coords_deg_lst, chat_id):
#Turn RA & DEC strings into floats.
ra = float(coords_deg_lst[0])
dec = float(coords_deg_lst[1])
#Calculate the center of the file to be retrieved.
hcg7_center = SkyCoord(ra*u.deg, dec*u.deg, frame='icrs')
type(hcg7_center.ra), type(hcg7_center.dec)
#Size in pixels of image to be downloaded.
impix_w = 1657
impix_h = 1129
#Frame width in degrees.
imsize = 1.2*u.deg
#Base url to queery for SDSS cutouts
cutoutbaseurl = 'http://skyservice.pha.jhu.edu/DR12/ImgCutout/getjpeg.aspx'
#Queery to concatenate to base url
query_string = urlencode(dict(ra=hcg7_center.ra.deg,
dec=hcg7_center.dec.deg,
width=impix_w, height=impix_h,
scale=imsize.to(u.arcsec).value/impix_w))
#
url = cutoutbaseurl + '?' + query_string
#Download .jpg file
urlretrieve(url, 'SDSS_' + str(chat_id) + '.jpg')
#Convert 'hh:mm:ss hh:mm:ss' coords to deg
def convert_to_deg(self, obj_ra, obj_dec):
#Generate skycoord object with RA & DEC
coords_deg = SkyCoord(str(obj_ra + " " + obj_dec), unit=(u.hourangle, u.deg))
#Extract RA & DEC from skycoord obj
coords_deg_ra = round(coords_deg.ra.degree, 4)
coords_deg_dec = round(coords_deg.dec.degree, 4)
#Generate string with RA & DEC
str_list = [str(coords_deg_ra), str(coords_deg_dec)]
return str_list
#Find object coordinates from "nickname" input.
def find_object_coords_fname(self, object_name, chat_id):
try:
#Query Simbad for the object.
result_table = Simbad.query_object(object_name)
if result_table == None:
self.object_at_string = None
return True
#Extract RA & DEC
obj_ra = result_table['RA'][0].replace(" ", ":")
obj_dec = result_table['DEC'][0].replace(" ", ":")
#Generate coords string
coords = ' '.join(self.convert_to_deg(obj_ra, obj_dec))
#Get image for coords
self.get_SDDS_image(self.convert_to_deg(obj_ra, obj_dec), chat_id)
#Get constellation for coords
constellation = coord.get_constellation(SkyCoord(coords, frame='icrs', unit=(u.deg)))
#Generate string with RA, DEC, and constellation
self.object_at_string = "Object <b>{}</b> is at:\n○ RA: {} \n○ DEC: {} \n \nIn the constellation of <b>{}</b>.".format(object_name.upper(), obj_ra, obj_dec, constellation)
return False
except:
return True
def cleanup(self, chat_id):
os.remove('SDSS_' + str(chat_id) + '.jpg')
def get_SDDS(self, update, context, chat_id):
object_name = update.message.text
return [self.find_object_coords_fname(object_name, chat_id), self.object_at_string]