Search in sources :

Example 21 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class HitachiReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    if (!checkSuffix(id, "txt")) {
        String base = id;
        if (base.indexOf('.') >= 0) {
            base = base.substring(0, base.lastIndexOf("."));
        }
        id = base + ".txt";
        initFile(id);
        return;
    }
    super.initFile(id);
    String data = DataTools.readFile(id);
    IniParser parser = new IniParser();
    parser.setBackslashContinuesLine(false);
    IniList ini = parser.parseINI(new BufferedReader(new StringReader(data)));
    IniTable image = ini.getTable("SemImageFile");
    if (image == null) {
        throw new FormatException("Could not find 'SemImageFile' table.");
    }
    for (String key : image.keySet()) {
        addGlobalMeta(key, image.get(key));
    }
    String imageName = image.get("SampleName");
    String pixelsFile = image.get("ImageName");
    String date = image.get("Date");
    String time = image.get("Time");
    Location parent = new Location(id).getAbsoluteFile().getParentFile();
    pixelsFile = new Location(parent, pixelsFile).getAbsolutePath();
    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(HitachiReader.class)) {
            newClasses.addClass(c);
        }
    }
    helperReader = new ImageReader(newClasses);
    helperReader.setId(pixelsFile);
    core = new ArrayList<CoreMetadata>(helperReader.getCoreMetadataList());
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM);
    store.setImageName(imageName, 0);
    date = DateTools.formatDate(date + " " + time, DATE_FORMAT);
    if (date != null) {
        store.setImageAcquisitionDate(new Timestamp(date), 0);
    }
    populateOMEMetadata(image, store);
}
Also used : IniParser(loci.common.IniParser) IFormatReader(loci.formats.IFormatReader) IniList(loci.common.IniList) ClassList(loci.formats.ClassList) CoreMetadata(loci.formats.CoreMetadata) Timestamp(ome.xml.model.primitives.Timestamp) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) IniTable(loci.common.IniTable) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) ImageReader(loci.formats.ImageReader) Location(loci.common.Location)

Example 22 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class ImaconReader method initMetadataStore.

/* @see BaseTiffReader#initMetadataStore() */
@Override
protected void initMetadataStore() throws FormatException {
    super.initMetadataStore();
    MetadataStore store = makeFilterMetadata();
    if (creationDate != null) {
        creationDate = DateTools.formatDate(creationDate, "yyyyMMdd HHmmSSZ");
    }
    for (int i = 0; i < getSeriesCount(); i++) {
        String name = imageName;
        if (imageName.length() == 0) {
            name = "#" + (i + 1);
        } else {
            name += " #" + (i + 1);
        }
        store.setImageName(name, i);
        if (creationDate != null) {
            store.setImageAcquisitionDate(new Timestamp(creationDate), i);
        }
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        if (experimenterName == null)
            experimenterName = "";
        int nameSpace = experimenterName.indexOf(' ');
        String firstName = nameSpace == -1 ? "" : experimenterName.substring(0, nameSpace);
        String lastName = nameSpace == -1 ? experimenterName : experimenterName.substring(nameSpace + 1);
        String experimenter = MetadataTools.createLSID("Experimenter", 0);
        store.setExperimenterID(experimenter, 0);
        store.setExperimenterFirstName(firstName, 0);
        store.setExperimenterLastName(lastName, 0);
        for (int i = 0; i < getSeriesCount(); i++) {
            store.setImageExperimenterRef(experimenter, i);
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Timestamp(ome.xml.model.primitives.Timestamp)

Example 23 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class ImarisHDFReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    try {
        ServiceFactory factory = new ServiceFactory();
        netcdf = factory.getInstance(NetCDFService.class);
        netcdf.setFile(id);
    } catch (DependencyException e) {
        throw new MissingLibraryException(NetCDFServiceImpl.NO_NETCDF_MSG, e);
    }
    pixelSizeX = pixelSizeY = pixelSizeZ = 1;
    emWave = new ArrayList<String>();
    exWave = new ArrayList<String>();
    channelMin = new ArrayList<String>();
    channelMax = new ArrayList<String>();
    gain = new ArrayList<String>();
    pinhole = new ArrayList<String>();
    channelName = new ArrayList<String>();
    microscopyMode = new ArrayList<String>();
    colors = new ArrayList<double[]>();
    seriesCount = 0;
    // read all of the metadata key/value pairs
    parseAttributes();
    CoreMetadata ms0 = core.get(0);
    if (seriesCount > 1) {
        for (int i = 1; i < seriesCount; i++) {
            core.add(new CoreMetadata());
        }
        for (int i = 1; i < seriesCount; i++) {
            CoreMetadata ms = core.get(i);
            String groupPath = "DataSet/ResolutionLevel_" + i + "/TimePoint_0/Channel_0";
            ms.sizeX = Integer.parseInt(netcdf.getAttributeValue(groupPath + "/ImageSizeX"));
            ms.sizeY = Integer.parseInt(netcdf.getAttributeValue(groupPath + "/ImageSizeY"));
            ms.sizeZ = Integer.parseInt(netcdf.getAttributeValue(groupPath + "/ImageSizeZ"));
            ms.imageCount = ms.sizeZ * getSizeC() * getSizeT();
            ms.sizeC = getSizeC();
            ms.sizeT = getSizeT();
            ms.thumbnail = true;
            if (ms.sizeZ == ms0.sizeZ && ms.sizeC == ms0.sizeC && ms.sizeT == ms0.sizeT) {
                // do not assume that all series will have the same dimensions
                // if the Z, C or T size is different, then it cannot
                // be a subresolution
                ms0.resolutionCount++;
            }
        }
    }
    ms0.imageCount = getSizeZ() * getSizeC() * getSizeT();
    ms0.thumbnail = false;
    ms0.dimensionOrder = "XYZCT";
    // determine pixel type - this isn't stored in the metadata, so we need
    // to check the pixels themselves
    int type = -1;
    Object pix = getImageData(0, 0, 0, 1, 1);
    if (pix instanceof byte[][])
        type = FormatTools.UINT8;
    else if (pix instanceof short[][])
        type = FormatTools.UINT16;
    else if (pix instanceof int[][])
        type = FormatTools.UINT32;
    else if (pix instanceof float[][])
        type = FormatTools.FLOAT;
    else if (pix instanceof double[][])
        type = FormatTools.DOUBLE;
    else {
        throw new FormatException("Unknown pixel type: " + pix);
    }
    for (int i = 0; i < core.size(); i++) {
        CoreMetadata ms = core.get(i);
        ms.pixelType = type;
        ms.dimensionOrder = "XYZCT";
        ms.rgb = false;
        ms.thumbSizeX = 128;
        ms.thumbSizeY = 128;
        ms.orderCertain = true;
        ms.littleEndian = true;
        ms.interleaved = false;
        ms.indexed = colors.size() >= getSizeC();
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    String imageName = new Location(getCurrentFile()).getName();
    for (int s = 0; s < getSeriesCount(); s++) {
        store.setImageName(imageName + " Resolution Level " + (s + 1), s);
    }
    if (getMetadataOptions().getMetadataLevel() == MetadataLevel.MINIMUM) {
        return;
    }
    int cIndex = 0;
    for (int s = 0; s < getSeriesCount(); s++) {
        setSeries(s);
        double px = pixelSizeX, py = pixelSizeY, pz = pixelSizeZ;
        if (px == 1)
            px = (maxX - minX) / getSizeX();
        if (py == 1)
            py = (maxY - minY) / getSizeY();
        if (pz == 1)
            pz = (maxZ - minZ) / getSizeZ();
        Length sizeX = FormatTools.getPhysicalSizeX(px);
        Length sizeY = FormatTools.getPhysicalSizeY(py);
        Length sizeZ = FormatTools.getPhysicalSizeZ(pz);
        if (sizeX != null) {
            store.setPixelsPhysicalSizeX(sizeX, s);
        }
        if (sizeY != null) {
            store.setPixelsPhysicalSizeY(sizeY, s);
        }
        if (sizeZ != null) {
            store.setPixelsPhysicalSizeZ(sizeZ, s);
        }
        for (int i = 0; i < getSizeC(); i++, cIndex++) {
            Float gainValue = null;
            Integer pinholeValue = null, emWaveValue = null, exWaveValue;
            if (cIndex < gain.size()) {
                try {
                    gainValue = new Float(gain.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            if (cIndex < pinhole.size()) {
                try {
                    pinholeValue = new Integer(pinhole.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            if (cIndex < emWave.size()) {
                try {
                    emWaveValue = new Integer(emWave.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            if (cIndex < exWave.size()) {
                try {
                    exWaveValue = new Integer(exWave.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            Double minValue = null, maxValue = null;
            if (cIndex < channelMin.size()) {
                try {
                    minValue = new Double(channelMin.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            if (cIndex < channelMax.size()) {
                try {
                    maxValue = new Double(channelMax.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            if (i < colors.size()) {
                double[] color = colors.get(i);
                Color realColor = new Color((int) (color[0] * 255), (int) (color[1] * 255), (int) (color[2] * 255), 255);
                store.setChannelColor(realColor, s, i);
            }
        }
    }
    setSeries(0);
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) Color(ome.xml.model.primitives.Color) DependencyException(loci.common.services.DependencyException) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) MissingLibraryException(loci.formats.MissingLibraryException) NetCDFService(loci.formats.services.NetCDFService) Location(loci.common.Location)

Example 24 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class FujiReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    if (checkSuffix(id, "inf")) {
        infFile = new Location(id).getAbsolutePath();
        pixelsFile = infFile.substring(0, infFile.lastIndexOf(".")) + ".img";
    } else {
        pixelsFile = new Location(id).getAbsolutePath();
        infFile = pixelsFile.substring(0, pixelsFile.lastIndexOf(".")) + ".inf";
    }
    String[] lines = DataTools.readFile(infFile).split("\r{0,1}\n");
    int bits = Integer.parseInt(lines[5]);
    CoreMetadata m = core.get(0);
    m.pixelType = FormatTools.pixelTypeFromBytes(bits / 8, false, false);
    m.sizeX = Integer.parseInt(lines[6]);
    m.sizeY = Integer.parseInt(lines[7]);
    m.sizeC = 1;
    m.sizeT = 1;
    m.sizeZ = 1;
    m.imageCount = getSizeZ() * getSizeC() * getSizeT();
    m.dimensionOrder = "XYCZT";
    for (String line : lines) {
        addGlobalMetaList("Line", line);
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    String imageName = lines[1];
    String timestamp = lines[10];
    timestamp = DateTools.formatDate(timestamp, DATE_FORMAT);
    store.setImageName(imageName, 0);
    if (timestamp != null) {
        store.setImageAcquisitionDate(new Timestamp(timestamp), 0);
    }
    double physicalWidth = Double.parseDouble(lines[3]);
    double physicalHeight = Double.parseDouble(lines[4]);
    Length sizeX = FormatTools.getPhysicalSizeX(physicalWidth);
    Length sizeY = FormatTools.getPhysicalSizeY(physicalHeight);
    if (sizeX != null) {
        store.setPixelsPhysicalSizeX(sizeX, 0);
    }
    if (sizeY != null) {
        store.setPixelsPhysicalSizeY(sizeY, 0);
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        String instrument = lines[13];
        String instrumentID = MetadataTools.createLSID("Instrument", 0);
        store.setInstrumentID(instrumentID, 0);
        store.setMicroscopeModel(instrument, 0);
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) CoreMetadata(loci.formats.CoreMetadata) Timestamp(ome.xml.model.primitives.Timestamp) Location(loci.common.Location)

Example 25 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class GatanReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    in = new RandomAccessInputStream(id);
    pixelOffset = 0;
    CoreMetadata m = core.get(0);
    LOGGER.info("Verifying Gatan format");
    m.littleEndian = false;
    pixelSizes = new ArrayList<Double>();
    units = new ArrayList<String>();
    shapes = new ArrayList<ROIShape>();
    in.order(isLittleEndian());
    // only support version 3
    version = in.readInt();
    if (version != 3 && version != 4) {
        throw new FormatException("invalid header");
    }
    LOGGER.info("Reading tags");
    in.skipBytes(4);
    skipPadding();
    m.littleEndian = in.readInt() != 1;
    in.order(isLittleEndian());
    // TagGroup instance
    in.skipBytes(2);
    skipPadding();
    int numTags = in.readInt();
    if (numTags > in.length()) {
        m.littleEndian = !isLittleEndian();
        in.order(isLittleEndian());
        adjustEndianness = false;
    }
    LOGGER.debug("tags ({}) {", numTags);
    try {
        parseTags(numTags, null, "  ");
    } catch (Exception e) {
        throw new FormatException("Unable to parse metadata tag", e);
    }
    LOGGER.debug("}");
    LOGGER.info("Populating metadata");
    m.littleEndian = true;
    if (getSizeX() == 0 || getSizeY() == 0) {
        throw new FormatException("Dimensions information not found");
    }
    if (m.sizeZ == 0) {
        m.sizeZ = 1;
    }
    m.sizeC = 1;
    m.sizeT = 1;
    m.dimensionOrder = "XYZTC";
    m.imageCount = getSizeZ() * getSizeC() * getSizeT();
    int bytes = (int) (numPixelBytes / (getSizeX() * getSizeY() * (long) getImageCount()));
    if (bytes != FormatTools.getBytesPerPixel(getPixelType())) {
        m.pixelType = FormatTools.pixelTypeFromBytes(bytes, signed, false);
    }
    m.rgb = false;
    m.interleaved = false;
    m.metadataComplete = true;
    m.indexed = false;
    m.falseColor = false;
    // The metadata store we're working with.
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, true);
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        int index = 0;
        if (pixelSizes.size() > 4) {
            index = pixelSizes.size() - 3;
        } else if (pixelSizes.size() == 4) {
            if (Math.abs(pixelSizes.get(0) - 1.0) < Constants.EPSILON) {
                index = pixelSizes.size() - 2;
            }
        }
        if (index + 2 < pixelSizes.size() && Math.abs(pixelSizes.get(index + 1) - pixelSizes.get(index + 2)) < Constants.EPSILON) {
            if (Math.abs(pixelSizes.get(index) - pixelSizes.get(index + 1)) > Constants.EPSILON && getSizeY() > 1) {
                index++;
            }
        }
        if (index < pixelSizes.size() - 1) {
            Double x = pixelSizes.get(index);
            Double y = pixelSizes.get(index + 1);
            String xUnits = index < units.size() ? units.get(index) : "";
            String yUnits = index + 1 < units.size() ? units.get(index + 1) : "";
            Length sizeX = FormatTools.getPhysicalSizeX(x, convertUnits(xUnits));
            Length sizeY = FormatTools.getPhysicalSizeY(y, convertUnits(yUnits));
            if (sizeX != null) {
                store.setPixelsPhysicalSizeX(sizeX, 0);
            }
            if (sizeY != null) {
                store.setPixelsPhysicalSizeY(sizeY, 0);
            }
            if (index < pixelSizes.size() - 2) {
                Double z = pixelSizes.get(index + 2);
                String zUnits = index + 2 < units.size() ? units.get(index + 2) : "";
                Length sizeZ = FormatTools.getPhysicalSizeZ(z, convertUnits(zUnits));
                if (sizeZ != null) {
                    store.setPixelsPhysicalSizeZ(sizeZ, 0);
                }
            }
        }
        String instrument = MetadataTools.createLSID("Instrument", 0);
        store.setInstrumentID(instrument, 0);
        store.setImageInstrumentRef(instrument, 0);
        String objective = MetadataTools.createLSID("Objective", 0, 0);
        store.setObjectiveID(objective, 0, 0);
        store.setObjectiveCorrection(getCorrection("Unknown"), 0, 0);
        store.setObjectiveImmersion(getImmersion("Unknown"), 0, 0);
        store.setObjectiveNominalMagnification(mag, 0, 0);
        store.setObjectiveSettingsID(objective, 0);
        String detector = MetadataTools.createLSID("Detector", 0, 0);
        store.setDetectorID(detector, 0, 0);
        store.setDetectorSettingsID(detector, 0, 0);
        store.setDetectorSettingsVoltage(new ElectricPotential(voltage, UNITS.VOLT), 0, 0);
        if (info == null)
            info = "";
        String[] scopeInfo = info.split("\\(");
        for (String token : scopeInfo) {
            token = token.trim();
            if (token.startsWith("Mode")) {
                token = token.substring(token.indexOf(' ')).trim();
                String mode = token.substring(0, token.indexOf(' ')).trim();
                if (mode.equals("TEM"))
                    mode = "Other";
                store.setChannelAcquisitionMode(getAcquisitionMode(mode), 0, 0);
            }
        }
        store.setPlanePositionX(posX, 0, 0);
        store.setPlanePositionY(posY, 0, 0);
        store.setPlanePositionZ(posZ, 0, 0);
        for (int i = 0; i < getImageCount(); i++) {
            store.setPlaneExposureTime(new Time(sampleTime, UNITS.SECOND), 0, i);
        }
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.NO_OVERLAYS && shapes.size() > 0) {
        for (int i = 0; i < shapes.size(); i++) {
            String roi = MetadataTools.createLSID("ROI", i);
            store.setROIID(roi, i);
            store.setImageROIRef(roi, 0, i);
            String shapeID = MetadataTools.createLSID("Shape", i, 0);
            ROIShape shape = shapes.get(i);
            switch(shape.type) {
                case LINE:
                    store.setLineID(shapeID, i, 0);
                    store.setLineX1(shape.x1, i, 0);
                    store.setLineY1(shape.y1, i, 0);
                    store.setLineX2(shape.x2, i, 0);
                    store.setLineY2(shape.y2, i, 0);
                    store.setLineText(shape.text, i, 0);
                    store.setLineFontSize(shape.fontSize, i, 0);
                    store.setLineStrokeColor(shape.strokeColor, i, 0);
                    break;
                case TEXT:
                    store.setLabelID(shapeID, i, 0);
                    store.setLabelX(shape.x1, i, 0);
                    store.setLabelY(shape.y1, i, 0);
                    store.setLabelText(shape.text, i, 0);
                    store.setLabelFontSize(shape.fontSize, i, 0);
                    store.setLabelStrokeColor(shape.strokeColor, i, 0);
                    break;
                case ELLIPSE:
                    store.setEllipseID(shapeID, i, 0);
                    double radiusX = (shape.x2 - shape.x1) / 2;
                    double radiusY = (shape.y2 - shape.y1) / 2;
                    store.setEllipseX(shape.x1 + radiusX, i, 0);
                    store.setEllipseY(shape.y1 + radiusY, i, 0);
                    store.setEllipseRadiusX(radiusX, i, 0);
                    store.setEllipseRadiusY(radiusY, i, 0);
                    store.setEllipseText(shape.text, i, 0);
                    store.setEllipseFontSize(shape.fontSize, i, 0);
                    store.setEllipseStrokeColor(shape.strokeColor, i, 0);
                    break;
                case RECTANGLE:
                    store.setRectangleID(shapeID, i, 0);
                    store.setRectangleX(shape.x1, i, 0);
                    store.setRectangleY(shape.y1, i, 0);
                    store.setRectangleWidth(shape.x2 - shape.x1, i, 0);
                    store.setRectangleHeight(shape.y2 - shape.y1, i, 0);
                    store.setRectangleText(shape.text, i, 0);
                    store.setRectangleFontSize(shape.fontSize, i, 0);
                    store.setRectangleStrokeColor(shape.strokeColor, i, 0);
                    break;
                default:
                    LOGGER.warn("Unknown ROI type: {}", shape.type);
            }
        }
    }
}
Also used : Time(ome.units.quantity.Time) CoreMetadata(loci.formats.CoreMetadata) ElectricPotential(ome.units.quantity.ElectricPotential) FormatException(loci.formats.FormatException) FormatException(loci.formats.FormatException) IOException(java.io.IOException) ParseException(java.text.ParseException) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Aggregations

MetadataStore (loci.formats.meta.MetadataStore)180 CoreMetadata (loci.formats.CoreMetadata)130 RandomAccessInputStream (loci.common.RandomAccessInputStream)97 Length (ome.units.quantity.Length)82 FormatException (loci.formats.FormatException)66 Timestamp (ome.xml.model.primitives.Timestamp)54 Location (loci.common.Location)51 Time (ome.units.quantity.Time)41 ArrayList (java.util.ArrayList)34 IFD (loci.formats.tiff.IFD)21 DependencyException (loci.common.services.DependencyException)16 IOException (java.io.IOException)15 TiffParser (loci.formats.tiff.TiffParser)15 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)15 ServiceFactory (loci.common.services.ServiceFactory)14 PositiveInteger (ome.xml.model.primitives.PositiveInteger)13 IFDList (loci.formats.tiff.IFDList)12 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)11 ServiceException (loci.common.services.ServiceException)10 Map (java.util.Map)9