Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script to generate URLs for Ultracool Sheet. Closes #389 #451

Merged
merged 8 commits into from
Jan 11, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions scripts/ultracool_sheet/generate_simple_links.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# from scripts.ingests.ingest_utils import *
from scripts.ingests.utils import load_simpledb, find_source_in_db
kelle marked this conversation as resolved.
Show resolved Hide resolved
from astropy.io import ascii
from urllib.parse import quote
import requests
from astropy.table import Table


RECREATE_DB = False
db = load_simpledb("SIMPLE.sqlite", recreatedb=RECREATE_DB)

# Load Ultracool sheet
sheet_id = "1i98ft8g5mzPp2DNno0kcz4B9nzMxdpyz5UquAVhz-U8"
# link = f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv"
link = "scripts/ultracool_sheet/UltracoolSheet - Main_010824.csv"

# read the csv data into an astropy table
uc_sheet_table = ascii.read(
link,
format="csv",
data_start=1,
header_start=0,
guess=False,
fast_reader=False,
delimiter=",",
)

# Match sources in Ultracool sheet to sources in SIMPLE
uc_names = []
simple_urls = []
simple_sources = []
for source in uc_sheet_table[0:100]:
uc_sheet_name = source["name"]
match = find_source_in_db(
db,
uc_sheet_name,
ra=source["ra_j2000_formula"],
dec=source["dec_j2000_formula"],
)

# convert SIMPLE source name to URL
if len(match) == 0:
msg = f"No match found for {uc_sheet_name}"
raise ValueError(msg)
elif len(match) > 1:
msg = f"Multiple matches found for {uc_sheet_name}"
raise ValueError(msg)
elif len(match) == 1:
simple_source = match[0]
print(f"Match found for {uc_sheet_name}: {simple_source}")
else:
raise ValueError("Unexpected state")

# URLify source name
source_url = quote(simple_source)
url = "https://simple-bd-archive.org/solo_result/" + source_url

# TODO: THIS DOESN'T WORK!!! Even bad URLs return 200
# test the URL
# url_status = 200
url_status = requests.get(url).status_code
kelle marked this conversation as resolved.
Show resolved Hide resolved
if url_status != 200:
raise ValueError("URL not valid for ", uc_sheet_name, simple_source, url)
else:
print("URL valid for ", uc_sheet_name, simple_source, url)
Will-Cooper marked this conversation as resolved.
Show resolved Hide resolved

uc_names.append(uc_sheet_name)
simple_sources.append(simple_source)
simple_urls.append(url)

# write the results to a file
results_table = Table(
[uc_names, simple_sources, simple_urls],
names=["Ultracool Sheet Name", "SIMPLE Source Name", "SIMPLE URL"],
)
results_table.write(
"scripts/ultracool_sheet/uc_sheet_simple_urls.csv",
delimiter=",",
overwrite=True,
format="ascii.ecsv",
)