use of loci.common.services.ServiceException in project bioformats by openmicroscopy.
the class FileWriteSPW method initializeMetadata.
/**
* Populate the minimum amount of metadata required to export a Plate.
*/
private IMetadata initializeMetadata(int[][] nFovs) {
Exception exception = null;
try {
// create the OME-XML metadata storage object
ServiceFactory factory = new ServiceFactory();
service = factory.getInstance(OMEXMLService.class);
OMEXMLMetadata meta = service.createOMEXMLMetadata();
// IMetadata meta = service.createOMEXMLMetadata();
meta.createRoot();
int plateIndex = 0;
// count of images
int series = 0;
int well = 0;
meta.setPlateDescription(plateDescription, 0);
meta.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
meta.setPlateRowNamingConvention(NamingConvention.LETTER, 0);
meta.setPlateColumnNamingConvention(NamingConvention.NUMBER, 0);
meta.setPlateRows(new PositiveInteger(rows), 0);
meta.setPlateColumns(new PositiveInteger(cols), 0);
meta.setPlateName("First test Plate", 0);
PositiveInteger pwidth = new PositiveInteger(width);
PositiveInteger pheight = new PositiveInteger(height);
char rowChar = 'A';
for (int row = 0; row < rows; row++) {
for (int column = 0; column < cols; column++) {
// set up well
String wellID = MetadataTools.createLSID("Well", well);
meta.setWellID(wellID, plateIndex, well);
meta.setWellRow(new NonNegativeInteger(row), plateIndex, well);
meta.setWellColumn(new NonNegativeInteger(column), plateIndex, well);
int nFOV = nFovs[row][column];
for (int fov = 0; fov < nFOV; fov++) {
// Create Image NB numberng in the Name goes from 1->n not 0-> n-1
String imageName = rowChar + ":" + Integer.toString(column + 1) + ":FOV:" + Integer.toString(fov + 1);
String imageID = MetadataTools.createLSID("Image", well, fov);
meta.setImageID(imageID, series);
meta.setImageName(imageName, series);
String pixelsID = MetadataTools.createLSID("Pixels", well, fov);
meta.setPixelsID(pixelsID, series);
// specify that the pixel data is stored in big-endian format
// change 'TRUE' to 'FALSE' to specify little-endian format
meta.setPixelsBigEndian(Boolean.TRUE, series);
// specify that the image is stored in ZCT order
meta.setPixelsDimensionOrder(DimensionOrder.XYZCT, series);
// specify the pixel type of the image
meta.setPixelsType(PixelType.fromString(FormatTools.getPixelTypeString(pixelType)), series);
// specify the dimensions of the image
meta.setPixelsSizeX(pwidth, series);
meta.setPixelsSizeY(pheight, series);
meta.setPixelsSizeZ(new PositiveInteger(1), series);
meta.setPixelsSizeC(new PositiveInteger(1), series);
meta.setPixelsSizeT(new PositiveInteger(sizet), series);
// define each channel and specify the number of samples in the channel
// the number of samples is 3 for RGB images and 1 otherwise
String channelID = MetadataTools.createLSID("Channel", well, fov);
meta.setChannelID(channelID, series, 0);
meta.setChannelSamplesPerPixel(new PositiveInteger(1), series, 0);
// set sample
String wellSampleID = MetadataTools.createLSID("WellSample", well, fov);
meta.setWellSampleID(wellSampleID, 0, well, fov);
// NB sampleIndex here == series ie the image No
meta.setWellSampleIndex(new NonNegativeInteger(series), 0, well, fov);
meta.setWellSampleImageRef(imageID, 0, well, fov);
if (exposureTimes != null && exposureTimes.length == sizet) {
for (int t = 0; t < sizet; t++) {
meta.setPlaneTheT(new NonNegativeInteger(t), series, t);
meta.setPlaneTheC(new NonNegativeInteger(0), series, t);
meta.setPlaneTheZ(new NonNegativeInteger(0), series, t);
meta.setPlaneExposureTime(new Time(exposureTimes[t], ome.units.UNITS.SECOND), series, t);
}
}
// add FLIM ModuloAlongT annotation if required
if (delays != null) {
CoreMetadata modlo = createModuloAnn(meta);
service.addModuloAlong(meta, modlo, series);
}
series++;
}
// end of samples
well++;
}
rowChar++;
}
expectedImages = new int[series];
// System.out.println(dump);
return meta;
} catch (DependencyException | ServiceException | EnumerationException e) {
exception = e;
}
System.err.println("Failed to populate OME-XML metadata object.");
return null;
}
use of loci.common.services.ServiceException in project bioformats by openmicroscopy.
the class MultiFileExportExample method main.
public static void main(String[] args) throws FormatException, IOException {
if (args.length < 2) {
System.out.println("Usage: java MultiFileExportExample <infile> <output file extension>");
System.exit(1);
}
ImageReader reader = new ImageReader();
IMetadata metadata;
try {
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
metadata = service.createOMEXMLMetadata();
} catch (DependencyException exc) {
throw new FormatException("Could not create OME-XML store.", exc);
} catch (ServiceException exc) {
throw new FormatException("Could not create OME-XML store.", exc);
}
reader.setMetadataStore(metadata);
reader.setId(args[0]);
ImageWriter writer = new ImageWriter();
writer.setMetadataRetrieve(metadata);
String baseFile = args[0].substring(0, args[0].lastIndexOf("."));
writer.setId(baseFile + "_s0_z0" + args[1]);
for (int series = 0; series < reader.getSeriesCount(); series++) {
reader.setSeries(series);
writer.setSeries(series);
int planesPerFile = reader.getImageCount() / reader.getSizeZ();
for (int z = 0; z < reader.getSizeZ(); z++) {
String file = baseFile + "_s" + series + "_z" + z + args[1];
writer.changeOutputFile(file);
for (int image = 0; image < planesPerFile; image++) {
int[] zct = FormatTools.getZCTCoords(reader.getDimensionOrder(), 1, reader.getEffectiveSizeC(), reader.getSizeT(), planesPerFile, image);
int index = FormatTools.getIndex(reader, z, zct[1], zct[2]);
writer.saveBytes(image, reader.openBytes(index));
}
}
}
reader.close();
writer.close();
}
use of loci.common.services.ServiceException in project bioformats by openmicroscopy.
the class VeecoReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
CoreMetadata m = core.get(0);
try {
ServiceFactory factory = new ServiceFactory();
netcdf = factory.getInstance(NetCDFService.class);
netcdf.setFile(id);
} catch (DependencyException e) {
throw new MissingLibraryException(e);
}
Vector<String> variableList = netcdf.getVariableList();
if (variableList.size() == 0) {
throw new FormatException("No image data found");
}
String imageName = variableList.get(0);
try {
image = netcdf.getVariableValue(imageName);
} catch (ServiceException e) {
throw new FormatException("Could not retrieve image data", e);
}
Hashtable<String, Object> attributes = netcdf.getVariableAttributes(imageName);
for (String attr : attributes.keySet()) {
addGlobalMeta(attr, attributes.get(attr));
}
if (image instanceof byte[][]) {
byte[][] byteImage = (byte[][]) image;
m.sizeX = byteImage[0].length;
m.sizeY = byteImage.length;
m.pixelType = FormatTools.INT8;
} else if (image instanceof short[][]) {
short[][] shortImage = (short[][]) image;
m.sizeX = shortImage[0].length;
m.sizeY = shortImage.length;
m.pixelType = FormatTools.INT16;
// set the endianness to use when unpacking pixels
// NetCDF may not return the pixels with a constant endianness,
// so this ensures that the reader corrects accordingly (see ticket 12085)
short nativeMin = 0;
short nativeMax = 0;
short swappedMin = 0;
short swappedMax = 0;
for (int y = 0; y < shortImage.length; y++) {
for (int x = 0; x < shortImage[y].length; x++) {
if (shortImage[y][x] < nativeMin) {
nativeMin = shortImage[y][x];
}
if (shortImage[y][x] > nativeMax) {
nativeMax = shortImage[y][x];
}
short swapped = DataTools.swap(shortImage[y][x]);
if (swapped < swappedMin) {
swappedMin = swapped;
}
if (swapped > swappedMax) {
swappedMax = swapped;
}
}
}
unpackEndian = nativeMin <= swappedMin && nativeMax >= swappedMax;
}
m.sizeZ = 1;
m.sizeC = 1;
m.sizeT = 1;
m.imageCount = m.sizeZ * m.sizeC * m.sizeT;
m.dimensionOrder = "XYCZT";
m.littleEndian = false;
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
}
use of loci.common.services.ServiceException in project bioformats by openmicroscopy.
the class DisplayHandler method displayOMEXML.
/**
* Displays OME-XML metadata in a tree in its own window.
*/
public XMLWindow displayOMEXML() throws FormatException, IOException {
if (!options.isShowOMEXML())
return null;
XMLWindow metaWindow = null;
metaWindow = new XMLWindow("OME Metadata - " + process.getIdName());
Exception exc = null;
try {
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
metaWindow.setXML(service.getOMEXML(process.getOMEMetadata()));
WindowTools.placeWindow(metaWindow);
metaWindow.setVisible(true);
} catch (DependencyException e) {
exc = e;
} catch (ServiceException e) {
exc = e;
} catch (ParserConfigurationException e) {
exc = e;
} catch (SAXException e) {
exc = e;
}
if (exc != null)
throw new FormatException(exc);
// save reference to OME-XML window
xmlWindow = metaWindow;
return metaWindow;
}
use of loci.common.services.ServiceException in project bioformats by openmicroscopy.
the class ImportProcess method createBaseReader.
/**
* Initializes an {@link loci.formats.IFormatReader}
* according to the current configuration.
*/
private void createBaseReader() throws FormatException, IOException {
if (options.isLocal() || options.isHTTP()) {
BF.status(options.isQuiet(), "Identifying " + idName);
imageReader = LociPrefs.makeImageReader();
baseReader = imageReader.getReader(options.isUsingPatternIds() ? new FilePattern(options.getId()).getFiles()[0] : options.getId());
} else if (options.isOMERO()) {
BF.status(options.isQuiet(), "Establishing server connection");
try {
ReflectedUniverse r = new ReflectedUniverse();
r.exec("import loci.ome.io.OmeroReader");
r.exec("baseReader = new OmeroReader()");
ClassList<IFormatReader> classes = new ClassList<IFormatReader>(IFormatReader.class);
r.setVar("classes", classes);
r.exec("class = baseReader.getClass()");
r.exec("classes.addClass(class)");
imageReader = new ImageReader(classes);
baseReader = imageReader.getReader(options.getId());
} catch (Exception exc) {
WindowTools.reportException(exc, options.isQuiet(), "Sorry, there was a problem communicating with the server.");
cancel();
return;
}
} else {
WindowTools.reportException(null, options.isQuiet(), "Sorry, there has been an internal error: unknown data source");
cancel();
return;
}
// attach OME-XML metadata store
Exception exc = null;
try {
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
meta = service.createOMEXMLMetadata();
omeXML = null;
} catch (DependencyException de) {
exc = de;
} catch (ServiceException se) {
exc = se;
}
if (exc != null) {
WindowTools.reportException(exc, options.isQuiet(), "Sorry, there was a problem constructing the OME-XML metadata store");
throw new FormatException(exc);
}
baseReader.setMetadataStore(meta);
BF.status(options.isQuiet(), "");
DebugTools.enableIJLogging(IJ.debugMode);
}
Aggregations