/**
  * Add a measurement set to this Sats store
  *
  * @param id
  * @param m
  * @return
  * @throws IOException
  */
 public boolean add(int id, Measurement m) throws IOException {
   if (m instanceof RtMeasurement) {
     try {
       save(m, rtFileName);
     } catch (IOException e) {
       // NEED TO SET A FLAG HERE THAT IS THEN SEEN BY THE GUI WHEN IT POLLS FOR RESULTS
       e.printStackTrace(Log.getWriter());
     }
     rtRecords.add(m);
     updatedRt = true;
     return true;
   } else if (m instanceof PassMeasurement) {
     try {
       save(m, passFileName);
     } catch (IOException e) {
       // NEED TO SET A FLAG HERE THAT IS THEN SEEN BY THE GUI WHEN IT POLLS FOR RESULTS
       e.printStackTrace(Log.getWriter());
     }
     passRecords.add(m);
     updatedPass = true;
     return true;
   }
   return false;
 }
  /**
   * Load a file from disk
   *
   * @param log
   * @throws FileNotFoundException
   */
  public void load(String log) throws FileNotFoundException {
    String line;
    if (!Config.logFileDirectory.equalsIgnoreCase("")) {
      log = Config.logFileDirectory + File.separator + log;
      Log.println("Loading: " + log);
    }
    File aFile = new File(log);
    if (!aFile.exists()) {
      try {
        aFile.createNewFile();
      } catch (IOException e) {
        JOptionPane.showMessageDialog(
            MainWindow.frame,
            e.toString(),
            "ERROR creating file " + log,
            JOptionPane.ERROR_MESSAGE);
        e.printStackTrace(Log.getWriter());
      }
    }

    BufferedReader dis = new BufferedReader(new FileReader(log));

    try {
      while ((line = dis.readLine()) != null) {
        if (line != null) {
          StringTokenizer st = new StringTokenizer(line, ",");
          String date = st.nextToken();
          int id = Integer.valueOf(st.nextToken()).intValue();
          int reset = Integer.valueOf(st.nextToken()).intValue();
          long uptime = Long.valueOf(st.nextToken()).longValue();
          int type = Integer.valueOf(st.nextToken()).intValue();

          // We should never get this situation, but good to check..
          if (Config.satManager.getSpacecraft(id) == null) {
            Log.errorDialog(
                "FATAL",
                "Attempting to Load payloads from the Payload store for satellite with Fox Id: "
                    + id
                    + "\n when no sattellite with that FoxId is configured.  Add this spacecraft to the satellite directory and restart FoxTelem."
                    + "\nProgram will now exit");
            System.exit(1);
          }
          if (type == RT_MEASUREMENT_TYPE) {
            RtMeasurement rt = new RtMeasurement(id, date, reset, uptime, type, st);
            rtRecords.add(rt);
            updatedRt = true;
          }
          if (type == PASS_MEASUREMENT_TYPE) {
            PassMeasurement rt = new PassMeasurement(id, date, reset, uptime, type, st);
            passRecords.add(rt);
            updatedPass = true;
          }
        }
      }
    } catch (IOException e) {
      e.printStackTrace(Log.getWriter());

    } catch (NumberFormatException n) {
      n.printStackTrace(Log.getWriter());
    } finally {
      try {
        dis.close();
      } catch (IOException e) {
        e.printStackTrace(Log.getWriter());
      }
    }
  }
  public double[][] getMeasurementGraphData(
      String name, int period, Spacecraft fox, int fromReset, long fromUptime) {

    int start = 0;
    int end = 0;

    if (fromReset == 0.0 && fromUptime == 0.0) { // then we take rtRecords nearest the end
      start = rtRecords.size() - period;
      end = rtRecords.size();
    } else {
      // we need to find the start point
      start = rtRecords.getNearestFrameIndex(fox.foxId, fromUptime, fromReset);
      if (start == -1) start = rtRecords.size() - period;
      end = start + period;
    }
    if (end > rtRecords.size()) end = rtRecords.size();
    if (end < start) end = start;
    if (start < 0) start = 0;
    if (start > rtRecords.size()) start = rtRecords.size();
    double[] results = new double[end - start];
    double[] upTime = new double[end - start];
    double[] resets = new double[end - start];
    double[] dates = new double[end - start];

    int j = results.length - 1;
    for (int i = end - 1; i >= start; i--) {
      // System.out.println(rtrtRecords.size());
      results[j] = ((RtMeasurement) rtRecords.get(i)).getRawValue(name);
      upTime[j] = rtRecords.get(i).uptime;
      resets[j] = rtRecords.get(i).reset;
      dates[j--] = rtRecords.get(i).date.getTime();
    }

    double[][] resultSet = new double[4][end - start];
    resultSet[PayloadStore.DATA_COL] = results;
    resultSet[PayloadStore.UPTIME_COL] = upTime;
    resultSet[PayloadStore.RESETS_COL] = resets;
    resultSet[PayloadStore.UTC_COL] = dates;
    return resultSet;
  }
 public PassMeasurement getLatestPassMeasurement() {
   if (passRecords.size() == 0) return null;
   return (PassMeasurement) passRecords.get(passRecords.size() - 1);
 }
 public RtMeasurement getLatestMeasurement() {
   if (rtRecords.size() == 0) return null;
   return (RtMeasurement) rtRecords.get(rtRecords.size() - 1);
 }