Search in sources :

Example 1 with Pressure

use of ome.units.quantity.Pressure in project bioformats by openmicroscopy.

the class ZeissCZIReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    parser = XMLTools.createBuilder();
    // switch to the master file if this is part of a multi-file dataset
    int lastDot = id.lastIndexOf(".");
    String base = lastDot < 0 ? id : id.substring(0, lastDot);
    if (base.endsWith(")") && isGroupFiles()) {
        LOGGER.info("Checking for master file");
        int lastFileSeparator = base.lastIndexOf(File.separator);
        int end = base.lastIndexOf(" (");
        if (end < 0 || end < lastFileSeparator) {
            end = base.lastIndexOf("(");
        }
        if (end > 0 && end > lastFileSeparator) {
            base = base.substring(0, end) + ".czi";
            if (new Location(base).exists()) {
                LOGGER.info("Initializing master file {}", base);
                initFile(base);
                return;
            }
        }
    }
    CoreMetadata ms0 = core.get(0);
    ms0.littleEndian = true;
    pixels = new HashMap<Integer, String>();
    segments = new ArrayList<Segment>();
    planes = new ArrayList<SubBlock>();
    readSegments(id);
    // check if we have the master file in a multi-file dataset
    // file names are not stored in the files; we have to rely on a
    // specific naming convention:
    // 
    // master_file.czi
    // master_file (1).czi
    // master_file (2).czi
    // ...
    // 
    // the number of files is also not stored, so we have to manually check
    // for all files with a matching name
    Location file = new Location(id).getAbsoluteFile();
    base = file.getName();
    lastDot = base.lastIndexOf(".");
    if (lastDot >= 0) {
        base = base.substring(0, lastDot);
    }
    Location parent = file.getParentFile();
    String[] list = parent.list(true);
    for (String f : list) {
        if (f.startsWith(base + "(") || f.startsWith(base + " (")) {
            String part = f.substring(f.lastIndexOf("(") + 1, f.lastIndexOf(")"));
            try {
                pixels.put(Integer.parseInt(part), new Location(parent, f).getAbsolutePath());
            } catch (NumberFormatException e) {
                LOGGER.debug("{} not included in multi-file dataset", f);
            }
        }
    }
    Integer[] keys = pixels.keySet().toArray(new Integer[pixels.size()]);
    Arrays.sort(keys);
    for (Integer key : keys) {
        readSegments(pixels.get(key));
    }
    calculateDimensions();
    if (planes.size() == 0) {
        throw new FormatException("Pixel data could not be found; this file may be corrupted");
    }
    int firstX = planes.get(0).x;
    int firstY = planes.get(0).y;
    if (getSizeC() == 0) {
        ms0.sizeC = 1;
    }
    if (getSizeZ() == 0) {
        ms0.sizeZ = 1;
    }
    if (getSizeT() == 0) {
        ms0.sizeT = 1;
    }
    if (getImageCount() == 0) {
        ms0.imageCount = ms0.sizeZ * ms0.sizeT;
    }
    int originalC = getSizeC();
    convertPixelType(planes.get(0).directoryEntry.pixelType);
    // remove any invalid SubBlocks
    int bpp = FormatTools.getBytesPerPixel(getPixelType());
    if (isRGB()) {
        bpp *= (getSizeC() / originalC);
    }
    int fullResBlockCount = planes.size();
    for (int i = 0; i < planes.size(); i++) {
        long planeSize = (long) planes.get(i).x * planes.get(i).y * bpp;
        int compression = planes.get(i).directoryEntry.compression;
        if (compression == UNCOMPRESSED || compression == JPEGXR) {
            long size = planes.get(i).dataSize;
            if (size < planeSize || planeSize >= Integer.MAX_VALUE || size < 0) {
                // check for reduced resolution in the pyramid
                DimensionEntry[] entries = planes.get(i).directoryEntry.dimensionEntries;
                int pyramidType = planes.get(i).directoryEntry.pyramidType;
                if ((pyramidType == 1 || pyramidType == 2 || compression == JPEGXR) && (compression == JPEGXR || size == entries[0].storedSize * entries[1].storedSize * bpp)) {
                    int scale = planes.get(i).x / entries[0].storedSize;
                    if (scale == 1 || (((scale % 2) == 0 || (scale % 3) == 0) && allowAutostitching())) {
                        if (scale > 1 && scaleFactor == 0) {
                            scaleFactor = scale % 2 == 0 ? 2 : 3;
                        }
                        planes.get(i).coreIndex = 0;
                        while (scale > 1) {
                            scale /= scaleFactor;
                            planes.get(i).coreIndex++;
                        }
                        if (planes.get(i).coreIndex > maxResolution) {
                            maxResolution = planes.get(i).coreIndex;
                        }
                    } else {
                        LOGGER.trace("removing block #{}; calculated size = {}, recorded size = {}, scale = {}", i, planeSize, size, scale);
                        planes.remove(i);
                        i--;
                    }
                } else {
                    LOGGER.trace("removing block #{}; calculated size = {}, recorded size = {}", i, planeSize, size);
                    planes.remove(i);
                    i--;
                }
                fullResBlockCount--;
            } else {
                scanDim = (int) (size / planeSize);
            }
        } else {
            byte[] pixels = planes.get(i).readPixelData();
            if (pixels.length < planeSize || planeSize >= Integer.MAX_VALUE) {
                LOGGER.trace("removing block #{}; calculated size = {}, decoded size = {}", i, planeSize, pixels.length);
                planes.remove(i);
                i--;
            } else {
                scanDim = (int) (pixels.length / planeSize);
            }
        }
    }
    if (getSizeZ() == 0) {
        ms0.sizeZ = 1;
    }
    if (getSizeC() == 0) {
        ms0.sizeC = 1;
    }
    if (getSizeT() == 0) {
        ms0.sizeT = 1;
    }
    // set modulo annotations
    // rotations -> modulo Z
    // illuminations -> modulo C
    // phases -> modulo T
    LOGGER.trace("rotations = {}", rotations);
    LOGGER.trace("illuminations = {}", illuminations);
    LOGGER.trace("phases = {}", phases);
    LOGGER.trace("positions = {}", positions);
    LOGGER.trace("acquisitions = {}", acquisitions);
    LOGGER.trace("mosaics = {}", mosaics);
    LOGGER.trace("angles = {}", angles);
    ms0.moduloZ.step = ms0.sizeZ;
    ms0.moduloZ.end = ms0.sizeZ * (rotations - 1);
    ms0.moduloZ.type = FormatTools.ROTATION;
    ms0.sizeZ *= rotations;
    ms0.moduloC.step = ms0.sizeC;
    ms0.moduloC.end = ms0.sizeC * (illuminations - 1);
    ms0.moduloC.type = FormatTools.ILLUMINATION;
    ms0.moduloC.parentType = FormatTools.CHANNEL;
    ms0.sizeC *= illuminations;
    ms0.moduloT.step = ms0.sizeT;
    ms0.moduloT.end = ms0.sizeT * (phases - 1);
    ms0.moduloT.type = FormatTools.PHASE;
    ms0.sizeT *= phases;
    // finish populating the core metadata
    int seriesCount = positions * acquisitions * angles;
    int originalMosaicCount = mosaics;
    if (maxResolution == 0) {
        seriesCount *= mosaics;
    } else {
        prestitched = true;
    }
    ms0.imageCount = getSizeZ() * (isRGB() ? getSizeC() / 3 : getSizeC()) * getSizeT();
    LOGGER.trace("Size Z = {}", getSizeZ());
    LOGGER.trace("Size C = {}", getSizeC());
    LOGGER.trace("Size T = {}", getSizeT());
    LOGGER.trace("is RGB = {}", isRGB());
    LOGGER.trace("calculated image count = {}", ms0.imageCount);
    LOGGER.trace("number of available planes = {}", planes.size());
    LOGGER.trace("prestitched = {}", prestitched);
    LOGGER.trace("scanDim = {}", scanDim);
    int calculatedSeries = fullResBlockCount / getImageCount();
    if (((mosaics == seriesCount) || (positions == seriesCount)) && ((seriesCount == calculatedSeries) || (maxResolution > 0 && seriesCount * mosaics == calculatedSeries)) && prestitched != null && prestitched) {
        boolean equalTiles = true;
        for (SubBlock plane : planes) {
            if (plane.x != planes.get(0).x || plane.y != planes.get(0).y) {
                equalTiles = false;
                break;
            }
        }
        if ((getSizeX() > planes.get(0).x || (getSizeX() == planes.get(0).x && calculatedSeries == seriesCount * mosaics * positions)) && !equalTiles && allowAutostitching()) {
            // image was fused; treat the mosaics as a single image
            seriesCount = 1;
            positions = 1;
            acquisitions = 1;
            mosaics = 1;
            angles = 1;
        } else {
            int newX = planes.get(planes.size() - 1).x;
            int newY = planes.get(planes.size() - 1).y;
            if (allowAutostitching() && (ms0.sizeX < newX || ms0.sizeY < newY)) {
                prestitched = true;
                mosaics = 1;
            } else {
                prestitched = maxResolution > 0;
            }
            ms0.sizeX = newX;
            ms0.sizeY = newY;
        }
    } else if (!allowAutostitching() && calculatedSeries > seriesCount) {
        ms0.sizeX = firstX;
        ms0.sizeY = firstY;
        prestitched = true;
    }
    if (ms0.imageCount * seriesCount > planes.size() * scanDim && planes.size() > 0) {
        if (planes.size() != ms0.imageCount && planes.size() != ms0.sizeT && (planes.size() % (seriesCount * getSizeZ())) == 0) {
            if (!isGroupFiles() && planes.size() == (ms0.imageCount * seriesCount) / positions) {
                seriesCount /= positions;
                positions = 1;
            }
        } else if (planes.size() == ms0.sizeT || planes.size() == ms0.imageCount || (!isGroupFiles() && positions > 1)) {
            positions = 1;
            acquisitions = 1;
            mosaics = 1;
            angles = 1;
            seriesCount = 1;
        } else if (seriesCount > mosaics && mosaics > 1 && prestitched != null && prestitched) {
            seriesCount /= mosaics;
            mosaics = 1;
        }
    }
    ms0.dimensionOrder = "XYCZT";
    ArrayList<Integer> pixelTypes = new ArrayList<Integer>();
    pixelTypes.add(planes.get(0).directoryEntry.pixelType);
    if (maxResolution == 0) {
        for (SubBlock plane : planes) {
            if (!pixelTypes.contains(plane.directoryEntry.pixelType)) {
                pixelTypes.add(plane.directoryEntry.pixelType);
            }
            plane.pixelTypeIndex = pixelTypes.indexOf(plane.directoryEntry.pixelType);
        }
        if (seriesCount * pixelTypes.size() > 1) {
            core.clear();
            for (int j = 0; j < pixelTypes.size(); j++) {
                for (int i = 0; i < seriesCount; i++) {
                    CoreMetadata add = new CoreMetadata(ms0);
                    if (pixelTypes.size() > 1) {
                        int newC = originalC / pixelTypes.size();
                        add.sizeC = newC;
                        add.imageCount = add.sizeZ * add.sizeT;
                        add.rgb = false;
                        convertPixelType(add, pixelTypes.get(j));
                    }
                    core.add(add);
                }
            }
        }
    }
    if (seriesCount > 1 || maxResolution > 0) {
        core.clear();
        for (int i = 0; i < seriesCount; i++) {
            CoreMetadata add = new CoreMetadata(ms0);
            add.resolutionCount = maxResolution + 1;
            core.add(add);
            for (int r = 0; r < maxResolution; r++) {
                CoreMetadata resolution = new CoreMetadata(add);
                resolution.resolutionCount = 1;
                core.add(resolution);
            }
        }
    }
    assignPlaneIndices();
    if (maxResolution > 0) {
        tileWidth = new int[core.size()];
        tileHeight = new int[core.size()];
        for (int s = 0; s < core.size(); ) {
            if (s > 0) {
                core.get(s).sizeX = 0;
                core.get(s).sizeY = 0;
                calculateDimensions(s, true);
            }
            if (originalMosaicCount > 1) {
                // calculate total stitched size if the image was not fused
                int minRow = Integer.MAX_VALUE;
                int maxRow = Integer.MIN_VALUE;
                int minCol = Integer.MAX_VALUE;
                int maxCol = Integer.MIN_VALUE;
                int x = 0, y = 0;
                int lastX = 0, lastY = 0;
                for (SubBlock plane : planes) {
                    if (plane.coreIndex != s) {
                        continue;
                    }
                    if (x == 0 && y == 0) {
                        x = plane.x;
                        y = plane.y;
                    }
                    if (plane.row < minRow) {
                        minRow = plane.row;
                    }
                    if (plane.row > maxRow) {
                        maxRow = plane.row;
                    }
                    if (plane.col < minCol) {
                        minCol = plane.col;
                    }
                    if (plane.col > maxCol) {
                        maxCol = plane.col;
                    }
                    if (plane.x > tileWidth[s]) {
                        tileWidth[s] = plane.x;
                    }
                    if (plane.y > tileHeight[s]) {
                        tileHeight[s] = plane.y;
                    }
                    if (plane.row == maxRow && plane.col == maxCol) {
                        lastX = plane.x;
                        lastY = plane.y;
                    }
                }
                // don't overwrite the dimensions if stitching already occurred
                if (core.get(s).sizeX == x && core.get(s).sizeY == y) {
                    core.get(s).sizeX = (lastX + maxCol) - minCol;
                    core.get(s).sizeY = (lastY + maxRow) - minRow;
                }
            }
            boolean keepMissingPyramid = false;
            for (int r = 0; r < core.get(s).resolutionCount; r++) {
                boolean hasValidPlane = false;
                for (SubBlock plane : planes) {
                    if (plane.coreIndex == s + r) {
                        hasValidPlane = true;
                        break;
                    }
                }
                if (!hasValidPlane && r > 0 && !keepMissingPyramid) {
                    core.remove(s + r);
                    core.get(s).resolutionCount--;
                    // adjust the core indexes of any subsequent planes
                    for (SubBlock plane : planes) {
                        if (plane.coreIndex > s + r) {
                            plane.coreIndex--;
                        }
                    }
                    r--;
                } else {
                    int div = (int) Math.pow(scaleFactor, r);
                    if (r == 0 && s > 0 && core.get(s).sizeX == 1) {
                        core.get(s).sizeX = core.get(s - maxResolution).sizeX;
                        core.get(s).sizeY = core.get(s - maxResolution).sizeY;
                    } else {
                        core.get(s + r).sizeX = core.get(s).sizeX / div;
                        core.get(s + r).sizeY = core.get(s).sizeY / div;
                    }
                    tileWidth[s + r] = tileWidth[s] / div;
                    tileHeight[s + r] = tileHeight[s] / div;
                }
                if (r == 0 && !hasValidPlane) {
                    keepMissingPyramid = true;
                }
            }
            s += core.get(s).resolutionCount;
        }
    }
    // check for PALM data; requires planes to split into separate series
    String firstXML = null;
    boolean canSkipXML = true;
    String currentPath = new Location(currentId).getAbsolutePath();
    boolean isPALM = false;
    if (planes.size() <= 2 && getImageCount() <= 2) {
        for (Segment segment : segments) {
            String path = new Location(segment.filename).getAbsolutePath();
            if (currentPath.equals(path) && segment instanceof Metadata) {
                segment.fillInData();
                String xml = ((Metadata) segment).xml;
                xml = XMLTools.sanitizeXML(xml);
                if (firstXML == null && canSkipXML) {
                    firstXML = xml;
                }
                if (canSkipXML && firstXML.equals(xml)) {
                    isPALM = checkPALM(xml);
                } else if (!firstXML.equals(xml)) {
                    canSkipXML = false;
                }
                ((Metadata) segment).clearXML();
            }
        }
    }
    if (isPALM) {
        LOGGER.debug("Detected PALM data");
        core.get(0).sizeC = 1;
        core.get(0).imageCount = core.get(0).sizeZ * core.get(0).sizeT;
        for (int i = 0; i < planes.size(); i++) {
            SubBlock p = planes.get(i);
            int storedX = p.directoryEntry.dimensionEntries[0].storedSize;
            int storedY = p.directoryEntry.dimensionEntries[1].storedSize;
            if (p.planeIndex >= getImageCount()) {
                if (core.size() == 1) {
                    CoreMetadata second = new CoreMetadata(core.get(0));
                    core.add(second);
                }
                p.coreIndex = 1;
                p.planeIndex -= (planes.size() / 2);
                core.get(1).sizeX = storedX;
                core.get(1).sizeY = storedY;
            } else {
                core.get(0).sizeX = storedX;
                core.get(0).sizeY = storedY;
            }
        }
        if (core.size() == 2) {
            // prevent misidentification of PALM data; each plane should be a different size
            if (core.get(0).sizeX == core.get(1).sizeX && core.get(0).sizeY == core.get(1).sizeY) {
                isPALM = false;
                core.remove(1);
                core.get(0).sizeC = 2;
                core.get(0).imageCount *= getSizeC();
                for (int i = 0; i < planes.size(); i++) {
                    SubBlock p = planes.get(i);
                    if (p.coreIndex == 1) {
                        p.coreIndex = 0;
                        p.planeIndex += (planes.size() / 2);
                    }
                }
            }
        }
    }
    // find and add attached label/overview images
    readAttachments();
    // populate the OME metadata
    store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, true);
    firstXML = null;
    canSkipXML = true;
    for (Segment segment : segments) {
        String path = new Location(segment.filename).getAbsolutePath();
        if (currentPath.equals(path) && segment instanceof Metadata) {
            segment.fillInData();
            String xml = ((Metadata) segment).xml;
            xml = XMLTools.sanitizeXML(xml);
            if (firstXML == null && canSkipXML) {
                firstXML = xml;
            }
            if (canSkipXML && firstXML.equals(xml)) {
                translateMetadata(xml);
            } else if (!firstXML.equals(xml)) {
                canSkipXML = false;
            }
            ((Metadata) segment).clearXML();
        } else if (segment instanceof Attachment) {
            AttachmentEntry entry = ((Attachment) segment).attachment;
            String name = entry.name.trim();
            if (name.equals("TimeStamps")) {
                segment.fillInData();
                RandomAccessInputStream s = new RandomAccessInputStream(((Attachment) segment).attachmentData);
                try {
                    s.order(isLittleEndian());
                    s.seek(8);
                    while (s.getFilePointer() + 8 <= s.length()) {
                        timestamps.add(s.readDouble());
                    }
                } finally {
                    s.close();
                }
            }
        }
        segment.close();
    }
    if (rotationLabels != null) {
        ms0.moduloZ.labels = rotationLabels;
        ms0.moduloZ.end = ms0.moduloZ.start;
    }
    if (illuminationLabels != null) {
        ms0.moduloC.labels = illuminationLabels;
        ms0.moduloC.end = ms0.moduloC.start;
    }
    if (phaseLabels != null) {
        ms0.moduloT.labels = phaseLabels;
        ms0.moduloT.end = ms0.moduloT.start;
    }
    for (int i = 0; i < planes.size(); i++) {
        SubBlock p = planes.get(i);
        Coordinate c = new Coordinate(p.coreIndex, p.planeIndex, getImageCount());
        ArrayList<Integer> indices = new ArrayList<Integer>();
        if (indexIntoPlanes.containsKey(c)) {
            indices = indexIntoPlanes.get(c);
        }
        indices.add(i);
        indexIntoPlanes.put(c, indices);
        // Add series metadata : populate position list
        int nameWidth = String.valueOf(getSeriesCount()).length();
        for (DimensionEntry dimension : p.directoryEntry.dimensionEntries) {
            if (dimension == null) {
                continue;
            }
            switch(dimension.dimension.charAt(0)) {
                case 'S':
                    setCoreIndex(p.coreIndex);
                    int seriesId = p.coreIndex + 1;
                    // add padding to make sure the original metadata table is organized properly in ImageJ
                    String sIndex = String.format("Positions|Series %0" + nameWidth + "d|", seriesId);
                    addSeriesMetaList(sIndex, dimension.start);
                    break;
            }
        }
        setCoreIndex(0);
    }
    if (channels.size() > 0 && channels.get(0).color != null && !isRGB()) {
        for (int i = 0; i < seriesCount; i++) {
            core.get(i).indexed = true;
        }
    }
    String experimenterID = MetadataTools.createLSID("Experimenter", 0);
    store.setExperimenterID(experimenterID, 0);
    store.setExperimenterEmail(userEmail, 0);
    store.setExperimenterFirstName(userFirstName, 0);
    store.setExperimenterInstitution(userInstitution, 0);
    store.setExperimenterLastName(userLastName, 0);
    store.setExperimenterMiddleName(userMiddleName, 0);
    store.setExperimenterUserName(userName, 0);
    String name = new Location(getCurrentFile()).getName();
    if (imageName != null && imageName.trim().length() > 0) {
        name = imageName;
    }
    store.setInstrumentID(MetadataTools.createLSID("Instrument", 0), 0);
    int indexLength = String.valueOf(getSeriesCount()).length();
    for (int i = 0; i < getSeriesCount(); i++) {
        store.setImageInstrumentRef(MetadataTools.createLSID("Instrument", 0), i);
        if (acquiredDate != null) {
            store.setImageAcquisitionDate(new Timestamp(acquiredDate), i);
        } else if (planes.get(0).timestamp != null) {
            long timestamp = (long) (planes.get(0).timestamp * 1000);
            String date = DateTools.convertDate(timestamp, DateTools.UNIX);
            store.setImageAcquisitionDate(new Timestamp(date), i);
        }
        if (experimenterID != null) {
            store.setImageExperimenterRef(experimenterID, i);
        }
        if (timeIncrement != null) {
            store.setPixelsTimeIncrement(timeIncrement, i);
        }
        String imageIndex = String.valueOf(i + 1);
        while (imageIndex.length() < indexLength) {
            imageIndex = "0" + imageIndex;
        }
        int extraIndex = i - (getSeriesCount() - extraImages.size());
        if (extraIndex < 0) {
            if (hasFlattenedResolutions()) {
                store.setImageName(name + " #" + imageIndex, i);
            } else if (positions == 1) {
                store.setImageName("", i);
            } else {
                store.setImageName("Scene #" + i, i);
            }
        } else if (extraIndex == 0) {
            store.setImageName("label image", i);
        } else if (extraIndex == 1) {
            store.setImageName("macro image", i);
        } else {
            store.setImageName("thumbnail image", i);
        }
        // label and macro images
        if (extraIndex >= 0) {
            continue;
        }
        if (description != null && description.length() > 0) {
            store.setImageDescription(description, i);
        }
        if (airPressure != null) {
            store.setImagingEnvironmentAirPressure(new Pressure(new Double(airPressure), UNITS.MILLIBAR), i);
        }
        if (co2Percent != null) {
            store.setImagingEnvironmentCO2Percent(PercentFraction.valueOf(co2Percent), i);
        }
        if (humidity != null) {
            store.setImagingEnvironmentHumidity(PercentFraction.valueOf(humidity), i);
        }
        if (temperature != null) {
            store.setImagingEnvironmentTemperature(new Temperature(new Double(temperature), UNITS.CELSIUS), i);
        }
        if (objectiveSettingsID != null) {
            store.setObjectiveSettingsID(objectiveSettingsID, i);
            if (correctionCollar != null) {
                store.setObjectiveSettingsCorrectionCollar(new Double(correctionCollar), i);
            }
            if (medium != null) {
                store.setObjectiveSettingsMedium(getMedium(medium), i);
            }
            if (refractiveIndex != null) {
                store.setObjectiveSettingsRefractiveIndex(new Double(refractiveIndex), i);
            }
        }
        Double startTime = null;
        if (acquiredDate != null) {
            Timestamp t = Timestamp.valueOf(acquiredDate);
            if (t != null)
                startTime = t.asInstant().getMillis() / 1000d;
        }
        for (int plane = 0; plane < getImageCount(); plane++) {
            Coordinate coordinate = new Coordinate(i, plane, getImageCount());
            ArrayList<Integer> index = indexIntoPlanes.get(coordinate);
            if (index == null) {
                continue;
            }
            SubBlock p = planes.get(index.get(0));
            if (startTime == null) {
                startTime = p.timestamp;
            }
            if (p.stageX != null) {
                store.setPlanePositionX(p.stageX, i, plane);
            } else if (positionsX != null && i < positionsX.length && positionsX[i] != null) {
                store.setPlanePositionX(positionsX[i], i, plane);
            } else {
                store.setPlanePositionX(new Length(p.col, UNITS.REFERENCEFRAME), i, plane);
            }
            if (p.stageY != null) {
                store.setPlanePositionY(p.stageY, i, plane);
            } else if (positionsY != null && i < positionsY.length && positionsY[i] != null) {
                store.setPlanePositionY(positionsY[i], i, plane);
            } else {
                store.setPlanePositionY(new Length(p.row, UNITS.REFERENCEFRAME), i, plane);
            }
            if (p.stageZ != null) {
                store.setPlanePositionZ(p.stageZ, i, plane);
            } else if (positionsZ != null && i < positionsZ.length) {
                int zIndex = getZCTCoords(plane)[0];
                if (positionsZ[i] != null) {
                    if (zStep != null) {
                        double value = positionsZ[i].value(zStep.unit()).doubleValue();
                        if (zStep != null) {
                            value += zIndex * zStep.value().doubleValue();
                        }
                        Length pos = new Length(value, zStep.unit());
                        store.setPlanePositionZ(pos, i, plane);
                    } else {
                        store.setPlanePositionZ(positionsZ[i], i, plane);
                    }
                }
            }
            if (p.timestamp != null) {
                store.setPlaneDeltaT(new Time(p.timestamp - startTime, UNITS.SECOND), i, plane);
            } else if (plane < timestamps.size() && timestamps.size() == getImageCount()) {
                // only use the plane index if there is one timestamp per plane
                if (timestamps.get(plane) != null) {
                    store.setPlaneDeltaT(new Time(timestamps.get(plane), UNITS.SECOND), i, plane);
                }
            } else if (getZCTCoords(plane)[2] < timestamps.size()) {
                // otherwise use the timepoint index, to prevent incorrect timestamping of channels
                int t = getZCTCoords(plane)[2];
                if (timestamps.get(t) != null) {
                    store.setPlaneDeltaT(new Time(timestamps.get(t), UNITS.S), i, plane);
                }
            }
            if (p.exposureTime != null) {
                store.setPlaneExposureTime(new Time(p.exposureTime, UNITS.SECOND), i, plane);
            } else {
                int channel = getZCTCoords(plane)[1];
                if (channel < channels.size() && channels.get(channel).exposure != null) {
                    store.setPlaneExposureTime(new Time(channels.get(channel).exposure, UNITS.SECOND), i, plane);
                }
            }
        }
        for (int c = 0; c < getEffectiveSizeC(); c++) {
            if (c < channels.size()) {
                if (isPALM && i < channels.size()) {
                    store.setChannelName(channels.get(i).name, i, c);
                } else {
                    store.setChannelName(channels.get(c).name, i, c);
                }
                store.setChannelFluor(channels.get(c).fluor, i, c);
                if (channels.get(c).filterSetRef != null) {
                    store.setChannelFilterSetRef(channels.get(c).filterSetRef, i, c);
                }
                String color = channels.get(c).color;
                if (color != null && !isRGB()) {
                    color = color.replaceAll("#", "");
                    if (color.length() > 6) {
                        color = color.substring(2, color.length());
                    }
                    try {
                        // shift by 8 to allow alpha in the final byte
                        store.setChannelColor(new Color((Integer.parseInt(color, 16) << 8) | 0xff), i, c);
                    } catch (NumberFormatException e) {
                        LOGGER.warn("", e);
                    }
                }
                String emWave = channels.get(c).emission;
                if (emWave != null) {
                    Double wave = new Double(emWave);
                    Length em = FormatTools.getEmissionWavelength(wave);
                    if (em != null) {
                        store.setChannelEmissionWavelength(em, i, c);
                    }
                }
                String exWave = channels.get(c).excitation;
                if (exWave != null) {
                    Double wave = new Double(exWave);
                    Length ex = FormatTools.getExcitationWavelength(wave);
                    if (ex != null) {
                        store.setChannelExcitationWavelength(ex, i, c);
                    }
                }
                if (channels.get(c).illumination != null) {
                    store.setChannelIlluminationType(channels.get(c).illumination, i, c);
                }
                if (channels.get(c).pinhole != null) {
                    store.setChannelPinholeSize(new Length(new Double(channels.get(c).pinhole), UNITS.MICROMETER), i, c);
                }
                if (channels.get(c).acquisitionMode != null) {
                    store.setChannelAcquisitionMode(channels.get(c).acquisitionMode, i, c);
                }
            }
            if (c < detectorRefs.size()) {
                String detector = detectorRefs.get(c);
                store.setDetectorSettingsID(detector, i, c);
                if (c < binnings.size()) {
                    store.setDetectorSettingsBinning(getBinning(binnings.get(c)), i, c);
                }
                if (c < channels.size()) {
                    store.setDetectorSettingsGain(channels.get(c).gain, i, c);
                }
            }
            if (c < channels.size()) {
                if (hasDetectorSettings) {
                    store.setDetectorSettingsGain(channels.get(c).gain, i, c);
                }
            }
        }
    }
    // not needed by further calls on the reader
    segments = null;
}
Also used : Temperature(ome.units.quantity.Temperature) ArrayList(java.util.ArrayList) CoreMetadata(loci.formats.CoreMetadata) Time(ome.units.quantity.Time) Timestamp(ome.xml.model.primitives.Timestamp) Color(ome.xml.model.primitives.Color) CoreMetadata(loci.formats.CoreMetadata) Pressure(ome.units.quantity.Pressure) FormatException(loci.formats.FormatException) Length(ome.units.quantity.Length) RandomAccessInputStream(loci.common.RandomAccessInputStream) Location(loci.common.Location)

Aggregations

ArrayList (java.util.ArrayList)1 Location (loci.common.Location)1 RandomAccessInputStream (loci.common.RandomAccessInputStream)1 CoreMetadata (loci.formats.CoreMetadata)1 FormatException (loci.formats.FormatException)1 Length (ome.units.quantity.Length)1 Pressure (ome.units.quantity.Pressure)1 Temperature (ome.units.quantity.Temperature)1 Time (ome.units.quantity.Time)1 Color (ome.xml.model.primitives.Color)1 Timestamp (ome.xml.model.primitives.Timestamp)1