1 package net.sf.openrocket.file.motor;
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.file.MotorLoader;
12 import net.sf.openrocket.motor.Manufacturer;
13 import net.sf.openrocket.motor.Motor;
14 import net.sf.openrocket.motor.MotorDigest;
15 import net.sf.openrocket.motor.ThrustCurveMotor;
16 import net.sf.openrocket.motor.MotorDigest.DataType;
17 import net.sf.openrocket.util.Coordinate;
19 public class RASPMotorLoader extends MotorLoader {
21 public static final String CHARSET_NAME = "ISO-8859-1";
23 public static final Charset CHARSET = Charset.forName(CHARSET_NAME);
29 protected Charset getDefaultCharset() {
35 * Load a <code>Motor</code> from a RASP file specified by the <code>Reader</code>.
36 * The <code>Reader</code> is responsible for using the correct charset.
38 * The CG is assumed to be located at the center of the motor casing and the mass
39 * is calculated from the thrust curve by assuming a constant exhaust velocity.
41 * @param reader the source of the file.
42 * @return a list of the {@link Motor} objects defined in the file.
43 * @throws IOException if an I/O error occurs or if the file format is illegal.
46 public List<Motor> load(Reader reader, String filename) throws IOException {
47 List<Motor> motors = new ArrayList<Motor>();
48 BufferedReader in = new BufferedReader(reader);
50 String manufacturer = "";
51 String designation = "";
56 ArrayList<Double> delays = null;
58 List<Double> time = new ArrayList<Double>();
59 List<Double> thrust = new ArrayList<Double>();
69 main: while (line != null) { // Until EOF
76 delays = new ArrayList<Double>();
83 while (line.length()==0 || line.charAt(0)==';') {
84 if (line.length() > 0) {
85 comment += line.substring(1).trim() + "\n";
91 comment = comment.trim();
93 // Parse header line, example:
94 // F32 24 124 5-10-15-P .0377 .0695 RV
95 // desig diam len delays prop.w tot.w manufacturer
97 if (pieces.length != 7) {
98 throw new IOException("Illegal file format.");
101 designation = pieces[0];
102 diameter = Double.parseDouble(pieces[1]) / 1000.0;
103 length = Double.parseDouble(pieces[2]) / 1000.0;
105 if (pieces[3].equalsIgnoreCase("None")) {
108 buf = split(pieces[3],"[-,]+");
109 for (int i=0; i < buf.length; i++) {
110 if (buf[i].equalsIgnoreCase("P") ||
111 buf[i].equalsIgnoreCase("plugged")) {
112 delays.add(Motor.PLUGGED);
114 // Many RASP files have "100" as an only delay
115 double d = Double.parseDouble(buf[i]);
120 Collections.sort(delays);
123 propW = Double.parseDouble(pieces[4]);
124 totalW = Double.parseDouble(pieces[5]);
125 manufacturer = pieces[6];
127 if (propW > totalW) {
128 throw new IOException("Propellant weight exceeds total weight in " +
133 for (line = in.readLine();
134 (line != null) && (line.length()==0 || line.charAt(0) != ';');
135 line = in.readLine()) {
138 if (buf.length == 0) {
140 } else if (buf.length == 2) {
142 time.add(Double.parseDouble(buf[0]));
143 thrust .add(Double.parseDouble(buf[1]));
146 throw new IOException("Illegal file format.");
150 // Comment of EOF encountered, marks the start of the next motor
151 if (time.size() < 2) {
152 throw new IOException("Illegal file format, too short thrust-curve.");
154 double[] delayArray = new double[delays.size()];
155 for (int i=0; i<delays.size(); i++) {
156 delayArray[i] = delays.get(i);
158 motors.add(createRASPMotor(manufacturer, designation, comment,
159 length, diameter, delayArray, propW, totalW, time, thrust));
162 } catch (NumberFormatException e) {
164 throw new IOException("Illegal file format.");
177 * Create a motor from RASP file data.
178 * @throws IOException if the data is illegal for a thrust curve
180 private static Motor createRASPMotor(String manufacturer, String designation,
181 String comment, double length, double diameter, double[] delays,
182 double propW, double totalW, List<Double> time, List<Double> thrust)
185 // Add zero time/thrust if necessary
186 sortLists(time, thrust);
187 finalizeThrustCurve(time, thrust);
188 List<Double> mass = calculateMass(time,thrust,totalW,propW);
190 double[] timeArray = new double[time.size()];
191 double[] thrustArray = new double[time.size()];
192 Coordinate[] cgArray = new Coordinate[time.size()];
193 for (int i=0; i < time.size(); i++) {
194 timeArray[i] = time.get(i);
195 thrustArray[i] = thrust.get(i);
196 cgArray[i] = new Coordinate(length/2,0,0,mass.get(i));
199 designation = removeDelay(designation);
201 // Create the motor digest from data available in RASP files
202 MotorDigest motorDigest = new MotorDigest();
203 motorDigest.update(DataType.TIME_ARRAY, timeArray);
204 motorDigest.update(DataType.MASS_SPECIFIC, totalW, totalW-propW);
205 motorDigest.update(DataType.FORCE_PER_TIME, thrustArray);
206 final String digest = motorDigest.getDigest();
211 return new ThrustCurveMotor(Manufacturer.getManufacturer(manufacturer),
212 designation, comment, Motor.Type.UNKNOWN,
213 delays, diameter, length, timeArray, thrustArray, cgArray, digest);
215 } catch (IllegalArgumentException e) {
217 // Bad data read from file.
218 throw new IOException("Illegal file format.", e);