/** Instances which have same contents are equal. Careful!! this is not object identity !! */ @Override public boolean equals(Object oo) { if (this == oo) return true; if (!(oo instanceof Dimension)) return false; Dimension other = (Dimension) oo; if ((g != null) && !g.equals(other.getGroup())) return false; if ((getName() == null) && (other.getName() != null)) return false; if ((getName() != null) && !getName().equals(other.getName())) return false; return (getLength() == other.getLength()) && (isUnlimited() == other.isUnlimited()) && (isVariableLength() == other.isVariableLength()) && (isShared() == other.isShared()); }
// look if the wanted dimension is in the unknownDims list. private Dimension checkUnknownDims( String wantDim, List<Dimension> unknownDims, Dimension oldDim, String location) { for (Dimension dim : unknownDims) { if (dim.getShortName().equals(wantDim)) { int len = oldDim.getLength(); if (len == 0) dim.setUnlimited(true); // allow zero length dimension !! dim.setLength(len); // use existing (anon) dimension Group parent = dim.getGroup(); parent.addDimensionIfNotExists(dim); // add to the parent unknownDims.remove(dim); // remove from list LOOK is this ok? log.warn("unknownDim {} length set to {}{}", wantDim, oldDim.getLength(), location); return dim; } } return null; }
/** * Parses a variable recursively into appropriate ViewVariable implementations * * @throws IOException * @throws */ private static AbstractViewVariable parseVariableRecursive(Variable var) throws IOException { List<Dimension> dimensions = var.getDimensions(); // A single dimension means we can parse a SimpleAxis if (dimensions.size() == 1) { SimpleAxis axis = new SimpleAxis(var.getName(), var.getDataType().name(), var.getUnitsString(), null, null); Dimension d = dimensions.get(0); axis.setDimensionBounds(new SimpleBounds(0, d.getLength())); // Read our first and last values Array first = null, last = null; try { first = var.read(new int[] {0}, new int[] {1}); last = var.read(new int[] {d.getLength() - 1}, new int[] {1}); } catch (InvalidRangeException ex) { throw new IllegalArgumentException( String.format("Unable to read variable ranges '%1$s'", var), ex); } axis.setValueBounds(new SimpleBounds(first.getDouble(0), last.getDouble(0))); return axis; // Otherwise we have a multi dimensional variable that we can parse as a grid } else if (dimensions.size() > 0) { SimpleGrid grid = new SimpleGrid(var.getName(), var.getDataType().name(), var.getUnitsString(), null); List<AbstractViewVariable> childAxes = new ArrayList<>(); // Recursively parse each dimension (which should map to a variable in the parent group) for (Dimension d : dimensions) { Variable mappedVariable = d.getGroup().findVariable(d.getName()); if (mappedVariable == null) { // If the dimension doesn't map to a variable, we can't pull much information out of it // So instead we'll have to introduce an axis that only includes dimension bounds log.warn( String.format( "Dimension '%1$s' has no matching variable in parent group '%2$s'", d, d.getGroup())); SimpleAxis axis = new SimpleAxis(d.getName(), DataType.FLOAT.name(), "????", null, null); axis.setDimensionBounds(new SimpleBounds(0, d.getLength() - 1)); childAxes.add(axis); } else { AbstractViewVariable parsedVar = parseVariableRecursive(mappedVariable); if (parsedVar != null) childAxes.add(parsedVar); } } if (childAxes.size() > 0) { grid.setAxes(childAxes.toArray(new AbstractViewVariable[childAxes.size()])); return grid; } else { return null; } } else { // Currently unsupported... log.warn( String.format("Variables with 0 dimensions are currently unsupported. var='%1$s'", var)); return null; } }