public void readByte2Short() throws Exception { Variable t2 = null; assert (null != (t2 = ncfileRead.findVariable("t2"))); assert (t2.getDataType() == DataType.BYTE); Attribute att = t2.findAttribute(CDM.SCALE_FACTOR); assert (null != att); assert (!att.isArray()); assert (1 == att.getLength()); assert (2 == att.getNumericValue().doubleValue()); assert (DataType.SHORT == att.getDataType()); assert (null != (t2 = dsRead.findVariable("t2"))); assert t2 instanceof VariableEnhanced; VariableDS vs = (VariableDS) t2; assert (vs.getDataType() == DataType.SHORT) : vs.getDataType(); assert (!vs.hasMissing()); Array A = vs.read(); assert (A.getElementType() == short.class) : A.getElementType(); Index ima = A.getIndex(); int[] shape = A.getShape(); int i, j; for (i = 0; i < shape[0]; i++) { for (j = 0; j < shape[1]; j++) { assert (A.getShort(ima.set(i, j)) == (2 * (i * 10 + j) + 77)); } } System.out.println("**************TestStandardVar readByte2Short"); }
private void makeVariableNoCoords( NetcdfFile ncfile, int datatype, String shortName, String longName, Variable from) { Variable v = new Variable(ncfile, null, null, shortName); v.setDataType(DataType.BYTE); v.setDimensions(from.getDimensions()); ncfile.addVariable(null, v); v.addAttribute(new Attribute(CDM.UNITS, Cinrad2Record.getDatatypeUnits(datatype))); v.addAttribute(new Attribute(CDM.LONG_NAME, longName)); byte[] b = new byte[2]; b[0] = Cinrad2Record.MISSING_DATA; b[1] = Cinrad2Record.BELOW_THRESHOLD; Array missingArray = Array.factory(DataType.BYTE.getPrimitiveClassType(), new int[] {2}, b); v.addAttribute(new Attribute(CDM.MISSING_VALUE, missingArray)); v.addAttribute( new Attribute("signal_below_threshold", new Byte(Cinrad2Record.BELOW_THRESHOLD))); v.addAttribute( new Attribute(CDM.SCALE_FACTOR, new Float(Cinrad2Record.getDatatypeScaleFactor(datatype)))); v.addAttribute( new Attribute(CDM.ADD_OFFSET, new Float(Cinrad2Record.getDatatypeAddOffset(datatype)))); v.addAttribute(new Attribute(CDM.UNSIGNED, "true")); Attribute fromAtt = from.findAttribute(_Coordinate.Axes); v.addAttribute(new Attribute(_Coordinate.Axes, fromAtt)); Vgroup vgFrom = (Vgroup) from.getSPobject(); Vgroup vg = new Vgroup(datatype, vgFrom.map); v.setSPobject(vg); }
public void readDouble() throws Exception { Variable t1 = null; assert (null != (t1 = ncfileRead.findVariable("t1"))); assert (t1.getDataType() == DataType.DOUBLE); Attribute att = t1.findAttribute(CDM.SCALE_FACTOR); assert (null != att); assert (!att.isArray()); assert (1 == att.getLength()); assert (2.0 == att.getNumericValue().doubleValue()); assert (DataType.DOUBLE == att.getDataType()); // read Array A = t1.read(); int i, j; Index ima = A.getIndex(); int[] shape = A.getShape(); for (i = 0; i < shape[0]; i++) { for (j = 0; j < shape[1]; j++) { assert (A.getDouble(ima.set(i, j)) == (double) (i * 10.0 + j)); } } assert (null != (t1 = dsRead.findVariable("t1"))); assert t1 instanceof VariableEnhanced; VariableEnhanced dsVar = (VariableEnhanced) t1; assert (dsVar.getDataType() == DataType.DOUBLE); A = dsVar.read(); ima = A.getIndex(); shape = A.getShape(); for (i = 0; i < shape[0]; i++) { for (j = 0; j < shape[1]; j++) { assert (A.getDouble(ima.set(i, j)) == (2.0 * (i * 10.0 + j) + 77.0)); } } assert (null == t1.findAttribute(CDM.SCALE_FACTOR)); assert (null == t1.findAttribute("add_offset")); System.out.println("**************TestStandardVar ReadDouble"); }
public void testCoordVar(NetcdfFile ncfile) { Variable lat = ncfile.findVariable("lat"); assert null != lat; assert lat.getShortName().equals("lat"); assert lat.getRank() == 1; assert lat.getSize() == 3; assert lat.getShape()[0] == 3; assert lat.getDataType() == DataType.FLOAT; assert !lat.isUnlimited(); assert lat.getDimension(0).equals(ncfile.findDimension("lat")); Attribute att = lat.findAttribute("units"); assert null != att; assert !att.isArray(); assert att.isString(); assert att.getDataType() == DataType.STRING; assert att.getStringValue().equals("degrees_north"); assert att.getNumericValue() == null; assert att.getNumericValue(3) == null; try { Array data = lat.read(); assert data.getRank() == 1; assert data.getSize() == 3; assert data.getShape()[0] == 3; assert data.getElementType() == float.class; IndexIterator dataI = data.getIndexIterator(); assert TestUtils.close(dataI.getDoubleNext(), 41.0); assert TestUtils.close(dataI.getDoubleNext(), 40.0); assert TestUtils.close(dataI.getDoubleNext(), 39.0); } catch (IOException io) { } }
/** * Setup needed for all SingleTrajectoryObsDatatypes. Can only be called once. * * <p>Units of time varible must be udunits time units. Units of latitude variable must be * convertible to "degrees_north" by udunits. Units of longitude variable must be convertible to * "degrees_east" by udunits. Units of altitude variable must be convertible to "meters" by * udunits. * * @throws IllegalArgumentException if units of time, latitude, longitude, or altitude variables * are not as required. * @throws IllegalStateException if this method has already been called. */ public void setTrajectoryInfo(Config trajConfig) throws IOException { if (timeDim != null) throw new IllegalStateException("The setTrajectoryInfo() method can only be called once."); this.trajectoryId = trajConfig.getTrajectoryId(); this.timeDim = trajConfig.getTimeDim(); this.timeVar = trajConfig.getTimeVar(); this.latVar = trajConfig.getLatVar(); this.lonVar = trajConfig.getLonVar(); this.elevVar = trajConfig.getElevVar(); trajectoryNumPoint = this.timeDim.getLength(); timeVarUnitsString = this.timeVar.findAttribute("units").getStringValue(); // Check that time, lat, lon, elev units are acceptable. if (DateUnit.getStandardDate(timeVarUnitsString) == null) { throw new IllegalArgumentException( "Units of time variable <" + timeVarUnitsString + "> not a date unit."); } String latVarUnitsString = this.latVar.findAttribute("units").getStringValue(); if (!SimpleUnit.isCompatible(latVarUnitsString, "degrees_north")) { throw new IllegalArgumentException( "Units of lat var <" + latVarUnitsString + "> not compatible with \"degrees_north\"."); } String lonVarUnitsString = this.lonVar.findAttribute("units").getStringValue(); if (!SimpleUnit.isCompatible(lonVarUnitsString, "degrees_east")) { throw new IllegalArgumentException( "Units of lon var <" + lonVarUnitsString + "> not compatible with \"degrees_east\"."); } String elevVarUnitsString = this.elevVar.findAttribute("units").getStringValue(); if (!SimpleUnit.isCompatible(elevVarUnitsString, "meters")) { throw new IllegalArgumentException( "Units of elev var <" + elevVarUnitsString + "> not compatible with \"meters\"."); } try { elevVarUnitsConversionFactor = getMetersConversionFactor(elevVarUnitsString); } catch (Exception e) { throw new IllegalArgumentException( "Exception on getMetersConversionFactor() for the units of elev var <" + elevVarUnitsString + ">."); } if (this.ncfile.hasUnlimitedDimension() && this.ncfile.getUnlimitedDimension().equals(timeDim)) { Object result = this.ncfile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE); if ((result != null) && (Boolean) result) this.recordVar = (Structure) this.ncfile.getRootGroup().findVariable("record"); else this.recordVar = new StructurePseudo(this.ncfile, null, "record", timeDim); } else { this.recordVar = new StructurePseudo(this.ncfile, null, "record", timeDim); } // @todo HACK, HACK, HACK - remove once addRecordStructure() deals with ncd attribute changes. Variable elevVarInRecVar = this.recordVar.findVariable(this.elevVar.getFullNameEscaped()); if (!elevVarUnitsString.equals(elevVarInRecVar.findAttribute("units").getStringValue())) { elevVarInRecVar.addAttribute(new Attribute("units", elevVarUnitsString)); } trajectoryVarsMap = new HashMap(); // for ( Iterator it = this.recordVar.getVariables().iterator(); it.hasNext(); ) for (Iterator it = this.ncfile.getRootGroup().getVariables().iterator(); it.hasNext(); ) { Variable curVar = (Variable) it.next(); if (curVar.getRank() > 0 && !curVar.equals(this.timeVar) && !curVar.equals(this.latVar) && !curVar.equals(this.lonVar) && !curVar.equals(this.elevVar) && (this.recordVar == null ? true : !curVar.equals(this.recordVar))) { MyTypedDataVariable typedVar = new MyTypedDataVariable(new VariableDS(null, curVar, true)); dataVariables.add(typedVar); trajectoryVarsMap.put(typedVar.getShortName(), typedVar); } } trajectory = new SingleTrajectory( this.trajectoryId, trajectoryNumPoint, this.timeVar, timeVarUnitsString, this.latVar, this.lonVar, this.elevVar, dataVariables, trajectoryVarsMap); startDate = trajectory.getTime(0); endDate = trajectory.getTime(trajectoryNumPoint - 1); ((SingleTrajectory) trajectory).setStartDate(startDate); ((SingleTrajectory) trajectory).setEndDate(endDate); }