All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ucar.nc2.ft2.coverage.writer.CoverageSubsetter2 Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 1998-2018 John Caron and University Corporation for Atmospheric Research/Unidata
 * See LICENSE for license information.
 */
package ucar.nc2.ft2.coverage.writer;

import ucar.nc2.AttributeContainerMutable;
import ucar.nc2.ft2.coverage.*;
import java.util.*;

/**
 * Helper class to create logical subsets.
 * Used by CFGridCoverageWriter
 *
 * @author caron
 * @since 7/12/2015
 */
public class CoverageSubsetter2 {

  public static ucar.nc2.util.Optional makeCoverageDatasetSubset(CoverageCollection org,
      List gridsWanted, SubsetParams params) {

    // Get subset of original objects that are needed by the requested grids
    List orgCoverages = new ArrayList<>();
    Map orgCoordSys = new HashMap<>(); // eliminate duplicates
    Set coordTransformSet = new HashSet<>(); // eliminate duplicates

    for (String gridName : gridsWanted) {
      Coverage orgGrid = org.findCoverage(gridName);
      if (orgGrid == null)
        continue;
      orgCoverages.add(orgGrid);
      CoverageCoordSys cs = orgGrid.getCoordSys();
      orgCoordSys.put(cs.getName(), cs);
      coordTransformSet.addAll(cs.getTransformNames());
    }

    // LOOK bail out if any fail, make more robust
    // subset all coordSys, and eliminate duplicate axes.
    Map subsetCoordAxes = new HashMap<>();
    Map subsetCFCoordSys = new HashMap<>();
    for (CoverageCoordSys orgCs : orgCoordSys.values()) {
      // subsetCF make do some CF tweaks, not needed in regular subset
      ucar.nc2.util.Optional opt = orgCs.subset(params, true, false);
      if (!opt.isPresent()) {
        return ucar.nc2.util.Optional.empty(opt.getErrorMessage());
      }

      CoverageCoordSys subsetCoordSys = opt.get();
      subsetCFCoordSys.put(orgCs.getName(), subsetCoordSys);
      for (CoverageCoordAxis axis : subsetCoordSys.getAxes()) {
        subsetCoordAxes.put(axis.getName(), axis); // eliminate duplicates
      }
    }

    // here are the objects we need to make the subsetted dataset
    List coordSys = new ArrayList<>();
    List coordAxes = new ArrayList<>();
    List coverages = new ArrayList<>();
    List coordTransforms = new ArrayList<>();

    coordSys.addAll(subsetCFCoordSys.values());

    // must use a copy, because of setDataset()
    coordAxes.addAll(subsetCoordAxes.values());

    for (Coverage orgCov : orgCoverages) {
      // must substitute subsetCS
      CoverageCoordSys subsetCs = subsetCFCoordSys.get(orgCov.getCoordSysName());
      coverages.add(new Coverage(orgCov, subsetCs)); // must use a copy, because of setCoordSys()
    }

    for (String tname : coordTransformSet) {
      CoverageTransform t = org.findCoordTransform(tname); // these are truly immutable, so can use originals
      if (t != null)
        coordTransforms.add(t);
    }

    // put it all together
    return ucar.nc2.util.Optional.of(new CoverageCollection(org.getName(), org.getCoverageType(),
        new AttributeContainerMutable(org.getName(), org.getGlobalAttributes()), null, null, null, coordSys,
        coordTransforms, coordAxes, coverages, org.getReader())); // use org.reader -> subset always in coord space !
  }

  CoverageCoordAxis1D findIndependentAxis(String want, List axes) {
    String name = want == null ? null : want.trim();
    for (CoverageCoordAxis axis : axes)
      if (axis instanceof CoverageCoordAxis1D && axis.getName().equalsIgnoreCase(name))
        return (CoverageCoordAxis1D) axis;
    return null;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy