private void getintbright() {
   weights = new float[ncurves][xpts][ypts];
   for (int i = 0; i < ncurves; i++) {
     nmeas[i] = 0;
     for (int j = 0; j < xpts; j++) {
       for (int k = 0; k < ypts; k++) {
         nmeas[i] += (int) pch[i][j][k];
       }
     }
     double tempavg = 0.0;
     double tempavg2 = 0.0;
     double temp2avg = 0.0;
     double temp2avg2 = 0.0;
     double tempccavg = 0.0;
     for (int j = 0; j < xpts; j++) {
       for (int k = 0; k < ypts; k++) {
         double normed = (double) pch[i][j][k] / (double) nmeas[i];
         if (pch[i][j][k] > 0.0f) {
           weights[i][j][k] = (float) ((double) nmeas[i] / (normed * (1.0f - normed)));
         } else {
           weights[i][j][k] = 1.0f;
         }
         tempavg += normed * (double) j;
         tempavg2 += normed * (double) j * (double) j;
         temp2avg += normed * (double) k;
         temp2avg2 += normed * (double) k * (double) k;
         tempccavg += normed * (double) k * (double) j;
       }
     }
     tempccavg -= tempavg * temp2avg;
     brightcc[i] = tempccavg / Math.sqrt(tempavg * temp2avg);
     tempavg2 -= tempavg * tempavg;
     tempavg2 /= tempavg;
     bright1[i] = (tempavg2 - 1.0);
     temp2avg2 -= temp2avg * temp2avg;
     temp2avg2 /= temp2avg;
     bright2[i] = (temp2avg2 - 1.0);
     intensity1[i] = tempavg;
     intensity2[i] = temp2avg;
     if (psfflag == 0) {
       bright1[i] /= 0.3536;
       bright2[i] /= 0.3536;
       brightcc[i] /= 0.3536;
     } else {
       if (psfflag == 1) {
         bright1[i] /= 0.078;
         bright2[i] /= 0.078;
         brightcc[i] /= 0.078;
       } else {
         bright1[i] /= 0.5;
         bright2[i] /= 0.5;
         brightcc[i] /= 0.5;
       }
     }
     number1[i] = intensity1[i] / bright1[i];
     number2[i] = intensity2[i] / bright2[i];
     brightmincc[i] = (bright1[i] * beta) * Math.sqrt(intensity1[i] / intensity2[i]);
   }
 }
 public void updatebeta() {
   for (int i = 0; i <= ncurves; i++) {
     brightmincc[i] = (bright1[i] * beta) / Math.sqrt(intensity1[i] / intensity2[i]);
     eminccarray[i].setText("" + (float) brightmincc[i]);
   }
 }
 private void updateavg() {
   nmeas[ncurves] = 0;
   avg = new float[xpts][ypts];
   avgweights = new float[xpts][ypts];
   for (int i = 0; i < ncurves; i++) {
     if (include[i]) {
       for (int j = 0; j < xpts; j++) {
         for (int k = 0; k < ypts; k++) {
           avg[j][k] += pch[i][j][k];
           nmeas[ncurves] += (int) pch[i][j][k];
         }
       }
     }
   }
   double tempavg = 0.0;
   double tempavg2 = 0.0;
   double temp2avg = 0.0;
   double temp2avg2 = 0.0;
   double tempccavg = 0.0;
   for (int i = 0; i < xpts; i++) {
     for (int j = 0; j < ypts; j++) {
       double normed = (double) avg[i][j] / (double) nmeas[ncurves];
       avgweights[i][j] = (float) ((double) nmeas[ncurves] / (normed * (1.0f - normed)));
       if (avg[i][j] > 0.0f) {
         avgweights[i][j] = (float) ((double) nmeas[ncurves] / (normed * (1.0f - normed)));
       } else {
         avgweights[i][j] = 1.0f;
       }
       tempavg += (double) i * normed;
       tempavg2 += (double) i * (double) i * normed;
       temp2avg += (double) j * normed;
       temp2avg2 += (double) j * (double) j * normed;
       tempccavg += (double) i * (double) j * normed;
     }
   }
   tempccavg -= tempavg * temp2avg;
   brightcc[ncurves] = tempccavg / Math.sqrt(tempavg * temp2avg);
   tempavg2 -= tempavg * tempavg;
   tempavg2 /= tempavg;
   bright1[ncurves] = (tempavg2 - 1.0);
   temp2avg2 -= temp2avg * temp2avg;
   temp2avg2 /= temp2avg;
   bright2[ncurves] = (temp2avg2 - 1.0);
   intensity1[ncurves] = tempavg;
   intensity2[ncurves] = temp2avg;
   if (psfflag == 0) {
     bright1[ncurves] /= 0.3536;
     bright2[ncurves] /= 0.3536;
     brightcc[ncurves] /= 0.3536;
   } else {
     if (psfflag == 1) {
       bright1[ncurves] /= 0.078;
       bright2[ncurves] /= 0.078;
       brightcc[ncurves] /= 0.078;
     } else {
       bright1[ncurves] /= 0.5;
       bright2[ncurves] /= 0.5;
       brightcc[ncurves] /= 0.5;
     }
   }
   number1[ncurves] = intensity1[ncurves] / bright1[ncurves];
   number2[ncurves] = intensity2[ncurves] / bright2[ncurves];
   brightmincc[ncurves] =
       (bright1[ncurves] * beta) * Math.sqrt(intensity1[ncurves] / intensity2[ncurves]);
 }
  private void geterrors() {
    GenericDialog gd = new GenericDialog("Options");
    float conf = 0.67f;
    gd.addNumericField("Confidence Limit", (int) (conf * 100.0f), 5, 10, null);
    gd.addChoice("Error Parameter", paramsnames, paramsnames[0]);
    double spacing = 0.01;
    gd.addNumericField("Chi^2 plot spacing (% of value)?", spacing * 100.0, 2, 10, null);
    boolean globalerror = false;
    gd.addCheckbox("Global Fit Error?", globalerror);
    int dataset = 0;
    gd.addNumericField("Data Set (for Global Error)", dataset, 0);
    gd.showDialog();
    if (gd.wasCanceled()) {
      return;
    }
    conf = 0.01f * (float) gd.getNextNumber();
    int paramindex = (int) gd.getNextChoiceIndex();
    spacing = 0.01 * gd.getNextNumber();
    globalerror = gd.getNextBoolean();
    dataset = (int) gd.getNextNumber();

    if (globalerror) {
      support_plane_errors erclass = new support_plane_errors(this, 0.0001, 50, true, 0.1);
      int[] erindeces = {paramindex, dataset};
      // need to set up all the matrices
      int nsel = 0;
      int nparams = 11;
      for (int i = 0; i < ncurves; i++) {
        if (include[i]) {
          nsel++;
        }
      }
      double[][] params = new double[nsel][nparams];
      String[][] tempformulas = new String[nsel][nparams];
      double[][][] constraints = new double[2][nsel][nparams];
      int[][] vflmatrix = new int[nsel][nparams];

      float[][] tempdata = new float[nsel][xpts * ypts];
      float[][] tempweights = new float[nsel][xpts * ypts];

      int nfit = 0;
      int counter = 0;
      for (int i = 0; i < ncurves; i++) {
        if (include[i]) {
          for (int j = 0; j < nparams; j++) {
            params[counter][j] = globalparams[i][j];
            tempformulas[counter][j] = globalformulas[i][j];
            constraints[0][counter][j] = globalconstraints[0][i][j];
            constraints[1][counter][j] = globalconstraints[1][i][j];
            vflmatrix[counter][j] = globalvflmatrix[i][j];
            if (vflmatrix[counter][j] == 0 || (j == 0 && vflmatrix[counter][j] == 2)) {
              nfit++;
            }
          }
          for (int j = 0; j < xpts; j++) {
            for (int k = 0; k < ypts; k++) {
              tempdata[counter][j + k * xpts] = (float) ((double) pch[i][j][k] / (double) nmeas[i]);
              tempweights[counter][j + k * xpts] = weights[i][j][k];
            }
          }
          counter++;
        }
      }
      int dofnum = xpts * ypts * nsel - (nfit - 1) - 1;
      int dofden = xpts * ypts * nsel - nfit - 1;
      // double flim=FLimit(dofnum,dofden,(double)conf);
      double flim = (new jdist()).FLimit(dofnum, dofden, (double) conf);
      IJ.log("FLimit = " + (float) flim);
      if (flim == Double.NaN && flim < 1.0) {
        IJ.showMessage("Invalid Limiting F Value");
        return;
      }
      double truespacing = Math.abs(params[erindeces[1]][erindeces[0]] * spacing);
      double[][] c2plot =
          erclass.geterrorsglobal(
              params,
              vflmatrix,
              tempformulas,
              paramsnames,
              constraints,
              tempdata,
              tempweights,
              flim,
              truespacing,
              erindeces);
      IJ.log("upper limit = " + c2plot[1][0] + " lower limit = " + c2plot[0][0]);
      int templength = c2plot[0].length;
      float[][] c2plotf = new float[2][templength - 1];
      for (int i = 0; i < (templength - 1); i++) {
        c2plotf[0][i] = (float) c2plot[0][i + 1];
        c2plotf[1][i] = (float) c2plot[1][i + 1];
      }
      new PlotWindow4(
              "c2 plot",
              paramsnames[paramindex] + "[" + dataset + "]",
              "Chi^2",
              c2plotf[0],
              c2plotf[1])
          .draw();
    } else {
      support_plane_errors erclass = new support_plane_errors(this, 0.0001, 50, false, 0.1);
      int errindex = paramindex;

      float[] tempdata = new float[xpts * ypts];
      float[] tempweights = new float[xpts * ypts];
      for (int i = 0; i < xpts; i++) {
        for (int j = 0; j < ypts; j++) {
          tempdata[i + j * xpts] = (float) ((double) avg[i][j] / (double) nmeas[ncurves]);
          tempweights[i + j * xpts] = avgweights[i][j];
        }
      }

      int nfit = 0;
      for (int i = 0; i < 7; i++) {
        if (avgfixes[i] == 0) {
          nfit++;
        }
      }
      int dofnum = xpts * ypts - (nfit - 1) - 1;
      int dofden = xpts * ypts - nfit - 1;
      double flim = (new jdist()).FLimit(dofnum, dofden, (double) conf);
      IJ.log("FLimit = " + (float) flim);
      if (flim == Double.NaN && flim < 1.0) {
        IJ.showMessage("Invalid Limiting F Value");
        return;
      }
      double truespacing = Math.abs(avgparams[errindex] * spacing);
      double[][] c2plot =
          erclass.geterrors(
              avgparams,
              avgfixes,
              avgconstraints,
              tempdata,
              tempweights,
              flim,
              truespacing,
              errindex);
      IJ.log("upper limit = " + c2plot[1][0] + " lower limit = " + c2plot[0][0]);
      int templength = c2plot[0].length;
      float[][] c2plotf = new float[2][templength - 1];
      for (int i = 0; i < (templength - 1); i++) {
        c2plotf[0][i] = (float) c2plot[0][i + 1];
        c2plotf[1][i] = (float) c2plot[1][i + 1];
      }
      new PlotWindow4("c2 plot", paramsnames[errindex], "Chi^2", c2plotf[0], c2plotf[1]).draw();
    }
  }
 public boolean get_errors(double[] params, int[] fixes) {
   GenericDialog gd = new GenericDialog("Error Options");
   String[] methods = {"Support Plane", "Monte Carlo"};
   gd.addChoice("Method", methods, methods[0]);
   float conf = 0.67f;
   gd.addNumericField("SP_Confidence Limit (%)", (int) (conf * 100.0f), 5, 10, null);
   String[] labels = {"P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10"};
   gd.addChoice("SP_Parameter", labels, labels[0]);
   double spacing = 0.01;
   gd.addNumericField("SP_Chi^2_plot_spacing (% of value)?", spacing * 100.0, 2, 10, null);
   int ntrials = 100;
   gd.addNumericField("MC_#_Trials", ntrials, 0);
   gd.showDialog();
   if (gd.wasCanceled()) {
     return false;
   }
   int methodindex = gd.getNextChoiceIndex();
   conf = 0.01f * (float) gd.getNextNumber();
   int paramindex = gd.getNextChoiceIndex();
   spacing = 0.01 * gd.getNextNumber();
   ntrials = (int) gd.getNextNumber();
   if (methodindex == 0) {
     support_plane_errors_v2 erclass = new support_plane_errors_v2(this, 0.0001, 50, false, 0.1);
     int errindex = paramindex;
     int nfit = 0;
     for (int i = 0; i < labels.length; i++) {
       if (fixes[i] == 0) {
         nfit++;
       }
     }
     int npts = tempdata.length;
     int dofnum = npts - (nfit - 1) - 1;
     int dofden = npts - nfit - 1;
     double flim = (new jdist()).FLimit(dofnum, dofden, (double) conf);
     IJ.log("FLimit = " + (float) flim);
     if (flim == Double.NaN && flim < 1.0) {
       IJ.showMessage("Invalid Limiting F Value");
       return false;
     }
     double truespacing = Math.abs(params[errindex] * spacing);
     double[][] c2plot =
         erclass.geterrors(
             params, fixes, constraints, tempdata, weights, flim, truespacing, errindex);
     IJ.log("upper limit = " + c2plot[1][0] + " lower limit = " + c2plot[0][0]);
     IJ.log(
         "upper error = "
             + (c2plot[1][0] - params[errindex])
             + " lower error = "
             + (params[errindex] - c2plot[0][0]));
     int templength = c2plot[0].length;
     float[][] c2plotf = new float[2][templength - 1];
     for (int i = 0; i < (templength - 1); i++) {
       c2plotf[0][i] = (float) c2plot[0][i + 1];
       c2plotf[1][i] = (float) c2plot[1][i + 1];
     }
     new PlotWindow4("c2 plot", labels[errindex], "Chi^2", c2plotf[0], c2plotf[1]).draw();
   } else {
     StringBuffer sb = new StringBuffer();
     sb.append("Trial\t");
     for (int i = 0; i < labels.length; i++) {
       if (fixes[i] == 0) sb.append(labels[i] + "\t");
     }
     sb.append("chi^2");
     tw = new TextWindow("Monte Carlo Results", sb.toString(), "", 400, 400);
     redirect = true;
     monte_carlo_errors_v2 erclass = new monte_carlo_errors_v2(this, 0.0001, 50, false, 0.1);
     double[][] errors = erclass.geterrors(params, fixes, constraints, tempdata, weights, ntrials);
     sb = new StringBuffer();
     sb.append("StDev\t");
     for (int i = 0; i < errors.length; i++) {
       float[] ferr = new float[errors[0].length];
       for (int j = 0; j < ferr.length; j++) ferr[j] = (float) errors[i][j];
       float stdev = jstatistics.getstatistic("StDev", ferr, null);
       sb.append("" + stdev);
       if (i < (errors.length - 1)) sb.append("\t");
     }
     tw.append(sb.toString());
     redirect = false;
   }
   return true;
 }