use of com.beanit.openiec61850.Array in project geotoolkit by Geomatys.
the class NetCDFExtractor method getProcedureTS.
private static List<ProcedureTree> getProcedureTS(final NCFieldAnalyze analyze, final String procedureID, final List<String> acceptedSensorID) throws NetCDFParsingException {
final List<ProcedureTree> results = new ArrayList<>();
if (analyze.mainField == null) {
LOGGER.warning("No main field found");
return results;
}
LOGGER.info("parsing netCDF TS");
try {
final List<String> separators = parseSeparatorValues(analyze);
final boolean single = separators.isEmpty();
Array latArray = null;
Array lonArray = null;
if (analyze.hasSpatial()) {
latArray = analyze.getArrayFromField(analyze.latField);
lonArray = analyze.getArrayFromField(analyze.lonField);
}
final Variable timeVar = analyze.vars.get(analyze.mainField.name);
final String timeUnits = analyze.mainField.uom;
final Array timeArray = analyze.file.readArrays(Arrays.asList(timeVar)).get(0);
final boolean constantT = analyze.mainField.dimension == 1;
final boolean timeFirst = analyze.mainField.mainVariableFirst;
final Set<String> fields = analyze.getPhenomenonArrayMap().keySet();
if (single) {
if (acceptedSensorID == null || acceptedSensorID.contains(procedureID)) {
final Process proc = (Process) OMUtils.buildProcess(procedureID);
final ProcedureTree compo = new ProcedureTree(proc.getHref(), proc.getName(), proc.getHref(), "Component", "timeseries", fields);
results.add(compo);
final int count = timeVar.getDimension(0).getLength();
final GeoSpatialBound gb = new GeoSpatialBound();
if (analyze.hasSpatial()) {
final double latitude = getDoubleValue(latArray, analyze.latField.fillValue);
final double longitude = Longitude.normalize(getDoubleValue(lonArray, analyze.lonField.fillValue));
if (!Double.isNaN(latitude) && !Double.isNaN(longitude)) {
gb.addXYCoordinate(longitude, latitude);
}
}
// iterating over time
for (int i = 0; i < count; i++) {
final long millis = getTimeValue(timeUnits, timeArray, i);
if (millis == 0 || millis == LIMIT) {
continue;
}
gb.addDate(millis);
}
compo.spatialBound.merge(gb);
}
} else {
final Process proc = (Process) OMUtils.buildProcess(procedureID);
final ProcedureTree system = new ProcedureTree(proc.getHref(), proc.getName(), proc.getHref(), "System", "timeseries", fields);
results.add(system);
for (int j = 0; j < separators.size(); j++) {
final String identifier = separators.get(j);
final int count = getGoodTimeDimension(timeVar, analyze.dimensionSeparator).getLength();
final GeoSpatialBound gb = new GeoSpatialBound();
final String currentProcID = procedureID + '-' + identifier;
final Process currentProc = (Process) OMUtils.buildProcess(currentProcID);
final ProcedureTree compo = new ProcedureTree(currentProc.getHref(), currentProc.getName(), currentProc.getDescription(), "Component", "timeseries", fields);
if (acceptedSensorID == null || acceptedSensorID.contains(currentProcID)) {
if (analyze.hasSpatial()) {
final double latitude = getDoubleValue(latArray, j, analyze.latField.fillValue);
final double longitude = Longitude.normalize(getDoubleValue(lonArray, j, analyze.lonField.fillValue));
if (!Double.isNaN(latitude) && !Double.isNaN(longitude)) {
gb.addXYCoordinate(longitude, latitude);
}
}
for (int i = 0; i < count; i++) {
final long millis = getTimeValue(timeUnits, timeFirst, constantT, timeArray, i, j);
if (millis == 0 || millis == LIMIT) {
continue;
}
gb.addDate(millis);
}
compo.spatialBound.merge(gb);
system.children.add(compo);
}
}
}
} catch (IOException | IllegalArgumentException ex) {
throw new NetCDFParsingException("error while parsing netcdf timeserie", ex);
}
LOGGER.info("datablock parsed");
return results;
}
use of com.beanit.openiec61850.Array in project s1tbx by senbox-org.
the class K5HDF method readBandRasterDataImpl.
public void readBandRasterDataImpl(int sourceOffsetX, int sourceOffsetY, int sourceWidth, int sourceHeight, int sourceStepX, int sourceStepY, Band destBand, int destOffsetX, int destOffsetY, int destWidth, int destHeight, ProductData destBuffer, ProgressMonitor pm) throws IOException {
Guardian.assertTrue("sourceStepX == 1 && sourceStepY == 1", sourceStepX == 1 && sourceStepY == 1);
Guardian.assertTrue("sourceWidth == destWidth", sourceWidth == destWidth);
Guardian.assertTrue("sourceHeight == destHeight", sourceHeight == destHeight);
final int sceneHeight = product.getSceneRasterHeight();
final int sceneWidth = product.getSceneRasterWidth();
destHeight = Math.min(destHeight, sceneHeight - sourceOffsetY);
destWidth = Math.min(destWidth, sceneWidth - destOffsetX);
final int y0 = yFlipped ? (sceneHeight - 1) - sourceOffsetY : sourceOffsetY;
final Variable variable = bandMap.get(destBand);
final int rank = variable.getRank();
final int[] origin = new int[rank];
final int[] shape = new int[rank];
for (int i = 0; i < rank; i++) {
shape[i] = 1;
origin[i] = 0;
}
shape[0] = 1;
shape[1] = destWidth;
origin[1] = sourceOffsetX;
if (isComplex && destBand.getUnit().equals(Unit.IMAGINARY)) {
origin[2] = 1;
}
pm.beginTask("Reading data from band " + destBand.getName(), destHeight);
try {
for (int y = 0; y < destHeight; y++) {
origin[0] = yFlipped ? y0 - y : y0 + y;
final Array array;
synchronized (netcdfFile) {
array = variable.read(origin, shape);
}
if (destBand.getDataType() == ProductData.TYPE_FLOAT32) {
for (int x = 0; x < destWidth; x++) {
destBuffer.setElemFloatAt(y * destWidth + x, ArrayCopy.toFloat(array.getShort(x)));
}
} else {
System.arraycopy(array.getStorage(), 0, destBuffer.getElems(), y * destWidth, destWidth);
}
pm.worked(1);
}
} catch (InvalidRangeException e) {
throw new IOException(e.getMessage(), e);
} finally {
pm.done();
}
}
use of com.beanit.openiec61850.Array in project s1tbx by senbox-org.
the class Sentinel1OCNReader method readDataForRank4Variable.
private synchronized void readDataForRank4Variable(int sourceOffsetX, int sourceOffsetY, int sourceWidth, int sourceHeight, int sourceStepX, int sourceStepY, Variable var, int destWidth, int destHeight, ProductData destBuffer) {
final int[] shape0 = var.getShape();
// shape0[0] is height of "outer" grid.
// shape0[1] is width of "outer" grid.
// shape0[2] is height of "inner" grid.
// shape0[3] is width of "inner" grid.
final int[] origin = { sourceOffsetY / shape0[2], sourceOffsetX / shape0[3], 0, 0 };
// System.out.println("sourceOffsetY = " + sourceOffsetY + " shape0[2] = " + shape0[2] + " sourceOffsetX = " + sourceOffsetX + " shape0[3] = " + shape0[3]);
// System.out.println("origin " + origin[0] + " " + origin[1]);
final int outerYEnd = (sourceOffsetY + (sourceHeight - 1) * sourceStepY) / shape0[2];
final int outerXEnd = (sourceOffsetX + (sourceWidth - 1) * sourceStepX) / shape0[3];
// System.out.println("sourceHeight = " + sourceHeight + " sourceStepY = " + sourceStepY + " outerYEnd = " + outerYEnd);
// System.out.println("sourceWidth = " + sourceWidth + " sourceStepX = " + sourceStepX + " outerXEnd = " + outerXEnd);
final int[] shape = { outerYEnd - origin[0] + 1, outerXEnd - origin[1] + 1, shape0[2], shape0[3] };
try {
final Array srcArray = var.read(origin, shape);
final int[] idx = new int[4];
for (int i = 0; i < destHeight; i++) {
// srcY is wrt to what is read in srcArray
final int srcY = (sourceOffsetY - shape0[2] * origin[0]) + i * sourceStepY;
idx[0] = srcY / shape[2];
for (int j = 0; j < destWidth; j++) {
// srcX is wrt to what is read in srcArray
final int srcX = (sourceOffsetX - shape0[3] * origin[1]) + j * sourceStepX;
idx[1] = srcX / shape[3];
idx[2] = srcY - idx[0] * shape[2];
idx[3] = srcX - idx[1] * shape[3];
final int srcIdx = (idx[0] * shape[1] * shape[2] * shape[3]) + (idx[1] * shape[2] * shape[3]) + (idx[2] * shape[3]) + idx[3];
final int destIdx = i * destWidth + j;
destBuffer.setElemFloatAt(destIdx, srcArray.getFloat(srcIdx));
}
}
} catch (IOException e) {
SystemUtils.LOG.severe("Sentinel1OCNReader.readDataForRank4Variable: IOException when reading variable " + var.getFullName());
} catch (InvalidRangeException e) {
SystemUtils.LOG.severe("Sentinel1OCNReader.readDataForRank4Variable: InvalidRangeException when reading variable " + var.getFullName());
}
}
use of com.beanit.openiec61850.Array in project s1tbx by senbox-org.
the class NetCDFUtils method createTiePointGrid.
public static TiePointGrid createTiePointGrid(final Variable variable, final int gridWidth, final int gridHeight, final int sceneWidth, final int sceneHeight) throws IOException {
final NcAttributeMap attMap = NcAttributeMap.create(variable);
final double subSamplingX = (double) sceneWidth / (double) (gridWidth - 1);
final double subSamplingY = (double) sceneHeight / (double) (gridHeight - 1);
final Array data = variable.read();
// (float[])data.copyTo1DJavaArray();
final float[] dataArray = new float[(int) data.getSize()];
for (int i = 0; i < data.getSize(); ++i) {
dataArray[i] = data.getFloat(i);
}
final TiePointGrid tpg = new TiePointGrid(variable.getShortName(), gridWidth, gridHeight, 0, 0, subSamplingX, subSamplingY, dataArray);
tpg.setDescription(getDescription(variable, attMap));
tpg.setUnit(getUnit(variable, attMap));
tpg.setScalingFactor(getScalingFactor(attMap));
tpg.setScalingOffset(getAddOffset(attMap));
final Number noDataValue = getNoDataValue(attMap);
if (noDataValue != null) {
tpg.setNoDataValue(noDataValue.doubleValue());
tpg.setNoDataValueUsed(true);
}
return tpg;
}
use of com.beanit.openiec61850.Array in project s1tbx by senbox-org.
the class NetCDFUtils method createMapInfoX.
public static MapInfoX createMapInfoX(final Variable lonVar, final Variable latVar, final int sceneRasterWidth, final int sceneRasterHeight) throws IOException {
float pixelX;
float pixelY;
float easting;
float northing;
float pixelSizeX;
float pixelSizeY;
final NcAttributeMap lonAttrMap = NcAttributeMap.create(lonVar);
final Number lonValidMin = lonAttrMap.getNumericValue(NetcdfConstants.VALID_MIN_ATT_NAME);
final Number lonStep = lonAttrMap.getNumericValue(NetcdfConstants.STEP_ATT_NAME);
final NcAttributeMap latAttrMap = NcAttributeMap.create(latVar);
final Number latValidMin = latAttrMap.getNumericValue(NetcdfConstants.VALID_MIN_ATT_NAME);
final Number latStep = latAttrMap.getNumericValue(NetcdfConstants.STEP_ATT_NAME);
boolean yFlipped;
if (lonValidMin != null && lonStep != null && latValidMin != null && latStep != null) {
// COARDS convention uses 'valid_min' and 'step' attributes
pixelX = 0.5f;
pixelY = (sceneRasterHeight - 1.0f) + 0.5f;
easting = lonValidMin.floatValue();
northing = latValidMin.floatValue();
pixelSizeX = lonStep.floatValue();
pixelSizeY = latStep.floatValue();
// must flip
// todo - check
yFlipped = true;
} else {
// CF convention
final Array lonData = lonVar.read();
final Array latData = latVar.read();
final Index i0 = lonData.getIndex().set(0);
final Index i1 = lonData.getIndex().set(1);
pixelSizeX = lonData.getFloat(i1) - lonData.getFloat(i0);
easting = lonData.getFloat(i0);
final int latSize = (int) latVar.getSize();
final Index j0 = latData.getIndex().set(0);
final Index j1 = latData.getIndex().set(1);
pixelSizeY = latData.getFloat(j1) - latData.getFloat(j0);
pixelX = 0.5f;
pixelY = 0.5f;
// this should be the 'normal' case
if (pixelSizeY < 0) {
pixelSizeY *= -1;
yFlipped = false;
northing = latData.getFloat(latData.getIndex().set(0));
} else {
yFlipped = true;
northing = latData.getFloat(latData.getIndex().set(latSize - 1));
}
}
if (pixelSizeX <= 0 || pixelSizeY <= 0) {
return null;
}
final MapProjection projection = MapProjectionRegistry.getProjection(IdentityTransformDescriptor.NAME);
final MapInfo mapInfo = new MapInfo(projection, pixelX, pixelY, easting, northing, pixelSizeX, pixelSizeY, Datum.WGS_84);
mapInfo.setSceneWidth(sceneRasterWidth);
mapInfo.setSceneHeight(sceneRasterHeight);
return new MapInfoX(mapInfo, yFlipped);
}
Aggregations