use of ome.units.quantity.Length in project bioformats by openmicroscopy.
the class FormatReaderTest method testExcitationWavelengths.
@Test(groups = { "all", "fast", "automated" })
public void testExcitationWavelengths() {
if (config == null)
throw new SkipException("No config tree");
String testName = "ExcitationWavelengths";
if (!initFile())
result(testName, false, "initFile");
IMetadata retrieve = (IMetadata) reader.getMetadataStore();
for (int i = 0; i < reader.getSeriesCount(); i++) {
config.setSeries(i);
for (int c = 0; c < config.getChannelCount(); c++) {
Length realWavelength = retrieve.getChannelExcitationWavelength(i, c);
Length expectedWavelength = config.getExcitationWavelength(c);
if (!isAlmostEqual(expectedWavelength, realWavelength)) {
result(testName, false, "Series " + i + " channel " + c + " (expected " + expectedWavelength + ", actual " + realWavelength + ")");
}
}
}
result(testName, true);
}
use of ome.units.quantity.Length in project bioformats by openmicroscopy.
the class OrthogonalReader method initialiseWriter.
private OMETiffWriter initialiseWriter(String fileName, ImageReader reader) throws Exception {
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
IMetadata metadata = service.createOMEXMLMetadata();
MetadataRetrieve mr = (MetadataRetrieve) reader.getMetadataStore();
Length originalSizeX = mr.getPixelsPhysicalSizeX(0);
Length originalSizeY = mr.getPixelsPhysicalSizeY(0);
Length originalSizeZ = mr.getPixelsPhysicalSizeZ(0);
// Original XY planes
// XZ planes
MetadataTools.populateMetadata(metadata, 0, "XZ", reader.isLittleEndian(), reader.getDimensionOrder(), FormatTools.getPixelTypeString(reader.getPixelType()), reader.getSizeX(), reader.getSizeZ(), reader.getSizeY(), 1, 1, 1);
metadata.setPixelsPhysicalSizeX(originalSizeX, 0);
metadata.setPixelsPhysicalSizeY(originalSizeZ, 0);
metadata.setPixelsPhysicalSizeZ(originalSizeY, 0);
// YZ planes
MetadataTools.populateMetadata(metadata, 1, "YZ", reader.isLittleEndian(), reader.getDimensionOrder(), FormatTools.getPixelTypeString(reader.getPixelType()), reader.getSizeY(), reader.getSizeZ(), reader.getSizeX(), 1, 1, 1);
metadata.setPixelsPhysicalSizeX(originalSizeY, 1);
metadata.setPixelsPhysicalSizeY(originalSizeZ, 1);
metadata.setPixelsPhysicalSizeZ(originalSizeX, 1);
OMETiffWriter writer = new OMETiffWriter();
writer.setMetadataRetrieve(metadata);
writer.setId(fileName);
return writer;
}
use of ome.units.quantity.Length in project bioformats by openmicroscopy.
the class OBFReader method initFile.
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
currentInflatedFrame.series = -1;
currentInflatedFrame.number = -1;
in = new RandomAccessInputStream(id);
file_version = getFileVersion(in);
long stackPosition = in.readLong();
final int lengthOfDescription = in.readInt();
final String description = in.readString(lengthOfDescription);
metadata.put("Description", description);
if (file_version >= 2) {
final long meta_data_position = in.readLong();
final long current_position = in.getFilePointer();
in.seek(meta_data_position);
for (String key = readString(); key.length() > 0; key = readString()) {
if (key.equals("ome_xml")) {
final String ome_xml = readString();
try {
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
if (service.validateOMEXML(ome_xml)) {
ome_meta_data = service.createOMEXMLMetadata(ome_xml);
for (int image = 0; image != ome_meta_data.getImageCount(); ++image) {
if (ome_meta_data.getPixelsBigEndian(image) == null) {
ome_meta_data.setPixelsBigEndian(Boolean.FALSE, image);
}
int channels = ome_meta_data.getChannelCount(image);
for (int channel = 0; channel != channels; ++channel) {
if (ome_meta_data.getChannelSamplesPerPixel(image, channel) == null) {
ome_meta_data.setChannelSamplesPerPixel(new PositiveInteger(1), image, channel);
}
}
}
service.convertMetadata(ome_meta_data, metadataStore);
OMEXMLMetadata reference = service.getOMEMetadata(service.asRetrieve(metadataStore));
for (int image = 0; image != ome_meta_data.getImageCount(); ++image) {
service.addMetadataOnly(reference, image);
}
}
} catch (DependencyException exception) {
throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, exception);
} catch (ServiceException exception) {
throw new FormatException(exception);
} catch (Exception e) {
LOGGER.warn("Could not parse OME-XML metadata", e);
}
break;
} else {
addGlobalMeta(key, readString());
}
}
in.seek(current_position);
}
if (stackPosition != 0) {
core.clear();
do {
stackPosition = initStack(stackPosition);
} while (stackPosition != 0);
}
if (ome_meta_data == null) {
MetadataTools.populatePixels(metadataStore, this);
for (int image = 0; image != core.size(); ++image) {
CoreMetadata meta_data = core.get(image);
final String name = meta_data.seriesMetadata.get("Name").toString();
metadataStore.setImageName(name, image);
@SuppressWarnings("unchecked") final List<Double> lengths = (List<Double>) meta_data.seriesMetadata.get("Lengths");
if (lengths.size() > 0) {
double lengthX = Math.abs(lengths.get(0));
if (lengthX < 0.01) {
lengthX *= 1000000;
}
if (lengthX > 0) {
Length physicalSizeX = FormatTools.getPhysicalSizeX(lengthX / meta_data.sizeX, UNITS.MICROMETER);
if (physicalSizeX != null) {
metadataStore.setPixelsPhysicalSizeX(physicalSizeX, image);
}
}
}
if (lengths.size() > 1) {
double lengthY = Math.abs(lengths.get(1));
if (lengthY < 0.01) {
lengthY *= 1000000;
}
if (lengthY > 0) {
Length physicalSizeY = FormatTools.getPhysicalSizeY(lengthY / meta_data.sizeY, UNITS.MICROMETER);
if (physicalSizeY != null) {
metadataStore.setPixelsPhysicalSizeY(physicalSizeY, image);
}
}
}
if (lengths.size() > 2) {
double lengthZ = Math.abs(lengths.get(2));
if (lengthZ < 0.01) {
lengthZ *= 1000000;
}
if (lengthZ > 0) {
Length physicalSizeZ = FormatTools.getPhysicalSizeZ(lengthZ / meta_data.sizeZ, UNITS.MICROMETER);
if (physicalSizeZ != null) {
metadataStore.setPixelsPhysicalSizeZ(physicalSizeZ, image);
}
}
}
}
}
}
use of ome.units.quantity.Length 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);
}
}
}
use of ome.units.quantity.Length in project bioformats by openmicroscopy.
the class NRRDReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
// make sure we actually have the .nrrd/.nhdr file
if (!checkSuffix(id, "nhdr") && !checkSuffix(id, "nrrd")) {
id += ".nhdr";
if (!new Location(id).exists()) {
id = id.substring(0, id.lastIndexOf("."));
id = id.substring(0, id.lastIndexOf("."));
id += ".nhdr";
}
id = new Location(id).getAbsolutePath();
}
super.initFile(id);
in = new RandomAccessInputStream(id);
ClassList<IFormatReader> classes = ImageReader.getDefaultReaderClasses();
Class<? extends IFormatReader>[] classArray = classes.getClasses();
ClassList<IFormatReader> newClasses = new ClassList<IFormatReader>(IFormatReader.class);
for (Class<? extends IFormatReader> c : classArray) {
if (!c.equals(NRRDReader.class)) {
newClasses.addClass(c);
}
}
helper = new ImageReader(newClasses);
helper.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.MINIMUM));
String key, v;
String[] pixelSizeUnits = null;
int numDimensions = 0;
CoreMetadata m = core.get(0);
m.sizeX = 1;
m.sizeY = 1;
m.sizeZ = 1;
m.sizeC = 1;
m.sizeT = 1;
m.dimensionOrder = "XYCZT";
String line = in.readLine();
while (line != null && line.length() > 0) {
if (!line.startsWith("#") && !line.startsWith("NRRD")) {
// parse key/value pair
key = line.substring(0, line.indexOf(':')).trim();
v = line.substring(line.indexOf(':') + 1).trim();
addGlobalMeta(key, v);
if (key.equals("type")) {
if (v.indexOf("char") != -1 || v.indexOf('8') != -1) {
m.pixelType = FormatTools.UINT8;
} else if (v.indexOf("short") != -1 || v.indexOf("16") != -1) {
m.pixelType = FormatTools.UINT16;
} else if (v.equals("int") || v.equals("signed int") || v.equals("int32") || v.equals("int32_t") || v.equals("uint") || v.equals("unsigned int") || v.equals("uint32") || v.equals("uint32_t")) {
m.pixelType = FormatTools.UINT32;
} else if (v.equals("float"))
m.pixelType = FormatTools.FLOAT;
else if (v.equals("double"))
m.pixelType = FormatTools.DOUBLE;
else
throw new FormatException("Unsupported data type: " + v);
} else if (key.equals("dimension")) {
numDimensions = Integer.parseInt(v);
} else if (key.equals("sizes")) {
String[] tokens = v.split(" ");
for (int i = 0; i < numDimensions; i++) {
int size = Integer.parseInt(tokens[i]);
if (numDimensions >= 3 && i == 0 && size > 1 && size <= 16) {
m.sizeC = size;
} else if (i == 0 || (getSizeC() > 1 && i == 1)) {
m.sizeX = size;
} else if (i == 1 || (getSizeC() > 1 && i == 2)) {
m.sizeY = size;
} else if (i == 2 || (getSizeC() > 1 && i == 3)) {
m.sizeZ = size;
} else if (i == 3 || (getSizeC() > 1 && i == 4)) {
m.sizeT = size;
}
}
} else if (key.equals("data file") || key.equals("datafile")) {
dataFile = v;
} else if (key.equals("encoding"))
encoding = v;
else if (key.equals("endian")) {
m.littleEndian = v.equals("little");
} else if (key.equals("spacings") || key.equals("space directions")) {
pixelSizes = v.split(" ");
} else if (key.equals("space units")) {
pixelSizeUnits = v.split(" ");
} else if (key.equals("byte skip")) {
offset = Long.parseLong(v);
}
}
line = in.readLine();
if (line != null)
line = line.trim();
}
if (dataFile == null)
offset = in.getFilePointer();
else {
Location f = new Location(currentId).getAbsoluteFile();
Location parent = f.getParentFile();
if (f.exists() && parent != null) {
dataFile = dataFile.substring(dataFile.indexOf(File.separator) + 1);
dataFile = new Location(parent, dataFile).getAbsolutePath();
}
initializeHelper = !encoding.equals("raw");
}
m.rgb = getSizeC() > 1;
m.interleaved = true;
m.imageCount = getSizeZ() * getSizeT();
m.indexed = false;
m.falseColor = false;
m.metadataComplete = true;
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
if (pixelSizes != null) {
for (int i = 0; i < pixelSizes.length; i++) {
if (pixelSizes[i] == null)
continue;
try {
Double d = parsePixelSize(i);
String unit = pixelSizeUnits == null || i >= pixelSizeUnits.length ? null : pixelSizeUnits[i].replaceAll("\"", "");
if (i == 0) {
Length x = FormatTools.getPhysicalSizeX(d, unit);
if (x != null) {
store.setPixelsPhysicalSizeX(x, 0);
}
} else if (i == 1) {
Length y = FormatTools.getPhysicalSizeY(d, unit);
if (y != null) {
store.setPixelsPhysicalSizeY(y, 0);
}
} else if (i == 2) {
Length z = FormatTools.getPhysicalSizeZ(d, unit);
if (z != null) {
store.setPixelsPhysicalSizeZ(z, 0);
}
}
} catch (NumberFormatException e) {
}
}
}
}
}
Aggregations