-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastrometry.py
97 lines (77 loc) · 2.98 KB
/
astrometry.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
import time
import os.path
import json
import requests
API_KEY = 'ailckcattnyvxxfu'
SERVER = 'nova.astrometry.net'
#SERVER = 'localhost:8080'
def solve(filename, session_key=None, api_key=API_KEY):
if not session_key:
print('Logging in with apikey')
session_key = login(api_key)
submission_id = submit_image(filename, session_key)
submission_status_url = 'http://{}/status/{}'.format(SERVER, submission_id)
print('See submission status at: {}'.format(submission_status_url))
print('Waiting for solve...')
while True:
job_id = get_job_id(submission_id)
if job_id:
break
else:
time.sleep(2)
print('.', end='', flush=True)
while True:
job_status = get_job_status(job_id)
if job_status == 'solving':
time.sleep(2)
print('.', end='', flush=True)
elif job_status == 'failure':
print()
print('Could not solve image!')
return
else:
print()
break
calibration_result = get_calibration_results(job_id)
return calibration_result, session_key
def get_job_id(submission_id):
submissions_url = 'http://{}/api/submissions/{}'.format(SERVER, submission_id)
response = requests.get(submissions_url).json()
return response['jobs'][0] if response['jobs'] else None
def get_job_status(job_id):
job_url = 'http://{}/api/jobs/{}'.format(SERVER, job_id)
response = requests.get(job_url).json()
return response['status']
def get_calibration_results(job_id):
calibration_url = 'http://{}/api/jobs/{}/calibration/'.format(SERVER, job_id)
calibration_response = requests.get(calibration_url).json()
return calibration_response
def login(api_key):
login_url = 'http://{}/api/login'.format(SERVER)
request_data = {'request-json': json.dumps({'apikey': api_key})}
response = requests.post(login_url, data=request_data)
response_json = response.json()
if response_json['status'] != 'success':
raise RuntimeError('Could not log in!')
session_key = response_json['session']
return session_key
def submit_image(filename, session_key):
arguments_json = json.dumps({
'session': session_key,
'allow_commercial_use': 'n'
})
_, filename_tail = os.path.split(filename)
multipart_data = {
'request-json' : (None, arguments_json, 'text/plain'),
'file': (filename_tail, open(filename, 'rb'), 'application/octet-stream')
}
upload_url = 'http://{}/api/upload'.format(SERVER)
response = requests.post(upload_url, files=multipart_data)
if response.status_code != 200:
raise RuntimeError('API responded with HTTP {}'.format(response.status_code))
response_json = response.json()
if response_json['status'] != 'success':
raise RuntimeError('Could not process image: {}'.format(response_json))
return response_json['subid']
if __name__ == '__main__':
print(solve('cygnus.jpg')[0])