Search in sources :

Example 61 with FormatException

use of loci.formats.FormatException in project bioformats by openmicroscopy.

the class ColumbusReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
protected void initFile(String id) throws FormatException, IOException {
    Location xml = findXML(id);
    if (null == xml) {
        throw new FormatException("Could not find " + XML_FILE);
    }
    id = xml.getAbsolutePath();
    super.initFile(id);
    Location parent = new Location(currentId).getAbsoluteFile().getParentFile();
    // parse plate layout and image dimensions from the XML files
    String xmlData = DataTools.readFile(id);
    MeasurementHandler handler = new MeasurementHandler();
    XMLTools.parseXML(xmlData, handler);
    String[] parentDirectories = parent.list(true);
    Arrays.sort(parentDirectories);
    ArrayList<String> timepointDirs = new ArrayList<String>();
    for (String file : parentDirectories) {
        Location absFile = new Location(parent, file);
        if (absFile.isDirectory()) {
            timepointDirs.add(absFile.getAbsolutePath());
            for (String f : absFile.list(true)) {
                if (!checkSuffix(f, "tif")) {
                    if (!metadataFiles.contains(file + File.separator + f)) {
                        metadataFiles.add(file + File.separator + f);
                    }
                }
            }
        }
    }
    for (int i = 0; i < metadataFiles.size(); i++) {
        String metadataFile = metadataFiles.get(i);
        int end = metadataFile.indexOf(File.separator);
        String timepointPath = end < 0 ? "" : parent + File.separator + metadataFile.substring(0, end);
        Location f = new Location(parent + File.separator + metadataFile);
        if (!f.exists()) {
            metadataFile = metadataFile.substring(end + 1);
            f = new Location(parent, metadataFile);
        }
        String path = f.getAbsolutePath();
        metadataFiles.set(i, path);
        if (checkSuffix(path, "columbusidx.xml")) {
            int timepoint = timepointDirs.indexOf(timepointPath);
            if (timepointDirs.size() == 0) {
                timepoint = 0;
            }
            parseImageXML(path, timepoint);
        }
    }
    // process plane list to determine plate size
    Comparator<Plane> planeComp = new Comparator<Plane>() {

        public int compare(Plane p1, Plane p2) {
            if (p1.row != p2.row) {
                return p1.row - p2.row;
            }
            if (p1.col != p2.col) {
                return p1.col - p2.col;
            }
            if (p1.field != p2.field) {
                return p1.field - p2.field;
            }
            if (p1.timepoint != p2.timepoint) {
                return p1.timepoint - p2.timepoint;
            }
            if (p1.channel != p2.channel) {
                return p1.channel - p2.channel;
            }
            return 0;
        }
    };
    Plane[] tmpPlanes = planes.toArray(new Plane[planes.size()]);
    Arrays.sort(tmpPlanes, planeComp);
    planes.clear();
    reader = new MinimalTiffReader();
    reader.setId(tmpPlanes[0].file);
    core = reader.getCoreMetadataList();
    CoreMetadata m = core.get(0);
    m.sizeC = 0;
    m.sizeT = 0;
    ArrayList<Integer> uniqueSamples = new ArrayList<Integer>();
    ArrayList<Integer> uniqueRows = new ArrayList<Integer>();
    ArrayList<Integer> uniqueCols = new ArrayList<Integer>();
    for (Plane p : tmpPlanes) {
        planes.add(p);
        int sampleIndex = p.row * handler.getPlateColumns() + p.col;
        if (!uniqueSamples.contains(sampleIndex)) {
            uniqueSamples.add(sampleIndex);
        }
        if (!uniqueRows.contains(p.row)) {
            uniqueRows.add(p.row);
        }
        if (!uniqueCols.contains(p.col)) {
            uniqueCols.add(p.col);
        }
        // counts are assumed to be non-sparse
        if (p.field >= nFields) {
            nFields = p.field + 1;
        }
        if (p.channel >= getSizeC()) {
            m.sizeC = p.channel + 1;
        }
        if (p.timepoint >= getSizeT()) {
            m.sizeT = p.timepoint + 1;
        }
    }
    m.sizeZ = 1;
    m.imageCount = getSizeZ() * getSizeC() * getSizeT();
    m.dimensionOrder = "XYCTZ";
    m.rgb = false;
    int seriesCount = uniqueSamples.size() * nFields;
    for (int i = 1; i < seriesCount; i++) {
        core.add(m);
    }
    // populate the MetadataStore
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, true);
    store.setScreenID(MetadataTools.createLSID("Screen", 0), 0);
    store.setScreenName(handler.getScreenName(), 0);
    store.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
    store.setPlateName(handler.getPlateName(), 0);
    store.setPlateRows(new PositiveInteger(handler.getPlateRows()), 0);
    store.setPlateColumns(new PositiveInteger(handler.getPlateColumns()), 0);
    String imagePrefix = handler.getPlateName() + " Well ";
    int wellSample = 0;
    int nextWell = -1;
    Timestamp date = new Timestamp(acquisitionDate);
    long timestampSeconds = date.asInstant().getMillis() / 1000;
    for (Integer row : uniqueRows) {
        for (Integer col : uniqueCols) {
            if (!uniqueSamples.contains(row * handler.getPlateColumns() + col)) {
                continue;
            }
            nextWell++;
            store.setWellID(MetadataTools.createLSID("Well", 0, nextWell), 0, nextWell);
            store.setWellRow(new NonNegativeInteger(row), 0, nextWell);
            store.setWellColumn(new NonNegativeInteger(col), 0, nextWell);
            for (int field = 0; field < nFields; field++) {
                Plane p = lookupPlane(row, col, field, 0, 0);
                String wellSampleID = MetadataTools.createLSID("WellSample", 0, nextWell, field);
                store.setWellSampleID(wellSampleID, 0, nextWell, field);
                store.setWellSampleIndex(new NonNegativeInteger(wellSample), 0, nextWell, field);
                if (p != null) {
                    store.setWellSamplePositionX(new Length(p.positionX, UNITS.REFERENCEFRAME), 0, nextWell, field);
                    store.setWellSamplePositionY(new Length(p.positionY, UNITS.REFERENCEFRAME), 0, nextWell, field);
                }
                String imageID = MetadataTools.createLSID("Image", wellSample);
                store.setImageID(imageID, wellSample);
                store.setWellSampleImageRef(imageID, 0, nextWell, field);
                store.setImageName(imagePrefix + (char) (row + 'A') + (col + 1) + " Field #" + (field + 1), wellSample);
                store.setImageAcquisitionDate(date, wellSample);
                if (p != null) {
                    p.series = wellSample;
                    store.setPixelsPhysicalSizeX(FormatTools.getPhysicalSizeX(p.sizeX), p.series);
                    store.setPixelsPhysicalSizeY(FormatTools.getPhysicalSizeY(p.sizeY), p.series);
                    for (int c = 0; c < getSizeC(); c++) {
                        p = lookupPlane(row, col, field, 0, c);
                        if (p != null) {
                            p.series = wellSample;
                            store.setChannelName(p.channelName, p.series, p.channel);
                            if ((int) p.emWavelength > 0) {
                                store.setChannelEmissionWavelength(FormatTools.getEmissionWavelength(p.emWavelength), p.series, p.channel);
                            }
                            if ((int) p.exWavelength > 0) {
                                store.setChannelExcitationWavelength(FormatTools.getExcitationWavelength(p.exWavelength), p.series, p.channel);
                            }
                            store.setChannelColor(p.channelColor, p.series, p.channel);
                        }
                        for (int t = 0; t < getSizeT(); t++) {
                            p = lookupPlane(row, col, field, t, c);
                            if (p != null) {
                                p.series = wellSample;
                                store.setPlaneDeltaT(new Time(p.deltaT - timestampSeconds, UNITS.SECOND), p.series, getIndex(0, c, t));
                            }
                        }
                    }
                }
                wellSample++;
            }
        }
    }
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) ArrayList(java.util.ArrayList) Time(ome.units.quantity.Time) CoreMetadata(loci.formats.CoreMetadata) Timestamp(ome.xml.model.primitives.Timestamp) FormatException(loci.formats.FormatException) Comparator(java.util.Comparator) PositiveInteger(ome.xml.model.primitives.PositiveInteger) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) Location(loci.common.Location)

Example 62 with FormatException

use of loci.formats.FormatException in project bioformats by openmicroscopy.

the class FV1000Reader 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.setCommentDelimiter(null);
    isOIB = checkSuffix(id, OIB_SUFFIX);
    if (isOIB) {
        initPOIService();
    }
    // mappedOIF is used to distinguish between datasets that are being read
    // directly (e.g. using ImageJ or showinf), and datasets that are being
    // imported through omebf. In the latter case, the necessary directory
    // structure is not preserved (only relative file names are stored in
    // OMEIS), so we will need to use slightly different logic to build the
    // list of associated files.
    boolean mappedOIF = !isOIB && !new File(id).getAbsoluteFile().exists();
    wavelengths = new ArrayList<Double>();
    illuminations = new ArrayList<String>();
    channels = new ArrayList<ChannelData>();
    planes = new ArrayList<PlaneData>();
    String oifName = null;
    if (isOIB) {
        oifName = mapOIBFiles();
    } else {
        // make sure we have the OIF file, not a TIFF
        if (!checkSuffix(id, OIF_SUFFIX)) {
            currentId = findOIFFile(id);
            initFile(currentId);
        }
        oifName = currentId;
    }
    String oifPath = new Location(oifName).getAbsoluteFile().getAbsolutePath();
    if (mappedOIF) {
        oifPath = oifName.substring(0, oifName.lastIndexOf(File.separator) + 1);
    }
    String path = isOIB ? "" : oifPath.substring(0, oifPath.lastIndexOf(File.separator) + 1);
    try {
        RandomAccessInputStream s = getFile(oifName);
        s.close();
    } catch (IOException e) {
        oifName = oifName.replaceAll(".oif", ".OIF");
    }
    // parse key/value pairs from the OIF file
    code = new String[NUM_DIMENSIONS];
    size = new String[NUM_DIMENSIONS];
    pixelSize = new Double[NUM_DIMENSIONS];
    previewNames = new ArrayList<String>();
    SortedMap<Integer, String> previewFileNames = new TreeMap<Integer, String>();
    boolean laserEnabled = true;
    IniList f = getIniFile(oifName);
    IniTable saveInfo = f.getTable("ProfileSaveInfo");
    String[] saveKeys = saveInfo.keySet().toArray(new String[saveInfo.size()]);
    for (String key : saveKeys) {
        String value = saveInfo.get(key).toString();
        value = sanitizeValue(value).trim();
        if (key.startsWith("IniFileName") && key.indexOf("Thumb") == -1 && !isPreviewName(value)) {
            filenames.put(new Integer(key.substring(11)), value);
        } else if (key.startsWith("RoiFileName") && key.indexOf("Thumb") == -1 && !isPreviewName(value)) {
            try {
                roiFilenames.put(new Integer(key.substring(11)), value);
            } catch (NumberFormatException e) {
            }
        } else if (key.equals("PtyFileNameS"))
            ptyStart = value;
        else if (key.equals("PtyFileNameE"))
            ptyEnd = value;
        else if (key.equals("PtyFileNameT2"))
            ptyPattern = value;
        else if (key.indexOf("Thumb") != -1) {
            if (thumbId == null)
                thumbId = value.trim();
        } else if (key.startsWith("LutFileName")) {
            lutNames.add(path + value);
        } else if (isPreviewName(value)) {
            try {
                RandomAccessInputStream s = getFile(path + value.trim());
                if (s != null) {
                    s.close();
                    Integer previewIndex = getPreviewNameIndex(key);
                    if (previewIndex != null) {
                        previewFileNames.put(previewIndex, path + value.trim());
                    } else {
                        previewNames.add(path + value.trim());
                    }
                }
            } catch (FormatException e) {
                LOGGER.debug("Preview file not found", e);
            } catch (IOException e) {
                LOGGER.debug("Preview file not found", e);
            }
        }
    }
    // Store sorted list of preview names
    previewNames.addAll(previewFileNames.values());
    if (filenames.isEmpty())
        addPtyFiles();
    for (int i = 0; i < NUM_DIMENSIONS; i++) {
        IniTable commonParams = f.getTable("Axis " + i + " Parameters Common");
        code[i] = commonParams.get("AxisCode");
        size[i] = commonParams.get("MaxSize");
        double end = Double.parseDouble(commonParams.get("EndPosition"));
        double start = Double.parseDouble(commonParams.get("StartPosition"));
        pixelSize[i] = end - start;
    }
    IniTable referenceParams = f.getTable("Reference Image Parameter");
    imageDepth = Integer.parseInt(referenceParams.get("ImageDepth"));
    pixelSizeX = referenceParams.get("WidthConvertValue");
    pixelSizeY = referenceParams.get("HeightConvertValue");
    String ripValidBitCounts = referenceParams.get("ValidBitCounts");
    if (ripValidBitCounts != null) {
        validBits = Integer.parseInt(ripValidBitCounts);
    }
    int index = 0;
    IniTable laser = f.getTable("Laser " + index + " Parameters");
    while (laser != null) {
        laserEnabled = laser.get("Laser Enable").equals("1");
        if (laserEnabled) {
            wavelengths.add(new Double(laser.get("LaserWavelength")));
        }
        creationDate = laser.get("ImageCaputreDate");
        if (creationDate == null) {
            creationDate = laser.get("ImageCaptureDate");
        }
        index++;
        laser = f.getTable("Laser " + index + " Parameters");
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        index = 1;
        IniTable guiChannel = f.getTable("GUI Channel " + index + " Parameters");
        while (guiChannel != null) {
            ChannelData channel = new ChannelData();
            channel.gain = DataTools.parseDouble(guiChannel.get("AnalogPMTGain"));
            channel.voltage = DataTools.parseDouble(guiChannel.get("AnalogPMTVoltage"));
            channel.barrierFilter = channel.getFilter(guiChannel.get("BF Name"));
            channel.active = Integer.parseInt(guiChannel.get("CH Activate")) != 0;
            channel.name = guiChannel.get("CH Name");
            channel.dyeName = guiChannel.get("DyeName");
            channel.emissionFilter = channel.getFilter(guiChannel.get("EmissionDM Name"));
            channel.emWave = DataTools.parseDouble(guiChannel.get("EmissionWavelength"));
            channel.excitationFilter = channel.getFilter(guiChannel.get("ExcitationDM Name"));
            channel.exWave = DataTools.parseDouble(guiChannel.get("ExcitationWavelength"));
            channels.add(channel);
            index++;
            guiChannel = f.getTable("GUI Channel " + index + " Parameters");
        }
        index = 1;
        IniTable channel = f.getTable("Channel " + index + " Parameters");
        while (channel != null) {
            String illumination = channel.get("LightType");
            if (illumination != null)
                illumination = illumination.toLowerCase();
            if (illumination == null) {
            // Ignored
            } else if (illumination.indexOf("fluorescence") != -1) {
                illumination = "Epifluorescence";
            } else if (illumination.indexOf("transmitted") != -1) {
                illumination = "Transmitted";
            } else
                illumination = null;
            illuminations.add(illumination);
            index++;
            channel = f.getTable("Channel " + index + " Parameters");
        }
        HashMap<String, String> iniMap = f.flattenIntoHashMap();
        metadata.putAll(iniMap);
    }
    LOGGER.info("Initializing helper readers");
    if (previewNames.size() > 0) {
        final List<String> v = new ArrayList<String>();
        for (int i = 0; i < previewNames.size(); i++) {
            String ss = previewNames.get(i);
            ss = replaceExtension(ss, "pty", "tif");
            if (ss.endsWith(".tif"))
                v.add(ss);
        }
        previewNames = v;
        if (previewNames.size() > 0) {
            core.clear();
            core.add(new CoreMetadata());
            core.add(new CoreMetadata());
            IFDList ifds = null;
            CoreMetadata ms1 = core.get(1);
            for (String previewName : previewNames) {
                RandomAccessInputStream preview = getFile(previewName);
                TiffParser tp = new TiffParser(preview);
                ifds = tp.getIFDs();
                preview.close();
                tp = null;
                ms1.imageCount += ifds.size();
            }
            ms1.sizeX = (int) ifds.get(0).getImageWidth();
            ms1.sizeY = (int) ifds.get(0).getImageLength();
            ms1.sizeZ = 1;
            ms1.sizeT = 1;
            ms1.sizeC = ms1.imageCount;
            ms1.rgb = false;
            int bits = ifds.get(0).getBitsPerSample()[0];
            while ((bits % 8) != 0) bits++;
            bits /= 8;
            ms1.pixelType = FormatTools.pixelTypeFromBytes(bits, false, false);
            ms1.dimensionOrder = "XYCZT";
            ms1.indexed = false;
        }
    }
    CoreMetadata ms0 = core.get(0);
    ms0.imageCount = filenames.size();
    tiffs = new ArrayList<String>(getImageCount());
    thumbReader = new BMPReader();
    if (thumbId != null) {
        thumbId = replaceExtension(thumbId, "pty", "bmp");
        thumbId = sanitizeFile(thumbId, path);
    }
    LOGGER.info("Reading additional metadata");
    // open each INI file (.pty extension) and build list of TIFF files
    String tiffPath = null;
    ms0.dimensionOrder = "XY";
    final Map<String, String> values = new HashMap<String, String>();
    final List<String> baseKeys = new ArrayList<String>();
    for (int i = 0, ii = 0; ii < getImageCount(); i++, ii++) {
        String file = filenames.get(i);
        while (file == null) file = filenames.get(++i);
        file = sanitizeFile(file, path);
        if (file.indexOf(File.separator) != -1) {
            tiffPath = file.substring(0, file.lastIndexOf(File.separator));
        } else
            tiffPath = file;
        Location ptyFile = new Location(file);
        if (!isOIB && !ptyFile.exists()) {
            LOGGER.warn("Could not find .pty file ({}); guessing at the " + "corresponding TIFF file.", file);
            String tiff = replaceExtension(file, "pty", "tif");
            Location tiffFile = new Location(tiff);
            if (tiffFile.exists()) {
                tiffs.add(ii, tiffFile.getAbsolutePath());
                continue;
            } else {
                if (!tiffFile.getParentFile().exists()) {
                    String realOIFName = new Location(currentId).getName();
                    String basePath = tiffFile.getParentFile().getParent();
                    if (mappedOIF) {
                        tiffPath = basePath + File.separator + realOIFName + ".files";
                        ptyFile = new Location(tiffPath, ptyFile.getName());
                        file = ptyFile.getAbsolutePath();
                    } else {
                        Location newFile = new Location(basePath, realOIFName + ".files");
                        ptyFile = new Location(newFile, ptyFile.getName());
                        file = ptyFile.getAbsolutePath();
                        tiffPath = newFile.getAbsolutePath();
                    }
                }
            }
        } else if (!isOIB) {
            file = ptyFile.getAbsolutePath();
        }
        IniList pty = getIniFile(file);
        IniTable fileInfo = pty.getTable("File Info");
        file = sanitizeValue(fileInfo.get("DataName"));
        if (!isPreviewName(file)) {
            while (file.indexOf("GST") != -1) {
                file = removeGST(file);
            }
            if (isOIB) {
                file = tiffPath + File.separator + file;
            } else
                file = new Location(tiffPath, file).getAbsolutePath();
            file = replaceExtension(file, "pty", "tif");
            tiffs.add(ii, file);
        }
        PlaneData plane = new PlaneData();
        for (int dim = 0; dim < NUM_DIMENSIONS; dim++) {
            IniTable axis = pty.getTable("Axis " + dim + " Parameters");
            if (axis == null)
                break;
            boolean addAxis = Integer.parseInt(axis.get("Number")) > 1;
            if (dim == 2) {
                if (addAxis && getDimensionOrder().indexOf('C') == -1) {
                    ms0.dimensionOrder += 'C';
                }
            } else if (dim == 3) {
                if (addAxis && getDimensionOrder().indexOf('Z') == -1) {
                    ms0.dimensionOrder += 'Z';
                }
                final Double number = Double.valueOf(axis.get("AbsPositionValue"));
                plane.positionZ = new Length(number, UNITS.REFERENCEFRAME);
            } else if (dim == 4) {
                if (addAxis && getDimensionOrder().indexOf('T') == -1) {
                    ms0.dimensionOrder += 'T';
                }
                // divide by 1000, as the position is in milliseconds
                // and DeltaT is in seconds
                plane.deltaT = Double.parseDouble(axis.get("AbsPositionValue")) / 1000;
            } else if (dim == 7) {
                try {
                    String xPos = axis.get("AbsPositionValueX");
                    if (xPos != null) {
                        final Double number = Double.valueOf(xPos);
                        plane.positionX = new Length(number, UNITS.REFERENCEFRAME);
                    }
                } catch (NumberFormatException e) {
                }
                try {
                    String yPos = axis.get("AbsPositionValueY");
                    if (yPos != null) {
                        final Double number = Double.valueOf(yPos);
                        plane.positionY = new Length(number, UNITS.REFERENCEFRAME);
                    }
                } catch (NumberFormatException e) {
                }
            }
        }
        ms0.bitsPerPixel = validBits;
        planes.add(plane);
        IniTable acquisition = pty.getTable("Acquisition Parameters Common");
        if (acquisition != null) {
            magnification = acquisition.get("Magnification");
            lensNA = acquisition.get("ObjectiveLens NAValue");
            objectiveName = acquisition.get("ObjectiveLens Name");
            workingDistance = acquisition.get("ObjectiveLens WDValue");
            pinholeSize = acquisition.get("PinholeDiameter");
            String validBitCounts = acquisition.get("ValidBitCounts");
            if (validBitCounts != null) {
                ms0.bitsPerPixel = Integer.parseInt(validBitCounts);
            }
        }
        if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
            for (IniTable table : pty) {
                String[] keys = table.keySet().toArray(new String[table.size()]);
                for (String key : keys) {
                    values.put("Image " + ii + " : " + key, table.get(key));
                    if (!baseKeys.contains(key))
                        baseKeys.add(key);
                }
            }
        }
    }
    for (String key : baseKeys) {
        if (key.equals("DataName") || key.indexOf("FileName") >= 0)
            break;
        boolean equal = true;
        String first = values.get("Image 0 : " + key);
        for (int i = 1; i < getImageCount(); i++) {
            if (!first.equals(values.get("Image " + i + " : " + key))) {
                equal = false;
                break;
            }
        }
        if (equal) {
            addGlobalMeta(key, first);
        } else {
            for (int i = 0; i < getImageCount(); i++) {
                String k = "Image " + i + " : " + key;
                addGlobalMeta(k, values.get(k));
            }
        }
    }
    if (tiffs.size() != getImageCount()) {
        ms0.imageCount = tiffs.size();
    }
    usedFiles = new ArrayList<String>();
    if (tiffPath != null) {
        usedFiles.add(isOIB ? id : oifName);
        if (!isOIB) {
            Location dir = new Location(tiffPath);
            if (!mappedOIF && !dir.exists()) {
                throw new FormatException("Required directory " + tiffPath + " was not found.");
            }
            String[] list = mappedOIF ? Location.getIdMap().keySet().toArray(new String[0]) : dir.list(true);
            for (int i = 0; i < list.length; i++) {
                if (mappedOIF)
                    usedFiles.add(list[i]);
                else {
                    String p = new Location(tiffPath, list[i]).getAbsolutePath();
                    String check = p.toLowerCase();
                    if (!check.endsWith(".tif") && !check.endsWith(".pty") && !check.endsWith(".roi") && !check.endsWith(".lut") && !check.endsWith(".bmp")) {
                        continue;
                    }
                    usedFiles.add(p);
                }
            }
        }
    }
    LOGGER.info("Populating metadata");
    // calculate axis sizes
    int realChannels = 0;
    for (int i = 0; i < NUM_DIMENSIONS; i++) {
        int ss = Integer.parseInt(size[i]);
        if (pixelSize[i] == null)
            pixelSize[i] = 1.0;
        if (code[i].equals("X"))
            ms0.sizeX = ss;
        else if (code[i].equals("Y") && ss > 1)
            ms0.sizeY = ss;
        else if (code[i].equals("Z")) {
            if (getSizeY() == 0) {
                ms0.sizeY = ss;
            } else {
                ms0.sizeZ = ss;
                // Z size stored in nm
                pixelSizeZ = Math.abs((pixelSize[i].doubleValue() / (getSizeZ() - 1)) / 1000);
            }
        } else if (code[i].equals("T")) {
            if (getSizeY() == 0) {
                ms0.sizeY = ss;
            } else {
                ms0.sizeT = ss;
                pixelSizeT = Math.abs((pixelSize[i].doubleValue() / (getSizeT() - 1)) / 1000);
            }
        } else if (ss > 0) {
            if (getSizeC() == 0)
                ms0.sizeC = ss;
            else
                ms0.sizeC *= ss;
            if (code[i].equals("C"))
                realChannels = ss;
        }
    }
    if (getSizeZ() == 0)
        ms0.sizeZ = 1;
    if (getSizeC() == 0)
        ms0.sizeC = 1;
    if (getSizeT() == 0)
        ms0.sizeT = 1;
    if (getImageCount() == getSizeC() && getSizeY() == 1) {
        ms0.imageCount *= getSizeZ() * getSizeT();
    } else if (getImageCount() == getSizeC()) {
        ms0.sizeZ = 1;
        ms0.sizeT = 1;
    }
    if (getSizeZ() * getSizeT() * getSizeC() != getImageCount()) {
        int diff = (getSizeZ() * getSizeC() * getSizeT()) - getImageCount();
        if (diff == previewNames.size() || diff < 0) {
            diff /= getSizeC();
            if (getSizeT() > 1 && getSizeZ() == 1)
                ms0.sizeT -= diff;
            else if (getSizeZ() > 1 && getSizeT() == 1)
                ms0.sizeZ -= diff;
        } else
            ms0.imageCount += diff;
    }
    if (getSizeC() > 1 && getSizeZ() == 1 && getSizeT() == 1) {
        if (getDimensionOrder().indexOf('C') == -1)
            ms0.dimensionOrder += 'C';
    }
    if (getDimensionOrder().indexOf('Z') == -1)
        ms0.dimensionOrder += 'Z';
    if (getDimensionOrder().indexOf('C') == -1)
        ms0.dimensionOrder += 'C';
    if (getDimensionOrder().indexOf('T') == -1)
        ms0.dimensionOrder += 'T';
    ms0.pixelType = FormatTools.pixelTypeFromBytes(imageDepth, false, false);
    try {
        RandomAccessInputStream thumb = getFile(thumbId);
        byte[] b = new byte[(int) thumb.length()];
        thumb.read(b);
        thumb.close();
        Location.mapFile("thumbnail.bmp", new ByteArrayHandle(b));
        thumbReader.setId("thumbnail.bmp");
        for (int i = 0; i < getSeriesCount(); i++) {
            core.get(i).thumbSizeX = thumbReader.getSizeX();
            core.get(i).thumbSizeY = thumbReader.getSizeY();
        }
        thumbReader.close();
        Location.mapFile("thumbnail.bmp", null);
    } catch (IOException e) {
        LOGGER.debug("Could not read thumbnail", e);
    } catch (FormatException e) {
        LOGGER.debug("Could not read thumbnail", e);
    }
    // initialize lookup table
    lut = new short[getSizeC()][3][65536];
    byte[] buffer = new byte[65536 * 4];
    int count = (int) Math.min(getSizeC(), lutNames.size());
    for (int c = 0; c < count; c++) {
        Exception exc = null;
        try {
            RandomAccessInputStream stream = getFile(lutNames.get(c));
            stream.seek(stream.length() - 65536 * 4);
            stream.read(buffer);
            stream.close();
            for (int q = 0; q < buffer.length; q += 4) {
                lut[c][0][q / 4] = (short) ((buffer[q + 2] & 0xff) * 257);
                lut[c][1][q / 4] = (short) ((buffer[q + 1] & 0xff) * 257);
                lut[c][2][q / 4] = (short) ((buffer[q] & 0xff) * 257);
            }
        } catch (IOException e) {
            exc = e;
        } catch (FormatException e) {
            exc = e;
        }
        if (exc != null) {
            LOGGER.debug("Could not read LUT", exc);
            lut = null;
            break;
        }
    }
    for (int i = 0; i < getSeriesCount(); i++) {
        CoreMetadata ms = core.get(i);
        ms.rgb = false;
        ms.littleEndian = true;
        ms.interleaved = false;
        ms.metadataComplete = true;
        ms.indexed = lut != null;
        ms.falseColor = true;
        int nFiles = i == 0 ? tiffs.size() : previewNames.size();
        for (int file = 0; file < nFiles; file++) {
            RandomAccessInputStream plane = getFile(i == 0 ? tiffs.get(file) : previewNames.get(file));
            if (plane == null) {
                ifds.add(null);
                continue;
            }
            try {
                TiffParser tp = new TiffParser(plane);
                IFDList ifd = tp.getIFDs();
                ifds.add(ifd);
            } finally {
                plane.close();
            }
        }
    }
    // populate MetadataStore
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, true);
    if (creationDate != null) {
        creationDate = creationDate.replaceAll("'", "");
        creationDate = DateTools.formatDate(creationDate, DATE_FORMAT);
    }
    for (int i = 0; i < getSeriesCount(); i++) {
        // populate Image data
        store.setImageName("Series " + (i + 1), i);
        if (creationDate != null)
            store.setImageAcquisitionDate(new Timestamp(creationDate), i);
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        populateMetadataStore(store, path);
    }
}
Also used : IniList(loci.common.IniList) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Timestamp(ome.xml.model.primitives.Timestamp) IOException(java.io.IOException) TreeMap(java.util.TreeMap) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException) IOException(java.io.IOException) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) IniTable(loci.common.IniTable) IFDList(loci.formats.tiff.IFDList) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle) File(java.io.File) Location(loci.common.Location)

Example 63 with FormatException

use of loci.formats.FormatException in project bioformats by openmicroscopy.

the class FV1000Reader method findOIFFile.

// -- Helper methods --
private String findOIFFile(String baseFile) throws FormatException {
    Location current = new Location(baseFile).getAbsoluteFile();
    String parent = current.getParent();
    Location tmp = new Location(parent).getParentFile();
    parent = tmp.getAbsolutePath();
    baseFile = current.getName();
    if (baseFile == null || baseFile.indexOf('_') == -1)
        return null;
    baseFile = baseFile.substring(0, baseFile.lastIndexOf("_"));
    if (checkSuffix(current.getName(), new String[] { "roi", "lut" })) {
        if (!new Location(tmp, baseFile + ".oif").exists() && !new Location(tmp, baseFile + ".OIF").exists() && baseFile.indexOf('_') >= 0) {
            // some metadata files have an extra underscore
            baseFile = baseFile.substring(0, baseFile.lastIndexOf("_"));
        }
    }
    baseFile += ".oif";
    tmp = new Location(tmp, baseFile);
    String oifFile = tmp.getAbsolutePath();
    if (!tmp.exists()) {
        oifFile = oifFile.substring(0, oifFile.lastIndexOf(".")) + ".OIF";
        tmp = new Location(oifFile);
        if (!tmp.exists()) {
            baseFile = current.getParent();
            baseFile = baseFile.substring(0, baseFile.lastIndexOf("."));
            baseFile = baseFile.substring(0, baseFile.lastIndexOf("."));
            tmp = new Location(baseFile + ".oif");
            oifFile = tmp.getAbsolutePath();
            if (!tmp.exists()) {
                tmp = new Location(tmp.getParent(), tmp.getName().toUpperCase());
                oifFile = tmp.getAbsolutePath();
                if (!tmp.exists()) {
                    // check in parent directory
                    if (parent.endsWith(File.separator)) {
                        parent = parent.substring(0, parent.length() - 1);
                    }
                    String dir = parent.substring(parent.lastIndexOf(File.separator));
                    dir = dir.substring(0, dir.lastIndexOf("."));
                    tmp = new Location(parent);
                    oifFile = new Location(tmp, dir).getAbsolutePath();
                    if (!new Location(oifFile).exists()) {
                        throw new FormatException("OIF file not found");
                    }
                }
            }
        }
    }
    return oifFile;
}
Also used : FormatException(loci.formats.FormatException) Location(loci.common.Location)

Example 64 with FormatException

use of loci.formats.FormatException in project bioformats by openmicroscopy.

the class FV1000Reader method initPOIService.

private void initPOIService() throws FormatException, IOException {
    try {
        ServiceFactory factory = new ServiceFactory();
        poi = factory.getInstance(POIService.class);
    } catch (DependencyException de) {
        throw new FormatException("POI library not found", de);
    }
    poi.initialize(Location.getMappedId(getCurrentFile()));
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) POIService(loci.formats.services.POIService) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException)

Example 65 with FormatException

use of loci.formats.FormatException in project bioformats by openmicroscopy.

the class DNGReader method initStandardMetadata.

// -- Internal BaseTiffReader API methods --
/* @see BaseTiffReader#initStandardMetadata() */
@Override
protected void initStandardMetadata() throws FormatException, IOException {
    super.initStandardMetadata();
    // reset image dimensions
    // the actual image data is stored in IFDs referenced by the SubIFD tag
    // in the 'real' IFD
    CoreMetadata m = core.get(0);
    m.imageCount = ifds.size();
    IFD firstIFD = ifds.get(0);
    PhotoInterp photo = firstIFD.getPhotometricInterpretation();
    int samples = firstIFD.getSamplesPerPixel();
    m.rgb = samples > 1 || photo == PhotoInterp.RGB || photo == PhotoInterp.CFA_ARRAY;
    if (photo == PhotoInterp.CFA_ARRAY)
        samples = 3;
    m.sizeX = (int) firstIFD.getImageWidth();
    m.sizeY = (int) firstIFD.getImageLength();
    m.sizeZ = 1;
    m.sizeC = isRGB() ? samples : 1;
    m.sizeT = ifds.size();
    m.pixelType = FormatTools.UINT16;
    m.indexed = false;
    // now look for the EXIF IFD pointer
    IFDList exifIFDs = tiffParser.getExifIFDs();
    if (exifIFDs.size() > 0) {
        IFD exifIFD = exifIFDs.get(0);
        tiffParser.fillInIFD(exifIFD);
        for (Integer key : exifIFD.keySet()) {
            int tag = key.intValue();
            String name = IFD.getIFDTagName(tag);
            if (tag == IFD.CFA_PATTERN) {
                byte[] cfa = (byte[]) exifIFD.get(key);
                int[] colorMap = new int[cfa.length];
                for (int i = 0; i < cfa.length; i++) colorMap[i] = (int) cfa[i];
                addGlobalMeta(name, colorMap);
                cfaPattern = colorMap;
            } else {
                addGlobalMeta(name, exifIFD.get(key));
                if (name.equals("MAKER_NOTE")) {
                    byte[] b = (byte[]) exifIFD.get(key);
                    int offset = DataTools.bytesToInt(b, b.length - 4, isLittleEndian());
                    byte[] buf = new byte[b.length + offset - 8];
                    System.arraycopy(b, b.length - 8, buf, 0, 8);
                    System.arraycopy(b, 0, buf, offset, b.length - 8);
                    RandomAccessInputStream makerNote = new RandomAccessInputStream(buf);
                    TiffParser tp = new TiffParser(makerNote);
                    IFD note = null;
                    try {
                        note = tp.getFirstIFD();
                    } catch (Exception e) {
                        LOGGER.debug("Failed to parse first IFD", e);
                    }
                    if (note != null) {
                        for (Integer nextKey : note.keySet()) {
                            int nextTag = nextKey.intValue();
                            addGlobalMeta(name, note.get(nextKey));
                            if (nextTag == WHITE_BALANCE_RGB_COEFFS) {
                                if (note.get(nextTag) instanceof TiffRational[]) {
                                    TiffRational[] wb = (TiffRational[]) note.get(nextTag);
                                    whiteBalance = new double[wb.length];
                                    for (int i = 0; i < wb.length; i++) {
                                        whiteBalance[i] = wb[i].doubleValue();
                                    }
                                } else {
                                    // use a default white balance table
                                    whiteBalance = new double[3];
                                    whiteBalance[0] = 2.391381;
                                    whiteBalance[1] = 0.929156;
                                    whiteBalance[2] = 1.298254;
                                }
                            }
                        }
                    }
                    makerNote.close();
                }
            }
        }
    }
}
Also used : IFD(loci.formats.tiff.IFD) PhotoInterp(loci.formats.tiff.PhotoInterp) TiffRational(loci.formats.tiff.TiffRational) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) IOException(java.io.IOException) IFDList(loci.formats.tiff.IFDList) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Aggregations

FormatException (loci.formats.FormatException)246 IOException (java.io.IOException)91 CoreMetadata (loci.formats.CoreMetadata)86 RandomAccessInputStream (loci.common.RandomAccessInputStream)73 MetadataStore (loci.formats.meta.MetadataStore)66 Location (loci.common.Location)48 DependencyException (loci.common.services.DependencyException)44 ServiceException (loci.common.services.ServiceException)43 Length (ome.units.quantity.Length)39 ServiceFactory (loci.common.services.ServiceFactory)35 ArrayList (java.util.ArrayList)33 IFD (loci.formats.tiff.IFD)32 TiffParser (loci.formats.tiff.TiffParser)25 OMEXMLService (loci.formats.services.OMEXMLService)23 Time (ome.units.quantity.Time)23 Timestamp (ome.xml.model.primitives.Timestamp)23 ImagePlus (ij.ImagePlus)21 ImageReader (loci.formats.ImageReader)20 IMetadata (loci.formats.meta.IMetadata)18 IFormatReader (loci.formats.IFormatReader)14