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++; } }
@Override public VariableDS findVariable(String axisName) { return (VariableDS) struct.findVariable(axisName); }
public VariableDS findVariable(String axisName) { return (VariableDS) struct.findVariable(axisName); }
public VariableDS findVariable(String axisName) { return (VariableDS) struct.findVariable(axisName); }
private String findUniqueName(Structure struct, String want, String def) { if (want == null) return def + tempNo++; String vwant = NetcdfFile.makeValidCdmObjectName(want); Variable oldV = struct.findVariable(vwant); if (oldV == null) return vwant; int seq = 2; while (true) { String wantSeq = vwant + "-" + seq; oldV = struct.findVariable(wantSeq); if (oldV == null) return wantSeq; seq++; } }
/** * 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; }
/** * 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; }
/** * 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<Variable>(); for (String name : memberNames) { Variable m = findVariable(name); if (null != m) members.add(m); } result.setMemberVariables(members); result.isSubset = true; return result; }
private void cmdRemove(Variable v, String type, String name) { boolean err = false; if (type.equals("attribute")) { ucar.nc2.Attribute a = v.findAttribute(name); if (a != null) { v.remove(a); if (debugCmd) System.out.println("CMD remove " + type + " " + name); } else err = true; } else if (type.equals("variable") && v instanceof Structure) { Structure s = (Structure) v; Variable nested = s.findVariable(name); if (nested != null) { s.removeMemberVariable(nested); if (debugCmd) System.out.println("CMD remove " + type + " " + name); } else err = true; } if (err) { Formatter f = new Formatter(); f.format("CMD remove %s CANT find %s location %s%n", type, name, location); log.info(f.toString()); } }
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."); }
private void readCompoundAttValues(int grpid, int varid, String attname, int len, UserType userType, List<Attribute> result, Variable v) throws IOException { int buffSize = len * userType.size; ByteBuffer bbuff = ByteBuffer.allocate(buffSize); int ret = nc4.nc_get_att(grpid, varid, attname, bbuff); if (ret != 0) throw new IOException(ret + ": " + nc4.nc_strerror(ret)); decodeCompoundData(len, userType, bbuff); // if its a Structure, distribute to matching fields if ((v != null) && (v instanceof Structure)) { Structure s = (Structure) v; for (Field fld : userType.flds) { Variable mv = s.findVariable(fld.name); if (mv != null) mv.addAttribute(new Attribute(attname, fld.data)); else result.add(new Attribute(attname + "." + fld.name, fld.data)); } } else { for (Field fld : userType.flds) result.add(new Attribute(attname + "." + fld.name, fld.data)); } }
private void readCompoundAttValues(int grpid, int varid, String attname, int len, UserType userType, List<Attribute> result, Variable v) throws IOException { int buffSize = len * userType.size; byte[] bb = new byte[buffSize]; int ret = nc4.nc_get_att(grpid, varid, attname, bb); if (ret != 0) throw new IOException(ret + ": " + nc4.nc_strerror(ret)); ByteBuffer bbuff = ByteBuffer.wrap(bb); decodeCompoundData(len, userType, bbuff); // if its a Structure, distribute to matching fields if ((v != null) && (v instanceof Structure)) { Structure s = (Structure) v; for (Field fld : userType.flds) { Variable mv = s.findVariable(fld.name); if (mv != null) mv.addAttribute(new Attribute(attname, fld.data)); else result.add(new Attribute(attname + "." + fld.name, fld.data)); } } else { for (Field fld : userType.flds) result.add(new Attribute(attname + "." + fld.name, fld.data)); } }
public void testSequence() { String url = "http://tsds.net/tsds/test/Scalar"; try { NetcdfDataset ds = NetcdfDataset.openDataset(url); System.out.println(ds); Structure struct = (Structure) ds.findVariable("TimeSeries"); Variable var = struct.findVariable("time"); Array arr = var.read(); int n = (int) arr.getSize(); int i; for (i=0; arr.hasNext() && i<n; i++) System.out.println(arr.nextDouble()); if(i != n) { System.err.println("short sequence"); System.exit(1); } } catch (Exception e) { e.printStackTrace(); } } }
public void testNames() { List<Variable> vars = ncfile.getVariables(); String[] trueNames = {"rh", "T", "lat", "lon", "time", "recordvarTest", "record"}; for (int i = 0; i < vars.size(); i++) { Assert.assertEquals("Checking names", trueNames[i], vars.get(i).getFullName()); } Structure record = (Structure) ncfile.findVariable("record"); assert record != null; vars = record.getVariables(); String[] trueRecordNames = {"record.rh", "record.T", "record.time", "record.recordvarTest"}; for (int i = 0; i < vars.size(); i++) { Assert.assertEquals("Checking record names", trueRecordNames[i], vars.get(i).getFullName()); } Variable time = ncfile.findVariable("record.time"); assert time != null; Variable time2 = record.findVariable("time"); assert time2 != null; Assert.assertEquals(time, time2); }
public void testReadStructureSubset() throws IOException, InvalidRangeException { Structure record = (Structure) ncfile.findVariable("record"); assert record != null; List<String> vars = new ArrayList<String>(); vars.add( "wind_speed"); vars.add( "wind_gust"); vars.add( "report"); Structure subset = record.select(vars); // read entire subset ArrayStructure dataAll = (ArrayStructure) subset.read(); 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"); }
@Test public void testAttStruct2() throws IOException { try (NetcdfFile ncfile = NetcdfFile.open(TestN4reading.testDir + "compound-attribute-test.nc")) { Variable v = ncfile.findVariable("compound_test"); assert v != null; assert v instanceof Structure; Structure s = (Structure) v; Variable v2 = s.findVariable("field0"); assert v2 != null; assert v2.getDataType() == DataType.FLOAT; Attribute att = v2.findAttribute("att_primitive_test"); assert !att.isString(); assert att.getNumericValue().floatValue() == 1.0; att = v2.findAttribute("att_string_test"); assert att.getStringValue().equals("string for field 0"); att = v2.findAttribute("att_char_array_test"); assert att.getStringValue().equals("a"); } }
@Test public void testAttStruct() throws IOException { try (NetcdfFile ncfile = NetcdfFile.open(TestN4reading.testDir + "attributeStruct.nc")) { Variable v = ncfile.findVariable("observations"); assert v != null; assert v instanceof Structure; Structure s = (Structure) v; Variable v2 = s.findVariable("tempMin"); assert v2 != null; assert v2.getDataType() == DataType.FLOAT; assert null != v2.findAttribute("units"); assert null != v2.findAttribute("coordinates"); Attribute att = v2.findAttribute("units"); assert att.getStringValue().equals("degF"); } }
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; }
@Test public void testScalarReadByte() throws IOException { Variable v = null; Array a = null; assert null != (v = dodsfile.findVariable("types")); v.setCaching(false); assert v instanceof Structure; Structure s = (Structure) v; assert (null != (v = s.findVariable("b"))); assert v.getShortName().equals("b"); assert v.getRank() == 0; assert v.getSize() == 1; assert v.getDataType() == DataType.UBYTE; // note this reads all 10 bytes a = v.read(); assert a.getRank() == 1; assert a.getSize() == 10; assert a.getElementType() == byte.class; assert a instanceof ArrayByte.D1; byte valb = ((ArrayByte.D1) a).get(0); assert (valb == 0); }
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"); }