Search in sources :

Example 1 with ValueIterator

use of org.csstudio.archive.reader.ValueIterator in project org.csstudio.display.builder by kasemir.

the class MatlabFileExportJob method performExport.

/**
 * {@inheritDoc}
 */
@Override
protected void performExport(final IProgressMonitor monitor, final PrintStream out) throws Exception {
    // This exporter does its own file handling
    if (out != null)
        throw new IllegalStateException();
    final MatFileIncrementalWriter writer = new MatFileIncrementalWriter(filename);
    int i = 0;
    for (ModelItem item : model.getItems()) {
        // Get data
        monitor.subTask(NLS.bind("Fetching data for {0}", item.getName()));
        final ValueIterator iter = createValueIterator(item);
        final List<Instant> times = new ArrayList<Instant>();
        final List<Double> values = new ArrayList<Double>();
        final List<AlarmSeverity> severities = new ArrayList<AlarmSeverity>();
        while (iter.hasNext() && !monitor.isCanceled()) {
            final VType value = iter.next();
            times.add(VTypeHelper.getTimestamp(value));
            values.add(VTypeHelper.toDouble(value));
            severities.add(VTypeHelper.getSeverity(value));
            if (values.size() % PROGRESS_UPDATE_LINES == 0)
                monitor.subTask(NLS.bind("{0}: Obtained {1} samples", item.getName(), values.size()));
        }
        // Add to Matlab file
        final MLStructure struct = createMLStruct(i++, item.getName(), times, values, severities);
        writer.write(struct);
    }
    writer.close();
}
Also used : AlarmSeverity(org.diirt.vtype.AlarmSeverity) Instant(java.time.Instant) ArrayList(java.util.ArrayList) ModelItem(org.csstudio.trends.databrowser3.model.ModelItem) ValueIterator(org.csstudio.archive.reader.ValueIterator) MatFileIncrementalWriter(com.jmatio.io.MatFileIncrementalWriter) MLDouble(com.jmatio.types.MLDouble) MLStructure(com.jmatio.types.MLStructure) VType(org.diirt.vtype.VType)

Example 2 with ValueIterator

use of org.csstudio.archive.reader.ValueIterator in project org.csstudio.display.builder by kasemir.

the class MatlabScriptExportJob method performExport.

/**
 * {@inheritDoc}
 */
@Override
protected void performExport(final IProgressMonitor monitor, final PrintStream out) throws Exception {
    final DateFormat date_format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
    int count = 0;
    for (ModelItem item : model.getItems()) {
        // Item header
        if (count > 0)
            out.println();
        printItemInfo(out, item);
        // Get data
        monitor.subTask(NLS.bind("Fetching data for {0}", item.getName()));
        final ValueIterator values = createValueIterator(item);
        // Dump all values
        MatlabQualityHelper qualities = new MatlabQualityHelper();
        long line_count = 0;
        out.println("clear t;");
        out.println("clear v;");
        out.println("clear q;");
        while (values.hasNext() && !monitor.isCanceled()) {
            final VType value = values.next();
            ++line_count;
            // t(1)='2010/03/15 13:30:10.123';
            out.println("t{" + line_count + "}='" + date_format.format(Date.from(VTypeHelper.getTimestamp(value))) + "';");
            // v(1)=4.125;
            final double num = VTypeHelper.toDouble(value);
            if (Double.isNaN(num) || Double.isInfinite(num))
                out.println("v(" + line_count + ")=NaN;");
            else
                out.println("v(" + line_count + ")=" + num + ";");
            // q(1)=0;
            out.println("q(" + line_count + ")=" + qualities.getQualityCode(VTypeHelper.getSeverity(value), VTypeHelper.getMessage(value)) + ";");
            if (line_count % PROGRESS_UPDATE_LINES == 0)
                monitor.subTask(NLS.bind("{0}: Wrote {1} samples", item.getName(), line_count));
        }
        out.println(comment + "Convert time stamps into 'date numbers'");
        out.println("tn=datenum(t, 'yyyy/mm/dd HH:MM:SS.FFF');");
        out.println(comment + "Prepare patched data because");
        out.println(comment + "timeseries() cannot handle duplicate time stamps");
        out.println("[xx, idx]=unique(tn, 'last');");
        out.println("pt=tn(idx);");
        out.println("pv=v(idx);");
        out.println("pq=q(idx);");
        out.println("clear xx idx");
        out.println(comment + "Convert into time series and plot");
        // Patch "_" in name because Matlab plot will interprete it as LaTeX sub-script
        final String channel_name = item.getResolvedDisplayName().replace("_", "\\_");
        out.println("channel" + count + "=timeseries(pv', pt', pq', 'IsDatenum', true, 'Name', '" + channel_name + "');");
        out.print("channel" + count + ".QualityInfo.Code=[");
        for (int q = 0; q < qualities.getNumCodes(); ++q) out.print(" " + q);
        out.println(" ];");
        out.print("channel" + count + ".QualityInfo.Description={");
        for (int q = 0; q < qualities.getNumCodes(); ++q) out.print(" '" + qualities.getQuality(q) + "'");
        out.println(" };");
        out.println();
        ++count;
    }
    out.println(comment + "Example for plotting the data");
    for (int i = 0; i < count; ++i) {
        out.println("subplot(1, " + count + ", " + (i + 1) + ");");
        out.println("plot(channel" + i + ");");
    }
}
Also used : VType(org.diirt.vtype.VType) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ModelItem(org.csstudio.trends.databrowser3.model.ModelItem) ValueIterator(org.csstudio.archive.reader.ValueIterator) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with ValueIterator

use of org.csstudio.archive.reader.ValueIterator in project org.csstudio.display.builder by kasemir.

the class SpreadsheetExportJob method performExport.

/**
 * {@inheritDoc}
 */
@Override
protected void performExport(final IProgressMonitor monitor, final PrintStream out) throws Exception {
    // Item header
    for (ModelItem item : model.getItems()) printItemInfo(out, item);
    out.println();
    // Spreadsheet Header
    out.print("# " + Messages.TimeColumn);
    for (ModelItem item : model.getItems()) out.print(Messages.Export_Delimiter + item.getName() + " " + formatter.getHeader());
    out.println();
    // Create speadsheet interpolation
    final List<ValueIterator> iters = new ArrayList<>();
    for (ModelItem item : model.getItems()) {
        monitor.subTask(NLS.bind("Fetching data for {0}", item.getName()));
        iters.add(createValueIterator(item));
    }
    final SpreadsheetIterator sheet = new SpreadsheetIterator(iters.toArray(new ValueIterator[iters.size()]));
    // Dump the spreadsheet lines
    long line_count = 0;
    while (sheet.hasNext() && !monitor.isCanceled()) {
        final Instant time = sheet.getTime();
        final VType[] line = sheet.next();
        out.print(TimestampHelper.format(time));
        for (int i = 0; i < line.length; ++i) out.print(Messages.Export_Delimiter + formatter.format(line[i]));
        out.println();
        ++line_count;
        if ((line_count % PROGRESS_UPDATE_LINES) == 0)
            monitor.subTask(NLS.bind("Wrote {0} samples", line_count));
        if (monitor.isCanceled())
            break;
    }
}
Also used : VType(org.diirt.vtype.VType) SpreadsheetIterator(org.csstudio.archive.reader.SpreadsheetIterator) Instant(java.time.Instant) ArrayList(java.util.ArrayList) ModelItem(org.csstudio.trends.databrowser3.model.ModelItem) ValueIterator(org.csstudio.archive.reader.ValueIterator)

Example 4 with ValueIterator

use of org.csstudio.archive.reader.ValueIterator in project org.csstudio.display.builder by kasemir.

the class ExportJob method createValueIterator.

/**
 * @param item ModelItem
 *  @return ValueIterator for samples in the item
 *  @throws Exception on error
 */
protected ValueIterator createValueIterator(final ModelItem item) throws Exception {
    if (source == Source.PLOT || !(item instanceof PVItem))
        return new ModelSampleIterator(item, start, end);
    // Start ValueIterator for each sub-archive
    final ArchiveDataSource[] archives = ((PVItem) item).getArchiveDataSources();
    final List<ValueIterator> iters = new ArrayList<>();
    Exception error = null;
    for (ArchiveDataSource archive : archives) {
        // Create reader, remember to close it when done
        final ArchiveReader reader = ArchiveRepository.getInstance().getArchiveReader(archive.getUrl());
        archive_readers.add(reader);
        // Create ValueIterator
        try {
            ValueIterator iter;
            if (source == Source.OPTIMIZED_ARCHIVE && optimize_parameter > 1)
                iter = reader.getOptimizedValues(archive.getKey(), item.getName(), start, end, (int) optimize_parameter);
            else {
                iter = reader.getRawValues(archive.getKey(), item.getName(), start, end);
                if (source == Source.LINEAR_INTERPOLATION && optimize_parameter >= 1)
                    iter = new LinearValueIterator(iter, TimeDuration.ofSeconds(optimize_parameter));
            }
            iters.add(iter);
        } catch (Exception ex) {
            Logger.getLogger(getClass().getName()).log(Level.FINE, "Export error for " + item.getName(), ex);
            if (error == null)
                error = ex;
        }
    }
    // If none of the iterators work out, report the first error that we found
    if (iters.isEmpty() && error != null)
        throw error;
    // Return a merging iterator
    return new MergingValueIterator(iters.toArray(new ValueIterator[iters.size()]));
}
Also used : ArchiveReader(org.csstudio.archive.reader.ArchiveReader) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArchiveDataSource(org.csstudio.trends.databrowser3.model.ArchiveDataSource) ValueIterator(org.csstudio.archive.reader.ValueIterator) MergingValueIterator(org.csstudio.archive.reader.MergingValueIterator) LinearValueIterator(org.csstudio.archive.reader.LinearValueIterator) MergingValueIterator(org.csstudio.archive.reader.MergingValueIterator) PVItem(org.csstudio.trends.databrowser3.model.PVItem) LinearValueIterator(org.csstudio.archive.reader.LinearValueIterator)

Example 5 with ValueIterator

use of org.csstudio.archive.reader.ValueIterator in project org.csstudio.display.builder by kasemir.

the class PlainExportJob method performExport.

/**
 * {@inheritDoc}
 */
@Override
protected void performExport(final IProgressMonitor monitor, final PrintStream out) throws Exception {
    int count = 0;
    for (ModelItem item : model.getItems()) {
        // Item header
        if (count > 0)
            out.println();
        printItemInfo(out, item);
        // Get data
        monitor.subTask(NLS.bind("Fetching data for {0}", item.getName()));
        final ValueIterator values = createValueIterator(item);
        // Dump all values
        out.println(comment + Messages.TimeColumn + Messages.Export_Delimiter + formatter.getHeader());
        long line_count = 0;
        while (values.hasNext() && !monitor.isCanceled()) {
            final VType value = values.next();
            final String time = TimestampHelper.format(VTypeHelper.getTimestamp(value));
            out.println(time + Messages.Export_Delimiter + formatter.format(value));
            ++line_count;
            if (++line_count % PROGRESS_UPDATE_LINES == 0)
                monitor.subTask(NLS.bind("{0}: Wrote {1} samples", item.getName(), line_count));
        }
        ++count;
    }
}
Also used : VType(org.diirt.vtype.VType) ModelItem(org.csstudio.trends.databrowser3.model.ModelItem) ValueIterator(org.csstudio.archive.reader.ValueIterator)

Aggregations

ValueIterator (org.csstudio.archive.reader.ValueIterator)5 ModelItem (org.csstudio.trends.databrowser3.model.ModelItem)4 VType (org.diirt.vtype.VType)4 ArrayList (java.util.ArrayList)3 Instant (java.time.Instant)2 MatFileIncrementalWriter (com.jmatio.io.MatFileIncrementalWriter)1 MLDouble (com.jmatio.types.MLDouble)1 MLStructure (com.jmatio.types.MLStructure)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ArchiveReader (org.csstudio.archive.reader.ArchiveReader)1 LinearValueIterator (org.csstudio.archive.reader.LinearValueIterator)1 MergingValueIterator (org.csstudio.archive.reader.MergingValueIterator)1 SpreadsheetIterator (org.csstudio.archive.reader.SpreadsheetIterator)1 ArchiveDataSource (org.csstudio.trends.databrowser3.model.ArchiveDataSource)1 PVItem (org.csstudio.trends.databrowser3.model.PVItem)1 AlarmSeverity (org.diirt.vtype.AlarmSeverity)1