public ExtSed interpolate(ExtSed sed, InterpolationConfig interpConf) throws Exception { if (sed.getNumberOfSegments() == 0) { throw new SedNoDataException(); } ExtSed newsed = ExtSed.flatten(sed, "Angstrom", "Jy"); String intervUnits = interpConf.getUnits(); Double xmin = interpConf.getXMin(); Double xmax = interpConf.getXMax(); if (xmin > Double.NEGATIVE_INFINITY) xmin = um.convertX(new double[] {xmin}, intervUnits, "Angstrom")[0]; if (xmax < Double.POSITIVE_INFINITY) xmax = um.convertX(new double[] {xmax}, intervUnits, "Angstrom")[0]; interpConf.setXMin(Math.min(xmin, xmax)); interpConf.setXMax(Math.max(xmin, xmax)); interpConf.setX(newsed.getSegment(0).getSpectralAxisValues()); interpConf.setY(newsed.getSegment(0).getFluxAxisValues()); SAMPMessage message = SAMPFactory.createMessage(INTERPOLATE_MTYPE, interpConf, InterpolationPayload.class); Response rspns = client.sendMessage(message); InterpolationPayload response = (InterpolationPayload) SAMPFactory.get(rspns.getResult(), InterpolationPayload.class); if (xmin > Double.NEGATIVE_INFINITY) xmin = um.convertX(new double[] {xmin}, "Angstrom", intervUnits)[0]; if (xmax < Double.POSITIVE_INFINITY) xmax = um.convertX(new double[] {xmax}, "Angstrom", intervUnits)[0]; interpConf.setXMin(Math.min(xmin, xmax)); interpConf.setXMax(Math.max(xmin, xmax)); double[] x = um.convertX(response.getX(), "Angstrom", intervUnits); Segment segment = new Segment(); segment.setSpectralAxisValues(x); segment.setFluxAxisValues(response.getY()); segment.setTarget(sed.getSegment(0).getTarget()); segment.setSpectralAxisUnits(intervUnits); segment.setFluxAxisUnits("Jy"); String ucd = "em.wl"; if (intervUnits.equals("Hz")) ucd = "em.freq"; else if (intervUnits.equals("keV")) ucd = "em.energy"; segment.createChar().createSpectralAxis().setUcd(ucd); segment.createChar().createFluxAxis().setUcd("phot.flux.density;" + ucd); ExtSed newSed = manager.newSed(sed.getId() + "_" + interpConf.getMethod().replaceAll(" ", "")); newSed.addSegment(segment); newSed.checkChar(); return newSed; }
@Test public void testStacker() throws Exception { ExtSed sed1 = new ExtSed("Sed1"); ExtSed sed2 = new ExtSed("Sed2"); ExtSed sed3 = new ExtSed("Sed3"); Segment seg1 = new Segment(); for (int k = 0; k < x1.length; k++) { y1[k] = y1[k] * 1e23; yerr1[k] = yerr1[k] * 1e23; } seg1.setFluxAxisValues(y1); seg1.setSpectralAxisValues(x1); seg1.setFluxAxisUnits("Jy"); seg1.setSpectralAxisUnits("Angstrom"); seg1.setDataValues(yerr1, UTYPE.FLUX_STAT_ERROR); sed1.addSegment(seg1); Segment seg2 = new Segment(); seg2.setFluxAxisValues(y2); seg2.setSpectralAxisValues(x2); seg2.setFluxAxisUnits("erg/s/cm2/Hz"); seg2.setSpectralAxisUnits("Angstrom"); seg2.setDataValues(yerr2, UTYPE.FLUX_STAT_ERROR); sed2.addSegment(seg2); Segment seg3 = new Segment(); seg3.setFluxAxisValues(y3); // convert the values in x3 to nm so I can test the unit conversions // too. int k = 0; for (double x : x3) { x3[k] = x * 0.1; k++; } seg3.setSpectralAxisValues(x3); seg3.setFluxAxisUnits("erg/s/cm2/Hz"); seg3.setSpectralAxisUnits("nm"); seg3.setDataValues(yerr3, UTYPE.FLUX_STAT_ERROR); sed3.addSegment(seg3); SedStack stack = new SedStack("Stack"); stack.add(sed1); stack.add(sed2); stack.add(sed3); // setup the stacking configuration StackConfiguration config = new StackConfiguration(); config.setBinsize(2.0); config.setBinsizeUnit("Angstrom"); config.setLogbin(false); config.setSmooth(false); config.setSmoothBinsize(20.); config.setStatistic("avg"); config.setYUnits("erg/s/cm2/Hz"); // stack stacker = new SedStackerStacker(client, (UnitsManager) null); ExtSed result = stacker.stack(stack, config); List<double[]> xs = new ArrayList<>(); List<double[]> ys = new ArrayList<>(); xs.add(x1); xs.add(x2); xs.add(x3); ys.add(y1); ys.add(y2); ys.add(y3); // stack.getOrigSeds() should return original seds for (int j = 0; j < stack.getOrigSeds().size(); j++) { ExtSed origSed = stack.getOrigSeds().get(j); double[] x = xs.get(j); double[] y = ys.get(j); for (int i = 0; i < stack.getOrigSeds().get(j).getSegment(0).getLength(); i++) { double xOrigValue = origSed.getSegment(0).getSpectralAxisValues()[i]; double yOrigValue = origSed.getSegment(0).getFluxAxisValues()[i]; assertEquals(xOrigValue, x[i], EPSILON); assertEquals(yOrigValue, y[i], EPSILON); } } 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}; // test values of stacked Sed double[] yerrValues = (double[]) result.getSegment(0).getDataValues(UTYPE.FLUX_STAT_ERROR); double[] counts = (double[]) result.getAttachment(COUNTS); for (int i = 0; i < result.getSegment(0).getLength(); i++) { double xValue = result.getSegment(0).getSpectralAxisValues()[i]; double yValue = result.getSegment(0).getFluxAxisValues()[i]; assertEquals(controlX[i], xValue, EPSILON); assertEquals(controlY[i], yValue, EPSILON); assertEquals(controlYerr[i], yerrValues[i], EPSILON); assertEquals(controlCounts[i], counts[i], EPSILON); } }
@Test public void testNormalizer() throws Exception { ExtSed sed1 = new ExtSed("Sed1"); ExtSed sed2 = new ExtSed("Sed2"); ExtSed sed3 = new ExtSed("Sed3"); Segment seg1 = new Segment(); for (int k = 0; k < x1.length; k++) { y1[k] = y1[k] * 1e23; yerr1[k] = yerr1[k] * 1e23; } seg1.setFluxAxisValues(y1); seg1.setSpectralAxisValues(x1); seg1.setFluxAxisUnits("Jy"); seg1.setSpectralAxisUnits("Angstrom"); seg1.setDataValues(yerr1, UTYPE.FLUX_STAT_ERROR); sed1.addSegment(seg1); Segment seg2 = new Segment(); seg2.setFluxAxisValues(y2); seg2.setSpectralAxisValues(x2); seg2.setFluxAxisUnits("erg/s/cm2/Hz"); seg2.setSpectralAxisUnits("Angstrom"); seg2.setDataValues(yerr2, UTYPE.FLUX_STAT_ERROR); sed2.addSegment(seg2); Segment seg3 = new Segment(); seg3.setFluxAxisValues(y3); // convert the values in x3 to nm so I can test the unit conversions // too. for (int k = 0; k < x3.length; k++) { x3[k] = x3[k] * 0.1; } seg3.setSpectralAxisValues(x3); seg3.setFluxAxisUnits("erg/s/cm2/Hz"); seg3.setSpectralAxisUnits("nm"); seg3.setDataValues(yerr3, UTYPE.FLUX_STAT_ERROR); sed3.addSegment(seg3); SedStack stack = new SedStack("Stack"); stack.add(sed1); stack.add(sed2); stack.add(sed3); // setup the redshift configuration NormalizationConfiguration config = new NormalizationConfiguration(); config.setMultiply(true); config.setIntegrate(true); config.setStats("Average"); config.setXUnits("Angstrom"); config.setXmax(Double.POSITIVE_INFINITY); config.setXmin(Double.NEGATIVE_INFINITY); config.setIntegrateValueYUnits("erg/s/cm2/Hz"); config.setYValue(1.0); // normalize the Stack SedStackerNormalizer normalizer = new SedStackerNormalizer(client, Default.getInstance().getUnitsManager()); normalizer.normalize(stack, config); List<double[]> xs = new ArrayList<>(); List<double[]> ys = new ArrayList<>(); xs.add(x1); xs.add(x2); xs.add(x3); ys.add(y1); ys.add(y2); ys.add(y3); // stack.getOrigSeds() should return original seds for (int j = 0; j < stack.getOrigSeds().size(); j++) { ExtSed origSed = stack.getOrigSeds().get(j); double[] x = xs.get(j); double[] y = ys.get(j); for (int i = 0; i < stack.getOrigSeds().get(j).getSegment(0).getLength(); i++) { double xOrigValue = origSed.getSegment(0).getSpectralAxisValues()[i]; double yOrigValue = origSed.getSegment(0).getFluxAxisValues()[i]; assertEquals(xOrigValue, x[i], EPSILON); assertEquals(yOrigValue, y[i], EPSILON); } } for (int j = 0; j < stack.getSed(0).getSegment(0).getLength(); j++) assertEquals( 0.49234923 * y1[j], stack.getSed(0).getSegment(0).getFluxAxisValues()[j], EPSILON * 0.49234923 * y1[j]); for (int j = 0; j < stack.getSed(1).getSegment(0).getLength(); j++) assertEquals(9.846 * y2[j], stack.getSed(1).getSegment(0).getFluxAxisValues()[j], EPSILON); assertEquals( 1.1529274, Double.valueOf(stack.getSed(2).getAttachment(NORM_CONSTANT).toString()), EPSILON); }
public void testWriteAscii() throws Exception { URL fileURL = SaveSedDialog.class.getResource("/test_data/3c273.xml"); Sed s = Sed.read(fileURL.openStream(), SedFormat.VOT); List<Segment> segList = new ArrayList<>(); // To assign the RA and DEC correctly, in case the pos.eq is in // SpatialAxis.Coverage.Location rather than Target.Pos for (int i = 0; i < s.getNumberOfSegments(); i++) { Segment seg = s.getSegment(i); if (s.getSegment(i).createTarget().getPos() == null) { if (seg.createChar().createSpatialAxis().createCoverage().getLocation() != null) { seg.createTarget() .createPos() .setValue(seg.getChar().getSpatialAxis().getCoverage().getLocation().getValue()); } else { seg.createTarget() .createPos() .setValue( new DoubleParam[] {new DoubleParam(Double.NaN), new DoubleParam(Double.NaN)}); } } segList.add(s.getSegment(i)); } ExtSed sed = new ExtSed(fileURL.getPath()); sed.addSegment(segList); File output = File.createTempFile("writeAsciiTest_3c273", ".dat"); SaveSedDialog ssd = new SaveSedDialog(SedBuilder.getWorkspace().getRootFrame(), sed, false); ssd.setXunit("Hz"); ssd.setYunit("erg/s/cm2/Angstrom"); ssd.setFilePath("file:///" + output.getPath()); ssd.writeAscii(sed, output, false); URL url = new URL(ssd.getFilePath()); SetupBean result = new AsciiConf().makeConf(url); Segment segment = SegmentImporter.getSegments(result).get(0); assertEquals(segment.getSpectralAxisUnits(), "Hz"); assertEquals(segment.getFluxAxisUnits(), "erg/s/cm2/Angstrom"); // Test SED created in Science window (from calculated fluxes File output2 = File.createTempFile("writeAsciiTest_3c273-2", ".dat"); ssd.setFilePath("file:///" + output2.getPath()); ssd.writeAscii(sed, output2, true); URL url2 = new URL(ssd.getFilePath()); URLConnection urlConnection = url2.openConnection(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String line; String metadata = ""; while ((line = bufferedReader.readLine()).startsWith("#")) { metadata = metadata + line; } SetupBean result2 = new AsciiConf().makeConf(url2); Segment segment2 = SegmentImporter.getSegments(result2).get(0); assertEquals(segment2.getSpectralAxisUnits(), "Hz"); assertEquals(segment2.getFluxAxisUnits(), "erg/s/cm2/Angstrom"); assert (metadata.contains("Iris Flux Integration output")); output.deleteOnExit(); output2.deleteOnExit(); }
@Test public void testNormalizerOutsideRange() throws Exception { ExtSed sed1 = new ExtSed("Sed1"); ExtSed sed2 = new ExtSed("Sed2"); ExtSed sed3 = new ExtSed("Sed3"); Segment seg1 = new Segment(); // for (int k=0; k<x1.length; k++) { // y1[k] = y1[k]*1e23; // yerr1[k] = yerr1[k]*1e23; // } seg1.setFluxAxisValues(y1); seg1.setSpectralAxisValues(x1); seg1.setFluxAxisUnits("erg/s/cm2/Angstrom"); seg1.setSpectralAxisUnits("Angstrom"); seg1.setDataValues(yerr1, UTYPE.FLUX_STAT_ERROR); sed1.addSegment(seg1); Segment seg2 = new Segment(); seg2.setFluxAxisValues(y2); seg2.setSpectralAxisValues(x2); seg2.setFluxAxisUnits("erg/s/cm2/Angstrom"); seg2.setSpectralAxisUnits("Angstrom"); seg2.setDataValues(yerr2, UTYPE.FLUX_STAT_ERROR); sed2.addSegment(seg2); Segment seg3 = new Segment(); seg3.setFluxAxisValues(y3); // convert the values in x3 to nm so I can test the unit conversions // too. for (int k = 0; k < x3.length; k++) { x3[k] = x3[k] * 0.1; } seg3.setSpectralAxisValues(x3); seg3.setFluxAxisUnits("erg/s/cm2/Angstrom"); seg3.setSpectralAxisUnits("nm"); seg3.setDataValues(yerr3, UTYPE.FLUX_STAT_ERROR); sed3.addSegment(seg3); SedStack stack = new SedStack("Stack"); stack.add(sed1); stack.add(sed2); stack.add(sed3); // setup the redshift configuration NormalizationConfiguration config = new NormalizationConfiguration(); config.setMultiply(true); config.setIntegrate(true); config.setStats("Value"); config.setXUnits("Angstrom"); config.setXmax(9.0); config.setXmin(1.5); config.setIntegrateValueYUnits("erg/s/cm2"); config.setYValue(1.0); // normalize the Stack normalizeWithWindowInterceptor(stack, config); List<double[]> xs = new ArrayList<>(); List<double[]> ys = new ArrayList<>(); xs.add(x1); xs.add(x2); xs.add(x3); ys.add(y1); ys.add(y2); ys.add(y3); // stack.getOrigSeds() should return original seds for (int j = 0; j < stack.getOrigSeds().size(); j++) { ExtSed origSed = stack.getOrigSeds().get(j); double[] x = xs.get(j); double[] y = ys.get(j); for (int i = 0; i < stack.getOrigSeds().get(j).getSegment(0).getLength(); i++) { double xOrigValue = origSed.getSegment(0).getSpectralAxisValues()[i]; double yOrigValue = origSed.getSegment(0).getFluxAxisValues()[i]; assertEquals(xOrigValue, x[i], EPSILON); assertEquals(yOrigValue, y[i], EPSILON); } } for (int j = 0; j < stack.getSed(0).getSegment(0).getLength(); j++) assertEquals(y1[j], stack.getSed(0).getSegment(0).getFluxAxisValues()[j], EPSILON); for (int j = 0; j < stack.getSed(1).getSegment(0).getLength(); j++) assertEquals(0.0625 * y2[j], stack.getSed(1).getSegment(0).getFluxAxisValues()[j], EPSILON); assertEquals( 0.035714285714, Double.valueOf(stack.getSed(2).getAttachment(NORM_CONSTANT).toString()), EPSILON); }