-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlegacysurvey_crawler.py
91 lines (81 loc) · 4.11 KB
/
legacysurvey_crawler.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
import contextlib
import os
import requests
from astropy.coordinates import SkyCoord
import astropy.coordinates.name_resolve as name_resolve
def resolve_name(name):
with contextlib.suppress(Exception):
return name_resolve.get_icrs_coordinates(name)
with contextlib.suppress(Exception):
return name_resolve.get_icrs_coordinates(name, 'SIMBAD')
with contextlib.suppress(Exception):
# Try to resolve using Sesame
coord = SkyCoord.from_name(name)
return coord.icrs
return None
def download_cutout_image(name=None,
ra=None,
dec=None,
layer=None,
pixscale=None,
size=None,
save_to_file=True,
print_info=False,
root=".",
fits=False):
# sourcery skip: merge-else-if-into-elif
'''
This Python function allows users to download cutout images from the Legacy Survey website. The cutouts are of a specific sky location and are saved as either a JPG or FITS file. The following parameters can be set:
- `name`: If `None`, the right ascension (`ra`) and declination (`dec`) of the sky location must be given.
- `ra`: The right ascension of the sky location.
- `dec`: The declination of the sky location.
- `layer`: The Legacy Survey layer to use.
- `pixscale`: The angular size of the pixels in arc seconds.
- `size`: The length of each side of the cutout image in pixels.
- `save_to_file`: Whether or not to save the downloaded image to a file.
- `print_info`: Whether or not to print information about the download process.
- `root`: The directory in which to save the downloaded image file.
- `fits`: Whether to save the cutout image as a FITS file. If not, it is saved as a JPG file.
If `name` is not `None`, but `ra` and `dec` are `None`, `resolve_name` is called to determine the `ra` and `dec` of the given name. The URL for the image is then generated based on the specified parameters and a GET request is made to download the image data. If `save_to_file` is `True`, the image is saved to a file in the specified directory, and if `print_info` is `True`, information about the saved file is printed. The function returns the image data if the download was successful, and `None` otherwise.
'''
if (name is not None) and (ra is None) and (dec is None):
ra_dec = resolve_name(name)
ra = ra_dec.ra.value
dec = ra_dec.dec.value
if fits:
url = f"https://www.legacysurvey.org/viewer/cutout.fits?ra={ra}&dec={dec}&layer={layer}&pixscale={pixscale}&size={size}"
else:
url = f"https://www.legacysurvey.org/viewer/jpeg-cutout?ra={ra}&dec={dec}&layer={layer}&pixscale={pixscale}&size={size}"
response = requests.get(url)
if response.status_code == 200:
if save_to_file:
if fits:
filename = f"{layer}_ra{ra:.4f}_dec{dec:.4f}_scale{pixscale}_size{size}.fits"
else:
filename = f"{layer}_ra{ra:.4f}_dec{dec:.4f}_scale{pixscale}_size{size}.jpg"
file_path = os.path.join(root, filename)
with open(file_path, "wb") as f:
f.write(response.content)
if print_info:
print(
f"Downloaded {layer} cutout image for RA {ra}, Dec {dec} with a pixscale of {pixscale} and size of {size} to {file_path}"
)
else:
if print_info:
print(
f"Image not saved to file. Content: {response.content[:50]}..."
)
return response.content
else:
if print_info:
print("Failed to download image")
return None
if __name__ == '__main__':
download_cutout_image(name='ngc1365',
layer='ls-dr10',
pixscale=1,
size=512,
save_to_file=True,
print_info=True,
root=".",
fits=False)