use of ucar.ma2.DataType in project imageio-ext by geosolutions-it.
the class NetCDFConverterUtilities method writeData.
public static void writeData(NetcdfFileWriteable ncFileOut, final String varName, Variable var, final Array originalVarData, final Array destArray, final boolean findNewRange, final boolean updateFillValue, final int[] loopLengths, final boolean flipY) throws IOException, InvalidRangeException {
final int nestedLoops = loopLengths.length;
final boolean setDepth = nestedLoops > 3;
// timeDim
final int timePositions = loopLengths[0];
final int depthPositions;
final int latPositions;
final int lonPositions;
if (setDepth) {
depthPositions = loopLengths[1];
latPositions = loopLengths[2];
lonPositions = loopLengths[3];
} else {
depthPositions = -1;
latPositions = loopLengths[1];
lonPositions = loopLengths[2];
}
final DataType varDataType = var.getDataType();
Attribute fv = null;
if (updateFillValue)
fv = var.findAttribute(NetCDFUtilities.DatasetAttribs.MISSING_VALUE);
else
fv = var.findAttribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE);
Index varIndex = originalVarData.getIndex();
Index destIndex = destArray.getIndex();
// //
if (varDataType == DataType.FLOAT) {
float min = Float.MAX_VALUE;
float max = Float.MIN_VALUE;
float fillValue = Float.MAX_VALUE;
if (fv != null) {
fillValue = (fv.getNumericValue()).floatValue();
}
if (setDepth) {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int levelPos = 0; levelPos < depthPositions; levelPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
float sVal = originalVarData.getFloat(varIndex.set(tPos, levelPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setFloat(destIndex.set(tPos, levelPos, newYpos, xPos), sVal);
}
}
}
}
} else {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
float sVal = originalVarData.getFloat(varIndex.set(tPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
// Flipping y
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setFloat(destIndex.set(tPos, newYpos, xPos), sVal);
}
}
}
}
ncFileOut.write(varName, destArray);
if (findNewRange) {
Array range = NetCDFConverterUtilities.getRangeArray(varDataType);
Index index = range.getIndex();
range.setFloat(index.set(0), min);
range.setFloat(index.set(1), max);
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE, range));
}
if (updateFillValue) {
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE, new Float(fillValue)));
}
// //
//
// DOUBLE
//
// //
} else if (varDataType == DataType.DOUBLE) {
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
double fillValue = Double.MAX_VALUE;
if (fv != null) {
fillValue = (fv.getNumericValue()).doubleValue();
}
if (setDepth) {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int levelPos = 0; levelPos < depthPositions; levelPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
double sVal = originalVarData.getDouble(varIndex.set(tPos, levelPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setDouble(destIndex.set(tPos, levelPos, newYpos, xPos), sVal);
}
}
}
}
} else {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
double sVal = originalVarData.getDouble(varIndex.set(tPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
// Flipping y
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setDouble(destIndex.set(tPos, newYpos, xPos), sVal);
}
}
}
}
ncFileOut.write(varName, destArray);
if (findNewRange) {
Array range = NetCDFConverterUtilities.getRangeArray(varDataType);
Index index = range.getIndex();
range.setDouble(index.set(0), min);
range.setDouble(index.set(1), max);
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE, range));
}
if (updateFillValue) {
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE, new Double(fillValue)));
}
// //
//
// BYTE
//
// //
} else if (varDataType == DataType.BYTE) {
byte min = Byte.MAX_VALUE;
byte max = Byte.MIN_VALUE;
byte fillValue = Byte.MAX_VALUE;
if (fv != null) {
fillValue = (fv.getNumericValue()).byteValue();
}
if (setDepth) {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int levelPos = 0; levelPos < depthPositions; levelPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
byte sVal = originalVarData.getByte(varIndex.set(tPos, levelPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setByte(destIndex.set(tPos, levelPos, newYpos, xPos), sVal);
}
}
}
}
} else {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
byte sVal = originalVarData.getByte(varIndex.set(tPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
// Flipping y
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setByte(destIndex.set(tPos, newYpos, xPos), sVal);
}
}
}
}
ncFileOut.write(varName, destArray);
if (findNewRange) {
Array range = NetCDFConverterUtilities.getRangeArray(varDataType);
Index index = range.getIndex();
range.setByte(index.set(0), min);
range.setByte(index.set(1), max);
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE, range));
}
if (updateFillValue) {
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE, new Byte(fillValue)));
}
// //
//
// SHORT
//
// //
} else if (varDataType == DataType.SHORT) {
short min = Short.MAX_VALUE;
short max = Short.MIN_VALUE;
short fillValue = Short.MAX_VALUE;
if (fv != null) {
fillValue = (fv.getNumericValue()).shortValue();
}
if (setDepth) {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int levelPos = 0; levelPos < depthPositions; levelPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
short sVal = originalVarData.getShort(varIndex.set(tPos, levelPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setShort(destIndex.set(tPos, levelPos, newYpos, xPos), sVal);
}
}
}
}
} else {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
short sVal = originalVarData.getShort(varIndex.set(tPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
// Flipping y
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setShort(destIndex.set(tPos, newYpos, xPos), sVal);
}
}
}
}
ncFileOut.write(varName, destArray);
if (findNewRange) {
Array range = NetCDFConverterUtilities.getRangeArray(varDataType);
Index index = range.getIndex();
range.setShort(index.set(0), min);
range.setShort(index.set(1), max);
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE, range));
}
if (updateFillValue) {
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE, new Short(fillValue)));
}
} else // //
if (varDataType == DataType.INT) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int fillValue = Integer.MAX_VALUE;
if (fv != null) {
fillValue = (fv.getNumericValue()).intValue();
}
if (setDepth) {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int levelPos = 0; levelPos < depthPositions; levelPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
int sVal = originalVarData.getInt(varIndex.set(tPos, levelPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setInt(destIndex.set(tPos, levelPos, newYpos, xPos), sVal);
}
}
}
}
} else {
for (int tPos = 0; tPos < timePositions; tPos++) {
for (int yPos = 0; yPos < latPositions; yPos++) {
for (int xPos = 0; xPos < lonPositions; xPos++) {
int sVal = originalVarData.getInt(varIndex.set(tPos, yPos, xPos));
if (findNewRange) {
if (sVal >= max && sVal != fillValue)
max = sVal;
if (sVal <= min && sVal != fillValue)
min = sVal;
}
// Flipping y
int newYpos = yPos;
// Flipping y
if (flipY) {
newYpos = latPositions - yPos - 1;
}
destArray.setInt(destIndex.set(tPos, newYpos, xPos), sVal);
}
}
}
}
ncFileOut.write(varName, destArray);
if (findNewRange) {
Array range = NetCDFConverterUtilities.getRangeArray(varDataType);
Index index = range.getIndex();
range.setInt(index.set(0), min);
range.setInt(index.set(1), max);
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE, range));
}
if (updateFillValue) {
ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE, new Integer(fillValue)));
}
} else
throw new IllegalArgumentException("Unsupported DataType");
}
use of ucar.ma2.DataType in project imageio-ext by geosolutions-it.
the class GRIB1Utilities method getValuesAsString.
public static String getValuesAsString(Variable variable, int[] indexes) {
final int dataType = NetCDFUtilities.getRawDataType(variable);
final StringBuilder sb = new StringBuilder("");
try {
final int size = indexes.length;
final Array values = variable.read();
for (int i = 0; i < size; i++) {
switch(dataType) {
// TODO: ADD MORE.
case DataBuffer.TYPE_SHORT:
final short val1s = values.getShort(indexes[i]);
sb.append(Short.toString(val1s));
break;
case DataBuffer.TYPE_INT:
final int val1 = values.getInt(indexes[i]);
sb.append(Integer.toString(val1));
break;
case DataBuffer.TYPE_FLOAT:
final float val1f = values.getFloat(indexes[i]);
sb.append(Float.toString(val1f));
break;
case DataBuffer.TYPE_DOUBLE:
final double val1d = values.getDouble(indexes[i]);
sb.append(Double.toString(val1d));
break;
}
if (size > 1 && i != size - 1)
sb.append(VALUES_SEPARATOR);
}
} catch (IOException e) {
// TODO Auto-generated catch block
// TODO LOG ME
}
return sb.toString();
}
use of ucar.ma2.DataType in project AMASE by loonwerks.
the class SafetyScopeProvider method getNamedElements.
private Set<NamedElement> getNamedElements(EObject ctx) {
Set<NamedElement> components = new HashSet<>();
if (ctx instanceof AadlPackage) {
PublicPackageSection pubSec = ((AadlPackage) ctx).getPublicSection();
for (Element el : pubSec.getOwnedElements()) {
if (el instanceof DataImplementation || el instanceof DataType) {
components.add((NamedElement) el);
}
}
for (AnnexLibrary annex : AnnexUtil.getAllActualAnnexLibraries(((AadlPackage) ctx), AgreePackage.eINSTANCE.getAgreeContractLibrary())) {
AgreeContract contract = (AgreeContract) ((AgreeContractLibrary) annex).getContract();
components.addAll(getNamedElementsFromSpecs(contract.getSpecs()));
}
components.add((AadlPackage) ctx);
} else {
components.addAll(getNamedElementsFromClassifier((Classifier) ctx, false));
}
return components;
}
Aggregations