Search in sources :

Example 1 with Binning

use of ome.xml.model.enums.Binning in project bioformats by openmicroscopy.

the class ZeissCZIReader method translateExperiment.

private void translateExperiment(Element root) throws FormatException {
    NodeList experiments = root.getElementsByTagName("Experiment");
    if (experiments == null || experiments.getLength() == 0) {
        return;
    }
    Element experimentBlock = getFirstNode((Element) experiments.item(0), "ExperimentBlocks");
    Element acquisition = getFirstNode(experimentBlock, "AcquisitionBlock");
    Element tilesSetup = getFirstNode(acquisition, "TilesSetup");
    NodeList groups = getGrandchildren(tilesSetup, "PositionGroup");
    positionsX = new Length[core.size()];
    positionsY = new Length[core.size()];
    positionsZ = new Length[core.size()];
    if (groups != null) {
        for (int i = 0; i < groups.getLength(); i++) {
            Element group = (Element) groups.item(i);
            Element position = getFirstNode(group, "Position");
            String tilesXValue = getFirstNodeValue(group, "TilesX");
            String tilesYValue = getFirstNodeValue(group, "TilesY");
            if (position != null && tilesXValue != null && !tilesXValue.isEmpty() && tilesYValue != null && !tilesYValue.isEmpty()) {
                Integer tilesX = DataTools.parseInteger(tilesXValue);
                Integer tilesY = DataTools.parseInteger(tilesYValue);
                String x = position.getAttribute("X");
                String y = position.getAttribute("Y");
                String z = position.getAttribute("Z");
                Length xPos = null;
                try {
                    xPos = new Length(Double.valueOf(x), UNITS.METRE);
                } catch (NumberFormatException e) {
                }
                Length yPos = null;
                try {
                    yPos = new Length(Double.valueOf(y), UNITS.METRE);
                } catch (NumberFormatException e) {
                }
                Length zPos = null;
                try {
                    zPos = new Length(Double.valueOf(z), UNITS.METRE);
                } catch (NumberFormatException e) {
                }
                int numTiles = (tilesX == null || tilesY == null) ? 0 : tilesX * tilesY;
                for (int tile = 0; tile < numTiles; tile++) {
                    int index = i * tilesX * tilesY + tile;
                    if (groups.getLength() == core.size()) {
                        index = i;
                    }
                    if (index < positionsX.length) {
                        positionsX[index] = xPos;
                        positionsY[index] = yPos;
                        positionsZ[index] = zPos;
                    }
                }
            }
        }
    } else {
        Element regionsSetup = getFirstNode(acquisition, "RegionsSetup");
        if (regionsSetup != null) {
            Element sampleHolder = getFirstNode(regionsSetup, "SampleHolder");
            if (sampleHolder != null) {
                NodeList regions = getGrandchildren(sampleHolder, "SingleTileRegionArray", "SingleTileRegion");
                if (regions != null) {
                    for (int i = 0; i < regions.getLength(); i++) {
                        Element region = (Element) regions.item(i);
                        String x = getFirstNode(region, "X").getTextContent();
                        String y = getFirstNode(region, "Y").getTextContent();
                        String z = getFirstNode(region, "Z").getTextContent();
                        // safe to assume all 3 arrays have the same length
                        if (i < positionsX.length) {
                            if (x == null) {
                                positionsX[i] = null;
                            } else {
                                final Double number = Double.valueOf(x);
                                positionsX[i] = new Length(number, UNITS.MICROMETER);
                            }
                            if (y == null) {
                                positionsY[i] = null;
                            } else {
                                final Double number = Double.valueOf(y);
                                positionsY[i] = new Length(number, UNITS.MICROMETER);
                            }
                            if (z == null) {
                                positionsZ[i] = null;
                            } else {
                                final Double number = Double.valueOf(z);
                                positionsZ[i] = new Length(number, UNITS.MICROMETER);
                            }
                        }
                    }
                }
            }
        }
    }
    NodeList detectors = getGrandchildren(acquisition, "Detector");
    Element setup = getFirstNode(acquisition, "AcquisitionModeSetup");
    String cameraModel = getFirstNodeValue(setup, "SelectedCamera");
    if (detectors != null) {
        for (int i = 0; i < detectors.getLength(); i++) {
            Element detector = (Element) detectors.item(i);
            String id = MetadataTools.createLSID("Detector", 0, i);
            store.setDetectorID(id, 0, i);
            String model = detector.getAttribute("Id");
            store.setDetectorModel(model, 0, i);
            String bin = getFirstNodeValue(detector, "Binning");
            if (bin != null) {
                bin = bin.replaceAll(",", "x");
                Binning binning = getBinning(bin);
                if (model != null && model.equals(cameraModel)) {
                    for (int image = 0; image < getSeriesCount(); image++) {
                        for (int c = 0; c < getEffectiveSizeC(); c++) {
                            store.setDetectorSettingsID(id, image, c);
                            store.setDetectorSettingsBinning(binning, image, c);
                        }
                    }
                    hasDetectorSettings = true;
                }
            }
        }
    }
    Element multiTrack = getFirstNode(acquisition, "MultiTrackSetup");
    if (multiTrack == null) {
        return;
    }
    NodeList detectorGroups = multiTrack.getElementsByTagName("Detectors");
    for (int d = 0; d < detectorGroups.getLength(); d++) {
        Element detectorGroup = (Element) detectorGroups.item(d);
        detectors = detectorGroup.getElementsByTagName("Detector");
        if (detectors != null && detectors.getLength() > 0) {
            for (int i = 0; i < detectors.getLength(); i++) {
                Element detector = (Element) detectors.item(i);
                String voltage = getFirstNodeValue(detector, "Voltage");
                if (i == 0 && d == 0) {
                    gain = voltage;
                }
                gains.add(voltage);
            }
        }
    }
    NodeList tracks = multiTrack.getElementsByTagName("Track");
    if (tracks != null && tracks.getLength() > 0) {
        for (int i = 0; i < tracks.getLength(); i++) {
            Element track = (Element) tracks.item(i);
            Element channel = getFirstNode(track, "Channel");
            String exposure = getFirstNodeValue(channel, "ExposureTime");
            String gain = getFirstNodeValue(channel, "EMGain");
            while (channels.size() <= i) {
                channels.add(new Channel());
            }
            try {
                if (exposure != null) {
                    channels.get(i).exposure = new Double(exposure);
                }
            } catch (NumberFormatException e) {
                LOGGER.debug("Could not parse exposure time", e);
            }
            try {
                if (gain != null) {
                    channels.get(i).gain = new Double(gain);
                }
            } catch (NumberFormatException e) {
                LOGGER.debug("Could not parse gain", e);
            }
        }
    }
}
Also used : Binning(ome.xml.model.enums.Binning) Length(ome.units.quantity.Length) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element)

Example 2 with Binning

use of ome.xml.model.enums.Binning in project bioformats by openmicroscopy.

the class PCIReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    imageFiles = new HashMap<Integer, String>();
    timestamps = new HashMap<Integer, Double>();
    uniqueZ = new ArrayList<Double>();
    CoreMetadata m = core.get(0);
    initPOIService();
    double scaleFactor = 1;
    final List<String> allFiles = poi.getDocumentList();
    if (allFiles.isEmpty()) {
        throw new FormatException("No files were found - the .cxd may be corrupt.");
    }
    double firstZ = 0d, secondZ = 0d;
    int mode = 0;
    for (String name : allFiles) {
        int separator = name.lastIndexOf(File.separator);
        String parent = name.substring(0, separator);
        String relativePath = name.substring(separator + 1);
        RandomAccessInputStream stream = null;
        if (!(relativePath.startsWith("Bitmap") || (relativePath.equals("Data") && parent.indexOf("Image") != -1))) {
            stream = poi.getDocumentStream(name);
            stream.order(true);
        }
        if (stream != null && stream.length() == 8) {
            double value = stream.readDouble();
            stream.seek(0);
            String key = name.replace(File.separatorChar, ' ');
            key = key.replaceAll("Root Entry ", "");
            key = key.replaceAll("Field Data ", "");
            key = key.replaceAll("Details ", "");
            addGlobalMeta(key, value);
        }
        if (relativePath.equals("Field Count")) {
            m.imageCount = stream.readInt();
        } else if (relativePath.equals("File Has Image")) {
            if (stream.readShort() == 0) {
                throw new FormatException("This file does not contain image data.");
            }
        } else if (relativePath.startsWith("Bitmap") || (relativePath.equals("Data") && parent.indexOf("Image") != -1)) {
            imageFiles.put(imageFiles.size(), name);
            if (getSizeX() != 0 && getSizeY() != 0) {
                int bpp = FormatTools.getBytesPerPixel(getPixelType());
                int plane = getSizeX() * getSizeY() * bpp;
                if (getSizeC() == 0 || getSizeC() * plane > poi.getFileSize(name)) {
                    m.sizeC = poi.getFileSize(name) / plane;
                }
            }
        } else if (relativePath.indexOf("Image_Depth") != -1) {
            boolean firstBits = m.bitsPerPixel == 0;
            int bits = (int) stream.readDouble();
            m.bitsPerPixel = bits;
            while (bits % 8 != 0 || bits == 0) bits++;
            if (bits % 3 == 0) {
                m.sizeC = 3;
                bits /= 3;
                m.bitsPerPixel /= 3;
            }
            bits /= 8;
            m.pixelType = FormatTools.pixelTypeFromBytes(bits, false, false);
            if (getSizeC() > 1 && firstBits) {
                m.sizeC /= bits;
            }
        } else if (relativePath.indexOf("Image_Height") != -1 && getSizeY() == 0) {
            m.sizeY = (int) stream.readDouble();
        } else if (relativePath.indexOf("Image_Width") != -1 && getSizeX() == 0) {
            m.sizeX = (int) stream.readDouble();
        } else if (relativePath.indexOf("Time_From_Start") != -1) {
            timestamps.put(getTimestampIndex(parent), stream.readDouble());
        } else if (relativePath.indexOf("Position_Z") != -1) {
            double zPos = stream.readDouble();
            if (!uniqueZ.contains(zPos) && getSizeZ() <= 1) {
                uniqueZ.add(zPos);
            }
            if (name.indexOf("Field 1" + File.separator) != -1)
                firstZ = zPos;
            else if (name.indexOf("Field 2" + File.separator) != -1)
                secondZ = zPos;
        } else if (relativePath.equals("First Field Date & Time")) {
            long date = (long) stream.readDouble() * 1000;
            creationDate = DateTools.convertDate(date, DateTools.COBOL);
        } else if (relativePath.equals("GroupMode")) {
            mode = stream.readInt();
        } else if (relativePath.equals("GroupSelectedFields")) {
            m.sizeZ = (int) (stream.length() / 8);
        } else if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
            if (relativePath.equals("Binning")) {
                binning = (int) stream.readDouble();
            } else if (relativePath.equals("Comments")) {
                String comments = stream.readString((int) stream.length());
                String[] lines = comments.split("\n");
                for (String line : lines) {
                    int eq = line.indexOf('=');
                    if (eq != -1) {
                        String key = line.substring(0, eq).trim();
                        String value = line.substring(eq + 1).trim();
                        addGlobalMeta(key, value);
                        if (key.equals("factor")) {
                            if (value.indexOf(';') != -1) {
                                value = value.substring(0, value.indexOf(';'));
                            }
                            scaleFactor = Double.parseDouble(value.trim());
                        }
                    }
                }
            }
        }
        if (stream != null) {
            stream.close();
        }
    }
    boolean zFirst = Math.abs(firstZ - secondZ) > Constants.EPSILON;
    if (getSizeC() == 0)
        m.sizeC = 1;
    if (mode == 0) {
        m.sizeZ = 0;
    }
    if (getSizeZ() <= 1 || (getImageCount() % getSizeZ()) != 0) {
        m.sizeZ = uniqueZ.isEmpty() ? 1 : uniqueZ.size();
    }
    m.sizeT = getImageCount() / getSizeZ();
    while (getSizeZ() * getSizeT() < getImageCount()) {
        m.sizeZ++;
        m.sizeT = getImageCount() / getSizeZ();
    }
    m.rgb = getSizeC() > 1;
    if (imageFiles.size() > getImageCount() && getSizeC() == 1) {
        m.sizeC = imageFiles.size() / getImageCount();
        m.imageCount *= getSizeC();
    } else {
        m.imageCount = getSizeZ() * getSizeT();
    }
    m.interleaved = false;
    m.dimensionOrder = zFirst ? "XYCZT" : "XYCTZ";
    m.littleEndian = true;
    m.indexed = false;
    m.falseColor = false;
    m.metadataComplete = true;
    // re-index image files
    String[] files = imageFiles.values().toArray(new String[imageFiles.size()]);
    for (String file : files) {
        int separator = file.lastIndexOf(File.separator);
        String parent = file.substring(0, separator);
        imageFiles.put(getImageIndex(parent), file);
    }
    int bpp = FormatTools.getBytesPerPixel(m.pixelType);
    int expectedPlaneSize = m.sizeX * m.sizeY * bpp * m.sizeC;
    String file = imageFiles.get(0);
    RandomAccessInputStream s = poi.getDocumentStream(file);
    TiffParser tp = new TiffParser(s);
    // don't correct the image width if it's stored as a TIFF
    if (!tp.isValidHeader() && s.length() > expectedPlaneSize) {
        m.sizeX += (s.length() - expectedPlaneSize) / (m.sizeY * bpp * m.sizeC);
    }
    s.close();
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, true);
    if (creationDate != null) {
        store.setImageAcquisitionDate(new Timestamp(creationDate), 0);
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        Length sizeX = FormatTools.getPhysicalSizeX(scaleFactor);
        Length sizeY = FormatTools.getPhysicalSizeY(scaleFactor);
        if (sizeX != null) {
            store.setPixelsPhysicalSizeX(sizeX, 0);
        }
        if (sizeY != null) {
            store.setPixelsPhysicalSizeY(sizeY, 0);
        }
        for (int i = 0; i < timestamps.size(); i++) {
            if (i >= getImageCount()) {
                break;
            }
            Double timestamp = timestamps.get(i);
            if (timestamp != null) {
                store.setPlaneDeltaT(new Time(timestamp, UNITS.SECOND), 0, i);
            }
            if (i == 2) {
                Double first = timestamps.get(1);
                Double increment = timestamp - first;
                if (increment != null) {
                    store.setPixelsTimeIncrement(new Time(increment, UNITS.SECOND), 0);
                }
            }
        }
        if (binning > 0) {
            String instrumentID = MetadataTools.createLSID("Instrument", 0);
            String detectorID = MetadataTools.createLSID("Detector", 0);
            store.setInstrumentID(instrumentID, 0);
            store.setDetectorID(detectorID, 0, 0);
            store.setDetectorType(getDetectorType("Other"), 0, 0);
            store.setImageInstrumentRef(instrumentID, 0);
            Binning binningEnum = getBinning(binning + "x" + binning);
            for (int c = 0; c < getEffectiveSizeC(); c++) {
                store.setDetectorSettingsID(detectorID, 0, c);
                store.setDetectorSettingsBinning(binningEnum, 0, c);
            }
        }
    }
}
Also used : Time(ome.units.quantity.Time) CoreMetadata(loci.formats.CoreMetadata) Timestamp(ome.xml.model.primitives.Timestamp) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) Binning(ome.xml.model.enums.Binning) Length(ome.units.quantity.Length) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Aggregations

Length (ome.units.quantity.Length)2 Binning (ome.xml.model.enums.Binning)2 RandomAccessInputStream (loci.common.RandomAccessInputStream)1 CoreMetadata (loci.formats.CoreMetadata)1 FormatException (loci.formats.FormatException)1 MetadataStore (loci.formats.meta.MetadataStore)1 TiffParser (loci.formats.tiff.TiffParser)1 Time (ome.units.quantity.Time)1 Timestamp (ome.xml.model.primitives.Timestamp)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1