solution_031c.py

#!/usr/bin/python3
# ===================================================================
# Test transformation matrix – draw all of the stars
#
# no star: rotation,selection,scaling
# ===================================================================

import graphics as gr
import draw_xy_axes as ax
import user_interface as ui
import coordinate_conversion as cc
from math import radians, sin, cos

import sys

star_catalog = 'bsc5.dat'

win_width    = 801
win_height   = 801


# -------------------------------------------------------------------
# ---- draw a circle
# -------------------------------------------------------------------

def draw_circle(win,x,y,color="black",raidus=2):

    wx,wy = cc.center_to_win_coords(x,y,win.width,win.height)

    c = gr.Circle(gr.Point(wx,wy),raidus)
    c.setFill(color)
    c.setWidth(1)
    c.setOutline(color)
    c.draw(win)
    return c 


# -------------------------------------------------------------------
# ---- draw stars
# -------------------------------------------------------------------

def draw_stars(win,stars):

    for s in stars:
        if s[2] > 0.0:
            if s[3] == '':
               draw_circle(win,s[0],s[1])
            else:
               draw_circle(win,s[0],s[1],'red')

# -------------------------------------------------------------------
# ---- convert galactic lat/lon to xyz
# ---- (unit vector is radius of sphere)
# -------------------------------------------------------------------

def lat_lon_to_xyz(lat,lon,scale=300.0):

    rlat = radians(lat)        # convert to radians
    rlon = radians(lon)        # convert to radians

    x = sin(rlat) * cos(rlon)
    y = sin(rlat)
    z = cos(rlat) * cos(rlon)

    return (x*scale, y*scale, z*scale)


# -------------------------------------------------------------------
# ---- convert galactic lat/lon to xyz
# -------------------------------------------------------------------

def read_star_catalog(catalog):

    stars = []

    lcount  = 0
    skipped = 0

    catalog_file = open(catalog,'r')

    for line in catalog_file:

        lcount += 1            # count lines

        # ---- strip '\r\n' characters from the end of the line

        line = line.rstrip('\r\n')

        # ---- get galactic lat/lon and convert to float

        lon = line[90:96].strip()
        lat = line[96:102].strip()

        if not lon or not lat:
            skipped += 1
            continue

        x,glon = ui.is_float(lon)

        if not x:
            catalog_file.close()
            print()
            print(f'unknow longitude value ({lon}) in record {i}')
            print()
            sys.exit()

        x,glat = ui.is_float(lat)

        if not x:
            catalog_file.close()
            print()
            print(f'unknow latitude value ({lat}) in record {i}')
            print()
            sys.exit()

        # ---- double,multiple-star code

        code = line[43]

        if code == ' ':
            code = ''

        # ---- convert galactic lat/lon to xyz coordinates

        x,y,z = lat_lon_to_xyz(glat,glon)

        stars.append((x,y,z,code))

        # ---- limit the number of stars for testing

        ##if lcount > 10:
        ##    break

    # ---- return stars

    catalog_file.close()

    print()
    print(f'{lcount:5} stars read from file')
    print(f'{skipped:5} stars skipped')
    return stars


# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------

if __name__ == '__main__':

    # ---- get a list of stars from the catalog

    stars = read_star_catalog(star_catalog)

    # ---- create window

    win = gr.GraphWin("Star Map",win_width,win_height)
    win.setBackground("white")

    # ---- draw axes

    ax.draw_xy_axes(win,True)

    # ---- draw the stars

    draw_stars(win,stars)

    # ---- end of program

    ui.pause()

    win.close()