use of com.beanit.openiec61850.Array 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 com.beanit.openiec61850.Array in project imageio-ext by geosolutions-it.
the class HDF4APSImageReader method initializeProfile.
/**
* Initialize main properties for this <code>HDF4APSImageReader</code>
*/
protected void initializeProfile() throws IOException {
final NetcdfDataset dataset = reader.getDataset();
if (dataset == null) {
throw new IOException("Unable to initialize profile due to a null dataset");
}
final List<Variable> variables = dataset.getVariables();
final List<Attribute> attributes = dataset.getGlobalAttributes();
final int numVars = variables.size();
reader.setNumGlobalAttributes(attributes.size());
// //
//
// Getting projection dataset name
//
// //
final String navAttrib = NetCDFUtilities.getGlobalAttributeAsString(dataset, HDF4APSProperties.PFA_NA_MAPPROJECTION);
if (navAttrib != null && navAttrib.length() > 0) {
projectionDatasetName = navAttrib;
}
final String prodAttrib = NetCDFUtilities.getGlobalAttributeAsString(dataset, HDF4APSProperties.PRODLIST);
int numImages = 0;
if (prodAttrib != null && prodAttrib.length() > 0) {
String[] products = prodAttrib.split(",");
productList = HDF4APSProperties.refineProductList(products);
numImages = productList.length;
} else {
numImages = numVars;
}
setNumImages(numImages);
reader.setNumImages(numImages);
final Map<Range, APSDatasetWrapper> indexMap = new HashMap<Range, APSDatasetWrapper>(numImages);
Variable varProjection;
// //
//
// Setting spatial domain
//
// //
// getting map dataset
varProjection = dataset.findVariable(projectionDatasetName);
if (varProjection != null && varProjection.getName().equalsIgnoreCase(projectionDatasetName)) {
// TODO: All projection share the same dataset
// structure?
Array data = varProjection.read();
final int datatype = NetCDFUtilities.getRawDataType(varProjection);
if (projectionMap == null) {
projectionMap = buildProjectionAttributesMap(data, datatype);
// Force UoM of MapBoundary product as the last element in
// the map
}
}
try {
// Scanning all the datasets
for (Variable var : variables) {
final String name = var.getName();
for (int j = 0; j < numImages; j++) {
// Checking if the actual dataset is a product.
if (name.equals(productList[j])) {
// Updating the subDatasetsMap map
indexMap.put(new Range(j, j + 1), new APSDatasetWrapper(var));
break;
}
}
}
} catch (InvalidRangeException e) {
throw new IllegalArgumentException("Error occurred during NetCDF file parsing", e);
}
reader.setIndexMap(indexMap);
}
use of com.beanit.openiec61850.Array in project imageio-ext by geosolutions-it.
the class SWANConverter 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 hasZeta = false;
// input dimensions
final Dimension timeDim0 = ncFileIn.findDimension("time");
final int nTimes = timeDim0.getLength();
final Dimension latDim0 = ncFileIn.findDimension(NetCDFUtilities.LATITUDE);
final int nLat = latDim0.getLength();
final Dimension lonDim0 = ncFileIn.findDimension(NetCDFUtilities.LONGITUDE);
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(NetCDFUtilities.LONGITUDE);
final DataType lonDataType = lonOriginalVar.getDataType();
final Variable latOriginalVar = ncFileIn.findVariable(NetCDFUtilities.LATITUDE);
final DataType latDataType = latOriginalVar.getDataType();
final Array latOriginalData = latOriginalVar.read();
final Array lonOriginalData = lonOriginalVar.read();
// //
//
// Depth related vars
//
// //
Array levelOriginalData = null;
int nZeta = 0;
Array zeta1Data = null;
Dimension zDim = null;
DataType zetaDataType = null;
// Depth
final Variable levelOriginalVar = ncFileIn.findVariable("z");
if (levelOriginalVar != null) {
nZeta = levelOriginalVar.getDimension(0).getLength();
levelOriginalData = levelOriginalVar.read();
zetaDataType = levelOriginalVar.getDataType();
hasZeta = true;
}
Dimension timeDim = ncFileOut.addDimension("time", nTimes);
Dimension latDim = ncFileOut.addDimension(NetCDFUtilities.LAT, nLat);
Dimension lonDim = ncFileOut.addDimension(NetCDFUtilities.LON, nLon);
if (hasZeta)
zDim = ncFileOut.addDimension(NetCDFUtilities.HEIGHT, nZeta);
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, NetCDFUtilities.LAT);
ncFileOut.addVariable(NetCDFUtilities.LON, lonDataType, new Dimension[] { lonDim });
NetCDFConverterUtilities.setVariableAttributes(lonOriginalVar, ncFileOut, NetCDFUtilities.LON);
if (hasZeta) {
ncFileOut.addVariable(NetCDFUtilities.HEIGHT, zetaDataType, new Dimension[] { zDim });
NetCDFConverterUtilities.setVariableAttributes(levelOriginalVar, ncFileOut, NetCDFUtilities.HEIGHT, new String[] { "long_name" });
ncFileOut.addVariableAttribute(NetCDFUtilities.HEIGHT, "positive", "up");
ncFileOut.addVariableAttribute(NetCDFUtilities.HEIGHT, "long_name", NetCDFUtilities.HEIGHT);
}
// 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 (hasZeta) {
// depth level Variable
zeta1Data = NetCDFConverterUtilities.getArray(nZeta, zetaDataType);
NetCDFConverterUtilities.setData1D(levelOriginalData, zeta1Data, zetaDataType, nZeta, false);
}
// {} Variables
final ArrayList<String> variables = new ArrayList<String>(5);
int numVars = 0;
List<Variable> findVariables = ncFileIn.getVariables();
for (Variable var : findVariables) {
if (var != null) {
String varName = var.getName();
if (varName.equalsIgnoreCase(NetCDFUtilities.LATITUDE) || varName.equalsIgnoreCase(NetCDFUtilities.LONGITUDE) || varName.equalsIgnoreCase(NetCDFUtilities.TIME) || varName.equalsIgnoreCase(NetCDFUtilities.ZETA))
continue;
variables.add(varName);
List<Dimension> dims = var.getDimensions();
boolean hasLocalZeta = false;
for (Dimension dim : dims) {
if (dim.getName().equalsIgnoreCase(NetCDFUtilities.ZETA)) {
hasLocalZeta = true;
break;
}
}
if (hasZeta && hasLocalZeta)
ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, zDim, latDim, lonDim });
else
ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, latDim, lonDim });
NetCDFConverterUtilities.setVariableAttributes(var, ncFileOut, new String[] { "missing_value" });
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 (hasZeta) {
Variable heightVar = ncFileOut.findVariable(NetCDFUtilities.HEIGHT);
zDim.addCoordinateVariable(heightVar);
ncFileOut.write(NetCDFUtilities.HEIGHT, zeta1Data);
}
for (int i = 0; i < numVars; i++) {
String varName = (String) variables.get(i);
Variable var = ncFileIn.findVariable(varName);
boolean hasLocalZeta = NetCDFConverterUtilities.hasThisDimension(var, NetCDFUtilities.ZETA);
Array originalVarArray = var.read();
DataType varDataType = var.getDataType();
Array destArray = null;
int[] dimensions = null;
if (hasZeta && hasLocalZeta) {
dimensions = new int[] { timeDim.getLength(), zDim.getLength(), latDim.getLength(), lonDim.getLength() };
} else {
dimensions = new int[] { timeDim.getLength(), latDim.getLength(), lonDim.getLength() };
}
destArray = NetCDFConverterUtilities.getArray(dimensions, varDataType);
final boolean setZeta = hasZeta && hasLocalZeta;
final int[] loopLengths;
if (setZeta)
loopLengths = new int[] { nTimes, nZeta, nLat, nLon };
else
loopLengths = new int[] { nTimes, nLat, nLon };
NetCDFConverterUtilities.writeData(ncFileOut, varName, var, originalVarArray, destArray, false, 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 com.beanit.openiec61850.Array in project OpenTripPlanner by opentripplanner.
the class GenericEdgeUpdater method updateEdge.
/**
* Updates the edge according to the generic variable data
*
* @param streetEdge street edge being updated with extra data
*/
private void updateEdge(StreetEdge streetEdge) {
Vertex fromVertex = streetEdge.getFromVertex();
Vertex toVertex = streetEdge.getToVertex();
Coordinate fromCoordinate = fromVertex.getCoordinate();
Coordinate toCoordinate = toVertex.getCoordinate();
HashMap<String, float[]> edgeGenericDataValues = new HashMap<>();
for (Map.Entry<String, Array> variableValues : genericVariablesData.entrySet()) {
float[] averageDataValue = getAverageValue(fromCoordinate.x, fromCoordinate.y, toCoordinate.x, toCoordinate.y, variableValues.getKey());
edgeGenericDataValues.put(variableValues.getKey(), averageDataValue);
}
DataOverlayStreetEdgeCostExtension edgeGenData = new DataOverlayStreetEdgeCostExtension(dataStartTime, edgeGenericDataValues, timeFormat);
streetEdge.setCostExtension(edgeGenData);
edgesUpdated++;
if (LOG.isInfoEnabled() && (edgesUpdated % REPORT_EVERY_N_EDGE) == 0) {
LOG.info(String.format("%d / %d street edges updated", edgesUpdated, streetEdges.size()));
}
}
use of com.beanit.openiec61850.Array in project OpenTripPlanner by opentripplanner.
the class GenericEdgeUpdater method calculateDataStartTime.
/**
* Returns ms from epoch for the first data point of the file, by default data format is assumed
* to be hours
*
* @return epoch milliseconds
*/
private long calculateDataStartTime(TimeUnit timeFormat) {
Array timeArray = dataFile.getTimeArray();
Class dataType = timeArray.getDataType().getPrimitiveClassType();
Instant originInstant = this.dataFile.getOriginDate().toInstant();
if ((timeFormat == null || timeFormat == TimeUnit.SECONDS) && dataType.equals(Integer.TYPE)) {
return originInstant.plusSeconds(timeArray.getInt(0)).toEpochMilli();
} else if (timeFormat == TimeUnit.MS_EPOCH && dataType.equals(Long.TYPE)) {
return timeArray.getLong(0);
} else {
long addSeconds = 0;
if (dataType.equals(Double.TYPE)) {
addSeconds = (long) (timeArray.getDouble(0) * 3600);
} else if (dataType.equals(Float.TYPE)) {
addSeconds = (long) (timeArray.getFloat(0) * 3600);
}
return originInstant.plusSeconds(addSeconds).toEpochMilli();
}
}
Aggregations