@@ -1012,31 +1012,36 @@ def export_eng(self, file_name, motor_name):
1012
1012
None
1013
1013
"""
1014
1014
# Open file
1015
- file = open (file_name , "w" )
1016
-
1017
- # Write first line
1018
- file .write (
1019
- motor_name
1020
- + " {:3.1f} {:3.1f} 0 {:2.3} {:2.3} RocketPy\n " .format (
1021
- 2000 * self .grain_outer_radius ,
1022
- 1000
1023
- * self .grain_number
1024
- * (self .grain_initial_height + self .grain_separation ),
1025
- self .propellant_initial_mass ,
1026
- self .propellant_initial_mass ,
1027
- )
1028
- )
1015
+ with open (file_name , "w" ) as file :
1016
+ # Write first line
1017
+ def get_attr_value (obj , attr_name , multiplier = 1 ):
1018
+ return multiplier * getattr (obj , attr_name , 0 )
1019
+
1020
+ grain_outer_radius = get_attr_value (self , "grain_outer_radius" , 2000 )
1021
+ grain_number = get_attr_value (self , "grain_number" , 1000 )
1022
+ grain_initial_height = get_attr_value (self , "grain_initial_height" )
1023
+ grain_separation = get_attr_value (self , "grain_separation" )
1024
+
1025
+ grain_total = grain_number * (grain_initial_height + grain_separation )
1026
+
1027
+ if grain_outer_radius == 0 or grain_total == 0 :
1028
+ warnings .warn (
1029
+ "The motor object doesn't have some grain-related attributes. "
1030
+ "Using zeros to write to file."
1031
+ )
1029
1032
1030
- # Write thrust curve data points
1031
- for time , thrust in self .thrust .source [1 :- 1 , :]:
1032
- # time, thrust = item
1033
- file .write ("{:.4f} {:.3f}\n " .format (time , thrust ))
1033
+ file .write (
1034
+ f"{ motor_name } { grain_outer_radius :3.1f} { grain_total :3.1f} 0 "
1035
+ f"{ self .propellant_initial_mass :2.3} "
1036
+ f"{ self .propellant_initial_mass :2.3} RocketPy\n "
1037
+ )
1034
1038
1035
- # Write last line
1036
- file .write ("{:.4f} {:.3f}\n " .format (self .thrust .source [- 1 , 0 ], 0 ))
1039
+ # Write thrust curve data points
1040
+ for time , thrust in self .thrust .source [1 :- 1 , :]:
1041
+ file .write (f"{ time :.4f} { thrust :.3f} \n " )
1037
1042
1038
- # Close file
1039
- file .close ( )
1043
+ # Write last line
1044
+ file .write ( f" { self . thrust . source [ - 1 , 0 ]:.4f } { 0 :.3f } \n " )
1040
1045
1041
1046
return None
1042
1047
0 commit comments