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.rocketcomponent.Motor;
12 import net.sf.openrocket.rocketcomponent.ThrustCurveMotor;
13 import net.sf.openrocket.util.Coordinate;
15 public class RASPMotorLoader extends MotorLoader {
17 public static final String CHARSET_NAME = "ISO-8859-1";
19 public static final Charset CHARSET = Charset.forName(CHARSET_NAME);
25 protected Charset getDefaultCharset() {
31 * Load a <code>Motor</code> from a RASP file specified by the <code>Reader</code>.
32 * The <code>Reader</code> is responsible for using the correct charset.
34 * The CG is assumed to be located at the center of the motor casing and the mass
35 * is calculated from the thrust curve by assuming a constant exhaust velocity.
37 * @param reader the source of the file.
38 * @return a list of the {@link Motor} objects defined in the file.
39 * @throws IOException if an I/O error occurs or if the file format is illegal.
42 public List<Motor> load(Reader reader, String filename) throws IOException {
43 List<Motor> motors = new ArrayList<Motor>();
44 BufferedReader in = new BufferedReader(reader);
46 String manufacturer = "";
47 String designation = "";
52 ArrayList<Double> delays = null;
54 List<Double> time = new ArrayList<Double>();
55 List<Double> thrust = new ArrayList<Double>();
65 main: while (line != null) { // Until EOF
72 delays = new ArrayList<Double>();
79 while (line.length()==0 || line.charAt(0)==';') {
80 if (line.length() > 0) {
81 comment += line.substring(1).trim() + "\n";
87 comment = comment.trim();
89 // Parse header line, example:
90 // F32 24 124 5-10-15-P .0377 .0695 RV
91 // desig diam len delays prop.w tot.w manufacturer
93 if (pieces.length != 7) {
94 throw new IOException("Illegal file format.");
97 designation = pieces[0];
98 diameter = Double.parseDouble(pieces[1]) / 1000.0;
99 length = Double.parseDouble(pieces[2]) / 1000.0;
101 if (pieces[3].equalsIgnoreCase("None")) {
104 buf = split(pieces[3],"[-,]+");
105 for (int i=0; i < buf.length; i++) {
106 if (buf[i].equalsIgnoreCase("P") ||
107 buf[i].equalsIgnoreCase("plugged")) {
108 delays.add(Motor.PLUGGED);
110 // Many RASP files have "100" as an only delay
111 double d = Double.parseDouble(buf[i]);
116 Collections.sort(delays);
119 propW = Double.parseDouble(pieces[4]);
120 totalW = Double.parseDouble(pieces[5]);
121 manufacturer = convertManufacturer(pieces[6]);
123 if (propW > totalW) {
124 throw new IOException("Propellant weight exceeds total weight in " +
129 for (line = in.readLine();
130 (line != null) && (line.length()==0 || line.charAt(0) != ';');
131 line = in.readLine()) {
134 if (buf.length == 0) {
136 } else if (buf.length == 2) {
138 time.add(Double.parseDouble(buf[0]));
139 thrust .add(Double.parseDouble(buf[1]));
142 throw new IOException("Illegal file format.");
146 // Comment of EOF encountered, marks the start of the next motor
147 if (time.size() < 2) {
148 throw new IOException("Illegal file format, too short thrust-curve.");
150 double[] delayArray = new double[delays.size()];
151 for (int i=0; i<delays.size(); i++) {
152 delayArray[i] = delays.get(i);
154 motors.add(createRASPMotor(manufacturer, designation, comment,
155 length, diameter, delayArray, propW, totalW, time, thrust));
158 } catch (NumberFormatException e) {
160 throw new IOException("Illegal file format.");
173 * Create a motor from RASP file data.
174 * @throws IOException if the data is illegal for a thrust curve
176 private static Motor createRASPMotor(String manufacturer, String designation,
177 String comment, double length, double diameter, double[] delays,
178 double propW, double totalW, List<Double> time, List<Double> thrust)
181 // Add zero time/thrust if necessary
182 sortLists(time, thrust);
183 finalizeThrustCurve(time, thrust);
184 List<Double> mass = calculateMass(time,thrust,totalW,propW);
186 double[] timeArray = new double[time.size()];
187 double[] thrustArray = new double[time.size()];
188 Coordinate[] cgArray = new Coordinate[time.size()];
189 for (int i=0; i < time.size(); i++) {
190 timeArray[i] = time.get(i);
191 thrustArray[i] = thrust.get(i);
192 cgArray[i] = new Coordinate(length/2,0,0,mass.get(i));
195 designation = removeDelay(designation);
199 return new ThrustCurveMotor(manufacturer, designation, comment, Motor.Type.UNKNOWN,
200 delays, diameter, length, timeArray, thrustArray, cgArray);
202 } catch (IllegalArgumentException e) {
204 // Bad data read from file.
205 throw new IOException("Illegal file format.", e);