use of ucar.ma2.ArrayShort in project imageio-ext by geosolutions-it.
the class GRIB1ImageReader method read.
/**
* @see javax.imageio.ImageReader#read(int, javax.imageio.ImageReadParam)
*/
@Override
public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
BufferedImage image = null;
Variable variable = null;
Range indexRange = null;
GribVariableWrapper wrapper = null;
for (Range range : indexMap.keySet()) {
if (range.contains(imageIndex) && range.first() <= imageIndex && imageIndex < range.last()) {
wrapper = 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) {
}
}
final Section sections = new Section(ranges);
/*
* Setting SampleModel and ColorModel.
*/
SampleModel sampleModel = wrapper.getSampleModel().createCompatibleSampleModel(destWidth, destHeight);
ColorModel colorModel = ImageIOUtilities.createColorModel(sampleModel);
/*
* Reads the requested sub-region only.
*/
final int numDstBands = 1;
final int size = destHeight * destWidth * numDstBands;
for (int zi = 0; zi < numDstBands; zi++) {
final int dstBand = (dstBands == null) ? zi : dstBands[zi];
final Array array;
try {
array = variable.read(sections);
DataBuffer dataBuffer = null;
if (array instanceof ArrayByte) {
dataBuffer = new DataBufferByte((byte[]) array.get1DJavaArray(byte.class), size);
} else if (array instanceof ArrayShort) {
dataBuffer = new DataBufferShort((short[]) array.get1DJavaArray(short.class), size);
} else if (array instanceof ArrayInt) {
dataBuffer = new DataBufferInt((int[]) array.get1DJavaArray(int.class), size);
} else if (array instanceof ArrayFloat) {
dataBuffer = new DataBufferFloat((float[]) array.get1DJavaArray(float.class), size);
} else if (array instanceof ArrayDouble) {
dataBuffer = new DataBufferDouble((double[]) array.get1DJavaArray(double.class), size);
}
WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0));
image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
} catch (InvalidRangeException e) {
}
}
return image;
}
use of ucar.ma2.ArrayShort in project imageio-ext by geosolutions-it.
the class NCOMConverter 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
final Dimension timeDim0 = ncFileIn.findDimension("time");
final int nTimes = timeDim0.getLength();
final Dimension latDim0 = ncFileIn.findDimension("lat");
final int nLat = latDim0.getLength();
final Dimension lonDim0 = ncFileIn.findDimension("lon");
final int nLon = lonDim0.getLength();
// input VARIABLES
final Variable timeOriginalVar = ncFileIn.findVariable("time");
final Array timeOriginalData = timeOriginalVar.read();
final Index timeOriginalIndex = timeOriginalData.getIndex();
final DataType timeDataType = timeOriginalVar.getDataType();
final Variable lonOriginalVar = ncFileIn.findVariable("lon");
final DataType lonDataType = lonOriginalVar.getDataType();
final Variable latOriginalVar = ncFileIn.findVariable("lat");
final DataType latDataType = latOriginalVar.getDataType();
final Array latOriginalData = latOriginalVar.read();
final Array lonOriginalData = lonOriginalVar.read();
// //
//
// Depth related vars
//
// //
Array depthOriginalData = null;
int nDepths = 0;
Array depth1Data = null;
Dimension depthDim = null;
DataType depthDataType = null;
// Depth
final Variable depthOriginalVar = ncFileIn.findVariable("depth");
if (depthOriginalVar != null) {
nDepths = depthOriginalVar.getDimension(0).getLength();
depthOriginalData = depthOriginalVar.read();
depthDataType = depthOriginalVar.getDataType();
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);
NetCDFConverterUtilities.copyGlobalAttributes(ncFileOut, ncFileIn.getGlobalAttributes());
// Dimensions
Variable timeVar = ncFileOut.addVariable("time", timeDataType, new Dimension[] { timeDim });
NetCDFConverterUtilities.setVariableAttributes(timeOriginalVar, ncFileOut, new String[] { "long_name" });
ncFileOut.addVariableAttribute("time", "long_name", "time");
ncFileOut.addVariable(NetCDFUtilities.LAT, latDataType, new Dimension[] { latDim });
NetCDFConverterUtilities.setVariableAttributes(latOriginalVar, ncFileOut);
ncFileOut.addVariable(NetCDFUtilities.LON, lonDataType, new Dimension[] { lonDim });
NetCDFConverterUtilities.setVariableAttributes(lonOriginalVar, ncFileOut);
if (hasDepth) {
ncFileOut.addVariable(NetCDFUtilities.DEPTH, depthDataType, new Dimension[] { depthDim });
NetCDFConverterUtilities.setVariableAttributes(depthOriginalVar, ncFileOut);
}
// lat Variable
Array lat1Data = NetCDFConverterUtilities.getArray(nLat, latDataType);
NetCDFConverterUtilities.setData1D(latOriginalData, lat1Data, latDataType, nLat, true);
// lon Variable
Array lon1Data = NetCDFConverterUtilities.getArray(nLon, lonDataType);
NetCDFConverterUtilities.setData1D(lonOriginalData, lon1Data, lonDataType, nLon, false);
if (hasDepth) {
// depth level Variable
depth1Data = NetCDFConverterUtilities.getArray(nDepths, depthDataType);
NetCDFConverterUtilities.setData1D(depthOriginalData, depth1Data, depthDataType, nDepths, false);
}
// {} Variables
final ArrayList<String> variables = new ArrayList<String>(5);
final HashMap<String, String> updatingValidRange = 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);
List<Dimension> dims = var.getDimensions();
boolean hasLocalDepth = false;
for (Dimension dim : dims) {
if (dim.getName().equalsIgnoreCase("depth")) {
hasLocalDepth = true;
break;
}
}
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 });
Attribute validRange = var.findAttribute(NetCDFUtilities.DatasetAttribs.VALID_MAX);
if (validRange != null)
validRange = var.findAttribute(NetCDFUtilities.DatasetAttribs.VALID_MIN);
if (validRange == null)
validRange = var.findAttribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE);
if (validRange == null) {
updatingValidRange.put(varName, "");
ArrayShort range = new ArrayShort(new int[] { 2 });
Index index = range.getIndex();
range.setShort(index.set(0), Short.MIN_VALUE);
range.setShort(index.set(1), Short.MAX_VALUE);
ncFileOut.addVariableAttribute(varName, NetCDFUtilities.DatasetAttribs.VALID_RANGE, range);
}
NetCDFConverterUtilities.setVariableAttributes(var, ncFileOut);
numVars++;
}
}
// writing bin data ...
ncFileOut.create();
ArrayFloat timeData = new ArrayFloat(new int[] { timeDim.getLength() });
Index timeIndex = timeData.getIndex();
for (int t = 0; t < timeDim.getLength(); t++) {
timeData.setFloat(timeIndex.set(t), timeOriginalData.getFloat(timeOriginalIndex.set(t)));
}
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, depth1Data);
}
for (int i = 0; i < numVars; i++) {
String varName = (String) variables.get(i);
Variable var = ncFileIn.findVariable(varName);
boolean hasLocalDepth = NetCDFConverterUtilities.hasThisDimension(var, "depth");
Array originalVarArray = var.read();
DataType varDataType = var.getDataType();
Array destArray = null;
int[] dimensions = null;
if (hasDepth && hasLocalDepth) {
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);
final boolean setDepth = hasDepth && hasLocalDepth;
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, false, 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);
JAI.getDefaultInstance().getTileCache().flush();
}
}
use of ucar.ma2.ArrayShort 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 ucar.ma2.ArrayShort in project imageio-ext by geosolutions-it.
the class BaseHDF4ImageReader method read2DVariable.
protected BufferedImage read2DVariable(final int imageIndex, final ImageReadParam param) throws IOException {
BufferedImage image = null;
final HDF4DatasetWrapper wrapper = getDatasetWrapper(imageIndex);
final Variable 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 = variable.getRank();
/*
* 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();
final int numBands = wrapper.getNumBands();
/*
* 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(i) {
case 1:
{
first = srcRegion.x;
length = srcRegion.width;
stride = strideX;
break;
}
case 0:
{
first = srcRegion.y;
length = srcRegion.height;
stride = strideY;
break;
}
default:
{
first = 0;
length = numBands;
stride = 1;
break;
}
}
try {
ranges.add(new Range(first, first + length - 1, stride));
} catch (InvalidRangeException e) {
// TODO LOGME
}
}
final Section sections = new Section(ranges);
/*
* Setting SampleModel and ColorModel.
*/
final SampleModel sampleModel = wrapper.getSampleModel().createCompatibleSampleModel(destWidth, destHeight);
final ColorModel colorModel = ImageIOUtilities.createColorModel(sampleModel);
/*
* Reads the requested sub-region only.
*/
final int size = destHeight * destWidth * numBands;
Array array = null;
try {
array = variable.read(sections);
DataBuffer dataBuffer = null;
if (array instanceof ArrayByte) {
dataBuffer = new DataBufferByte((byte[]) array.get1DJavaArray(byte.class), size);
} else if (array instanceof ArrayShort) {
dataBuffer = new DataBufferShort((short[]) array.get1DJavaArray(short.class), size);
} else if (array instanceof ArrayInt) {
dataBuffer = new DataBufferInt((int[]) array.get1DJavaArray(int.class), size);
} else if (array instanceof ArrayFloat) {
dataBuffer = new DataBufferFloat((float[]) array.get1DJavaArray(float.class), size);
} else if (array instanceof ArrayDouble) {
dataBuffer = new DataBufferDouble((double[]) array.get1DJavaArray(double.class), size);
}
WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0));
image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
} catch (InvalidRangeException e) {
// TODO LOGME
}
return image;
}
Aggregations