/* Structure { int a_name; byte b_name(3); byte c_name(3); short d_name(3); int e_name(3); long f_name(3); int g_name(3); short h_name(3); int i_name(3); long j_name(3); float k_name(3); double l_name(3); } CompoundNative(15); type = Layout(8); type= 1 (contiguous) storageSize = (15,144) dataSize=0 dataAddress=2048 */ @Test public void testReadH5StructureArrayMembers() throws java.io.IOException { try (NetcdfFile ncfile = TestH5.openH5("complex/compound_native.h5")) { Variable dset = ncfile.findVariable("CompoundNative"); assert (null != dset); assert (dset.getDataType() == DataType.STRUCTURE); assert (dset.getRank() == 1); assert (dset.getSize() == 15); Dimension d = dset.getDimension(0); assert (d.getLength() == 15); Structure s = (Structure) dset; // read all with the iterator StructureDataIterator iter = s.getStructureIterator(); while (iter.hasNext()) { StructureData sd = iter.next(); for (StructureMembers.Member m : sd.getMembers()) { Array data = sd.getArray(m); NCdumpW.printArray(data, m.getName(), out, null); } } } System.out.println("*** testReadH5StructureArrayMembers ok"); }
private void init() throws IOException { members = new StructureMembers("stationObs"); // used to convert from adde format calendar = new GregorianCalendar(); calendar.setTimeZone(TimeZone.getTimeZone("GMT")); // time unit timeUnit = DateUnit.getUnixDateUnit(); try { AddePointDataReader reader = callAdde(addeURL); String[] params = reader.getParams(); String[] units = reader.getUnits(); int[] scales = reader.getScales(); scaleFactor = new double[params.length]; if (debugHead) System.out.println(" Param Unit Scale"); for (int paramNo = 0; paramNo < params.length; paramNo++) { // memberNames.add( params[i]); if (debugHead) System.out.println(" " + params[paramNo] + " " + units[paramNo] + " " + scales[paramNo]); if (scales[paramNo] != 0) scaleFactor[paramNo] = 1.0 / Math.pow(10.0, (double) scales[paramNo]); DataType dt = null; if ("CHAR".equals(units[paramNo])) dt = DataType.STRING; else if (scaleFactor[paramNo] == 0) dt = DataType.INT; else dt = DataType.DOUBLE; String unitString = null; if ((units[paramNo] != null) && (units[paramNo].length() > 0)) unitString = visad.jmet.MetUnits.makeSymbol(units[paramNo]); AddeTypedDataVariable tdv = new AddeTypedDataVariable(params[paramNo], unitString, dt); dataVariables.add(tdv); StructureMembers.Member m = members.addMember( tdv.getShortName(), tdv.getDescription(), tdv.getUnitsString(), tdv.getDataType(), tdv.getShape()); m.setDataParam(paramNo); members.addMember(m); } } catch (AddeException e) { e.printStackTrace(); throw new IOException(e.getMessage()); } }
private double getTime(StructureMembers.Member timeVar, StructureData sdata) { if (timeVar == null) return 0.0; if ((timeVar.getDataType() == DataType.CHAR) || (timeVar.getDataType() == DataType.STRING)) { String time = sdata.getScalarString(timeVar); CalendarDate date = CalendarDateFormatter.isoStringToCalendarDate(null, time); if (date == null) { log.error("Cant parse date - not ISO formatted, = " + time); return 0.0; } return date.getMillis() / 1000.0; } else { return sdata.convertScalarDouble(timeVar); } }
protected void makeFeatureVariables(StructureData featureData, boolean isExtended) throws IOException { // LOOK why not unlimited here ? Dimension profileDim = writer.addDimension(null, profileDimName, nfeatures); // Dimension profileDim = isExtendedModel ? writer.addUnlimitedDimension(profileDimName) : // writer.addDimension(null, profileDimName, nprofiles); // add the profile Variables using the profile dimension List<VariableSimpleIF> profileVars = new ArrayList<>(); profileVars.add( VariableSimpleImpl.makeScalar(latName, "profile latitude", CDM.LAT_UNITS, DataType.DOUBLE)); profileVars.add( VariableSimpleImpl.makeScalar( lonName, "profile longitude", CDM.LON_UNITS, DataType.DOUBLE)); profileVars.add( VariableSimpleImpl.makeString(profileIdName, "profile identifier", null, id_strlen) .add(new Attribute(CF.CF_ROLE, CF.PROFILE_ID))); // profileId:cf_role = "profile_id"; profileVars.add( VariableSimpleImpl.makeScalar( numberOfObsName, "number of obs for this profile", null, DataType.INT) .add( new Attribute( CF.SAMPLE_DIMENSION, recordDimName))); // rowSize:sample_dimension = "obs" profileVars.add( VariableSimpleImpl.makeScalar( profileTimeName, "nominal time of profile", timeUnit.getUnitsString(), DataType.DOUBLE)); for (StructureMembers.Member m : featureData.getMembers()) { VariableSimpleIF dv = getDataVar(m.getName()); if (dv != null) profileVars.add(dv); } if (isExtended) { profileStruct = (Structure) writer.addVariable(null, profileStructName, DataType.STRUCTURE, profileDimName); addCoordinatesExtended(profileStruct, profileVars); } else { addCoordinatesClassic(profileDim, profileVars, featureVarMap); } }
@Test public void testH5StructureDS() throws java.io.IOException { int a_name = 0; String[] b_name = new String[] { "A fight is a contract that takes two people to honor.", "A combative stance means that you've accepted the contract.", "In which case, you deserve what you get.", " -- Professor Cheng Man-ch'ing" }; String c_name = "Hello!"; // H5header.setDebugFlags(new ucar.nc2.util.DebugFlagsImpl("H5header/header")); try (NetcdfDataset ncfile = NetcdfDataset.openDataset(TestH5.testDir + "complex/compound_complex.h5")) { Variable dset = ncfile.findVariable("CompoundComplex"); assert (null != dset); assert (dset.getDataType() == DataType.STRUCTURE); assert (dset.getRank() == 1); assert (dset.getSize() == 6); Dimension d = dset.getDimension(0); assert (d.getLength() == 6); Structure s = (Structure) dset; // read all with the iterator StructureDataIterator iter = s.getStructureIterator(); while (iter.hasNext()) { StructureData sd = iter.next(); assert sd.getScalarInt("a_name") == a_name; a_name++; assert sd.getScalarString("c_name").equals(c_name); String[] results = sd.getJavaArrayString(sd.findMember("b_name")); assert results.length == b_name.length; int count = 0; for (String r : results) assert r.equals(b_name[count++]); for (StructureMembers.Member m : sd.getMembers()) { Array data = sd.getArray(m); NCdumpW.printArray(data, m.getName(), out, null); } } } System.out.println("*** testH5StructureDS ok"); }
public boolean compareStructureData( StructureData sdata1, StructureData sdata2, double tol, boolean justOne) { boolean ok = true; StructureMembers sm1 = sdata1.getStructureMembers(); StructureMembers sm2 = sdata2.getStructureMembers(); if (sm1.getMembers().size() != sm2.getMembers().size()) { f.format(" size %d !== %d%n", sm1.getMembers().size(), sm2.getMembers().size()); ok = false; } for (StructureMembers.Member m1 : sm1.getMembers()) { if (m1.getName().equals("time")) continue; StructureMembers.Member m2 = sm2.findMember(m1.getName()); Array data1 = sdata1.getArray(m1); Array data2 = sdata2.getArray(m2); ok &= compareData(m1.getName(), data1, data2, tol, justOne); } return ok; }
private double copyVarData(NetcdfFileWriteable ncfile, Structure recordStruct) throws IOException, InvalidRangeException { int nrecs = (int) recordStruct.getSize(); int sdataSize = recordStruct.getElementSize(); double total = 0; double totalRecordBytes = 0; for (int count = 0; count < nrecs; count++) { StructureData recordData = recordStruct.readStructure(count); for (StructureMembers.Member m : recordData.getMembers()) { if (m.getDataType() == DataType.STRUCTURE) { int countLevel = 0; ArrayStructure seq1 = recordData.getArrayStructure(m); StructureDataIterator iter = seq1.getStructureDataIterator(); try { while (iter.hasNext()) { StructureData seqData = iter.next(); for (StructureMembers.Member seqm : seqData.getMembers()) { Array data = seqData.getArray(seqm); int[] shape = data.getShape(); int[] newShape = new int[data.getRank() + 2]; newShape[0] = 1; newShape[1] = 1; for (int i = 0; i < data.getRank(); i++) newShape[i + 1] = shape[i]; int[] origin = new int[data.getRank() + 2]; origin[0] = count; origin[1] = countLevel; String mname = seqm.getName() + "-" + m.getName(); if (debug && (count == 0) && (countLevel == 0)) System.out.println("write to = " + mname); ncfile.write(mname, origin, data.reshape(newShape)); } countLevel++; } } finally { iter.finish(); } } else { Array data = recordData.getArray(m); int[] shape = data.getShape(); int[] newShape = new int[data.getRank() + 1]; newShape[0] = 1; for (int i = 0; i < data.getRank(); i++) newShape[i + 1] = shape[i]; int[] origin = new int[data.getRank() + 1]; origin[0] = count; if (debug && (count == 0)) System.out.println("write to = " + m.getName()); ncfile.write(m.getName(), origin, data.reshape(newShape)); } } totalRecordBytes += sdataSize; } total += totalRecordBytes; totalRecordBytes /= 1000 * 1000; if (debug) System.out.println( "write record var; total = " + totalRecordBytes + " Mbytes # recs=" + nrecs); return total; }