Java Code Examples for ucar.nc2.Variable#getShortName()

The following examples show how to use ucar.nc2.Variable#getShortName() . 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
/**
 * Reads and returns the data from the given variable.
 *
 * @param variable
 * @return Object.
 */
public static Object readData(Variable variable) {
	double[] values;
	try {
		values = (double[]) variable.read().get1DJavaArray(double.class);
	} catch (IOException e) {
		throw new RuntimeException("Error while reading data from netcdf variable '" + variable.getShortName()
				+ "'. Message was: " + e.getMessage(), e);
	}

	//apply scale factor and offset and replace missing values with Double.NaN.
	double missingValue = getMissingValueDouble(variable);
	double scaleFactor = getScaleFactorDouble(variable);
	double offSet = getOffSetDouble(variable);
	NetcdfUtils.convertDoubleValuesFromNetcdf(values, missingValue, scaleFactor, offSet);

	return new Array(values, variable.getShape(), false);
}
 
Example 2
private Netcdf2Variable(NetcdfDataset nds, Variable v0) {
  super(v0.getShortName(), v0);

  sweeps = new ArrayList<>();
  nsweeps = 0;
  int[] shape = v0.getShape();
  int count = v0.getRank() - 1;

  int ngates = shape[count];
  count--;
  int nrays = shape[count];
  count--;

  if (shape.length == 3)
    nsweeps = shape[count];
  else
    nsweeps = 1;

  for (int i = 0; i < nsweeps; i++)
    sweeps.add(new Netcdf2Sweep(v0, i, nrays, ngates));

}
 
Example 3
private void findNestedStructures(Structure s, TableConfig parent) {
  for (Variable v : s.getVariables()) {
    if (v instanceof Structure) { // handles Sequences too
      TableConfig nestedTable = new TableConfig(Table.Type.NestedStructure, v.getFullName());
      nestedTable.structName = v.getFullName();
      nestedTable.nestedTableName = v.getShortName();

      addTable(nestedTable);
      parent.addChild(nestedTable);

      // LOOK why not add the join(parent,child) here ?
      // nestedTable.join = new TableConfig.JoinConfig(Join.Type.NestedStructure);
      // joins.add(nestedTable.join);

      findNestedStructures((Structure) v, nestedTable); // search for nested structures
    }
  }
}
 
Example 4
public void finish() {

		// Here the intelligence to go back to the right waterlevel (from the fictive one) is needed, before writing the states
		for (Variable variable : this.netcdfFile.getVariables()){
			String varName = variable.getShortName();
			if (Arrays.asList(fictiveVariables).contains(varName)) {
				ITimeInfo timeInfo = NetcdfUtils.createTimeInfo(variable, this.netcdfFile, this.timeInfoCache);
				int LastTimeIndex = timeInfo.getTimes().length;
				double[] values = getExchangeItemValues(varName);
				// This is the critical method applying the intelligence:
				values = back2RealDomain(values,LastTimeIndex);
				// Might not be the most efficient as we write a second time into the files:
				writeExchangeItemValues(varName, values);
			}
		}
		try {
			netcdfFile.close();
			if (binRestartFile != null) {
				binRestartFile.close();
			}
		} catch (IOException e){
			e.printStackTrace();
		}

	}
 
Example 5
private Nids2Variable(NetcdfDataset nds, Variable v0) {
  super(v0.getShortName(), v0);
  sweeps = new ArrayList<>();

  int[] shape = v0.getShape();
  int count = v0.getRank() - 1;

  int ngates = shape[count];
  count--;
  int nrays = shape[count];
  count--;

  sweeps.add(new Nids2Sweep(nds, v0, 0, nrays, ngates));
}
 
Example 6
private void createNewVariables(NetcdfDataset ds, Variable ncVar, List<Dimension> newDims, Dimension levelDim)
    throws InvalidRangeException {

  List<Dimension> dims = ncVar.getDimensions();
  int newDimIndex = dims.indexOf(levelDim);
  // String shapeS = ncVar.getShapeS();

  int[] origin = new int[ncVar.getRank()];
  int[] shape = ncVar.getShape();
  int count = 0;
  for (Dimension dim : newDims) {
    String name = ncVar.getShortName() + "-" + dim.getShortName();

    origin[newDimIndex] = count;
    shape[newDimIndex] = dim.getLength();

    Variable varNew = ncVar.section(new Section(origin, shape));
    varNew.setName(name);
    varNew.setDimension(newDimIndex, dim);

    // synthesize long name
    String long_name = ds.findAttValueIgnoreCase(ncVar, CDM.LONG_NAME, ncVar.getShortName());
    long_name = long_name + "-" + dim.getShortName();
    ds.addVariableAttribute(varNew, new Attribute(CDM.LONG_NAME, long_name));

    ds.addVariable(null, varNew);

    parseInfo.format("Created New Variable as section = ");
    varNew.getNameAndDimensions(parseInfo, true, false);
    parseInfo.format("%n");

    count += dim.getLength();
  }
}
 
Example 7
protected AxisType getAxisType(NetcdfDataset ds, VariableEnhanced ve) {
  Variable v = (Variable) ve;
  String vname = v.getShortName();

  if (vname.equalsIgnoreCase("x"))
    return AxisType.GeoX;

  if (vname.equalsIgnoreCase("lon"))
    return AxisType.Lon;

  if (vname.equalsIgnoreCase("y"))
    return AxisType.GeoY;

  if (vname.equalsIgnoreCase("lat"))
    return AxisType.Lat;

  if (vname.equalsIgnoreCase("record"))
    return AxisType.Time;
  Dimension dim = v.getDimension(0);
  if ((dim != null) && dim.getShortName().equalsIgnoreCase("record"))
    return AxisType.Time;

  String unit = ve.getUnitsString();
  if (unit != null) {
    if (SimpleUnit.isCompatible("millibar", unit))
      return AxisType.Pressure;

    if (SimpleUnit.isCompatible("m", unit))
      return AxisType.Height;
  }


  return AxisType.GeoZ;
}
 
Example 8
/**
 * Creates and returns an ITimeInfo object for the given variable in the given netcdfFile.
 * If no time info available, then returns null.
 *
 * @param variable
 * @param netcdfFile
 * @param timeInfoCache
 * @return timeInfo or null.
 */
public static IArrayTimeInfo createTimeInfo(Variable variable, NetcdfFile netcdfFile,
		Map<Variable, IArrayTimeInfo> timeInfoCache) {

	Variable timeVariable = findTimeVariableForVariable(variable, netcdfFile);
	if (timeVariable == null) {//if data does not depend on time.
		return null;
	}

	//if data depends on time.
	IArrayTimeInfo timeInfo = timeInfoCache.get(timeVariable);
	if (timeInfo == null) {
		//create timeInfo.
		double[] times;
		try {
			times = readTimes(timeVariable);
		} catch (IOException e) {
			throw new RuntimeException("Error while reading data from netcdf time variable '" + timeVariable.getShortName()
					+ "'. Message was: " + e.getMessage(), e);
		}
		timeInfo = new TimeInfo(times);
		//cache timeInfo.
		timeInfoCache.put(timeVariable, timeInfo);
	}

	return timeInfo;
}
 
Example 9
/**
 * Find the variable pointed to by key
 *
 * @param ds in this dataset
 * @param key may be variable name or ":gatt" where gatt is local attribute whose value is the variable name
 * @param errlog error messages here
 * @return name of variable or null if not exist
 */
public static String getVariableName(NetcdfDataset ds, String key, Formatter errlog) {
  Variable v = null;
  String vs = getLiteral(ds, key, errlog);
  if (vs != null) {
    v = ds.findVariable(vs);
    if ((v == null) && (errlog != null))
      errlog.format(" Cant find Variable %s from %s%n", vs, key);
  }
  return v == null ? null : v.getShortName();
}
 
Example 10
protected String matchAxisTypeAndDimension(NetcdfDataset ds, AxisType type, Dimension outer, Dimension inner) {
  Variable var = CoordSysEvaluator.findCoordByType(ds, type,
      axis -> ((axis.getRank() == 2) && outer.equals(axis.getDimension(0)) && inner.equals(axis.getDimension(1))));
  if (var == null)
    return null;
  return var.getShortName();
}
 
Example 11
protected String matchAxisTypeAndDimension(NetcdfDataset ds, AxisType type, Dimension outer, Dimension middle,
    Dimension inner) {
  Variable var =
      CoordSysEvaluator.findCoordByType(ds, type, axis -> ((axis.getRank() == 3) && outer.equals(axis.getDimension(0))
          && middle.equals(axis.getDimension(1)) && inner.equals(axis.getDimension(2))));
  if (var == null)
    return null;
  return var.getShortName();
}
 
Example 12
private boolean isCoordinate(Variable v) {
  if (v == null)
    return false;
  String name = v.getShortName();
  return (latVE != null && latVE.axisName.equals(name)) || (lonVE != null && lonVE.axisName.equals(name))
      || (altVE != null && altVE.axisName.equals(name)) || (stnAltVE != null && stnAltVE.axisName.equals(name))
      || (timeVE != null && timeVE.axisName.equals(name)) || (nomTimeVE != null && nomTimeVE.axisName.equals(name));
}
 
Example 13
private Field(TypeAndOrder tao, Variable v) {
  this.tao = tao;
  this.memberName = v.getShortName();
  Attribute att = v.attributes().findAttribute("scale_factor");
  if (att != null && !att.isString()) {
    scale = att.getNumericValue().doubleValue();
    hasScale = true;
  }
  att = v.attributes().findAttribute("add_offset");
  if (att != null && !att.isString()) {
    offset = att.getNumericValue().doubleValue();
    hasScale = true;
  }
}
 
Example 14
/**
 * Read data from netcdf file.
 *
 * @throws IOException
 */
//TODO remove, always read data lazily. AK
private void readNetcdfFile() throws IOException  {
	this.exchangeItems.clear();
	NetcdfFile netcdfFile = this.netcdfFileWriter.getNetcdfFile();

	Results.putMessage(this.getClass().getSimpleName() + ": reading data from file " + this.file.getAbsolutePath());

	//in most netcdfFiles the time and spatial coordinate variables are shared between multiple data variables.
	//Therefore cache timeInfo objects so that time coordinate variables
	//are never read more than once.
	Map<Variable, IArrayTimeInfo> timeInfoCache = new HashMap<Variable, IArrayTimeInfo>();
	for (Variable variable : netcdfFile.getVariables()) {
		//do not create exchangeItems for coordinate variables.
		//the coordinate values will be stored in the metadata objects contained in the exchangeItems.
		if (variable.isCoordinateVariable()) {
			continue;
		}

		if (!variable.getDataType().isNumeric()) {
			continue;
		}

		//create exchangeItem.
		IExchangeItem exchangeItem;
		//Note: never use standard name or long name as exchangeItemId, since these are not always unique within a netcdf file.
		String exchangeItemId = variable.getShortName();
		if (variable.getDimensions().isEmpty()) {//if scalar.
			exchangeItem = new DoubleExchangeItem(exchangeItemId, Role.InOut, variable.readScalarDouble());

		} else {//if array.
			ArrayExchangeItem arrayBasedExchangeItem = new ArrayExchangeItem(exchangeItemId, IPrevExchangeItem.Role.InOut);
			arrayBasedExchangeItem.setQuantityInfo(new QuantityInfo(exchangeItemId, variable.getUnitsString()));
			IArrayTimeInfo newTimeInfo = NetcdfUtils.createTimeInfo(variable, netcdfFile, timeInfoCache);
			//skip variables that do not depend on time.
			if (newTimeInfo == null && !allowTimeIndependentItems) continue;

			arrayBasedExchangeItem.setTimeInfo(newTimeInfo);
			//TODO cache variables with spatial coordinates. AK
			arrayBasedExchangeItem.setGeometryInfo(NetcdfUtils.createGeometryInfo(variable, netcdfFile));
			IArray array = (IArray) NetcdfUtils.readData(variable);
			arrayBasedExchangeItem.setArray(array);
			exchangeItem = arrayBasedExchangeItem;
		}

		this.exchangeItems.add(exchangeItem);
	}
}
 
Example 15
private void transferGroup(Group srcGroup, Group targetGroup, NetcdfDataset target) throws IOException {
  // group attributes
  DatasetConstructor.transferGroupAttributes(srcGroup, targetGroup);

  // dimensions
  for (Dimension d : srcGroup.getDimensions()) {
    if (null == targetGroup.findDimensionLocal(d.getShortName())) {
      Dimension newd =
          new Dimension(d.getShortName(), d.getLength(), d.isShared(), d.isUnlimited(), d.isVariableLength());
      targetGroup.addDimension(newd);
    }
  }

  // transfer variables - eliminate any references to component files
  for (Variable v : srcGroup.getVariables()) {
    Variable targetV = targetGroup.findVariableLocal(v.getShortName());

    if (null == targetV) { // add it
      if (v instanceof Structure) {
        targetV = new StructureDS(target, targetGroup, null, v.getShortName(), v.getDimensionsString(),
            v.getUnitsString(), v.getDescription());
        // LOOK - not adding the members here - what to do ??

      } else {
        targetV = new VariableDS(target, targetGroup, null, v.getShortName(), v.getDataType(),
            v.getDimensionsString(), v.getUnitsString(), v.getDescription());
      }

      DatasetConstructor.transferVariableAttributes(v, targetV);
      VariableDS vds = (VariableDS) v;
      targetV.setSPobject(vds); // temporary, for non-agg variables when proto is made
      if (vds.hasCachedDataRecurse()) {
        if (vds.getSize() > 1000 * 1000) {
          boolean wtf = vds.hasCachedDataRecurse();
        }
        targetV.setCachedData(vds.read()); //
      }
      targetGroup.addVariable(targetV);
    }
  }

  // nested groups - check if target already has it
  for (Group srcNested : srcGroup.getGroups()) {
    Group nested = targetGroup.findGroupLocal(srcNested.getShortName());
    if (null == nested) {
      nested = new Group(target, targetGroup, srcNested.getShortName());
      targetGroup.addGroup(nested);
      for (EnumTypedef et : srcNested.getEnumTypedefs()) {
        targetGroup.addEnumeration(et);
      }
    }
    transferGroup(srcNested, nested, target);
  }
}
 
Example 16
public static String findNameVariableWithStandardNameAndDimension(NetcdfDataset ds, String standard_name,
    Dimension outer, Formatter errlog) {
  Variable v = findVariableWithAttributeAndDimension(ds, CF.STANDARD_NAME, standard_name, outer, errlog);
  return (v == null) ? null : v.getShortName();
}
 
Example 17
public TableConfig getConfig(FeatureType wantFeatureType, NetcdfDataset ds, Formatter errlog) {
  Dimension stationDim = CoordSysEvaluator.findDimensionByType(ds, AxisType.Lat);
  if (stationDim == null) {
    errlog.format("Must have a latitude coordinate");
    return null;
  }

  Variable stationVar = ds.findVariable(stationDim.getShortName());
  if (stationVar == null) {
    errlog.format("Must have a station coordinate variable");
    return null;
  }

  Dimension obsDim = CoordSysEvaluator.findDimensionByType(ds, AxisType.Time);
  if (obsDim == null) {
    errlog.format("Must have a Time coordinate");
    return null;
  }

  // station table
  TableConfig stationTable = new TableConfig(Table.Type.Structure, "station");
  stationTable.structName = "station";
  stationTable.structureType = TableConfig.StructureType.PsuedoStructure;
  stationTable.featureType = FeatureType.STATION;
  stationTable.dimName = stationDim.getShortName();

  stationTable.stnId = stationVar.getShortName();

  stationTable.lat = CoordSysEvaluator.findCoordNameByType(ds, AxisType.Lat);
  stationTable.lon = CoordSysEvaluator.findCoordNameByType(ds, AxisType.Lon);
  stationTable.stnAlt = CoordSysEvaluator.findCoordNameByType(ds, AxisType.Height);

  // obs table
  TableConfig obsTable;
  obsTable = new TableConfig(Table.Type.MultidimInner, "obs");
  obsTable.time = CoordSysEvaluator.findCoordNameByType(ds, AxisType.Time);
  obsTable.outerName = stationDim.getShortName();
  obsTable.dimName = obsDim.getShortName();

  stationTable.addChild(obsTable);
  return stationTable;
}
 
Example 18
private Variable.Builder copyVariable(Group.Builder parent, Variable oldVar) throws IOException {
  Variable.Builder vb;
  DataType newType = oldVar.getDataType();
  String dimNames = Dimensions.makeDimensionsString(oldVar.getDimensions());

  if (newType == DataType.STRUCTURE) {
    Structure oldStruct = (Structure) oldVar;
    Structure.Builder sb = Structure.builder().setName(oldVar.getShortName());
    for (Variable nested : oldStruct.getVariables()) {
      sb.addMemberVariable(copyVariable(parent, nested));
    }
    vb = sb;
  } else {
    vb = Variable.builder().setName(oldVar.getShortName()).setDataType(newType);
    if (!extended && newType == DataType.STRING) {
      // find maximum length
      Array data = oldVar.read();
      IndexIterator ii = data.getIndexIterator();
      int max_len = 0;
      while (ii.hasNext()) {
        String s = (String) ii.getObjectNext();
        max_len = Math.max(max_len, s.length());
      }

      // add last dimension
      String strlenDimName = oldVar.getShortName() + "_strlen";
      parent.addDimension(Dimension.builder(strlenDimName, max_len).setIsShared(false).build());

      newType = DataType.CHAR;
      vb.setDataType(DataType.CHAR);
      dimNames += " " + strlenDimName;
    }
  }
  vb.setParentGroupBuilder(parent).setDimensionsByName(dimNames);

  if (newType.isEnum()) {
    EnumTypedef en = oldVar.getEnumTypedef();
    vb.setEnumTypeName(en.getShortName());
  }

  // attributes
  for (Attribute att : oldVar.attributes()) {
    vb.addAttribute(convertAttribute(att));
    if (debug) {
      System.out.println("add varAtt= " + att);
    }
  }

  return vb;
}
 
Example 19
private void readNetCdfVariables() {

		for (Variable variable : this.netcdfFile.getVariables()) {

			if (Arrays.asList(keyVariables).contains(variable.getShortName())) {

				Variable timeVariable = NetcdfUtils.findTimeVariableForVariable(variable, this.netcdfFile);
				if (timeVariable == null) {
					throw new RuntimeException("NetcdfD3dMapDataObject: no time axis for " + variable.getShortName() + ", file: " + netcdfFile.getLocation());
				}
				this.timeDimensionIndex = variable.findDimensionIndex(timeVariable.getShortName());
				timeDependentVars.put(variable.getShortName(), variable);

				// if this is the first time dependent variable: read the times
				if (timesInNetcdfFile == null) {
					ucar.ma2.Array timesArray;
					try {
						timesArray = timeVariable.read();
					} catch (IOException e) {
						throw new RuntimeException("NetcdfD3dMapDataObject could not read time variable " + timeVariable.getShortName() +
								"from netcdf file " + netcdfFile.getLocation());
					}
					timesInNetcdfFile = (double[]) timesArray.get1DJavaArray(double.class);
				}

				// get the number of spatial dimensions
				// one of the dimensions was for time
				List<Dimension> dimensions = variable.getDimensions();
				int nonTimeDimensions = dimensions.size() - 1;
				if (nonTimeDimensions != 4 && variable.getShortName().equalsIgnoreCase("R1")) {
					throw new RuntimeException("NetcdfD3dMapDataObject: #dims for R1 should be four, file: " + netcdfFile.getLocation());
				}

				int layerCount = 0;
				for (int i = 0; i < dimensions.size(); i++) {
					Dimension dimension = dimensions.get(i);
					if (variable.getShortName().equalsIgnoreCase("R1")){
						if (dimension.getShortName().equalsIgnoreCase("LSTSCI")) {
							lstsciDimensionIndex = i;
							if (dimension.getLength() != 1) {
								throw new RuntimeException("NetcdfD3dMapDataObject: #R1 != 1 is not supported (temp. or salinity), file: "
									+ netcdfFile.getLocation());
							}
						}
					}
					if (!variable.getShortName().equalsIgnoreCase("S1")) {
						if (dimension.getShortName().equalsIgnoreCase("KMAXOUT_RESTR")) {
							if (variable.getShortName().equalsIgnoreCase("R1")) {
								kmaxOutRestrDimensionIndex = i;
							} else if (variable.getShortName().equalsIgnoreCase("U1") || variable.getShortName().equalsIgnoreCase("V1")) {
								kmaxOutRestrDimensionIndexVelocity = i;
							}
							if (dimension.getLength() < 0) {
								throw new RuntimeException("NetcdfD3dMapDataObject: could not read number of layers, file: "
									+ netcdfFile.getLocation());
							}
							layerCount = dimension.getLength();
						}
					}

				}

				if (lstsciDimensionIndex == 0 || kmaxOutRestrDimensionIndex == 0 || kmaxOutRestrDimensionIndexVelocity == 0) {
					throw new RuntimeException("NetcdfD3dMapDataObject: dims not available, file: "
						+ netcdfFile.getLocation());
				}

				ITimeInfo timeInfo = NetcdfUtils.createTimeInfo(variable, this.netcdfFile, timeInfoCache);
				// for memory reasons, for the time being we only keep the last time step in the exchange time
				// so adjust the time info
				double[] times = timeInfo.getTimes();
				timeInfo = new TimeInfo(new double[]{times[times.length-1]});

				// Extracting geometry information only once
				if (this.xCoords == null | this.yCoords == null | this.zCoords == null) {
					Variable variableX = this.netcdfFile.findVariable("XZ");
					Variable variableY = this.netcdfFile.findVariable("YZ");
					Variable variableZ = this.netcdfFile.findVariable("ZK_LYR");

					int[] originXY = createOrigin(variableX);
					int[] sizeArrayXY = variableX.getShape();
					int[] originZ = createOrigin(variableZ);
					int[] sizeArrayZ = variableZ.getShape();

					this.xCoords = NetcdfUtils.readSelectedData(variableX, originXY, sizeArrayXY, -1);
					this.yCoords = NetcdfUtils.readSelectedData(variableY, originXY, sizeArrayXY, -1);
					this.zCoords = NetcdfUtils.readSelectedData(variableZ, originZ, sizeArrayZ, -1);
				}

				IGeometryInfo geometryInfo;
				if (variable.getShortName().equalsIgnoreCase("S1")) {
					geometryInfo = new NetcdfD3dMapExchangeItemGeometryInfo(this.xCoords, this.yCoords, null);
				} else {
					geometryInfo = new NetcdfD3dMapExchangeItemGeometryInfo(this.xCoords, this.yCoords, this.zCoords);
				}
				IExchangeItem exchangeItem = new NetcdfD3dMapExchangeItem(variable.getShortName(), this, timeInfo, geometryInfo);
				this.exchangeItems.put(exchangeItem.getId(), exchangeItem);

			}
		}
	}
 
Example 20
/**
 * Find first variable with given attribute name and value
 *
 * @param ds in this dataset
 * @param attName attribute name, case insensitive
 * @param attValue attribute value, case sensitive
 * @return name of first variable with given attribute name and value, or null
 */
public static String findNameOfVariableWithAttributeValue(NetcdfDataset ds, String attName, String attValue) {
  Variable v = findVariableWithAttributeValue(ds, attName, attValue);
  return (v == null) ? null : v.getShortName();
}