use of ome.xml.model.primitives.Timestamp 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;
}
use of ome.xml.model.primitives.Timestamp in project bioformats by openmicroscopy.
the class ZeissLSMReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
if (checkSuffix(id, MDB_SUFFIX)) {
lsmFilenames = parseMDB(id);
} else
lsmFilenames = new String[] { id };
if (lsmFilenames == null || lsmFilenames.length == 0) {
throw new FormatException("LSM files were not found.");
}
totalROIs = 0;
timestamps = new ArrayList<Double>();
imageNames = new ArrayList<String>();
xCoordinates = new ArrayList<Double>();
yCoordinates = new ArrayList<Double>();
zCoordinates = new ArrayList<Double>();
seriesCounts = new HashMap<String, Integer>();
int seriesCount = 0;
final List<String> validFiles = new ArrayList<String>();
for (String filename : lsmFilenames) {
try {
int extraSeries = getExtraSeries(filename);
seriesCounts.put(filename, extraSeries);
seriesCount += extraSeries;
validFiles.add(filename);
} catch (IOException e) {
LOGGER.debug("Failed to parse " + filename, e);
}
}
lsmFilenames = validFiles.toArray(new String[validFiles.size()]);
core.clear();
for (int c = 0; c < seriesCount; c++) {
CoreMetadata ms = new CoreMetadata();
core.add(ms);
}
channelNames = new String[seriesCount][];
ifdsList = new ArrayList<IFDList>();
for (int series = 0; series < seriesCount; series++) {
ifdsList.add(null);
}
int realSeries = 0;
for (int i = 0; i < lsmFilenames.length; i++) {
RandomAccessInputStream stream = null;
try {
stream = new RandomAccessInputStream(lsmFilenames[i], 16);
int count = seriesCounts.get(lsmFilenames[i]);
TiffParser tp = new TiffParser(stream);
Boolean littleEndian = tp.checkHeader();
long[] ifdOffsets = tp.getIFDOffsets();
int ifdsPerSeries = (ifdOffsets.length / 2) / count;
int offset = 0;
Object zeissTag = null;
for (int s = 0; s < count; s++, realSeries++) {
CoreMetadata ms = core.get(realSeries);
ms.littleEndian = littleEndian;
IFDList ifds = new IFDList();
while (ifds.size() < ifdsPerSeries) {
tp.setDoCaching(offset == 0);
IFD ifd = tp.getIFD(ifdOffsets[offset]);
if (offset == 0)
zeissTag = ifd.get(ZEISS_ID);
if (offset > 0 && ifds.isEmpty()) {
ifd.putIFDValue(ZEISS_ID, zeissTag);
}
ifds.add(ifd);
if (zeissTag != null)
offset += 2;
else
offset++;
}
for (IFD ifd : ifds) {
tp.fillInIFD(ifd);
}
ifdsList.set(realSeries, ifds);
}
} catch (IOException e) {
throw e;
} finally {
if (stream != null)
stream.close();
}
}
MetadataStore store = makeFilterMetadata();
lut = new byte[ifdsList.size()][][];
long[] previousStripOffsets = null;
for (int series = 0; series < ifdsList.size(); series++) {
// the previous series
if (series > 0 && getSizeT() > 1) {
previousStripOffsets = null;
}
IFDList ifds = ifdsList.get(series);
for (IFD ifd : ifds) {
// than LZW compression is used
if (ifd.getCompression() != TiffCompression.LZW) {
ifd.putIFDValue(IFD.PREDICTOR, 1);
}
}
// fix the offsets for > 4 GB files
RandomAccessInputStream s = null;
try {
s = new RandomAccessInputStream(getLSMFileFromSeries(series));
for (int i = 0; i < ifds.size(); i++) {
long[] stripOffsets = ifds.get(i).getStripOffsets();
if (stripOffsets == null || (i != 0 && previousStripOffsets == null)) {
throw new FormatException("Strip offsets are missing; this is an invalid file.");
} else if (i == 0 && previousStripOffsets == null) {
previousStripOffsets = stripOffsets;
continue;
}
boolean neededAdjustment = false;
for (int j = 0; j < stripOffsets.length; j++) {
if (j >= previousStripOffsets.length)
break;
if (stripOffsets[j] < previousStripOffsets[j]) {
stripOffsets[j] = (previousStripOffsets[j] & ~0xffffffffL) | (stripOffsets[j] & 0xffffffffL);
if (stripOffsets[j] < previousStripOffsets[j]) {
long newOffset = stripOffsets[j] + 0x100000000L;
if (newOffset < s.length()) {
stripOffsets[j] = newOffset;
}
}
neededAdjustment = true;
}
if (neededAdjustment) {
ifds.get(i).putIFDValue(IFD.STRIP_OFFSETS, stripOffsets);
}
}
previousStripOffsets = stripOffsets;
}
initMetadata(series);
} catch (IOException e) {
throw e;
} finally {
if (s != null)
s.close();
}
}
for (int i = 0; i < getSeriesCount(); i++) {
CoreMetadata ms = core.get(i);
ms.imageCount = ms.sizeZ * ms.sizeC * ms.sizeT;
}
MetadataTools.populatePixels(store, this, true);
for (int series = 0; series < ifdsList.size(); series++) {
setSeries(series);
if (series < imageNames.size()) {
store.setImageName(imageNames.get(series), series);
}
if (acquiredDate.containsKey(series)) {
store.setImageAcquisitionDate(new Timestamp(acquiredDate.get(series)), series);
}
}
setSeries(0);
}
use of ome.xml.model.primitives.Timestamp in project bioformats by openmicroscopy.
the class SimplePCITiffReader method initMetadataStore.
/* @see BaseTiffReader#initMetadataStore() */
@Override
protected void initMetadataStore() throws FormatException {
super.initMetadataStore();
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this, true);
if (date != null) {
date = DateTools.formatDate(date, DATE_FORMAT);
if (date != null) {
store.setImageAcquisitionDate(new Timestamp(date), 0);
}
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
store.setImageDescription(MAGIC_STRING, 0);
Length sizeX = FormatTools.getPhysicalSizeX(scaling);
Length sizeY = FormatTools.getPhysicalSizeY(scaling);
if (sizeX != null) {
store.setPixelsPhysicalSizeX(sizeX, 0);
}
if (sizeY != null) {
store.setPixelsPhysicalSizeY(sizeY, 0);
}
String instrument = MetadataTools.createLSID("Instrument", 0);
store.setInstrumentID(instrument, 0);
store.setImageInstrumentRef(instrument, 0);
store.setObjectiveID(MetadataTools.createLSID("Objective", 0, 0), 0, 0);
store.setObjectiveNominalMagnification(magnification, 0, 0);
store.setObjectiveImmersion(getImmersion(immersion), 0, 0);
String detector = MetadataTools.createLSID("Detector", 0, 0);
store.setDetectorID(detector, 0, 0);
store.setDetectorModel(cameraType + " " + cameraName, 0, 0);
store.setDetectorType(getDetectorType("CCD"), 0, 0);
for (int i = 0; i < getSizeC(); i++) {
store.setDetectorSettingsID(detector, 0, i);
store.setDetectorSettingsBinning(getBinning(binning), 0, i);
}
for (int i = 0; i < getImageCount(); i++) {
int[] zct = getZCTCoords(i);
if (zct[1] < exposureTimes.size() && exposureTimes.get(zct[1]) != null) {
store.setPlaneExposureTime(new Time(exposureTimes.get(zct[1]) / 1000000, UNITS.SECOND), 0, i);
}
}
}
}
use of ome.xml.model.primitives.Timestamp in project bioformats by openmicroscopy.
the class SpiderReader 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.littleEndian = true;
in.order(isLittleEndian());
int nSlice = (int) in.readFloat();
m.littleEndian = nSlice > 0;
if (!isLittleEndian()) {
in.order(isLittleEndian());
in.seek(0);
nSlice = (int) in.readFloat();
}
int nRow = (int) in.readFloat();
int irec = (int) in.readFloat();
in.skipBytes(4);
int iform = (int) in.readFloat();
int imami = (int) in.readFloat();
float fmax = in.readFloat();
float fmin = in.readFloat();
float average = in.readFloat();
float sig = in.readFloat();
in.skipBytes(4);
int nsam = (int) in.readFloat();
int labrec = (int) in.readFloat();
headerSize = (long) labrec * nsam * 4;
int iAngle = (int) in.readFloat();
float phi = in.readFloat();
float theta = in.readFloat();
float gamma = in.readFloat();
float xOff = in.readFloat();
float yOff = in.readFloat();
float zOff = in.readFloat();
float scale = in.readFloat();
int labbyte = (int) in.readFloat();
int lenbyte = (int) in.readFloat();
int istack = (int) in.readFloat();
in.skipBytes(4);
float maxim = in.readFloat();
float imgnum = in.readFloat();
float lastIndx = in.readFloat();
in.skipBytes(8);
float kAngle = in.readFloat();
float phi1 = in.readFloat();
float theta1 = in.readFloat();
float psi1 = in.readFloat();
float phi2 = in.readFloat();
float theta2 = in.readFloat();
float psi2 = in.readFloat();
// in angstroms
float pixelSize = in.readFloat();
float ev = in.readFloat();
in.skipBytes(4 * 61);
float psi3 = in.readFloat();
float theta3 = in.readFloat();
float phi3 = in.readFloat();
float lAngle = in.readFloat();
in.skipBytes(4 * 107);
String creationDate = in.readString(12).trim();
String creationTime = in.readString(8);
String title = in.readString(160);
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
addGlobalMeta("NSLICE", nSlice);
addGlobalMeta("NROW", nRow);
addGlobalMeta("IREC", irec);
addGlobalMeta("IFORM", iform);
addGlobalMeta("IMAMI", imami);
addGlobalMeta("FMAX", fmax);
addGlobalMeta("FMIN", fmin);
addGlobalMeta("AV", average);
addGlobalMeta("SIG", sig);
addGlobalMeta("NSAM", nsam);
addGlobalMeta("LABREC", labrec);
addGlobalMeta("IANGLE", iAngle);
addGlobalMeta("PHI", phi);
addGlobalMeta("THETA", theta);
addGlobalMeta("GAMMA", gamma);
addGlobalMeta("XOFF", xOff);
addGlobalMeta("YOFF", yOff);
addGlobalMeta("ZOFF", zOff);
addGlobalMeta("SCALE", scale);
addGlobalMeta("LABBYT", labbyte);
addGlobalMeta("LENBYT", lenbyte);
addGlobalMeta("ISTACK/MAXINDX", istack);
addGlobalMeta("MAXIM", maxim);
addGlobalMeta("IMGNUM", imgnum);
addGlobalMeta("LASTINDX", lastIndx);
addGlobalMeta("KANGLE", kAngle);
addGlobalMeta("PHI1", phi1);
addGlobalMeta("THETA1", theta1);
addGlobalMeta("PSI1", psi1);
addGlobalMeta("PHI2", phi2);
addGlobalMeta("THETA2", theta2);
addGlobalMeta("PSI2", psi2);
addGlobalMeta("PIXSIZ", pixelSize);
addGlobalMeta("EV", ev);
addGlobalMeta("PHI3", phi3);
addGlobalMeta("THETA3", theta3);
addGlobalMeta("PSI3", psi3);
addGlobalMeta("LANGLE", lAngle);
addGlobalMeta("CDAT", creationDate);
addGlobalMeta("CTIM", creationTime);
addGlobalMeta("CTIT", title);
}
m.imageCount = (int) Math.max(nSlice, 1);
if (maxim > 0) {
m.imageCount *= maxim;
}
m.sizeZ = getImageCount();
m.sizeC = 1;
m.sizeT = 1;
m.sizeY = nRow;
m.sizeX = nsam;
m.pixelType = FormatTools.FLOAT;
m.dimensionOrder = "XYZCT";
m.rgb = false;
long planeSize = FormatTools.getPlaneSize(this);
oneHeaderPerSlice = (irec * nsam * 4) != planeSize && ((irec - 1) * 4) != planeSize;
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
store.setImageName(title, 0);
String date = creationDate + " " + creationTime;
date = DateTools.formatDate(date, DATE_FORMAT);
if (date != null) {
store.setImageAcquisitionDate(new Timestamp(date), 0);
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
Double size = new Double(pixelSize);
Length sizeX = FormatTools.getPhysicalSizeX(size, UNITS.ANGSTROM);
Length sizeY = FormatTools.getPhysicalSizeY(size, UNITS.ANGSTROM);
if (sizeX != null) {
store.setPixelsPhysicalSizeX(sizeX, 0);
}
if (sizeY != null) {
store.setPixelsPhysicalSizeY(sizeY, 0);
}
}
}
use of ome.xml.model.primitives.Timestamp in project bioformats by openmicroscopy.
the class TopometrixReader 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.littleEndian = true;
in.order(isLittleEndian());
in.skipBytes(2);
int version = (int) Double.parseDouble(in.readString(4));
in.skipBytes(2);
pixelOffset = Long.parseLong(in.readString(4));
in.skipBytes(2);
long fp = in.getFilePointer();
String date = in.readLine().trim();
int commentLength = (int) (240 - in.getFilePointer() + fp);
String comment = in.readString(commentLength).trim();
if (version == 5) {
in.seek(452);
}
in.skipBytes(152);
m.sizeX = in.readShort();
in.skipBytes(2);
m.sizeY = in.readShort();
double xSize = 0d, ySize = 0d;
double adc = 0d, dacToWorldZero = 0d;
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
in.skipBytes(10);
if (version == 5) {
in.skipBytes(4);
xSize = in.readDouble();
in.skipBytes(8);
ySize = in.readDouble();
adc = in.readDouble();
dacToWorldZero = in.readDouble();
in.skipBytes(1176);
double sampleVolts = in.readDouble();
double tunnelCurrent = in.readDouble();
in.skipBytes(16);
double timePerPixel = in.readDouble();
in.skipBytes(40);
double scanAngle = in.readDouble();
addGlobalMeta("Sample volts", sampleVolts);
addGlobalMeta("Tunnel current", tunnelCurrent);
addGlobalMeta("Scan rate", timePerPixel);
addGlobalMeta("Scan angle", scanAngle);
} else {
xSize = in.readFloat();
in.skipBytes(4);
ySize = in.readFloat();
adc = in.readFloat();
in.skipBytes(764);
dacToWorldZero = in.readFloat();
}
addGlobalMeta("Version", version);
addGlobalMeta("X size (in um)", xSize);
addGlobalMeta("Y size (in um)", ySize);
addGlobalMeta("ADC", adc);
addGlobalMeta("DAC to world zero", dacToWorldZero);
addGlobalMeta("Comment", comment);
addGlobalMeta("Acquisition date", date);
}
m.pixelType = FormatTools.UINT16;
m.sizeZ = 1;
m.sizeC = 1;
m.sizeT = 1;
m.imageCount = 1;
m.dimensionOrder = "XYZCT";
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
date = DateTools.formatDate(date, new String[] { "MM/dd/yy HH:mm:ss", "MM/dd/yyyy HH:mm:ss" });
if (date != null) {
store.setImageAcquisitionDate(new Timestamp(date), 0);
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
Length sizeX = FormatTools.getPhysicalSizeX((double) xSize / getSizeX());
Length sizeY = FormatTools.getPhysicalSizeY((double) ySize / getSizeY());
if (sizeX != null) {
store.setPixelsPhysicalSizeX(sizeX, 0);
}
if (sizeY != null) {
store.setPixelsPhysicalSizeY(sizeY, 0);
}
store.setImageDescription(comment, 0);
}
}
Aggregations