From 703210f2287e901aa8a463f4e1e394de136311b6 Mon Sep 17 00:00:00 2001 From: KastanDay Date: Tue, 29 Aug 2023 22:23:03 +0000 Subject: [PATCH] Format code with Yapf & custom options --- eklavya/image_testing.py | 74 +++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/eklavya/image_testing.py b/eklavya/image_testing.py index 56008b6..9dcb27f 100644 --- a/eklavya/image_testing.py +++ b/eklavya/image_testing.py @@ -1,13 +1,14 @@ from function_calling_sloan import * import requests + # Function to query galaxies within 1 arcminute of a given point def query_galaxies(ra, dec): - # Base URL for the SDSS API's SkyServer SQL Search service - base_url = "http://skyserver.sdss.org/dr16/SkyServerWS/SearchTools/SqlSearch" + # Base URL for the SDSS API's SkyServer SQL Search service + base_url = "http://skyserver.sdss.org/dr16/SkyServerWS/SearchTools/SqlSearch" - # SQL query to retrieve galaxies within 1 arcminute of the specified point - query = f""" + # SQL query to retrieve galaxies within 1 arcminute of the specified point + query = f""" SELECT TOP 100 p.objid, p.ra, p.dec FROM PhotoObjAll p @@ -17,21 +18,22 @@ def query_galaxies(ra, dec): AND p.type = 3 -- Select only galaxies (type=3) """ - # Construct the API request URL - url = f"{base_url}?cmd={query}" + # Construct the API request URL + url = f"{base_url}?cmd={query}" - # Make the HTTP GET request to the SDSS API - response = requests.get(url) + # Make the HTTP GET request to the SDSS API + response = requests.get(url) + + # Check if the request was successful (status code 200) + if response.status_code == 200: + # Parse the response JSON to obtain the galaxy coordinates + data = response.json() + galaxies = [(i + 1, float(entry['ra']), float(entry['dec'])) for i, entry in enumerate(data)] + return galaxies + else: + print(f"Error: Unable to fetch galaxy data. Status code: {response.status_code}") + return [] - # Check if the request was successful (status code 200) - if response.status_code == 200: - # Parse the response JSON to obtain the galaxy coordinates - data = response.json() - galaxies = [(i+1, float(entry['ra']), float(entry['dec'])) for i, entry in enumerate(data)] - return galaxies - else: - print(f"Error: Unable to fetch galaxy data. Status code: {response.status_code}") - return [] # Given point (RA, Dec) in degrees ra_point = 184.9511 @@ -40,26 +42,28 @@ def query_galaxies(ra, dec): # Query galaxies within 1 arcminute of the given point galaxies_within_1arcmin = query_galaxies(ra_point, dec_point) + # Function to get the image cutouts for the galaxies def get_galaxy_images(galaxies, image_size=256): - # Base URL for the SDSS API's Image Cutout service - base_url = "http://skyserver.sdss.org/dr16/SkyServerWS/ImgCutout/getjpeg" - - for i, (ra, dec) in enumerate(galaxies): - # Construct the API request URL for each galaxy - url = f"{base_url}?ra={ra}&dec={dec}&scale=0.2&width={image_size}&height={image_size}" - - # Make the HTTP GET request to the SDSS API - response = requests.get(url) - - # Check if the request was successful (status code 200) - if response.status_code == 200: - # Save the image as a file - with open(f"galaxy_{i+1}.jpg", "wb") as f: - f.write(response.content) - print(f"Image for galaxy {i+1} saved as galaxy_{i+1}.jpg") - else: - print(f"Error: Unable to fetch the image for galaxy {i+1}. Status code: {response.status_code}") + # Base URL for the SDSS API's Image Cutout service + base_url = "http://skyserver.sdss.org/dr16/SkyServerWS/ImgCutout/getjpeg" + + for i, (ra, dec) in enumerate(galaxies): + # Construct the API request URL for each galaxy + url = f"{base_url}?ra={ra}&dec={dec}&scale=0.2&width={image_size}&height={image_size}" + + # Make the HTTP GET request to the SDSS API + response = requests.get(url) + + # Check if the request was successful (status code 200) + if response.status_code == 200: + # Save the image as a file + with open(f"galaxy_{i+1}.jpg", "wb") as f: + f.write(response.content) + print(f"Image for galaxy {i+1} saved as galaxy_{i+1}.jpg") + else: + print(f"Error: Unable to fetch the image for galaxy {i+1}. Status code: {response.status_code}") + # Get images for the galaxies within 1 arcminute get_galaxy_images(galaxies_within_1arcmin)