Merge commit '46077ef99f953486550547c15bd60dd02bab9241' into upstream
[debian/openrocket] / core / src / net / sf / openrocket / utils / MotorCheck.java
1 package net.sf.openrocket.utils;
2
3 import java.io.FileInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.List;
7
8 import net.sf.openrocket.file.motor.GeneralMotorLoader;
9 import net.sf.openrocket.file.motor.MotorLoader;
10 import net.sf.openrocket.motor.Manufacturer;
11 import net.sf.openrocket.motor.Motor;
12 import net.sf.openrocket.motor.ThrustCurveMotor;
13
14 public class MotorCheck {
15         
16         // Warn if less that this many points
17         public static final int WARN_POINTS = 6;
18         
19         
20         public static void main(String[] args) {
21                 MotorLoader loader = new GeneralMotorLoader();
22                 
23                 // Load files
24                 for (String file : args) {
25                         System.out.print("Checking " + file + "... ");
26                         System.out.flush();
27                         
28                         boolean ok = true;
29                         
30                         List<Motor> motors = null;
31                         try {
32                                 InputStream stream = new FileInputStream(file);
33                                 motors = loader.load(stream, file);
34                                 stream.close();
35                         } catch (IOException e) {
36                                 System.out.println("ERROR: " + e.getMessage());
37                                 e.printStackTrace(System.out);
38                                 ok = false;
39                         }
40                         
41                         String base = file.split("_")[0];
42                         Manufacturer mfg = Manufacturer.getManufacturer(base);
43                         
44                         if (motors != null) {
45                                 if (motors.size() == 0) {
46                                         System.out.println("ERROR: File contained no motors");
47                                         ok = false;
48                                 } else {
49                                         for (Motor motor : motors) {
50                                                 ThrustCurveMotor m = (ThrustCurveMotor) motor;
51                                                 double sum = 0;
52                                                 sum += m.getAverageThrustEstimate();
53                                                 sum += m.getBurnTimeEstimate();
54                                                 sum += m.getTotalImpulseEstimate();
55                                                 //                                              sum += m.getTotalTime();
56                                                 sum += m.getDiameter();
57                                                 sum += m.getLength();
58                                                 sum += m.getEmptyCG().weight;
59                                                 sum += m.getEmptyCG().x;
60                                                 sum += m.getLaunchCG().weight;
61                                                 sum += m.getLaunchCG().x;
62                                                 sum += m.getMaxThrustEstimate();
63                                                 if (Double.isInfinite(sum) || Double.isNaN(sum)) {
64                                                         System.out.println("ERROR: Invalid motor values");
65                                                         ok = false;
66                                                 }
67                                                 
68                                                 if (m.getManufacturer() != mfg) {
69                                                         System.out.println("ERROR: Inconsistent manufacturer " +
70                                                                         m.getManufacturer() + " (file name indicates " + mfg
71                                                                         + ")");
72                                                         ok = false;
73                                                 }
74                                                 
75                                                 int points = ((ThrustCurveMotor) m).getTimePoints().length;
76                                                 if (points < WARN_POINTS) {
77                                                         System.out.println("WARNING: Only " + points + " data points");
78                                                         ok = false;
79                                                 }
80                                         }
81                                 }
82                         }
83                         
84                         if (ok) {
85                                 System.out.println("OK");
86                         }
87                 }
88         }
89 }