Search in sources :

Example 31 with Range

use of ucar.ma2.Range in project geowave by locationtech.

the class GeoWaveStatisticsIT method testAddStatisticWithBinningStrategy.

@Test
public void testAddStatisticWithBinningStrategy() {
    DataStore ds = dataStore.createDataStore();
    NumericRangeStatistic longitudeRange = new NumericRangeStatistic(SimpleIngest.FEATURE_NAME, "Longitude");
    // binning by the same as the statistic should be easy to sanity check
    longitudeRange.setBinningStrategy(new NumericRangeFieldValueBinningStrategy("Longitude"));
    NumericRangeStatistic latitudeRange = new NumericRangeStatistic(SimpleIngest.FEATURE_NAME, "Latitude");
    latitudeRange.setBinningStrategy(new NumericRangeFieldValueBinningStrategy(45, "Latitude"));
    TimeRangeStatistic timeRangeHourBin = new TimeRangeStatistic(SimpleIngest.FEATURE_NAME, "TimeStamp");
    timeRangeHourBin.setBinningStrategy(new TimeRangeFieldValueBinningStrategy(Unit.HOUR, "TimeStamp"));
    timeRangeHourBin.setTag("hour");
    TimeRangeStatistic timeRangeDayBin = new TimeRangeStatistic(SimpleIngest.FEATURE_NAME, "TimeStamp");
    timeRangeDayBin.setBinningStrategy(new TimeRangeFieldValueBinningStrategy(Unit.DAY, "TimeStamp"));
    timeRangeDayBin.setTag("day");
    TimeRangeStatistic timeRangeWeekBin = new TimeRangeStatistic(SimpleIngest.FEATURE_NAME, "TimeStamp");
    timeRangeWeekBin.setBinningStrategy(new TimeRangeFieldValueBinningStrategy(Unit.WEEK, "TimeStamp"));
    timeRangeWeekBin.setTag("week");
    TimeRangeStatistic timeRangeMonthBin = new TimeRangeStatistic(SimpleIngest.FEATURE_NAME, "TimeStamp");
    timeRangeMonthBin.setBinningStrategy(new TimeRangeFieldValueBinningStrategy(Unit.MONTH, "TimeStamp"));
    timeRangeMonthBin.setTag("month");
    TimeRangeStatistic timeRangeYearBin = new TimeRangeStatistic(SimpleIngest.FEATURE_NAME, "TimeStamp");
    timeRangeYearBin.setBinningStrategy(new TimeRangeFieldValueBinningStrategy(Unit.YEAR, "TimeStamp"));
    timeRangeYearBin.setTag("year");
    CountStatistic countByGridUsingMultifield = new CountStatistic(SimpleIngest.FEATURE_NAME);
    countByGridUsingMultifield.setTag("multifield-latlon");
    countByGridUsingMultifield.setBinningStrategy(new NumericRangeFieldValueBinningStrategy(45, "Latitude", "Longitude"));
    CountStatistic countByGridUsingComposite = new CountStatistic(SimpleIngest.FEATURE_NAME);
    countByGridUsingComposite.setTag("composite-latlon");
    countByGridUsingComposite.setBinningStrategy(new CompositeBinningStrategy(new NumericRangeFieldValueBinningStrategy(45, 22.5, "Latitude"), new NumericRangeFieldValueBinningStrategy(90, 45, "Longitude")));
    long min = Long.MAX_VALUE, max = Long.MIN_VALUE;
    try (CloseableIterator<SimpleFeature> it = ds.query(VectorQueryBuilder.newBuilder().build())) {
        while (it.hasNext()) {
            final long time = ((Date) it.next().getAttribute("TimeStamp")).getTime();
            min = Math.min(min, time);
            max = Math.max(max, time);
        }
    }
    final Interval overallInterval = Interval.of(Instant.ofEpochMilli(min), Instant.ofEpochMilli(max));
    ds.addStatistic(longitudeRange, latitudeRange, timeRangeHourBin, timeRangeDayBin, timeRangeWeekBin, timeRangeMonthBin, timeRangeYearBin, countByGridUsingMultifield, countByGridUsingComposite);
    // let's make sure seralization/deserialization works for stats
    ds = dataStore.createDataStore();
    longitudeRange = (NumericRangeStatistic) ds.getFieldStatistic(longitudeRange.getStatisticType(), longitudeRange.getTypeName(), longitudeRange.getFieldName(), longitudeRange.getTag());
    latitudeRange = (NumericRangeStatistic) ds.getFieldStatistic(latitudeRange.getStatisticType(), latitudeRange.getTypeName(), latitudeRange.getFieldName(), latitudeRange.getTag());
    timeRangeHourBin = (TimeRangeStatistic) ds.getFieldStatistic(timeRangeHourBin.getStatisticType(), timeRangeHourBin.getTypeName(), timeRangeHourBin.getFieldName(), timeRangeHourBin.getTag());
    timeRangeDayBin = (TimeRangeStatistic) ds.getFieldStatistic(timeRangeDayBin.getStatisticType(), timeRangeDayBin.getTypeName(), timeRangeDayBin.getFieldName(), timeRangeDayBin.getTag());
    timeRangeWeekBin = (TimeRangeStatistic) ds.getFieldStatistic(timeRangeWeekBin.getStatisticType(), timeRangeWeekBin.getTypeName(), timeRangeWeekBin.getFieldName(), timeRangeWeekBin.getTag());
    timeRangeMonthBin = (TimeRangeStatistic) ds.getFieldStatistic(timeRangeMonthBin.getStatisticType(), timeRangeMonthBin.getTypeName(), timeRangeMonthBin.getFieldName(), timeRangeMonthBin.getTag());
    timeRangeYearBin = (TimeRangeStatistic) ds.getFieldStatistic(timeRangeYearBin.getStatisticType(), timeRangeYearBin.getTypeName(), timeRangeYearBin.getFieldName(), timeRangeYearBin.getTag());
    countByGridUsingMultifield = (CountStatistic) ds.getDataTypeStatistic(countByGridUsingMultifield.getStatisticType(), countByGridUsingMultifield.getTypeName(), countByGridUsingMultifield.getTag());
    countByGridUsingComposite = (CountStatistic) ds.getDataTypeStatistic(countByGridUsingComposite.getStatisticType(), countByGridUsingComposite.getTypeName(), countByGridUsingComposite.getTag());
    Range<Double> rangeValue = ds.getStatisticValue(longitudeRange);
    assertEquals(-165.0, rangeValue.getMinimum(), 0.1);
    assertEquals(180.0, rangeValue.getMaximum(), 0.1);
    rangeValue = ds.getStatisticValue(latitudeRange);
    assertEquals(-90.0, rangeValue.getMinimum(), 0.1);
    assertEquals(85.0, rangeValue.getMaximum(), 0.1);
    // Verify count statistic exists
    final Statistic<CountValue> countStat = ds.getDataTypeStatistic(CountStatistic.STATS_TYPE, SimpleIngest.FEATURE_NAME, Statistic.INTERNAL_TAG);
    assertNotNull(countStat);
    // Verify value exists
    Long countValue = ds.getStatisticValue(countStat);
    assertEquals(new Long(20), countValue);
    countValue = ds.getStatisticValue(countByGridUsingMultifield);
    assertEquals(new Long(20), countValue);
    countValue = ds.getStatisticValue(countByGridUsingComposite);
    assertEquals(new Long(20), countValue);
    try (CloseableIterator<Pair<ByteArray, Range<Double>>> iterator = ds.getBinnedStatisticValues(longitudeRange)) {
        int count = 0;
        while (iterator.hasNext()) {
            final Pair<ByteArray, Range<Double>> binValue = iterator.next();
            final Range<Double> binRange = ((NumericRangeFieldValueBinningStrategy) longitudeRange.getBinningStrategy()).getRange(binValue.getKey());
            assertEquals(1, binRange.getMaximum() - binRange.getMinimum(), 0.1);
            assertTrue(binRange.containsRange(binValue.getValue()));
            count++;
        }
        assertEquals(20, count);
    }
    try (CloseableIterator<Pair<ByteArray, Range<Double>>> iterator = ds.getBinnedStatisticValues(latitudeRange)) {
        int count = 0;
        while (iterator.hasNext()) {
            final Pair<ByteArray, Range<Double>> binValue = iterator.next();
            final Range<Double> binRange = ((NumericRangeFieldValueBinningStrategy) latitudeRange.getBinningStrategy()).getRange(binValue.getKey());
            assertEquals(45, binRange.getMaximum() - binRange.getMinimum(), 0.1);
            assertTrue(binRange.containsRange(binValue.getValue()));
            count++;
        }
        assertEquals(4, count);
    }
    try (CloseableIterator<Pair<ByteArray, Range<Double>>> iterator = ds.getBinnedStatisticValues(latitudeRange)) {
        int count = 0;
        while (iterator.hasNext()) {
            final Pair<ByteArray, Range<Double>> binValue = iterator.next();
            final Range<Double> binRange = ((NumericRangeFieldValueBinningStrategy) latitudeRange.getBinningStrategy()).getRange(binValue.getKey());
            assertEquals(45, binRange.getMaximum() - binRange.getMinimum(), 0.1);
            assertTrue(binRange.containsRange(binValue.getValue()));
            count++;
        }
        assertEquals(4, count);
    }
    assertTimeBinning(ds, timeRangeHourBin, 20, (i) -> Duration.ofHours(1L), overallInterval);
    assertTimeBinning(ds, timeRangeDayBin, 20, (i) -> Duration.ofDays(1L), overallInterval);
    assertTimeBinning(ds, timeRangeWeekBin, 20, (i) -> Duration.ofDays(7L), overallInterval);
    assertTimeBinning(ds, timeRangeMonthBin, 12, (i) -> {
        final Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(i.getStart().toEpochMilli());
        return Duration.ofDays(cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    }, overallInterval);
    assertTimeBinning(ds, timeRangeYearBin, 1, (i) -> {
        final Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(i.getStart().toEpochMilli());
        return Duration.ofDays(cal.getActualMaximum(Calendar.DAY_OF_YEAR));
    }, overallInterval);
    final Set<ByteArray> multiFieldFilteredExpectedResults = new HashSet<>();
    int multiFieldFilteredExpectedCount = 0;
    try (CloseableIterator<Pair<ByteArray, Long>> iterator = ds.getBinnedStatisticValues(countByGridUsingMultifield)) {
        int count = 0;
        while (iterator.hasNext()) {
            final Pair<ByteArray, Long> binValue = iterator.next();
            final Map<String, Range<Double>> rangePerField = ((NumericRangeFieldValueBinningStrategy) countByGridUsingMultifield.getBinningStrategy()).getRanges(binValue.getKey());
            assertEquals(1L, binValue.getValue().longValue());
            assertEquals(2, rangePerField.size());
            final Range<Double> latRange = rangePerField.get("Latitude");
            final Range<Double> lonRange = rangePerField.get("Longitude");
            // this ensures the interval is 45
            assertEquals(45, latRange.getMaximum() - latRange.getMinimum(), 0.1);
            assertEquals(45, lonRange.getMaximum() - lonRange.getMinimum(), 0.1);
            // this ensures the offset is 0
            assertEquals(0.0, latRange.getMinimum() % 45.0, 0.1);
            assertEquals(0.0, lonRange.getMinimum() % 45.0, 0.1);
            if (latRange.isOverlappedBy(Range.is(12.0)) && lonRange.isOverlappedBy(Range.between(-89.0, 89.0))) {
                multiFieldFilteredExpectedResults.add(binValue.getKey());
                multiFieldFilteredExpectedCount += binValue.getValue();
            }
            count++;
        }
        assertEquals(20, count);
    }
    // now query by object constraints on the gridded bins
    try (CloseableIterator<Pair<ByteArray, Long>> iterator = ds.getBinnedStatisticValues(countByGridUsingMultifield, BinConstraints.ofObject(new Pair[] { Pair.of("Latitude", Double.valueOf(12.0)), Pair.of("Longitude", Range.between(-89.0, 89.0)) }))) {
        final Set<ByteArray> multiFieldFilteredActualResults = new HashSet<>();
        int count = 0;
        while (iterator.hasNext()) {
            final Pair<ByteArray, Long> binValue = iterator.next();
            final Map<String, Range<Double>> rangePerField = ((NumericRangeFieldValueBinningStrategy) countByGridUsingMultifield.getBinningStrategy()).getRanges(binValue.getKey());
            assertEquals(1L, binValue.getValue().longValue());
            assertEquals(2, rangePerField.size());
            final Range<Double> latRange = rangePerField.get("Latitude");
            final Range<Double> lonRange = rangePerField.get("Longitude");
            // this ensures the interval is 45
            assertEquals(0.0, latRange.getMinimum(), 0.1);
            assertEquals(45.0, latRange.getMaximum(), 0.1);
            assertEquals(45, lonRange.getMaximum() - lonRange.getMinimum(), 0.1);
            assertTrue(lonRange.getMaximum() < 90.1);
            assertTrue(lonRange.getMinimum() > -90.1);
            // this ensures the offset is 0
            assertEquals(0.0, latRange.getMinimum() % 45.0, 0.1);
            assertEquals(0.0, lonRange.getMinimum() % 45.0, 0.1);
            count += binValue.getValue();
            multiFieldFilteredActualResults.add(binValue.getKey());
        }
        assertEquals(multiFieldFilteredExpectedCount, count);
        assertTrue(multiFieldFilteredExpectedResults.containsAll(multiFieldFilteredActualResults));
        assertTrue(multiFieldFilteredActualResults.containsAll(multiFieldFilteredExpectedResults));
    }
    final Set<ByteArray> compositeFilteredExpectedResults = new HashSet<>();
    int compositeFilteredExpectedCount = 0;
    try (CloseableIterator<Pair<ByteArray, Long>> iterator = ds.getBinnedStatisticValues(countByGridUsingComposite)) {
        int count = 0;
        int totalCount = 0;
        while (iterator.hasNext()) {
            final Pair<ByteArray, Long> binValue = iterator.next();
            totalCount += binValue.getValue();
            final Pair<StatisticBinningStrategy, ByteArray>[] bins = ((CompositeBinningStrategy) countByGridUsingComposite.getBinningStrategy()).getSubBins(binValue.getKey());
            assertEquals(2, bins.length);
            final Range<Double> latRange = ((NumericRangeFieldValueBinningStrategy) bins[0].getLeft()).getRange(bins[0].getRight());
            final Range<Double> lonRange = ((NumericRangeFieldValueBinningStrategy) bins[1].getLeft()).getRange(bins[1].getRight());
            // this ensures the interval is 45 and 90 respectively
            assertEquals(45, latRange.getMaximum() - latRange.getMinimum(), 0.1);
            assertEquals(90, lonRange.getMaximum() - lonRange.getMinimum(), 0.1);
            // this ensures the offset is 22.5 and 45 respectively
            assertEquals(22.5, Math.abs(latRange.getMinimum() % 45.0), 0.1);
            assertEquals(45.0, Math.abs(lonRange.getMinimum() % 90.0), 0.1);
            count++;
            if (latRange.isOverlappedBy(Range.between(-44.0, 44.0)) && lonRange.isOverlappedBy(Range.between(-179.0, 89.0))) {
                compositeFilteredExpectedResults.add(binValue.getKey());
                compositeFilteredExpectedCount += binValue.getValue();
            }
        }
        assertEquals(16, count);
        assertEquals(20, totalCount);
    }
    try (CloseableIterator<Pair<ByteArray, Long>> iterator = ds.getBinnedStatisticValues(countByGridUsingComposite, BinConstraints.ofObject(new Range[] { Range.between(-44.0, 44.0), Range.between(-179.0, 89.0) }))) {
        final Set<ByteArray> compositeFilteredActualResults = new HashSet<>();
        int totalCount = 0;
        while (iterator.hasNext()) {
            final Pair<ByteArray, Long> binValue = iterator.next();
            totalCount += binValue.getValue();
            final Pair<StatisticBinningStrategy, ByteArray>[] bins = ((CompositeBinningStrategy) countByGridUsingComposite.getBinningStrategy()).getSubBins(binValue.getKey());
            assertEquals(2, bins.length);
            final Range<Double> latRange = ((NumericRangeFieldValueBinningStrategy) bins[0].getLeft()).getRange(bins[0].getRight());
            final Range<Double> lonRange = ((NumericRangeFieldValueBinningStrategy) bins[1].getLeft()).getRange(bins[1].getRight());
            // this ensures the interval is 45 and 90 respectively
            assertEquals(45, latRange.getMaximum() - latRange.getMinimum(), 0.1);
            assertEquals(90, lonRange.getMaximum() - lonRange.getMinimum(), 0.1);
            // this ensures the offset is 22.5 and 45 respectively
            assertEquals(22.5, Math.abs(latRange.getMinimum() % 45.0), 0.1);
            assertEquals(45.0, Math.abs(lonRange.getMinimum() % 90.0), 0.1);
            assertTrue(latRange.getMaximum() < 67.6);
            assertTrue(latRange.getMinimum() > -67.6);
            assertTrue(lonRange.getMaximum() < 135.1);
            assertTrue(lonRange.getMinimum() > -225.1);
            compositeFilteredActualResults.add(binValue.getKey());
        }
        assertTrue(compositeFilteredExpectedResults.containsAll(compositeFilteredActualResults));
        assertTrue(compositeFilteredActualResults.containsAll(compositeFilteredExpectedResults));
        assertEquals(compositeFilteredExpectedCount, totalCount);
    }
}
Also used : NumericRangeStatistic(org.locationtech.geowave.core.store.statistics.field.NumericRangeStatistic) TimeRangeFieldValueBinningStrategy(org.locationtech.geowave.core.geotime.store.statistics.binning.TimeRangeFieldValueBinningStrategy) CountValue(org.locationtech.geowave.core.store.statistics.adapter.CountStatistic.CountValue) DataStore(org.locationtech.geowave.core.store.api.DataStore) ByteArray(org.locationtech.geowave.core.index.ByteArray) CountStatistic(org.locationtech.geowave.core.store.statistics.adapter.CountStatistic) Pair(org.apache.commons.lang3.tuple.Pair) HashSet(java.util.HashSet) Calendar(java.util.Calendar) Range(org.apache.commons.lang3.Range) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Date(java.util.Date) NumericRangeFieldValueBinningStrategy(org.locationtech.geowave.core.store.statistics.binning.NumericRangeFieldValueBinningStrategy) CompositeBinningStrategy(org.locationtech.geowave.core.store.statistics.binning.CompositeBinningStrategy) TimeRangeStatistic(org.locationtech.geowave.core.geotime.store.statistics.TimeRangeStatistic) Interval(org.threeten.extra.Interval) Test(org.junit.Test)

Example 32 with Range

use of ucar.ma2.Range in project gridfour by gwlucastrig.

the class PackageData method process.

void process(PrintStream ps, TestOptions options, String[] args) throws IOException {
    // The packaging of data in a Gvrs file can be thought of in terms of
    // the steps shown below.
    // 
    // 0.  Obtain descriptive parameters about source data.  In this
    // case, the application is packing data from a NetCDF source
    // and most of the descriptive parameters follow the pattern
    // established in the earlier ExtractData.java demonstration
    // 
    // 1.  Define the fixed metadata about the file (it's dimensions,
    // data type, tile organization, etc.) using a GvrsFileSpecification
    // object.
    // 
    // 2.  Open a new GvrsFile object using the settings created in step 1.
    // Adjust any run-time parameters (such as the tile-cache size)
    // according to the needs of the application.
    // 
    // 3.  Extract the data from its source and store in the Gvrs file.
    // 
    ps.format("%nGvrs Packaging Application for NetCDF-format Global DEM files%n");
    Locale locale = Locale.getDefault();
    Date date = new Date();
    SimpleDateFormat sdFormat = new SimpleDateFormat("dd MMM yyyy HH:mm z", locale);
    ps.format("Date of Execution: %s%n", sdFormat.format(date));
    String inputPath = options.getInputFile().getPath();
    File outputFile = options.getOutputFile();
    if (outputFile == null) {
        ps.format("Missing specification for output file%n");
        ps.format("Packaging application terminated%n");
        return;
    }
    ps.format("Input file:  %s%n", inputPath);
    ps.format("Output file: %s%n", outputFile.getPath());
    boolean[] matched = new boolean[args.length];
    boolean useLsop = options.scanBooleanOption(args, "-lsop", matched, false);
    // Open the NetCDF file -----------------------------------
    ps.println("Opening NetCDF input file");
    NetcdfFile ncfile = NetcdfFile.open(inputPath);
    // Identify which Variable instances carry information about the
    // geographic (latitude/longitude) coordinate system and also which
    // carry information for elevation and bathymetry.
    // the Variable that carries row-latitude information
    Variable lat;
    // the Variable that carries column-longitude information
    Variable lon;
    // the variable that carries elevation and bathymetry
    Variable z;
    lat = ncfile.findVariable("lat");
    lon = ncfile.findVariable("lon");
    z = ncfile.findVariable("elevation");
    int[] tileSize;
    // Use the input file name to format a product label
    File inputFile = new File(inputPath);
    String productLabel = inputFile.getName();
    if (productLabel.toLowerCase().endsWith(".nc")) {
        productLabel = productLabel.substring(0, productLabel.length() - 3);
    }
    if (lat == null) {
        // ETOPO1 specification
        tileSize = options.getTileSize(90, 120);
        lat = ncfile.findVariable("y");
        lon = ncfile.findVariable("x");
        z = ncfile.findVariable("z");
    } else {
        tileSize = options.getTileSize(90, 120);
    }
    if (lat == null || lon == null || z == null) {
        throw new IllegalArgumentException("Input does not contain valid lat,lon, and elevation Variables");
    }
    // using the variables from above, extract coordinate system
    // information for the product and print it to the output.
    ExtractionCoordinates extractionCoords = new ExtractionCoordinates(lat, lon);
    extractionCoords.summarizeCoordinates(ps);
    // Get the dimensions of the raster (grid) elevation/bathymetry data.
    // should be 1.
    int rank = z.getRank();
    int[] shape = z.getShape();
    int nRows = shape[0];
    int nCols = shape[1];
    ps.format("Rows:      %8d%n", nRows);
    ps.format("Columns:   %8d%n", nCols);
    int nRowsInTile = tileSize[0];
    int nColsInTile = tileSize[1];
    // Initialize the specification used to initialize the Gvrs file -------
    GvrsFileSpecification spec = new GvrsFileSpecification(nRows, nCols, nRowsInTile, nColsInTile);
    spec.setLabel(productLabel);
    // Initialize the data type.  If a zScale option was specified,
    // use integer-coded floats.  Otherwise, pick the data type
    // based on whether the NetCDF file gives integral or floating point
    // data.
    boolean isZScaleSpecified = options.isZScaleSpecified();
    float zScale = (float) options.getZScale();
    float zOffset = (float) options.getZOffset();
    // data type from NetCDF file
    DataType sourceDataType = z.getDataType();
    GvrsElementSpecification elementSpec = null;
    GvrsElementType gvrsDataType;
    if (isZScaleSpecified) {
        // the options define our data type
        int encodedLimitDepth = (int) ((LIMIT_DEPTH - zOffset) * zScale);
        int encodedLimitElev = (int) ((LIMIT_ELEVATION - zOffset) * zScale);
        elementSpec = new GvrsElementSpecificationIntCodedFloat("z", zScale, zOffset, encodedLimitDepth, encodedLimitElev, Integer.MIN_VALUE, true);
        spec.addElementSpecification(elementSpec);
        gvrsDataType = GvrsElementType.INT_CODED_FLOAT;
    } else if (sourceDataType.isIntegral()) {
        elementSpec = new GvrsElementSpecificationShort("z", LIMIT_DEPTH, LIMIT_ELEVATION, FILL_VALUE);
        spec.addElementSpecification(elementSpec);
        gvrsDataType = GvrsElementType.SHORT;
    } else {
        elementSpec = new GvrsElementSpecificationFloat("z", LIMIT_DEPTH, LIMIT_ELEVATION, Float.NaN);
        spec.addElementSpecification(elementSpec);
        gvrsDataType = GvrsElementType.FLOAT;
    }
    elementSpec.setDescription("Elevation (positive values) or depth (negative), in meters");
    elementSpec.setUnitOfMeasure("m");
    // Example with special character
    elementSpec.setLabel("die H\u00f6henlage");
    ps.println("Source date type " + sourceDataType + ", stored as " + gvrsDataType);
    ps.println("");
    // Determine whether data compression is used -------------------
    boolean compressionEnabled = options.isCompressionEnabled();
    spec.setDataCompressionEnabled(compressionEnabled);
    boolean checksumsEnalbed = options.isChecksumComputationEnabled();
    spec.setChecksumEnabled(checksumsEnalbed);
    boolean bigAddressSpaceEnabled = options.isBigAddressSpaceEnabled();
    spec.setExtendedFileSizeEnabled(bigAddressSpaceEnabled);
    double[] geoCoords = extractionCoords.getGeographicCoordinateBounds();
    spec.setGeographicCoordinates(geoCoords[0], geoCoords[1], geoCoords[2], geoCoords[3]);
    // Check to verify that the geographic coordinates and grid coordinate
    // are correctly implemented. This test is not truly part of the packaging
    // process (since it should always work), but is included here as a
    // diagnostic.
    extractionCoords.checkSpecificationTransform(ps, spec);
    // is enabled and the data type is integral.
    if (useLsop) {
        LsCodecUtility.addLsopToSpecification(spec, false);
    }
    // Create the output file and store the content from the input file.
    if (outputFile.exists()) {
        ps.println("Output file exists. Removing old file");
        boolean status = outputFile.delete();
        if (!status) {
            ps.println("Removal attempt failed");
            return;
        }
    }
    ps.println("Begin processing");
    double zMin = Double.POSITIVE_INFINITY;
    double zMax = Double.NEGATIVE_INFINITY;
    double zSum = 0;
    long nSum = 0;
    try (GvrsFile gvrs = new GvrsFile(outputFile, spec)) {
        gvrs.writeMetadata(GvrsMnc.Copyright, "This data is in the public domain and may be used free of charge");
        gvrs.writeMetadata(GvrsMnc.TermsOfUse, "This data should not be used for navigation");
        GvrsElement zElement = gvrs.getElement("z");
        gvrs.setTileCacheSize(GvrsCacheSize.Large);
        storeGeoreferencingInformation(gvrs);
        // Initialize data-statistics collection ---------------------------
        // we happen to know the range of values for the global DEM a-priori.
        // it ranges from about -11000 to 8650.  This allows us to tabulate counts
        // of which values we find in the data source.  We can use this information
        // to estimate the entropy of the source data and make a realistic
        // assessment of how many bytes would be needed to store them.
        InputDataStatCollector stats = new InputDataStatCollector(-11000, 8650, zScale);
        int[] readOrigin = new int[rank];
        int[] readShape = new int[rank];
        // -----------------------------------------------------------------
        // Package the data
        long time0 = System.currentTimeMillis();
        for (int iRow = 0; iRow < nRows; iRow++) {
            if (iRow % 1000 == 999) {
                long time1 = System.currentTimeMillis();
                double deltaT = time1 - time0;
                // rows per millis
                double rate = (iRow + 1) / deltaT;
                int nRemaining = nRows - iRow;
                long remainingT = (long) (nRemaining / rate);
                Date d = new Date(time1 + remainingT);
                ps.format("Completed %d rows, %4.1f%% of total, est completion at %s%n", iRow + 1, 100.0 * (double) iRow / (nRows - 1.0), d);
                ps.flush();
            }
            int row0 = iRow;
            int col0 = 0;
            readOrigin[0] = row0;
            readOrigin[1] = col0;
            readShape[0] = 1;
            readShape[1] = nCols;
            // happen in this application unless the input file is corrupt.
            try {
                Array array = z.read(readOrigin, readShape);
                // and store it in the Gvrs file.
                switch(gvrsDataType) {
                    case INTEGER:
                    case SHORT:
                        for (int iCol = 0; iCol < nCols; iCol++) {
                            int sample = array.getInt(iCol);
                            zElement.writeValueInt(iRow, iCol, sample);
                            stats.addSample(sample);
                            if (sample < zMin) {
                                zMin = sample;
                            }
                            if (sample > zMax) {
                                zMax = sample;
                            }
                            zSum += sample;
                            nSum++;
                        }
                        break;
                    case INT_CODED_FLOAT:
                    case FLOAT:
                    default:
                        for (int iCol = 0; iCol < nCols; iCol++) {
                            float sample = array.getFloat(iCol);
                            zElement.writeValue(iRow, iCol, sample);
                            stats.addSample(sample);
                            if (sample < zMin) {
                                zMin = sample;
                            }
                            if (sample > zMax) {
                                zMax = sample;
                            }
                            zSum += sample;
                            nSum++;
                        }
                }
            } catch (InvalidRangeException irex) {
                throw new IOException(irex.getMessage(), irex);
            }
        }
        gvrs.flush();
        long time1 = System.currentTimeMillis();
        double timeToProcess = (time1 - time0) / 1000.0;
        ps.format("Finished processing file in %4.1f seconds%n", timeToProcess);
        ps.format("Entropy for input data %4.1f bits/sample%n", stats.getEntropy());
        long outputSize = outputFile.length();
        long nCells = (long) nRows * (long) nCols;
        double bitsPerSymbol = 8.0 * (double) outputSize / (double) nCells;
        ps.format("Storage used (including overhead) %6.4f bits/sample%n", bitsPerSymbol);
        ps.format("%nSummary of file content and packaging actions------------%n");
        gvrs.summarize(ps, true);
        ps.format("Range of z values:%n");
        ps.format("  Min z: %8.3f%n", zMin);
        ps.format("  Max z: %8.3f%n", zMax);
        ps.format("  Avg z: %8.3f%n", zSum / (nSum > 0 ? nSum : 1));
    }
    // to those of the source data.
    if (options.isVerificationEnabled()) {
        int[] readOrigin = new int[rank];
        int[] readShape = new int[rank];
        ps.println("\nTesting product for data consistency with source");
        ps.println("Opening gvrs file for reading");
        long time0 = System.currentTimeMillis();
        try (GvrsFile gvrs = new GvrsFile(outputFile, "r")) {
            long time1 = System.currentTimeMillis();
            ps.println("Opening complete in " + (time1 - time0) + " ms");
            GvrsFileSpecification testSpec = gvrs.getSpecification();
            String testLabel = testSpec.getLabel();
            ps.println("Label:     " + testLabel);
            GvrsMetadata m = gvrs.readMetadata("Copyright", 0);
            if (m != null) {
                ps.println("Copyright: " + m.getString());
            }
            GvrsElement zElement = gvrs.getElement("z");
            ps.println("Element:   " + zElement.getName() + ", " + zElement.getDescription());
            gvrs.setTileCacheSize(GvrsCacheSize.Large);
            for (int iRow = 0; iRow < nRows; iRow++) {
                if (iRow % 10000 == 9999) {
                    time1 = System.currentTimeMillis();
                    double deltaT = time1 - time0;
                    // rows per millis
                    double rate = (iRow + 1) / deltaT;
                    int nRemaining = nRows - iRow;
                    long remainingT = (long) (nRemaining / rate);
                    Date d = new Date(time1 + remainingT);
                    ps.format("Completed %d rows, %4.1f%% of total, est completion at %s%n", iRow + 1, 100.0 * (double) iRow / (nRows - 1.0), d);
                    ps.flush();
                }
                int row0 = iRow;
                int col0 = 0;
                readOrigin[0] = row0;
                readOrigin[1] = col0;
                readShape[0] = 1;
                readShape[1] = nCols;
                try {
                    Array array = z.read(readOrigin, readShape);
                    switch(gvrsDataType) {
                        case INTEGER:
                            for (int iCol = 0; iCol < nCols; iCol++) {
                                int sample = array.getInt(iCol);
                                int test = zElement.readValueInt(iRow, iCol);
                                if (sample != test) {
                                    ps.println("Failure at " + iRow + ", " + iCol);
                                    test = zElement.readValueInt(iRow, iCol);
                                    System.exit(-1);
                                }
                            }
                            break;
                        case INT_CODED_FLOAT:
                            for (int iCol = 0; iCol < nCols; iCol++) {
                                double sample = array.getDouble(iCol);
                                int iSample = (int) ((sample - zOffset) * zScale + 0.5);
                                float fSample = iSample / zScale + zOffset;
                                float test = zElement.readValue(iRow, iCol);
                                double delta = Math.abs(fSample - test);
                                if (delta > 1.01 / zScale) {
                                    ps.println("Failure at " + iRow + ", " + iCol);
                                    System.exit(-1);
                                }
                            }
                            break;
                        case FLOAT:
                        default:
                            for (int iCol = 0; iCol < nCols; iCol++) {
                                float sample = array.getFloat(iCol);
                                float test = zElement.readValue(iRow, iCol);
                                if (sample != test) {
                                    ps.println("Failure at " + iRow + ", " + iCol);
                                    test = zElement.readValueInt(iRow, iCol);
                                    System.exit(-1);
                                }
                            }
                    }
                } catch (InvalidRangeException irex) {
                    throw new IOException(irex.getMessage(), irex);
                }
            }
            time1 = System.currentTimeMillis();
            ps.println("Exhaustive cross check complete in " + (time1 - time0) + " ms");
            gvrs.summarize(ps, false);
        }
    }
    ncfile.close();
}
Also used : Locale(java.util.Locale) GvrsElementSpecificationFloat(org.gridfour.gvrs.GvrsElementSpecificationFloat) Variable(ucar.nc2.Variable) GvrsElement(org.gridfour.gvrs.GvrsElement) GvrsElementSpecificationShort(org.gridfour.gvrs.GvrsElementSpecificationShort) DataType(ucar.ma2.DataType) GvrsElementSpecification(org.gridfour.gvrs.GvrsElementSpecification) InvalidRangeException(ucar.ma2.InvalidRangeException) IOException(java.io.IOException) Date(java.util.Date) GvrsElementSpecificationIntCodedFloat(org.gridfour.gvrs.GvrsElementSpecificationIntCodedFloat) NetcdfFile(ucar.nc2.NetcdfFile) Array(ucar.ma2.Array) GvrsElementType(org.gridfour.gvrs.GvrsElementType) GvrsMetadata(org.gridfour.gvrs.GvrsMetadata) GvrsFileSpecification(org.gridfour.gvrs.GvrsFileSpecification) SimpleDateFormat(java.text.SimpleDateFormat) GvrsFile(org.gridfour.gvrs.GvrsFile) File(java.io.File) NetcdfFile(ucar.nc2.NetcdfFile) GvrsFile(org.gridfour.gvrs.GvrsFile)

Aggregations

Range (org.apache.commons.lang3.Range)14 Date (java.util.Date)11 Variable (ucar.nc2.Variable)10 Test (org.junit.jupiter.api.Test)9 Array (ucar.ma2.Array)9 InvalidRangeException (ucar.ma2.InvalidRangeException)8 Range (ucar.ma2.Range)8 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 Attribute (ucar.nc2.Attribute)6 Point (java.awt.Point)5 DataType (ucar.ma2.DataType)5 ArrayFloat (ucar.ma2.ArrayFloat)4 ArrayShort (ucar.ma2.ArrayShort)4 NetcdfDataset (ucar.nc2.dataset.NetcdfDataset)4 Range (controlP5.Range)3 Slider (controlP5.Slider)3 Toggle (controlP5.Toggle)3 Rectangle (java.awt.Rectangle)3 BandedSampleModel (java.awt.image.BandedSampleModel)3