Search in sources :

Example 1 with Data

use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.

the class LargestTriangleThreeBucketsGeneralizer method calculateBucketAverage.

private BucketAverage calculateBucketAverage(int bucketIndex, double bucketSize, QuantityValue[] data) {
    int dataLength = data.length;
    int avgRangeStart = (int) Math.floor((bucketIndex + 0) * bucketSize) + 1;
    int avgRangeEnd = (int) Math.floor((bucketIndex + 1) * bucketSize) + 1;
    avgRangeEnd = avgRangeEnd < dataLength ? avgRangeEnd : dataLength;
    double avgRangeLength = avgRangeEnd - avgRangeStart;
    Double avgValue = 0d;
    Double avgTimestamp = 0d;
    int amountOfNodataValues = 0;
    boolean noDataThresholdExceeded = false;
    for (; avgRangeStart < avgRangeEnd; avgRangeStart++) {
        final QuantityValue current = data[avgRangeStart];
        avgTimestamp += current.getTimestamp();
        if (noDataThresholdExceeded) {
            // keep on calc avg timestamp
            continue;
        }
        if (current.isNoDataValue()) {
            amountOfNodataValues++;
            if (amountOfNodataValues == noDataGapThreshold) {
                noDataThresholdExceeded = true;
            }
        } else {
            avgValue += current.getValue();
        }
    }
    avgTimestamp /= avgRangeLength;
    avgValue /= avgRangeLength;
    return new BucketAverage(avgTimestamp, avgValue);
}
Also used : QuantityValue(org.n52.io.response.dataset.quantity.QuantityValue)

Example 2 with Data

use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.

the class LargestTriangleThreeBucketsGeneralizer method generalizeData.

private QuantityData generalizeData(QuantityValue[] data) {
    int dataLength = data.length;
    // Bucket size. Leave room for start and end data points
    double bucketSize = ((double) dataLength - 2) / (maxOutputValues - 2);
    int pointIndex = 0;
    QuantityData sampled = new QuantityData();
    sampled.addValues(data[pointIndex]);
    for (int bucketIndex = 0; bucketIndex < maxOutputValues - 2; bucketIndex++) {
        // get the range for this bucket
        int rangeOff = (int) Math.floor((bucketIndex + 0) * bucketSize) + 1;
        int rangeTo = (int) Math.floor((bucketIndex + 1) * bucketSize) + 1;
        // first point of triangle
        QuantityValue triangleLeft = data[pointIndex];
        if (triangleLeft.isNoDataValue()) {
            addNodataValue(sampled, triangleLeft.getTimestamp());
            pointIndex = rangeTo - 1;
            continue;
        }
        // last point of triangle (next bucket's average)
        BucketAverage triangleRight = calculateBucketAverage(bucketIndex + 1, bucketSize, data);
        // init fallback value
        BucketAverage avgCurrentBucket = calculateBucketAverage(bucketIndex, bucketSize, data);
        long fallBackTimestamp = avgCurrentBucket.toTimeseriesValue().getTimestamp();
        QuantityValue maxAreaPoint = new QuantityValue(fallBackTimestamp, null);
        double area;
        int amountOfNodataValues = 0;
        double maxArea = area = -1;
        int nextPointIndex = 0;
        for (; rangeOff < rangeTo; rangeOff++) {
            //if (triangleRight.isNoDataBucket()) {
            //  triangleRight = // TODO
            //}
            // calculate triangle area over three buckets
            final QuantityValue triangleMiddle = data[rangeOff];
            if (triangleMiddle.isNoDataValue()) {
                amountOfNodataValues++;
                if (isExceededGapThreshold(amountOfNodataValues, bucketSize)) {
                    if (triangleMiddle.isNoDataValue()) {
                        maxAreaPoint = avgCurrentBucket.toTimeseriesValue();
                        LOGGER.debug("No data value for bucket {}.", bucketIndex);
                        pointIndex = rangeTo - 1;
                        break;
                    }
                }
            } else {
                area = calcTriangleArea(triangleLeft, triangleRight, triangleMiddle);
                if (area > maxArea) {
                    maxArea = area;
                    maxAreaPoint = triangleMiddle;
                    nextPointIndex = rangeOff;
                }
            }
        }
        // Pick this point from the Bucket
        sampled.addValues(maxAreaPoint);
        // This a is the next a
        pointIndex = nextPointIndex;
    }
    // Always add last value
    sampled.addValues(data[dataLength - 1]);
    return sampled;
}
Also used : QuantityValue(org.n52.io.response.dataset.quantity.QuantityValue) QuantityData(org.n52.io.response.dataset.quantity.QuantityData)

Example 3 with Data

use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.

the class ChartIoHandler method encodeAndWriteTo.

@Override
public void encodeAndWriteTo(DataCollection<QuantityData> data, OutputStream stream) throws IoParseException {
    try {
        writeDataToChart(data);
        ImageIO.write(createImage(), mimeType.getFormatName(), stream);
    } catch (IOException e) {
        throw new IoParseException("Could not write image to output stream.", e);
    }
}
Also used : IoParseException(org.n52.io.IoParseException) IOException(java.io.IOException)

Example 4 with Data

use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.

the class DouglasPeuckerGeneralizer method generalize.

@Override
public DataCollection<QuantityData> generalize(DataCollection<QuantityData> data) throws GeneralizerException {
    TvpDataCollection<QuantityData> generalizedDataCollection = new TvpDataCollection<>();
    for (String timeseriesId : data.getAllSeries().keySet()) {
        QuantityData timeseries = data.getSeries(timeseriesId);
        generalizedDataCollection.addNewSeries(timeseriesId, generalize(timeseries));
    }
    return generalizedDataCollection;
}
Also used : TvpDataCollection(org.n52.io.series.TvpDataCollection) QuantityData(org.n52.io.response.dataset.quantity.QuantityData)

Example 5 with Data

use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.

the class PDFReportGenerator method generateTimeseriesMetadata.

private void generateTimeseriesMetadata() {
    for (DatasetOutput metadata : getSeriesMetadatas()) {
        TimeSeries timeseries = addTimeseries(metadata);
        // addDataTable(timeseries, metadata, data);
        addMetadata(timeseries, metadata);
    }
}
Also used : TimeSeries(org.n52.oxf.DocumentStructureType.TimeSeries) DatasetOutput(org.n52.io.response.dataset.DatasetOutput)

Aggregations

IoParameters (org.n52.io.request.IoParameters)25 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)18 Data (org.n52.io.response.dataset.Data)13 IOException (java.io.IOException)12 QuantityValue (org.n52.io.response.dataset.quantity.QuantityValue)11 HashMap (java.util.HashMap)6 RequestSimpleParameterSet (org.n52.io.request.RequestSimpleParameterSet)6 QuantityData (org.n52.io.response.dataset.quantity.QuantityData)6 InputStream (java.io.InputStream)5 XmlObject (org.apache.xmlbeans.XmlObject)5 IoParseException (org.n52.io.IoParseException)5 File (java.io.File)4 Test (org.junit.Test)4 InternalServerException (org.n52.web.exception.InternalServerException)4 ModelAndView (org.springframework.web.servlet.ModelAndView)4 BigDecimal (java.math.BigDecimal)3 ElasticsearchAwareTest (org.n52.iceland.statistics.basetests.ElasticsearchAwareTest)3 ResultTimeClassifiedData (org.n52.io.format.ResultTimeClassifiedData)3 RawDataService (org.n52.series.spi.srv.RawDataService)3 DecodingException (org.n52.svalbard.decode.exception.DecodingException)3