use of com.beanit.openiec61850.Array in project Protocol-Adapter-IEC61850 by OSGP.
the class NodeContainer method writeFloatArray.
public void writeFloatArray(final SubDataAttribute child, final Float[] values) throws NodeWriteException {
final Array array = (Array) this.parent.getChild(child.getDescription());
if (array.size() != values.length) {
throw new NodeWriteException(String.format("Invalid array size %d. Size on device is %d", values.length, array.size()));
}
for (int i = 0; i < values.length; i++) {
final BdaFloat32 bdaFloat = (BdaFloat32) array.getChild(i);
bdaFloat.setFloat(values[i]);
this.writeNode(bdaFloat);
}
// Unfortunately writing an array using "this.writeNode(array);"
// doesn't seem to work...
// Therefore the items are written in individual calls...
}
use of com.beanit.openiec61850.Array in project Protocol-Adapter-IEC61850 by OSGP.
the class NodeContainer method writeDateArray.
public void writeDateArray(final SubDataAttribute child, final Date[] values) throws NodeWriteException {
final Array array = (Array) this.parent.getChild(child.getDescription());
if (array.size() != values.length) {
throw new NodeWriteException(String.format("Invalid array size %d. Size on device is %d", values.length, array.size()));
}
for (int i = 0; i < values.length; i++) {
final BdaTimestamp bdaTimestamp = (BdaTimestamp) array.getChild(i);
bdaTimestamp.setDate(values[i]);
this.writeNode(bdaTimestamp);
}
// Unfortunately writing an array using "this.writeNode(array);"
// doesn't seem to work...
// Therefore the items are written in individual calls...
}
use of com.beanit.openiec61850.Array in project sis by apache.
the class VariableWrapper method read.
/**
* Reads a sub-sampled sub-area of the variable.
*
* @param areaLower index of the first value to read along each dimension.
* @param areaUpper index after the last value to read along each dimension.
* @param subsampling sub-sampling along each dimension. 1 means no sub-sampling.
* @return the data as an array of a Java primitive type.
*/
@Override
public Vector read(final int[] areaLower, final int[] areaUpper, final int[] subsampling) throws IOException, DataStoreException {
final int[] size = new int[areaUpper.length];
for (int i = 0; i < size.length; i++) {
size[i] = areaUpper[i] - areaLower[i];
}
final Array array;
try {
array = variable.read(new Section(areaLower, size, subsampling));
} catch (InvalidRangeException e) {
throw new DataStoreContentException(e);
}
return Vector.create(array.get1DJavaArray(array.getElementType()), variable.isUnsigned());
}
use of com.beanit.openiec61850.Array in project sis by apache.
the class VariableWrapper method readArray.
/**
* Reads the data from this variable and returns them as an array of a Java primitive type.
* Multi-dimensional variables are flattened as a one-dimensional array (wrapped in a vector).
* This method may replace fill/missing values by NaN values and caches the returned vector.
*
* @param area indices of cell values to read along each dimension, in "natural" order.
* @param subsampling subsampling along each dimension, or {@code null} if none.
* @return the data as an array of a Java primitive type.
*
* @see #read()
* @see #read(GridExtent, int[])
*/
private Object readArray(final GridExtent area, final int[] subsampling) throws IOException, DataStoreException {
int n = area.getDimension();
final int[] lower = new int[n];
final int[] size = new int[n];
final int[] sub = (subsampling != null) ? new int[n] : null;
n--;
for (int i = 0; i <= n; i++) {
final int j = (n - i);
lower[j] = Math.toIntExact(area.getLow(i));
size[j] = Math.toIntExact(area.getSize(i));
if (sub != null) {
sub[j] = subsampling[i];
}
}
final Array array;
try {
array = variable.read(sub != null ? new Section(lower, size, sub) : new Section(lower, size));
} catch (InvalidRangeException e) {
throw new DataStoreException(e);
}
return get1DJavaArray(array);
}
use of com.beanit.openiec61850.Array in project imageio-ext by geosolutions-it.
the class INGVConverter method run.
private void run(String fileNameIn, String fileNameOut) {
try {
final File fileIn = new File(fileNameIn);
final NetcdfFile ncFileIn = NetcdfFile.open(fileNameIn);
final File fileOut = new File(fileNameOut);
// keep original name
final File outputFile = File.createTempFile(fileIn.getName(), ".tmp");
final NetcdfFileWriteable ncFileOut = NetcdfFileWriteable.createNew(outputFile.getAbsolutePath());
boolean hasDepth = false;
// input dimensions
String timeName = "time_counter";
Dimension timeOriginalDim = ncFileIn.findDimension(timeName);
if (timeOriginalDim == null) {
timeOriginalDim = ncFileIn.findDimension("time");
timeName = "time";
}
final Dimension yDim = ncFileIn.findDimension("y");
final Dimension xDim = ncFileIn.findDimension("x");
// input VARIABLES
final Variable timeOriginalVar = ncFileIn.findVariable(timeName);
final Array timeOriginalData = timeOriginalVar.read();
final DataType timeDataType = timeOriginalVar.getDataType();
final Variable navLat = ncFileIn.findVariable(NAV_LAT);
final DataType navLatDataType = navLat.getDataType();
final Variable navLon = ncFileIn.findVariable(NAV_LON);
final DataType navLonDataType = navLon.getDataType();
final int nLat = yDim.getLength();
final int nLon = xDim.getLength();
final int nTimes = timeOriginalDim.getLength();
final Array latOriginalData = navLat.read("0:" + (nLat - 1) + ":1, 0:0:1").reduce();
final Array lonOriginalData = navLon.read("0:0:1, 0:" + (nLon - 1) + ":1").reduce();
// //
//
// Depth related vars
//
// //
Array depthOriginalData = null;
DataType depthDataType = null;
int nDepths = 0;
Array depthDestData = null;
Dimension depthDim = null;
String depthName = "depth";
Variable depthOriginalVar = null;
int dName = 0;
while (depthOriginalVar == null) {
if (dName == depthNames.length)
break;
String name = depthNames[dName++];
// Depth
depthOriginalVar = ncFileIn.findVariable(name);
}
if (depthOriginalVar != null) {
depthName = depthNames[dName - 1];
nDepths = depthOriginalVar.getDimension(0).getLength();
depthOriginalData = depthOriginalVar.read();
hasDepth = true;
}
Dimension timeDim = ncFileOut.addDimension("time", nTimes);
Dimension latDim = ncFileOut.addDimension(NetCDFUtilities.LAT, nLat);
Dimension lonDim = ncFileOut.addDimension(NetCDFUtilities.LON, nLon);
if (hasDepth)
depthDim = ncFileOut.addDimension(NetCDFUtilities.DEPTH, nDepths);
// writing file
NetCDFConverterUtilities.copyGlobalAttributes(ncFileOut, ncFileIn.getGlobalAttributes());
// //
//
// Time requires a special Management
//
// //
// time Variable
Variable timeVar = ncFileOut.addVariable(NetCDFUtilities.TIME, timeDataType, new Dimension[] { timeDim });
NetCDFConverterUtilities.setVariableAttributes(timeOriginalVar, ncFileOut, NetCDFUtilities.TIME);
// Dimensions
ncFileOut.addVariable(NetCDFUtilities.LAT, navLatDataType, new Dimension[] { latDim });
NetCDFConverterUtilities.setVariableAttributes(navLat, ncFileOut, NetCDFUtilities.LAT);
ncFileOut.addVariable(NetCDFUtilities.LON, navLonDataType, new Dimension[] { lonDim });
NetCDFConverterUtilities.setVariableAttributes(navLon, ncFileOut, NetCDFUtilities.LON);
Array lat1Data = NetCDFConverterUtilities.getArray(nLat, navLatDataType);
NetCDFConverterUtilities.setData1D(latOriginalData, lat1Data, navLatDataType, nLat, true);
// lon Variable
Array lon1Data = NetCDFConverterUtilities.getArray(nLon, navLonDataType);
NetCDFConverterUtilities.setData1D(lonOriginalData, lon1Data, navLonDataType, nLon, false);
if (hasDepth) {
depthDataType = depthOriginalVar.getDataType();
ncFileOut.addVariable(NetCDFUtilities.DEPTH, depthDataType, new Dimension[] { depthDim });
NetCDFConverterUtilities.setVariableAttributes(depthOriginalVar, ncFileOut, NetCDFUtilities.DEPTH);
}
if (hasDepth) {
// depth level Variable
depthDestData = NetCDFConverterUtilities.getArray(nDepths, depthDataType);
NetCDFConverterUtilities.setData1D(depthOriginalData, depthDestData, depthDataType, nDepths, false);
}
// {} Variables
final ArrayList<String> variables = new ArrayList<String>(5);
final HashMap<String, String> updatingValidRange = new HashMap<String, String>(5);
final HashMap<String, String> updatingFilLValue = new HashMap<String, String>(5);
int numVars = 0;
for (int i = 0; i < NUMVARS; i++) {
String varName = (String) VARIABLES.get(i);
Variable var = ncFileIn.findVariable(varName);
if (var != null) {
variables.add(varName);
boolean hasLocalDepth = NetCDFConverterUtilities.hasThisDimension(var, depthName);
if (hasDepth && hasLocalDepth)
ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, depthDim, latDim, lonDim });
else
ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, latDim, lonDim });
// //
//
// Check for updating valid range
//
// //
boolean hasMinMax = false;
Attribute validMax = var.findAttribute(NetCDFUtilities.DatasetAttribs.VALID_MAX);
Attribute validMin = var.findAttribute(NetCDFUtilities.DatasetAttribs.VALID_MIN);
Attribute fillValue = var.findAttribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE);
boolean hasMissingValue = false;
boolean hasFillValue = true;
if (fillValue == null) {
hasFillValue = false;
fillValue = var.findAttribute(NetCDFUtilities.DatasetAttribs.MISSING_VALUE);
if (fillValue != null)
hasMissingValue = true;
}
Attribute validRange = var.findAttribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE);
boolean hasValidRange = false;
boolean rewriteAttribute = false;
if (validMin != null && validMax != null && fillValue != null) {
rewriteAttribute = !NetCDFConverterUtilities.isFillValueOutsideValidRange(validMax, validMin, fillValue, var.getDataType());
hasMinMax = true;
} else if (validRange != null && fillValue != null) {
rewriteAttribute = !NetCDFConverterUtilities.isFillValueOutsideValidRange(validRange, fillValue, var.getDataType());
hasValidRange = true;
} else {
rewriteAttribute = true;
}
if (rewriteAttribute) {
updatingValidRange.put(varName, "");
DataType varDatatype = var.getDataType();
Array range = NetCDFConverterUtilities.getRangeArray(varDatatype);
ncFileOut.addVariableAttribute(varName, NetCDFUtilities.DatasetAttribs.VALID_RANGE, range);
if (hasMissingValue && !hasFillValue) {
updatingFilLValue.put(varName, "");
Number fillVal = NetCDFConverterUtilities.getNumber(varDatatype);
ncFileOut.addVariableAttribute(varName, NetCDFUtilities.DatasetAttribs.FILL_VALUE, fillVal);
}
}
String[] exceptions = null;
if (hasMinMax) {
if (hasMissingValue)
exceptions = new String[] { NetCDFUtilities.DatasetAttribs.VALID_MAX, NetCDFUtilities.DatasetAttribs.VALID_MIN, NetCDFUtilities.DatasetAttribs.MISSING_VALUE };
else
exceptions = new String[] { NetCDFUtilities.DatasetAttribs.VALID_MAX, NetCDFUtilities.DatasetAttribs.VALID_MIN };
} else if (hasValidRange) {
if (hasMissingValue)
exceptions = new String[] { NetCDFUtilities.DatasetAttribs.VALID_RANGE, NetCDFUtilities.DatasetAttribs.MISSING_VALUE };
else
exceptions = new String[] { NetCDFUtilities.DatasetAttribs.VALID_RANGE };
} else if (hasMissingValue)
exceptions = new String[] { NetCDFUtilities.DatasetAttribs.MISSING_VALUE };
NetCDFConverterUtilities.setVariableAttributes(var, ncFileOut, exceptions);
numVars++;
}
}
// writing bin data ...
ncFileOut.create();
Array timeData = NetCDFConverterUtilities.getArray(nTimes, timeDataType);
NetCDFConverterUtilities.setData1D(timeOriginalData, timeData, timeDataType, nTimes, false);
ncFileOut.write("time", timeData);
timeVar = ncFileOut.findVariable("time");
timeDim.addCoordinateVariable(timeVar);
ncFileOut.write(NetCDFUtilities.LAT, lat1Data);
ncFileOut.write(NetCDFUtilities.LON, lon1Data);
if (hasDepth) {
Variable depthVar = ncFileOut.findVariable("depth");
depthDim.addCoordinateVariable(depthVar);
ncFileOut.write(NetCDFUtilities.DEPTH, depthDestData);
}
for (int i = 0; i < numVars; i++) {
String varName = (String) variables.get(i);
Variable var = ncFileIn.findVariable(varName);
final boolean hasLocalDepth = NetCDFConverterUtilities.hasThisDimension(var, depthName);
Array originalVarArray = var.read();
DataType varDataType = var.getDataType();
Array destArray = null;
int[] dimensions = null;
final boolean setDepth = hasDepth && hasLocalDepth;
if (setDepth) {
dimensions = new int[] { timeDim.getLength(), depthDim.getLength(), latDim.getLength(), lonDim.getLength() };
} else {
dimensions = new int[] { timeDim.getLength(), latDim.getLength(), lonDim.getLength() };
}
destArray = NetCDFConverterUtilities.getArray(dimensions, varDataType);
boolean findNewRange = updatingValidRange.containsKey(varName);
boolean updateFillValue = updatingFilLValue.containsKey(varName);
final int[] loopLengths;
if (setDepth)
loopLengths = new int[] { nTimes, nDepths, nLat, nLon };
else
loopLengths = new int[] { nTimes, nLat, nLon };
NetCDFConverterUtilities.writeData(ncFileOut, varName, var, originalVarArray, destArray, findNewRange, updateFillValue, loopLengths, true);
}
ncFileOut.close();
outputFile.renameTo(fileOut);
} catch (Exception e) {
// something bad happened
if (NetCDFConverterUtilities.LOGGER.isLoggable(Level.INFO))
NetCDFConverterUtilities.LOGGER.log(Level.INFO, e.getLocalizedMessage(), e);
}
}
Aggregations