Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tnakaicode opened this issue Feb 27, 2023 · 1 comment
Open

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

tnakaicode opened this issue Feb 27, 2023 · 1 comment

Comments

@tnakaicode
Copy link

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
Copy link
Contributor

kasemir commented Feb 28, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants