List indexedDimensionSlicing()

in src/main/java/org/apache/sdap/ningester/datatiler/SliceFileByDimension.java [62:91]


    List<String> indexedDimensionSlicing(File inputfile) throws IOException {

        // This is sort of a hack to help the python netcdf library. When you try to get dimensions by name and they are unnamed, the
        // python library uses 'phony_dim_' then the index of the dimension as the dimension name. Weird, I know.
        if (Strings.isNullOrEmpty(this.dimensionNamePrefix)) {
            this.dimensionNamePrefix = "phony_dim_";
        }
        Map<String, Integer> dimensionNameToLength;
        try (NetcdfDataset ds = NetcdfDataset.openDataset(inputfile.getAbsolutePath())) {


            // Because this is indexed-based dimension slicing, the dimensions are assumed to be unlimited with no names (ie. ds.dimensions == [])
            // Therefore, we need to find a 'representative' variable with dimensions that we can inspect and work with
            // 'lat' and 'lon' are common variable names in the datasets we work with. So try to find one of those first
            // Otherwise, just find the first variable that has the same number of dimensions as was given in this.dimensions
            List<String> commonVariableNames = Arrays.asList("lat", "latitude", "lon", "longitude");
            Optional<Variable> var = ds.getVariables().stream()
                    .filter(variable -> commonVariableNames.contains(variable.getShortName().toLowerCase())
                            || variable.getDimensions().size() == this.dimensions.size())
                    .findFirst();

            assert var.isPresent() : "Could not find a variable in " + inputfile.getName() + " with " + dimensions.size() + " dimension(s).";

            dimensionNameToLength = IntStream.range(0, this.dimensions.size()).boxed()
                    .collect(Collectors.toMap(dimIndex -> this.dimensionNamePrefix + dimIndex, dimIndex -> var.get().getDimension(dimIndex).getLength()));
        }

        return generateTileBoundrySlices(this.dimensionNamePrefix + this.sliceByDimension, dimensionNameToLength);

    }