// 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()); }
/* 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"); }
// convert to shared dimensions private void setSharedDimensions( Variable v, List<Element> values, List<Dimension> unknownDims, String location) { if (values.size() == 0) return; // remove the "scalar" dumbension Iterator<Element> iter = values.iterator(); while (iter.hasNext()) { Element value = iter.next(); String dimName = value.getText().trim(); if (dimName.equalsIgnoreCase("scalar")) iter.remove(); } // gotta have same number of dimensions List<Dimension> oldDims = v.getDimensions(); if (oldDims.size() != values.size()) { log.error("Different number of dimensions for {} {}", v, location); return; } List<Dimension> newDims = new ArrayList<>(); Group group = v.getParentGroup(); for (int i = 0; i < values.size(); i++) { Element value = values.get(i); String dimName = value.getText().trim(); dimName = NetcdfFile.makeValidCdmObjectName(dimName); Dimension dim = group.findDimension(dimName); Dimension oldDim = oldDims.get(i); if (dim == null) dim = checkUnknownDims(dimName, unknownDims, oldDim, location); if (dim == null) { log.error( "Unknown Dimension= {} for variable = {} {} ", dimName, v.getFullName(), location); return; } if (dim.getLength() != oldDim.getLength()) { log.error( "Shared dimension (" + dim.getShortName() + ") has different length than data dimension (" + oldDim.getShortName() + ") shared=" + dim.getLength() + " org=" + oldDim.getLength() + " for " + v + " " + location); return; } newDims.add(dim); } v.setDimensions(newDims); if (showWork) System.out.printf(" set shared dimensions for %s %n", v.getNameAndDimensions()); }
private boolean compare(List<Dimension> dims1, List<Dimension> dims2) { if (dims1.size() != dims2.size()) return false; for (int i = 0; i < dims1.size(); i++) { Dimension dim1 = dims1.get(i); Dimension dim2 = dims2.get(i); // if (!dim1.getName().equals(dim2.getName())) return false; if (dim1.getLength() != dim2.getLength()) return false; } return true; }
/* 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"); }
// look if the wanted dimension is in the unknownDims list. private Dimension checkUnknownDims( String wantDim, List<Dimension> unknownDims, Dimension oldDim, String location) { for (Dimension dim : unknownDims) { if (dim.getShortName().equals(wantDim)) { int len = oldDim.getLength(); if (len == 0) dim.setUnlimited(true); // allow zero length dimension !! dim.setLength(len); // use existing (anon) dimension Group parent = dim.getGroup(); parent.addDimensionIfNotExists(dim); // add to the parent unknownDims.remove(dim); // remove from list LOOK is this ok? log.warn("unknownDim {} length set to {}{}", wantDim, oldDim.getLength(), location); return dim; } } return null; }
/** get the shape */ public int[] getShape() { int[] shape = new int[mydims.size()]; for (int i = 0; i < mydims.size(); i++) { Dimension d = mydims.get(i); shape[i] = d.getLength(); } return shape; }
/** Create a DAS for this netcdf file */ NcDAS(NetcdfFile ncfile) { // Variable attributes Iterator iter = ncfile.getVariables().iterator(); while (iter.hasNext()) { Variable v = (Variable) iter.next(); doVariable(v, null); } // Global attributes opendap.dap.AttributeTable gtable = new opendap.dap.AttributeTable("NC_GLOBAL"); int count = addAttributes(gtable, null, ncfile.getGlobalAttributes().iterator()); if (count > 0) try { addAttributeTable("NC_GLOBAL", gtable); } catch (AttributeExistsException e) { log.error("Cant add NC_GLOBAL", e); } // unlimited dimension iter = ncfile.getDimensions().iterator(); while (iter.hasNext()) { Dimension d = (Dimension) iter.next(); if (d.isUnlimited()) { opendap.dap.AttributeTable table = new opendap.dap.AttributeTable("DODS_EXTRA"); try { table.appendAttribute("Unlimited_Dimension", opendap.dap.Attribute.STRING, d.getName()); addAttributeTable("DODS_EXTRA", table); } catch (Exception e) { log.error("Error adding Unlimited_Dimension =" + e); } break; } } // unused dimensions opendap.dap.AttributeTable dimTable = null; iter = ncfile.getDimensions().iterator(); while (iter.hasNext()) { Dimension d = (Dimension) iter.next(); if (null == usedDims.get(d.getName())) { if (dimTable == null) dimTable = new opendap.dap.AttributeTable("EXTRA_DIMENSION"); try { dimTable.appendAttribute( d.getName(), opendap.dap.Attribute.INT32, Integer.toString(d.getLength())); } catch (Exception e) { log.error("Error adding Unlimited_Dimension =" + e); } } } if (dimTable != null) try { addAttributeTable("EXTRA_DIMENSION", dimTable); } catch (AttributeExistsException e) { log.error("Cant add EXTRA_DIMENSION", e); } }
public void testDimensions(NetcdfFile ncfile) { System.out.println("ncfile = \n" + ncfile); Dimension latDim = ncfile.findDimension("lat"); assert null != latDim; assert latDim.getShortName().equals("lat"); assert latDim.getLength() == 3; assert !latDim.isUnlimited(); Dimension lonDim = ncfile.findDimension("lon"); assert null != lonDim; assert lonDim.getShortName().equals("lon"); assert lonDim.getLength() == 4; assert !lonDim.isUnlimited(); Dimension timeDim = ncfile.findDimension("time"); assert null != timeDim; assert timeDim.getShortName().equals("time"); assert timeDim.getLength() == 3 : timeDim.getLength(); }
/** Instances which have same contents are equal. Careful!! this is not object identity !! */ @Override public boolean equals(Object oo) { if (this == oo) return true; if (!(oo instanceof Dimension)) return false; Dimension other = (Dimension) oo; if ((g != null) && !g.equals(other.getGroup())) return false; if ((getName() == null) && (other.getName() != null)) return false; if ((getName() != null) && !getName().equals(other.getName())) return false; return (getLength() == other.getLength()) && (isUnlimited() == other.isUnlimited()) && (isVariableLength() == other.isVariableLength()) && (isShared() == other.isShared()); }
/** * Create a new GeoGrid that is a logical subset of this GeoGrid. * * @param t_range subset the time dimension, or null if you want all of it * @param z_range subset the vertical dimension, or null if you want all of it * @param bbox a lat/lon bounding box, or null if you want all x,y * @param z_stride use only if z_range is null, then take all z with this stride (1 means all) * @param y_stride use this stride on the y coordinate (1 means all) * @param x_stride use this stride on the x coordinate (1 means all) * @return subsetted GeoGrid * @throws InvalidRangeException if bbox does not intersect GeoGrid */ public GeoGrid subset( Range t_range, Range z_range, LatLonRect bbox, int z_stride, int y_stride, int x_stride) throws InvalidRangeException { if ((z_range == null) && (z_stride > 1)) { Dimension zdim = getZDimension(); if (zdim != null) z_range = new Range(0, zdim.getLength() - 1, z_stride); } Range y_range = null, x_range = null; if (bbox != null) { List yx_ranges = gcs.getRangesFromLatLonRect(bbox); y_range = (Range) yx_ranges.get(0); x_range = (Range) yx_ranges.get(1); } if (y_stride > 1) { if (y_range == null) { Dimension ydim = getYDimension(); y_range = new Range(0, ydim.getLength() - 1, y_stride); } else { y_range = new Range(y_range.first(), y_range.last(), y_stride); } } if (x_stride > 1) { if (x_range == null) { Dimension xdim = getXDimension(); x_range = new Range(0, xdim.getLength() - 1, x_stride); } else { x_range = new Range(x_range.first(), x_range.last(), x_stride); } } return subset(t_range, z_range, y_range, x_range); }
@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"); }
private void createDataVariables(List<VariableSimpleIF> dataVars) throws IOException { /* height variable Variable heightVar = ncfile.addStringVariable(altName, recordDims, 20); ncfile.addVariableAttribute(heightVar, new Attribute("long_name", "height of observation")); ncfile.addVariableAttribute(heightVar, new Attribute("units", altUnits)); */ Variable v = ncfile.addVariable(parentProfileIndex, DataType.INT, recordDimName); ncfile.addVariableAttribute(v, new Attribute("long_name", "index of parent profile")); v = ncfile.addVariable(nextObsName, DataType.INT, recordDimName); ncfile.addVariableAttribute( v, new Attribute("long_name", "record number of next obs in linked list for this profile")); // find all dimensions needed by the data variables for (VariableSimpleIF var : dataVars) { List<Dimension> dims = var.getDimensions(); dimSet.addAll(dims); } // add them for (Dimension d : dimSet) { if (!d.isUnlimited()) ncfile.addDimension(d.getName(), d.getLength(), d.isShared(), false, d.isVariableLength()); } // add the data variables all using the record dimension for (VariableSimpleIF oldVar : dataVars) { List<Dimension> dims = oldVar.getDimensions(); StringBuffer dimNames = new StringBuffer(recordDimName); for (Dimension d : dims) { if (!d.isUnlimited()) dimNames.append(" ").append(d.getName()); } Variable newVar = ncfile.addVariable(oldVar.getName(), oldVar.getDataType(), dimNames.toString()); List<Attribute> atts = oldVar.getAttributes(); for (Attribute att : atts) { ncfile.addVariableAttribute(newVar, att); } } }
public VariableBean(VariableEnhanced v) { this.ve = v; setName(v.getFullName()); setDescription(v.getDescription()); setUnits(v.getUnitsString()); // collect dimensions StringBuilder lens = new StringBuilder(); StringBuilder names = new StringBuilder(); java.util.List dims = v.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.getShortName() : "anon"; names.append(name); lens.append(dim.getLength()); } setDims(names.toString()); setShape(lens.toString()); StringBuilder buff = new StringBuilder(); List<CoordinateSystem> csList = v.getCoordinateSystems(); for (CoordinateSystem cs : csList) { if (firstCoordSys == null) firstCoordSys = cs; else buff.append("; "); buff.append(cs.getName()); Formatter gridBuff = new Formatter(); if (GridCoordSys.isGridCoordSys(gridBuff, cs, v)) { addDataType("grid"); } /* else if (PointDatasetDefaultHandler.isPointFeatureDataset(ds)) { addDataType("point"); } */ } setCoordSys(buff.toString()); }
public AxisBean(CoordinateAxis v) { this.axis = v; setName(v.getFullName()); setCoordVar(v.isCoordinateVariable()); setDescription(v.getDescription()); setUnits(v.getUnitsString()); // collect dimensions StringBuilder lens = new StringBuilder(); StringBuilder names = new StringBuilder(); java.util.List dims = v.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.getShortName() : "anon"; names.append(name); lens.append(dim.getLength()); } setDims(names.toString()); setShape(lens.toString()); AxisType at = v.getAxisType(); if (at != null) setAxisType(at.toString()); String p = v.getPositive(); if (p != null) setPositive(p); if (v instanceof CoordinateAxis1D) { CoordinateAxis1D v1 = (CoordinateAxis1D) v; if (v1.isRegular()) setRegular(Double.toString(v1.getIncrement())); } isLayer = (null != axis.findAttribute(_Coordinate.ZisLayer)); isInterval = axis.isInterval(); }
/* 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"); }
public int getRecordCount() { Dimension unlimitedDim = ncfile.getUnlimitedDimension(); return unlimitedDim.getLength(); }
@Test public void testDoradeAir() throws IOException { System.out.println("**** Open " + airDoradeFile); try (NetcdfFile ncfile = NetcdfFile.open(airDoradeFile)) { for (Variable v : ncfile.getVariables()) { System.out.println(v.getFullName()); } Dimension gateDim = ncfile.getRootGroup().findDimension("gate_1"); assert (gateDim.getLength() == 320); Dimension radialDim = ncfile.getRootGroup().findDimension("radial"); assert (radialDim.getLength() == 274); Variable var; var = ncfile.findVariable("elevation"); testReadData(var); var = ncfile.findVariable("azimuth"); testReadData(var); var = ncfile.findVariable("distance_1"); testReadData(var); var = ncfile.findVariable("latitudes_1"); testReadData(var); var = ncfile.findVariable("longitudes_1"); testReadData(var); var = ncfile.findVariable("altitudes_1"); testReadData(var); var = ncfile.findVariable("rays_time"); testReadData(var); var = ncfile.findVariable("Range_to_First_Cell"); float t = testReadScalar(var); assert (t == (float) -2.0); var = ncfile.findVariable("Cell_Spacing"); t = testReadScalar(var); assert (t == (float) 150.0); var = ncfile.findVariable("Fixed_Angle"); t = testReadScalar(var); assert (t == (float) -16.53); var = ncfile.findVariable("Nyquist_Velocity"); t = testReadScalar(var); assert (t == (float) 78.03032); var = ncfile.findVariable("Unambiguous_Range"); t = testReadScalar(var); assert (t == (float) 60.0); var = ncfile.findVariable("Radar_Constant"); t = testReadScalar(var); assert (t == (float) -81.17389); var = ncfile.findVariable("rcvr_gain"); t = testReadScalar(var); assert (t == (float) 32.64); var = ncfile.findVariable("ant_gain"); t = testReadScalar(var); assert (t == (float) 39.35); var = ncfile.findVariable("sys_gain"); t = testReadScalar(var); assert (t == (float) 52.34); var = ncfile.findVariable("bm_width"); t = testReadScalar(var); assert (t == (float) 1.79); var = ncfile.findVariable("VS"); testReadData(var); var = ncfile.findVariable("VL"); testReadData(var); var = ncfile.findVariable("SW"); testReadData(var); var = ncfile.findVariable("VR"); testReadData(var); var = ncfile.findVariable("NCP"); testReadData(var); var = ncfile.findVariable("DBZ"); testReadData(var); var = ncfile.findVariable("VG"); testReadData(var); var = ncfile.findVariable("VT"); testReadData(var); assert (null != var.getDimension(0)); assert (null != var.getDimension(1)); } }
private FeatureType amendGrid( Element gridElem, NetcdfFile ncfile, Group parent, String location) { List<Dimension> unknownDims = new ArrayList<>(); // always has x and y dimension String xdimSizeS = gridElem.getChild("XDim").getText().trim(); String ydimSizeS = gridElem.getChild("YDim").getText().trim(); int xdimSize = Integer.parseInt(xdimSizeS); int ydimSize = Integer.parseInt(ydimSizeS); parent.addDimensionIfNotExists(new Dimension("XDim", xdimSize)); parent.addDimensionIfNotExists(new Dimension("YDim", ydimSize)); /* see HdfEosModisConvention UpperLeftPointMtrs=(-20015109.354000,1111950.519667) LowerRightMtrs=(-18903158.834333,-0.000000) Projection=GCTP_SNSOID ProjParams=(6371007.181000,0,0,0,0,0,0,0,0,0,0,0,0) SphereCode=-1 */ Element proj = gridElem.getChild("Projection"); if (proj != null) { Variable crs = new Variable(ncfile, parent, null, HDFEOS_CRS); crs.setDataType(DataType.SHORT); crs.setDimensions(""); // scalar crs.setCachedData(Array.makeArray(DataType.SHORT, 1, 0, 0)); // fake data parent.addVariable(crs); addAttributeIfExists(gridElem, HDFEOS_CRS_Projection, crs, false); addAttributeIfExists(gridElem, HDFEOS_CRS_UpperLeft, crs, true); addAttributeIfExists(gridElem, HDFEOS_CRS_LowerRight, crs, true); addAttributeIfExists(gridElem, HDFEOS_CRS_ProjParams, crs, true); addAttributeIfExists(gridElem, HDFEOS_CRS_SphereCode, crs, false); } // global Dimensions Element d = gridElem.getChild("Dimension"); List<Element> dims = d.getChildren(); for (Element elem : dims) { String name = elem.getChild("DimensionName").getText().trim(); name = NetcdfFile.makeValidCdmObjectName(name); if (name.equalsIgnoreCase("scalar")) continue; String sizeS = elem.getChild("Size").getText().trim(); int length = Integer.parseInt(sizeS); Dimension old = parent.findDimension(name); if ((old == null) || (old.getLength() != length)) { if (length > 0) { Dimension dim = new Dimension(name, length); if (parent.addDimensionIfNotExists(dim) && showWork) System.out.printf(" Add dimension %s %n", dim); } else { log.warn("Dimension {} has size {} {} ", sizeS, name, location); Dimension udim = new Dimension(name, 1); udim.setGroup(parent); unknownDims.add(udim); if (showWork) System.out.printf(" Add dimension %s %n", udim); } } } // Geolocation Variables Group geoFieldsG = parent.findGroup(GEOLOC_FIELDS); if (geoFieldsG == null) geoFieldsG = parent.findGroup(GEOLOC_FIELDS2); if (geoFieldsG != null) { Element floc = gridElem.getChild("GeoField"); List<Element> varsLoc = floc.getChildren(); for (Element elem : varsLoc) { String varname = elem.getChild("GeoFieldName").getText().trim(); Variable v = geoFieldsG.findVariable(varname); // if (v == null) // v = geoFieldsG.findVariable( H4header.createValidObjectName(varname)); assert v != null : varname; Element dimList = elem.getChild("DimList"); List<Element> values = dimList.getChildren("value"); setSharedDimensions(v, values, unknownDims, location); } } // Data Variables Group dataG = parent.findGroup(DATA_FIELDS); if (dataG == null) dataG = parent.findGroup( DATA_FIELDS2); // eg C:\data\formats\hdf4\eos\mopitt\MOP03M-200501-L3V81.0.1.hdf if (dataG != null) { Element f = gridElem.getChild("DataField"); List<Element> vars = f.getChildren(); for (Element elem : vars) { String varname = elem.getChild("DataFieldName").getText().trim(); varname = NetcdfFile.makeValidCdmObjectName(varname); Variable v = dataG.findVariable(varname); // if (v == null) // v = dataG.findVariable( H4header.createValidObjectName(varname)); assert v != null : varname; Element dimList = elem.getChild("DimList"); List<Element> values = dimList.getChildren("value"); setSharedDimensions(v, values, unknownDims, location); } // get projection String projS = null; Element projElem = gridElem.getChild("Projection"); if (projElem != null) projS = projElem.getText().trim(); boolean isLatLon = "GCTP_GEO".equals(projS); // look for XDim, YDim coordinate variables if (isLatLon) { for (Variable v : dataG.getVariables()) { if (v.isCoordinateVariable()) { if (v.getShortName().equals("YDim")) { v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString())); v.addAttribute(new Attribute(CDM.UNITS, CDM.LAT_UNITS)); } if (v.getShortName().equals("XDim")) v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString())); } } } } return FeatureType.GRID; }
private FeatureType amendSwath(NetcdfFile ncfile, Element swathElem, Group parent) { FeatureType featureType = FeatureType.SWATH; List<Dimension> unknownDims = new ArrayList<>(); // Dimensions Element d = swathElem.getChild("Dimension"); List<Element> dims = d.getChildren(); for (Element elem : dims) { String name = elem.getChild("DimensionName").getText().trim(); name = NetcdfFile.makeValidCdmObjectName(name); if (name.equalsIgnoreCase("scalar")) continue; String sizeS = elem.getChild("Size").getText().trim(); int length = Integer.parseInt(sizeS); if (length > 0) { Dimension dim = parent.findDimensionLocal(name); if (dim != null) { // already added - may be dimension scale ? if (dim.getLength() != length) { // ok as long as it matches log.error("Conflicting Dimensions = {} {}", dim, ncfile.getLocation()); throw new IllegalStateException("Conflicting Dimensions = " + name); } } else { dim = new Dimension(name, length); if (parent.addDimensionIfNotExists(dim) && showWork) System.out.printf(" Add dimension %s %n", dim); } } else { log.warn("Dimension " + name + " has size " + sizeS, ncfile.getLocation()); Dimension udim = new Dimension(name, 1); udim.setGroup(parent); unknownDims.add(udim); if (showWork) System.out.printf(" Add dimension %s %n", udim); } } // Dimension Maps Element dmap = swathElem.getChild("DimensionMap"); List<Element> dimMaps = dmap.getChildren(); for (Element elem : dimMaps) { String geoDimName = elem.getChild("GeoDimension").getText().trim(); geoDimName = NetcdfFile.makeValidCdmObjectName(geoDimName); String dataDimName = elem.getChild("DataDimension").getText().trim(); dataDimName = NetcdfFile.makeValidCdmObjectName(dataDimName); String offsetS = elem.getChild("Offset").getText().trim(); String incrS = elem.getChild("Increment").getText().trim(); int offset = Integer.parseInt(offsetS); int incr = Integer.parseInt(incrS); // make new variable for this dimension map Variable v = new Variable(ncfile, parent, null, dataDimName); v.setDimensions(geoDimName); v.setDataType(DataType.INT); int npts = (int) v.getSize(); Array data = Array.makeArray(v.getDataType(), npts, offset, incr); v.setCachedData(data, true); v.addAttribute(new Attribute("_DimensionMap", "")); parent.addVariable(v); if (showWork) System.out.printf(" Add dimensionMap %s %n", v); } // Geolocation Variables Group geoFieldsG = parent.findGroup(GEOLOC_FIELDS); if (geoFieldsG == null) geoFieldsG = parent.findGroup(GEOLOC_FIELDS2); if (geoFieldsG != null) { Variable latAxis = null, lonAxis = null; Element floc = swathElem.getChild("GeoField"); List<Element> varsLoc = floc.getChildren(); for (Element elem : varsLoc) { String varname = elem.getChild("GeoFieldName").getText().trim(); Variable v = geoFieldsG.findVariable(varname); // if (v == null) // v = geoFieldsG.findVariable( H4header.createValidObjectName(varname)); assert v != null : varname; AxisType axis = addAxisType(ncfile, v); if (axis == AxisType.Lat) latAxis = v; if (axis == AxisType.Lon) lonAxis = v; Element dimList = elem.getChild("DimList"); List<Element> values = dimList.getChildren("value"); setSharedDimensions(v, values, unknownDims, ncfile.getLocation()); if (showWork) System.out.printf(" set coordinate %s %n", v); } if ((latAxis != null) && (lonAxis != null)) { List<Dimension> xyDomain = CoordinateSystem.makeDomain(new Variable[] {latAxis, lonAxis}); if (xyDomain.size() < 2) featureType = FeatureType.PROFILE; // ?? } } // Data Variables Group dataG = parent.findGroup(DATA_FIELDS); if (dataG == null) dataG = parent.findGroup(DATA_FIELDS2); if (dataG != null) { Element f = swathElem.getChild("DataField"); List<Element> vars = f.getChildren(); for (Element elem : vars) { Element dataFieldNameElem = elem.getChild("DataFieldName"); if (dataFieldNameElem == null) continue; String varname = NetcdfFile.makeValidCdmObjectName(dataFieldNameElem.getText().trim()); Variable v = dataG.findVariable(varname); // if (v == null) // v = dataG.findVariable( H4header.createValidObjectName(varname)); if (v == null) { log.error("Cant find variable {} {}", varname, ncfile.getLocation()); continue; } Element dimList = elem.getChild("DimList"); List<Element> values = dimList.getChildren("value"); setSharedDimensions(v, values, unknownDims, ncfile.getLocation()); } } return featureType; }
public void testWritePermute() throws Exception { NetcdfFileWriteable ncfile = new NetcdfFileWriteable(); ncfile.setName(TestLocal.cdmTestDataDir + "permuteTest.nc"); // define dimensions Dimension xDim = ncfile.addDimension("x", 3); Dimension yDim = ncfile.addDimension("y", 5); Dimension zDim = ncfile.addDimension("z", 4); Dimension tDim = ncfile.addDimension("time", 2); // define Variables ncfile.addVariable("time", double.class, new Dimension[] {tDim}); ncfile.addVariableAttribute("time", "units", "secs since 1-1-1 00:00"); ncfile.addVariable("z", double.class, new Dimension[] {zDim}); ncfile.addVariableAttribute("z", "units", "meters"); ncfile.addVariableAttribute("z", "positive", "up"); ncfile.addVariable("y", double.class, new Dimension[] {yDim}); ncfile.addVariableAttribute("y", "units", "degrees_north"); ncfile.addVariable("x", double.class, new Dimension[] {xDim}); ncfile.addVariableAttribute("x", "units", "degrees_east"); ncfile.addVariable("tzyx", double.class, new Dimension[] {tDim, zDim, yDim, xDim}); ncfile.addVariableAttribute("tzyx", "units", "K"); ncfile.addVariable("tzxy", double.class, new Dimension[] {tDim, zDim, xDim, yDim}); ncfile.addVariableAttribute("tzxy", "units", "K"); ncfile.addVariable("tyxz", double.class, new Dimension[] {tDim, yDim, xDim, zDim}); ncfile.addVariableAttribute("tyxz", "units", "K"); ncfile.addVariable("txyz", double.class, new Dimension[] {tDim, xDim, yDim, zDim}); ncfile.addVariableAttribute("txyz", "units", "K"); ncfile.addVariable("zyxt", double.class, new Dimension[] {zDim, yDim, xDim, tDim}); ncfile.addVariableAttribute("zyxt", "units", "K"); ncfile.addVariable("zxyt", double.class, new Dimension[] {zDim, xDim, yDim, tDim}); ncfile.addVariableAttribute("zxyt", "units", "K"); ncfile.addVariable("yxzt", double.class, new Dimension[] {yDim, xDim, zDim, tDim}); ncfile.addVariableAttribute("yxzt", "units", "K"); ncfile.addVariable("xyzt", double.class, new Dimension[] {xDim, yDim, zDim, tDim}); ncfile.addVariableAttribute("xyzt", "units", "K"); // missing one dimension ncfile.addVariable("zyx", double.class, new Dimension[] {zDim, yDim, xDim}); ncfile.addVariable("txy", double.class, new Dimension[] {tDim, xDim, yDim}); ncfile.addVariable("yxz", double.class, new Dimension[] {yDim, xDim, zDim}); ncfile.addVariable("xzy", double.class, new Dimension[] {xDim, zDim, yDim}); ncfile.addVariable("yxt", double.class, new Dimension[] {yDim, xDim, tDim}); ncfile.addVariable("xyt", double.class, new Dimension[] {xDim, yDim, tDim}); ncfile.addVariable("xyz", double.class, new Dimension[] {xDim, yDim, zDim}); // missing two dimension ncfile.addVariable("yx", double.class, new Dimension[] {yDim, xDim}); ncfile.addVariable("xy", double.class, new Dimension[] {xDim, yDim}); ncfile.addVariable("yz", double.class, new Dimension[] {yDim, zDim}); ncfile.addVariable("xz", double.class, new Dimension[] {xDim, zDim}); ncfile.addVariable("yt", double.class, new Dimension[] {yDim, tDim}); ncfile.addVariable("xt", double.class, new Dimension[] {xDim, tDim}); ncfile.addVariable("ty", double.class, new Dimension[] {tDim, yDim}); ncfile.addVariable("tx", double.class, new Dimension[] {tDim, xDim}); // add global attributes ncfile.addGlobalAttribute("Convention", "COARDS"); // create the file try { ncfile.create(); } catch (IOException e) { System.err.println("ERROR creating file"); assert (false); } // write time data int len = tDim.getLength(); ArrayDouble A = new ArrayDouble.D1(len); Index ima = A.getIndex(); for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 3600)); int[] origin = new int[1]; try { ncfile.write("time", origin, A); } catch (IOException e) { System.err.println("ERROR writing time"); assert (false); } // write z data len = zDim.getLength(); A = new ArrayDouble.D1(len); ima = A.getIndex(); for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 10)); try { ncfile.write("z", origin, A); } catch (IOException e) { System.err.println("ERROR writing z"); assert (false); } // write y data len = yDim.getLength(); A = new ArrayDouble.D1(len); ima = A.getIndex(); for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 3)); try { ncfile.write("y", origin, A); } catch (IOException e) { System.err.println("ERROR writing y"); assert (false); } // write x data len = xDim.getLength(); A = new ArrayDouble.D1(len); ima = A.getIndex(); for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 5)); try { ncfile.write("x", origin, A); } catch (IOException e) { System.err.println("ERROR writing x"); assert (false); } // write tzyx data doWrite4(ncfile, "tzyx"); doWrite4(ncfile, "tzxy"); doWrite4(ncfile, "txyz"); doWrite4(ncfile, "tyxz"); doWrite4(ncfile, "zyxt"); doWrite4(ncfile, "zxyt"); doWrite4(ncfile, "xyzt"); doWrite4(ncfile, "yxzt"); doWrite3(ncfile, "zyx"); doWrite3(ncfile, "txy"); doWrite3(ncfile, "yxz"); doWrite3(ncfile, "xzy"); doWrite3(ncfile, "yxt"); doWrite3(ncfile, "xyt"); doWrite3(ncfile, "yxt"); doWrite3(ncfile, "xyz"); doWrite2(ncfile, "yx"); doWrite2(ncfile, "xy"); doWrite2(ncfile, "yz"); doWrite2(ncfile, "xz"); doWrite2(ncfile, "yt"); doWrite2(ncfile, "xt"); doWrite2(ncfile, "ty"); doWrite2(ncfile, "tx"); if (show) System.out.println("ncfile = " + ncfile); // all done try { ncfile.close(); } catch (IOException e) { System.err.println("ERROR writing file"); assert (false); } System.out.println("*****************Test Write done"); }
public void testWriteStandardVar() throws Exception { NetcdfFileWriteable ncfile = new NetcdfFileWriteable(filename, false); // define dimensions Dimension latDim = ncfile.addDimension("lat", 2); Dimension lonDim = ncfile.addDimension("lon", 3); ArrayList dims = new ArrayList(); dims.add(latDim); dims.add(lonDim); // case 1 ncfile.addVariable("t1", DataType.DOUBLE, dims); ncfile.addVariableAttribute("t1", CDM.SCALE_FACTOR, new Double(2.0)); ncfile.addVariableAttribute("t1", "add_offset", new Double(77.0)); // case 2 ncfile.addVariable("t2", DataType.BYTE, dims); ncfile.addVariableAttribute("t2", CDM.SCALE_FACTOR, new Short((short) 2)); ncfile.addVariableAttribute("t2", "add_offset", new Short((short) 77)); // case 3 ncfile.addVariable("t3", DataType.BYTE, dims); ncfile.addVariableAttribute("t3", "_FillValue", new Byte((byte) 255)); // case 4 ncfile.addVariable("t4", DataType.SHORT, dims); ncfile.addVariableAttribute("t4", CDM.MISSING_VALUE, new Short((short) -9999)); // case 5 ncfile.addVariable("t5", DataType.SHORT, dims); ncfile.addVariableAttribute("t5", CDM.MISSING_VALUE, new Short((short) -9999)); ncfile.addVariableAttribute("t5", CDM.SCALE_FACTOR, new Short((short) 2)); ncfile.addVariableAttribute("t5", "add_offset", new Short((short) 77)); // case 1 ncfile.addVariable("m1", DataType.DOUBLE, dims); ncfile.addVariableAttribute("m1", CDM.MISSING_VALUE, -999.99); // create the file ncfile.create(); // write t1 ArrayDouble A = new ArrayDouble.D2(latDim.getLength(), lonDim.getLength()); int i, j; Index ima = A.getIndex(); // write for (i = 0; i < latDim.getLength(); i++) for (j = 0; j < lonDim.getLength(); j++) A.setDouble(ima.set(i, j), (double) (i * 10.0 + j)); int[] origin = new int[2]; ncfile.write("t1", origin, A); // write t2 ArrayByte Ab = new ArrayByte.D2(latDim.getLength(), lonDim.getLength()); ima = Ab.getIndex(); for (i = 0; i < latDim.getLength(); i++) for (j = 0; j < lonDim.getLength(); j++) Ab.setByte(ima.set(i, j), (byte) (i * 10 + j)); ncfile.write("t2", origin, Ab); // write t3 ncfile.write("t3", origin, Ab); // write t4 Array As = new ArrayShort.D2(latDim.getLength(), lonDim.getLength()); ima = As.getIndex(); for (i = 0; i < latDim.getLength(); i++) for (j = 0; j < lonDim.getLength(); j++) As.setShort(ima.set(i, j), (short) (i * 10 + j)); ncfile.write("t4", origin, As); As.setShort(ima.set(0, 0), (short) -9999); ncfile.write("t5", origin, As); // write m1 ArrayDouble.D2 Ad = new ArrayDouble.D2(latDim.getLength(), lonDim.getLength()); for (i = 0; i < latDim.getLength(); i++) for (j = 0; j < lonDim.getLength(); j++) Ad.setDouble(ima.set(i, j), (double) (i * 10.0 + j)); Ad.set(1, 1, -999.99); ncfile.write("m1", new int[2], Ad); // all done ncfile.close(); System.out.println("**************TestStandardVar Write done"); }
@Override public Product createProduct() throws ProductIOException { int[] dims; int sceneHeight = 0; int sceneWidth = 0; Group geodata = ncFile.findGroup("geophysical_data"); if (productReader.getProductType() == SeadasProductReader.ProductType.OISST) { dims = ncFile.getVariables().get(4).getShape(); sceneHeight = dims[2]; sceneWidth = dims[3]; mustFlipY = true; } else if (productReader.getProductType() == SeadasProductReader.ProductType.ANCCLIM) { List<Variable> vars = ncFile.getVariables(); for (Variable v : vars) { if (v.getRank() == 2) { dims = v.getShape(); sceneHeight = dims[0]; sceneWidth = dims[1]; } } } else { if (geodata != null) { dims = geodata.getVariables().get(0).getShape(); sceneHeight = dims[0]; sceneWidth = dims[1]; } else { ucar.nc2.Dimension latdim = ncFile.findDimension("lat"); ucar.nc2.Dimension londim = ncFile.findDimension("lon"); if (latdim != null) { sceneHeight = latdim.getLength(); sceneWidth = londim.getLength(); } else { dims = ncFile.getVariables().get(0).getShape(); sceneHeight = dims[0]; sceneWidth = dims[1]; } } } String productName = productReader.getInputFile().getName(); try { productName = getStringAttribute("Product_Name"); } catch (Exception ignored) { } SeadasProductReader.ProductType productType = productReader.getProductType(); Product product = new Product(productName, productType.toString(), sceneWidth, sceneHeight); product.setDescription(productName); product.setFileLocation(productReader.getInputFile()); product.setProductReader(productReader); addGlobalMetadata(product); addSmiMetadata(product); // variableMap = addBands(product, ncFile.getVariables()); variableMap = addSmiBands(product, ncFile.getVariables()); try { addGeocoding(product); } catch (Exception ignored) { } addFlagsAndMasks(product); if (productReader.getProductType() == SeadasProductReader.ProductType.Bathy) { mustFlipY = true; Dimension tileSize = new Dimension(640, 320); product.setPreferredTileSize(tileSize); } return product; }
@Test public void testDoradeGround() throws IOException { System.out.println("**** Open " + groundDoradeFile); try (NetcdfFile ncfile = NetcdfFile.open(groundDoradeFile)) { for (Variable v : ncfile.getVariables()) { System.out.println(v.getFullName()); } /* test both gate and radial dimension */ Dimension gateDim = ncfile.getRootGroup().findDimension("gate_1"); assert (gateDim.getLength() == 1008); Dimension radialDim = ncfile.getRootGroup().findDimension("radial"); assert (radialDim.getLength() == 439); /* test some att */ Attribute testAtt = ncfile.getRootGroup().findAttribute("Conventions"); assert (testAtt.getStringValue().equals(_Coordinate.Convention)); testAtt = ncfile.getRootGroup().findAttribute("format"); assert (testAtt.getStringValue().equals("Unidata/netCDF/Dorade")); testAtt = ncfile.getRootGroup().findAttribute("Project_name"); assert (testAtt.getStringValue().equalsIgnoreCase("IHOP_2002")); testAtt = ncfile.getRootGroup().findAttribute("Radar_Name"); assert (testAtt.getStringValue().equalsIgnoreCase("SPOL")); testAtt = ncfile.getRootGroup().findAttribute("VolumeCoveragePatternName"); assert (testAtt.getStringValue().equalsIgnoreCase("SUR")); testAtt = ncfile.getRootGroup().findAttribute("Volume_Number"); assert (testAtt.getStringValue().equalsIgnoreCase("1")); testAtt = ncfile.getRootGroup().findAttribute("Sweep_Number"); assert (testAtt.getStringValue().equalsIgnoreCase("2")); testAtt = ncfile.getRootGroup().findAttribute("Sweep_Date"); assert (testAtt.getStringValue().equalsIgnoreCase("2002-05-11 01:58:15Z")); Variable var; var = ncfile.findVariable("elevation"); testReadData(var); var = ncfile.findVariable("azimuth"); testReadData(var); var = ncfile.findVariable("distance_1"); testReadData(var); var = ncfile.findVariable("latitudes_1"); testReadData(var); var = ncfile.findVariable("longitudes_1"); testReadData(var); var = ncfile.findVariable("altitudes_1"); testReadData(var); var = ncfile.findVariable("rays_time"); testReadData(var); var = ncfile.findVariable("Range_to_First_Cell"); float t = testReadScalar(var); assert (t == (float) 150.0); var = ncfile.findVariable("Cell_Spacing"); t = testReadScalar(var); assert (t == (float) 149.89624); var = ncfile.findVariable("Fixed_Angle"); t = testReadScalar(var); assert (t == (float) 1.1975098); var = ncfile.findVariable("Nyquist_Velocity"); t = testReadScalar(var); assert (t == (float) 25.618269); var = ncfile.findVariable("Unambiguous_Range"); t = testReadScalar(var); assert (t == (float) 156.11694); var = ncfile.findVariable("Radar_Constant"); t = testReadScalar(var); assert (t == (float) 70.325195); var = ncfile.findVariable("rcvr_gain"); t = testReadScalar(var); assert (t == (float) 46.95); var = ncfile.findVariable("ant_gain"); t = testReadScalar(var); assert (t == (float) 45.58); var = ncfile.findVariable("sys_gain"); t = testReadScalar(var); assert (t == (float) 46.95); var = ncfile.findVariable("bm_width"); t = testReadScalar(var); assert (t == (float) 0.92); var = ncfile.findVariable("VE"); testReadData(var); var = ncfile.findVariable("DM"); testReadData(var); var = ncfile.findVariable("NCP"); testReadData(var); var = ncfile.findVariable("SW"); testReadData(var); var = ncfile.findVariable("DZ"); testReadData(var); var = ncfile.findVariable("DCZ"); testReadData(var); var = ncfile.findVariable("LVDR"); testReadData(var); var = ncfile.findVariable("NIQ"); testReadData(var); var = ncfile.findVariable("AIQ"); testReadData(var); var = ncfile.findVariable("CH"); testReadData(var); var = ncfile.findVariable("AH"); testReadData(var); var = ncfile.findVariable("CV"); testReadData(var); var = ncfile.findVariable("AV"); testReadData(var); var = ncfile.findVariable("RHOHV"); testReadData(var); var = ncfile.findVariable("LDR"); testReadData(var); var = ncfile.findVariable("DL"); testReadData(var); var = ncfile.findVariable("DX"); testReadData(var); var = ncfile.findVariable("ZDR"); testReadData(var); var = ncfile.findVariable("PHI"); testReadData(var); var = ncfile.findVariable("KDP"); testReadData(var); assert (0 == var.findDimensionIndex("radial")); assert (1 == var.findDimensionIndex("gate_1")); } }
/** * Parses a variable recursively into appropriate ViewVariable implementations * * @throws IOException * @throws */ private static AbstractViewVariable parseVariableRecursive(Variable var) throws IOException { List<Dimension> dimensions = var.getDimensions(); // A single dimension means we can parse a SimpleAxis if (dimensions.size() == 1) { SimpleAxis axis = new SimpleAxis(var.getName(), var.getDataType().name(), var.getUnitsString(), null, null); Dimension d = dimensions.get(0); axis.setDimensionBounds(new SimpleBounds(0, d.getLength())); // Read our first and last values Array first = null, last = null; try { first = var.read(new int[] {0}, new int[] {1}); last = var.read(new int[] {d.getLength() - 1}, new int[] {1}); } catch (InvalidRangeException ex) { throw new IllegalArgumentException( String.format("Unable to read variable ranges '%1$s'", var), ex); } axis.setValueBounds(new SimpleBounds(first.getDouble(0), last.getDouble(0))); return axis; // Otherwise we have a multi dimensional variable that we can parse as a grid } else if (dimensions.size() > 0) { SimpleGrid grid = new SimpleGrid(var.getName(), var.getDataType().name(), var.getUnitsString(), null); List<AbstractViewVariable> childAxes = new ArrayList<>(); // Recursively parse each dimension (which should map to a variable in the parent group) for (Dimension d : dimensions) { Variable mappedVariable = d.getGroup().findVariable(d.getName()); if (mappedVariable == null) { // If the dimension doesn't map to a variable, we can't pull much information out of it // So instead we'll have to introduce an axis that only includes dimension bounds log.warn( String.format( "Dimension '%1$s' has no matching variable in parent group '%2$s'", d, d.getGroup())); SimpleAxis axis = new SimpleAxis(d.getName(), DataType.FLOAT.name(), "????", null, null); axis.setDimensionBounds(new SimpleBounds(0, d.getLength() - 1)); childAxes.add(axis); } else { AbstractViewVariable parsedVar = parseVariableRecursive(mappedVariable); if (parsedVar != null) childAxes.add(parsedVar); } } if (childAxes.size() > 0) { grid.setAxes(childAxes.toArray(new AbstractViewVariable[childAxes.size()])); return grid; } else { return null; } } else { // Currently unsupported... log.warn( String.format("Variables with 0 dimensions are currently unsupported. var='%1$s'", var)); return null; } }
public static void main2(String args[]) throws Exception { final int NLVL = 2; final int NLAT = 6; final int NLON = 12; final int NumberOfRecords = 2; final float SAMPLE_PRESSURE = 900.0f; final float SAMPLE_TEMP = 9.0f; final float START_LAT = 25.0f; final float START_LON = -125.0f; // Create the file. String filename = "pres_temp_4D.nc"; NetcdfFileWriteable dataFile = null; try { // Create new netcdf-3 file with the given filename dataFile = NetcdfFileWriteable.createNew(filename, false); // add dimensions where time dimension is unlimit Dimension lvlDim = dataFile.addDimension("level", NLVL); Dimension latDim = dataFile.addDimension("latitude", NLAT); Dimension lonDim = dataFile.addDimension("longitude", NLON); Dimension timeDim = dataFile.addDimension("time", 1000); // should // not be // need // second // argument ArrayList dims = null; // Define the coordinate variables. dataFile.addVariable("latitude", DataType.FLOAT, new Dimension[] {latDim}); dataFile.addVariable("longitude", DataType.FLOAT, new Dimension[] {lonDim}); // Define units attributes for data variables. dataFile.addVariableAttribute("latitude", "units", "degrees_north"); dataFile.addVariableAttribute("longitude", "units", "degrees_east"); // Define the netCDF variables for the pressure and temperature // data. dims = new ArrayList(); dims.add(timeDim); dims.add(lvlDim); dims.add(latDim); dims.add(lonDim); dataFile.addVariable("pressure", DataType.FLOAT, dims); dataFile.addVariable("temperature", DataType.FLOAT, dims); // Define units attributes for data variables. dataFile.addVariableAttribute("pressure", "units", "hPa"); dataFile.addVariableAttribute("temperature", "units", "celsius"); // Create some pretend data. If this wasn't an example program, we // would have some real data to write for example, model output. ArrayFloat.D1 lats = new ArrayFloat.D1(latDim.getLength()); ArrayFloat.D1 lons = new ArrayFloat.D1(lonDim.getLength()); int i, j; for (i = 0; i < latDim.getLength(); i++) { lats.set(i, START_LAT + 5.f * i); } for (j = 0; j < lonDim.getLength(); j++) { lons.set(j, START_LON + 5.f * j); } // Create the pretend data. This will write our surface pressure and // surface temperature data. ArrayFloat.D4 dataTemp = new ArrayFloat.D4( NumberOfRecords, lvlDim.getLength(), latDim.getLength(), lonDim.getLength()); ArrayFloat.D4 dataPres = new ArrayFloat.D4( NumberOfRecords, lvlDim.getLength(), latDim.getLength(), lonDim.getLength()); for (int record = 0; record < NumberOfRecords; record++) { i = 0; for (int lvl = 0; lvl < NLVL; lvl++) for (int lat = 0; lat < NLAT; lat++) for (int lon = 0; lon < NLON; lon++) { dataPres.set(record, lvl, lat, lon, SAMPLE_PRESSURE + i); dataTemp.set(record, lvl, lat, lon, SAMPLE_TEMP + i++); } } // Create the file. At this point the (empty) file will be written // to disk dataFile.create(); // A newly created Java integer array to be initialized to zeros. int[] origin = new int[4]; dataFile.write("latitude", lats); dataFile.write("longitude", lons); dataFile.write("pressure", origin, dataPres); dataFile.write("temperature", origin, dataTemp); dataFile.close(); } catch (IOException e) { e.printStackTrace(System.err); } catch (InvalidRangeException e) { e.printStackTrace(System.err); } finally { // if (dataFile != null) { // try { // dataFile.close(); // } catch (IOException ioe) { // ioe.printStackTrace(); // } // } } System.out.println("*** SUCCESS writing example file " + filename); }
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); }
/** * This reads an arbitrary data slice, returning the data in canonical order (rt-e-t-z-y-x). If * any dimension does not exist, ignore it. * * @param rt if < 0, get all of runtime dim; if valid index, fix slice to that value. * @param e if < 0, get all of ensemble dim; if valid index, fix slice to that value. * @param t if < 0, get all of time dim; if valid index, fix slice to that value. * @param z if < 0, get all of z dim; if valid index, fix slice to that value. * @param y if < 0, get all of y dim; if valid index, fix slice to that value. * @param x if < 0, get all of x dim; if valid index, fix slice to that value. * @return data[rt,e,t,z,y,x], eliminating missing or fixed dimension. */ public Array readDataSlice(int rt, int e, int t, int z, int y, int x) throws java.io.IOException { int rank = vs.getRank(); int[] start = new int[rank]; int[] shape = new int[rank]; for (int i = 0; i < rank; i++) { start[i] = 0; shape[i] = 1; } Dimension xdim = getXDimension(); Dimension ydim = getYDimension(); Dimension zdim = getZDimension(); Dimension tdim = getTimeDimension(); Dimension edim = getEnsembleDimension(); Dimension rtdim = getRunTimeDimension(); // construct the shape of the data volume to be read if (rtdim != null) { if ((rt >= 0) && (rt < rtdim.getLength())) start[rtDimOrgIndex] = rt; // fix rt else { shape[rtDimOrgIndex] = rtdim.getLength(); // all of rt } } if (edim != null) { if ((e >= 0) && (e < edim.getLength())) start[eDimOrgIndex] = e; // fix e else { shape[eDimOrgIndex] = edim.getLength(); // all of e } } if (tdim != null) { if ((t >= 0) && (t < tdim.getLength())) start[tDimOrgIndex] = t; // fix t else { shape[tDimOrgIndex] = tdim.getLength(); // all of t } } if (zdim != null) { if ((z >= 0) && (z < zdim.getLength())) start[zDimOrgIndex] = z; // fix z else { shape[zDimOrgIndex] = zdim.getLength(); // all of z } } if (ydim != null) { if ((y >= 0) && (y < ydim.getLength())) start[yDimOrgIndex] = y; // fix y else { shape[yDimOrgIndex] = ydim.getLength(); // all of y } } if (xdim != null) { if ((x >= 0) && (x < xdim.getLength())) // all of x start[xDimOrgIndex] = x; // fix x else { shape[xDimOrgIndex] = xdim.getLength(); // all of x } } if (debugArrayShape) { System.out.println("read shape from org variable = "); for (int i = 0; i < rank; i++) System.out.println( " start = " + start[i] + " shape = " + shape[i] + " name = " + vs.getDimension(i).getName()); } // read it Array dataVolume; try { dataVolume = vs.read(start, shape); } catch (Exception ex) { log.error( "GeoGrid.getdataSlice() on dataset " + getFullName() + " " + dataset.getLocation(), ex); throw new java.io.IOException(ex.getMessage()); } // LOOK: the real problem is the lack of named dimensions in the Array object // figure out correct permutation for canonical ordering for permute List<Dimension> oldDims = new ArrayList<Dimension>(vs.getDimensions()); int[] permuteIndex = new int[dataVolume.getRank()]; int count = 0; if (oldDims.contains(rtdim)) permuteIndex[count++] = oldDims.indexOf(rtdim); if (oldDims.contains(edim)) permuteIndex[count++] = oldDims.indexOf(edim); if (oldDims.contains(tdim)) permuteIndex[count++] = oldDims.indexOf(tdim); if (oldDims.contains(zdim)) permuteIndex[count++] = oldDims.indexOf(zdim); if (oldDims.contains(ydim)) permuteIndex[count++] = oldDims.indexOf(ydim); if (oldDims.contains(xdim)) permuteIndex[count] = oldDims.indexOf(xdim); if (debugArrayShape) { System.out.println("oldDims = "); for (Dimension oldDim : oldDims) System.out.println(" oldDim = " + oldDim.getName()); System.out.println("permute dims = "); for (int aPermuteIndex : permuteIndex) System.out.println(" oldDim index = " + aPermuteIndex); } // check to see if we need to permute boolean needPermute = false; for (int i = 0; i < permuteIndex.length; i++) { if (i != permuteIndex[i]) needPermute = true; } // permute to the order rt,e,t,z,y,x if (needPermute) dataVolume = dataVolume.permute(permuteIndex); // eliminate fixed dimensions, but not all dimensions of length 1. count = 0; if (rtdim != null) { if (rt >= 0) dataVolume = dataVolume.reduce(count); else count++; } if (edim != null) { if (e >= 0) dataVolume = dataVolume.reduce(count); else count++; } if (tdim != null) { if (t >= 0) dataVolume = dataVolume.reduce(count); else count++; } if (zdim != null) { if (z >= 0) dataVolume = dataVolume.reduce(count); else count++; } if (ydim != null) { if (y >= 0) dataVolume = dataVolume.reduce(count); else count++; } if (xdim != null) { if (x >= 0) dataVolume = dataVolume.reduce(count); } return dataVolume; }
/** * Build the configuration from the dataset * * @param ncd NetcdfDataset * @return the trajectory configuration */ private static Config buildConfig(NetcdfDataset ncd) { // already did this in isValid, but we'll keep here for later refactor Attribute attrib = ncd.findGlobalAttributeIgnoreCase("center"); if (attrib == null) { return null; } if (!attrib.isString()) { return null; } if (!attrib.getStringValue().equals("UCAR/CDAAC")) { return null; } // Check for start_time, stop_time attrib = ncd.findGlobalAttributeIgnoreCase("start_time"); if (attrib == null) { return null; } if (attrib.isString()) { return null; } double startTime = attrib.getNumericValue().doubleValue(); attrib = ncd.findGlobalAttributeIgnoreCase("stop_time"); if (attrib == null) { return null; } if (attrib.isString()) { return null; } double endTime = attrib.getNumericValue().doubleValue(); // Check that only one dimension and that it is the alt dimension. List list = ncd.getRootGroup().getDimensions(); if (list.size() != 1) { return null; } Dimension d = (Dimension) list.get(0); if (!d.getName().equals(timeDimName)) { return null; } Config trajConfig = new Config(); trajConfig.setTimeDim(d); // Check for latitude variable with time dimension and units convertable to "degrees_north". Variable var = ncd.getRootGroup().findVariable(latVarName); if (var == null) { return null; } list = var.getDimensions(); if (list.size() != 1) { return null; } d = (Dimension) list.get(0); if (!d.getName().equals(timeDimName)) { return null; } String units = var.findAttribute("units").getStringValue(); if (!SimpleUnit.isCompatible(units, "degrees_north")) { return null; } trajConfig.setLatVar(var); // Make the time Variable int numTimes = d.getLength(); double[] times = new double[numTimes]; // Variable timeVar = new Variable(var); // timeVar.setName(timeVarName); VariableDS timeVar = new VariableDS( ncd, ncd.getRootGroup(), null, timeVarName, DataType.DOUBLE, timeDimName, "seconds since 1980-01-06 00:00:00", "Time coordinate"); // Variable timeVar = new Variable(ncd, ncd.getRootGroup(), null, // timeVarName); // timeVar.setDataType(DataType.DOUBLE); // timeVar.setDimensions(list); // Attribute newUnits = // new Attribute("units", "seconds since 1980-01-06 00:00:00"); // timeVar.addAttribute(newUnits); timeVar.setCachedData( Array.makeArray(DataType.DOUBLE, numTimes, endTime, ((startTime - endTime) / numTimes)), true); ncd.addVariable(ncd.getRootGroup(), timeVar); trajConfig.setTimeVar(timeVar); // Check for longitude variable with time dimension and units convertable to "degrees_east". var = ncd.getRootGroup().findVariable(lonVarName); if (var == null) { return null; } list = var.getDimensions(); if (list.size() != 1) { return null; } d = (Dimension) list.get(0); if (!d.getName().equals(timeDimName)) { return null; } units = var.findAttribute("units").getStringValue(); if (!SimpleUnit.isCompatible(units, "degrees_east")) { return null; } trajConfig.setLonVar(var); // Check for altitude variable with time dimension and units convertable to "m". var = ncd.getRootGroup().findVariable(elevVarName); if (var == null) { return null; } list = var.getDimensions(); if (list.size() != 1) { return null; } d = (Dimension) list.get(0); if (!d.getName().equals(timeDimName)) { return null; } units = var.findAttribute("units").getStringValue(); if (!SimpleUnit.isCompatible(units, "meters")) { return null; } trajConfig.setElevVar(var); trajConfig.setTrajectoryId(trajId); return trajConfig; }
Write2ncRect(NetcdfFile bufr, String fileOutName, boolean fill) throws IOException, InvalidRangeException { NetcdfFileWriteable ncfile = NetcdfFileWriteable.createNew(fileOutName, fill); if (debug) { System.out.println("FileWriter write " + bufr.getLocation() + " to " + fileOutName); } // global attributes List<Attribute> glist = bufr.getGlobalAttributes(); for (Attribute att : glist) { String useName = N3iosp.makeValidNetcdfObjectName(att.getName()); Attribute useAtt; if (att.isArray()) useAtt = ncfile.addGlobalAttribute(useName, att.getValues()); else if (att.isString()) useAtt = ncfile.addGlobalAttribute(useName, att.getStringValue()); else useAtt = ncfile.addGlobalAttribute(useName, att.getNumericValue()); if (debug) System.out.println("add gatt= " + useAtt); } // global dimensions Dimension recordDim = null; Map<String, Dimension> dimHash = new HashMap<String, Dimension>(); for (Dimension oldD : bufr.getDimensions()) { String useName = N3iosp.makeValidNetcdfObjectName(oldD.getName()); boolean isRecord = useName.equals("record"); Dimension newD = ncfile.addDimension(useName, oldD.getLength(), true, false, false); dimHash.put(newD.getName(), newD); if (isRecord) recordDim = newD; if (debug) System.out.println("add dim= " + newD); } // Variables Structure recordStruct = (Structure) bufr.findVariable(BufrIosp.obsRecord); for (Variable oldVar : recordStruct.getVariables()) { if (oldVar.getDataType() == DataType.STRUCTURE) continue; String varName = N3iosp.makeValidNetcdfObjectName(oldVar.getShortName()); DataType newType = oldVar.getDataType(); List<Dimension> newDims = new ArrayList<Dimension>(); newDims.add(recordDim); for (Dimension dim : oldVar.getDimensions()) { newDims.add(ncfile.addDimension(oldVar.getShortName() + "_strlen", dim.getLength())); } Variable newVar = ncfile.addVariable(varName, newType, newDims); if (debug) System.out.println("add var= " + newVar); // attributes List<Attribute> attList = oldVar.getAttributes(); for (Attribute att : attList) { String useName = N3iosp.makeValidNetcdfObjectName(att.getName()); if (att.isArray()) ncfile.addVariableAttribute(varName, useName, att.getValues()); else if (att.isString()) ncfile.addVariableAttribute(varName, useName, att.getStringValue()); else ncfile.addVariableAttribute(varName, useName, att.getNumericValue()); } } // int max_seq = countSeq(recordStruct); // Dimension seqD = ncfile.addDimension("level", max_seq); for (Variable v : recordStruct.getVariables()) { if (v.getDataType() != DataType.STRUCTURE) continue; String structName = N3iosp.makeValidNetcdfObjectName(v.getShortName()); int shape[] = v.getShape(); Dimension structDim = ncfile.addDimension(structName, shape[0]); Structure struct = (Structure) v; for (Variable seqVar : struct.getVariables()) { String varName = N3iosp.makeValidNetcdfObjectName(seqVar.getShortName() + "-" + structName); DataType newType = seqVar.getDataType(); List<Dimension> newDims = new ArrayList<Dimension>(); newDims.add(recordDim); newDims.add(structDim); for (Dimension dim : seqVar.getDimensions()) { newDims.add(ncfile.addDimension(seqVar.getShortName() + "_strlen", dim.getLength())); } Variable newVar = ncfile.addVariable(varName, newType, newDims); if (debug) System.out.println("add var= " + newVar); // attributes List<Attribute> attList = seqVar.getAttributes(); for (Attribute att : attList) { String useName = N3iosp.makeValidNetcdfObjectName(att.getName()); if (att.isArray()) ncfile.addVariableAttribute(varName, useName, att.getValues()); else if (att.isString()) ncfile.addVariableAttribute(varName, useName, att.getStringValue()); else ncfile.addVariableAttribute(varName, useName, att.getNumericValue()); } } } // create the file ncfile.create(); if (debug) System.out.println("File Out= " + ncfile.toString()); // boolean ok = (Boolean) ncfile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE); double total = copyVarData(ncfile, recordStruct); ncfile.flush(); System.out.println("FileWriter done total bytes = " + total); ncfile.close(); }