use of loci.common.IniParser in project bioformats by openmicroscopy.
the class LiFlimReader method parseHeader.
// -- Helper methods --
private void parseHeader() throws IOException {
String headerData = in.findString("{END}");
dataOffset = in.getFilePointer();
IniParser parser = new IniParser();
ini = parser.parseINI(new BufferedReader(new StringReader(headerData)));
}
use of loci.common.IniParser in project bioformats by openmicroscopy.
the class FEITiffReader method initStandardMetadata.
// -- Internal BaseTiffReader API methods --
/* @see BaseTiffReader#initStandardMetadata() */
@Override
protected void initStandardMetadata() throws FormatException, IOException {
super.initStandardMetadata();
boolean helios = ifds.get(0).containsKey(HELIOS_TAG);
boolean titan = ifds.get(0).containsKey(TITAN_TAG);
// Helios etc data might have a stray Titan tag
if (titan && ifds.get(0).getIFDTextValue(TITAN_TAG).trim().isEmpty()) {
titan = false;
}
// Titan data (always?) has an empty Helios tag as well, so the Titan tag is checked first
String software = "S-FEG";
if (titan) {
software = "Titan";
} else if (helios) {
software = "Helios NanoLab";
}
addGlobalMeta("Software", software);
int tagKey = SFEG_TAG;
if (titan) {
tagKey = TITAN_TAG;
} else if (helios) {
tagKey = HELIOS_TAG;
}
String tag = ifds.get(0).getIFDTextValue(tagKey);
if (tag == null) {
return;
}
tag = tag.trim();
if (tag.isEmpty()) {
// fall back to regular reader
return;
}
// store metadata for later conversion to OME-XML
if (tag.startsWith("<")) {
XMLTools.parseXML(tag, new FEIHandler());
} else {
IniParser parser = new IniParser();
IniList ini = parser.parseINI(new BufferedReader(new StringReader(tag)));
detectors = new ArrayList<String>();
if (helios) {
IniTable userTable = ini.getTable("User");
date = userTable.get("Date") + " " + userTable.get("Time");
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
userName = userTable.get("User");
IniTable systemTable = ini.getTable("System");
if (systemTable == null) {
systemTable = ini.getTable("SYSTEM");
}
if (systemTable != null) {
microscopeModel = systemTable.get("SystemType");
}
IniTable beamTable = ini.getTable("Beam");
if (beamTable != null) {
String beamTableName = beamTable.get("Beam");
if (beamTableName != null) {
beamTable = ini.getTable(beamTableName);
}
}
if (beamTable != null) {
String beamX = beamTable.get("StageX");
String beamY = beamTable.get("StageY");
String beamZ = beamTable.get("StageZ");
IniTable stageTable = ini.getTable("Stage");
if (beamX != null) {
final Double number = Double.valueOf(beamX);
stageX = new Length(number, UNITS.REFERENCEFRAME);
} else if (stageTable != null) {
final Double number = Double.valueOf(stageTable.get("StageX"));
stageX = new Length(number, UNITS.REFERENCEFRAME);
}
if (beamY != null) {
final Double number = Double.valueOf(beamY);
stageY = new Length(number, UNITS.REFERENCEFRAME);
} else if (stageTable != null) {
final Double number = Double.valueOf(stageTable.get("StageY"));
stageY = new Length(number, UNITS.REFERENCEFRAME);
}
if (beamZ != null) {
final Double number = Double.valueOf(beamZ);
stageZ = new Length(number, UNITS.REFERENCEFRAME);
} else if (stageTable != null) {
final Double number = Double.valueOf(stageTable.get("StageZ"));
stageZ = new Length(number, UNITS.REFERENCEFRAME);
}
}
IniTable scanTable = ini.getTable("Scan");
sizeX = new Double(scanTable.get("PixelWidth"));
sizeY = new Double(scanTable.get("PixelHeight"));
timeIncrement = new Double(scanTable.get("FrameTime"));
}
} else {
IniTable dataTable = ini.getTable("DatabarData");
imageName = dataTable.get("ImageName");
imageDescription = dataTable.get("szUserText");
String magnification = ini.getTable("Vector").get("Magnification");
sizeX = new Double(magnification) * MAG_MULTIPLIER;
sizeY = new Double(magnification) * MAG_MULTIPLIER;
IniTable scanTable = ini.getTable("Vector.Sysscan");
final Double posX = Double.valueOf(scanTable.get("PositionX"));
final Double posY = Double.valueOf(scanTable.get("PositionY"));
stageX = new Length(posX, UNITS.REFERENCEFRAME);
stageY = new Length(posY, UNITS.REFERENCEFRAME);
IniTable detectorTable = ini.getTable("Vector.Video.Detectors");
int detectorCount = Integer.parseInt(detectorTable.get("NrDetectorsConnected"));
for (int i = 0; i < detectorCount; i++) {
detectors.add(detectorTable.get("Detector_" + i + "_Name"));
}
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
HashMap<String, String> iniMap = ini.flattenIntoHashMap();
metadata.putAll(iniMap);
}
}
}
use of loci.common.IniParser 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);
}
use of loci.common.IniParser in project bioformats by openmicroscopy.
the class HamamatsuVMSReader 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);
IniParser parser = new IniParser();
IniList layout = parser.parseINI(new BufferedReader(new InputStreamReader(in, Constants.ENCODING)));
IniTable slideInfo = layout.getTable("Virtual Microscope Specimen");
int nLayers = Integer.parseInt(slideInfo.get("NoLayers"));
nRows = Integer.parseInt(slideInfo.get("NoJpegRows"));
nCols = Integer.parseInt(slideInfo.get("NoJpegColumns"));
String imageFile = slideInfo.get("ImageFile");
mapFile = slideInfo.get("MapFile");
String optimisationFile = slideInfo.get("OptimisationFile");
macroFile = slideInfo.get("MacroImage");
Double physicalWidth = new Double(slideInfo.get("PhysicalWidth"));
Double physicalHeight = new Double(slideInfo.get("PhysicalHeight"));
Double magnification = new Double(slideInfo.get("SourceLens"));
Double macroWidth = new Double(slideInfo.get("PhysicalMacroWidth"));
Double macroHeight = new Double(slideInfo.get("PhysicalMacroHeight"));
for (String key : slideInfo.keySet()) {
addGlobalMeta(key, slideInfo.get(key));
}
Location dir = new Location(id).getAbsoluteFile().getParentFile();
if (imageFile != null) {
imageFile = new Location(dir, imageFile).getAbsolutePath();
files.add(imageFile);
}
tileFiles = new String[nLayers][nRows][nCols];
tileFiles[0][0][0] = imageFile;
for (int layer = 0; layer < nLayers; layer++) {
for (int row = 0; row < nRows; row++) {
for (int col = 0; col < nCols; col++) {
String f = slideInfo.get("ImageFile(" + col + "," + row + ")");
if (f != null) {
f = new Location(dir, f).getAbsolutePath();
tileFiles[layer][row][col] = f;
files.add(f);
}
}
}
}
if (mapFile != null) {
mapFile = new Location(dir, mapFile).getAbsolutePath();
files.add(mapFile);
}
if (optimisationFile != null) {
optimisationFile = new Location(dir, optimisationFile).getAbsolutePath();
files.add(optimisationFile);
}
if (macroFile != null) {
macroFile = new Location(dir, macroFile).getAbsolutePath();
files.add(macroFile);
}
int seriesCount = 3;
core.clear();
for (int i = 0; i < seriesCount; i++) {
String file = null;
switch(i) {
case 0:
file = tileFiles[0][nRows - 1][nCols - 1];
break;
case 1:
file = macroFile;
break;
case 2:
file = mapFile;
break;
}
int[] dims;
try (RandomAccessInputStream s = new RandomAccessInputStream(file);
JPEGTileDecoder decoder = new JPEGTileDecoder()) {
dims = decoder.preprocess(s);
}
CoreMetadata m = new CoreMetadata();
if (i == 0) {
m.sizeX = (MAX_JPEG_SIZE * (nCols - 1)) + dims[0];
m.sizeY = (MAX_JPEG_SIZE * (nRows - 1)) + dims[1];
} else {
m.sizeX = dims[0];
m.sizeY = dims[1];
}
m.sizeZ = 1;
m.sizeC = 3;
m.sizeT = 1;
m.rgb = true;
m.imageCount = 1;
m.dimensionOrder = "XYCZT";
m.pixelType = FormatTools.UINT8;
m.interleaved = m.sizeX > MAX_SIZE && m.sizeY > MAX_SIZE;
m.thumbnail = i > 0;
core.add(m);
}
CoreMetadata ms0 = core.get(0);
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
String path = new Location(currentId).getAbsoluteFile().getName();
store.setImageName(path + " full resolution", 0);
store.setImageName(path + " macro", 1);
store.setImageName(path + " map", 2);
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
Length sizeX = FormatTools.getPhysicalSizeX(physicalWidth / ms0.sizeX);
Length sizeY = FormatTools.getPhysicalSizeY(physicalHeight / ms0.sizeY);
Length macroSizeX = FormatTools.getPhysicalSizeX(macroWidth / core.get(1).sizeX);
Length macroSizeY = FormatTools.getPhysicalSizeY(macroHeight / core.get(1).sizeY);
if (sizeX != null) {
store.setPixelsPhysicalSizeX(sizeX, 0);
}
if (sizeY != null) {
store.setPixelsPhysicalSizeY(sizeY, 0);
}
if (macroSizeX != null) {
store.setPixelsPhysicalSizeX(macroSizeX, 1);
}
if (macroSizeY != null) {
store.setPixelsPhysicalSizeY(macroSizeY, 1);
}
String instrumentID = MetadataTools.createLSID("Instrument", 0);
store.setInstrumentID(instrumentID, 0);
store.setImageInstrumentRef(instrumentID, 0);
String objectiveID = MetadataTools.createLSID("Objective", 0, 0);
store.setObjectiveID(objectiveID, 0, 0);
store.setObjectiveNominalMagnification(magnification, 0, 0);
store.setObjectiveSettingsID(objectiveID, 0);
}
}
use of loci.common.IniParser in project bioformats by openmicroscopy.
the class TillVisionReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
if (!checkSuffix(id, "vws")) {
Location pst = new Location(id).getAbsoluteFile();
String name = pst.getParentFile().getName();
Location parent = pst.getParentFile().getParentFile();
Location vwsFile = new Location(parent, name.replaceAll(".pst", ".vws"));
if (vwsFile.exists() && !vwsFile.isDirectory()) {
id = vwsFile.getAbsolutePath();
} else if (vwsFile.isDirectory()) {
parent = pst.getParentFile();
String[] list = parent.list(true);
boolean foundVWS = false;
for (String f : list) {
if (checkSuffix(f, "vws")) {
id = new Location(parent, f).getAbsolutePath();
foundVWS = true;
break;
}
}
if (!foundVWS) {
throw new FormatException("Could not find .vws file.");
}
} else
throw new FormatException("Could not find .vws file.");
}
super.initFile(id);
exposureTimes = new HashMap<Integer, Double>();
POIService poi = null;
try {
ServiceFactory factory = new ServiceFactory();
poi = factory.getInstance(POIService.class);
} catch (DependencyException de) {
throw new FormatException("POI library not found", de);
}
poi.initialize(id);
Vector<String> documents = poi.getDocumentList();
int nImages = 0;
final Hashtable<String, Object> tmpSeriesMetadata = new Hashtable<String, Object>();
for (String name : documents) {
LOGGER.debug("Reading {}", name);
if (name.equals("Root Entry" + File.separator + "Contents")) {
RandomAccessInputStream s = poi.getDocumentStream(name);
s.order(true);
boolean specialCImage = false;
int nFound = 0;
Long[] cimages = null;
Location dir = new Location(id).getAbsoluteFile().getParentFile();
String[] list = dir.list(true);
boolean hasPST = false;
for (String f : list) {
if (checkSuffix(f, "pst")) {
hasPST = true;
break;
}
}
if (!hasPST) {
cimages = findImages(s);
nFound = cimages.length;
if (nFound == 0) {
s.seek(13);
int len = s.readShort();
String type = s.readString(len);
if (type.equals("CImage")) {
nFound = 1;
cimages = new Long[] { s.getFilePointer() + 6 };
specialCImage = true;
}
}
embeddedImages = nFound > 0;
}
LOGGER.debug("Images are {}embedded", embeddedImages ? "" : "not ");
if (embeddedImages) {
core.clear();
embeddedOffset = new long[nFound];
for (int i = 0; i < nFound; i++) {
CoreMetadata ms = new CoreMetadata();
core.add(ms);
s.seek(cimages[i]);
int len = s.read();
String imageName = s.readString(len);
imageNames.add(imageName);
if (specialCImage) {
s.seek(1280);
} else {
while (true) {
if (s.readString(2).equals("sB")) {
break;
} else
s.seek(s.getFilePointer() - 1);
}
}
s.skipBytes(20);
ms.sizeX = s.readInt();
ms.sizeY = s.readInt();
ms.sizeZ = s.readInt();
ms.sizeC = s.readInt();
ms.sizeT = s.readInt();
ms.pixelType = convertPixelType(s.readInt());
if (specialCImage) {
embeddedOffset[i] = s.getFilePointer() + 27;
} else {
embeddedOffset[i] = s.getFilePointer() + 31;
}
}
if (in != null)
in.close();
in = poi.getDocumentStream(name);
s.close();
break;
}
s.seek(0);
int lowerBound = 0;
int upperBound = 0x1000;
while (s.getFilePointer() < s.length() - 2) {
LOGGER.debug(" Looking for image at {}", s.getFilePointer());
s.order(false);
int nextOffset = findNextOffset(s);
if (nextOffset < 0 || nextOffset >= s.length())
break;
s.seek(nextOffset);
s.skipBytes(3);
int len = s.readShort();
if (len <= 0)
continue;
imageNames.add(s.readString(len));
if (s.getFilePointer() + 8 >= s.length())
break;
s.skipBytes(6);
s.order(true);
len = s.readShort();
if (nImages == 0 && len > upperBound * 2 && len < upperBound * 4) {
lowerBound = 512;
upperBound = 0x4000;
}
if (len < lowerBound || len > upperBound)
continue;
String description = s.readString(len);
LOGGER.debug("Description: {}", description);
// parse key/value pairs from description
String dateTime = "";
String[] lines = description.split("[\r\n]");
for (String line : lines) {
line = line.trim();
int colon = line.indexOf(':');
if (colon != -1 && !line.startsWith(";")) {
String key = line.substring(0, colon).trim();
String value = line.substring(colon + 1).trim();
String metaKey = "Series " + nImages + " " + key;
addMeta(metaKey, value, tmpSeriesMetadata);
if (key.equals("Start time of experiment")) {
// HH:mm:ss aa OR HH:mm:ss.sss aa
dateTime += " " + value;
} else if (key.equals("Date")) {
// mm/dd/yy ?
dateTime = value + " " + dateTime;
} else if (key.equals("Exposure time [ms]")) {
double exp = Double.parseDouble(value) / 1000;
exposureTimes.put(nImages, exp);
} else if (key.equals("Image type")) {
types.add(value);
}
}
}
dateTime = dateTime.trim();
if (!dateTime.equals("")) {
boolean success = false;
for (String format : DATE_FORMATS) {
try {
dateTime = DateTools.formatDate(dateTime, format, ".");
success = true;
} catch (NullPointerException e) {
}
}
dates.add(success ? dateTime : "");
}
nImages++;
}
s.close();
}
}
Location directory = new Location(currentId).getAbsoluteFile().getParentFile();
String[] pixelsFile = new String[nImages];
if (!embeddedImages) {
if (nImages == 0) {
throw new FormatException("No images found.");
}
// look for appropriate pixels files
String[] files = directory.list(true);
String name = currentId.substring(currentId.lastIndexOf(File.separator) + 1, currentId.lastIndexOf("."));
int nextFile = 0;
for (String f : files) {
if (checkSuffix(f, "pst")) {
Location pst = new Location(directory, f);
if (pst.isDirectory() && f.startsWith(name)) {
String[] subfiles = pst.list(true);
Arrays.sort(subfiles);
for (String q : subfiles) {
if (checkSuffix(q, "pst") && nextFile < nImages) {
pixelsFile[nextFile++] = f + File.separator + q;
}
}
}
}
}
if (nextFile == 0) {
for (String f : files) {
if (checkSuffix(f, "pst")) {
pixelsFile[nextFile++] = new Location(directory, f).getAbsolutePath();
}
}
if (nextFile == 0)
throw new FormatException("No image files found.");
}
}
Arrays.sort(pixelsFile);
int nSeries = core.size();
if (!embeddedImages) {
core.clear();
nSeries = nImages;
}
pixelsFiles = new String[nSeries];
infFiles = new String[nSeries];
Object[] metadataKeys = tmpSeriesMetadata.keySet().toArray();
IniParser parser = new IniParser();
for (int i = 0; i < nSeries; i++) {
CoreMetadata ms;
if (!embeddedImages) {
ms = new CoreMetadata();
core.add(ms);
setSeries(i);
// make sure that pixels file exists
String file = pixelsFile[i];
file = file.replace('/', File.separatorChar);
file = file.replace('\\', File.separatorChar);
String oldFile = file;
Location f = new Location(directory, oldFile);
if (!f.exists()) {
oldFile = oldFile.substring(oldFile.lastIndexOf(File.separator) + 1);
f = new Location(directory, oldFile);
if (!f.exists()) {
throw new FormatException("Could not find pixels file '" + file);
}
}
file = f.getAbsolutePath();
pixelsFiles[i] = file;
// read key/value pairs from .inf files
int dot = file.lastIndexOf(".");
String inf = file.substring(0, dot) + ".inf";
infFiles[i] = inf;
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inf), Constants.ENCODING));
IniList data = parser.parseINI(reader);
reader.close();
IniTable infoTable = data.getTable("Info");
ms.sizeX = Integer.parseInt(infoTable.get("Width"));
ms.sizeY = Integer.parseInt(infoTable.get("Height"));
ms.sizeC = Integer.parseInt(infoTable.get("Bands"));
ms.sizeZ = Integer.parseInt(infoTable.get("Slices"));
ms.sizeT = Integer.parseInt(infoTable.get("Frames"));
int dataType = Integer.parseInt(infoTable.get("Datatype"));
ms.pixelType = convertPixelType(dataType);
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
HashMap<String, String> iniMap = data.flattenIntoHashMap();
ms.seriesMetadata.putAll(iniMap);
}
} else {
ms = core.get(i);
setSeries(i);
}
ms.imageCount = ms.sizeZ * ms.sizeC * ms.sizeT;
ms.rgb = false;
ms.littleEndian = true;
ms.dimensionOrder = "XYCZT";
ms.seriesMetadata = new Hashtable<String, Object>();
for (Object key : metadataKeys) {
String keyName = key.toString();
if (keyName.startsWith("Series " + i + " ")) {
keyName = keyName.replaceAll("Series " + i + " ", "");
ms.seriesMetadata.put(keyName, tmpSeriesMetadata.get(key));
}
}
}
setSeries(0);
populateMetadataStore();
poi.close();
poi = null;
}
Aggregations