private void subtractActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_subtractActionPerformed

    RasterData data = maincont.getData();
    if (data == null) {
      JOptionPane.showMessageDialog(this, "I have no data yet to subtract anything from :-)");
      return;
    }
    if (subtract_type == null) {
      subtract_type = maincont.getFiletype();
    }
    FlowSelection pan =
        new FlowSelection(
            "Select the flow and file type you wish to subtract from the currently selected data");
    pan.setFlow(subtract_flow);
    pan.setFiletype(subtract_type);
    int ans =
        JOptionPane.showConfirmDialog(
            this,
            pan,
            "Data Subtraction",
            JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE);
    if (ans != JOptionPane.OK_OPTION) {
      return;
    }

    subtract_flow = pan.getFlow();
    subtract_type = pan.getFiletype();
    // otherwise load the data and then subtract
    RasterData sub = null;
    DataAccessManager manager = DataAccessManager.getManager(maincont.getExp().getWellContext());
    try {
      p("Loading subregion, RELATIVE coord " + maincont.getRelativeDataAreaCoord());
      sub =
          manager.getRasterDataForArea(
              null,
              maincont.getRasterSize(),
              maincont.getRelativeDataAreaCoord(),
              pan.getFlow(),
              pan.getFiletype(),
              null,
              0,
              -1);
      // now subtract first frame
    } catch (Exception ex) {
      p("Error when loading: " + ErrorHandler.getString(ex));
    }
    if (sub == null) {
      JOptionPane.showMessageDialog(
          this,
          "I could not load flow "
              + pan.getFlow()
              + ", "
              + pan.getFiletype()
              + ", @ "
              + maincont.getAbsDataAreaCoord());
      return;
    }
    String what = "raw";
    if (this.automatic_nn) {
      GuiUtils.showNonModalMsg("Computing NN before subtraction");
      sub = computeNN(null, sub);
      what = "NN subtracted";
    }

    data.subtract(sub);
    JOptionPane.showMessageDialog(
        this,
        "I subtracted the "
            + what
            + " data from "
            + pan.getFlow()
            + ", "
            + pan.getFiletype()
            + ", @ "
            + maincont.getAbsDataAreaCoord());
    this.rasterViewUpdate();
  } // GEN-LAST:event_subtractActionPerformed
  private RasterData computeNN(ProgressListener prog, RasterData rawdata) {
    //   p("computeNN. Maincont is: " + maincont);
    if (rawdata == null) {
      GuiUtils.showNonModalDialog(
          "<html>I see no data yet - did you already pick a region?<br>"
              + "(Even if you see something somewhere, if you didn't actually select a region, it might just show some sample data)</html>",
          "No data - region selected?");
      return null;
    }
    RasterData nndata = null;
    try {
      if (maincont == null) {
        maincont = ExplorerContext.getCurContext(expContext);
      }
      int span = Math.max(1, this.maincont.getSpan());
      NearestNeighbor nn = new NearestNeighbor(span, maincont.getMedianFunction());
      BitMask ignore = maincont.getIgnoreMask();
      BitMask take = maincont.getBgMask();

      if (take != null && take == ignore) {
        JOptionPane.showMessageDialog(
            this,
            "You select the same mask for ignore and bg :-). \nYou should select another mask for the bg (or you get a null result. I will just return the old data.");
        return rawdata;
      }
      if (take != null && take.computePercentage() < 1) {
        int ans =
            JOptionPane.showConfirmDialog(
                this,
                "<html>The bg mask only has "
                    + take.computePercentage()
                    + "% wells, do you want to still use it?"
                    + "<br><b>Did you already select a region?</b>"
                    + "<br>You might want to use the MaskEditor (and <b>refresh</b> the masks possibly) to check them</html>",
                "Few wells",
                JOptionPane.OK_CANCEL_OPTION);
        if (ans == JOptionPane.CANCEL_OPTION) {
          return rawdata;
        }
      }
      maincont.setBgMask(take);
      maincont.setIgnoreMask(ignore);
      if (prog != null)
        prog.setMessage(
            "Masked neighbor subtraction: ignore mask " + ignore + " and empty mask " + take);
      // RasterData nndata = nn.compute(rawdata, mask, prog, span);

      //   p("calling computebetter");
      // if (boxslow.isSelected()) nndata =nn.computeSlow(rawdata, ignore, take, prog, span);
      nndata = nn.computeBetter(rawdata, ignore, take, prog, span);

    } catch (Exception e) {
      p("Error with nn: " + ErrorHandler.getString(e));
      JOptionPane.showMessageDialog(
          this,
          "I was not able to do the masked neighbor subtraction:\n" + ErrorHandler.getString(e));
      return null;
    }
    if (nndata == null) {
      JOptionPane.showMessageDialog(
          this,
          "I was not able to do the masked neighbor subtraction - I got no error but also no result :-) ");
    }
    return nndata;
  }