-
Notifications
You must be signed in to change notification settings - Fork 0
/
rocketcalc.py
137 lines (118 loc) · 5.11 KB
/
rocketcalc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import pandas as pd
import os
import sys
import time
start_time = time.time()
"""[General director set]
Sets the working directory to the current directory \
that the __main__ file is being executed from
"""
os.chdir(os.path.dirname(os.path.abspath(__file__)))
"""[General UI render and parse]
Will ask user a series of questions, and then respond \
accordingly.
"""
def run():
temp_string = ""
while True:
temp_string = input("Enter name of CSV to calculate or Name of Object you wish to print \
(enter 'format' for the required format) or exit: ")
if temp_string == "exit":
break
elif "csv" in temp_string and "name" not in temp_string:
file = temp_string.split(" ")[1]
name = None
break
elif "name" in temp_string and "csv" in temp_string:
name = temp_string.split(" ")[1]
file = temp_string.split(" ")[3]
break
elif "format" in temp_string:
print("Proper Formatting -\n To calculate: \"csv {name of file}\n To print Name: name {name of object} csv {name of csv}")
else:
print("Please enter proper format\n")
calculate(file, temp_string, name)
"""[Calculations and Resultant]
Takes file, string, and name input in order to calculate \
the resultant force, as well as, calculate the launch needed.
"""
def calculate(file, temp_string, name=None):
untapped_data = pd.read_csv(file)
foo = 0
if "csv " in temp_string and "name " not in temp_string:
try:
while (foo <= untapped_data.shape[0]):
# Indexing the DataFrame
current_name = untapped_data['object_name'][foo]
current_mass_grams = untapped_data['object_mass'][foo]
current_thrust = untapped_data['object_thrust'][[foo]]
# Convert grams to kilograms = G/1000
current_mass_kilograms = current_mass_grams / 1000
# Calculate weight through gravity = KG * 9.8 // Converts to Newtons
current_weight_n = current_mass_kilograms * 9.8
# Calculate the resultant force
current_resultant_force = current_thrust - current_weight_n
# Calculate the acceleration m/s²
current_acceleration = current_resultant_force / current_mass_kilograms
#print(int(current_resultant_force))
untapped_data.at[foo, 'resultantForce'] = str(round(float(current_resultant_force), 2)) + " N"
untapped_data.at[foo, 'acceleration'] = str(round(float(current_acceleration), 2)) + " m/s²"
# Pointer
foo += 1
except Exception as e:
untapped_data.to_csv("modified_"+file, index=False)
os.system("clear")
time_calculation = round(time.time() - start_time, 2)
if time_calculation < 1:
ending = "ms"
else:
ending = "mins"
print(f"Calculations finished\n\n---------LOG---------\n{e}\nTime Taken: {time_calculation}{ending}\n")
elif "name" in temp_string:
os.system("clear")
data = untapped_data.loc[untapped_data['object_name'] == name]
if "Empty DataFrame" in str(data):
print(f"No data found for {name}")
else:
print("----------------------------------------------------------------")
print(f"----------------------Data for {name}------------------------")
print("----------------------------------------------------------------")
print(str(data))
else:
os.system("clear")
print("Unable to access CSV")
"""[Argument parser]
Parse command line arguments and then respond accordingly.
"""
def main():
args = sys.argv[1:]
try:
if len(args) >=1:
if "-csv" in args and "-name" not in args:
child_location = int(args.index("-csv")) + 1
file = args[child_location]
calculate(file, f"csv {file}")
elif "-name" in args and "-csv" in args:
child_location_file = int(args.index("-csv")) + 1
child_location_name = int(args.index("-name")) + 1
if "-name" not in args[child_location_file] and "-csv" not in args[child_location_name]:
file = args[child_location_file]
name = args[child_location_name]
calculate(file=file, temp_string=(f"name {name} csv {file}"), name=name)
else:
print(f"rocketcalc.py[ Missing argument(1) ]")
elif args[0] == "-run":
run()
else:
os.system("clear")
print(f"Unable to access CSV with commands: {args}")
except Exception as e:
os.system("clear")
if "list index" in str(e):
print(f"rocketcalc.py[ Missing argument(1) ]")
elif "local variable" in str(e):
print("rocketcalc.py[ Exit(0) ]")
else:
print(f"ERROR: {e}")
if __name__ == "__main__":
main()