Search in sources :

Example 1 with MetadataAttribute

use of org.esa.snap.core.datamodel.MetadataAttribute in project s1tbx by senbox-org.

the class Radarsat2ProductReader method readCalibrationLUT.

private void readCalibrationLUT(final String lutName, final MetadataElement root, final boolean flipLUT) throws IOException {
    InputStream is;
    if (dataDir.exists(dataDir.getRootFolder() + lutName + ".xml")) {
        is = dataDir.getInputStream(dataDir.getRootFolder() + lutsigma + ".xml");
    } else if (dataDir.exists(dataDir.getRootFolder() + lutName.toLowerCase() + ".xml")) {
        is = dataDir.getInputStream(dataDir.getRootFolder() + lutsigma.toLowerCase() + ".xml");
    } else {
        return;
    }
    final Document xmlDoc;
    try {
        xmlDoc = XMLSupport.LoadXML(is);
    } finally {
        is.close();
    }
    final Element rootElement = xmlDoc.getRootElement();
    final Element offsetElem = rootElement.getChild("offset");
    final double offset = Double.parseDouble(offsetElem.getValue());
    final Element gainsElem = rootElement.getChild("gains");
    final String gainsValue = gainsElem.getValue().trim().replace("  ", " ");
    final double[] gainsArray = toDoubleArray(gainsValue, " ");
    if (flipLUT) {
        double tmp;
        for (int i = 0; i < gainsArray.length / 2; i++) {
            tmp = gainsArray[i];
            gainsArray[i] = gainsArray[gainsArray.length - i - 1];
            gainsArray[gainsArray.length - i - 1] = tmp;
        }
    }
    final MetadataElement lut = new MetadataElement(lutName);
    root.addElement(lut);
    final MetadataAttribute offsetAttrib = new MetadataAttribute("offset", ProductData.TYPE_FLOAT64);
    offsetAttrib.getData().setElemDouble(offset);
    lut.addAttribute(offsetAttrib);
    final MetadataAttribute gainsAttrib = new MetadataAttribute("gains", ProductData.TYPE_FLOAT64, gainsArray.length);
    gainsAttrib.getData().setElems(gainsArray);
    lut.addAttribute(gainsAttrib);
}
Also used : InputStream(java.io.InputStream) MetadataElement(org.esa.snap.core.datamodel.MetadataElement) Element(org.jdom2.Element) MetadataElement(org.esa.snap.core.datamodel.MetadataElement) MetadataAttribute(org.esa.snap.core.datamodel.MetadataAttribute) Document(org.jdom2.Document)

Example 2 with MetadataAttribute

use of org.esa.snap.core.datamodel.MetadataAttribute in project s1tbx by senbox-org.

the class TerraSarXProductDirectory method addAbstractedMetadataHeader.

@Override
protected void addAbstractedMetadataHeader(final MetadataElement root) {
    final MetadataElement absRoot = AbstractMetadata.addAbstractedMetadataHeader(root);
    final MetadataElement origProdRoot = AbstractMetadata.addOriginalProductMetadata(root);
    final String defStr = AbstractMetadata.NO_METADATA_STRING;
    final int defInt = AbstractMetadata.NO_METADATA;
    final MetadataElement level1Elem = origProdRoot.getElementAt(0);
    final MetadataElement generalHeader = level1Elem.getElement("generalHeader");
    final MetadataElement productInfo = level1Elem.getElement("productInfo");
    final MetadataElement productSpecific = level1Elem.getElement("productSpecific");
    final MetadataElement missionInfo = productInfo.getElement("missionInfo");
    final MetadataElement productVariantInfo = productInfo.getElement("productVariantInfo");
    final MetadataElement imageDataInfo = productInfo.getElement("imageDataInfo");
    final MetadataElement sceneInfo = productInfo.getElement("sceneInfo");
    final MetadataElement processing = level1Elem.getElement("processing");
    final MetadataElement instrument = level1Elem.getElement("instrument");
    final MetadataElement platform = level1Elem.getElement("platform");
    final MetadataElement complexImageInfo = productSpecific.getElement("complexImageInfo");
    final MetadataElement geocodedImageInfo = productSpecific.getElement("geocodedImageInfo");
    MetadataAttribute attrib = generalHeader.getAttribute("fileName");
    if (attrib != null)
        productName = attrib.getData().getElemString().replace("_____", "_").replace("__", "_");
    if (productName.endsWith(".xml"))
        productName = productName.substring(0, productName.length() - 4);
    // mph
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.PRODUCT, productName);
    productType = productVariantInfo.getAttributeString("productType", defStr).replace("_____", "_").replace("__", "_");
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.PRODUCT_TYPE, productType);
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.SPH_DESCRIPTOR, generalHeader.getAttributeString("itemName", defStr));
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.MISSION, getMission());
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.PROC_TIME, ReaderUtils.getTime(generalHeader, "generationTime", standardDateFormat));
    MetadataElement elem = generalHeader.getElement("generationSystem");
    if (elem != null) {
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.ProcessingSystemIdentifier, elem.getAttributeString("generationSystem", defStr));
    }
    if (missionInfo != null) {
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.CYCLE, missionInfo.getAttributeInt("orbitCycle", defInt));
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.REL_ORBIT, missionInfo.getAttributeInt("relOrbit", defInt));
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.ABS_ORBIT, missionInfo.getAttributeInt("absOrbit", defInt));
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.PASS, missionInfo.getAttributeString("orbitDirection", defStr));
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.SAMPLE_TYPE, imageDataInfo.getAttributeString("imageDataType", defStr));
    }
    final MetadataElement acquisitionInfo = productInfo.getElement("acquisitionInfo");
    final String imagingMode = getAcquisitionMode(acquisitionInfo.getAttributeString("imagingMode", defStr));
    if (acquisitionInfo != null) {
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.ACQUISITION_MODE, imagingMode);
        final String lookDirection = acquisitionInfo.getAttributeString("lookDirection", defStr);
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.antenna_pointing, lookDirection.toLowerCase());
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.BEAMS, acquisitionInfo.getAttributeString("elevationBeamConfiguration", defStr));
        productDescription = productType + ' ' + imagingMode;
        if (missionInfo == null) {
            AbstractMetadata.setAttribute(absRoot, AbstractMetadata.PASS, acquisitionInfo.getAttributeString("orbitDirection", defStr));
        }
    }
    final MetadataElement polarisationList = acquisitionInfo.getElement("polarisationList");
    final MetadataAttribute[] polList = polarisationList.getAttributes();
    for (int i = 0; i < polList.length; ++i) {
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.polarTags[i], polList[i].getData().getElemString());
    }
    final MetadataElement imageRaster = imageDataInfo.getElement("imageRaster");
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.azimuth_looks, imageRaster.getAttributeDouble("azimuthLooks", defInt));
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.range_looks, imageRaster.getAttributeDouble("rangeLooks", defInt));
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.num_samples_per_line, imageRaster.getAttributeInt("numberOfColumns", defInt));
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.num_output_lines, imageRaster.getAttributeInt("numberOfRows", defInt));
    if (sceneInfo != null) {
        setStartStopTime(absRoot, sceneInfo, imageRaster.getAttributeInt("numberOfRows", defInt));
        getCornerCoords(sceneInfo, geocodedImageInfo);
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.avg_scene_height, sceneInfo.getAttributeDouble("sceneAverageHeight", defInt));
    } else if (acquisitionInfo != null) {
        setStartStopTime(absRoot, acquisitionInfo, imageRaster.getAttributeInt("numberOfRows", defInt));
    }
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.first_near_lat, latCorners[0]);
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.first_near_long, lonCorners[0]);
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.first_far_lat, latCorners[1]);
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.first_far_long, lonCorners[1]);
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.last_near_lat, latCorners[2]);
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.last_near_long, lonCorners[2]);
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.last_far_lat, latCorners[3]);
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.last_far_long, lonCorners[3]);
    // See Andrea's email dated Sept. 30, 2010
    final String sampleType = absRoot.getAttributeString(AbstractMetadata.SAMPLE_TYPE);
    if (sampleType.contains("COMPLEX") && complexImageInfo != null) {
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.azimuth_spacing, complexImageInfo.getAttributeDouble("projectedSpacingAzimuth", defInt));
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.range_spacing, complexImageInfo.getElement("projectedSpacingRange").getAttributeDouble("slantRange", defInt));
    } else {
        final MetadataElement rowSpacing = imageDataInfo.getElement("imageRaster").getElement("rowSpacing");
        final MetadataElement colSpacing = imageDataInfo.getElement("imageRaster").getElement("columnSpacing");
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.azimuth_spacing, rowSpacing.getAttributeDouble("rowSpacing", defInt));
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.range_spacing, colSpacing.getAttributeDouble("columnSpacing", defInt));
    }
    if (instrument != null) {
        final MetadataElement settings = instrument.getElement("settings");
        final MetadataElement settingRecord = settings.getElement("settingRecord");
        final MetadataElement PRF = settingRecord.getElement("PRF");
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.pulse_repetition_frequency, PRF.getAttributeDouble("PRF", defInt));
        final MetadataElement RSF = settings.getElement("RSF");
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.range_sampling_rate, RSF.getAttributeDouble("RSF", defInt) / Constants.oneMillion);
        final MetadataElement radarParameters = instrument.getElement("radarParameters");
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.radar_frequency, radarParameters.getAttributeDouble("centerFrequency", defInt) / Constants.oneMillion);
    }
    int srgr = 1;
    if (productVariantInfo.getAttributeString("projection", " ").equalsIgnoreCase("SLANTRANGE"))
        srgr = 0;
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.srgr_flag, srgr);
    final String mapProjection = productVariantInfo.getAttributeString("mapProjection", " ").trim();
    if (!mapProjection.isEmpty()) {
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.map_projection, mapProjection);
    }
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.abs_calibration_flag, 0);
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.coregistered_stack, 0);
    final MetadataElement processingFlags = processing.getElement("processingFlags");
    if (processingFlags != null) {
        setFlag(processingFlags, "rangeSpreadingLossCorrectedFlag", "true", absRoot, AbstractMetadata.range_spread_comp_flag);
        setFlag(processingFlags, "elevationPatternCorrectedFlag", "true", absRoot, AbstractMetadata.ant_elev_corr_flag);
    }
    // add Range and Azimuth bandwidth
    final MetadataElement processingParameter = processing.getElement("processingParameter");
    if (processingParameter != null) {
        // Hz
        final double rangeBW = processingParameter.getAttributeDouble("totalProcessedRangeBandwidth");
        // Hz
        final double azimuthBW = processingParameter.getAttributeDouble("totalProcessedAzimuthBandwidth");
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.range_bandwidth, rangeBW / Constants.oneMillion);
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.azimuth_bandwidth, azimuthBW);
    }
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.slant_range_to_first_pixel, (Math.min(slantRangeCorners[0], slantRangeCorners[2]) / Constants.oneBillion) * Constants.halfLightSpeed);
    // Note: Here we use the minimum of the slant range times of two corners because the original way cause
    // problem for stripmap product when the two slant range times are different.
    final MetadataElement calibration = level1Elem.getElement("calibration");
    if (calibration != null) {
        final MetadataElement calibrationConstant = calibration.getElement("calibrationConstant");
        AbstractMetadata.setAttribute(absRoot, AbstractMetadata.calibration_factor, calibrationConstant.getAttributeDouble("calFactor", defInt));
    }
    if (platform != null) {
        final MetadataElement orbit = platform.getElement("orbit");
        addOrbitStateVectors(absRoot, orbit);
        addSRGRCoefficients(absRoot, productSpecific, productInfo);
    }
    final MetadataElement doppler = processing.getElement("doppler");
    if (doppler != null) {
        final MetadataElement dopplerCentroid = doppler.getElement("dopplerCentroid");
        addDopplerCentroidCoefficients(absRoot, dopplerCentroid);
    }
    // Add metadata for Spotlight
    if (imagingMode.equalsIgnoreCase("spotlight")) {
        final MetadataElement dopplerSpotlightElem = new MetadataElement("dopplerSpotlight");
        absRoot.addElement(dopplerSpotlightElem);
        addDopplerRateAndCentroidSpotlight(dopplerSpotlightElem, origProdRoot);
        addAzimuthTimeZpSpotlight(dopplerSpotlightElem);
    }
    // handle ATI products by copying abs metadata to slv metadata
    final String antennaReceiveConfiguration = acquisitionInfo.getAttributeString("antennaReceiveConfiguration", "");
    if (antennaReceiveConfiguration.equals("DRA")) {
        final MetadataElement targetSlaveMetadataRoot = AbstractMetadata.getSlaveMetadata(root);
        // copy Abstracted Metadata
        for (File cosFile : cosarFileList) {
            final String fileName = cosFile.getName().toUpperCase();
            if (fileName.contains("_SRA_"))
                continue;
            AbstractMetadata.setAttribute(absRoot, AbstractMetadata.coregistered_stack, 1);
            final MetadataElement targetSlaveMetadata = new MetadataElement(fileName);
            targetSlaveMetadataRoot.addElement(targetSlaveMetadata);
            ProductUtils.copyMetadata(absRoot, targetSlaveMetadata);
        }
    // modify abstracted metadata
    }
    AbstractMetadata.setAttribute(absRoot, AbstractMetadata.bistatic_correction_applied, 1);
}
Also used : MetadataElement(org.esa.snap.core.datamodel.MetadataElement) MetadataAttribute(org.esa.snap.core.datamodel.MetadataAttribute) ImageIOFile(org.esa.s1tbx.commons.io.ImageIOFile) File(java.io.File)

Example 3 with MetadataAttribute

use of org.esa.snap.core.datamodel.MetadataAttribute in project s1tbx by senbox-org.

the class Sentinel1Level0Directory method addGeoCodingForLevel0Products.

private void addGeoCodingForLevel0Products(final Product product) {
    final float[] latCorners = new float[4];
    final float[] lonCorners = new float[latCorners.length];
    final MetadataElement elem = getMetadataObject(AbstractMetadata.getOriginalProductMetadata(product), "measurementFrameSet");
    if (elem != null) {
        final MetadataElement footprint = elem.getElement("metadataWrap").getElement("xmlData").getElement("frameSet").getElement("frame").getElement("footprint");
        final MetadataAttribute coordinates = footprint.getAttribute("coordinates");
        final String coordinatesStr = coordinates.getData().getElemString();
        // System.out.println("Sentinel1Level0Directory.addGeoCodingForLevel0Products: " + coordinatesStr);
        final String[] latLonPairsStr = coordinatesStr.split(" ");
        final int numLatLonPairs = latLonPairsStr.length;
        final ArrayList<float[]> latLonList = new ArrayList<>();
        for (String s : latLonPairsStr) {
            // System.out.println("Sentinel1Level0Directory.addGeoCodingForLevel0Products: " + s);
            String[] latStrLonStr = s.split(",");
            if (latStrLonStr.length != 2) {
                System.out.println("Sentinel1Level0Directory.addGeoCodingForLevel0Products: ERROR in footprint coordinates");
                continue;
            }
            float[] latLon = new float[2];
            latLon[0] = Float.parseFloat(latStrLonStr[0]);
            latLon[1] = Float.parseFloat(latStrLonStr[1]);
            latLonList.add(latLon);
        }
        /*
                for (float[] latLon : latLonList) {
                    System.out.println("Sentinel1Level0Directory.addGeoCodingForLevel0Products: lat = " + latLon[0] + " lon = " + latLon[1]);
                }
                */
        // The footprint coordinates are counter clockwise with the last coordinates being a repeat of the first.
        // So we remove the last pair of lat/lon if it is the same as the first pair.
        final float[] latLonFirst = latLonList.get(0);
        final float[] latLonLast = latLonList.get(numLatLonPairs - 1);
        if (latLonFirst[0] == latLonLast[0] && latLonFirst[1] == latLonLast[1]) {
            latLonList.remove(numLatLonPairs - 1);
        }
        if (latLonList.size() != latCorners.length) {
            return;
        }
        /*
                for (float[] latLon : latLonList) {
                    System.out.println("Sentinel1Level0Directory.addGeoCodingForLevel0Products: (after removing duplicate) lat = " + latLon[0] + " lon = " + latLon[1]);
                }
                */
        for (int i = 0; i < latCorners.length; i++) {
            latCorners[i] = latLonList.get(i)[0];
            lonCorners[i] = latLonList.get(i)[1];
        }
        // The lat/lon from the product are counter clockwise
        // Swap the first two pairs of lat/lon
        float tmp = latCorners[0];
        latCorners[0] = latCorners[1];
        latCorners[1] = tmp;
        tmp = lonCorners[0];
        lonCorners[0] = lonCorners[1];
        lonCorners[1] = tmp;
    }
    ReaderUtils.addGeoCoding(product, latCorners, lonCorners);
}
Also used : ArrayList(java.util.ArrayList) MetadataElement(org.esa.snap.core.datamodel.MetadataElement) MetadataAttribute(org.esa.snap.core.datamodel.MetadataAttribute)

Example 4 with MetadataAttribute

use of org.esa.snap.core.datamodel.MetadataAttribute in project s1tbx by senbox-org.

the class BaseRecordTest method assertStringAttribute.

public static void assertStringAttribute(MetadataElement elem, String attibuteName, String expectedValue) {
    final MetadataAttribute attribute = elem.getAttribute(attibuteName);
    assertNotNull(attribute);
    assertEquals(ProductData.TYPESTRING_ASCII, attribute.getData().getTypeString());
    assertEquals(expectedValue, attribute.getData().getElemString());
}
Also used : MetadataAttribute(org.esa.snap.core.datamodel.MetadataAttribute)

Example 5 with MetadataAttribute

use of org.esa.snap.core.datamodel.MetadataAttribute in project s1tbx by senbox-org.

the class BaseRecordTest method assertIntAttribute.

public static void assertIntAttribute(MetadataElement elem, String attributeName, int expectedValue) {
    final MetadataAttribute attribute = elem.getAttribute(attributeName);
    assertNotNull(attribute);
    assertEquals(ProductData.TYPE_INT32, attribute.getDataType());
    assertEquals(1, attribute.getNumDataElems());
    assertEquals(expectedValue, attribute.getData().getElemInt());
}
Also used : MetadataAttribute(org.esa.snap.core.datamodel.MetadataAttribute)

Aggregations

MetadataAttribute (org.esa.snap.core.datamodel.MetadataAttribute)39 MetadataElement (org.esa.snap.core.datamodel.MetadataElement)33 OperatorException (org.esa.snap.core.gpf.OperatorException)14 ProductData (org.esa.snap.core.datamodel.ProductData)6 IOException (java.io.IOException)5 IllegalBinaryFormatException (org.esa.s1tbx.io.binary.IllegalBinaryFormatException)3 File (java.io.File)2 InputStream (java.io.InputStream)2 ImageIOFile (org.esa.s1tbx.commons.io.ImageIOFile)2 Document (org.jdom2.Document)2 Element (org.jdom2.Element)2 Dimension (java.awt.Dimension)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 Band (org.esa.snap.core.datamodel.Band)1 Product (org.esa.snap.core.datamodel.Product)1 TargetProduct (org.esa.snap.core.gpf.annotations.TargetProduct)1 Array (ucar.ma2.Array)1 InvalidRangeException (ucar.ma2.InvalidRangeException)1 Attribute (ucar.nc2.Attribute)1