-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastrometry.py
210 lines (169 loc) · 7.02 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import json
import logging
from time import sleep
import requests
from modules import ConfigLoader
# Enable or disable logging
logging.basicConfig(
# filename="app.log",
# filemode="a",
format="%(asctime)s > %(name)s - %(levelname)s - %(message)s",
datefmt="%d-%b-%y %H:%M:%S",
level=logging.INFO,
)
class AstrometryClient:
"""Client for interacting with the Astrometry API.
This class provides methods for authenticating the user, uploading images to the Astrometry.net API,
checking the status of a job, retrieving calibration information, and downloading WCS files.
Attributes:
api_key (str): The API key used for authentication.
session (str): The session key obtained after successful authentication.
"""
def __init__(self):
self.api_key = ConfigLoader().get_astrometry_key()
self.session = None
def authenticate(self):
"""Authenticate user and obtain session key.
This method sends a POST request to the Astrometry API to authenticate the user
using the provided API key. If the authentication is successful, the session key
is stored in the `session` attribute of the object. Otherwise, an error message
is logged.
Returns:
None
"""
response = requests.post(
"http://nova.astrometry.net/api/login",
data={"request-json": json.dumps({"apikey": self.api_key})},
)
data = response.json()
if data["status"] == "success":
self.session = data["session"]
logging.info("Authentication successful. Session key: %s", self.session)
else:
logging.critical("Authentication failed. Session key not obtained.")
def upload_image(self, image_path):
"""Uploads an image to the Astrometry.net API and returns the submission ID.
Args:
image_path (str): The path to the image file to be uploaded.
Returns:
str: The submission ID of the uploaded image, or None if the upload failed.
"""
if not self.session:
logging.warning("Please authenticate first.")
return None
files = {
"request-json": (None, json.dumps({"session": self.session})),
"file": (image_path, open(image_path, "rb"), "application/octet-stream"),
}
response = requests.post("http://nova.astrometry.net/api/upload", files=files)
logging.debug("Status code: %s", response.json())
if response.status_code == 200:
logging.debug(
"Image upload successful. Submission ID: %s", response.json()["subid"]
)
return response.json()["subid"]
else:
logging.error("Image upload failed. Status code: %s", response.status_code)
return None
def check_job_status(self, job_id):
"""Check the status of a job.
Args:
job_id (int): The ID of the job to check.
Returns:
str: The status of the job, or None if there was an error.
"""
if not self.session:
logging.warning(f"Please authenticate first, Job_ID: {job_id}")
return None
url = f"http://nova.astrometry.net/api/jobs/{job_id}"
response = requests.get(url)
if response.status_code == 200:
logging.info(f"Job status: {response.json().get('status')}")
return response.json().get("status")
else:
logging.warning(
f"Error checking job status. Status code: {response.status_code}"
)
return None
def check_submission_status(self, submission_id):
"""Check the status of a submission.
Args:
submission_id (int): The ID of the submission to check.
Returns:
str: The status of the submission, or None if there was an error.
"""
if not self.session:
logging.warning("Please authenticate first, Job_ID: {submission_id}")
return None
url = f"http://nova.astrometry.net/api/submissions/{submission_id}"
response = requests.get(url)
if response.status_code == 200:
logging.info(f"Submission status: {response.json}, Job_ID: {submission_id}")
return response.json().get("jobs")
else:
logging.warning(
f"Error checking submission status. Status code: {response.status_code}, Job_ID: {submission_id}"
)
return None
def is_job_done(self, job_id):
"""Check if a job is done.
Args:
job_id (int): The ID of the job to check.
Returns:
bool: True if the job is done, False otherwise.
"""
url = f"http://nova.astrometry.net/api/submissions/{job_id}"
response = requests.get(url)
if response.json().get("job_calibrations") != []:
logging.debug(f"Job is done, Job_ID: {job_id}")
return response.json().get("job_calibrations")
else:
logging.debug(f"Job is not done, Job_ID: {job_id}")
return False
def get_calibration(self, job_id):
"""Get calibration information for a job.
Args:
job_id (int): The ID of the job for which to retrieve calibration information.
Returns:
dict or None: A dictionary containing the calibration information if successful,
None otherwise.
Raises:
None
"""
if not self.session:
logging.warning(f"Please authenticate first, Job_ID: {job_id}")
return None
url = f"http://nova.astrometry.net/api/jobs/{job_id}/calibration/"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
logging.error(
f"Error getting calibration information. Status code: {response.status_code}, Job_ID: {job_id}"
)
return None
def get_wcs_file(self, job_id, save_path):
"""
Get the URL of the WCS (World Coordinate System) file for a given job ID
and download it to the specified save path.
Args:
job_id (int): The ID of the job.
save_path (str): The path where the downloaded WCS file will be saved.
Returns:
str: The URL of the WCS file if successful, None otherwise.
"""
if not self.session:
logging.warning(f"Please authenticate first, Job_ID: {job_id}")
return None
url = f"http://nova.astrometry.net/wcs_file/{job_id}"
response = requests.get(url)
if response.status_code == 200:
with open(save_path, "wb") as f:
f.write(response.content)
logging.info(f"WCS file downloaded successfully, Job_ID: {job_id}")
return response.url
else:
logging.error(
f"Error getting WCS file. Status code: {response.status_code}, Job_ID: {job_id}"
)
return None