numass-framework/numass-viewer/src/main/java/inr/numass/viewer/MspViewController.java

160 lines
6.5 KiB
Java
Raw Normal View History

2015-12-18 16:20:47 +03:00
/*
* Copyright 2015 Alexander Nozik.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package inr.numass.viewer;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
2016-04-21 19:43:58 +03:00
import hep.dataforge.context.Context;
2016-04-23 19:12:55 +03:00
import hep.dataforge.context.DFProcess;
import hep.dataforge.context.ProcessManager;
import hep.dataforge.exceptions.StorageException;
import hep.dataforge.points.DataPoint;
import hep.dataforge.points.MapPoint;
2016-01-14 16:16:33 +03:00
import hep.dataforge.plots.PlotUtils;
import hep.dataforge.plots.data.DynamicPlottable;
import hep.dataforge.plots.data.DynamicPlottableSet;
import hep.dataforge.plots.fx.PlotContainer;
import hep.dataforge.plots.jfreechart.JFreeChartFrame;
2015-12-18 16:20:47 +03:00
import hep.dataforge.storage.api.PointLoader;
import hep.dataforge.storage.api.Storage;
import hep.dataforge.values.Value;
import java.util.ArrayList;
import java.util.List;
2016-04-23 19:12:55 +03:00
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
2016-01-14 16:16:33 +03:00
import java.util.stream.StreamSupport;
2015-12-18 16:20:47 +03:00
import javafx.application.Platform;
import javafx.scene.layout.AnchorPane;
2015-12-22 14:19:17 +03:00
import org.slf4j.LoggerFactory;
2015-12-18 16:20:47 +03:00
/**
* FXML Controller class
*
* @author darksnake
*/
2016-01-14 16:16:33 +03:00
public class MspViewController {
2015-12-18 16:20:47 +03:00
2016-01-14 16:16:33 +03:00
private final AnchorPane mspPlotPane;
2016-04-21 19:43:58 +03:00
private final Context context;
2015-12-18 16:20:47 +03:00
2016-04-21 19:43:58 +03:00
public MspViewController(Context context, AnchorPane mspPlotPane) {
this.context = context;
2016-01-14 16:16:33 +03:00
this.mspPlotPane = mspPlotPane;
2015-12-18 16:20:47 +03:00
}
/**
* update detector pane with new data
*/
2016-01-14 16:16:33 +03:00
private void updateMspPane(DynamicPlottableSet mspData) {
// MetaBuilder plotMeta = new MetaBuilder("plot")
// .setNode(new MetaBuilder("xAxis")
// .setValue("axisTitle", "time")
// .setValue("type", "time"))
// .setNode(new MetaBuilder("yAxis")
// .setValue("axisTitle", "partial pressure")
// .setValue("axisUnits", "mbar")
// .setValue("type", "log")
// );
JFreeChartFrame frame = new JFreeChartFrame("mspData", null);
PlotUtils.setYAxis(frame, "partial pressure", "mbar", "log");
frame.getConfig().setValue("yAxis.range.lower", 1e-10);
frame.getConfig().setValue("yAxis.range.upper", 1e-3);
PlotUtils.setXAxis(frame, "time", null, "time");
StreamSupport.stream(mspData.spliterator(), false)
2016-04-23 19:12:55 +03:00
.sorted((DynamicPlottable o1, DynamicPlottable o2)
-> Integer.valueOf(o1.getName()).compareTo(Integer.valueOf(o2.getName()))).forEach((pl) -> frame.add(pl));
2015-12-18 16:20:47 +03:00
Platform.runLater(() -> {
2016-01-14 16:16:33 +03:00
PlotContainer container = PlotContainer.anchorTo(mspPlotPane);
container.setPlot(frame);
2015-12-18 16:20:47 +03:00
});
}
public void fillMspData(Storage rootStorage) {
if (rootStorage != null) {
2016-04-23 19:12:55 +03:00
context.processManager().post("viewer.msp.fill", (ProcessManager.Callback callback) -> {
try {
// callback.updateTitle("Fill msp data (" + rootStorage.getName() + ")");
callback.updateTitle("Load msp data (" + rootStorage.getName() + ")");
List<DataPoint> mspData = new ArrayList<>();
DataPoint last = null;
for (String loaderName : rootStorage.loaders().keySet()) {
if (loaderName.startsWith("msp")) {
try (final PointLoader mspLoader = (PointLoader) rootStorage.getLoader(loaderName)) {
mspLoader.open();
callback.updateMessage("Loading mass spectrometer data from " + mspLoader.getName());
for (DataPoint dp : mspLoader.asDataSet()) {
mspData.add(dp);
last = dp;
}
if (last != null) {
mspData.add(terminatorPoint(last));
}
} catch (Exception ex) {
LoggerFactory.getLogger(getClass()).error("Can't read msp loader data", ex);
}
2015-12-27 18:04:05 +03:00
}
2015-12-22 14:19:17 +03:00
}
2016-04-23 19:12:55 +03:00
callback.updateMessage("Loading msp data finished");
// return mspData;
// List<DataPoint> mspData = (List<DataPoint>) loadProcess.getTask().get();
if (!mspData.isEmpty()) {
DynamicPlottableSet plottables = new DynamicPlottableSet();
for (DataPoint point : mspData) {
for (String name : point.names()) {
if (!name.equals("timestamp")) {
if (!plottables.hasPlottable(name)) {
plottables.addPlottable(new DynamicPlottable(name, name));
}
}
}
plottables.put(point);
2015-12-27 18:04:05 +03:00
}
2016-04-23 19:12:55 +03:00
updateMspPane(plottables);
2015-12-27 18:04:05 +03:00
}
2016-04-23 19:12:55 +03:00
} catch (StorageException ex) {
throw new RuntimeException(ex);
2015-12-27 18:04:05 +03:00
}
2016-04-23 19:12:55 +03:00
});
2015-12-18 16:20:47 +03:00
}
}
/**
2015-12-22 14:19:17 +03:00
* Create a null value point to terminate msp series
2015-12-18 16:20:47 +03:00
*
* @param last
* @return
*/
private DataPoint terminatorPoint(DataPoint last) {
2016-02-28 20:08:59 +03:00
MapPoint p = new MapPoint();
2015-12-18 16:20:47 +03:00
p.putValue("timestamp", last.getValue("timestamp").timeValue().plusMillis(10));
for (String name : last.namesAsArray()) {
if (!name.equals("timestamp")) {
2015-12-24 12:20:42 +03:00
p.putValue(name, Value.NULL);
2015-12-18 16:20:47 +03:00
}
}
return p;
}
}