use of org.esa.snap.core.gpf.OperatorException in project s1tbx by senbox-org.
the class GoldsteinFilterOp method addSelectedBands.
/**
* Add the user selected bands to the target product.
*/
private void addSelectedBands() {
String[] sourceBandNames = null;
final Band[] sourceBands = OperatorUtils.getSourceBands(sourceProduct, sourceBandNames, false);
int i = 0;
while (i < sourceBands.length) {
final Band srcBandI = sourceBands[i];
final String unit = srcBandI.getUnit();
String nextUnit;
if (unit == null) {
throw new OperatorException("band " + srcBandI.getName() + " requires a unit");
} else if (unit.contains(Unit.DB)) {
throw new OperatorException("bands in dB are not supported");
} else if (unit.contains(Unit.IMAGINARY)) {
throw new OperatorException("I and Q bands should be selected in pairs");
} else if (unit.contains(Unit.REAL)) {
if (i + 1 >= sourceBands.length) {
throw new OperatorException("I and Q bands should be selected in pairs");
}
nextUnit = sourceBands[i + 1].getUnit();
if (nextUnit == null || !nextUnit.contains(Unit.IMAGINARY)) {
throw new OperatorException("I and Q bands should be selected in pairs");
}
} else {
// let other bands such as coherence pass through
ProductUtils.copyBand(srcBandI.getName(), sourceProduct, targetProduct, true);
++i;
continue;
}
final Band targetBandI = targetProduct.addBand(srcBandI.getName(), ProductData.TYPE_FLOAT32);
targetBandI.setUnit(unit);
targetBandI.setNoDataValueUsed(true);
targetBandI.setNoDataValue(srcBandI.getNoDataValue());
final Band srcBandQ = sourceBands[i + 1];
final Band targetBandQ = targetProduct.addBand(srcBandQ.getName(), ProductData.TYPE_FLOAT32);
targetBandQ.setUnit(nextUnit);
targetBandQ.setNoDataValueUsed(true);
targetBandQ.setNoDataValue(srcBandQ.getNoDataValue());
targetIQPair.put(targetBandI, targetBandQ);
final String suffix = targetBandI.getName().substring(targetBandI.getName().indexOf('_'));
ReaderUtils.createVirtualIntensityDBBand(targetProduct, targetBandI, targetBandQ, suffix);
ReaderUtils.createVirtualPhaseBand(targetProduct, targetBandI, targetBandQ, suffix);
i += 2;
}
}
use of org.esa.snap.core.gpf.OperatorException in project s1tbx by senbox-org.
the class GoldsteinFilterOp method computeTileStack.
/**
* Called by the framework in order to compute the stack of tiles for the given target bands.
* <p>The default implementation throws a runtime exception with the message "not implemented".</p>
*
* @param targetTileMap The current tiles to be computed for each target band.
* @param targetRectangle The area in pixel coordinates to be computed (same for all rasters in <code>targetRasters</code>).
* @param pm A progress monitor which should be used to determine computation cancelation requests.
* @throws OperatorException if an error occurs during computation of the target rasters.
*/
@Override
public void computeTileStack(Map<Band, Tile> targetTileMap, Rectangle targetRectangle, ProgressMonitor pm) throws OperatorException {
try {
final int x0 = targetRectangle.x;
final int y0 = targetRectangle.y;
final int w = targetRectangle.width;
final int h = targetRectangle.height;
if (w < FFTSize || h < FFTSize) {
return;
}
final Rectangle sourceTileRectangle = getSourceRectangle(x0, y0, w, h);
final int sx0 = sourceTileRectangle.x;
final int sy0 = sourceTileRectangle.y;
final int sw = sourceTileRectangle.width;
final int sh = sourceTileRectangle.height;
for (Band iBand : targetIQPair.keySet()) {
final Band qBand = targetIQPair.get(iBand);
final Tile iTargetTile = targetTileMap.get(iBand);
final Tile qTargetTile = targetTileMap.get(qBand);
final Tile iBandRaster = getSourceTile(sourceProduct.getBand(iBand.getName()), sourceTileRectangle);
final Tile qBandRaster = getSourceTile(sourceProduct.getBand(qBand.getName()), sourceTileRectangle);
final ProductData iBandData = iBandRaster.getDataBuffer();
final ProductData qBandData = qBandRaster.getDataBuffer();
final TileIndex srcIndex = new TileIndex(iBandRaster);
noDataValue = iBand.getNoDataValue();
// perform filtering with a sliding window
final boolean[][] mask = new boolean[FFTSize][FFTSize];
final double[][] I = new double[FFTSize][FFTSize];
final double[][] Q = new double[FFTSize][FFTSize];
final double[][] specI = new double[FFTSize][FFTSize];
final double[][] specQ = new double[FFTSize][FFTSize];
final double[][] pwrSpec = new double[FFTSize][FFTSize];
final double[][] fltSpec = new double[FFTSize][FFTSize];
final int colMax = I[0].length;
// arrays saving filtered I/Q data for the tile, note tile size could be different from 512x512 on boundary
final float[] iBandFiltered = new float[w * h];
final float[] qBandFiltered = new float[w * h];
final int stepSize = FFTSize / 4;
final int syMax = FastMath.min(sy0 + sh - FFTSize, sourceImageHeight - FFTSize);
final int sxMax = FastMath.min(sx0 + sw - FFTSize, sourceImageWidth - FFTSize);
for (int y = sy0; y <= syMax; y += stepSize) {
for (int x = sx0; x <= sxMax; x += stepSize) {
getComplexImagettes(x, y, iBandData, qBandData, srcIndex, I, Q, mask);
// check for no data value
boolean allNoData = true;
for (double[] aI : I) {
for (int c = 0; c < colMax; ++c) {
if (aI[c] != noDataValue) {
allNoData = false;
break;
}
}
if (!allNoData)
break;
}
if (allNoData) {
continue;
}
perform2DFFT(I, Q, specI, specQ);
getPowerSpectrum(specI, specQ, pwrSpec);
getFilteredPowerSpectrum(pwrSpec, fltSpec, alpha, halfWindowSize);
performInverse2DFFT(specI, specQ, fltSpec, I, Q);
updateFilteredBands(x0, y0, w, h, x, y, I, Q, mask, iBandFiltered, qBandFiltered);
}
}
// mask out pixels with low coherence
if (cohBand != null) {
try {
Tile cohBandRaster = getSourceTile(cohBand, targetRectangle);
final ProductData cohBandData = cohBandRaster.getDataBuffer();
final TileIndex cohIndex = new TileIndex(cohBandRaster);
final int yMax = y0 + h;
final int xMax = x0 + w;
for (int y = y0; y < yMax; y++) {
cohIndex.calculateStride(y);
for (int x = x0; x < xMax; x++) {
final int k = (y - y0) * w + x - x0;
if (cohBandData.getElemFloatAt(cohIndex.getIndex(x)) < coherenceThreshold) {
final int idx = iBandRaster.getDataBufferIndex(x, y);
iBandFiltered[k] = iBandData.getElemFloatAt(idx);
qBandFiltered[k] = qBandData.getElemFloatAt(idx);
}
}
}
} catch (Exception e) {
throw new OperatorException(e);
}
}
iTargetTile.setRawSamples(new ProductData.Float(iBandFiltered));
qTargetTile.setRawSamples(new ProductData.Float(qBandFiltered));
}
} catch (Exception e) {
throw new OperatorException(e);
}
}
use of org.esa.snap.core.gpf.OperatorException in project s1tbx by senbox-org.
the class WindFieldEstimationOp method computeTile.
/**
* Called by the framework in order to compute a tile for the given target band.
* <p>The default implementation throws a runtime exception with the message "not implemented".</p>
*
* @param targetBand The target band.
* @param targetTile The current tile associated with the target band to be computed.
* @param pm A progress monitor which should be used to determine computation cancelation requests.
* @throws OperatorException If an error occurs during computation of the target raster.
*/
@Override
public void computeTile(Band targetBand, Tile targetTile, ProgressMonitor pm) throws OperatorException {
final Rectangle targetTileRectangle = targetTile.getRectangle();
final int tx0 = targetTileRectangle.x;
final int ty0 = targetTileRectangle.y;
final int tw = targetTileRectangle.width;
final int th = targetTileRectangle.height;
// System.out.println("tx0 = " + tx0 + ", ty0 = " + ty0 + ", tw = " + tw + ", th = " + th);
final String targetBandName = targetBand.getName();
final List<WindFieldRecord> windFieldRecordList = new ArrayList<>();
final Band sourceBand = sourceProduct.getBand(targetBandName);
final double noDataValue = sourceBand.getNoDataValue();
final String pol = OperatorUtils.getBandPolarization(targetBandName, absRoot);
Tile sourceTile;
if (pol != null && !pol.contains("hh") && !pol.contains("vv")) {
throw new OperatorException("Polarization " + pol + " is not supported. Please select HH or VV.");
}
final Unit.UnitType bandUnit = Unit.getUnitType(sourceBand);
if (bandUnit != Unit.UnitType.INTENSITY && bandUnit != Unit.UnitType.INTENSITY_DB) {
throw new OperatorException("Please select calibrated amplitude or intensity band for wind field estimation");
}
// copy the original band data
targetTile.setRawSamples(getSourceTile(sourceBand, targetTile.getRectangle()).getRawSamples());
final boolean normlizeSigma = pol.contains("hh");
// loop through the center pixel of each frame in the target tile
int xStart = halfWindowSize;
while (xStart < tx0) {
xStart += windowSize;
}
int yStart = halfWindowSize;
while (yStart < ty0) {
yStart += windowSize;
}
final int maxY = ty0 + th;
final int maxX = tx0 + tw;
final int halfWindowArea = windowSize * windowSize / 2;
final int arrowSize = halfWindowSize * 2 / 3;
for (int y = yStart; y < maxY; y += windowSize) {
for (int x = xStart; x < maxX; x += windowSize) {
// get source data for the frame
final Rectangle sourceTileRectangle = getSourceRectangle(x, y);
if (sourceTileRectangle == null) {
continue;
}
final double lat = latitudeTPG.getPixelDouble(x, y);
final double lon = longitudeTPG.getPixelDouble(x, y);
final double theta = incidenceAngle.getPixelDouble(x, y);
sourceTile = getSourceTile(sourceBand, sourceTileRectangle);
final int numLandPixels = getNumLandPixels(sourceTile, noDataValue);
if (numLandPixels >= halfWindowArea) {
continue;
}
final double nrcs = getNormalizedRadarCrossSection(sourceTile, bandUnit, x, y, normlizeSigma, theta);
// estimate wind direction for the frame
final double[] direction = { 0.0, 0.0 };
double ratio = estimateWindDirection(sourceTile, numLandPixels, noDataValue, direction);
/*
if (ratio < 0.2 || ratio > 0.8) {
continue;
}
*/
// estimate wind speed for the frame
final double speed = estimateWindSpeed(nrcs, direction, theta);
// save wind field info
final WindFieldRecord record = new WindFieldRecord(x, y, lat, lon, speed, arrowSize * direction[0], arrowSize * direction[1], ratio);
windFieldRecordList.add(record);
}
}
if (!windFieldRecordList.isEmpty()) {
AddWindRecordsAsVectors(windFieldRecordList);
}
bandWindFieldRecord.get(targetBandName).addAll(windFieldRecordList);
windFieldEstimated = true;
}
use of org.esa.snap.core.gpf.OperatorException in project s1tbx by senbox-org.
the class AzimuthShiftEstimationUsingESDOp method initialize.
/**
* Initializes this operator and sets the one and only target product.
* <p>The target product can be either defined by a field of type {@link Product} annotated with the
* {@link TargetProduct TargetProduct} annotation or
* by calling {@link #setTargetProduct} method.</p>
* <p>The framework calls this method after it has created this operator.
* Any client code that must be performed before computation of tile data
* should be placed here.</p>
*
* @throws OperatorException If an error occurs during operator initialisation.
* @see #getTargetProduct()
*/
@Override
public void initialize() throws OperatorException {
try {
final InputProductValidator validator = new InputProductValidator(sourceProduct);
validator.checkIfSARProduct();
validator.checkIfSentinel1Product();
su = new Sentinel1Utils(sourceProduct);
su.computeDopplerRate();
su.computeReferenceTime();
subSwath = su.getSubSwath();
polarizations = su.getPolarizations();
subSwathNames = su.getSubSwathNames();
if (subSwathNames.length != 1) {
throw new OperatorException("Split product is expected");
} else {
// subSwathIndex is always 1 because of split product
subSwathIndex = 1;
}
constructSourceMetadata();
constructTargetMetadata();
createTargetProduct();
} catch (Throwable e) {
OperatorUtils.catchOperatorException(getId(), e);
}
}
use of org.esa.snap.core.gpf.OperatorException in project s1tbx by senbox-org.
the class AzimuthShiftOp method initialize.
/**
* Initializes this operator and sets the one and only target product.
* <p>The target product can be either defined by a field of type {@link Product} annotated with the
* {@link TargetProduct TargetProduct} annotation or
* by calling {@link #setTargetProduct} method.</p>
* <p>The framework calls this method after it has created this operator.
* Any client code that must be performed before computation of tile data
* should be placed here.</p>
*
* @throws OperatorException If an error occurs during operator initialisation.
* @see #getTargetProduct()
*/
@Override
public void initialize() throws OperatorException {
try {
final InputProductValidator validator = new InputProductValidator(sourceProduct);
validator.checkIfSARProduct();
validator.checkIfSentinel1Product();
checkDerampDemodPhaseBand();
final Sentinel1Utils su = new Sentinel1Utils(sourceProduct);
su.computeDopplerRate();
subSwath = su.getSubSwath();
subSwathNames = su.getSubSwathNames();
if (subSwathNames.length != 1) {
throw new OperatorException("Split product is expected.");
} else {
// Integer.parseInt(subSwathNames[0].substring(subSwathNames[0].length()-1));
subSwathIndex = 1;
swathIndexStr = subSwathNames[0].substring(2);
}
polarizations = su.getPolarizations();
createTargetProduct();
} catch (Throwable e) {
OperatorUtils.catchOperatorException(getId(), e);
}
}
Aggregations