-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueryGaia.py
188 lines (148 loc) · 7.1 KB
/
queryGaia.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import argparse
from astroquery.gaia import Gaia
import os
from astropy.coordinates import SkyCoord
from astropy.table import QTable
from astropy import units as u
def checkBool(arg):
if arg == 1:
return True
else:
return False
def queryGaia(var, gcns, num, save, c):
# Retrieves data from Gaia archive
# var = variable stars or all stars?
# gcns = GCNS or all Gaia stars?
# num = max number of stars to retrieve
# save = save to a file?
# c = include variable classification?
if gcns == True:
dist_col = 'dist_50'
cartesian = True
x_col, y_col, z_col = 'xcoord_50', 'ycoord_50', 'zcoord_50'
ra_col, dec_col = 'ra', 'dec'
dist84_col, dist16_col = 'dist_84', 'dist_16'
gmag_col, bpmag_col, rpmag_col = 'phot_g_mean_mag', 'phot_bp_mean_mag', 'phot_rp_mean_mag'
query_select = f'SELECT TOP {num} gcns.{dist_col}, gcns.{x_col}, gcns.{y_col}, gcns.{z_col}, \
gcns.{ra_col}, gcns.{dec_col}, gcns.{dist84_col}, gcns.{dist16_col}, \
gcns.{gmag_col}, gcns.{bpmag_col}, gcns.{rpmag_col}, gcns.source_id'
if var == True:
if c:
query_select += ', vclass.best_class_name'
sf = 'GCNS_var_class.fits'
query = f"{query_select} \
FROM gaiadr3.vari_summary AS var \
INNER JOIN external.gaiaedr3_gcns_main_1 AS gcns ON var.source_id=gcns.source_id \
INNER JOIN gaiadr3.vari_classifier_result AS vclass ON vclass.source_id = var.source_id"
else:
query = f'{query_select} \
FROM gaiadr3.vari_summary AS var \
JOIN external.gaiaedr3_gcns_main_1 AS gcns ON var.source_id=gcns.source_id'
sf = 'GCNS_var.fits'
elif var == False:
query = f'{query_select} \
FROM external.gaiaedr3_gcns_main_1 AS gcns'
sf = 'GCNS.fits'
elif gcns == False:
dist_col = 'r_med_photogeo'
cartesian = False
ra_col, dec_col = 'ra', 'dec'
dist84_col, dist16_col = 'r_hi_photogeo', 'r_lo_photogeo'
gmag_col, bpmag_col, rpmag_col = 'phot_g_mean_mag', 'phot_bp_mean_mag', 'phot_rp_mean_mag'
query_select = f'SELECT TOP {num} dist.{dist_col}, \
source.{ra_col}, source.{dec_col}, dist.{dist84_col}, dist.{dist16_col}, \
source.{gmag_col}, source.{bpmag_col}, source.{rpmag_col}, source.source_id'
if var == True:
if c:
query_select += ', vclass.best_class_name'
sf = 'Gaia_var_class.fits'
query = f"{query_select} \
FROM gaiadr3.vari_summary AS var \
INNER JOIN gaiadr3.gaia_source AS source ON var.source_id=source.source_id \
INNER JOIN external.gaiaedr3_distance AS dist ON var.source_id=dist.source_id \
INNER JOIN gaiadr3.vari_classifier_result AS vclass ON vclass.source_id = var.source_id \
WHERE source.has_epoch_photometry='true'"
else:
query = f"{query_select} \
FROM gaiadr3.vari_summary AS var \
INNER JOIN gaiadr3.gaia_source AS source ON var.source_id=source.source_id \
INNER JOIN external.gaiaedr3_distance AS dist ON var.source_id=dist.source_id \
WHERE source.has_epoch_photometry='true'"
sf = 'Gaia_var.fits'
elif var == False:
query = f"{query_select} \
FROM gaiadr3.gaia_source AS source \
JOIN external.gaiaedr3_distance AS dist ON source.source_id=dist.source_id"
sf = 'Gaia.fits'
job = Gaia.launch_job_async(query, output_format='fits')
results = job.get_results()
c1 = SkyCoord(ra = results[ra_col],
dec = results[dec_col],
distance = results[dist_col],
frame = 'icrs')
if cartesian == True:
xcoord, ycoord, zcoord = results[x_col], results[y_col], results[z_col]
else:
xcoord = c1.transform_to('galactocentric').x.to('pc') + 8122*u.pc
ycoord = c1.transform_to('galactocentric').y.to('pc')
zcoord = c1.transform_to('galactocentric').z.to('pc') - 20.8*u.pc
if c:
stars = QTable([results['source_id'], results[ra_col], results[dec_col], results[dist_col], results[dist84_col], results[dist16_col], xcoord, ycoord, zcoord, results[gmag_col], results[bpmag_col], results[rpmag_col], results['best_class_name']],
names=('id', 'ra', 'dec', 'dist', 'dist84', 'dist16', 'x', 'y', 'z', 'g', 'bp', 'rp', 'class'))
else:
stars = QTable([results['source_id'], results[ra_col], results[dec_col], results[dist_col], results[dist84_col], results[dist16_col], xcoord, ycoord, zcoord, results[gmag_col], results[bpmag_col], results[rpmag_col]],
names=('id', 'ra', 'dec', 'dist', 'dist84', 'dist16', 'x', 'y', 'z', 'g', 'bp', 'rp'))
savefile = f'../{sf}'
if save:
if os.path.exists(savefile):
os.remove(savefile)
stars.write(savefile, format='fits')
print(f'{len(stars)} stars retrieved and saved to {savefile}')
else:
print(f'{len(stars)} stars retrieved')
return c1, stars
def login(username, password):
Gaia.login(user=username, password=password)
def logout():
Gaia.logout()
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Select targets on SETI Ellipsoid'
)
parser.add_argument(
'-v', '--variable', type = int, default=1,
help='Only variable stars (1) or all stars (0)'
)
parser.add_argument(
'-ns', '--GCNS', type = int, default=1,
help='Use Gaia Catalogue of Nearby Stars (GCNS) (1) or entire Gaia catalogue (0)?'
)
parser.add_argument(
'-n', '--number', type=int, default=100000,
help='Maximum number of query targets'
)
parser.add_argument(
'-usr', '--username', type=str, default=None,
help='Gaia login username'
)
parser.add_argument(
'-pw', '--password', type=str, default=None,
help='Gaia login password'
)
parser.add_argument(
'-c', '--classification', type=int, default=0,
help='Include variable classification (1) or not (0)?'
)
parser.add_argument(
'-s', '--save', type=int, default=1,
help='Save results to a fits file (1) or not (0)?'
)
args = parser.parse_args()
v, ns, c, s = checkBool(args.variable), checkBool(args.GCNS), checkBool(args.classification), checkBool(args.save)
num = args.number
username = args.username
password = args.password
if username and password:
login(username, password)
c1, stars = queryGaia(v, ns, num, s, c)
logout()