Java Code Examples for ucar.nc2.Variable

The following examples show how to use ucar.nc2.Variable. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public void testNoCoords() throws IOException {
  String filename = "file:./" + TestNcMLRead.topDir + "exclude/aggExistingNoCoords.xml";
  logger.debug("{}", filename);
  NetcdfDataset ncd = null;

  try {
    ncd = NetcdfDatasets.openDataset(filename, true, null);
    Variable time = ncd.getRootGroup().findVariableLocal("time");
    Array data = time.read();
    // all missing
    // assert data.getInt(0) ==
  } finally {
    if (ncd != null)
      ncd.close();
  }
  // logger.debug("{}", ncd);
  // assert false;
}
 
Example 2
private void writeExtraVariables() throws IOException {
  if (extra == null)
    return;

  for (Variable v : extra) {
    NetcdfFile ncfile = writer.getOutputFile();
    Variable mv = ncfile.findVariable(v.getFullName());
    if (mv == null)
      continue; // may be removed
    try {
      writer.write(mv, v.read());
    } catch (InvalidRangeException e) {
      e.printStackTrace(); // cant happen haha
    }
  }
}
 
Example 3
protected Array createVar(DataCursor data) throws DapException {
  Array array = null;
  DapVariable d4var = (DapVariable) data.getTemplate();
  switch (d4var.getBaseType().getTypeSort()) {
    default: // atomic var
      array = createAtomicVar(data);
      break;
    case Sequence:
      array = createSequence(data);
      break;
    case Structure:
      array = createStructure(data);
      break;
  }
  if (d4var.isTopLevel() && this.dsp.getChecksumMode().enabled(dsp.getChecksumMode())) {
    // transfer the checksum attribute
    int csum = d4var.getChecksum();
    String scsum = String.format("0x%08x", csum);
    Variable cdmvar = (Variable) nodemap.get(d4var);
    Attribute acsum = new Attribute(DapUtil.CHECKSUMATTRNAME, scsum);
    cdmvar.addAttribute(acsum);
  }
  return array;
}
 
Example 4
Source Project: netcdf-java   Source File: H5diag.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void showCompress(Formatter f) throws IOException {
  NetcdfFile ncfile = iosp.getNetcdfFile();

  Size totalSize = new Size(0, 0);
  for (Variable v : ncfile.getVariables()) {
    H5header.Vinfo vinfo = (H5header.Vinfo) v.getSPobject();
    showCompress(v, vinfo, totalSize, f);
  }
  f.format("%n");
  f.format(" total bytes   = %d%n", totalSize.nominal);
  f.format(" total storage = %d%n", totalSize.storage);
  f.format(" compression = %f%n", totalSize.getRatio());
  f.format(" nchunks     = %d%n", totalSize.count);

  File raf = new File(ncfile.getLocation());
  f.format(" file size    = %d%n", raf.length());
  float overhead = totalSize.storage == 0 ? 0 : ((float) raf.length() / totalSize.storage);
  f.format(" overhead     = %f%n", overhead);
}
 
Example 5
public PointIteratorMultidim(String name, List<Variable> vars, int outerIndex, Filter filter) {
  this.vars = vars;
  this.outerIndex = outerIndex;
  this.filter = filter;

  Variable v = vars.get(0);
  npts = v.getDimension(1).getLength();

  StructureMembers.Builder builder = StructureMembers.builder().setName(name);
  for (Variable var : vars) {
    int[] shape = var.getShape();
    int[] newShape = new int[shape.length - 2];
    System.arraycopy(shape, 2, newShape, 0, shape.length - 2);
    builder.addMember(var.getShortName(), var.getDescription(), var.getUnitsString(), var.getDataType(), newShape);
  }
  members = builder.build();
}
 
Example 6
private void writeStructureDataClassic(int[] origin, StructureData sdata, Set<String> varSet)
    throws IOException, InvalidRangeException {
  for (StructureMembers.Member m : sdata.getMembers()) {
    Variable mv = findVariable(m.getName());
    if (!varSet.contains(m.getName()) || mv == null) {
      continue; // normal to fail here
    }

    Array org = sdata.getArray(m);
    if (m.getDataType() == DataType.STRING) { // convert to ArrayChar
      int strlen = mv.getDimension(mv.getDimensions().size() - 1).getLength();
      org = ArrayChar.makeFromStringArray((ArrayObject) org, strlen);
    }

    Array orgPlus1 = Array.makeArrayRankPlusOne(org); // add dimension on the left (slow)
    int[] useOrigin = origin;

    if (org.getRank() > 0) { // if rank 0 (common case, this is a nop, so skip)
      useOrigin = new int[org.getRank() + 1];
      useOrigin[0] = origin[0]; // the rest are 0
    }

    writer.write(mv, useOrigin, orgPlus1);
  }

}
 
Example 7
Source Project: netcdf-java   Source File: HdfEos.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void fixAttributes(Group.Builder g) {
  for (Variable.Builder v : g.vbuilders) {
    AttributeContainerMutable attHelper = v.getAttributeContainer();
    for (Attribute a : ImmutableList.copyOf(attHelper)) {
      if (a.getShortName().equalsIgnoreCase("UNIT") || a.getShortName().equalsIgnoreCase("UNITS")) {
        attHelper.replace(a, CDM.UNITS);
      }
      if (a.getShortName().equalsIgnoreCase("SCALE_FACTOR") || a.getShortName().equalsIgnoreCase("FACTOR")) {
        attHelper.replace(a, CDM.SCALE_FACTOR);
      }
      if (a.getShortName().equalsIgnoreCase("OFFSET")) {
        attHelper.replace(a, CDM.ADD_OFFSET);
      }
    }
  }

  for (Group.Builder ng : g.gbuilders) {
    fixAttributes(ng);
  }

}
 
Example 8
public void showCompress(Formatter f) throws IOException {
  Size totalSize = new Size(0, 0);
  for (Variable v : ncfile.getVariables()) {
    H5headerNew.Vinfo vinfo = (H5headerNew.Vinfo) v.getSPobject();
    showCompress(v, vinfo, totalSize, f);
  }
  f.format("%n");
  f.format(" total bytes   = %d%n", totalSize.nominal);
  f.format(" total storage = %d%n", totalSize.storage);
  f.format(" compression = %f%n", totalSize.getRatio());
  f.format(" nchunks     = %d%n", totalSize.count);

  File raf = new File(ncfile.getLocation());
  f.format(" file size    = %d%n", raf.length());
  float overhead = totalSize.storage == 0 ? 0 : ((float) raf.length() / totalSize.storage);
  f.format(" overhead     = %f%n", overhead);
}
 
Example 9
public void testSequence() {
  String url = "http://tsds.net/tsds/test/Scalar";
  try {
    NetcdfDataset ds = NetcdfDatasets.openDataset(url);
    System.out.println(ds);
    Structure struct = (Structure) ds.findVariable("TimeSeries");
    Variable var = struct.findVariable("time");
    Array arr = var.read();
    int n = (int) arr.getSize();
    int i;
    for (i = 0; arr.hasNext() && i < n; i++)
      System.out.println(arr.nextDouble());
    if (i != n) {
      System.err.println("short sequence");
      System.exit(1);
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
}
 
Example 10
private void dataPlot(BeanTable from) {
  List<VariableBean> l = from.getSelectedBeans();

  for (VariableBean vb : l) {
    if (vb == null)
      return;
    Variable v = vb.vs;
    if (v != null) {
      try {
        dataPlot.setDataset(ds);
        dataPlot.setVariable(v);
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    } else
      return;
  }
  plotWindow.show();
}
 
Example 11
public String getWcs1_0CrsId(GridDatatype gridDatatype, GridDataset gridDataset) throws IllegalArgumentException {
  gridDataset.getTitle();
  gridDatatype.getFullName();

  StringBuilder buf = new StringBuilder();

  Attribute gridMappingAtt = gridDatatype.findAttributeIgnoreCase(CF.GRID_MAPPING);
  if (gridMappingAtt != null) {
    String gridMapping = gridMappingAtt.getStringValue();
    Variable gridMapVar = gridDataset.getNetcdfFile().getRootGroup().findVariableLocal(gridMapping);
    if (gridMapVar != null) {
      String gridMappingNameAtt = gridMapVar.attributes().findAttributeString(CF.GRID_MAPPING_NAME, null);
      if (gridMappingNameAtt != null)
        buf.append("EPSG:").append(ProjectionStandardsInfo.getProjectionByCfName(gridMappingNameAtt));
    }
  }

  return buf.toString();
}
 
Example 12
Source Project: OpenDA   Source File: NetcdfUtils.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Write values for all time variables that are present.
 *
 * @param netcdfFileWriter
 * @param timeInfoTimeDimensionMap
 * @throws Exception
 */
public static void writeTimeVariablesValues(NetcdfFileWriter netcdfFileWriter,
		Map<ITimeInfo, Dimension> timeInfoTimeDimensionMap) throws Exception {

	for (Map.Entry<ITimeInfo, Dimension> entry : timeInfoTimeDimensionMap.entrySet()) {
		ITimeInfo timeInfo = entry.getKey();
		Dimension timeDimension = entry.getValue();

		Variable timeVariable = netcdfFileWriter.findVariable(timeDimension.getShortName());
		String timeUnitString = timeVariable.findAttribute(UNITS_ATTRIBUTE_NAME).getStringValue();
		DateUnit dateUnit = new DateUnit(timeUnitString);

		double[] times = timeInfo.getTimes();
		ArrayDouble.D1 timesArray = new ArrayDouble.D1(times.length);
		for (int n = 0; n < times.length; n++) {
			double newTime = dateUnit.makeValue(new Date(Time.mjdToMillies(times[n])));
			timesArray.set(n, newTime);
		}

		netcdfFileWriter.write(timeVariable, timesArray);
	}
}
 
Example 13
private void writeCoordinateData(CoverageCollection subsetDataset, NetcdfFormatWriter writer)
    throws IOException, InvalidRangeException {
  for (CoverageCoordAxis axis : subsetDataset.getCoordAxes()) {
    Variable v = writer.findVariable(axis.getName());
    if (v != null) {
      if (show)
        System.out.printf("CFGridCoverageWriter write axis %s%n", v.getNameAndDimensions());
      writer.write(v, axis.getCoordsAsArray());
    } else {
      logger.error("CFGridCoverageWriter No variable for {}", axis.getName());
    }

    if (axis.isInterval()) {
      Variable vb = writer.findVariable(axis.getName() + BOUNDS);
      writer.write(vb, axis.getCoordBoundsAsArray());
    }
  }
}
 
Example 14
protected String matchAxisTypeAndDimension(NetcdfDataset ds, AxisType type, Dimension outer) {
  Variable var = CoordSysEvaluator.findCoordByType(ds, type, axis -> {
    if ((outer == null) && (axis.getRank() == 0))
      return true;
    if ((outer != null) && (axis.getRank() == 1) && (outer.equals(axis.getDimension(0))))
      return true;

    // if axis is structure member, try pulling dimension out of parent structure
    if (axis.getParentStructure() != null) {
      Structure parent = axis.getParentStructure();
      return (outer != null) && (parent.getRank() == 1) && (outer.equals(parent.getDimension(0)));
    }
    return false;
  });
  if (var == null)
    return null;
  return var.getFullName();
}
 
Example 15
@Test
public void testStandaloneNoEnhance() throws IOException {
  try (NetcdfFile ncfile = NetcdfDatasets.openFile(dataDir + "testStandaloneNoEnhance.ncml", null)) {
    Variable unvar = ncfile.findVariable("unvar");
    assertThat((Object) unvar).isNotNull();
    assertThat(unvar.getDataType()).isEqualTo(DataType.SHORT);
    assertThat(unvar.attributes().hasAttribute("_Unsigned")).isTrue();
    assertThat(unvar.attributes().findAttributeString("_Unsigned", "")).isEqualTo("true");
    assertThat(unvar.readScalarShort()).isEqualTo(-9981);

    Variable scaledvar = ncfile.findVariable("scaledvar");
    assertThat((Object) scaledvar).isNotNull();
    assertThat(scaledvar.getDataType()).isEqualTo(DataType.SHORT);
    assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isTrue();
    assertThat(scaledvar.attributes().findAttributeDouble("scale_factor", 1.0)).isEqualTo(2.0);
    assertThat(scaledvar.readScalarShort()).isEqualTo(1);
  }
}
 
Example 16
/**
 * Returns a Line given a variable name and the geometric index. If the Line is not found it will return null. If the
 * Line is a part of the Multi-Line, it will return the head
 * (the first Line in the series which constitutes the Multi-Line).
 * 
 * @param name of the variable which holds the Line
 * @param index of the Line within the variable
 * @return a new line with all associated information
 */
public Line readLine(String name, int index) {

  Variable linevar = ds.findVariable(name);
  if (linevar == null)
    return null;
  Line line = null;

  // CFConvention
  if (ds.findGlobalAttribute(CF.CONVENTIONS) != null)
    if (ucar.nc2.dataset.conv.CF1Convention.getVersion(ds.findGlobalAttribute(CF.CONVENTIONS).getStringValue()) >= 8)
      line = new CFLine();

  if (line == null)
    return null;
  else
    return line.setupLine(ds, linevar, index);
}
 
Example 17
private void testStartAndEnd(Variable timeVar, String start, String end) throws IOException {
  Array vals = timeVar.read();
  int[] shape = vals.getShape();

  // check start date of aggregation
  CalendarDateUnit calendarDateUnit = getCalendarDateUnit(timeVar);
  CalendarDate calendarDate = calendarDateUnit.makeCalendarDate(vals.getInt(0));
  CalendarDate expected = CalendarDate.parseISOformat(calendarDateUnit.getCalendar().name(), start);
  assertThat(calendarDate).isEqualTo(expected);

  // check end date of aggregation
  calendarDate = calendarDateUnit.makeCalendarDate(vals.getInt(shape[0] - 1));
  expected = CalendarDate.parseISOformat(calendarDateUnit.getCalendar().name(), end);
  assertThat(calendarDate).isEqualTo(expected);
}
 
Example 18
Source Project: netcdf-java   Source File: H4iosp.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public String toStringDebug(Object o) {
  if (o instanceof Variable) {
    Variable v = (Variable) o;
    H4header.Vinfo vinfo = (H4header.Vinfo) v.getSPobject();
    return (vinfo != null) ? vinfo.toString() : "";
  }
  return null;
}
 
Example 19
public float[] readAllData() throws java.io.IOException {
  Array allData;
  Sweep spn = sweeps.get(0);
  Variable v = spn.getsweepVar();

  try {
    allData = v.read();
  } catch (IOException e) {
    throw new IOException(e.getMessage());
  }
  return (float[]) allData.get1DJavaArray(Float.TYPE);
}
 
Example 20
Map<Variable, Array> create() throws DapException {
  // iterate over the variables represented in the DSP
  List<DapVariable> topvars = this.dmr.getTopVariables();
  Map<Variable, Array> map = null;
  for (DapVariable var : topvars) {
    DataCursor cursor = this.dsp.getVariableData(var);
    Array array = createVar(cursor);
    Variable cdmvar = (Variable) nodemap.get(var);
    arraymap.put(cdmvar, array);
  }
  return this.arraymap;
}
 
Example 21
/**
 * @param ncfile the NetcdfFile to test
 * @return true if we think this is a IFPSConvention file.
 */
public static boolean isMine(NetcdfFile ncfile) {
  // check that file has a latitude and longitude variable, and that latitude has an attribute called
  // projectionType
  boolean geoVarsCheck;
  Variable v = ncfile.findVariable("latitude");
  if (null != ncfile.findVariable("longitude") && (null != v)) {
    geoVarsCheck = null != ncfile.findAttValueIgnoreCase(v, "projectionType", null);
  } else {
    // bail early
    return false;
  }

  // check that there is a global attribute called fileFormatVersion, and that it has one
  // of two known values
  boolean fileFormatCheck;
  Attribute ff = ncfile.findGlobalAttributeIgnoreCase("fileFormatVersion");
  if (ff != null) {
    String ffValue = ff.getStringValue();
    // two possible values (as of now)
    fileFormatCheck = (ffValue.equalsIgnoreCase("20030117") || ffValue.equalsIgnoreCase("20010816"));
  } else {
    // bail
    return false;
  }

  // both must be true
  return (geoVarsCheck && fileFormatCheck);
}
 
Example 22
private int[] computeChunkingGrib(Variable v) {
  int n = v.getRank();
  int[] result = new int[n];
  if (n < 2) {
    result[0] = 1; // Unlimited variable with rank 1

  } else {
    for (int i = 0; i < n; i++)
      result[i] = (i < n - 2) ? 1 : v.getDimension(i).getLength();
  }
  return result;
}
 
Example 23
Source Project: netcdf-java   Source File: H4iosp.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public String toStringDebug(Object o) {
  if (o instanceof Variable) {
    Variable v = (Variable) o;
    H4header.Vinfo vinfo = (H4header.Vinfo) v.getSPobject();
    return (vinfo != null) ? vinfo.toString() : "";
  }
  return null;
}
 
Example 24
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.USHORT : v.getDataType();
    CheckUInt16Value(v.read());
  }
 
Example 25
private void annotate(Variable.Builder v, BufrConfig.FieldConverter fld) {
  if (fld.type == null) {
    return;
  }

  switch (fld.type) {
    case lat:
      v.addAttribute(new Attribute(CDM.UNITS, CDM.LAT_UNITS));
      v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));
      coordinates.format("%s ", v.shortName);
      break;

    case lon:
      v.addAttribute(new Attribute(CDM.UNITS, CDM.LON_UNITS));
      v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));
      coordinates.format("%s ", v.shortName);
      break;

    case height:
    case heightOfStation:
    case heightAboveStation:
      v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Height.toString()));
      coordinates.format("%s ", v.shortName);
      break;

    case stationId:
      v.addAttribute(new Attribute(CF.STANDARD_NAME, CF.STATION_ID));
      break;

    case wmoId:
      v.addAttribute(new Attribute(CF.STANDARD_NAME, CF.STATION_WMOID));
      break;
  }
}
 
Example 26
Source Project: OpenDA   Source File: NetcdfUtils.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
public static double[] readDataForVariableForSingleLocationAndRealization(Variable variable, int stationDimensionIndex, int stationIndex, int realizationDimensionIndex, int realizationIndex) {
	int[] origin = createOrigin(variable);
	int[] sizeArray = variable.getShape();

	//select only the given station.
	origin[stationDimensionIndex] = stationIndex;
	sizeArray[stationDimensionIndex] = 1;
	//select only the given realization
	origin[realizationDimensionIndex] = realizationIndex;
	sizeArray[realizationDimensionIndex] = 1;
	return readSelectedData(variable, origin, sizeArray, -1);
}
 
Example 27
public void utestEnum() throws IOException {
  H5header.setDebugFlags(new ucar.nc2.util.DebugFlagsImpl("H5header/header"));
  String filename = TestN4reading.testDir + "nc4/tst_enum_data.nc";
  NetcdfFile ncfile = NetcdfFile.open(filename);
  Variable v = ncfile.findVariable("primary_cloud");
  Array data = v.read();
  System.out.println("\n**** testReadNetcdf4 done\n\n" + ncfile);
  logger.debug(Ncdump.printArray(data, "primary_cloud", null));
  ncfile.close();
  H5header.setDebugFlags(new ucar.nc2.util.DebugFlagsImpl());
}
 
Example 28
private void testStartAndEnd(Variable timeVar, String start, String end) throws IOException {
  Array vals = timeVar.read();
  int[] shape = vals.getShape();

  // check start date of aggregation
  CalendarDateUnit calendarDateUnit = getCalendarDateUnit(timeVar);
  CalendarDate calendarDate = calendarDateUnit.makeCalendarDate(vals.getInt(0));
  CalendarDate expected = CalendarDate.parseISOformat(calendarDateUnit.getCalendar().name(), start);
  assertThat(calendarDate).isEqualTo(expected);

  // check end date of aggregation
  calendarDate = calendarDateUnit.makeCalendarDate(vals.getInt(shape[0] - 1));
  expected = CalendarDate.parseISOformat(calendarDateUnit.getCalendar().name(), end);
  assertThat(calendarDate).isEqualTo(expected);
}
 
Example 29
private void cmdRemove(Variable v, String type, String name) {
  boolean err = false;

  if (type.equals("attribute")) {
    ucar.nc2.Attribute a = v.findAttribute(name);
    if (a != null) {
      v.remove(a);
      if (debugCmd)
        System.out.println("CMD remove " + type + " " + name);
    } else
      err = true;

  } else if (type.equals("variable") && v instanceof Structure) {
    Structure s = (Structure) v;
    Variable nested = s.findVariable(name);
    if (nested != null) {
      s.removeMemberVariable(nested);
      if (debugCmd)
        System.out.println("CMD remove " + type + " " + name);
    } else
      err = true;

  }

  if (err) {
    Formatter f = new Formatter();
    f.format("CMD remove %s CANT find %s location %s%n", type, name, location);
    log.info(f.toString());
  }
}
 
Example 30
private void makeMultidimInner(NetcdfDataset ds, TableConfig parentTable, TableConfig childTable) {
  Dimension parentDim = ds.findDimension(parentTable.dimName);
  Dimension childDim = ds.findDimension(childTable.innerName);

  // divide up the variables between the parent and the child
  List<String> obsVars;
  List<Variable> vars = ds.getVariables();
  List<String> parentVars = new ArrayList<>(vars.size());
  obsVars = new ArrayList<>(vars.size());
  for (Variable orgV : vars) {
    if (orgV instanceof Structure)
      continue;

    Dimension dim0 = orgV.getDimension(0);
    if ((dim0 != null) && dim0.equals(parentDim)) {
      if ((orgV.getRank() == 1) || ((orgV.getRank() == 2) && orgV.getDataType() == DataType.CHAR)) {
        parentVars.add(orgV.getShortName());
      } else {
        Dimension dim1 = orgV.getDimension(1);
        if ((dim1 != null) && dim1.equals(childDim))
          obsVars.add(orgV.getShortName());
      }
    }
  }
  parentTable.vars = parentVars;
  childTable.vars = obsVars;
}