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.MotorDigest;
14 import net.sf.openrocket.motor.ThrustCurveMotor;
15 import net.sf.openrocket.motor.MotorDigest.DataType;
16 import net.sf.openrocket.util.Coordinate;
18 public class RASPMotorLoader extends MotorLoader {
20 public static final String CHARSET_NAME = "ISO-8859-1";
22 public static final Charset CHARSET = Charset.forName(CHARSET_NAME);
28 protected Charset getDefaultCharset() {
34 * Load a <code>Motor</code> from a RASP file specified by the <code>Reader</code>.
35 * The <code>Reader</code> is responsible for using the correct charset.
37 * The CG is assumed to be located at the center of the motor casing and the mass
38 * is calculated from the thrust curve by assuming a constant exhaust velocity.
40 * @param reader the source of the file.
41 * @return a list of the {@link Motor} objects defined in the file.
42 * @throws IOException if an I/O error occurs or if the file format is illegal.
45 public List<Motor> load(Reader reader, String filename) throws IOException {
46 List<Motor> motors = new ArrayList<Motor>();
47 BufferedReader in = new BufferedReader(reader);
49 String manufacturer = "";
50 String designation = "";
55 ArrayList<Double> delays = null;
57 List<Double> time = new ArrayList<Double>();
58 List<Double> thrust = new ArrayList<Double>();
68 main: while (line != null) { // Until EOF
75 delays = new ArrayList<Double>();
82 while (line.length()==0 || line.charAt(0)==';') {
83 if (line.length() > 0) {
84 comment += line.substring(1).trim() + "\n";
90 comment = comment.trim();
92 // Parse header line, example:
93 // F32 24 124 5-10-15-P .0377 .0695 RV
94 // desig diam len delays prop.w tot.w manufacturer
96 if (pieces.length != 7) {
97 throw new IOException("Illegal file format.");
100 designation = pieces[0];
101 diameter = Double.parseDouble(pieces[1]) / 1000.0;
102 length = Double.parseDouble(pieces[2]) / 1000.0;
104 if (pieces[3].equalsIgnoreCase("None")) {
107 buf = split(pieces[3],"[-,]+");
108 for (int i=0; i < buf.length; i++) {
109 if (buf[i].equalsIgnoreCase("P") ||
110 buf[i].equalsIgnoreCase("plugged")) {
111 delays.add(Motor.PLUGGED);
113 // Many RASP files have "100" as an only delay
114 double d = Double.parseDouble(buf[i]);
119 Collections.sort(delays);
122 propW = Double.parseDouble(pieces[4]);
123 totalW = Double.parseDouble(pieces[5]);
124 manufacturer = pieces[6];
126 if (propW > totalW) {
127 throw new IOException("Propellant weight exceeds total weight in " +
132 for (line = in.readLine();
133 (line != null) && (line.length()==0 || line.charAt(0) != ';');
134 line = in.readLine()) {
137 if (buf.length == 0) {
139 } else if (buf.length == 2) {
141 time.add(Double.parseDouble(buf[0]));
142 thrust .add(Double.parseDouble(buf[1]));
145 throw new IOException("Illegal file format.");
149 // Comment of EOF encountered, marks the start of the next motor
150 if (time.size() < 2) {
151 throw new IOException("Illegal file format, too short thrust-curve.");
153 double[] delayArray = new double[delays.size()];
154 for (int i=0; i<delays.size(); i++) {
155 delayArray[i] = delays.get(i);
157 motors.add(createRASPMotor(manufacturer, designation, comment,
158 length, diameter, delayArray, propW, totalW, time, thrust));
161 } catch (NumberFormatException e) {
163 throw new IOException("Illegal file format.");
176 * Create a motor from RASP file data.
177 * @throws IOException if the data is illegal for a thrust curve
179 private static Motor createRASPMotor(String manufacturer, String designation,
180 String comment, double length, double diameter, double[] delays,
181 double propW, double totalW, List<Double> time, List<Double> thrust)
184 // Add zero time/thrust if necessary
185 sortLists(time, thrust);
186 finalizeThrustCurve(time, thrust);
187 List<Double> mass = calculateMass(time,thrust,totalW,propW);
189 double[] timeArray = new double[time.size()];
190 double[] thrustArray = new double[time.size()];
191 Coordinate[] cgArray = new Coordinate[time.size()];
192 for (int i=0; i < time.size(); i++) {
193 timeArray[i] = time.get(i);
194 thrustArray[i] = thrust.get(i);
195 cgArray[i] = new Coordinate(length/2,0,0,mass.get(i));
198 designation = removeDelay(designation);
200 // Create the motor digest from data available in RASP files
201 MotorDigest motorDigest = new MotorDigest();
202 motorDigest.update(DataType.TIME_ARRAY, timeArray);
203 motorDigest.update(DataType.MASS_SPECIFIC, totalW, totalW-propW);
204 motorDigest.update(DataType.FORCE_PER_TIME, thrustArray);
205 final String digest = motorDigest.getDigest();
210 return new ThrustCurveMotor(Manufacturer.getManufacturer(manufacturer),
211 designation, comment, Motor.Type.UNKNOWN,
212 delays, diameter, length, timeArray, thrustArray, cgArray, digest);
214 } catch (IllegalArgumentException e) {
216 // Bad data read from file.
217 throw new IOException("Illegal file format.", e);