Search in sources :

Example 76 with Location

use of loci.common.Location 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);
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Temperature(ome.units.quantity.Temperature) Length(ome.units.quantity.Length) Time(ome.units.quantity.Time) Timestamp(ome.xml.model.primitives.Timestamp) ElectricPotential(ome.units.quantity.ElectricPotential) Location(loci.common.Location)

Example 77 with Location

use of loci.common.Location 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) {
                }
            }
        }
    }
}
Also used : IFormatReader(loci.formats.IFormatReader) ClassList(loci.formats.ClassList) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) RandomAccessInputStream(loci.common.RandomAccessInputStream) ImageReader(loci.formats.ImageReader) Location(loci.common.Location)

Example 78 with Location

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

the class DicomReader method scanDirectory.

// -- Utility methods --
/**
 * Scan the given directory for files that belong to this dataset.
 */
private void scanDirectory(Location dir, boolean checkSeries) throws FormatException, IOException {
    Location currentFile = new Location(currentId).getAbsoluteFile();
    FilePattern pattern = new FilePattern(currentFile.getName(), dir.getAbsolutePath());
    String[] patternFiles = pattern.getFiles();
    if (patternFiles == null)
        patternFiles = new String[0];
    Arrays.sort(patternFiles);
    // path separator normalization is inconsistent
    for (int i = 0; i < patternFiles.length; i++) {
        patternFiles[i] = new Location(patternFiles[i]).getAbsolutePath();
    }
    String[] files = dir.list(true);
    if (files == null)
        return;
    Arrays.sort(files);
    for (String f : files) {
        String file = new Location(dir, f).getAbsolutePath();
        LOGGER.debug("Checking file {}", file);
        if (!f.equals(currentId) && !file.equals(currentId) && isThisType(file) && Arrays.binarySearch(patternFiles, file) >= 0) {
            addFileToList(file, checkSeries);
        }
    }
}
Also used : FilePattern(loci.formats.FilePattern) Location(loci.common.Location)

Example 79 with Location

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

the class FakeReader method findLogFiles.

private void findLogFiles() {
    iniFile = null;
    Location loc = new Location(getCurrentFile() + ".ini");
    if (loc.exists()) {
        iniFile = loc.getAbsolutePath();
    }
}
Also used : Location(loci.common.Location)

Example 80 with Location

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

the class LegacyQTTools method constructLoader.

protected static ClassLoader constructLoader() {
    // set up additional QuickTime for Java paths
    URL[] paths = null;
    if (MAC_OS_X) {
        try {
            paths = new URL[] { new URL("file:/System/Library/Java/Extensions/QTJava.zip") };
        } catch (MalformedURLException exc) {
            LOGGER.info("", exc);
        }
        return paths == null ? null : new URLClassLoader(paths);
    }
    // case for Windows
    try {
        String windir = System.getProperty("java.library.path");
        StringTokenizer st = new StringTokenizer(windir, ";");
        while (st.hasMoreTokens()) {
            Location f = new Location(st.nextToken(), "QTJava.zip");
            if (f.exists()) {
                try {
                    paths = new URL[] { f.toURL() };
                } catch (MalformedURLException exc) {
                    LOGGER.info("", exc);
                }
                return paths == null ? null : new URLClassLoader(paths);
            }
        }
    } catch (SecurityException e) {
        // this is common when using Bio-Formats within an applet
        LOGGER.warn("Cannot read value of 'java.library.path'", e);
    }
    return null;
}
Also used : MalformedURLException(java.net.MalformedURLException) StringTokenizer(java.util.StringTokenizer) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) 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