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

192 lines
6.7 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.
*/
import hep.dataforge.data.DataPoint;
import hep.dataforge.data.MapDataPoint;
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-01-14 16:16:33 +03:00
import java.util.stream.StreamSupport;
2015-12-18 16:20:47 +03:00
import javafx.application.Platform;
2015-12-27 18:04:05 +03:00
import javafx.concurrent.Task;
2015-12-18 16:20:47 +03:00
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
2015-12-27 18:04:05 +03:00
private FXTaskManager callback;
2015-12-18 16:20:47 +03:00
2016-01-14 16:16:33 +03:00
private final AnchorPane mspPlotPane;
2015-12-18 16:20:47 +03:00
2016-01-14 16:16:33 +03:00
public MspViewController(FXTaskManager callback, AnchorPane mspPlotPane) {
this.callback = callback;
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)
.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) {
2015-12-27 18:04:05 +03:00
MspDataFillTask fillTask = new MspDataFillTask(rootStorage);
2016-01-14 16:16:33 +03:00
if (callback != null) {
2015-12-27 18:04:05 +03:00
callback.postTask(fillTask);
2015-12-18 16:20:47 +03:00
}
2015-12-27 18:04:05 +03:00
Viewer.runTask(fillTask);
2015-12-18 16:20:47 +03:00
}
}
2015-12-27 18:04:05 +03:00
private class MspDataFillTask extends Task<Void> {
2016-01-14 16:16:33 +03:00
2015-12-27 18:04:05 +03:00
private final Storage storage;
public MspDataFillTask(Storage storage) {
this.storage = storage;
}
2016-01-14 16:16:33 +03:00
2015-12-27 18:04:05 +03:00
@Override
protected Void call() throws Exception {
2016-01-14 16:16:33 +03:00
updateTitle("Fill msp data (" + storage.getName() + ")");
2015-12-27 18:04:05 +03:00
MspDataLoadTask loadTask = new MspDataLoadTask(storage);
2016-01-14 16:16:33 +03:00
if (callback != null) {
2015-12-27 18:04:05 +03:00
callback.postTask(loadTask);
}
Viewer.runTask(loadTask);
List<DataPoint> mspData = loadTask.get();
2016-01-14 16:16:33 +03:00
DynamicPlottableSet plottables = new DynamicPlottableSet();
2015-12-27 18:04:05 +03:00
for (DataPoint point : mspData) {
for (String name : point.names()) {
if (!name.equals("timestamp")) {
2016-01-14 16:16:33 +03:00
if (!plottables.hasPlottable(name)) {
plottables.addPlottable(new DynamicPlottable(name, null, name));
2015-12-27 18:04:05 +03:00
}
2015-12-22 14:19:17 +03:00
}
2015-12-27 18:04:05 +03:00
}
2016-01-14 16:16:33 +03:00
plottables.put(point);
2015-12-27 18:04:05 +03:00
}
2016-01-14 16:16:33 +03:00
updateMspPane(plottables);
2015-12-27 18:04:05 +03:00
return null;
2015-12-18 16:20:47 +03:00
}
}
2015-12-27 18:04:05 +03:00
private class MspDataLoadTask extends Task<List<DataPoint>> {
private final Storage storage;
public MspDataLoadTask(Storage storage) {
this.storage = storage;
2015-12-18 16:20:47 +03:00
}
2015-12-27 18:04:05 +03:00
@Override
protected List<DataPoint> call() throws Exception {
2016-01-14 16:16:33 +03:00
updateTitle("Load msp data (" + storage.getName() + ")");
2015-12-27 18:04:05 +03:00
List<DataPoint> mspData = new ArrayList<>();
DataPoint last = null;
for (String loaderName : storage.loaders().keySet()) {
if (loaderName.startsWith("msp")) {
try (PointLoader mspLoader = (PointLoader) storage.getLoader(loaderName)) {
mspLoader.open();
updateMessage("Loading mass spectrometer data from " + mspLoader.getName());
updateProgress(-1, 1);
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);
}
}
}
updateMessage("Loading msp data finished");
updateProgress(0, 1);
return mspData;
2015-12-18 16:20:47 +03:00
}
2015-12-27 18:04:05 +03:00
2015-12-18 16:20:47 +03:00
}
2015-12-27 18:04:05 +03:00
public void setCallback(FXTaskManager callback) {
2015-12-18 16:20:47 +03:00
this.callback = callback;
}
/**
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) {
MapDataPoint p = new MapDataPoint();
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;
}
}