本文整理汇总了Java中ucar.nc2.Variable类的典型用法代码示例。如果您正苦于以下问题:Java Variable类的具体用法?Java Variable怎么用?Java Variable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Variable类属于ucar.nc2包,在下文中一共展示了Variable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getExchangeItemValues
import ucar.nc2.Variable; //导入依赖的package包/类
public double[] getExchangeItemValues(String varName, int stationIndex, int layerIndex) {
Variable variable = this.netcdfFile.findVariable(varName);
int[] origin = createOrigin(variable);
int[] sizeArray = variable.getShape();
//select only the given station and layer
origin[timeDimensionIndex] = 0;
origin[lstsciDimensionIndex] = 0; //Only a single constituent possible for now, if several, arguments need to be changed
origin[kmaxOutRestrDimensionIndex] = layerIndex;
origin[statDimensionIndex] = stationIndex;
//only one station and layer
sizeArray[kmaxOutRestrDimensionIndex] = 1;
sizeArray[statDimensionIndex] = 1;
return NetcdfUtils.readSelectedData(variable, origin, sizeArray, -1);
}
示例2: finish
import ucar.nc2.Variable; //导入依赖的package包/类
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();
}
}
示例3: readTimes
import ucar.nc2.Variable; //导入依赖的package包/类
/**
* Reads the times from the given timeVariable and converts them to MJD
* in the returned array.
*
* @param timeVariable
* @return times array.
* @throws IOException
*/
private static double[] readTimes(Variable timeVariable) throws IOException {
double[] convertedTimes = new double[0];
if ((timeVariable != null) && timeVariable.isCoordinateVariable()) {
//read times.
ucar.ma2.Array timesArray = timeVariable.read();
double[] times = (double[]) timesArray.get1DJavaArray(double.class);
//convert times.
convertedTimes = new double[times.length];
DateUnit dateUnit = getDateUnitFromDimension(timeVariable);
for (int n = 0; n < times.length; n++) {
Date date = dateUnit.makeDate(times[n]);
if (date == null) {
convertedTimes[n] = 0;
continue;
}
long time = date.getTime();
convertedTimes[n] = Time.milliesToMjd(time);
}
}
return convertedTimes;
}
示例4: readData
import ucar.nc2.Variable; //导入依赖的package包/类
/**
* 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);
}
示例5: writeDataForVariableForSingleTimeSingleLocationSingleRealization
import ucar.nc2.Variable; //导入依赖的package包/类
public static void writeDataForVariableForSingleTimeSingleLocationSingleRealization(NetcdfFileWriter netcdfFileWriter, Variable variable,
int timeIndex, int realizationDimensionIndex, int realizationIndex, int stationDimensionIndex, int stationIndex, double[] values) {
int[] origin = createOrigin(variable);
int[] sizeArray = variable.getShape();
//here assume that timeDimensionIndex is 0.
int timeDimensionIndex = 0;
//select only the given time, station and realization.
origin[timeDimensionIndex] = timeIndex;
sizeArray[timeDimensionIndex] = 1;
origin[realizationDimensionIndex] = realizationIndex;
sizeArray[realizationDimensionIndex] = 1;
origin[stationDimensionIndex] = stationIndex;
sizeArray[stationDimensionIndex] = 1;
writeSelectedData(netcdfFileWriter, variable, origin, sizeArray, values);
}
示例6: readAndStoreStationIdsMap
import ucar.nc2.Variable; //导入依赖的package包/类
/**
* Read the data for (optional) variable "station_id" from the netcdf file and store in the stationIdsMap.
*
* For each external location this method stores the external station name and the index
* that is used for the location in the netcdf file in stationIdsMap.
*
* Code copied and adapted from class nl.wldelft.fews.system.plugin.dataImport.NetcdfTimeSeriesTSParser
*
* @throws IOException
*/
public static Map<Integer, String> readAndStoreStationIdsMap(NetcdfFile netcdfDataset, String stationNameVarName) throws IOException {
// if no stations found, return empty hash map
Map<Integer, String> stationIdsMap = new LinkedHashMap<Integer, String>();
Variable stationIdsVar = netcdfDataset.findVariable(stationNameVarName);
if (stationIdsVar != null) {
// stations found, create hash map
int numberOfDimensions = stationIdsVar.getDimensions().size();
switch (numberOfDimensions) {
case 0:
stationIdsMap = readAndStoreStationIdVariable(stationIdsVar);
break;
case 1:
stationIdsMap = readAndStoreOneDimensionalStationIdsVariable(netcdfDataset, stationNameVarName);
break;
case 2: default:
stationIdsMap = readAndStoreTwoDimensionalStationIdsVariable(stationIdsVar, netcdfDataset, stationNameVarName);
break;
}
}
return stationIdsMap;
}
示例7: createTimeVariable
import ucar.nc2.Variable; //导入依赖的package包/类
/**
* Creates a time dimension and variable.
*
* @param dataFile
* @param timeCount if timeCount is -1, then creates an unlimited timeDimension
* @param timeUnitString
* @return timeDimension
*/
public static Dimension createTimeVariable(NetcdfFileWriter dataFile, String timeVariableName, int timeCount, String timeUnitString) {
//create time dimension.
Dimension timeDimension;
if (timeCount == -1 || timeCount == 0) {
timeDimension = dataFile.addUnlimitedDimension(timeVariableName);
} else {
timeDimension = dataFile.addDimension(null,timeVariableName, timeCount);
}
//create time variable.
Variable myVar = dataFile.addVariable(null,timeVariableName, ucar.ma2.DataType.DOUBLE, Arrays.asList(timeDimension) );
dataFile.addVariableAttribute(myVar, new Attribute (STANDARD_NAME_ATTRIBUTE_NAME, TIME_VARIABLE_NAME) );
dataFile.addVariableAttribute(myVar, new Attribute(LONG_NAME_ATTRIBUTE_NAME, TIME_VARIABLE_NAME));
dataFile.addVariableAttribute(myVar, new Attribute(UNITS_ATTRIBUTE_NAME, timeUnitString));
//use default calendar.
dataFile.addVariableAttribute(myVar, new Attribute(CALENDAR_ATTRIBUTE_NAME, DEFAULT_CALENDAR_ATTRIBUTE_VALUE));
dataFile.addVariableAttribute(myVar, new Attribute(AXIS_ATTRIBUTE_NAME, T_AXIS));
return timeDimension;
}
示例8: writeTimeVariablesValues
import ucar.nc2.Variable; //导入依赖的package包/类
/**
* 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);
}
}
示例9: addConcatenatedValueArraysToMaps
import ucar.nc2.Variable; //导入依赖的package包/类
private static void addConcatenatedValueArraysToMaps(Map<Variable, Array> variableArraysMap, Map<Variable, Array> timeVariableArraysMap, Variable targetVariable, Variable timeVariableTarget, boolean concatenateTimeVariable, int targetLocationDimensionLength, double[][] targetValues, double[][] addedValues, double[] timesTarget, double[] convertedTimesToBeAdded, int totalTimesCombined, boolean firstAddedTimeOverlapping) {
if (firstAddedTimeOverlapping) totalTimesCombined--;
ArrayDouble.D1 timeArrayDouble = new ArrayDouble.D1(totalTimesCombined);
ArrayDouble.D2 valueArrayDouble = new ArrayDouble.D2(totalTimesCombined, targetLocationDimensionLength);
int targetTimes = firstAddedTimeOverlapping ? timesTarget.length - 1 : timesTarget.length;
for (int i = 0; i < targetTimes; i++) {
if (concatenateTimeVariable) timeArrayDouble.set(i, timesTarget[i]);
for (int j = 0; j < targetLocationDimensionLength; j++) {
valueArrayDouble.set(i, j, targetValues[i][j]);
}
}
for (int i = 0; i < convertedTimesToBeAdded.length; i++) {
if (concatenateTimeVariable) timeArrayDouble.set(i + targetTimes, convertedTimesToBeAdded[i]);
for (int j = 0; j < targetLocationDimensionLength; j++) {
valueArrayDouble.set(i + targetTimes, j, addedValues[i][j]);
}
}
if (concatenateTimeVariable) timeVariableArraysMap.put(timeVariableTarget, timeArrayDouble);
variableArraysMap.put(targetVariable, valueArrayDouble);
}
示例10: getGrid
import ucar.nc2.Variable; //导入依赖的package包/类
/**
* Reads data section specified by a "section selector", and return a memory resident Array. Uses Fortran
* 90 array section syntax.
*
* @param sectionSpec specification string, eg "1:2,10,:,1:100:10". May optionally have (). ":, 0:200,
* 0:100:5 " means : all the first dimension, the 200 first values of the second dimension, and
* the 100 first of the third diemnsion, by selecting only one value out of 5.
* @param fullName variable, with the specified (full) name. It may possibly be nested in multiple groups
* and/or structures. eg "group/subgroup/name1.name2.name".
*
* @return a ucar.ma2.Array with data for the variable.
*
* @throws NetCdfVariableNotFoundException the net cdf variable not found exception
* @throws NetCdfVariableException the net cdf variable exception
*/
public Array getGrid(String fullName, String sectionSpec) throws NetCdfVariableException, NetCdfVariableNotFoundException {
if (LOG.isDebugEnabled()) {
LOG.debug("getGrid() - entering");
}
Variable var = null;
Array grid = null;
var = getVariable(fullName);
try {
grid = var.read(sectionSpec);
} catch (Exception e) {
LOG.error("getGrid()", e);
throw new NetCdfVariableException(var, String.format("Error in getGrid - range %s", sectionSpec), e);
}
if (LOG.isDebugEnabled()) {
LOG.debug("getGrid() - exiting");
}
return grid;
}
示例11: getStringValue
import ucar.nc2.Variable; //导入依赖的package包/类
/**
* Reads the value of an attribute of a variable and returns its value as a string.
*
* @param attribute a NetCDF attribute.
* @param variable a NetCDF variable.
*
* @return value of the attribute
*
* @throws NetCdfAttributeException invalid request (see error message).
*/
public static String getStringValue(Variable variable, Attribute attribute) throws NetCdfAttributeException {
String value = null;
if (attribute == null) {
return value;
}
if (!attribute.isString()) {
throw new NetCdfAttributeException(
variable,
attribute,
String.format("Error in getStringValue - Unable to get string value from attribute - Attribute type (%s) is not STRING ",
attribute.getDataType().toString()));
}
value = attribute.getStringValue();
if (value == null) {
throw new NetCdfAttributeException(variable, attribute, "Error in getStringValue - Unable to get string value from attribute");
}
return value;
}
示例12: writeVariableInOneGulp
import ucar.nc2.Variable; //导入依赖的package包/类
/**
* Writes variable data in one gulp. It reads all the variable data in memory and writes them in the
* netcdf file.
*
* @param var variable to be written
* @throws MotuException the motu exception
*/
protected void writeVariableInOneGulp(Variable var) throws MotuException {
if (LOG.isDebugEnabled()) {
LOG.debug("writeVariableInOneGulp() - entering");
}
Array data = null;
try {
// data = var.read();
data = read(var);
} catch (IOException e) {
LOG.error("writeVariableInOneGulp()", e);
throw new MotuException(ErrorType.NETCDF_GENERATION, "Error in NetcdfWriter writeVariableInOneGulp", e);
}
writeVariableData(var, data);
if (LOG.isDebugEnabled()) {
LOG.debug("writeVariableInOneGulp() - exiting");
}
}
示例13: getCoordinateVariables
import ucar.nc2.Variable; //导入依赖的package包/类
/**
* Gets the coordinate variables.
*
* @param var the var
* @param ds the net cdf dataset
*
* @return the coordinate variables
*
* @throws MotuNotImplementedException the motu not implemented exception
* @throws MotuException the net cdf variable not found exception
*/
public static List<Variable> getCoordinateVariables(Variable var, NetcdfDataset ds) throws MotuNotImplementedException, MotuException {
List<Variable> listCoordVars = new ArrayList<Variable>();
if (var instanceof CoordinateAxis) {
return listCoordVars;
}
List<Dimension> listDims = var.getDimensions();
for (Dimension dim : listDims) {
Variable dimCoordVars = NetCdfReader.getCoordinateVariable(dim, ds);
listCoordVars.add(dimCoordVars);
}
return listCoordVars;
}
示例14: containsAll
import ucar.nc2.Variable; //导入依赖的package包/类
/**
* Do we have the same Variable in two lists.
*
* @param list2 List to compare with list1
* @param list1 List to compare with list2
*
* @return true if all in list1 are in list2 and all in list2 are in list1.
*/
public static boolean containsAll(List<CoordinateAxis> list1, List<Variable> list2) {
if (list1.size() != list2.size()) {
return false;
}
for (Variable v1 : list1) {
boolean gotIt = false;
for (Variable v2 : list2) {
if (v1.getName().equals(v2.getName())) {
gotIt = true;
}
}
if (!gotIt) {
return false;
}
}
return true;
}
示例15: getLatAxisData
import ucar.nc2.Variable; //导入依赖的package包/类
/**
* Gets latitude axis data values.
*
* @return a {@link Array} constains latitude axis data values
*
* @throws MotuException the motu exception
* @throws NetCdfVariableException the net cdf variable exception
*/
public Array getLatAxisData() throws MotuException, NetCdfVariableException {
if (LOG.isDebugEnabled()) {
LOG.debug("getLatAxisData() - entering");
}
if (productMetaData == null) {
throw new MotuException(ErrorType.SYSTEM, "Error in getLatAxisData - productMetaData is null");
}
Variable variable = productMetaData.getLatAxis();
if (variable == null) {
throw new MotuException(
ErrorType.INVALID_LATITUDE,
String.format("Error in getLatAxisData - No latitude axis found in this product '%s'", this.getProductId()));
}
Array returnArray = readVariable(variable);
if (LOG.isDebugEnabled()) {
LOG.debug("getLatAxisData() - exiting");
}
return returnArray;
}