Search in sources :

Example 1 with MapProjection

use of org.esa.snap.core.dataop.maptransf.MapProjection 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);
}
Also used : Array(ucar.ma2.Array) MapProjection(org.esa.snap.core.dataop.maptransf.MapProjection) MapInfo(org.esa.snap.core.dataop.maptransf.MapInfo) Index(ucar.ma2.Index)

Aggregations

MapInfo (org.esa.snap.core.dataop.maptransf.MapInfo)1 MapProjection (org.esa.snap.core.dataop.maptransf.MapProjection)1 Array (ucar.ma2.Array)1 Index (ucar.ma2.Index)1