public void measureROIs() {
   targets = new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0};
   // System.out.println("acquireImageAndMeasure");
   byte[] data = acqCtrl.acquireSampleImage();
   // measure intensity at ROIs
   ImageStatistics[] iStats =
       measureROIsIn(data, acqCtrl.getImageWidth(), acqCtrl.getImageHeight(), rois);
   for (int i = 0; i < numIDs; i++) {
     measurements[i] = Math.abs(iStats[i].meanInROI - slmModel.getZeroIntensity() - targets[i]);
   }
   IJ.log("\nMeasured ROIs:");
   for (int i = 0; i < numIDs; i++) {
     IJ.log(
         "   sector "
             + i
             + ": " // + settings[i]
             + " = "
             + measurements[i]);
   }
 }
    public void run() {
      try {
        System.out.println("acquireImageAndMeasure");
        for (int i = 0; i < numIDs; i++) {
          // for testing using MockRetarders
          switch (layer) {
            case retarderA:
              slmCtrl.setRetA(i, (int) settings[i]);
              break;
            case retarderB:
              slmCtrl.setRetB(i, (int) settings[i]);
              break;
            case elliptical:
              slmCtrl.setElliptical(i, (int) settings[i]);
              break;
            case circular:
              slmCtrl.setCircular(i, (int) settings[i]);
              break;
          }
        }

        if (!SLMModel.useMockRetarders) {
          Thread.currentThread().sleep(slmModel.getSettlingtime());
          // byte[] data = acqCtrl.acquireSampleImage();
          acqCtrl.acquireImage(data);
          // test
          //                    BufferedImage img =
          // ImageFactoryGrayScale.createImage(acqCtrl.getImageWidth(), acqCtrl.getImageHeight(), 8,
          // (byte[]) data);
          //                    edu.mbl.jif.gui.imaging.FrameImageDisplay id = new
          // edu.mbl.jif.gui.imaging.FrameImageDisplay(img, "Sample");
          //                    id.setVisible(true);

          // measure intensity at ROIs
          ImageStatistics[] iStats =
              measureROIsIn(data, acqCtrl.getImageWidth(), acqCtrl.getImageHeight(), rois);
          iteration++;
          for (int i = 0; i < numIDs; i++) {
            measurements[i] =
                Math.abs(iStats[i].meanInROI - slmModel.getZeroIntensity() - targets[i]);
            if (mPlot != null) {
              mPlot.recordData(i, iteration, measurements[i]);
            }
          }
        } else { // useMockRetarders
          for (int i = 0; i < numIDs; i++) {
            // for testing using MockRetarders
            switch (layer) {
              case retarderA:
                measurements[i] = mockRet[i].calcA(settings[i]);
                break;
              case retarderB:
                measurements[i] = mockRet[i].calcB(settings[i]);
                break;
              case elliptical:
                measurements[i] = mockRet[i].calc(settings[i]);
                break;
              case circular:
                measurements[i] = mockRet[i].calc(settings[i]);
                break;
            }
            if (mPlot != null) {
              mPlot.recordData(i, iteration, measurements[i]);
            }
          }
        }
      } catch (InterruptedException ex) {
        Logger.getLogger(SLM_Calibrator.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    private void normalize(DataSet[] dataSets) {
      double[] max = new double[numIDs];

      // find max
      for (int i = 0; i < numIDs; i++) {
        for (int j = 0; j < SLMModel.numPoints; j++) {
          if (max[i] < dataSets[i].getMeasurement(j)) {
            max[i] = dataSets[i].getMeasurement(j);
          }
        }
      }
      // normalize
      for (int i = 0; i < numIDs; i++) {
        for (int j = 0; j < SLMModel.numPoints; j++) {
          dataSets[i].measurement[j] = dataSets[i].measurement[j] / max[i];
        }
      }
      // dumpDataSet();

      // Plot & record Normalized Intensity
      for (int i = 0; i < numIDs; i++) {
        System.out.println("\n" + ">>>>>>> ID: " + i);
        for (int j = 0; j < SLMModel.numPoints; j++) {
          System.out.println(dataSets[i].setting[j] + " => " + dataSets[i].measurement[j]);
          double s = dataSets[i].getSetting(j);
          double m = dataSets[i].getMeasurement(j);
          mNPlot.recordData(i, j, s, m);
        }
      }
      mNPlot.closeTextFile();
      writeOutMatrix(dataSets, "Normalized");

      // Plot & record RetardanceCurve
      for (int i = 0; i < numIDs; i++) {
        System.out.println("\n" + ">>>>>>> ID: " + i);
        double lastValue = 0;
        boolean inSection1 = true;
        boolean inSection2 = false;
        boolean inSection3 = false;
        for (int j = 0; j < SLMModel.numPoints; j++) {
          System.out.println(dataSets[i].setting[j] + " => " + dataSets[i].measurement[j]);
          double s = dataSets[i].getSetting(j);
          double m = dataSets[i].getMeasurement(j);
          if (inSection1 && (lastValue > m)) {
            inSection1 = false;
            inSection2 = true;
          }
          if (inSection2 && (lastValue < m)) {
            inSection2 = false;
            inSection3 = true;
          }
          lastValue = m;
          double ret = 0;
          if (inSection1) {
            ret = 3.0 * Math.PI - 2 * Math.asin(Math.sqrt(m));
          }
          if (inSection2) {
            ret = Math.PI + 2 * Math.asin(Math.sqrt(m));
          }
          if (inSection3) {
            ret = Math.PI - 2 * Math.asin(Math.sqrt(m));
          }

          mNPlot.recordData(i, j, s, ret);

          dataSets[i].measurement[j] = ret;
        }
      }
      mNPlot.closeTextFile();

      writeOutMatrix(dataSets, "Retardance");
    }