import pandas
import numpy as np
import urllib.request
import os
import json
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

# first download data from https://github.com/astronexus/HYG-Database
if not os.path.isfile('hygdata_v3.csv'):
    try:
        print('downloading hygdata_v3.csv')
        urllib.request.urlretrieve('http://github.com/astronexus/HYG-Database/blob/master/hygdata_v3.csv?raw=true', 'hygdata_v3.csv')
    except Exception as inst:
        print(inst)
        print(' Please download hygdata_v3.csv from https://github.com/astronexus/HYG-Database first')
        
df = pandas.read_csv('hygdata_v3.csv')
stars = df[['rarad','decrad','mag','hr','ci','proper']] # select columns that I am interested in
stars9000 = stars[pandas.notna(df['hr'])] # select 9000 or so with hr names

result = {
    "rightAscension":stars9000['rarad'].tolist(),
    "declination":stars9000['decrad'].tolist(),
    "apparentMagnitude": stars9000['mag'].tolist(),
    "colorIndexBV": stars9000['ci'].tolist(),
    "properName": stars9000['proper'].replace(np.nan,'').tolist(),
    "harvardID": stars9000['hr'].replace(np.nan,'').tolist(),
    "color": [
		[0.61, 0.7, 1], [0.62, 0.71, 1], [0.64, 0.72, 1], [0.66, 0.75, 1], [0.7, 0.77, 1], [0.73, 0.8, 1], [0.77, 0.82, 1], [0.8, 0.84, 1], [0.82, 0.86, 1], [0.85, 0.88, 1], [0.87, 0.89, 1], [0.89, 0.91, 1], [0.91, 0.92, 1], [0.93, 0.93, 1], [0.95, 0.95, 1], [0.97, 0.96, 1], [0.99, 0.97, 1], [1, 0.97, 0.98], [1, 0.96, 0.96], [1, 0.96, 0.93], [1, 0.95, 0.91], [1, 0.94, 0.89], [1, 0.93, 0.88], [1, 0.93, 0.86], [1, 0.92, 0.84], [1, 0.91, 0.82], [1, 0.91, 0.8], [1, 0.9, 0.79], [1, 0.89, 0.77], [1, 0.89, 0.76], [1, 0.88, 0.75], [1, 0.88, 0.73], [1, 0.87, 0.72], [1, 0.86, 0.7], [1, 0.86, 0.69], [1, 0.85, 0.68], [1, 0.84, 0.66], [1, 0.84, 0.64], [1, 0.83, 0.63], [1, 0.82, 0.61], [1, 0.81, 0.59], [1, 0.8, 0.56], [1, 0.78, 0.52], [1, 0.75, 0.47], [1, 0.71, 0.39], [1, 0.66, 0.29], [1, 0.58, 0.14], [1, 0.48, 0], [1, 0.32, 0]
	],
}

with open('hygDataFiltered.json', 'w') as outfile:
    jstr = json.dumps(result)
    jstr3 = jstr.replace(", ",",")
    jstr4 = jstr3.replace("NaN","\"NaN\"")
    outfile.write(jstr4)