Astrometry.net是一个可以计算WCS的软件,可以计算观测图像的ra,dec,并与参考星匹配。本文提供了该软件的基于python的接口调用方法,方便批量处理图像。
官方说明文档:
https://nova.astrometry.net/api_help
需要先登陆,得到一个api_key,一个账号一个api_key
http://astrometry.net/doc/net/api.html
以下程序都是按照这个文档写的,写成了一个类的形式
import json
import requests
class AstrometryAPI:
def __init__(self, api_key, session=''):
self.api_key = api_key
if len(session): self.session_id = session
else: self.session_id = None
self.subid = None
self.jobid = None
def get_session(self):
response = requests.post('http://nova.astrometry.net/api/login', data={'request-json': json.dumps({"apikey": self.api_key})})
self.session_id = response.text.split('"')[-2]
print('SESSION:', self.session_id)
def submit_file(self, file_path, file_name, **params):
# 注:file_name应与file_path中的文件名一致,带扩展名
headers = {
"MIME-Version": "1.0"
}
# 默认值
json_data = {
"session": self.session_id,
"publicly_visible": "y",
"allow_modifications": "d",
"allow_commercial_use": "d"
}
# 其他参数请参考: http://astrometry.net/doc/net/api.html
json_data.update(params)
files = {
"request-json": (
None, # 参数名
json.dumps(json_data),
'text/plain', # Content-Type 设置为 text/plain
{
'Content-disposition': 'form-data; name="request-json"'
}
),
"file": (
file_name,
open(file_path, 'rb'),
'application/octet-stream', # Content-Type 设置为 application/octet-stream
{
'Content-disposition': f'form-data; name="file"; filename="{file_name}"'
}
)
}
response = requests.post('http://nova.astrometry.net/api/upload', files=files, headers=headers)
self.subid = response.text.split('id": ')[-1].split(',')[0]
print('SUBID:', self.subid)
def check_submission(self):
count = 0
while count < 100:
response = requests.get(f'http://nova.astrometry.net/api/submissions/{self.subid}')
if not any(char.isdigit() for char in response.text.split('jobs": ')[-1].split(',')[0]):
time.sleep(0.5)
count += 1
else:
break
if count > 99:
print('Submission check timed out!')
self.jobid = response.text.split('jobs": [')[-1].split(']')[0]
print('JOBID:', self.jobid)
def check_job_completion(self):
count = 0
while count < 100:
response = requests.get(f'http://nova.astrometry.net/api/jobs/{self.jobid}')
if 'success' not in response.text:
time.sleep(0.5)
count += 1
else:
break
if count > 99:
print('Job completion check timed out!')
print("Job completion!")
def download_results(self, file_name, file_result, save_path):
# files = ['wcs_file', 'rdls_file', 'axy_file', 'corr_file']
results = [f'http://nova.astrometry.net/{f}/{self.jobid}' for f in file_result]
savepathes = [save_path+file_name+'_' + f + '.fits' for f in file_result]
for i, result in enumerate(results):
count = 0
while count < 50:
try:
response = requests.get(result)
if response.status_code == 200:
with open(savepathes[i], 'wb') as f:
f.write(response.content)
print(f"File saved successfully: {savepathes[i]}")
break
else:
time.sleep(1)
count += 1
except Exception as e:
print(f'Error downloading file: {e}')
time.sleep(1)
count += 1
if count > 49:
print(f'Failed to download file: {result}')
调用案例:
# 1、声明,这里的XXX需换成自己的api_key
api = AstrometryAPI(api_key="XXX")
# 2、获取session,一个session可以重复使用,不需要反复创建
api.get_session()
# 3、file_name应与file_path中的文件名一致,支持自定义参数,具体参考文章开头的官方文档,获得subid
api.submit_file(file_path="./data/2024bch-0001B-new.fit", file_name='2024bch-0001B-new.fit')
# 4、根据subid检查是否提交成功,获得jobid
api.check_submission()
# 5、根据jobid检查是否处理完成,完成后才能下载,否则可能发生报错或者文件数据错误
api.check_job_completion()
# 6、根据jobid下载数据,fits格式文件,支持自选
api.download_results(file_name='2024bch-0001B-new', file_result=['wcs_file', 'rdls_file', 'axy_file', 'corr_file'], save_path='./')
可选下载文件包括:
http://nova.astrometry.net/wcs_file/JOBID
# 该文件为坐标系统转换系数,仅包含header数据,可以用来将像素坐标x,y转换为赤经赤纬RA,DEC
http://nova.astrometry.net/new_fits_file/JOBID
http://nova.astrometry.net/rdls_file/JOBID
# 该文件内保存了参考星的RA,DEC
http://nova.astrometry.net/axy_file/JOBID
# 该文件内保存了从图片中提取出的星的X,Y,FLUX,BACKGROUND
http://nova.astrometry.net/corr_file/JOBID
# 该文件内保存了提取的星和参考星的共同源,列名包括field_x,field_y,field_ra,field_dec,index_x,index_y,index_ra,index_dec,index_id,field_id,match_weight,FLUX,BACKGROUND
http://nova.astrometry.net/annotated_display/JOBID
http://nova.astrometry.net/red_green_image_display/JOBID
http://nova.astrometry.net/extraction_image_display/JOBID