Search in sources :

Example 26 with Time

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

the class FormatReaderTest method testExposureTimes.

@Test(groups = { "all", "fast", "automated" })
public void testExposureTimes() {
    if (config == null)
        throw new SkipException("No config tree");
    String testName = "ExposureTimes";
    if (!initFile())
        result(testName, false, "initFile");
    IMetadata retrieve = (IMetadata) reader.getMetadataStore();
    for (int i = 0; i < reader.getSeriesCount(); i++) {
        reader.setSeries(i);
        config.setSeries(i);
        if (reader.getImageCount() != retrieve.getPlaneCount(i)) {
            continue;
        }
        for (int c = 0; c < config.getChannelCount(); c++) {
            if (config.hasExposureTime(c)) {
                Time exposureTime = config.getExposureTime(c);
                for (int p = 0; p < reader.getImageCount(); p++) {
                    int[] zct = reader.getZCTCoords(p);
                    if (zct[1] == c && p < retrieve.getPlaneCount(i)) {
                        Time planeExposureTime = retrieve.getPlaneExposureTime(i, p);
                        if (exposureTime == null && planeExposureTime == null) {
                            continue;
                        }
                        if (exposureTime == null || planeExposureTime == null || !exposureTime.equals(planeExposureTime)) {
                            result(testName, false, "Series " + i + " plane " + p + " channel " + c + " (got " + planeExposureTime + ", expected " + exposureTime + ")");
                        }
                    }
                }
            }
        }
    }
    result(testName, true);
}
Also used : IMetadata(loci.formats.meta.IMetadata) Time(ome.units.quantity.Time) SkipException(org.testng.SkipException) Test(org.testng.annotations.Test)

Example 27 with Time

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

the class MicromanagerReader method populateMetadata.

private void populateMetadata() throws FormatException, IOException {
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, true);
    String instrumentID = MetadataTools.createLSID("Instrument", 0);
    store.setInstrumentID(instrumentID, 0);
    for (int i = 0; i < positions.size(); i++) {
        Position p = positions.get(i);
        if (p.time != null) {
            String date = DateTools.formatDate(p.time, DATE_FORMAT);
            if (date != null) {
                store.setImageAcquisitionDate(new Timestamp(date), i);
            }
        }
        if (positions.size() > 1) {
            Location parent = new Location(p.metadataFile).getParentFile();
            store.setImageName(parent.getName(), i);
        }
        if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
            store.setImageDescription(p.comment, i);
            // link Instrument and Image
            store.setImageInstrumentRef(instrumentID, i);
            for (int c = 0; c < p.channels.length; c++) {
                store.setChannelName(p.channels[c], i, c);
            }
            Length sizeX = FormatTools.getPhysicalSizeX(p.pixelSize);
            Length sizeY = FormatTools.getPhysicalSizeY(p.pixelSize);
            Length sizeZ = FormatTools.getPhysicalSizeZ(p.sliceThickness);
            if (sizeX != null) {
                store.setPixelsPhysicalSizeX(sizeX, i);
            }
            if (sizeY != null) {
                store.setPixelsPhysicalSizeY(sizeY, i);
            }
            if (sizeZ != null) {
                store.setPixelsPhysicalSizeZ(sizeZ, i);
            }
            int nextStamp = 0;
            for (int q = 0; q < getImageCount(); q++) {
                store.setPlaneExposureTime(p.exposureTime, i, q);
                String tiff = positions.get(getSeries()).getFile(q);
                if (tiff != null && new Location(tiff).exists() && nextStamp < p.timestamps.length && p.timestamps[nextStamp] != null) {
                    store.setPlaneDeltaT(new Time(p.timestamps[nextStamp++], UNITS.MILLISECOND), i, q);
                }
                if (p.positions != null && q < p.positions.length) {
                    if (p.positions[q][0] != null) {
                        store.setPlanePositionX(new Length(p.positions[q][0], UNITS.MICROMETER), i, q);
                    }
                    if (p.positions[q][1] != null) {
                        store.setPlanePositionY(new Length(p.positions[q][1], UNITS.MICROMETER), i, q);
                    }
                    if (p.positions[q][2] != null) {
                        store.setPlanePositionZ(new Length(p.positions[q][2], UNITS.MICROMETER), i, q);
                    }
                }
            }
            String serialNumber = p.detectorID;
            p.detectorID = MetadataTools.createLSID("Detector", 0, i);
            for (int c = 0; c < p.channels.length; c++) {
                store.setDetectorSettingsBinning(getBinning(p.binning), i, c);
                store.setDetectorSettingsGain(new Double(p.gain), i, c);
                if (c < p.voltage.size()) {
                    store.setDetectorSettingsVoltage(new ElectricPotential(p.voltage.get(c), UNITS.VOLT), i, c);
                }
                store.setDetectorSettingsID(p.detectorID, i, c);
            }
            store.setDetectorID(p.detectorID, 0, i);
            if (p.detectorModel != null) {
                store.setDetectorModel(p.detectorModel, 0, i);
            }
            if (serialNumber != null) {
                store.setDetectorSerialNumber(serialNumber, 0, i);
            }
            if (p.detectorManufacturer != null) {
                store.setDetectorManufacturer(p.detectorManufacturer, 0, i);
            }
            if (p.cameraMode == null)
                p.cameraMode = "Other";
            store.setDetectorType(getDetectorType(p.cameraMode), 0, i);
            store.setImagingEnvironmentTemperature(new Temperature(p.temperature, UNITS.CELSIUS), i);
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Temperature(ome.units.quantity.Temperature) Length(ome.units.quantity.Length) Time(ome.units.quantity.Time) Timestamp(ome.xml.model.primitives.Timestamp) ElectricPotential(ome.units.quantity.ElectricPotential) Location(loci.common.Location)

Example 28 with Time

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

the class FakeReader method parseSeriesTable.

/**
 * Translate key/value pairs from the INI table for the specified series.
 */
private void parseSeriesTable(IniTable table, MetadataStore store, int newSeries) {
    int s = getSeries();
    setSeries(newSeries);
    for (int i = 0; i < getImageCount(); i++) {
        String exposureTime = table.get("ExposureTime_" + i);
        String exposureTimeUnit = table.get("ExposureTimeUnit_" + i);
        if (exposureTime != null) {
            try {
                Double v = Double.valueOf(exposureTime);
                Time exposure = FormatTools.getTime(v, exposureTimeUnit);
                if (exposure != null) {
                    store.setPlaneExposureTime(exposure, newSeries, i);
                }
            } catch (NumberFormatException e) {
                LOGGER.trace("Could not parse ExposureTime for series #" + s + " plane #" + i, e);
            }
        }
        // TODO: could be cleaned up further when Java 8 is the minimum version
        Length x = parsePosition("X", s, i, table);
        if (x != null) {
            store.setPlanePositionX(x, newSeries, i);
        }
        Length y = parsePosition("Y", s, i, table);
        if (y != null) {
            store.setPlanePositionY(y, newSeries, i);
        }
        Length z = parsePosition("Z", s, i, table);
        if (z != null) {
            store.setPlanePositionZ(z, newSeries, i);
        }
    }
    setSeries(s);
}
Also used : UnitsLength(ome.xml.model.enums.UnitsLength) Length(ome.units.quantity.Length) Time(ome.units.quantity.Time)

Example 29 with Time

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

the class TiffReader method parseCommentImageJ.

private void parseCommentImageJ(String comment) throws FormatException, IOException {
    int nl = comment.indexOf("\n");
    put("ImageJ", nl < 0 ? comment.substring(7) : comment.substring(7, nl));
    metadata.remove("Comment");
    description = "";
    int z = 1, t = 1;
    int c = getSizeC();
    int images = 1;
    CoreMetadata m = core.get(0);
    if (ifds.get(0).containsKey(IMAGEJ_TAG)) {
        comment += "\n" + ifds.get(0).getIFDTextValue(IMAGEJ_TAG);
    }
    // parse ImageJ metadata (ZCT sizes, calibration units, etc.)
    StringTokenizer st = new StringTokenizer(comment, "\n");
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        String value = null;
        int eq = token.indexOf('=');
        if (eq >= 0)
            value = token.substring(eq + 1);
        if (token.startsWith("channels="))
            c = parseInt(value);
        else if (token.startsWith("slices="))
            z = parseInt(value);
        else if (token.startsWith("frames="))
            t = parseInt(value);
        else if (token.startsWith("images=")) {
            images = parseInt(value);
        } else if (token.startsWith("mode=")) {
            put("Color mode", value);
        } else if (token.startsWith("unit=")) {
            calibrationUnit = value;
            put("Unit", calibrationUnit);
        } else if (token.startsWith("finterval=")) {
            Double valueDouble = parseDouble(value);
            if (valueDouble != null) {
                timeIncrement = new Time(valueDouble, UNITS.SECOND);
                put("Frame Interval", timeIncrement);
            }
        } else if (token.startsWith("spacing=")) {
            physicalSizeZ = parseDouble(value);
            put("Spacing", physicalSizeZ);
        } else if (token.startsWith("xorigin=")) {
            xOrigin = parseInt(value);
            put("X Origin", xOrigin);
        } else if (token.startsWith("yorigin=")) {
            yOrigin = parseInt(value);
            put("Y Origin", yOrigin);
        } else if (eq > 0) {
            put(token.substring(0, eq).trim(), value);
        }
    }
    if (z * c * t == c && isRGB()) {
        t = getImageCount();
    }
    m.dimensionOrder = "XYCZT";
    if (z * t * (isRGB() ? 1 : c) == ifds.size()) {
        m.sizeZ = z;
        m.sizeT = t;
        m.sizeC = isRGB() ? getSizeC() : c;
    } else if (z * c * t == ifds.size() && isRGB()) {
        m.sizeZ = z;
        m.sizeT = t;
        m.sizeC *= c;
    } else if (ifds.size() == 1 && images > ifds.size() && ifds.get(0).getCompression() == TiffCompression.UNCOMPRESSED) {
        // file is likely corrupt or larger than 4GB (missing end IFDs)
        // 
        // ImageJ writes TIFF files like this:
        // IFD #0
        // comment
        // all pixel data
        // IFD #1
        // IFD #2
        // ...
        // 
        // since we know where the pixel data is, we can create fake
        // IFDs in an attempt to read the rest of the pixels
        IFD firstIFD = ifds.get(0);
        int planeSize = getSizeX() * getSizeY() * getRGBChannelCount() * FormatTools.getBytesPerPixel(getPixelType());
        long[] stripOffsets = firstIFD.getStripOffsets();
        long[] stripByteCounts = firstIFD.getStripByteCounts();
        long endOfFirstPlane = stripOffsets[stripOffsets.length - 1] + stripByteCounts[stripByteCounts.length - 1];
        long totalBytes = in.length() - endOfFirstPlane;
        int totalPlanes = (int) (totalBytes / planeSize) + 1;
        ifds = new IFDList();
        ifds.add(firstIFD);
        for (int i = 1; i < totalPlanes; i++) {
            IFD ifd = new IFD(firstIFD);
            ifds.add(ifd);
            long[] prevOffsets = ifds.get(i - 1).getStripOffsets();
            long[] offsets = new long[stripOffsets.length];
            offsets[0] = prevOffsets[prevOffsets.length - 1] + stripByteCounts[stripByteCounts.length - 1];
            for (int j = 1; j < offsets.length; j++) {
                offsets[j] = offsets[j - 1] + stripByteCounts[j - 1];
            }
            ifd.putIFDValue(IFD.STRIP_OFFSETS, offsets);
        }
        if (z * c * t == ifds.size()) {
            m.sizeZ = z;
            m.sizeT = t;
            m.sizeC = c;
        } else if (z * t == ifds.size()) {
            m.sizeZ = z;
            m.sizeT = t;
        } else
            m.sizeZ = ifds.size();
        m.imageCount = ifds.size();
    } else {
        m.sizeT = ifds.size();
        m.imageCount = ifds.size();
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) IFD(loci.formats.tiff.IFD) IFDList(loci.formats.tiff.IFDList) Time(ome.units.quantity.Time) CoreMetadata(loci.formats.CoreMetadata)

Example 30 with Time

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

the class ICSWriter method setId.

// -- IFormatHandler API methods --
/* @see loci.formats.FormatWriter#setId(String) */
@Override
public void setId(String id) throws FormatException, IOException {
    super.setId(id);
    if (!uniqueFiles.contains(id)) {
        uniqueFiles.add(id);
    }
    if (checkSuffix(currentId, "ids")) {
        String metadataFile = currentId.substring(0, currentId.lastIndexOf("."));
        metadataFile += ".ics";
        out.close();
        out = new RandomAccessOutputStream(metadataFile);
    }
    if (out.length() == 0) {
        out.writeBytes("\t\n");
        if (checkSuffix(id, "ids")) {
            out.writeBytes("ics_version\t1.0\n");
        } else {
            out.writeBytes("ics_version\t2.0\n");
        }
        out.writeBytes("filename\t" + currentId + "\n");
        out.writeBytes("layout\tparameters\t6\n");
        MetadataRetrieve meta = getMetadataRetrieve();
        MetadataTools.verifyMinimumPopulated(meta, series);
        int pixelType = FormatTools.pixelTypeFromString(meta.getPixelsType(series).toString());
        dimensionOffset = out.getFilePointer();
        int[] sizes = overwriteDimensions(meta);
        dimensionLength = (int) (out.getFilePointer() - dimensionOffset);
        if (validBits != 0) {
            out.writeBytes("layout\tsignificant_bits\t" + validBits + "\n");
        }
        boolean signed = FormatTools.isSigned(pixelType);
        boolean littleEndian = false;
        if (meta.getPixelsBigEndian(series) != null) {
            littleEndian = !meta.getPixelsBigEndian(series).booleanValue();
        } else if (meta.getPixelsBinDataCount(series) == 0) {
            littleEndian = !meta.getPixelsBinDataBigEndian(series, 0).booleanValue();
        }
        out.writeBytes("representation\tformat\t" + (pixelType == FormatTools.FLOAT ? "real\n" : "integer\n"));
        out.writeBytes("representation\tsign\t" + (signed ? "signed\n" : "unsigned\n"));
        out.writeBytes("representation\tcompression\tuncompressed\n");
        out.writeBytes("representation\tbyte_order\t");
        for (int i = 0; i < sizes[0] / 8; i++) {
            if ((littleEndian && (sizes[0] < 32 || pixelType == FormatTools.FLOAT)) || (!littleEndian && sizes[0] >= 32 && pixelType != FormatTools.FLOAT)) {
                out.writeBytes((i + 1) + "\t");
            } else {
                out.writeBytes(((sizes[0] / 8) - i) + "\t");
            }
        }
        out.writeBytes("\nparameter\tscale\t1.000000\t");
        final StringBuilder units = new StringBuilder();
        for (int i = 0; i < outputOrder.length(); i++) {
            char dim = outputOrder.charAt(i);
            Number value = 1.0;
            if (dim == 'X') {
                if (meta.getPixelsPhysicalSizeX(0) != null) {
                    value = meta.getPixelsPhysicalSizeX(0).value(UNITS.MICROMETER).doubleValue();
                }
                units.append("micrometers\t");
            } else if (dim == 'Y') {
                if (meta.getPixelsPhysicalSizeY(0) != null) {
                    value = meta.getPixelsPhysicalSizeY(0).value(UNITS.MICROMETER).doubleValue();
                }
                units.append("micrometers\t");
            } else if (dim == 'Z') {
                if (meta.getPixelsPhysicalSizeZ(0) != null) {
                    value = meta.getPixelsPhysicalSizeZ(0).value(UNITS.MICROMETER).doubleValue();
                }
                units.append("micrometers\t");
            } else if (dim == 'T') {
                Time valueTime = meta.getPixelsTimeIncrement(0);
                if (valueTime != null) {
                    value = valueTime.value(UNITS.SECOND);
                    units.append("seconds\t");
                }
            }
            out.writeBytes(value + "\t");
        }
        out.writeBytes("\nparameter\tunits\tbits\t" + units.toString() + "\n");
        out.writeBytes("\nend\n");
        pixelOffset = out.getFilePointer();
    } else if (checkSuffix(currentId, "ics")) {
        RandomAccessInputStream in = new RandomAccessInputStream(currentId);
        in.findString("\nend\n");
        pixelOffset = in.getFilePointer();
        in.close();
    }
    if (checkSuffix(currentId, "ids")) {
        pixelOffset = 0;
    }
}
Also used : RandomAccessOutputStream(loci.common.RandomAccessOutputStream) Time(ome.units.quantity.Time) RandomAccessInputStream(loci.common.RandomAccessInputStream) MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Aggregations

Time (ome.units.quantity.Time)74 Length (ome.units.quantity.Length)46 MetadataStore (loci.formats.meta.MetadataStore)41 Timestamp (ome.xml.model.primitives.Timestamp)33 CoreMetadata (loci.formats.CoreMetadata)30 FormatException (loci.formats.FormatException)24 RandomAccessInputStream (loci.common.RandomAccessInputStream)21 Location (loci.common.Location)20 ArrayList (java.util.ArrayList)17 PositiveInteger (ome.xml.model.primitives.PositiveInteger)13 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)12 IOException (java.io.IOException)10 Temperature (ome.units.quantity.Temperature)9 IFD (loci.formats.tiff.IFD)8 ElectricPotential (ome.units.quantity.ElectricPotential)7 Color (ome.xml.model.primitives.Color)7 IMetadata (loci.formats.meta.IMetadata)6 File (java.io.File)5 DependencyException (loci.common.services.DependencyException)5 ServiceException (loci.common.services.ServiceException)5