+1 (315) 557-6473 

Python Program to Implement Ballistic Tables Assignment Solution.


Instructions

Objective
Write a python program to implement ballistic tables in python.

Requirements and Specifications

Overview: This program will extend what you did in the earlier ballistic table program by first writing the calculated data to a file, then reading in that data and displaying it in the same format as before. Ideally, this would be done in two separate program files, one for creating data, one for reading/displaying it, but for convenience both aspects will be in one file.
Program description: Using to complete a python assignment, correct version of HW5 as the starting point, add functions to write the calculations to a file, to read the file data into a list of lists, and modify the printing function to just do the display of the data read in. Function main will be responsible for opening and testing the output and input files.
The input angles will control the number of rows, based on the angle increment being added to the min angle, until the max angle is met or exceeded. For each row of output, the velocities will range from the minimum to the maximum, in eight even steps.
Source Code
# hw5 - Ballistic Table with functions
# V Manes 2/11/21
import math
GRAV = 9.81 # meters per second^2
# get named angle within specified limits
def get_angle(angle_name, min_angle, max_angle):
    angle = min_angle - 1
    # repeat input till valid
    while angle <= min_angle or angle >= max_angle:
        print("Enter", angle_name, "angle in range", min_angle, "-", max_angle)
        angle = float(input("Angle: "))
    return angle
# get named velocity within specified limits
def get_velocity(vel_name, min_vel, max_vel):
    velocity = min_vel - 1
    # repeat input till valid
    while velocity < min_vel or velocity > max_vel:
        print("Enter", vel_name, "velocity in range", min_vel, "-", max_vel)
        velocity = float(input("Velocity: "))
    return velocity
# get angle increment, less tha 1/3 differnece of angles
def get_increment(min_angle, max_angle):
    increment = 0
    # input increment
    increment = float(input("Angle Increment: "))
    # repeat input till valid
    while increment > 1 / 3 * (max_angle - min_angle):
        print("Increment too large, try again")
        increment = float(input("Angle Increment: "))
    return increment
# calculate distance projectile flies
def calc_distance(vel, rad_angle):
    # basic formula, no external effects
    return vel * math.cos(rad_angle) / GRAV * (vel * math.sin(rad_angle)
                                               + math.sqrt((vel * math.sin(rad_angle)) ** 2))
'''
######## New work goes below here #############
'''
'''***this function will be deleted***'''
def write_table_data(file, min_angle, max_angle, angle_step, min_vel, vel_step, num):
    file.write("V/A ")
    # print velocity values
    for i in range(num):
        file.write('{0:.1f} '.format(min_vel + (i * vel_step)))
    file.write("\n")
    angle = min_angle
    # comput from minimum to maximum angle
    while angle <= max_angle:
        # convert angle
        rad_angle = math.radians(angle)
        # reset to starting velocity
        vel = min_vel
        # display current angle
        file.write("{0:.1f} ".format(angle))
        # step through velocities, calculating distances flown
        for i in range(num):
            # find distance travelled
            distance = calc_distance(vel, rad_angle)
            file.write("{0:.1f} ".format(distance))
            # move to next velocity
            vel += vel_step
        # move to next angle
        angle += angle_step
        file.write("\n")
    file.close()
def read_table_data(file):
    lines = file.readlines()
    counter_line = 0
    counter_col = 0
    ret = list()
    for line in lines:
        line = line.strip() # remove \n
        elements = line.split(" ")
        sub_list = list()
        for x in elements:
            if x != 'V/A':
                sub_list.append(float(x))
            else:
                sub_list.append(0.0)
        ret.append(sub_list)
    file.close()
    return ret
# print column headings
def print_header(min_vel, vel_step, num):
    print("V/A ", end='')
    # print velocity values
    for i in range(num):
        print('{:10.1f}'.format(min_vel + (i * vel_step)), end='')
    print()
'''this function will be split into calclate/write to file and
    into print the data to screen functions'''
# print table of distances *** this function will be modified
def print_table(data):
    col_counter = 0
    for row in data:
        col_counter = 0
        for point in row:
            if point == 0.0:
                print("V/A", end='')
            else:
                if col_counter == 0:
                    print("{0:.1f}".format(point), end = '')
                else:
                    print("{:10.1f}".format(point), end= '')
            col_counter += 1
        print("") # new line
'''add the read_data function'''
def main():
    print("Velocity Table Printer\n")
    '''open and test input file'''
    # get angle inputs
    min_angle = get_angle("Minimum", 0, 90)
    max_angle = get_angle("Maximum", min_angle, 90)
    angle_step = get_increment(min_angle, max_angle)
    # get velocity inputs
    min_vel = get_velocity("Minimum", 10, 1000)
    max_vel = get_velocity("Maximum", min_vel, 1000)
    # calculate velocity increments
    vel_step = (max_vel - min_vel) / 7
    '''remove this call'''
    #print_header(min_vel, vel_step, 8)
    '''calculate data and write to file'''
    FILE_NAME = 'table_data.txt'
    file = open(FILE_NAME, "w+")
    write_table_data(file, min_angle, max_angle, angle_step, min_vel, vel_step, 7)
    '''open and test input file'''
    file = open(FILE_NAME, "r+")
    '''read data from input file to list of lists'''
    data = read_table_data(file)
    '''modify the call to this function'''
    print_table(data)
main()