// 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()); }
/** 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); } }
private int addAttributes(opendap.dap.AttributeTable table, Variable v, Iterator iter) { int count = 0; // add attribute table for this variable while (iter.hasNext()) { Attribute att = (Attribute) iter.next(); int dods_type = DODSNetcdfFile.convertToDODSType(att.getDataType(), false); try { String attName = NcDDS.escapeName(att.getName()); if (att.isString()) { /* FIX String value = escapeAttributeStringValues(att.getStringValue()); table.appendAttribute(attName, dods_type, "\""+value+"\""); */ table.appendAttribute(attName, dods_type, att.getStringValue()); } else { // cant send signed bytes if (att.getDataType() == DataType.BYTE) { boolean signed = false; for (int i = 0; i < att.getLength(); i++) { if (att.getNumericValue(i).byteValue() < 0) signed = true; } if (signed) // promote to signed short dods_type = opendap.dap.Attribute.INT16; } for (int i = 0; i < att.getLength(); i++) table.appendAttribute(attName, dods_type, att.getNumericValue(i).toString()); } count++; } catch (Exception e) { log.error( "Error appending attribute " + att.getName() + " = " + att.getStringValue() + "\n" + e); } } // loop over variable attributes // kludgy thing to map char arrays to DODS Strings if ((v != null) && (v.getDataType().getPrimitiveClassType() == char.class)) { int rank = v.getRank(); int strlen = (rank == 0) ? 0 : v.getShape(rank - 1); Dimension dim = (rank == 0) ? null : v.getDimension(rank - 1); try { opendap.dap.AttributeTable dodsTable = table.appendContainer("DODS"); dodsTable.appendAttribute("strlen", opendap.dap.Attribute.INT32, Integer.toString(strlen)); if ((dim != null) && dim.isShared()) dodsTable.appendAttribute("dimName", opendap.dap.Attribute.STRING, dim.getName()); count++; } catch (Exception e) { log.error("Error appending attribute strlen\n" + e); } } return count; }
private static String getStructMetadata(Group eosGroup) throws IOException { StringBuilder sbuff = null; String structMetadata = null; int n = 0; while (true) { Variable structMetadataVar = eosGroup.findVariable("StructMetadata." + n); if (structMetadataVar == null) break; if ((structMetadata != null) && (sbuff == null)) { // more than 1 StructMetadata sbuff = new StringBuilder(64000); sbuff.append(structMetadata); } // read and parse the ODL Array A = structMetadataVar.read(); if (A instanceof ArrayChar.D1) { ArrayChar ca = (ArrayChar) A; structMetadata = ca.getString(); // common case only StructMetadata.0, avoid extra copy } else if (A instanceof ArrayObject.D0) { ArrayObject ao = (ArrayObject) A; structMetadata = (String) ao.getObject(0); } else { log.error("Unsupported array type {} for StructMetadata", A.getElementType()); } if (sbuff != null) sbuff.append(structMetadata); n++; } return (sbuff != null) ? sbuff.toString() : structMetadata; }
private void doVariable(Variable v, opendap.dap.AttributeTable parentTable) { List dims = v.getDimensions(); for (int i = 0; i < dims.size(); i++) { Dimension dim = (Dimension) dims.get(i); if (dim.isShared()) usedDims.put(dim.getName(), dim); } // if (v.getAttributes().size() == 0) return; // LOOK DAP 2 say must have empty String name = NcDDS.escapeName(v.getShortName()); opendap.dap.AttributeTable table; if (parentTable == null) { table = new opendap.dap.AttributeTable(name); try { addAttributeTable(name, table); } catch (AttributeExistsException e) { log.error("Cant add " + name, e); } } else { table = parentTable.appendContainer(name); } addAttributes(table, v, v.getAttributes().iterator()); if (v instanceof Structure) { Structure s = (Structure) v; List nested = s.getVariables(); for (int i = 0; i < nested.size(); i++) { Variable nv = (Variable) nested.get(i); doVariable(nv, table); } } }
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); } }
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; }
/** * 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; }