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);
}
}
use of com.beanit.openiec61850.Array in project imageio-ext by geosolutions-it.
the class MercatorOceanConverter 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());
// input dimensions
final Dimension latOriginalDim = ncFileIn.findDimension(NetCDFUtilities.LATITUDE);
final Dimension lonOriginalDim = ncFileIn.findDimension(NetCDFUtilities.LONGITUDE);
// input VARIABLES
// Variable time_0 = ncFileIn.findVariable("time");
// Array time_0_Data = time_0.read();
// Index time_0_Index = time_0_Data.getIndex();
final Variable lonOriginalVar = ncFileIn.findVariable(NetCDFUtilities.LONGITUDE);
final int nLon = lonOriginalDim.getLength();
final Variable latOriginalVar = ncFileIn.findVariable(NetCDFUtilities.LATITUDE);
final int nLat = latOriginalDim.getLength();
final Array latOriginalData = latOriginalVar.read();
final Index latOriginalIndex = latOriginalData.getIndex();
final Array lonOriginalData = lonOriginalVar.read();
final Index lonOriginalIndex = lonOriginalData.getIndex();
final Variable depthOriginalVar = ncFileIn.findVariable(// Depth
NetCDFUtilities.DEPTH);
final int nLevels = depthOriginalVar.getDimension(0).getLength();
final Array depthOriginalData = depthOriginalVar.read();
// Dimension timeDim = ncFileOut.addDimension("time",
// timeDim0.getLength());
Dimension latDim = ncFileOut.addDimension(NetCDFUtilities.LAT, nLat);
Dimension lonDim = ncFileOut.addDimension(NetCDFUtilities.LON, nLon);
Dimension depthDim = ncFileOut.addDimension(NetCDFUtilities.DEPTH, nLevels);
Dimension timeDim = ncFileOut.addDimension(NetCDFUtilities.TIME, 1);
// writing file
computeMatrixExtremes(latOriginalData, lonOriginalData, nLon, nLat, latOriginalIndex, lonOriginalIndex);
NetCDFConverterUtilities.copyGlobalAttributes(ncFileOut, ncFileIn.getGlobalAttributes());
// //
//
// Time requires a special Management
//
// //
// time Variable
Variable timeVar = ncFileOut.addVariable(NetCDFUtilities.TIME, DataType.FLOAT, new Dimension[] { timeDim });
Attribute referenceTime = ncFileIn.findGlobalAttribute(TIME_ORIGIN);
Attribute forecastDate = ncFileIn.findGlobalAttribute(FORECAST_DAY);
int numDay = 0;
if (referenceTime != null && forecastDate != null) {
numDay = setTime(ncFileOut, referenceTime, forecastDate);
}
// final float referenceTime = setTimeVariableAttributes(timeVar,
// ncFileOut);
// lat Variable
ArrayFloat latDestData = new ArrayFloat(new int[] { latOriginalDim.getLength() });
Index latDestIndex = latDestData.getIndex();
ncFileOut.addVariable(NetCDFUtilities.LAT, DataType.FLOAT, new Dimension[] { latDim });
// NOTE: A Strange BUG (I guess)
// when you copy attributes from old vars to new vars, it overwrite
// coordvars!!!
ncFileOut.addVariableAttribute(NetCDFUtilities.LAT, "long_name", NetCDFUtilities.LATITUDE);
ncFileOut.addVariableAttribute(NetCDFUtilities.LAT, UNITS, latOriginalVar.getUnitsString());
// new String[] { "long_name", UNITS, "step" , "axis"});
for (int yPos = 0; yPos < latOriginalDim.getLength(); yPos++) {
latDestData.setFloat(latDestIndex.set(yPos), new Float(this.ymax - (new Float(yPos).floatValue() * this.periodY)).floatValue());
}
// lon Variable
ArrayFloat lonDestData = new ArrayFloat(new int[] { lonOriginalDim.getLength() });
Index lonDestIndex = lonDestData.getIndex();
ncFileOut.addVariable(NetCDFUtilities.LON, DataType.FLOAT, new Dimension[] { lonDim });
ncFileOut.addVariableAttribute(NetCDFUtilities.LON, "long_name", NetCDFUtilities.LONGITUDE);
ncFileOut.addVariableAttribute(NetCDFUtilities.LON, UNITS, lonOriginalVar.getUnitsString());
// new String[] { "long_name", UNITS, "step", "axis"});
for (int xPos = 0; xPos < lonOriginalDim.getLength(); xPos++) {
lonDestData.setFloat(lonDestIndex.set(xPos), new Float(this.xmin + (new Float(xPos).floatValue() * this.periodX)).floatValue());
}
// depth level Variable
ArrayFloat depthDestData = new ArrayFloat(new int[] { depthDim.getLength() });
Index depthDestIndex = depthDestData.getIndex();
ncFileOut.addVariable(NetCDFUtilities.DEPTH, DataType.FLOAT, new Dimension[] { depthDim });
ncFileOut.addVariableAttribute(NetCDFUtilities.DEPTH, "long_name", NetCDFUtilities.DEPTH);
ncFileOut.addVariableAttribute(NetCDFUtilities.DEPTH, UNITS, depthOriginalVar.getUnitsString());
final Attribute positiveAttrib = depthOriginalVar.findAttribute("positive");
String positiveValue = "down";
if (positiveAttrib != null)
positiveValue = positiveAttrib.getStringValue();
ncFileOut.addVariableAttribute(NetCDFUtilities.DEPTH, "positive", positiveValue);
for (int wPos = 0; wPos < depthDim.getLength(); wPos++) {
depthDestData.setFloat(depthDestIndex.set(wPos), depthOriginalData.getFloat(depthDestIndex));
}
int numVars = 0;
final ArrayList<String> variables = new ArrayList<String>(5);
// {} Variables
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, "depth");
if (hasLocalDepth)
ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, depthDim, latDim, lonDim });
else
ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, latDim, lonDim });
NetCDFConverterUtilities.setVariableAttributes(var, ncFileOut, new String[] { "positions" });
numVars++;
}
}
// writing bin data ...
ncFileOut.create();
ArrayFloat timeData = new ArrayFloat(new int[] { timeDim.getLength() });
Index timeIndex = timeData.getIndex();
timeData.setFloat(timeIndex.set(0), numDay);
ncFileOut.write(NetCDFUtilities.TIME, timeData);
timeDim.addCoordinateVariable(timeVar);
Variable latitudeVar = ncFileOut.findVariable(NetCDFUtilities.LAT);
latDim.addCoordinateVariable(latitudeVar);
ncFileOut.write(NetCDFUtilities.LAT, latDestData);
Variable longitudeVar = ncFileOut.findVariable(NetCDFUtilities.LON);
lonDim.addCoordinateVariable(longitudeVar);
ncFileOut.write(NetCDFUtilities.LON, lonDestData);
Variable depthVar = ncFileOut.findVariable(NetCDFUtilities.DEPTH);
depthDim.addCoordinateVariable(depthVar);
ncFileOut.write(NetCDFUtilities.DEPTH, depthDestData);
for (int i = 0; i < numVars; i++) {
final String varName = (String) variables.get(i);
final Variable var = ncFileIn.findVariable(varName);
final boolean hasLocalDepth = NetCDFConverterUtilities.hasThisDimension(var, "depth");
final Array originalData = var.read();
Index varIndex = originalData.getIndex();
final DataType varDataType = var.getDataType();
final Attribute fv = var.findAttribute("_FillValue");
float fillValue = Float.NaN;
if (fv != null) {
fillValue = (fv.getNumericValue()).floatValue();
}
Array destArray = null;
int[] dimensions = null;
if (hasLocalDepth) {
dimensions = new int[] { timeDim.getLength(), depthDim.getLength(), nLat, nLon };
} else {
dimensions = new int[] { timeDim.getLength(), nLat, nLon };
}
destArray = NetCDFConverterUtilities.getArray(dimensions, varDataType);
Index destIndex = destArray.getIndex();
ArrayFloat tempData = new ArrayFloat(new int[] { latOriginalDim.getLength(), lonOriginalDim.getLength() });
Index tempIndex = tempData.getIndex();
if (hasLocalDepth) {
for (int levelPos = 0; levelPos < depthDim.getLength(); levelPos++) {
for (int yPos = 0; yPos < latOriginalDim.getLength(); yPos++) {
for (int xPos = 0; xPos < lonOriginalDim.getLength(); xPos++) {
tempData.setFloat(tempIndex.set(yPos, xPos), originalData.getFloat(varIndex.set(levelPos, yPos, xPos)));
}
}
final WritableRaster outData = Resampler(latOriginalData, lonOriginalData, lonOriginalDim.getLength(), latOriginalDim.getLength(), 2, tempData, fillValue);
for (int j = 0; j < latOriginalDim.getLength(); j++) {
for (int k = 0; k < lonOriginalDim.getLength(); k++) {
float sample = outData.getSampleFloat(k, j, 0);
destArray.setFloat(destIndex.set(0, levelPos, j, k), sample);
}
}
}
} else {
for (int yPos = 0; yPos < latOriginalDim.getLength(); yPos++) {
for (int xPos = 0; xPos < lonOriginalDim.getLength(); xPos++) {
tempData.setFloat(tempIndex.set(yPos, xPos), originalData.getFloat(varIndex.set(yPos, xPos)));
}
}
final WritableRaster outData = Resampler(latOriginalData, lonOriginalData, lonOriginalDim.getLength(), latOriginalDim.getLength(), 2, tempData, fillValue);
for (int j = 0; j < latOriginalDim.getLength(); j++) {
for (int k = 0; k < lonOriginalDim.getLength(); k++) {
float sample = outData.getSampleFloat(k, j, 0);
destArray.setFloat(destIndex.set(0, j, k), sample);
}
}
}
// }
ncFileOut.write(varName, destArray);
}
ncFileOut.close();
outputFile.renameTo(fileOut);
} catch (Exception e) {
// something bad happened
if (LOGGER.isLoggable(Level.INFO))
LOGGER.log(Level.INFO, e.getLocalizedMessage(), e);
JAI.getDefaultInstance().getTileCache().flush();
}
}
use of com.beanit.openiec61850.Array in project imageio-ext by geosolutions-it.
the class NetCDFConverterUtilities method getRangeArray.
public static Array getRangeArray(DataType varDataType) {
int[] dim = new int[] { 2 };
if (varDataType == DataType.FLOAT) {
Array array = new ArrayFloat(dim);
Index index = array.getIndex();
array.setFloat(index.set(0), Float.MIN_VALUE);
array.setFloat(index.set(1), Float.MAX_VALUE);
return array;
} else if (varDataType == DataType.DOUBLE) {
Array array = new ArrayDouble(dim);
Index index = array.getIndex();
array.setDouble(index.set(0), Double.MIN_VALUE);
array.setDouble(index.set(1), Double.MAX_VALUE);
return array;
} else if (varDataType == DataType.BYTE) {
Array array = new ArrayByte(dim);
Index index = array.getIndex();
array.setByte(index.set(0), Byte.MIN_VALUE);
array.setByte(index.set(1), Byte.MAX_VALUE);
return array;
} else if (varDataType == DataType.SHORT) {
Array array = new ArrayShort(dim);
Index index = array.getIndex();
array.setShort(index.set(0), Short.MIN_VALUE);
array.setShort(index.set(1), Short.MAX_VALUE);
return array;
} else if (varDataType == DataType.INT) {
Array array = new ArrayInt(dim);
Index index = array.getIndex();
array.setInt(index.set(0), Integer.MIN_VALUE);
array.setInt(index.set(1), Integer.MAX_VALUE);
return array;
}
throw new IllegalArgumentException("Actually unsupported Datatype");
}
use of com.beanit.openiec61850.Array 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 com.beanit.openiec61850.Array in project imageio-ext by geosolutions-it.
the class NetCDFImageReader method read.
/**
* @see javax.imageio.ImageReader#read(int, javax.imageio.ImageReadParam)
*/
@Override
public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
clearAbortRequest();
Variable variable = null;
Range indexRange = null;
NetCDFVariableWrapper wrapper = null;
Map<Range, ?> indexMap = reader.getIndexMap();
for (Range range : indexMap.keySet()) {
if (range.contains(imageIndex) && range.first() <= imageIndex && imageIndex < range.last()) {
wrapper = (NetCDFVariableWrapper) indexMap.get(range);
indexRange = range;
break;
}
}
variable = wrapper.getVariable();
/*
* Fetches the parameters that are not already processed by utility
* methods like 'getDestination' or 'computeRegions' (invoked below).
*/
final int strideX, strideY;
final int[] srcBands, dstBands;
if (param != null) {
strideX = param.getSourceXSubsampling();
strideY = param.getSourceYSubsampling();
srcBands = param.getSourceBands();
dstBands = param.getDestinationBands();
} else {
strideX = 1;
strideY = 1;
srcBands = null;
dstBands = null;
}
final int rank = wrapper.getRank();
final int bandDimension = rank - NetCDFUtilities.Z_DIMENSION;
/*
* Gets the destination image of appropriate size. We create it now
* since it is a convenient way to get the number of destination bands.
*/
final int width = wrapper.getWidth();
final int height = wrapper.getHeight();
/*
* Computes the source region (in the NetCDF file) and the destination
* region (in the buffered image). Copies those informations into UCAR
* Range structure.
*/
final Rectangle srcRegion = new Rectangle();
final Rectangle destRegion = new Rectangle();
computeRegions(param, width, height, null, srcRegion, destRegion);
// flipVertically(param, height, srcRegion);
int destWidth = destRegion.x + destRegion.width;
int destHeight = destRegion.y + destRegion.height;
final List<Range> ranges = new LinkedList<Range>();
for (int i = 0; i < rank; i++) {
final int first, length, stride;
switch(rank - i) {
case NetCDFUtilities.X_DIMENSION:
{
first = srcRegion.x;
length = srcRegion.width;
stride = strideX;
break;
}
case NetCDFUtilities.Y_DIMENSION:
{
first = srcRegion.y;
length = srcRegion.height;
stride = strideY;
break;
}
default:
{
if (i == bandDimension) {
first = NetCDFUtilities.getZIndex(variable, indexRange, imageIndex);
} else {
first = NetCDFUtilities.getTIndex(variable, indexRange, imageIndex);
}
length = 1;
stride = 1;
break;
}
}
try {
ranges.add(new Range(first, first + length - 1, stride));
} catch (InvalidRangeException e) {
throw netcdfFailure(e);
}
}
final Section sections = new Section(ranges);
/*
* Setting SampleModel and ColorModel.
*/
final SampleModel sampleModel = wrapper.getSampleModel().createCompatibleSampleModel(destWidth, destHeight);
final ColorModel colorModel = ImageIOUtilities.createColorModel(sampleModel);
final WritableRaster raster = Raster.createWritableRaster(sampleModel, new Point(0, 0));
final BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
/*
* Reads the requested sub-region only.
*/
processImageStarted(imageIndex);
final int numDstBands = 1;
final float toPercent = 100f / numDstBands;
final int type = raster.getSampleModel().getDataType();
final int xmin = destRegion.x;
final int ymin = destRegion.y;
final int xmax = destRegion.width + xmin;
final int ymax = destRegion.height + ymin;
for (int zi = 0; zi < numDstBands; zi++) {
// final int srcBand = (srcBands == null) ? zi : srcBands[zi];
final int dstBand = (dstBands == null) ? zi : dstBands[zi];
final Array array;
try {
array = variable.read(sections);
} catch (InvalidRangeException e) {
throw netcdfFailure(e);
}
final IndexIterator it = array.getIndexIterator();
// for (int y = ymax; --y >= ymin;) {
for (int y = ymin; y < ymax; y++) {
for (int x = xmin; x < xmax; x++) {
switch(type) {
case DataBuffer.TYPE_DOUBLE:
{
raster.setSample(x, y, dstBand, it.getDoubleNext());
break;
}
case DataBuffer.TYPE_FLOAT:
{
raster.setSample(x, y, dstBand, it.getFloatNext());
break;
}
case DataBuffer.TYPE_BYTE:
{
byte b = it.getByteNext();
// int myByte = (0x000000FF & ((int) b));
// short anUnsignedByte = (short) myByte;
// raster.setSample(x, y, dstBand, anUnsignedByte);
raster.setSample(x, y, dstBand, b);
break;
}
default:
{
raster.setSample(x, y, dstBand, it.getIntNext());
break;
}
}
}
}
/*
* Checks for abort requests after reading. It would be a waste of a
* potentially good image (maybe the abort request occurred after we
* just finished the reading) if we didn't implemented the
* 'isCancel()' method. But because of the later, which is checked
* by the NetCDF library, we can't assume that the image is
* complete.
*/
if (abortRequested()) {
processReadAborted();
return image;
}
/*
* Reports progress here, not in the deeper loop, because the costly
* part is the call to 'variable.read(...)' which can't report
* progress. The loop that copy pixel values is fast, so reporting
* progress there would be pointless.
*/
processImageProgress(zi * toPercent);
}
if (lastError != null) {
throw new IIOException(lastError);
}
processImageComplete();
return image;
}
Aggregations