-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_feros.py
151 lines (110 loc) · 5 KB
/
get_feros.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
from db import *
import shutil
import tarfile
from astroquery.eso import Eso
# https://astroquery.readthedocs.io/en/latest/eso/eso.html
eso = Eso()
eso.login(input('Provide the username of your ESO account: '), store_password=True)
eso.ROW_LIMIT = -1
def get_feros(fname, colname=None, radius=2, program=None, limit=9999):
'''
Parameters
----------
fname : str
Enter the filename containing the source names [.lst/.txt/.fits].
colname : str, optional
Column name for the sources if .fits file used as input for table.
radius : int/float, optional
Max distance in arcmin to the queried result sources. Default is 2 arcmin.
program : str, optional
Program ID to search within the ESO archive. Default is None.
limit : int, optional
Maximum number of datasets to download per source. Default is 9999.
Returns: None (but it downloads the fits files).
'''
file = search(fname, maindir)
if fname.endswith('.lst') or fname.endswith('.txt'):
sources = findlist(fname)
elif fname.endswith('.fits'):
if colname == None:
while colname == '':
colname = input('Enter the column name with target names: ')
sources = findtable(fname)[colname]
bar = pb.ProgressBar(maxval=len(sources),
widgets=[pb.Bar('=','[',']'),' ',pb.Percentage()])
bar.start()
db = open(datadir+'FEROS/raw/downloaded_sources.txt','a')
i = 0
for source in sources:
bar.update(i)
i += 1
if type(source) != str:
name = str(source)
name = source.strip()
print('\nSearching data for %s...\n' % name)
query = eso.query_surveys('FEROS', cache=False, target=name)
if query is None:
db.write(name+', 0, 0, Query returned no results.\n')
continue
if program != None and 'Run/Program ID' in query.colnames:
query = query[query['Run/Program ID'] == program]
simbad = query_Simbad(name)
if simbad is None:
db.write(name+', 0, 0, Simbad return no results.\n')
continue
coords_S = SkyCoord(simbad['RA'], simbad['DEC'], unit=(u.hourangle,u.deg), frame='icrs')
datasets = []
for entry in query:
try:
if re.sub('[ /_/]','',str(entry['Object'])).replace('HD-','HD') == name:
datasets.append(entry['ARCFILE'])
else:
coords_F = SkyCoord(entry['RA'], entry['DEC'], unit=(u.deg,u.deg), frame='icrs')
if coords_S.separation(coords_F).arcmin[0] < radius:
datasets.append(entry['ARCFILE'])
except:
db.write('Source %s had problems during the query.\n' % name); continue
if len(datasets) == 0:
db.write(name+', 0, 0, No matches in FEROS database.\n'); continue
if len(datasets) > limit:
db.write(name+', 0, 0, >%i datasets for the source.\n'); continue
spectra = 0
for dataset in datasets:
miliseconds = round(float(dataset[-5:])+0.001, 3)
if miliseconds == 1.0:
db.write('Please check: data from %s is probably missing.\n' % name)
miliseconds = '%.3f' % miliseconds
dataset_f = dataset[:22] + miliseconds
#if os.path.exists(datadir+dataset_f) == True: continue
try:
print('Files will be downloaded to %s' % datadir+'FEROS/raw/')
data_files = eso.retrieve_data(dataset_f, destination=datadir+'FEROS/raw/')
print(data_files)
except:
db.write('ERROR: Dataset %s could not be retrieved.\n' %dataset_f)
continue
try:
os.remove(datadir+'FEROS/raw/'+dataset_f+'.xml')
except:
None
folder_name = datadir+'FEROS/raw/'+dataset_f
try:
tar = tarfile.open(folder_name+'.tgz')
tar.extractall(folder_name)
tar.close()
os.remove(folder_name+'.tgz')
except:
db.write('Tar file for %s had problems during download. Denied access?\n' % name)
continue
fits_files = [file for file in os.listdir(folder_name) if file.endswith('.fits')]
for fit_file in fits_files:
if not fit_file.endswith('1081.fits'):
os.remove(folder_name+'/'+fit_file)
else:
spectra = spectra + 1
shutil.move(folder_name+'/'+fit_file,datadir+'FEROS/raw/'+fit_file)
shutil.rmtree(folder_name)
db.write(name+', '+str(len(datasets))+', '+str(spectra)+',--\n')
bar.finish()
db.close()
return print('\n Data retrieval completed. Check the folder.')