use of org.jlinda.core.Window in project s1tbx by senbox-org.
the class InterferogramOp method estimateFlatEarthPolynomial.
/**
* Create a flat earth phase polynomial for a given burst in TOPSAR product.
*/
public static DoubleMatrix estimateFlatEarthPolynomial(final CplxContainer master, final CplxContainer slave, final int subSwathIndex, final int burstIndex, final Point[] mstSceneCentreXYZ, final int orbitDegree, final int srpPolynomialDegree, final int srpNumberPoints, final Sentinel1Utils.SubSwathInfo[] subSwath, final Sentinel1Utils su) throws Exception {
final double[][] masterOSV = getAdjacentOrbitStateVectors(master, mstSceneCentreXYZ[burstIndex]);
final double[][] slaveOSV = getAdjacentOrbitStateVectors(slave, mstSceneCentreXYZ[burstIndex]);
final Orbit masterOrbit = new Orbit(masterOSV, orbitDegree);
final Orbit slaveOrbit = new Orbit(slaveOSV, orbitDegree);
long minLine = 0;
long maxLine = subSwath[subSwathIndex - 1].linesPerBurst - 1;
long minPixel = 0;
long maxPixel = subSwath[subSwathIndex - 1].samplesPerBurst - 1;
int numberOfCoefficients = PolyUtils.numberOfCoefficients(srpPolynomialDegree);
int[][] position = MathUtils.distributePoints(srpNumberPoints, new Window(minLine, maxLine, minPixel, maxPixel));
// setup observation and design matrix
DoubleMatrix y = new DoubleMatrix(srpNumberPoints);
DoubleMatrix A = new DoubleMatrix(srpNumberPoints, numberOfCoefficients);
double masterMinPi4divLam = (-4 * Constants.PI * Constants.lightSpeed) / master.metaData.getRadarWavelength();
double slaveMinPi4divLam = (-4 * Constants.PI * Constants.lightSpeed) / slave.metaData.getRadarWavelength();
// Loop through vector or distributedPoints()
for (int i = 0; i < srpNumberPoints; ++i) {
double line = position[i][0];
double pixel = position[i][1];
// compute azimuth/range time for this pixel
final double mstRgTime = subSwath[subSwathIndex - 1].slrTimeToFirstPixel + pixel * su.rangeSpacing / Constants.lightSpeed;
final double mstAzTime = line2AzimuthTime(line, subSwathIndex, burstIndex, subSwath);
// compute xyz of this point : sourceMaster
Point xyzMaster = masterOrbit.lph2xyz(mstAzTime, mstRgTime, 0.0, mstSceneCentreXYZ[burstIndex]);
Point slaveTimeVector = slaveOrbit.xyz2t(xyzMaster, slave.metaData.getSceneCentreAzimuthTime());
final double slaveTimeRange = slaveTimeVector.x;
// observation vector
y.put(i, (masterMinPi4divLam * mstRgTime) - (slaveMinPi4divLam * slaveTimeRange));
// set up a system of equations
// ______Order unknowns: A00 A10 A01 A20 A11 A02 A30 A21 A12 A03 for degree=3______
double posL = PolyUtils.normalize2(line, minLine, maxLine);
double posP = PolyUtils.normalize2(pixel, minPixel, maxPixel);
int index = 0;
for (int j = 0; j <= srpPolynomialDegree; j++) {
for (int k = 0; k <= j; k++) {
A.put(i, index, (FastMath.pow(posL, (double) (j - k)) * FastMath.pow(posP, (double) k)));
index++;
}
}
}
// Fit polynomial through computed vector of phases
DoubleMatrix Atranspose = A.transpose();
DoubleMatrix N = Atranspose.mmul(A);
DoubleMatrix rhs = Atranspose.mmul(y);
return Solve.solve(N, rhs);
}
use of org.jlinda.core.Window in project s1tbx by senbox-org.
the class InterferogramOp method updateMstMetaData.
private void updateMstMetaData(final int burstIndex, final SLCImage mstMeta) {
final double burstFirstLineTimeMJD = subSwath[subSwathIndex - 1].burstFirstLineTime[burstIndex] / Constants.secondsInDay;
final double burstFirstLineTimeSecondsOfDay = (burstFirstLineTimeMJD - (int) burstFirstLineTimeMJD) * Constants.secondsInDay;
mstMeta.settAzi1(burstFirstLineTimeSecondsOfDay);
mstMeta.setCurrentWindow(new Window(0, subSwath[subSwathIndex - 1].linesPerBurst - 1, 0, subSwath[subSwathIndex - 1].samplesPerBurst - 1));
mstMeta.setOriginalWindow(new Window(0, subSwath[subSwathIndex - 1].linesPerBurst - 1, 0, subSwath[subSwathIndex - 1].samplesPerBurst - 1));
mstMeta.setApproxGeoCentreOriginal(getApproxGeoCentre(subSwathIndex, burstIndex));
}
use of org.jlinda.core.Window in project s1tbx by senbox-org.
the class InterferogramOp method computeTileStackForNormalProduct.
private void computeTileStackForNormalProduct(final Map<Band, Tile> targetTileMap, Rectangle targetRectangle, final ProgressMonitor pm) throws OperatorException {
try {
final BorderExtender border = BorderExtender.createInstance(BorderExtender.BORDER_ZERO);
final int y0 = targetRectangle.y;
final int yN = y0 + targetRectangle.height - 1;
final int x0 = targetRectangle.x;
final int xN = targetRectangle.x + targetRectangle.width - 1;
final Window tileWindow = new Window(y0, yN, x0, xN);
DemTile demTile = null;
if (subtractTopographicPhase) {
demTile = TopoPhase.getDEMTile(tileWindow, targetMap, dem, demNoDataValue, demSamplingLat, demSamplingLon, tileExtensionPercent);
if (demTile.getData().length < 3 || demTile.getData()[0].length < 3) {
throw new OperatorException("The resolution of the selected DEM is too low, " + "please select DEM with higher resolution.");
}
}
// parameters for coherence calculation
final int cohx0 = targetRectangle.x - (cohWinRg - 1) / 2;
final int cohy0 = targetRectangle.y - (cohWinAz - 1) / 2;
final int cohw = targetRectangle.width + cohWinRg - 1;
final int cohh = targetRectangle.height + cohWinAz - 1;
final Rectangle rect = new Rectangle(cohx0, cohy0, cohw, cohh);
final Window cohTileWindow = new Window(cohy0, cohy0 + cohh - 1, cohx0, cohx0 + cohw - 1);
DemTile cohDemTile = null;
if (subtractTopographicPhase) {
cohDemTile = TopoPhase.getDEMTile(cohTileWindow, targetMap, dem, demNoDataValue, demSamplingLat, demSamplingLon, tileExtensionPercent);
}
for (String ifgKey : targetMap.keySet()) {
final ProductContainer product = targetMap.get(ifgKey);
final Tile mstTileReal = getSourceTile(product.sourceMaster.realBand, targetRectangle, border);
final Tile mstTileImag = getSourceTile(product.sourceMaster.imagBand, targetRectangle, border);
final ComplexDoubleMatrix dataMaster = TileUtilsDoris.pullComplexDoubleMatrix(mstTileReal, mstTileImag);
final Tile slvTileReal = getSourceTile(product.sourceSlave.realBand, targetRectangle, border);
final Tile slvTileImag = getSourceTile(product.sourceSlave.imagBand, targetRectangle, border);
final ComplexDoubleMatrix dataSlave = TileUtilsDoris.pullComplexDoubleMatrix(slvTileReal, slvTileImag);
if (subtractFlatEarthPhase) {
final DoubleMatrix flatEarthPhase = computeFlatEarthPhase(x0, xN, dataMaster.columns, y0, yN, dataMaster.rows, 0, sourceImageWidth - 1, 0, sourceImageHeight - 1, product.sourceSlave.name);
final ComplexDoubleMatrix complexReferencePhase = new ComplexDoubleMatrix(MatrixFunctions.cos(flatEarthPhase), MatrixFunctions.sin(flatEarthPhase));
dataSlave.muli(complexReferencePhase);
if (OUTPUT_PHASE) {
saveFlatEarthPhase(x0, xN, y0, yN, flatEarthPhase, product, targetTileMap);
}
}
if (subtractTopographicPhase) {
final TopoPhase topoPhase = TopoPhase.computeTopoPhase(product, tileWindow, demTile, outputElevation, false);
final ComplexDoubleMatrix ComplexTopoPhase = new ComplexDoubleMatrix(MatrixFunctions.cos(new DoubleMatrix(topoPhase.demPhase)), MatrixFunctions.sin(new DoubleMatrix(topoPhase.demPhase)));
dataSlave.muli(ComplexTopoPhase);
if (OUTPUT_PHASE) {
saveTopoPhase(x0, xN, y0, yN, topoPhase.demPhase, product, targetTileMap);
}
if (outputElevation) {
saveElevation(x0, xN, y0, yN, topoPhase.elevation, product, targetTileMap);
}
if (outputLatLon) {
final TopoPhase topoPhase1 = TopoPhase.computeTopoPhase(product, tileWindow, demTile, false, true);
saveLatLon(x0, xN, y0, yN, topoPhase1.latitude, topoPhase1.longitude, product, targetTileMap);
}
}
dataMaster.muli(dataSlave.conji());
saveInterferogram(dataMaster, product, targetTileMap, targetRectangle);
// coherence calculation
if (includeCoherence) {
final Tile mstTileReal2 = getSourceTile(product.sourceMaster.realBand, rect, border);
final Tile mstTileImag2 = getSourceTile(product.sourceMaster.imagBand, rect, border);
final Tile slvTileReal2 = getSourceTile(product.sourceSlave.realBand, rect, border);
final Tile slvTileImag2 = getSourceTile(product.sourceSlave.imagBand, rect, border);
final ComplexDoubleMatrix dataMaster2 = TileUtilsDoris.pullComplexDoubleMatrix(mstTileReal2, mstTileImag2);
final ComplexDoubleMatrix dataSlave2 = TileUtilsDoris.pullComplexDoubleMatrix(slvTileReal2, slvTileImag2);
if (subtractFlatEarthPhase) {
final DoubleMatrix flatEarthPhase = computeFlatEarthPhase(cohx0, cohx0 + cohw - 1, cohw, cohy0, cohy0 + cohh - 1, cohh, 0, sourceImageWidth - 1, 0, sourceImageHeight - 1, product.sourceSlave.name);
final ComplexDoubleMatrix complexReferencePhase = new ComplexDoubleMatrix(MatrixFunctions.cos(flatEarthPhase), MatrixFunctions.sin(flatEarthPhase));
dataSlave2.muli(complexReferencePhase);
}
if (subtractTopographicPhase) {
final TopoPhase topoPhase = TopoPhase.computeTopoPhase(product, cohTileWindow, cohDemTile, false);
final ComplexDoubleMatrix ComplexTopoPhase = new ComplexDoubleMatrix(MatrixFunctions.cos(new DoubleMatrix(topoPhase.demPhase)), MatrixFunctions.sin(new DoubleMatrix(topoPhase.demPhase)));
dataSlave2.muli(ComplexTopoPhase);
}
for (int i = 0; i < dataMaster2.length; i++) {
double tmp = norm(dataMaster2.get(i));
dataMaster2.put(i, dataMaster2.get(i).mul(dataSlave2.get(i).conj()));
dataSlave2.put(i, new ComplexDouble(norm(dataSlave2.get(i)), tmp));
}
DoubleMatrix cohMatrix = SarUtils.coherence2(dataMaster2, dataSlave2, cohWinAz, cohWinRg);
saveCoherence(cohMatrix, product, targetTileMap, targetRectangle);
}
}
} catch (Throwable e) {
OperatorUtils.catchOperatorException(getId(), e);
} finally {
pm.done();
}
}
use of org.jlinda.core.Window in project s1tbx by senbox-org.
the class DInSAROp method computeTileStack.
@Override
public void computeTileStack(Map<Band, Tile> targetTileMap, Rectangle targetRectangle, ProgressMonitor pm) throws OperatorException {
try {
int y0 = targetRectangle.y;
int yN = y0 + targetRectangle.height - 1;
int x0 = targetRectangle.x;
int xN = targetRectangle.x + targetRectangle.width - 1;
final Window tileWindow = new Window(y0, yN, x0, xN);
Band targetBand_I;
Band targetBand_Q;
ComplexDoubleMatrix complexDefoPair = null;
DoubleMatrix doubleTopoPair = null;
ProductContainer product = null;
for (String ifgKey : targetMap.keySet()) {
product = targetMap.get(ifgKey);
// / check out results from source ///
Tile tileReal = getSourceTile(product.sourceMaster.realBand, targetRectangle);
Tile tileImag = getSourceTile(product.sourceMaster.imagBand, targetRectangle);
complexDefoPair = TileUtilsDoris.pullComplexDoubleMatrix(tileReal, tileImag);
}
// always pull from topoProduct
for (Band band : topoProduct.getBands()) {
if (band.getName().contains("unw") || band.getUnit().contains(Unit.ABS_PHASE)) {
// / check out results from source ///
Tile tileReal = getSourceTile(band, targetRectangle);
doubleTopoPair = TileUtilsDoris.pullDoubleMatrix(tileReal);
}
}
dinsar.applyDInSAR(tileWindow, complexDefoPair, doubleTopoPair);
// / commit complexDefoPair back to target ///
targetBand_I = targetProduct.getBand(product.targetBandName_I);
Tile tileOutReal = targetTileMap.get(targetBand_I);
TileUtilsDoris.pushDoubleMatrix(complexDefoPair.real(), tileOutReal, targetRectangle);
targetBand_Q = targetProduct.getBand(product.targetBandName_Q);
Tile tileOutImag = targetTileMap.get(targetBand_Q);
TileUtilsDoris.pushDoubleMatrix(complexDefoPair.imag(), tileOutImag, targetRectangle);
} catch (Throwable e) {
OperatorUtils.catchOperatorException(getId(), e);
}
}
use of org.jlinda.core.Window in project s1tbx by senbox-org.
the class SnaphuWriter method createSnaphuConfFile.
private void createSnaphuConfFile() throws IOException {
final Product sourceProduct = getSourceProduct();
// prepare snaphu config file
final MetadataElement masterRoot = AbstractMetadata.getAbstractedMetadata(sourceProduct);
final MetadataElement[] slaveRootS = sourceProduct.getMetadataRoot().getElement(AbstractMetadata.SLAVE_METADATA_ROOT).getElements();
// final MetadataElement slaveRoot = slaveRootS[0];
for (MetadataElement slaveRoot : slaveRootS) {
final SLCImage masterMetadata = new SLCImage(masterRoot, sourceProduct);
final SLCImage slaveMetadata = new SLCImage(slaveRoot, sourceProduct);
Orbit masterOrbit = null;
Orbit slaveOrbit = null;
try {
masterOrbit = new Orbit(masterRoot, 3);
slaveOrbit = new Orbit(slaveRoot, 3);
} catch (Exception ignored) {
}
String cohName = null;
String phaseName = null;
if (slaveRootS.length > 1) {
for (Band band : sourceProduct.getBands()) {
if (band.getUnit().contains(Unit.COHERENCE)) {
String curCohName = band.getName();
String[] curSplit = curCohName.split("_");
if (curSplit.length >= 3) {
String date = curSplit[2];
if (slaveRoot.getName().contains(date)) {
cohName = curCohName;
}
} else {
cohName = band.getName();
}
}
if (band.getUnit().contains(Unit.PHASE)) {
String curPhaseName = band.getName();
String[] curSplit = curPhaseName.split("_");
if (curSplit.length >= 4) {
String date = curSplit[3];
if (slaveRoot.getName().contains(date)) {
phaseName = curPhaseName;
}
} else {
phaseName = band.getName();
}
}
}
} else {
for (Band band : sourceProduct.getBands()) {
if (band.getUnit().contains(Unit.COHERENCE)) {
cohName = band.getName();
}
if (band.getUnit().contains(Unit.PHASE)) {
phaseName = band.getName();
}
}
}
if (phaseName == null) {
for (Band band : sourceProduct.getBands()) {
if (band.getUnit().contains(Unit.PHASE)) {
phaseName = band.getName();
}
}
}
if (cohName == null) {
for (Band band : sourceProduct.getBands()) {
if (band.getUnit().contains(Unit.COHERENCE)) {
cohName = band.getName();
}
}
}
SnaphuParameters parameters = new SnaphuParameters();
String temp;
temp = masterRoot.getAttributeString("snaphu_cost_mode", "DEFO");
parameters.setUnwrapMode(temp);
temp = masterRoot.getAttributeString("snaphu_init_mode", "MST");
parameters.setSnaphuInit(temp);
parameters.setnTileRow(masterRoot.getAttributeInt("snaphu_numberOfTileRows", 10));
parameters.setnTileCol(masterRoot.getAttributeInt("snaphu_numberOfTileCols", 10));
parameters.setNumProcessors(masterRoot.getAttributeInt("snaphu_numberOfProcessors", 4));
parameters.setRowOverlap(masterRoot.getAttributeInt("snaphu_rowOverlap", 200));
parameters.setColumnOverlap(masterRoot.getAttributeInt("snaphu_colOverlap", 200));
parameters.setTileCostThreshold(masterRoot.getAttributeInt("snaphu_tileCostThreshold", 500));
parameters.setLogFileName("snaphu.log");
parameters.setPhaseFileName(phaseName + SNAPHU_IMAGE_EXTENSION);
parameters.setCoherenceFileName(cohName + SNAPHU_IMAGE_EXTENSION);
parameters.setVerbosityFlag("true");
parameters.setOutFileName(UNWRAPPED_PREFIX + phaseName + SNAPHU_IMAGE_EXTENSION);
Window dataWindow = new Window(masterMetadata.getCurrentWindow());
int size = 0;
for (Band b : sourceProduct.getBands()) {
if (b.getName().toLowerCase().contains("phase")) {
size = b.getRasterWidth();
}
}
// / initiate snaphuconfig
try {
snaphuConfigFile = new SnaphuConfigFile(masterMetadata, slaveMetadata, masterOrbit, slaveOrbit, dataWindow, parameters, size);
if (slaveRootS.length > 1) {
snaphuConfigFile.buildConfFile(phaseName);
} else {
snaphuConfigFile.buildConfFile("");
}
} catch (Exception e) {
e.printStackTrace();
}
// write snaphu.conf file to the target directory
try {
if (slaveRootS.length > 1) {
BufferedWriter out = new BufferedWriter(new FileWriter(_outputDir + "/" + phaseName + SNAPHU_CONFIG_FILE));
out.write(snaphuConfigFile.getConfigFileBuffer().toString());
out.close();
// Write out snaphu.conf file without phase name appended to avoid breaking legacy workflows
// Clear reference
snaphuConfigFile = null;
snaphuConfigFile = new SnaphuConfigFile(masterMetadata, slaveMetadata, masterOrbit, slaveOrbit, dataWindow, parameters, size);
snaphuConfigFile.buildConfFile("");
BufferedWriter out2 = new BufferedWriter(new FileWriter(_outputDir + "/" + SNAPHU_CONFIG_FILE));
out2.write(snaphuConfigFile.getConfigFileBuffer().toString());
out2.close();
} else {
BufferedWriter out = new BufferedWriter(new FileWriter(_outputDir + "/" + SNAPHU_CONFIG_FILE));
out.write(snaphuConfigFile.getConfigFileBuffer().toString());
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Aggregations