1 package net.sf.openrocket.file;
3 import java.io.BufferedReader;
4 import java.io.IOException;
6 import java.nio.charset.Charset;
7 import java.util.ArrayList;
8 import java.util.Collections;
11 import net.sf.openrocket.motor.Manufacturer;
12 import net.sf.openrocket.motor.Motor;
13 import net.sf.openrocket.motor.ThrustCurveMotor;
14 import net.sf.openrocket.util.Coordinate;
16 public class RASPMotorLoader extends MotorLoader {
18 public static final String CHARSET_NAME = "ISO-8859-1";
20 public static final Charset CHARSET = Charset.forName(CHARSET_NAME);
26 protected Charset getDefaultCharset() {
32 * Load a <code>Motor</code> from a RASP file specified by the <code>Reader</code>.
33 * The <code>Reader</code> is responsible for using the correct charset.
35 * The CG is assumed to be located at the center of the motor casing and the mass
36 * is calculated from the thrust curve by assuming a constant exhaust velocity.
38 * @param reader the source of the file.
39 * @return a list of the {@link Motor} objects defined in the file.
40 * @throws IOException if an I/O error occurs or if the file format is illegal.
43 public List<Motor> load(Reader reader, String filename) throws IOException {
44 List<Motor> motors = new ArrayList<Motor>();
45 BufferedReader in = new BufferedReader(reader);
47 String manufacturer = "";
48 String designation = "";
53 ArrayList<Double> delays = null;
55 List<Double> time = new ArrayList<Double>();
56 List<Double> thrust = new ArrayList<Double>();
66 main: while (line != null) { // Until EOF
73 delays = new ArrayList<Double>();
80 while (line.length()==0 || line.charAt(0)==';') {
81 if (line.length() > 0) {
82 comment += line.substring(1).trim() + "\n";
88 comment = comment.trim();
90 // Parse header line, example:
91 // F32 24 124 5-10-15-P .0377 .0695 RV
92 // desig diam len delays prop.w tot.w manufacturer
94 if (pieces.length != 7) {
95 throw new IOException("Illegal file format.");
98 designation = pieces[0];
99 diameter = Double.parseDouble(pieces[1]) / 1000.0;
100 length = Double.parseDouble(pieces[2]) / 1000.0;
102 if (pieces[3].equalsIgnoreCase("None")) {
105 buf = split(pieces[3],"[-,]+");
106 for (int i=0; i < buf.length; i++) {
107 if (buf[i].equalsIgnoreCase("P") ||
108 buf[i].equalsIgnoreCase("plugged")) {
109 delays.add(Motor.PLUGGED);
111 // Many RASP files have "100" as an only delay
112 double d = Double.parseDouble(buf[i]);
117 Collections.sort(delays);
120 propW = Double.parseDouble(pieces[4]);
121 totalW = Double.parseDouble(pieces[5]);
122 manufacturer = pieces[6];
124 if (propW > totalW) {
125 throw new IOException("Propellant weight exceeds total weight in " +
130 for (line = in.readLine();
131 (line != null) && (line.length()==0 || line.charAt(0) != ';');
132 line = in.readLine()) {
135 if (buf.length == 0) {
137 } else if (buf.length == 2) {
139 time.add(Double.parseDouble(buf[0]));
140 thrust .add(Double.parseDouble(buf[1]));
143 throw new IOException("Illegal file format.");
147 // Comment of EOF encountered, marks the start of the next motor
148 if (time.size() < 2) {
149 throw new IOException("Illegal file format, too short thrust-curve.");
151 double[] delayArray = new double[delays.size()];
152 for (int i=0; i<delays.size(); i++) {
153 delayArray[i] = delays.get(i);
155 motors.add(createRASPMotor(manufacturer, designation, comment,
156 length, diameter, delayArray, propW, totalW, time, thrust));
159 } catch (NumberFormatException e) {
161 throw new IOException("Illegal file format.");
174 * Create a motor from RASP file data.
175 * @throws IOException if the data is illegal for a thrust curve
177 private static Motor createRASPMotor(String manufacturer, String designation,
178 String comment, double length, double diameter, double[] delays,
179 double propW, double totalW, List<Double> time, List<Double> thrust)
182 // Add zero time/thrust if necessary
183 sortLists(time, thrust);
184 finalizeThrustCurve(time, thrust);
185 List<Double> mass = calculateMass(time,thrust,totalW,propW);
187 double[] timeArray = new double[time.size()];
188 double[] thrustArray = new double[time.size()];
189 Coordinate[] cgArray = new Coordinate[time.size()];
190 for (int i=0; i < time.size(); i++) {
191 timeArray[i] = time.get(i);
192 thrustArray[i] = thrust.get(i);
193 cgArray[i] = new Coordinate(length/2,0,0,mass.get(i));
196 designation = removeDelay(designation);
200 return new ThrustCurveMotor(Manufacturer.getManufacturer(manufacturer),
201 designation, comment, Motor.Type.UNKNOWN,
202 delays, diameter, length, timeArray, thrustArray, cgArray);
204 } catch (IllegalArgumentException e) {
206 // Bad data read from file.
207 throw new IOException("Illegal file format.", e);