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