Skip to content

Instantly share code, notes, and snippets.

@thexa4
Created July 16, 2022 17:25
Show Gist options
  • Save thexa4/e40c033be3ef5444aea0ee701b720957 to your computer and use it in GitHub Desktop.
Save thexa4/e40c033be3ef5444aea0ee701b720957 to your computer and use it in GitHub Desktop.
Stars
#!/usr/bin/env python3
import pyvo
import requests
import sqlite3
from datetime import datetime
service_name = "Gaia@AIP"
url = "https://gaia.aip.de/tap"
token = ''
print('TAP service %s \n' % (service_name,))
# Setup authorization
tap_session = requests.Session()
tap_session.headers['Authorization'] = token
tap_service = pyvo.dal.TAPService(url, session=tap_session)
db = sqlite3.connect("stars.sqlite")
connection = db.cursor()
def fetch_n(start, n, tap_service):
query = f'''
SELECT "ra", "dec", "phot_g_mean_mag", "phot_bp_mean_mag", "phot_rp_mean_mag"
FROM "gaiadr3"."gaia_source_lite"
WHERE "random_index" >= {start} AND "random_index" < {start + n}
'''
return tap_service.run_sync(query, language='PostgreSQL')
connection.execute('CREATE TABLE IF NOT EXISTS stars (ra REAL, dec REAL, brightness REAL, redness REAL, blueness REAL)')
curcount = connection.execute('SELECT COUNT(1) from stars').fetchall()[0][0]
fetch_size = 25000
last_id=1811709770
start_time = datetime.now()
start_id = curcount
print(curcount)
while curcount <= last_id:
stars = fetch_n(curcount, fetch_size, tap_service)
rows = []
for row in stars:
ra = float(row['ra'])
dec = float(row['dec'])
brightness = float(row['phot_g_mean_mag'])
if brightness != brightness:
brightness = None
redness = float(row['phot_rp_mean_mag'])
if redness != redness:
redness = None
blueness = float(row['phot_bp_mean_mag'])
if blueness != blueness:
blueness = None
rows.append((ra, dec, brightness, redness, blueness))
connection.executemany('INSERT INTO stars VALUES(?, ?, ?, ?, ?)', rows)
db.commit()
report_size = 10000
prevpercent = int(curcount / last_id * report_size)
nextpercent = int((curcount + fetch_size) / last_id * report_size)
if prevpercent != nextpercent:
left = last_id - curcount
done = curcount - start_id
if done <= 0:
done = 1
delta = datetime.now() - start_time
eta = delta / done * left
percentage = prevpercent * 100 / report_size
print(f'{percentage}%, {eta}')
curcount += connection.rowcount
print("Indexing...")
connection.execute('CREATE INDEX IF NOT EXISTS stars_dec ON stars (dec DESC)')
#!/usr/bin/env python3
import numpy as np
import imageio
import sqlite3
import math
import shutil
def project(lat, lng, width, height):
x = (lng * (width / 360.0))
y = ((-lat + 90.0) * (height / 180.0))
return (x, y)
def unproject(x, y, width, height):
lng = x / (width / 360.0)
lat = (y / (height / 180.0) - 90.0) * -1
return (lat, lng)
width = 16 * 1024
height = 8 * 1024
db = sqlite3.connect('stars.sqlite')
image = np.zeros((height, width, 3), dtype='float64')
#lat == ra
#lng == dec
curpixel = 0
startlat = 90
starcount = 0
maximum = 1
while curpixel < height:
stoplat, _ = unproject(0, curpixel + 1, width, height)
samples = db.execute('select * from stars where dec <= ? AND dec > ? order by brightness asc', (startlat, stoplat))
for star in samples:
starcount += 1
if starcount % 100000 == 0:
print(str(starcount) + ": " + str(curpixel) + ", max: " + str(maximum))
lng, lat, brightness, redness, blueness = star
x,y = project(lat, lng, width, height)
x = int(x)
y = int(y)
values = list(filter(None, [brightness, blueness, redness]))
if len(values) == 0:
continue
mean = sum(values) / float(len(values))
if brightness == None:
brightness = mean
if blueness == None:
blueness = mean
if redness == None:
redness = mean
image[y, x, 0] += math.exp(redness - 20)
image[y, x, 1] += math.exp(brightness - 20)
image[y, x, 2] += math.exp(blueness - 20)
curpixel += 1
startlat = stoplat
maximum = np.amax(image)
if maximum == 0:
maximum = 1
normalized = (image / maximum).astype('float32')
imageio.imwrite('skybox.tmp.exr', normalized)
shutil.move('skybox.tmp.exr', 'skybox.exr')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment