Search in sources :

Example 86 with Location

use of loci.common.Location in project bioformats by openmicroscopy.

the class OMETiffWriter method populateImage.

private void populateImage(OMEXMLMetadata omeMeta, int series) {
    String dimensionOrder = omeMeta.getPixelsDimensionOrder(series).toString();
    int sizeZ = omeMeta.getPixelsSizeZ(series).getValue().intValue();
    int sizeC = omeMeta.getPixelsSizeC(series).getValue().intValue();
    int sizeT = omeMeta.getPixelsSizeT(series).getValue().intValue();
    int imageCount = getPlaneCount();
    if (imageCount == 0) {
        omeMeta.setTiffDataPlaneCount(new NonNegativeInteger(0), series, 0);
        return;
    }
    PositiveInteger samplesPerPixel = new PositiveInteger((sizeZ * sizeC * sizeT) / imageCount);
    for (int c = 0; c < omeMeta.getChannelCount(series); c++) {
        omeMeta.setChannelSamplesPerPixel(samplesPerPixel, series, c);
    }
    sizeC /= samplesPerPixel.getValue();
    int nextPlane = 0;
    for (int plane = 0; plane < imageCount; plane++) {
        int[] zct = FormatTools.getZCTCoords(dimensionOrder, sizeZ, sizeC, sizeT, imageCount, plane);
        int planeIndex = plane;
        if (imageLocations[series].length < imageCount) {
            planeIndex /= (imageCount / imageLocations[series].length);
        }
        String filename = imageLocations[series][planeIndex];
        if (filename != null) {
            filename = new Location(filename).getName();
            Integer ifdIndex = ifdCounts.get(filename);
            int ifd = ifdIndex == null ? 0 : ifdIndex.intValue();
            omeMeta.setUUIDFileName(filename, series, nextPlane);
            String uuid = "urn:uuid:" + getUUID(filename);
            omeMeta.setUUIDValue(uuid, series, nextPlane);
            // fill in any non-default TiffData attributes
            populateTiffData(omeMeta, zct, ifd, series, nextPlane);
            ifdCounts.put(filename, ifd + 1);
            nextPlane++;
        }
    }
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) PositiveInteger(ome.xml.model.primitives.PositiveInteger) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) Location(loci.common.Location)

Example 87 with Location

use of loci.common.Location in project bioformats by openmicroscopy.

the class OMETiffWriter method close.

// -- IFormatHandler API methods --
/* @see loci.formats.IFormatHandler#close() */
@Override
public void close() throws IOException {
    try {
        if (currentId != null) {
            setupServiceAndMetadata();
            // remove any BinData and old TiffData elements from the OME-XML
            service.removeBinData(omeMeta);
            service.removeTiffData(omeMeta);
            for (int series = 0; series < omeMeta.getImageCount(); series++) {
                setSeries(series);
                populateImage(omeMeta, series);
            }
            String companion = getCompanion();
            String companionUUID = null;
            if (null != companion) {
                String companionXML = getOMEXML(companion);
                PrintWriter out = new PrintWriter(companion, Constants.ENCODING);
                out.println(XMLTools.indentXML(companionXML, true));
                out.close();
                companionUUID = "urn:uuid:" + getUUID(new Location(companion).getName());
            }
            List<String> files = new ArrayList<String>();
            for (String[] s : imageLocations) {
                for (String f : s) {
                    if (!files.contains(f) && f != null) {
                        files.add(f);
                        String xml = null;
                        if (null != companion) {
                            xml = getBinaryOnlyOMEXML(f, companion, companionUUID);
                        } else {
                            xml = getOMEXML(f);
                        }
                        xml = insertWarningComment(xml);
                        if (getMetadataOptions().isValidate()) {
                            service.validateOMEXML(xml);
                        }
                        // write OME-XML to the first IFD's comment
                        saveComment(f, xml);
                    }
                }
            }
        }
    } catch (DependencyException de) {
        throw new RuntimeException(de);
    } catch (ServiceException se) {
        throw new RuntimeException(se);
    } catch (FormatException fe) {
        throw new RuntimeException(fe);
    } catch (IllegalArgumentException iae) {
        throw new RuntimeException(iae);
    } finally {
        super.close();
        boolean canReallyClose = omeMeta == null || ifdCounts.size() == omeMeta.getImageCount();
        if (omeMeta != null && canReallyClose) {
            int omePlaneCount = 0;
            for (int i = 0; i < omeMeta.getImageCount(); i++) {
                int sizeZ = omeMeta.getPixelsSizeZ(i).getValue();
                int sizeC = omeMeta.getPixelsSizeC(i).getValue();
                int sizeT = omeMeta.getPixelsSizeT(i).getValue();
                omePlaneCount += sizeZ * sizeC * sizeT;
            }
            int ifdCount = 0;
            for (String key : ifdCounts.keySet()) {
                ifdCount += ifdCounts.get(key);
            }
            canReallyClose = omePlaneCount == ifdCount;
        }
        if (canReallyClose) {
            imageLocations = null;
            omeMeta = null;
            service = null;
            ifdCounts.clear();
        } else {
            for (String k : ifdCounts.keySet()) ifdCounts.put(k, 0);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException) ServiceException(loci.common.services.ServiceException) PrintWriter(java.io.PrintWriter) Location(loci.common.Location)

Example 88 with Location

use of loci.common.Location in project bioformats by openmicroscopy.

the class ImageInfo method configureReaderPreInit.

public void configureReaderPreInit() throws FormatException, IOException {
    if (omexml) {
        reader.setOriginalMetadataPopulated(originalMetadata);
        try {
            ServiceFactory factory = new ServiceFactory();
            OMEXMLService service = factory.getInstance(OMEXMLService.class);
            reader.setMetadataStore(service.createOMEXMLMetadata(null, omexmlVersion));
        } catch (DependencyException de) {
            throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, de);
        } catch (ServiceException se) {
            throw new FormatException(se);
        }
    }
    // check file format
    if (reader instanceof ImageReader) {
        // determine format
        ImageReader ir = (ImageReader) reader;
        if (new Location(id).exists()) {
            LOGGER.info("Checking file format [{}]", ir.getFormat(id));
        }
    } else {
        // verify format
        LOGGER.info("Checking {} format [{}]", reader.getFormat(), reader.isThisType(id) ? "yes" : "no");
    }
    LOGGER.info("Initializing reader");
    if (stitch) {
        reader = new FileStitcher(reader, true);
        Location f = new Location(id);
        String pat = null;
        if (!f.exists()) {
            ((FileStitcher) reader).setUsingPatternIds(true);
            pat = id;
        } else {
            pat = FilePattern.findPattern(f);
        }
        if (pat != null)
            id = pat;
    }
    if (expand)
        reader = new ChannelFiller(reader);
    if (separate)
        reader = new ChannelSeparator(reader);
    if (merge)
        reader = new ChannelMerger(reader);
    if (cache) {
        if (cachedir != null) {
            reader = new Memoizer(reader, 0, new File(cachedir));
        } else {
            reader = new Memoizer(reader, 0);
        }
    }
    minMaxCalc = null;
    if (minmax || autoscale)
        reader = minMaxCalc = new MinMaxCalculator(reader);
    dimSwapper = null;
    if (swapOrder != null || shuffleOrder != null) {
        reader = dimSwapper = new DimensionSwapper(reader);
    }
    reader = biReader = new BufferedImageReader(reader);
    reader.close();
    reader.setNormalized(normalize);
    reader.setMetadataFiltered(filter);
    reader.setGroupFiles(group);
    options.setMetadataLevel(doMeta ? MetadataLevel.ALL : MetadataLevel.MINIMUM);
    options.setValidate(validate);
    reader.setMetadataOptions(options);
    reader.setFlattenedResolutions(flat);
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) Memoizer(loci.formats.Memoizer) ChannelMerger(loci.formats.ChannelMerger) ChannelFiller(loci.formats.ChannelFiller) BufferedImageReader(loci.formats.gui.BufferedImageReader) DependencyException(loci.common.services.DependencyException) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException) ChannelSeparator(loci.formats.ChannelSeparator) ServiceException(loci.common.services.ServiceException) FileStitcher(loci.formats.FileStitcher) MinMaxCalculator(loci.formats.MinMaxCalculator) MissingLibraryException(loci.formats.MissingLibraryException) DimensionSwapper(loci.formats.DimensionSwapper) ImageReader(loci.formats.ImageReader) BufferedImageReader(loci.formats.gui.BufferedImageReader) File(java.io.File) Location(loci.common.Location)

Example 89 with Location

use of loci.common.Location in project bioformats by openmicroscopy.

the class FormatReader method setId.

/**
 * Initializes a reader from the input file name.
 *
 * Calls {@link #initFile(String id)} to initializes the input file, reads
 * all of the metadata and sets the reader up for reading planes.
 * The performance of this method depends on the format and can be up to
 * several minutes for large file sets.
 *
 *  @param id a {@link String} specifying the path to the file
 */
@Override
public void setId(String id) throws FormatException, IOException {
    LOGGER.debug("{} initializing {}", this.getClass().getSimpleName(), id);
    if (currentId == null || !new Location(id).getAbsolutePath().equals(new Location(currentId).getAbsolutePath())) {
        initFile(id);
        MetadataStore store = getMetadataStore();
        if (saveOriginalMetadata) {
            if (store instanceof OMEXMLMetadata) {
                setupService();
                Hashtable<String, Object> allMetadata = new Hashtable<String, Object>();
                allMetadata.putAll(metadata);
                for (int series = 0; series < getSeriesCount(); series++) {
                    String name = "Series " + series;
                    try {
                        String realName = ((IMetadata) store).getImageName(series);
                        if (realName != null && realName.trim().length() != 0) {
                            name = realName;
                        }
                    } catch (Exception e) {
                    }
                    setSeries(series);
                    MetadataTools.merge(getSeriesMetadata(), allMetadata, name + " ");
                }
                setSeries(0);
                service.populateOriginalMetadata((OMEXMLMetadata) store, allMetadata);
            }
        }
        if (store instanceof OMEXMLMetadata) {
            ((OMEXMLMetadata) store).resolveReferences();
            setupService();
            if (getMetadataOptions().isValidate()) {
                try {
                    String omexml = service.getOMEXML((MetadataRetrieve) store);
                    service.validateOMEXML(omexml);
                } catch (ServiceException | NullPointerException e) {
                    LOGGER.warn("OMEXMLService unable to create OME-XML metadata object.", e);
                }
            }
            for (int series = 0; series < getSeriesCount(); series++) {
                setSeries(series);
                if (getModuloZ().length() > 1 || getModuloC().length() > 1 || getModuloT().length() > 1) {
                    service.addModuloAlong((OMEXMLMetadata) store, core.get(series), series);
                }
            }
            setSeries(0);
        }
    }
}
Also used : Hashtable(java.util.Hashtable) ServiceException(loci.common.services.ServiceException) DependencyException(loci.common.services.DependencyException) EnumerationException(ome.xml.model.enums.EnumerationException) IOException(java.io.IOException) MetadataStore(loci.formats.meta.MetadataStore) IMetadata(loci.formats.meta.IMetadata) ServiceException(loci.common.services.ServiceException) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) Location(loci.common.Location)

Example 90 with Location

use of loci.common.Location in project bioformats by openmicroscopy.

the class MetadataTools method setDefaultCreationDate.

/**
 * Sets a default creation date.  If the named file exists, then the creation
 * date is set to the file's last modification date.  Otherwise, it is set
 * to the current date.
 *
 * @see #setDefaultDateEnabled(boolean)
 */
public static void setDefaultCreationDate(MetadataStore store, String id, int series) {
    if (!defaultDateEnabled) {
        return;
    }
    Location file = id == null ? null : new Location(id).getAbsoluteFile();
    long time = System.currentTimeMillis();
    if (file != null && file.exists())
        time = file.lastModified();
    store.setImageAcquisitionDate(new Timestamp(DateTools.convertDate(time, DateTools.UNIX)), series);
}
Also used : Timestamp(ome.xml.model.primitives.Timestamp) Location(loci.common.Location)

Aggregations

Location (loci.common.Location)185 CoreMetadata (loci.formats.CoreMetadata)55 MetadataStore (loci.formats.meta.MetadataStore)51 FormatException (loci.formats.FormatException)49 ArrayList (java.util.ArrayList)47 RandomAccessInputStream (loci.common.RandomAccessInputStream)47 Length (ome.units.quantity.Length)34 IOException (java.io.IOException)28 Timestamp (ome.xml.model.primitives.Timestamp)28 Time (ome.units.quantity.Time)20 IFD (loci.formats.tiff.IFD)15 TiffParser (loci.formats.tiff.TiffParser)15 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)15 PositiveInteger (ome.xml.model.primitives.PositiveInteger)15 DependencyException (loci.common.services.DependencyException)13 ServiceException (loci.common.services.ServiceException)12 File (java.io.File)11 ServiceFactory (loci.common.services.ServiceFactory)11 IniList (loci.common.IniList)10 FilePattern (loci.formats.FilePattern)10