ControlSystemStudio / cs-studio

Control System Studio is an Eclipse-based collections of tools to monitor and operate large scale control systems, such as the ones in the accelerator community.
https://controlsystemstudio.org/
Eclipse Public License 1.0
111 stars 96 forks source link

How to export Data Browser embedded in the .OPI file to CSV? #2719

Open tnakaicode opened 1 year ago

tnakaicode commented 1 year ago

Is there any way to export the data in the range where Data Browser (linked to *.plt) embedded in the .OPI file is displayed to CSV? I would like to be able to output to CSV by writing it in JavaScript or other script and linking it to the Action Button.

kasemir commented 1 year ago

Sure, that's easy. With a script you can do pretty much anything. But beware that when you create a script, it will then be up to you to maintain it. I'm not making any promises that this will work in the next iteration of this or any other display tool:

# Embedded python script
# Attach to a DataBrowserWidget,
# triggered by a PV like "loc://save(0)".
#
# Then add a button that writes 1 to that PV
#
# --> This script runs and saves data from the plot

from org.csstudio.display.builder.runtime.script import PVUtil, ScriptUtil
from org.csstudio.trends.databrowser3.export import SpreadsheetExportJob, Source, ValueFormatter
from org.phoebus.archive.vtype import Style
from org.phoebus.framework.jobs import JobManager

# Was the PV set to 1?
if PVUtil.getDouble(pvs[0]) > 0:
    # Reset PV to zero
    pvs[0].write(0)

    # Start with checking the widget type
    print(widget.__class__.__name__)
    # Should be 'DataBrowserWidget'.
    # Spend some quality time reading the CS-Studio
    # source code to see what you can do with a DataBrowserWidget

    # Get representation, should be 'DataBrowserRepresentation'
    representation = widget.getUserData(widget.USER_DATA_REPRESENTATION)
    print(representation.__class__.__name__)

    # Since it's a DataBrowserRepresentation, get the model
    db = representation.getDataBrowserModel()
    print(db.__class__.__name__)

    # Then either dump the data yourself, or use for example
    # the spreadsheed exporter
    range = db.getTimerange().toAbsoluteInterval()
    exporter = SpreadsheetExportJob(db, range.getStart(), range.getEnd(),
                                    Source.PLOT, 0,
                                    ValueFormatter(Style.Decimal, 3),
                                    "/tmp/demo.dat",
                                    None, False)
    JobManager.schedule("Save data", exporter)