Search in sources :

Example 96 with OperatorException

use of org.esa.snap.core.gpf.OperatorException in project s1tbx by senbox-org.

the class ERSCalibrator method getZeroDopplerAzimuthTimeOfCentreAzimuthPixel.

/**
 * Get the zero doppler azimuth time of the centre azimuth pixel (in second).
 *
 * @return The zero doppler time.
 */
private double getZeroDopplerAzimuthTimeOfCentreAzimuthPixel() {
    // Field 126/5 in PRI Data Set Summary Record (in UTC)
    final MetadataElement facility = origMetadataRoot.getElement("Leader").getElement("Scene Parameters");
    if (facility == null) {
        throw new OperatorException("Scene Parameters not found");
    }
    final MetadataAttribute attr = facility.getAttribute("Zero-doppler azimuth time of centre azimuth pixel");
    if (attr == null) {
        throw new OperatorException("Zero-doppler azimuth time of centre azimuth pixel not found");
    }
    // in s
    return convertUTCTimes(attr.getData().getElemString());
}
Also used : MetadataElement(org.esa.snap.core.datamodel.MetadataElement) MetadataAttribute(org.esa.snap.core.datamodel.MetadataAttribute) OperatorException(org.esa.snap.core.gpf.OperatorException)

Example 97 with OperatorException

use of org.esa.snap.core.gpf.OperatorException in project s1tbx by senbox-org.

the class ERSCalibrator method getIncidenceAngleAtFirstRangePixel.

/**
 * Get the incidence angle at the first range pixel (in degree).
 *
 * @return The incidence angle.
 */
private double getIncidenceAngleAtFirstRangePixel() {
    // Field 56 in PRI Facility Related Data Record (in degree)
    final MetadataElement facility = origMetadataRoot.getElement("Leader").getElement("Facility Related");
    if (facility == null) {
        throw new OperatorException("Facility Related not found");
    }
    final MetadataAttribute attr = facility.getAttribute("Incidence angle at first range pixel");
    if (attr == null) {
        throw new OperatorException("Incidence angle at first range pixel not found");
    }
    return attr.getData().getElemFloat();
}
Also used : MetadataElement(org.esa.snap.core.datamodel.MetadataElement) MetadataAttribute(org.esa.snap.core.datamodel.MetadataAttribute) OperatorException(org.esa.snap.core.gpf.OperatorException)

Example 98 with OperatorException

use of org.esa.snap.core.gpf.OperatorException in project s1tbx by senbox-org.

the class ERSCalibrator method getNumOfRecordsInMainProcParam.

/**
 * Get number of records in Main Processing Params data set.
 */
private void getNumOfRecordsInMainProcParam() {
    MetadataElement dsd = origMetadataRoot.getElement("DSD").getElement("DSD.3");
    if (dsd == null) {
        throw new OperatorException("DSD not found");
    }
    MetadataAttribute numRecordsAttr = dsd.getAttribute("num_records");
    if (numRecordsAttr == null) {
        throw new OperatorException("num_records not found");
    }
    numMPPRecords = numRecordsAttr.getData().getElemInt();
    if (numMPPRecords < 1) {
        throw new OperatorException("Invalid num_records.");
    }
// System.out.println("The number of Main Processing Params records is " + numMPPRecords);
}
Also used : MetadataElement(org.esa.snap.core.datamodel.MetadataElement) MetadataAttribute(org.esa.snap.core.datamodel.MetadataAttribute) OperatorException(org.esa.snap.core.gpf.OperatorException)

Example 99 with OperatorException

use of org.esa.snap.core.gpf.OperatorException in project s1tbx by senbox-org.

the class ERSCalibrator 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 {
    try {
        final Rectangle targetTileRectangle = targetTile.getRectangle();
        final int x0 = targetTileRectangle.x;
        final int y0 = targetTileRectangle.y;
        final int w = targetTileRectangle.width;
        final int h = targetTileRectangle.height;
        // System.out.println("x0 = " + x0 + ", y0 = " + y0 + ", w = " + w + ", h = " + h);
        final ProductData trgData = targetTile.getDataBuffer();
        Band sourceBand1 = null;
        Band sourceBand2 = null;
        Tile sourceRaster1 = null;
        Tile sourceRaster2 = null;
        ProductData srcData1 = null;
        ProductData srcData2 = null;
        final String[] srcBandNames = targetBandNameToSourceBandName.get(targetBand.getName());
        if (srcBandNames.length == 1) {
            sourceBand1 = sourceProduct.getBand(srcBandNames[0]);
            sourceRaster1 = getSourceTile(sourceBand1, targetTileRectangle);
            srcData1 = sourceRaster1.getDataBuffer();
        } else {
            sourceBand1 = sourceProduct.getBand(srcBandNames[0]);
            sourceBand2 = sourceProduct.getBand(srcBandNames[1]);
            sourceRaster1 = getSourceTile(sourceBand1, targetTileRectangle);
            sourceRaster2 = getSourceTile(sourceBand2, targetTileRectangle);
            srcData1 = sourceRaster1.getDataBuffer();
            srcData2 = sourceRaster2.getDataBuffer();
        }
        final Unit.UnitType tgtBandUnit = Unit.getUnitType(targetBand);
        final Unit.UnitType srcBandUnit = Unit.getUnitType(sourceBand1);
        // copy band if unit is phase
        if (tgtBandUnit == Unit.UnitType.PHASE) {
            targetTile.setRawSamples(sourceRaster1.getRawSamples());
            return;
        }
        if (applyAntennaPatternCorrection && !isAntPattAvailable) {
            computeAntennaPatternCorrectionFactors(0, sourceImageWidth);
        }
        if (applyADCSaturationCorrection && !adcHasBeenTestedFlag) {
            testADC(sourceBand1, sourceBand2, srcBandUnit);
        }
        boolean applyADCSaturationCorrectionToCurrentTile = false;
        if (applyADCSaturationCorrection && h >= blockHeight && w >= blockWidth) {
            applyADCSaturationCorrectionToCurrentTile = true;
        }
        double[][] adcPowerLoss = null;
        if (applyADCSaturationCorrectionToCurrentTile) {
            adcPowerLoss = computeADCPowerLossValuesForCurrentTile(sourceBand1, sourceBand2, x0, y0, w, h, srcBandUnit);
        }
        final double k = calibrationConstant * FastMath.sin(referenceIncidenceAngle);
        final int maxY = y0 + h;
        final int maxX = x0 + w;
        double sigma, dn, i, q, phaseTerm = 0.0;
        int index;
        int adcJ = 0;
        for (int x = x0; x < maxX; x++) {
            final double sinIncidenceAngleByK = FastMath.sin(incidenceAngles[x]) / k;
            if (applyADCSaturationCorrectionToCurrentTile) {
                adcJ = Math.min(((x - x0) / blockWidth), adcPowerLoss[0].length - 1);
            }
            for (int y = y0; y < maxY; y++) {
                index = sourceRaster1.getDataBufferIndex(x, y);
                dn = srcData1.getElemDoubleAt(index);
                if (srcBandUnit == Unit.UnitType.AMPLITUDE) {
                    dn *= dn;
                } else if (srcBandUnit == Unit.UnitType.INTENSITY) {
                } else if (srcBandUnit == Unit.UnitType.REAL) {
                    i = dn;
                    q = srcData2.getElemDoubleAt(index);
                    dn = i * i + q * q;
                    if (dn > 0.0) {
                        if (tgtBandUnit == Unit.UnitType.REAL) {
                            phaseTerm = i / Math.sqrt(dn);
                        } else if (tgtBandUnit == Unit.UnitType.IMAGINARY) {
                            phaseTerm = q / Math.sqrt(dn);
                        }
                    } else {
                        phaseTerm = 0.0;
                    }
                } else if (srcBandUnit == Unit.UnitType.INTENSITY_DB) {
                    // convert dB to linear scale
                    dn = FastMath.pow(10, dn / 10.0);
                } else {
                    throw new OperatorException("ERS Calibration: unhandled unit");
                }
                if (inputSigma0) {
                    sigma = dn;
                } else {
                    double calFactor = sinIncidenceAngleByK;
                    if (applyAntennaPatternCorrection) {
                        calFactor *= antennaPatternCorrFactor[x];
                    }
                    if (applyRangeSpreadingLossCorrection) {
                        calFactor *= rangeSpreadingLoss[x];
                    }
                    if (applyReplicaPowerCorrection) {
                        calFactor *= replicaPulseVariationsCorrectionFactor;
                    }
                    if (applyADCSaturationCorrectionToCurrentTile) {
                        final int adcI = Math.min(((y - y0) / blockHeight), adcPowerLoss.length - 1);
                        calFactor *= adcPowerLoss[adcI][adcJ];
                    }
                    sigma = dn * calFactor;
                    if (isComplex && outputImageInComplex) {
                        sigma = Math.sqrt(sigma) * phaseTerm;
                    }
                }
                if (outputImageScaleInDb) {
                    // convert calibration result to dB
                    if (sigma < underFlowFloat) {
                        sigma = -underFlowFloat;
                    } else {
                        sigma = 10.0 * Math.log10(sigma);
                    }
                }
                trgData.setElemDoubleAt(targetTile.getDataBufferIndex(x, y), sigma);
            }
        }
    } catch (Throwable e) {
        OperatorUtils.catchOperatorException("ERSCalibrator", e);
    }
}
Also used : ProductData(org.esa.snap.core.datamodel.ProductData) Rectangle(java.awt.Rectangle) Tile(org.esa.snap.core.gpf.Tile) Band(org.esa.snap.core.datamodel.Band) Unit(org.esa.snap.engine_utilities.datamodel.Unit) Point(java.awt.Point) OperatorException(org.esa.snap.core.gpf.OperatorException)

Example 100 with OperatorException

use of org.esa.snap.core.gpf.OperatorException in project s1tbx by senbox-org.

the class ERSCalibrator method removeFactorsForCurrentTile.

public void removeFactorsForCurrentTile(Band targetBand, Tile targetTile, String srcBandName) throws OperatorException {
    // For ground range product,
    // a) remove antenna pattern gain
    // b) remove range spreading loss corrections
    // c) remove replica pulse variation (ERS-2 only)
    // d) multiply calibration constant (done in applyCalibration)
    // e) apply ADC power loss correction
    // 
    // For slant range complex product,
    // c) remove replica pulse variation (ERS-2 only)
    // d) multiply calibration constant (done in applyCalibration)
    // e) apply ADC power loss correction
    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;
    final ProductData trgData = targetTile.getDataBuffer();
    // System.out.println("ERSCalibrator: tx0 = " + tx0 + ", ty0 = " + ty0 + ", tw = " + tw + ", th = " + th);
    final Band sourceBand1 = sourceProduct.getBand(srcBandName);
    final Tile sourceTile = getSourceTile(sourceBand1, targetTileRectangle);
    final ProductData srcData = sourceTile.getDataBuffer();
    final String[] srcBandNames = { targetBand.getName() };
    Band sourceBand2 = null;
    if (srcBandNames.length > 1) {
        sourceBand2 = sourceProduct.getBand(srcBandNames[1]);
    }
    final Unit.UnitType bandUnit = Unit.getUnitType(sourceBand1);
    if (applyADCSaturationCorrection && !adcHasBeenTestedFlag) {
        testADC(sourceBand1, sourceBand2, bandUnit);
    }
    boolean applyADCSaturationCorrectionToCurrentTile = false;
    if (applyADCSaturationCorrection && th >= blockHeight && tw >= blockWidth) {
        applyADCSaturationCorrectionToCurrentTile = true;
    }
    double[][] adcPowerLoss = null;
    if (applyADCSaturationCorrectionToCurrentTile) {
        adcPowerLoss = computeADCPowerLossValuesForCurrentTile(sourceBand1, sourceBand2, tx0, ty0, tw, th, bandUnit);
    }
    double sigma = 0.0;
    int adcJ = 0;
    for (int x = tx0; x < tx0 + tw; x++) {
        double antennaPatternByRangeSpreadingLoss = 0.0;
        if (!isComplex) {
            antennaPatternByRangeSpreadingLoss = antennaPatternGain[x] / rangeSpreadingLoss[x];
        }
        if (applyADCSaturationCorrectionToCurrentTile) {
            adcJ = Math.min(((x - tx0) / blockWidth), adcPowerLoss[0].length - 1);
        }
        for (int y = ty0; y < ty0 + th; y++) {
            final int srcIndex = sourceTile.getDataBufferIndex(x, y);
            final int tgtIndex = targetTile.getDataBufferIndex(x, y);
            if (bandUnit == Unit.UnitType.AMPLITUDE) {
                final double dn = srcData.getElemDoubleAt(srcIndex);
                sigma = dn * dn;
            } else if (bandUnit == Unit.UnitType.AMPLITUDE_DB) {
                sigma = FastMath.pow(10, srcData.getElemDoubleAt(srcIndex) / 5.0);
            } else if (bandUnit == Unit.UnitType.INTENSITY) {
                sigma = srcData.getElemDoubleAt(srcIndex);
            } else if (bandUnit == Unit.UnitType.INTENSITY_DB) {
                sigma = FastMath.pow(10, srcData.getElemDoubleAt(srcIndex) / 10.0);
            } else {
                throw new OperatorException("ERSCalibrator: Unknown band unit");
            }
            if (!isComplex) {
                // ground range
                sigma *= antennaPatternByRangeSpreadingLoss;
            }
            if (!isERS1Mission) {
                sigma /= replicaPulseVariationsCorrectionFactor;
            }
            if (applyADCSaturationCorrectionToCurrentTile) {
                final int adcI = Math.min(((y - ty0) / blockHeight), adcPowerLoss.length - 1);
                sigma *= adcPowerLoss[adcI][adcJ];
            }
            if (bandUnit == Unit.UnitType.AMPLITUDE) {
                trgData.setElemDoubleAt(tgtIndex, Math.sqrt(sigma));
            } else if (bandUnit == Unit.UnitType.AMPLITUDE_DB) {
                trgData.setElemDoubleAt(tgtIndex, 5.0 * Math.log10(sigma));
            } else if (bandUnit == Unit.UnitType.INTENSITY) {
                trgData.setElemDoubleAt(tgtIndex, sigma);
            } else if (bandUnit == Unit.UnitType.INTENSITY_DB) {
                trgData.setElemDoubleAt(tgtIndex, 10.0 * Math.log10(sigma));
            }
        }
    }
}
Also used : ProductData(org.esa.snap.core.datamodel.ProductData) Rectangle(java.awt.Rectangle) Tile(org.esa.snap.core.gpf.Tile) Band(org.esa.snap.core.datamodel.Band) Unit(org.esa.snap.engine_utilities.datamodel.Unit) Point(java.awt.Point) OperatorException(org.esa.snap.core.gpf.OperatorException)

Aggregations

OperatorException (org.esa.snap.core.gpf.OperatorException)268 Tile (org.esa.snap.core.gpf.Tile)56 Band (org.esa.snap.core.datamodel.Band)46 IOException (java.io.IOException)42 InputProductValidator (org.esa.snap.engine_utilities.gpf.InputProductValidator)35 TileIndex (org.esa.snap.engine_utilities.gpf.TileIndex)34 MetadataElement (org.esa.snap.core.datamodel.MetadataElement)27 TargetProduct (org.esa.snap.core.gpf.annotations.TargetProduct)25 ProductData (org.esa.snap.core.datamodel.ProductData)21 Unit (org.esa.snap.engine_utilities.datamodel.Unit)20 Sentinel1Utils (org.esa.s1tbx.commons.Sentinel1Utils)18 ArrayList (java.util.ArrayList)14 MetadataAttribute (org.esa.snap.core.datamodel.MetadataAttribute)14 Point (org.jlinda.core.Point)13 Product (org.esa.snap.core.datamodel.Product)12 File (java.io.File)10 VirtualBand (org.esa.snap.core.datamodel.VirtualBand)10 SourceProduct (org.esa.snap.core.gpf.annotations.SourceProduct)9 Resampling (org.esa.snap.core.dataop.resamp.Resampling)8 Rectangle (java.awt.Rectangle)7