Search in sources :

Example 96 with Location

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

the class FilePattern method findSeriesPatterns.

/**
 * Works like {@link #findSeriesPatterns(String, String, String[])},
 * but dir and nameList are inferred from the given file's absolute
 * path.
 *
 * @param base The file basename to use as a template for the match.
 * @return an array containing all identified patterns.
 */
public static String[] findSeriesPatterns(String base) {
    Location file = new Location(base).getAbsoluteFile();
    Location parent = file.getParentFile();
    String[] list = parent.list(true);
    return findSeriesPatterns(base, parent.getAbsolutePath(), list);
}
Also used : Location(loci.common.Location)

Example 97 with Location

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

the class FilePattern method buildFiles.

// -- Helper methods --
// recursive method for building the list of matching filenames
private void buildFiles(String prefix, int ndx, List<String> fileList) {
    if (blocks.length == 0) {
        if (new Location(pattern).exists()) {
            fileList.add(pattern);
            return;
        }
        isRegex = true;
        String[] files = null;
        String dir;
        int endRegex = pattern.indexOf(File.separator + "\\E") + 1;
        int endNotRegex = pattern.lastIndexOf(File.separator) + 1;
        int end;
        // Check if an escaped path has been defined as part of the regex.
        if (pattern.startsWith("\\Q") && endRegex > 0 && endRegex <= endNotRegex) {
            dir = pattern.substring(2, endRegex);
            end = endRegex + 2;
        } else {
            dir = pattern.substring(0, endNotRegex);
            end = endNotRegex;
        }
        if (dir.equals("") || !new Location(dir).exists()) {
            files = Location.getIdMap().keySet().toArray(new String[0]);
            if (files.length == 0) {
                dir = ".";
                files = getAllFiles(dir);
            }
        } else {
            files = getAllFiles(dir);
        }
        Arrays.sort(files);
        String basePattern = pattern.substring(end);
        Pattern regex = null;
        try {
            regex = Pattern.compile(basePattern);
        } catch (PatternSyntaxException e) {
            regex = Pattern.compile(pattern);
        }
        for (String f : files) {
            Location path = new Location(dir, f);
            if (regex.matcher(path.getName()).matches()) {
                if (path.exists())
                    fileList.add(path.getAbsolutePath());
                else
                    fileList.add(f);
            }
        }
    } else {
        // compute bounds for constant (non-block) pattern fragment
        int num = startIndex.length;
        int n1 = ndx == 0 ? 0 : endIndex[ndx - 1];
        int n2 = ndx == num ? pattern.length() : startIndex[ndx];
        String pre = pattern.substring(n1, n2);
        if (ndx == 0)
            fileList.add(pre + prefix);
        else {
            FilePatternBlock block = blocks[--ndx];
            String[] blockElements = block.getElements();
            for (String element : blockElements) {
                buildFiles(element + pre + prefix, ndx, fileList);
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Location(loci.common.Location) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 98 with Location

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

the class FilePattern method getAllFiles.

private String[] getAllFiles(String dir) {
    ArrayList<String> files = new ArrayList<String>();
    Location root = new Location(dir);
    String[] children = root.list();
    for (String child : children) {
        Location file = new Location(root, child);
        if (file.isDirectory()) {
            String[] grandchildren = getAllFiles(file.getAbsolutePath());
            for (String g : grandchildren) {
                files.add(g);
            }
        } else {
            files.add(file.getAbsolutePath());
        }
    }
    return files.toArray(new String[files.size()]);
}
Also used : ArrayList(java.util.ArrayList) Location(loci.common.Location)

Example 99 with Location

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

the class FileStitcher method getUsedFiles.

/* @see IFormatReader#getUsedFiles() */
@Override
public String[] getUsedFiles() {
    FormatTools.assertId(getCurrentFile(), true, 2);
    if (noStitch)
        return reader.getUsedFiles();
    // returning the files list directly here is fast, since we do not
    // have to call initFile on each constituent file; but we can only do so
    // when each constituent file does not itself have multiple used files
    Set<String> files = new LinkedHashSet<String>();
    for (ExternalSeries s : externals) {
        String[] f = s.getFiles();
        for (String file : f) {
            String path = new Location(file).getAbsolutePath();
            files.add(path);
        }
        DimensionSwapper[] readers = s.getReaders();
        for (int i = 0; i < readers.length; i++) {
            try {
                readers[i].setId(f[i]);
                String[] used = readers[i].getUsedFiles();
                for (String file : used) {
                    String path = new Location(file).getAbsolutePath();
                    files.add(path);
                }
                readers[i].close();
            } catch (FormatException e) {
                LOGGER.debug("", e);
            } catch (IOException e) {
                LOGGER.debug("", e);
            }
        }
    }
    return files.toArray(new String[files.size()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IOException(java.io.IOException) Location(loci.common.Location)

Example 100 with Location

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

the class Memoizer method setId.

// -- ReaderWrapper API methods --
@Override
public void setId(String id) throws FormatException, IOException {
    StopWatch sw = stopWatch();
    try {
        realFile = new Location(id);
        memoFile = getMemoFile(id);
        if (memoFile == null) {
            // Memoization disabled.
            if (userMetadataStore != null) {
                reader.setMetadataStore(userMetadataStore);
            }
            // EARLY EXIT
            super.setId(id);
            return;
        }
        // Should never throw kryo exceptions
        IFormatReader memo = loadMemo();
        loadedFromMemo = false;
        savedToMemo = false;
        if (memo != null) {
            // loadMemo has already called handleMetadataStore with non-null
            try {
                loadedFromMemo = true;
                reader = memo;
                reader.reopenFile();
            } catch (FileNotFoundException e) {
                LOGGER.info("could not reopen file - deleting invalid memo file: {}", memoFile);
                deleteQuietly(memoFile);
                memo = null;
                reader.close();
                loadedFromMemo = false;
            }
        }
        if (memo == null) {
            OMEXMLService service = getService();
            super.setMetadataStore(service.createOMEXMLMetadata());
            long start = System.currentTimeMillis();
            super.setId(id);
            long elapsed = System.currentTimeMillis() - start;
            // Between setId and saveMemo
            handleMetadataStore(null);
            if (elapsed < minimumElapsed) {
                LOGGER.debug("skipping save memo. elapsed millis: {}", elapsed);
                // EARLY EXIT!
                return;
            }
            // Should never throw.
            savedToMemo = saveMemo();
        }
    } catch (ServiceException e) {
        LOGGER.error("Could not create OMEXMLMetadata", e);
    } finally {
        sw.stop("loci.formats.Memoizer.setId");
    }
}
Also used : ServiceException(loci.common.services.ServiceException) FileNotFoundException(java.io.FileNotFoundException) OMEXMLService(loci.formats.services.OMEXMLService) Slf4JStopWatch(org.perf4j.slf4j.Slf4JStopWatch) StopWatch(org.perf4j.StopWatch) 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