test_galactic_lat_long.py

#!/usr/bin/python3
# ===================================================================
# convert galactic lat/lon to x,y,z coordinates
# view point is the sun/earth
# ===================================================================
# tests:
#
#   ---input---   -----results-----
#   -lat- -lon-   --x-- --y-- --z--
#   -----------   -----------------
#      0      0       0     0     1      
#      0     90       1     0     0
#      0    180       0     0    -1
#      0    270      -1     0     0
#
#     90      0       0     1     0
#    -90      0       0    -1     0
#     90     90       0     1     0
#    -90     90       0    -1     0
#
#      0     45    0.71     0   0.71
#     45     45    0.50  0.71   0.50
#
# ===================================================================

import sys
from math import radians, sin, cos
import user_interface as ui


# -------------------------------------------------------------------
# ---- convert galactic lat/lon to x,y,z
# ----  glat - galactic latitude  (float)
# ----  glon - galactic longitude (float) 
# -------------------------------------------------------------------

def lat_lon_to_xyz(glat,glon):

    # ---- convert to radians

    rlat = radians(glat)
    rlon = radians(glon)

    # ---- convert to x,y,z

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

    print()
    print(f'lat: {round(glat,2)} deg')
    print(f'lon: {round(glon,2)} deg')
    print(f'x  : {round(x,2)}')
    print(f'y  : {round(y,2)}')
    print(f'z  : {round(z,2)}')

    ui.pause()


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

while(True):

    ui.clear_screen()

    print()
    print('Convert galatic lat/lon to x,y,z')
    print('view point is the sun/earth')

    # ---- ask the user for the latitude

    while(True):
        print()
        s = ui.get_user_input('Enter latitude (deg): ')
        if not s:
            sys.exit()
        tr,lat = ui.is_float(s)
        if tr:
            break
        print(f'Error, try again')

    # ---- ask the user for the longitude

    while(True):
        print()
        s = ui.get_user_input('Enter longitude (deg): ')
        if not s:
            sys.exit()
        tr,lon = ui.is_float(s)
        if tr:
            break
        print(f'Error, try again')

    # ---- convert lat/lon to x,y,z

    lat_lon_to_xyz(lat,lon)