Search in sources :

Example 61 with Timestamp

use of ome.xml.model.primitives.Timestamp in project bioformats by openmicroscopy.

the class GelReader method initMetadata.

// -- Internal BaseTiffReader API methods --
/* @see BaseTiffReader#initMetadata() */
@Override
protected void initMetadata() throws FormatException, IOException {
    ifds = tiffParser.getIFDs();
    if (ifds.size() > 1) {
        IFDList tmpIFDs = ifds;
        ifds = new IFDList();
        for (int i = 0; i < tmpIFDs.size() / 2; i++) {
            IFD ifd = new IFD();
            ifds.add(ifd);
            ifd.putAll(tmpIFDs.get(i * 2 + 1));
            ifd.putAll(tmpIFDs.get(i * 2));
            tiffParser.fillInIFD(ifd);
        }
    }
    IFD firstIFD = ifds.get(0);
    tiffParser.fillInIFD(firstIFD);
    super.initStandardMetadata();
    fmt = firstIFD.getIFDLongValue(MD_FILETAG, LINEAR);
    if (fmt == SQUARE_ROOT)
        core.get(0).pixelType = FormatTools.FLOAT;
    TiffRational scale = (TiffRational) firstIFD.getIFDValue(MD_SCALE_PIXEL);
    if (scale == null)
        scale = new TiffRational(1, 1);
    core.get(0).imageCount = ifds.size();
    core.get(0).sizeT = getImageCount();
    // ignore MD_COLOR_TABLE
    String info = firstIFD.getIFDStringValue(MD_SAMPLE_INFO);
    String prepDate = firstIFD.getIFDStringValue(MD_PREP_DATE);
    String prepTime = firstIFD.getIFDStringValue(MD_PREP_TIME);
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        String units = firstIFD.getIFDStringValue(MD_FILE_UNITS);
        String lab = firstIFD.getIFDStringValue(MD_LAB_NAME);
        addGlobalMeta("Scale factor", scale);
        addGlobalMeta("Lab name", lab);
        addGlobalMeta("Sample info", info);
        addGlobalMeta("Date prepared", prepDate);
        addGlobalMeta("Time prepared", prepTime);
        addGlobalMeta("File units", units);
        addGlobalMeta("Data format", fmt == SQUARE_ROOT ? "square root" : "linear");
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    String parsedDate = DateTools.formatDate(prepDate, FORMATS);
    String parsedTime = DateTools.formatDate(prepTime, FORMATS);
    if (parsedDate != null) {
        store.setImageAcquisitionDate(new Timestamp(parsedDate), 0);
    } else if (parsedTime != null) {
        store.setImageAcquisitionDate(new Timestamp(parsedTime), 0);
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        Double pixelSize = new Double(scale.doubleValue());
        Length sizeX = FormatTools.getPhysicalSizeX(pixelSize);
        Length sizeY = FormatTools.getPhysicalSizeY(pixelSize);
        if (sizeX != null) {
            store.setPixelsPhysicalSizeX(sizeX, 0);
        }
        if (sizeY != null) {
            store.setPixelsPhysicalSizeY(sizeY, 0);
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) IFD(loci.formats.tiff.IFD) IFDList(loci.formats.tiff.IFDList) TiffRational(loci.formats.tiff.TiffRational) Timestamp(ome.xml.model.primitives.Timestamp)

Example 62 with Timestamp

use of ome.xml.model.primitives.Timestamp in project bioformats by openmicroscopy.

the class MolecularImagingReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    in = new RandomAccessInputStream(id);
    CoreMetadata m = core.get(0);
    m.sizeZ = 0;
    String date = null;
    double pixelSizeX = 0d, pixelSizeY = 0d;
    String data = in.findString("Data_section  \r\n");
    String[] lines = data.split("\n");
    for (String line : lines) {
        line = line.trim();
        int space = line.indexOf(' ');
        if (space != -1) {
            String key = line.substring(0, space).trim();
            String value = line.substring(space + 1).trim();
            addGlobalMeta(key, value);
            if (key.equals("samples_x")) {
                m.sizeX = Integer.parseInt(value);
            } else if (key.equals("samples_y")) {
                m.sizeY = Integer.parseInt(value);
            } else if (key.equals("buffer_id")) {
                m.sizeZ++;
            } else if (key.equals("Date")) {
                date = value;
            } else if (key.equals("time")) {
                date += " " + value;
            } else if (key.equals("length_x")) {
                pixelSizeX = Double.parseDouble(value) / getSizeX();
            } else if (key.equals("length_y")) {
                pixelSizeY = Double.parseDouble(value) / getSizeY();
            }
        }
    }
    pixelOffset = in.getFilePointer();
    m.sizeC = 1;
    m.sizeT = 1;
    m.imageCount = getSizeZ();
    m.rgb = false;
    m.pixelType = FormatTools.UINT16;
    m.littleEndian = true;
    m.dimensionOrder = "XYZCT";
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    if (date != null) {
        date = DateTools.formatDate(date, DATE_FORMAT);
        if (date != null) {
            store.setImageAcquisitionDate(new Timestamp(date), 0);
        }
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        Length sizeX = FormatTools.getPhysicalSizeX(pixelSizeX);
        Length sizeY = FormatTools.getPhysicalSizeY(pixelSizeY);
        if (sizeX != null) {
            store.setPixelsPhysicalSizeX(sizeX, 0);
        }
        if (sizeY != null) {
            store.setPixelsPhysicalSizeY(sizeY, 0);
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) Timestamp(ome.xml.model.primitives.Timestamp)

Example 63 with Timestamp

use of ome.xml.model.primitives.Timestamp in project bioformats by openmicroscopy.

the class MetamorphTiffReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    List<String> uniqueChannels = new ArrayList<String>();
    final List<Double> uniqueZs = new ArrayList<Double>();
    final List<Length> stageX = new ArrayList<Length>();
    final List<Length> stageY = new ArrayList<Length>();
    CoreMetadata m = core.get(0);
    String filename = id.substring(id.lastIndexOf(File.separator) + 1);
    filename = filename.substring(0, filename.indexOf('.'));
    boolean integerFilename = true;
    try {
        Integer.parseInt(filename);
    } catch (NumberFormatException e) {
        integerFilename = false;
    }
    if (integerFilename && ifds.size() == 1 && ifds.get(0).getIFDIntValue(IFD.NEW_SUBFILE_TYPE) == 2) {
        // look for other files in the dataset
        findTIFFs();
        String stageLabel = null;
        for (String tiff : files) {
            MetamorphHandler handler = new MetamorphHandler();
            parseFile(tiff, handler);
            String label = handler.getStageLabel();
            if (stageLabel == null) {
                stageLabel = label;
            } else if (!label.equals(stageLabel)) {
                break;
            }
            if (!uniqueChannels.contains(handler.getChannelName())) {
                uniqueChannels.add(handler.getChannelName());
            }
            final List<Double> zPositions = handler.getZPositions();
            Double pos = Math.rint(zPositions.get(0));
            if (!uniqueZs.contains(pos)) {
                uniqueZs.add(pos);
            }
        }
        MetamorphHandler handler = new MetamorphHandler();
        parseFile(files[files.length - 1], handler);
        String lastStageLabel = handler.getStageLabel();
        int lastField = getField(lastStageLabel);
        int lastWellRow = getWellRow(lastStageLabel);
        int lastWellColumn = getWellColumn(lastStageLabel);
        int field = getField(stageLabel);
        int fieldRow = getWellRow(stageLabel);
        int fieldColumn = getWellColumn(stageLabel);
        wellCount = lastField - field + 1;
        fieldRowCount = lastWellRow - fieldRow + 1;
        fieldColumnCount = lastWellColumn - fieldColumn + 1;
        m.sizeC = uniqueChannels.size();
        m.sizeZ = uniqueZs.size();
    } else {
        files = new String[] { id };
        wellCount = 1;
        fieldRowCount = 1;
        fieldColumnCount = 1;
        m.sizeC = 0;
    }
    // parse XML comment
    MetamorphHandler handler = new MetamorphHandler(getGlobalMetadata());
    final List<Length> xPositions = new ArrayList<Length>();
    final List<Length> yPositions = new ArrayList<Length>();
    for (IFD ifd : ifds) {
        String xml = XMLTools.sanitizeXML(ifd.getComment());
        XMLTools.parseXML(xml, handler);
        final Length x = handler.getStagePositionX();
        final Length y = handler.getStagePositionY();
        if (xPositions.isEmpty()) {
            xPositions.add(x);
            yPositions.add(y);
        } else {
            final Length previousX = xPositions.get(xPositions.size() - 1);
            final Length previousY = yPositions.get(yPositions.size() - 1);
            final double x1 = x.value(UNITS.REFERENCEFRAME).doubleValue();
            final double x2 = previousX.value(UNITS.REFERENCEFRAME).doubleValue();
            final double y1 = y.value(UNITS.REFERENCEFRAME).doubleValue();
            final double y2 = previousY.value(UNITS.REFERENCEFRAME).doubleValue();
            if (Math.abs(x1 - x2) > 0.21 || Math.abs(y1 - y2) > 0.21) {
                xPositions.add(x);
                yPositions.add(y);
            }
        }
    }
    if (xPositions.size() > 1) {
        fieldRowCount = xPositions.size();
    }
    final List<Integer> wavelengths = handler.getWavelengths();
    final List<Double> zPositions = handler.getZPositions();
    dualCamera = handler.hasDualCamera();
    // calculate axis sizes
    final List<Integer> uniqueC = new ArrayList<Integer>();
    for (Integer c : wavelengths) {
        if (!uniqueC.contains(c)) {
            uniqueC.add(c);
        }
    }
    int effectiveC = uniqueC.size();
    if (effectiveC == 0)
        effectiveC = 1;
    if (getSizeC() == 0)
        m.sizeC = 1;
    int samples = ifds.get(0).getSamplesPerPixel();
    m.sizeC *= effectiveC * samples;
    final List<Double> uniqueZ = new ArrayList<Double>();
    for (Double z : zPositions) {
        if (!uniqueZ.contains(z))
            uniqueZ.add(z);
    }
    if (getSizeZ() == 0)
        m.sizeZ = 1;
    if (uniqueZ.size() > 1)
        m.sizeZ *= uniqueZ.size();
    Double physicalSizeZ = null;
    if (m.sizeZ > 1) {
        Double zRange = zPositions.get(zPositions.size() - 1) - zPositions.get(0);
        physicalSizeZ = Math.abs(zRange);
        physicalSizeZ /= (m.sizeZ - 1);
    }
    int totalPlanes = files.length * ifds.size();
    effectiveC = getSizeC() / samples;
    // account for the multiple actual Z sections
    if (effectiveC * getSizeZ() > totalPlanes && (effectiveC * (getSizeZ() / uniqueC.size()) == totalPlanes || effectiveC == totalPlanes)) {
        if (getSizeZ() >= uniqueC.size()) {
            m.sizeZ /= uniqueC.size();
        } else {
            m.sizeZ = 1;
        }
    }
    m.sizeT = totalPlanes / (wellCount * fieldRowCount * fieldColumnCount * getSizeZ() * effectiveC);
    if (getSizeT() == 0)
        m.sizeT = 1;
    int seriesCount = wellCount * fieldRowCount * fieldColumnCount;
    if (seriesCount > 1 && getSizeZ() > totalPlanes / seriesCount || totalPlanes > getSizeZ() * getSizeT() * effectiveC) {
        m.sizeZ = 1;
        m.sizeT = totalPlanes / (seriesCount * effectiveC);
    }
    m.imageCount = getSizeZ() * getSizeT() * effectiveC;
    if (dualCamera) {
        m.sizeX /= 2;
        m.sizeC *= 2;
        m.imageCount *= 2;
    }
    if (seriesCount > 1) {
        core.clear();
        for (int i = 0; i < seriesCount; i++) {
            core.add(m);
        }
    }
    for (int s = 0; s < wellCount * fieldRowCount * fieldColumnCount; s++) {
        if (files.length > 1) {
            int[] lengths = new int[] { getSizeZ(), getEffectiveSizeC(), fieldColumnCount, fieldRowCount, wellCount, getSizeT() };
            Well well = getWell(s);
            int[] position = new int[] { 0, 0, well.fieldCol, well.fieldRow, well.well, 0 };
            int fileIndex = FormatTools.positionToRaster(lengths, position);
            parseFile(files[fileIndex], handler);
            stageX.add(handler.getStagePositionX());
            stageY.add(handler.getStagePositionY());
        } else {
            stageX.add(xPositions.get(s));
            stageY.add(yPositions.get(s));
        }
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    // effectively useless).
    if (wellCount > 1) {
        store.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
        store.setPlateRows(new PositiveInteger(1), 0);
        store.setPlateColumns(new PositiveInteger(wellCount), 0);
        store.setPlateRowNamingConvention(NamingConvention.LETTER, 0);
        store.setPlateColumnNamingConvention(NamingConvention.NUMBER, 0);
        store.setPlateAcquisitionID(MetadataTools.createLSID("PlateAcquisition", 0, 0), 0, 0);
        for (int well = 0; well < wellCount; well++) {
            store.setWellID(MetadataTools.createLSID("Well", 0, well), 0, well);
            store.setWellRow(new NonNegativeInteger(0), 0, well);
            store.setWellColumn(new NonNegativeInteger(well), 0, well);
            for (int row = 0; row < fieldRowCount; row++) {
                for (int col = 0; col < fieldColumnCount; col++) {
                    int field = row * fieldColumnCount + col;
                    String wellSampleID = MetadataTools.createLSID("WellSample", 0, well, field);
                    store.setWellSampleID(wellSampleID, 0, well, field);
                    int seriesIndex = getSeriesIndex(row, col, well);
                    String imageID = MetadataTools.createLSID("Image", seriesIndex);
                    store.setImageID(imageID, seriesIndex);
                    store.setWellSampleImageRef(imageID, 0, well, field);
                    store.setWellSampleIndex(new NonNegativeInteger(seriesIndex), 0, well, field);
                    store.setPlateAcquisitionWellSampleRef(wellSampleID, 0, 0, seriesIndex);
                }
            }
        }
    }
    final List<String> timestamps = handler.getTimestamps();
    final List<Double> exposures = handler.getExposures();
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        for (int i = 0; i < timestamps.size(); i++) {
            long timestamp = DateTools.getTime(timestamps.get(i), DATE_FORMAT, ".");
            addGlobalMetaList("timestamp", timestamp);
        }
        for (int i = 0; i < exposures.size(); i++) {
            addGlobalMetaList("exposure time (ms)", exposures.get(i).floatValue() * 1000);
        }
    }
    for (int s = 0; s < seriesCount; s++) {
        setSeries(s);
        Well well = getWell(s);
        String name = handler.getImageName();
        if (seriesCount > 1) {
            name = "Field " + (char) (well.fieldRow + 'A') + (well.fieldCol + 1) + ", Well " + (well.well + 1) + ": " + name;
        }
        store.setImageName(name, s);
        String date = DateTools.formatDate(handler.getDate(), DateTools.ISO8601_FORMAT, ".");
        if (date != null) {
            store.setImageAcquisitionDate(new Timestamp(date), s);
        }
        if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
            long startDate = 0;
            if (timestamps.size() > 0) {
                startDate = DateTools.getTime(timestamps.get(0), DATE_FORMAT, ".");
            }
            store.setImageDescription("", s);
            int image = 0;
            for (int c = 0; c < getEffectiveSizeC(); c++) {
                for (int t = 0; t < getSizeT(); t++) {
                    store.setPlaneTheZ(new NonNegativeInteger(0), s, image);
                    store.setPlaneTheC(new NonNegativeInteger(c), s, image);
                    store.setPlaneTheT(new NonNegativeInteger(t), s, image);
                    if (t < timestamps.size()) {
                        String stamp = timestamps.get(t);
                        long ms = DateTools.getTime(stamp, DATE_FORMAT, ".");
                        store.setPlaneDeltaT(new Time((ms - startDate) / 1000.0, UNITS.SECOND), s, image);
                    }
                    int exposureIndex = image;
                    if (dualCamera) {
                        exposureIndex /= getEffectiveSizeC();
                    }
                    if (exposures.size() == 1) {
                        exposureIndex = 0;
                    }
                    if (exposureIndex < exposures.size() && exposures.get(exposureIndex) != null) {
                        store.setPlaneExposureTime(new Time(exposures.get(exposureIndex), UNITS.SECOND), s, image);
                    }
                    if (s < stageX.size()) {
                        store.setPlanePositionX(stageX.get(s), s, image);
                    }
                    if (s < stageY.size()) {
                        store.setPlanePositionY(stageY.get(s), s, image);
                    }
                    image++;
                }
            }
            store.setImagingEnvironmentTemperature(new Temperature(handler.getTemperature(), UNITS.CELSIUS), s);
            Length sizeX = FormatTools.getPhysicalSizeX(handler.getPixelSizeX());
            Length sizeY = FormatTools.getPhysicalSizeY(handler.getPixelSizeY());
            Length sizeZ = physicalSizeZ == null ? null : FormatTools.getPhysicalSizeZ(physicalSizeZ);
            if (sizeX != null) {
                store.setPixelsPhysicalSizeX(sizeX, s);
            }
            if (sizeY != null) {
                store.setPixelsPhysicalSizeY(sizeY, s);
            }
            if (sizeZ != null) {
                store.setPixelsPhysicalSizeZ(sizeZ, s);
            }
            if (uniqueChannels.size() == 0) {
                uniqueChannels = handler.getChannelNames();
            }
            for (int c = 0; c < getEffectiveSizeC(); c++) {
                if (uniqueChannels.size() > c) {
                    store.setChannelName(uniqueChannels.get(c), s, c);
                } else
                    store.setChannelName(handler.getChannelName(), s, c);
                if (c < wavelengths.size()) {
                    store.setChannelEmissionWavelength(FormatTools.getEmissionWavelength(Double.valueOf(wavelengths.get(c))), s, c);
                }
            }
        }
    }
}
Also used : Temperature(ome.units.quantity.Temperature) IFD(loci.formats.tiff.IFD) ArrayList(java.util.ArrayList) Time(ome.units.quantity.Time) Timestamp(ome.xml.model.primitives.Timestamp) PositiveInteger(ome.xml.model.primitives.PositiveInteger) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) CoreMetadata(loci.formats.CoreMetadata) PositiveInteger(ome.xml.model.primitives.PositiveInteger) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length)

Example 64 with Timestamp

use of ome.xml.model.primitives.Timestamp in project bioformats by openmicroscopy.

the class NikonElementsTiffReader method initMetadataStore.

/* @see BaseTiffReader#initMetadataStore() */
@Override
protected void initMetadataStore() throws FormatException {
    super.initMetadataStore();
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, true);
    String date = handler.getDate();
    if (date != null) {
        store.setImageAcquisitionDate(new Timestamp(date), 0);
    }
    if (getMetadataOptions().getMetadataLevel() == MetadataLevel.MINIMUM) {
        return;
    }
    Length sizeX = FormatTools.getPhysicalSizeX(handler.getPixelSizeX());
    Length sizeY = FormatTools.getPhysicalSizeY(handler.getPixelSizeY());
    Length sizeZ = FormatTools.getPhysicalSizeZ(handler.getPixelSizeZ());
    if (sizeX != null) {
        store.setPixelsPhysicalSizeX(sizeX, 0);
    }
    if (sizeY != null) {
        store.setPixelsPhysicalSizeY(sizeY, 0);
    }
    if (sizeZ != null) {
        store.setPixelsPhysicalSizeZ(sizeZ, 0);
    }
    String instrument = MetadataTools.createLSID("Instrument", 0);
    store.setInstrumentID(instrument, 0);
    store.setImageInstrumentRef(instrument, 0);
    ArrayList<Double> exposureTimes = handler.getExposureTimes();
    ArrayList<Length> posX = handler.getXPositions();
    ArrayList<Length> posY = handler.getYPositions();
    ArrayList<Length> posZ = handler.getZPositions();
    for (int i = 0; i < getImageCount(); i++) {
        int c = getZCTCoords(i)[1];
        if (c < exposureTimes.size() && exposureTimes.get(c) != null) {
            store.setPlaneExposureTime(new Time(exposureTimes.get(c), UNITS.SECOND), 0, i);
        }
        if (i < posX.size()) {
            store.setPlanePositionX(posX.get(i), 0, i);
        }
        if (i < posY.size()) {
            store.setPlanePositionY(posY.get(i), 0, i);
        }
        if (i < posZ.size()) {
            store.setPlanePositionZ(posZ.get(i), 0, i);
        }
    }
    String detector = MetadataTools.createLSID("Detector", 0, 0);
    store.setDetectorID(detector, 0, 0);
    store.setDetectorModel(handler.getCameraModel(), 0, 0);
    store.setDetectorType(getDetectorType("Other"), 0, 0);
    ArrayList<String> channelNames = handler.getChannelNames();
    ArrayList<String> modality = handler.getModalities();
    ArrayList<String> binning = handler.getBinnings();
    ArrayList<Double> speed = handler.getSpeeds();
    ArrayList<Double> gain = handler.getGains();
    ArrayList<Double> temperature = handler.getTemperatures();
    ArrayList<Double> exWave = handler.getExcitationWavelengths();
    ArrayList<Double> emWave = handler.getEmissionWavelengths();
    ArrayList<Integer> power = handler.getPowers();
    ArrayList<Hashtable<String, String>> rois = handler.getROIs();
    Double pinholeSize = handler.getPinholeSize();
    for (int c = 0; c < getEffectiveSizeC(); c++) {
        if (pinholeSize != null) {
            store.setChannelPinholeSize(new Length(pinholeSize, UNITS.MICROMETER), 0, c);
        }
        if (c < channelNames.size()) {
            store.setChannelName(channelNames.get(c), 0, c);
        }
        if (c < modality.size()) {
            store.setChannelAcquisitionMode(getAcquisitionMode(modality.get(c)), 0, c);
        }
        if (c < emWave.size()) {
            Length em = FormatTools.getEmissionWavelength(emWave.get(c));
            if (em != null) {
                store.setChannelEmissionWavelength(em, 0, c);
            }
        }
        if (c < exWave.size()) {
            Length ex = FormatTools.getExcitationWavelength(exWave.get(c));
            if (ex != null) {
                store.setChannelExcitationWavelength(ex, 0, c);
            }
        }
        if (c < binning.size()) {
            store.setDetectorSettingsBinning(getBinning(binning.get(c)), 0, c);
        }
        if (c < gain.size()) {
            store.setDetectorSettingsGain(gain.get(c), 0, c);
        }
        if (c < speed.size()) {
            store.setDetectorSettingsReadOutRate(new Frequency(speed.get(c), UNITS.HERTZ), 0, c);
        }
        store.setDetectorSettingsID(detector, 0, c);
    }
    if (temperature.size() > 0) {
        store.setImagingEnvironmentTemperature(new Temperature(temperature.get(0), UNITS.CELSIUS), 0);
    }
    Double voltage = handler.getVoltage();
    if (voltage != null) {
        store.setDetectorSettingsVoltage(new ElectricPotential(voltage, UNITS.VOLT), 0, 0);
    }
    Double na = handler.getNumericalAperture();
    if (na != null)
        store.setObjectiveLensNA(na, 0, 0);
    Double mag = handler.getMagnification();
    if (mag != null)
        store.setObjectiveCalibratedMagnification(mag, 0, 0);
    store.setObjectiveModel(handler.getObjectiveModel(), 0, 0);
    String immersion = handler.getImmersion();
    if (immersion == null)
        immersion = "Other";
    store.setObjectiveImmersion(getImmersion(immersion), 0, 0);
    String correction = handler.getCorrection();
    if (correction == null || correction.length() == 0)
        correction = "Other";
    store.setObjectiveCorrection(getCorrection(correction), 0, 0);
    String objective = MetadataTools.createLSID("Objective", 0, 0);
    store.setObjectiveID(objective, 0, 0);
    store.setObjectiveSettingsID(objective, 0);
    Double refractiveIndex = handler.getRefractiveIndex();
    if (refractiveIndex != null) {
        store.setObjectiveSettingsRefractiveIndex(refractiveIndex, 0);
    }
    if (getMetadataOptions().getMetadataLevel() == MetadataLevel.NO_OVERLAYS) {
        return;
    }
    handler.populateROIs(store);
}
Also used : Temperature(ome.units.quantity.Temperature) Hashtable(java.util.Hashtable) Time(ome.units.quantity.Time) Timestamp(ome.xml.model.primitives.Timestamp) ElectricPotential(ome.units.quantity.ElectricPotential) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) Frequency(ome.units.quantity.Frequency)

Example 65 with Timestamp

use of ome.xml.model.primitives.Timestamp 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

Timestamp (ome.xml.model.primitives.Timestamp)76 MetadataStore (loci.formats.meta.MetadataStore)54 Length (ome.units.quantity.Length)52 CoreMetadata (loci.formats.CoreMetadata)44 Time (ome.units.quantity.Time)33 Location (loci.common.Location)28 RandomAccessInputStream (loci.common.RandomAccessInputStream)28 FormatException (loci.formats.FormatException)23 ArrayList (java.util.ArrayList)20 IFD (loci.formats.tiff.IFD)11 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)10 TiffParser (loci.formats.tiff.TiffParser)9 IOException (java.io.IOException)8 IFDList (loci.formats.tiff.IFDList)8 PositiveInteger (ome.xml.model.primitives.PositiveInteger)8 Temperature (ome.units.quantity.Temperature)7 Hashtable (java.util.Hashtable)4 StringTokenizer (java.util.StringTokenizer)4 Frequency (ome.units.quantity.Frequency)4 File (java.io.File)3