/** * Create a subset of the Structure consisting only of the given member variables * @param memberNames list of Variable names, already a member * @return Structure containing just those members */ public Structure select( List<String> memberNames) { Structure result = (Structure) copy(); List<Variable> members = new ArrayList<>(); for (String name : memberNames) { Variable m = findVariable(name); if (null != m) members.add(m); } result.setMemberVariables(members); result.isSubset = true; return result; }
/** * Find first nested structure * @param s in this structure * @return first nested structure or null */ static public Structure findNestedStructure(Structure s) { for (Variable v : s.getVariables()) { if ((v instanceof Structure)) return (Structure) v; } return null; }
/** * _more_ * * @param point _more_ * * @return _more_ * * @throws IOException _more_ * @throws InvalidRangeException _more_ */ public StructureData getData(int point) throws IOException, InvalidRangeException { return TrajectoryObsDatasetImpl.this.recordVar.readStructure(point); }
/** Set the list of member variables. * @param vars this is the list of member variables */ public void setMemberVariables( List<Variable> vars) { if (isImmutable()) throw new IllegalStateException("Cant modify"); members = new ArrayList<>(); memberHash = new HashMap<>(2*vars.size()); for (Variable v : vars) { addMemberVariable(v); } }
/** * Use this when this is a scalar Structure. Its the same as read(), but it extracts the single * StructureData out of the Array. * @return StructureData for a scalar * @throws java.io.IOException on read error */ public StructureData readStructure() throws IOException { if (getRank() != 0) throw new java.lang.UnsupportedOperationException("not a scalar structure"); Array dataArray = read(); ArrayStructure data = (ArrayStructure) dataArray; return data.getStructureData(0); }
private Structure makeReportIndexStructure() throws IOException { Structure reportIndex = new Structure(ncfile, null, null, BufrIosp.obsIndex); ncfile.addVariable(null, reportIndex); reportIndex.setDimensions("record"); reportIndex.addAttribute(new Attribute("long_name", "index on report")); Variable v = reportIndex.addMemberVariable(new Variable(ncfile, null, reportIndex, "name", DataType.STRING, "")); v.addAttribute(new Attribute("long_name", "name of station")); v.addAttribute(new Attribute("standard_name", "station_name")); v = reportIndex.addMemberVariable(new Variable(ncfile, null, reportIndex, "time", DataType.LONG, "")); v.addAttribute(new Attribute("units", "msecs since 1970-01-01 00:00")); v.addAttribute(new Attribute("long_name", "observation time")); v.addAttribute(new Attribute(_Coordinate.AxisType, "Time")); return reportIndex; }
private void createCompoundType(Group4 g4, Structure s) throws IOException { IntByReference typeidp = new IntByReference(); long size = s.getElementSize(); String name = s.getShortName() + "_t"; int ret = nc4.nc_def_compound(g4.grpid, new SizeT(size), name, typeidp); if (ret != 0) int fldidx = 0; long offset = 0; for (Variable v : s.getVariables()) { if (v.getDataType() == DataType.STRING) continue; throw new IOException(nc4.nc_strerror(ret) + " on\n" + s.getShortName()); s.setSPobject(new Vinfo(g4, -1, typeidp.getValue())); // dont know the varid yet
private double copyVarData(NetcdfFile bufr, NetcdfFileWriter ncfile, Structure recordStruct) throws IOException, InvalidRangeException { int nrecs = (int) recordStruct.getSize(); int sdataSize = recordStruct.getElementSize(); for (int count = 0; count < nrecs; count++) { StructureData recordData = recordStruct.readStructure(count); for (StructureMembers.Member m : recordData.getMembers()) {
public void testReadStructureSection() throws IOException, InvalidRangeException { Structure record = (Structure) ncfile.findVariable("record"); assert record != null; Structure subset = (Structure) record.section(new Section("0:10")); assert subset != null; assert subset.getRank() == 1; assert subset.getSize() == 11; // read entire subset ArrayStructure dataAll = (ArrayStructure) subset.read(new Section("0:10")); assert dataAll.getSize() == 11; StructureMembers sm =dataAll.getStructureMembers(); for(StructureMembers.Member m : sm.getMembers()) { Variable v = subset.findVariable(m.getName()); assert v != null; Array mdata = dataAll.extractMemberArray(m); assert mdata.getShape()[0] == dataAll.getShape()[0]; assert mdata.getElementType() == m.getDataType().getPrimitiveClassType(); System.out.println(m.getName()+ " shape="+new Section(mdata.getShape())); } System.out.println("*** TestStructureSubset ok"); }
private void addStructure(Structure parent, DataDescriptor dataDesc, int count) { String structName = "struct" + structNum; structNum++; Structure struct = new Structure(ncfile, null, parent, structName); try { struct.setDimensionsAnonymous(new int[]{count}); // anon vector } catch (InvalidRangeException e) { log.error("illegal count= " + count + " for " + dataDesc); } for (DataDescriptor subKey : dataDesc.getSubKeys()) addMember(struct, subKey); parent.addMemberVariable(struct); struct.setSPobject(dataDesc); dataDesc.name = structName; dataDesc.refersTo = struct; }
@Override protected boolean identifyEncodingStation(NetcdfDataset ds, EncodingInfo info, CF.FeatureType ftype, Formatter errlog) { Structure obs = info.time.getParentStructure(); if (obs.getRank() == 0) { errlog.format("CFpointObs: must have a non-scalar Time coordinate%n"); return false; } Dimension obsDim = obs.getDimension(0); Structure station = info.lat.getParentStructure(); if (station.getRank() == 0) { // could be scalar info.set(Encoding.single, null, obsDim); } Dimension stnDim = station.getDimension(0); // the raggeds if (identifyRaggeds(ds, info, stnDim, obsDim, errlog)) return true; errlog.format("CFpointObsExt: %s Must have Lat/Lon coordinates of rank 0 or 1%n", ftype); return false; }
long nrecs = (int) recordVar.getSize(); int structureSize = recordVar.getElementSize(); int readAtaTime = Math.max( 10, buffer_size / structureSize); bytesDone += recordVar.readToByteChannel( section, channel); done += need; } catch (InvalidRangeException e) {
private String findUnique(Structure struct, String want) { Variable oldV = struct.findVariable(want); if (oldV == null) return want; int seq = 1; while (true) { String wantSeq = want + "-" + seq; oldV = struct.findVariable(wantSeq); if (oldV == null) return wantSeq; seq++; } }
private void addDpiSequence(Structure parent, BufrConfig.FieldConverter fld) { Structure struct = new Structure(ncfile, null, parent, "statistics"); try { struct.setDimensionsAnonymous(new int[]{fld.dds.replication}); // scalar } catch (InvalidRangeException e) { e.printStackTrace(); } Variable v = new Variable(ncfile, null, struct, "name"); v.setDataType(DataType.STRING); // scalar v.setDimensions(""); // scalar struct.addMemberVariable(v); v = new Variable(ncfile, null, struct, "data"); v.setDataType(DataType.FLOAT); // scalar v.setDimensions(""); // scalar struct.addMemberVariable(v); parent.addMemberVariable(struct); }
private ByteBuffer makeBB(Structure s, StructureData sdata) { int size = s.getElementSize(); ByteBuffer bb = ByteBuffer.allocate(size); bb.order(ByteOrder.nativeOrder()); long offset = 0; for (Variable v : s.getVariables()) { if (v.getDataType() == DataType.STRING) continue; // LOOK embedded strings getting lost StructureMembers.Member m = sdata.findMember(v.getShortName()); if (m == null) { System.out.printf("WARN Nc4Iosp.makeBB() cant find %s%n", v.getShortName()); bb.position((int) (offset + v.getElementSize() * v.getSize())); // skip over it } else { copy(sdata, m, bb); } offset += v.getElementSize() * v.getSize(); } return bb; }
private static Map<Integer, Float> readGainInfo(NetcdfFile ncFile) throws IOException { Variable gainInfoVar = ncFile.getRootGroup().findVariable(ChrisConstants.VS_NAME_GAIN_INFO); if (gainInfoVar instanceof Structure) { Structure gainInfoStruct = (Structure) gainInfoVar; int recordCount = gainInfoStruct.getDimension(0).getLength(); Variable gainSetting = gainInfoStruct.findVariable(ChrisConstants.VS_NAME_GAIN_SETTING); Variable gainValue = gainInfoStruct.findVariable(ChrisConstants.VS_NAME_GAIN_VALUE); if (gainSetting != null && gainValue != null && recordCount > 0) { final Map<Integer, Float> gainInfoMap = new HashMap<Integer, Float>(recordCount); Array settingsArray = gainSetting.read(); Array valuesArray = gainValue.read(); for (int i = 0; i < recordCount; i++) { gainInfoMap.put(settingsArray.getInt(i), valuesArray.getFloat(i)); } return gainInfoMap; } } throw new IOException("Failed to read 'Gain Info' Structure."); }
public void testStructureIterator() throws IOException, InvalidRangeException { NetcdfFile ncfile = NetcdfFile.open(TestDir.cdmUnitTestDir + "ft/station/Surface_METAR_20080205_0000.nc"); ncfile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE); Structure v = (Structure) ncfile.findVariable("record"); assert v != null; assert (v.getDataType() == DataType.STRUCTURE); int count = 0; try (StructureDataIterator si = v.getStructureIterator()) { while (si.hasNext()) { StructureData sd = si.next(); count++; } } assert count == v.getSize(); ncfile.close(); } }
protected TableConfig getProfileConfig(NetcdfDataset ds, Formatter errlog) { // construct the station table by reading through the timeseries TableConfig profileTable = new TableConfig(Table.Type.Structure, "profile"); profileTable.featureType = FeatureType.PROFILE; profileTable.structName = BufrIosp.obsRecord; Structure profileStruct = (Structure) ds.findVariable(BufrIosp.obsRecord); profileTable.lat = Evaluator.getNameOfVariableWithAttribute(profileStruct, _Coordinate.AxisType, AxisType.Lat.toString()); profileTable.lon = Evaluator.getNameOfVariableWithAttribute(profileStruct, _Coordinate.AxisType, AxisType.Lon.toString()); profileTable.time = Evaluator.getNameOfVariableWithAttribute(profileStruct, _Coordinate.AxisType, AxisType.Time.toString()); // the time series is just the outer structure TableConfig obsTable = new TableConfig(Table.Type.NestedStructure, "struct5"); Structure obsStruct = (Structure) profileStruct.findVariable("struct5"); obsTable.structName = obsStruct.getName(); obsTable.nestedTableName = obsStruct.getShortName(); obsTable.elev = Evaluator.getNameOfVariableWithAttribute(obsStruct, _Coordinate.AxisType, AxisType.Pressure.toString()); // HEY not height profileTable.addChild(obsTable); return profileTable; }
public void testW(String url, String sname, boolean isScalar) throws IOException, InvalidRangeException { NetcdfFile ncfile = NetcdfDataset.openFile(url, null); Structure v = (Structure) ncfile.findVariable(sname); assert v != null; assert( v.getDataType() == DataType.STRUCTURE); Array data = v.read(); assert( data instanceof ArrayStructure); assert(data.getElementType() == StructureData.class); new UtilsTestStructureArray().testArrayStructure( (ArrayStructure) data); ncfile.close(); }
CatIterator(Entry[] entries, Structure useStructure) { this.entries = entries; members = useStructure.makeStructureMembers(); for (Variable v2 : useStructure.getVariables()) { Vinfo vinfo = (Vinfo) v2.getSPobject(); StructureMembers.Member m = members.findMember(v2.getShortName()); m.setDataParam(vinfo.offset); } }