@Test
  public void testStackAvg() throws Exception {

    payload = (SedStackerStackPayload) SAMPFactory.get(SedStackerStackPayload.class);

    // Setup the stack payload
    payload.addSegment(segment1);
    payload.addSegment(segment2);
    payload.addSegment(segment3);
    payload.setBinsize(2.0);
    payload.setLogBin(false);
    payload.setSmooth(false);
    payload.setSmoothBinsize(5.0);
    payload.setStatistic("avg");

    // Setup and send SAMP message
    SAMPMessage message =
        SAMPFactory.createMessage("stack.stack", payload, SedStackerStackPayload.class);

    Response rspns = client.sendMessage(message);

    SedStackerStackPayload response =
        (SedStackerStackPayload) SAMPFactory.get(rspns.getResult(), SedStackerStackPayload.class);

    // get response values
    SegmentPayload seg = response.getSegments().get(0);
    double[] controlY =
        new double[] {5.0, 5.36666667, 4.5, 2.66666667, 4.0, 6.5, 1.5, 10.5, 5.0, 10.0};
    double[] controlX = new double[] {0., 2., 4., 6., 8., 10., 16., 22., 50., 100.};
    // double[] controlYerr = new double[] {0.5, 0.501120965, 0.3640055,
    // 0.18104634, 0.4, 0.48102899, 0.15, 1.05, 0.5, 1.0}; // calculated
    // from errors [sqrt(sum(errors^2))/N]
    double[] controlYerr =
        new double[] {0, 6.82169741, 2.5, 1.64991582, 0., 5.21216526, 0., 0., 0., 0.}; // calculated
    // from
    // stddev(flux)
    double[] controlCounts = new double[] {1, 3, 2, 3, 1, 3, 1, 1, 1, 1};

    assertEquals(response.getSegments().size(), 1);

    // tests
    for (int i = 0; i < seg.getY().length; i++) {
      assertEquals(controlY[i], seg.getY()[i], EPSILON);
    }
    for (int i = 0; i < seg.getY().length; i++) {
      assertEquals(controlX[i], seg.getX()[i], EPSILON);
    }
    for (int i = 0; i < seg.getY().length; i++) {
      assertEquals(controlYerr[i], seg.getYerr()[i], EPSILON);
    }
    for (int i = 0; i < seg.getY().length; i++) {
      assertEquals(controlCounts[i], seg.getCounts()[i], EPSILON);
    }
  }
  @Test
  public void testNormalize() throws Exception {
    payload = (SedStackerNormalizePayload) SAMPFactory.get(SedStackerNormalizePayload.class);

    // Setup the normalization payload
    payload.addSegment(segment1);
    payload.addSegment(segment2);
    payload.addSegment(segment3);

    payload.setNormOperator(0);
    payload.setY0(1.0);
    payload.setXmin("MIN");
    payload.setXmax("MAX");
    payload.setStats("avg");
    payload.setIntegrate(Boolean.TRUE);

    // Setup and send SAMP message
    SAMPMessage message =
        SAMPFactory.createMessage("stack.normalize", payload, SedStackerNormalizePayload.class);

    Response rspns = client.sendMessage(message);

    SedStackerNormalizePayload response =
        (SedStackerNormalizePayload)
            SAMPFactory.get(rspns.getResult(), SedStackerNormalizePayload.class);

    // get response values
    double[] resy1 = response.getSegments().get(0).getY();
    double[] resy2 = response.getSegments().get(1).getY();
    Double resnorm3 = response.getSegments().get(2).getNormConstant();
    SegmentPayload seg1 = response.getSegments().get(0);
    Double z = seg1.getZ();
    Double norm = seg1.getNormConstant();

    assertEquals(response.getSegments().size(), 3);

    // tests
    for (int i = 0; i < resy1.length; i++) {
      assertEquals(0.49234923 * y1[i], resy1[i], EPSILON);
    }
    for (int i = 0; i < resy2.length; i++) {
      assertEquals(9.846 * y2[i], resy2[i], EPSILON);
    }
    assertEquals(1.1529274, resnorm3, EPSILON);
  }