solution_031e.py

#!/usr/bin/python3
# ===================================================================
# test rotating the point of interest in galactic lat/long
# to the +z axis and converting it to x,y,x coordinates
# ===================================================================

import user_interface as ui
import transformation_matrix as tm
import coordinate_conversion as cc
from math import radians, sin, cos
import sys, os, platform

min_poi_latitude  = -90.0      # degrees
max_poi_latitude  = +90.0      # degrees
min_poi_longitude = +0.0       # degrees
max_poi_longitude = +360.0     # degrees

# -------------------------------------------------------------------
# ---- get user's Point of Interest (POI)
# -------------------------------------------------------------------

def get_user_poi():

    ui.clear_screen()

    # --- galactic longitude

    print()
    print('Enter POI galactic longitude in degrees (0.0 to 360.0)')
    print('Enter nothing to exit program')

    while(True):

        print()
        s = ui.get_user_input('Enter POI longitude: ')

        if not s:
            sys.exit()

        tf,poilon = ui.is_float(s)

        if tf != True                  or \
           poilon < min_poi_longitude  or \
           poilon > max_poi_longitude:
            print('illegal longitude - try again')
            continue

        break

    # ---- poi latitude

    print()
    print('Enter POI gala tic latitude in degrees (-90.0 to +90.0)')
    print('Enter nothing to exit program')

    while(True):

        print()
        s = ui.get_user_input('Enter POI latitude: ')

        if not s:
            sys.exit()

        tf,poilat = ui.is_float(s)

        if tf != True                 or \
            poilat < min_poi_latitude or \
            poilat > max_poi_latitude:
            print()
            print()
            print('illegal latitude - try again')
            continue

        break

    return (poilat,poilon)

# -------------------------------------------------------------------
# ---- convert galactic lat/lon to x,y,z coordinates
# ---- (unit vector is radius of sphere)
# -------------------------------------------------------------------

def lat_lon_to_xyz(glat,glon):

    rlat = radians(glat)       # convert to radians
    rlon = radians(glon)       # convert to radians

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

    return (x, y, z)


# -------------------------------------------------------------------
# ---- 
# -------------------------------------------------------------------

def galactic_rotation_matrix(glat,glon):

    lat_mtrx = tm.get_x_rotation_matrix_3d(glat)

    lon_mtrx = tm.get_y_rotation_matrix_3d(-glon)

    mtrx  = lat_mtrx @ lon_mtrx

    return mtrx


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

if __name__ == '__main__':

    while(True):

        ui.clear_screen()

        lat,lon = get_user_poi()

        print(f'lat = {lat}')
        print(f'lon = {lon}')

        mtrx = galactic_rotation_matrix(lat,lon)

        x,y,z = lat_lon_to_xyz(lat,lon)

        print(f'initial coordinates')
        print(f'x = {round(x,6)}')
        print(f'y = {round(y,6)}')
        print(f'z = {round(z,6)}')

        print('rotate to the +Z axes')

        xyz = mtrx @ (x,y,z,1)

        print(f'final coordinates')
        print(f'x = {round(xyz[0],6)}')
        print(f'y = {round(xyz[1],6)}')
        print(f'z = {round(xyz[2],6)}')

        ui.pause()