// create from a dataset public VariableBean(Variable vs) { this.vs = vs; // vs = (v instanceof VariableEnhanced) ? (VariableEnhanced) v : new VariableStandardized( v); setName(vs.getShortName()); setDescription(vs.getDescription()); setUnits(vs.getUnitsString()); setDataType(vs.getDataType().toString()); // Attribute csAtt = vs.findAttribute("_coordSystems"); // if (csAtt != null) // setCoordSys( csAtt.getStringValue()); // collect dimensions StringBuilder lens = new StringBuilder(); StringBuilder names = new StringBuilder(); java.util.List dims = vs.getDimensions(); for (int j = 0; j < dims.size(); j++) { ucar.nc2.Dimension dim = (ucar.nc2.Dimension) dims.get(j); if (j > 0) { lens.append(","); names.append(","); } String name = dim.isShared() ? dim.getName() : "anon"; names.append(name); lens.append(dim.getLength()); } setDimensions(names.toString()); setShape(lens.toString()); }
void CheckS(Variable v) throws IOException { // string // assert(null != (v = dodsfile.findVariable("types.strings.s"))); // assert v.getName().equals("types.strings.s"); assert v.getRank() == 0; assert v.getDataType() == DataType.STRING : v.getDataType(); CheckSValue(v.read()); }
void CheckUint16(Variable v) throws IOException { // uint16 // assert(null != (v = dodsfile.findVariable("types.integers.ui16"))); // assert v.getName().equals("types.integers.ui16"); assert v.getRank() == 0; assert v.getSize() == 1; assert v.getDataType() == DataType.SHORT : v.getDataType(); CheckUInt16Value(v.read()); }
void CheckUrl(Variable v) throws IOException { // url // assert(null != (v = dodsfile.findVariable("types.strings.u"))); // assert v.getName().equals("types.strings.u"); assert v.getRank() == 0; assert v.getDataType() == DataType.STRING : v.getDataType(); String str = v.readScalarString(); assert str.equals("http://www.opendap.org") || str.equals("http://www.dods.org") : str; }
void CheckD(Variable v) throws IOException { // double // assert(null != (v = dodsfile.findVariable("types.floats.f64"))); // assert v.getName().equals("types.floats.f64"); assert v.getRank() == 0; assert v.getSize() == 1; assert v.getDataType() == DataType.DOUBLE : v.getDataType(); CheckDValue(v.read()); }
void CheckF(Variable v) throws IOException { // float // assert(null != (v = dodsfile.findVariable("types.floats.f32"))); // assert v.getName().equals("types.floats.f32"); assert v.getRank() == 0; assert v.getSize() == 1; assert v.getDataType() == DataType.FLOAT : v.getDataType(); CheckFValue(v.read()); }
void CheckLong32(Variable v) throws IOException { // uint32 // assert(null != (v = dodsfile.findVariable("types.integers.ui32"))); // assert v.getName().equals("types.integers.ui32"); assert v.getRank() == 0; assert v.getSize() == 1; assert v.getDataType() == DataType.LONG : v.getDataType(); CheckLongValue(v.read()); }
/* Structure { int LAT[0]; ... int LAT[149]; } IMAGE_LAT_ARRAY(3600); type = Layout(8); type= 2 (chunked) storageSize = (1,600) dataSize=0 dataAddress=2548046 */ @Test public void testReadOneAtATime() throws java.io.IOException, InvalidRangeException { try (NetcdfFile ncfile = TestH5.openH5("IASI/IASI.h5")) { Variable dset = ncfile.findVariable("U-MARF/EPS/IASI_xxx_1C/DATA/IMAGE_LAT_ARRAY"); assert (null != dset); assert (dset.getDataType() == DataType.STRUCTURE); assert (dset.getRank() == 1); assert (dset.getSize() == 3600); Dimension d = dset.getDimension(0); assert (d.getLength() == 3600); Structure s = (Structure) dset; // read last one - chunked StructureData sd = s.readStructure(3599); assert sd.getScalarInt("LAT[0]") == 70862722; assert sd.getScalarInt("LAT[149]") == 85302263; // read one at a time for (int i = 3590; i < d.getLength(); i++) { s.readStructure(i); System.out.println(" read structure " + i); } } System.out.println("*** testReadIASI ok"); }
/* 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"); }
/** * Set extra information used by station obs datasets. Use stnIdVName or stnIndexVName. * * @param stnIdVName the obs variable that is used to find the station in the stnHash; may be type * int or a String (char). * @param stnDescVName optional station var containing station description */ public void setStationInfo( String stnIdVName, String stnDescVName, String stnIndexVName, StationHelper stationHelper) { this.stnIdVName = stnIdVName; this.stnDescVName = stnDescVName; this.stnIndexVName = stnIndexVName; this.stationHelper = stationHelper; if (stnIdVName != null) { Variable stationVar = ncfile.findVariable(stnIdVName); stationIdType = stationVar.getDataType(); } }
private void doWrite2(NetcdfFileWriteable ncfile, String varName) throws Exception { Variable v = ncfile.findVariable(varName); int[] w = getWeights(v); int[] shape = v.getShape(); Array aa = Array.factory(v.getDataType().getPrimitiveClassType(), shape); Index ima = aa.getIndex(); for (int i = 0; i < shape[0]; i++) { for (int j = 0; j < shape[1]; j++) { aa.setDouble(ima.set(i, j), (double) (i * w[0] + j * w[1])); } } ncfile.write(varName, aa); }
@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"); }
/* Structure { char EntryName(64); char Definition(1024); char Unit(1024); char Scale Factor(1024); } TIME_DESCR(60); type = Layout(8); type= 2 (chunked) storageSize = (1,3136) dataSize=0 dataAddress=684294 */ @Test public void testReadManyAtATime() throws java.io.IOException, InvalidRangeException { try (NetcdfFile ncfile = TestH5.openH5("IASI/IASI.h5")) { Variable dset = ncfile.findVariable("U-MARF/EPS/IASI_xxx_1C/DATA/TIME_DESCR"); assert (null != dset); assert (dset.getDataType() == DataType.STRUCTURE); assert (dset.getRank() == 1); assert (dset.getSize() == 60); Dimension d = dset.getDimension(0); assert (d.getLength() == 60); ArrayStructure data = (ArrayStructure) dset.read(); StructureMembers.Member m = data.getStructureMembers().findMember("EntryName"); assert m != null; for (int i = 0; i < dset.getSize(); i++) { String r = data.getScalarString(i, m); if (i % 2 == 0) assert r.equals("TIME[" + i / 2 + "]-days") : r + " at " + i; else assert r.equals("TIME[" + i / 2 + "]-milliseconds") : r + " at " + i; } } System.out.println("*** testReadManyAtATime ok"); }
@Test public void testArraySubset() throws IOException { DODSNetcdfFile dodsfile = TestDODSRead.open("test.02?i32[1:10],f64[2:2:10]"); Variable v = null; Array a = null; // int32 assert (null != (v = dodsfile.findVariable("i32"))); assert v.getFullName().equals("i32"); assert v.getRank() == 1; assert v.getSize() == 10; assert v.getDataType() == DataType.INT; a = v.read(); assert a.getRank() == 1; assert a.getSize() == 25; assert a.getElementType() == int.class; assert a instanceof ArrayInt.D1; ArrayInt.D1 ai = (ArrayInt.D1) a; for (int i = 0; i < 10; i++) { int val = ai.get(i); assert (val == i * 2048) : val + " != " + (i * 2048); } // uint16 assert null == (v = dodsfile.findVariable("ui16")); assert null == (v = dodsfile.findVariable("ui32")); // double assert (null != (v = dodsfile.findVariable("f64"))); assert v.getFullName().equals("f64"); assert v.getRank() == 1; assert v.getSize() == 5; assert v.getDataType() == DataType.DOUBLE : v.getDataType(); a = v.read(); assert a.getRank() == 1; assert a.getSize() == 25; assert a.getElementType() == double.class; assert a instanceof ArrayDouble.D1; ArrayDouble.D1 ad = (ArrayDouble.D1) a; double[] tFloat64 = new double[] { 1.0, 0.9999500004166653, 0.9998000066665778, 0.9995500337489875, 0.9992001066609779, 0.9987502603949663, 0.9982005399352042, 0.9975510002532796, 0.9968017063026194, 0.9959527330119943, 0.9950041652780257, 0.9939560979566968, 0.9928086358538663, 0.9915618937147881, 0.9902159962126371, 0.9887710779360422, 0.9872272833756269, 0.9855847669095608, 0.9838436927881214, 0.9820042351172703, 0.9800665778412416, 0.9780309147241483, 0.9758974493306055, 0.9736663950053749, 0.9713379748520297 }; for (int i = 0; i < 5; i++) { double val = ad.get(i); assert Misc.closeEnough(val, tFloat64[i], 1.0e-9); } dodsfile.close(); }
// create from a dataset public ObsBean(Structure obs, StructureData sdata) { // first choice for (Variable v : obs.getVariables()) { Attribute att = v.findAttribute("BUFR:TableB_descriptor"); if (att == null) continue; String val = att.getStringValue(); if (val.equals("0-5-1") && Double.isNaN(lat)) { lat = sdata.convertScalarDouble(v.getShortName()); } else if (val.equals("0-6-1") && Double.isNaN(lon)) { lon = sdata.convertScalarDouble(v.getShortName()); } else if (val.equals("0-7-30") && Double.isNaN(alt)) { alt = sdata.convertScalarDouble(v.getShortName()); } else if (val.equals("0-4-1") && (year < 0)) { year = sdata.convertScalarInt(v.getShortName()); } else if (val.equals("0-4-2") && (month < 0)) { month = sdata.convertScalarInt(v.getShortName()); } else if (val.equals("0-4-3") && (day < 0)) { day = sdata.convertScalarInt(v.getShortName()); } else if (val.equals("0-4-4") && (hour < 0)) { hour = sdata.convertScalarInt(v.getShortName()); } else if (val.equals("0-4-5") && (minute < 0)) { minute = sdata.convertScalarInt(v.getShortName()); } else if (val.equals("0-4-6") && (sec < 0)) { sec = sdata.convertScalarInt(v.getShortName()); } else if (val.equals("0-1-1") && (wmo_block < 0)) { wmo_block = sdata.convertScalarInt(v.getShortName()); } else if (val.equals("0-1-2") && (wmo_id < 0)) { wmo_id = sdata.convertScalarInt(v.getShortName()); } else if ((stn == null) && (val.equals("0-1-7") || val.equals("0-1-194") || val.equals("0-1-11") || val.equals("0-1-18"))) { if (v.getDataType().isString()) stn = sdata.getScalarString(v.getShortName()); else stn = Integer.toString(sdata.convertScalarInt(v.getShortName())); } } // second choice for (Variable v : obs.getVariables()) { Attribute att = v.findAttribute("BUFR:TableB_descriptor"); if (att == null) continue; String val = att.getStringValue(); if (val.equals("0-5-2") && Double.isNaN(lat)) { lat = sdata.convertScalarDouble(v.getShortName()); } else if (val.equals("0-6-2") && Double.isNaN(lon)) { lon = sdata.convertScalarDouble(v.getShortName()); } else if (val.equals("0-7-1") && Double.isNaN(alt)) { alt = sdata.convertScalarDouble(v.getShortName()); } else if ((val.equals("0-4-7")) && (sec < 0)) { sec = sdata.convertScalarInt(v.getShortName()); } } // third choice for (Variable v : obs.getVariables()) { Attribute att = v.findAttribute("BUFR:TableB_descriptor"); if (att == null) continue; String val = att.getStringValue(); if (val.equals("0-7-10") && Double.isNaN(alt)) { alt = sdata.convertScalarDouble(v.getShortName()); } else if (val.equals("0-7-2") && Double.isNaN(alt)) { alt = sdata.convertScalarDouble(v.getShortName()); } } }
public static void main(String args[]) throws Exception { long start = System.currentTimeMillis(); Map<String, ucar.unidata.geoloc.Station> staHash = new HashMap<String, ucar.unidata.geoloc.Station>(); String location = "R:/testdata/sounding/netcdf/Upperair_20070401_0000.nc"; NetcdfDataset ncfile = NetcdfDataset.openDataset(location); ncfile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE); // look through record varibles, for those that have "manLevel" dimension // make a StructureData object for those StructureMembers sm = new StructureMembers("manLevel"); Dimension manDim = ncfile.findDimension("manLevel"); Structure record = (Structure) ncfile.findVariable("record"); List<Variable> allList = record.getVariables(); List<VariableSimpleIF> varList = new ArrayList<VariableSimpleIF>(); for (Variable v : allList) { if ((v.getRank() == 1) && v.getDimension(0).equals(manDim)) { // public VariableDS(NetcdfDataset ds, Group group, Structure parentStructure, String // shortName, DataType dataType, // String dims, String units, String desc) { varList.add( new VariableDS( ncfile, null, null, v.getShortName(), v.getDataType(), "", v.getUnitsString(), v.getDescription())); // (String name, String desc, String units, DataType dtype, int []shape) sm.addMember( v.getShortName(), v.getDescription(), v.getUnitsString(), v.getDataType(), new int[0]); // scalar } } ArrayStructureMA manAS = new ArrayStructureMA(sm, new int[] {manDim.getLength()}); // need the date units Variable time = ncfile.findVariable("synTime"); String timeUnits = ncfile.findAttValueIgnoreCase(time, "units", null); timeUnits = StringUtil.remove(timeUnits, '('); // crappy fsl'ism timeUnits = StringUtil.remove(timeUnits, ')'); DateUnit timeUnit = new DateUnit(timeUnits); // extract stations int nrecs = 0; StructureDataIterator iter = record.getStructureIterator(); while (iter.hasNext()) { StructureData sdata = iter.next(); String name = sdata.getScalarString("staName"); ucar.unidata.geoloc.Station s = staHash.get(name); if (s == null) { float lat = sdata.convertScalarFloat("staLat"); float lon = sdata.convertScalarFloat("staLon"); float elev = sdata.convertScalarFloat("staElev"); s = new StationImpl(name, "", lat, lon, elev); staHash.put(name, s); } nrecs++; } List<ucar.unidata.geoloc.Station> stnList = Arrays.asList(staHash.values().toArray(new ucar.unidata.geoloc.Station[staHash.size()])); Collections.sort(stnList); // create the writer WriterProfileObsDataset writer = new WriterProfileObsDataset(location + ".out", "rewrite " + location); writer.writeHeader(stnList, varList, nrecs, "prMan"); // extract records iter = record.getStructureIterator(); while (iter.hasNext()) { StructureData sdata = iter.next(); String name = sdata.getScalarString("staName"); double timeValue = sdata.convertScalarDouble("synTime"); Date date = timeUnit.makeDate(timeValue); // transfer to the ArrayStructure List<String> names = sm.getMemberNames(); for (String mname : names) { manAS.setMemberArray(mname, sdata.getArray(mname)); } // each level is weritten as a seperate structure int numMand = sdata.getScalarInt("numMand"); if (numMand >= manDim.getLength()) continue; for (int i = 0; i < numMand; i++) { StructureData useData = manAS.getStructureData(i); writer.writeRecord(name, date, useData); } } writer.finish(); long took = System.currentTimeMillis() - start; System.out.println("That took = " + took); }
void CheckByte(Variable v) throws IOException { assert v.getRank() == 0; assert v.getSize() == 1; assert v.getDataType() == DataType.BYTE; CheckByteValue(v.read()); }