import os
import urllib.parse
import numpy as np
from utils import Url_manager
import requests
from bs4 import BeautifulSoup
import re
import requests
import io
from astroquery.skyview import SkyView
from astropy.io import fits
import astropy.units as u
import matplotlib.pyplot as plt
import astropy.io.fits as fits
from astropy.coordinates import SkyCoord
from astropy.coordinates import Angle
from astropy.units import deg
root_url = "https://xxxxx"
urls = Url_manager.UrlManager()
urls.add_new_url(root_url)
#fout = open("OK.txt", "w")
#current_fits_file =None
while urls.has_new_url():
curr_url = urls.get_url()
r = requests.get(curr_url)
if r.status_code != 200:
print("error,return status_code is not 200", curr_url)
continue
soup = BeautifulSoup(r.text, "lxml")
links = soup.find_all("a")
for link in links:
href = link.get("href")
newnew_url = curr_url + href
pattern1 = r'https://xxxxx/20\d{2}/\d{2}/\d{8}/[a-zA-Z0-9]+/.+s$'
if re.match(pattern1, newnew_url):
response = requests.get(newnew_url)
if response.status_code == 200:
with io.BytesIO(response.content) as fileobj:
# 使用BytesIO模拟一个file-like对象,将网络响应的内容传递给fits.open()
current_fits_file = fits.open(fileobj, ignore_missing_simple=True)
# 现在您可以像之前一样处理fits_file
header = current_fits_file[0].header
# 获取赤经和赤纬
ra_str = header['OBJCTRA'] # 或者 RA_OBJ、OBJCTRA 等对应的键名
dec_str = header['OBJCTDEC'] # 或者 DEC_OBJ、OBJCTDEC 等对应的键名
# 将字符串转换为天球坐标
try:
c = SkyCoord(ra=ra_str, dec=dec_str, unit=(u.hourangle, u.deg))
ra_degrees = c.ra.degree
dec_degrees = c.dec.degree
except KeyError: # 如果找不到RA或DEC,则需要根据实际的键名进行处理
print("无法在FITS头信息中找到赤经或赤纬数据")
# 打印赤经和赤纬(十进制度数格式)
print(f"赤经: {ra_degrees:.6f}°")
print(f"赤纬: {dec_degrees:.6f}°")
# 假设您已经有了 ra_degrees 和 dec_degrees
ra = Angle(ra_degrees, unit=deg)
dec = Angle(dec_degrees, unit=deg)
url_ = newnew_url[29:]
url__ = url_[1:]
final_fianl_final_final_observe = url__.split('/')[5]
observe_part = newnew_url.split('/')[-1]
observe_part_unqoute = urllib.parse.unquote(observe_part)
fits_observe_part_unqoute = observe_part_unqoute.replace(".fts", ".fits")
final_observe_decoded = observe_part_unqoute.split('_')[0]
workpath = "E:\\ok"
final_observe_path = os.path.join(workpath, final_fianl_final_final_observe,final_observe_decoded)
#final_observe_path = os.path.join(workpath, final_observe_decoded)
os.makedirs(final_observe_path, exist_ok=True)
local_dss_save_path = os.path.join(final_observe_path, fits_observe_part_unqoute)
png_save_path = local_dss_save_path.replace(".fits", ".png")
# # 创建一个 SkyCoord 对象
target_coord = SkyCoord(ra=ra, dec=dec, frame='icrs')
workpath1 = "E:\\okk"
workpath = "E:\\ok"
DSS = SkyView.get_images(position=target_coord, survey='DSS', radius=15 * u.arcmin)
DSS[0].writeto(workpath1 + '\\' + fits_observe_part_unqoute,overwrite=True)
#输出名字修改成目标名字
DSS = fits.open(workpath1 +'\\'+fits_observe_part_unqoute)
plt.imsave(png_save_path, DSS[0].data, cmap='gray', origin='lower') # 输出名字修改成目标名字
print("success:%s\n" % url_)
DSS.close()
if href is None:
continue
pattern = r'20\d+/|\d{2}/|\d{8}/|[a-zA-Z0-9]+/'
if re.match(pattern, href):
url = curr_url + href
urls.add_new_url(url)