diff --git a/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_N5.java b/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_N5.java index dfb17577..b9e5c9e8 100644 --- a/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_N5.java +++ b/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_N5.java @@ -131,8 +131,8 @@ public static SpimData2 resaveN5( } else if ( URITools.isS3( n5Params.n5URI ) || URITools.isGC( n5Params.n5URI ) ) { - // TODO: save to cloud - final N5Writer n5Writer = new N5Factory().openWriter( n5Params.n5URI.toString() ); // cloud support, avoid dependency hell if it is a local file + // save to cloud or file + final N5Writer n5Writer = URITools.instantiateGuessedN5Writer( n5Params.n5URI ); final int[] blockSize = null; final int[] computeBlockSize = null; diff --git a/src/main/java/net/preibisch/mvrecon/fiji/spimdata/interestpoints/InterestPointsN5.java b/src/main/java/net/preibisch/mvrecon/fiji/spimdata/interestpoints/InterestPointsN5.java index e5b4a915..60c74318 100644 --- a/src/main/java/net/preibisch/mvrecon/fiji/spimdata/interestpoints/InterestPointsN5.java +++ b/src/main/java/net/preibisch/mvrecon/fiji/spimdata/interestpoints/InterestPointsN5.java @@ -39,6 +39,7 @@ import org.janelia.saalfeldlab.n5.N5Writer; import org.janelia.saalfeldlab.n5.imglib2.N5Utils; import org.janelia.saalfeldlab.n5.universe.N5Factory; +import org.janelia.saalfeldlab.n5.universe.N5Factory.StorageFormat; import mpicbg.spim.data.sequence.ViewId; import net.imglib2.RandomAccess; @@ -150,12 +151,14 @@ public boolean saveInterestPoints( final boolean forceWrite ) try { - final N5Writer n5Writer; + final N5Writer n5Writer = URITools.instantiateN5Writer( StorageFormat.N5, URI.create( URITools.appendName( baseDir, baseN5 ) ) ); + /* if ( URITools.isFile( baseDir ) ) n5Writer = new N5FSWriter( new File( URITools.removeFilePrefix( baseDir ), baseN5 ).getAbsolutePath() ); else n5Writer = new N5Factory().openWriter( URITools.appendName( baseDir, baseN5 ) ); // cloud support, avoid dependency hell if it is a local file + */ if (n5Writer.exists(dataset)) n5Writer.remove(dataset); @@ -246,12 +249,14 @@ public boolean saveCorrespondingInterestPoints(boolean forceWrite) try { - final N5Writer n5Writer; + final N5Writer n5Writer = URITools.instantiateN5Writer( StorageFormat.N5, URI.create( URITools.appendName( baseDir, baseN5 ) ) ); + /* if ( URITools.isFile( baseDir ) ) n5Writer = new N5FSWriter( new File( URITools.removeFilePrefix( baseDir ), baseN5 ).getAbsolutePath() ); else n5Writer = new N5Factory().openWriter( URITools.appendName( baseDir, baseN5 ) ); // cloud support, avoid dependency hell if it is a local file + */ if (n5Writer.exists(dataset)) n5Writer.remove(dataset); @@ -359,12 +364,14 @@ protected boolean loadInterestPoints() try { - final N5Reader n5; + final N5Reader n5 = URITools.instantiateN5Reader( StorageFormat.N5, URI.create( URITools.appendName( baseDir, baseN5 ) ) ); + /* if ( URITools.isFile( baseDir ) ) n5 = new N5FSReader( new File( URITools.removeFilePrefix( baseDir ), baseN5 ).getAbsolutePath() ); else n5 = new N5Factory().openReader( URITools.appendName( baseDir, baseN5 ) ); // cloud support, avoid dependency hell if it is a local file + */ final String dataset = ipDataset(); @@ -464,12 +471,14 @@ protected boolean loadCorrespondences() { try { - final N5Reader n5; + final N5Reader n5 = URITools.instantiateN5Reader( StorageFormat.N5, URI.create( URITools.appendName( baseDir, baseN5 ) ) );; + /* if ( URITools.isFile( baseDir ) ) n5 = new N5FSReader( new File( URITools.removeFilePrefix( baseDir ), baseN5 ).getAbsolutePath() ); else n5 = new N5Factory().openReader( URITools.appendName( baseDir, baseN5 ) ); // cloud support, avoid dependency hell if it is a local file + */ final String dataset = corrDataset(); @@ -587,15 +596,14 @@ public boolean deleteInterestPoints() { try { - // cloud support - //final N5FSWriter n5Writer = new N5FSWriter( baseDir.getAbsolutePath() ); - final N5Writer n5Writer; + final N5Writer n5Writer = URITools.instantiateN5Writer( StorageFormat.N5, URI.create( URITools.appendName( baseDir, baseN5 ) ) ); + /* if ( URITools.isFile( baseDir ) ) n5Writer = new N5FSWriter( new File( URITools.removeFilePrefix( baseDir ), baseN5 ).getAbsolutePath() ); else n5Writer = new N5Factory().openWriter( URITools.appendName( baseDir, baseN5 ) ); // cloud support, avoid dependency hell if it is a local file - + */ if (n5Writer.exists(ipDataset())) n5Writer.remove(ipDataset()); @@ -618,15 +626,14 @@ public boolean deleteCorrespondingInterestPoints() { try { - // cloud support - //final N5FSWriter n5Writer = new N5FSWriter( baseDir.getAbsolutePath() ); - - final N5Writer n5Writer; + final N5Writer n5Writer = URITools.instantiateN5Writer( StorageFormat.N5, URI.create( URITools.appendName( baseDir, baseN5 ) ) ); + /* if ( URITools.isFile( baseDir ) ) n5Writer = new N5FSWriter( new File( URITools.removeFilePrefix( baseDir ), baseN5 ).getAbsolutePath() ); else n5Writer = new N5Factory().openWriter( URITools.appendName( baseDir, baseN5 ) ); // cloud support, avoid dependency hell if it is a local file + */ if (n5Writer.exists(corrDataset())) n5Writer.remove(corrDataset()); diff --git a/src/main/java/net/preibisch/mvrecon/process/export/ExportN5API.java b/src/main/java/net/preibisch/mvrecon/process/export/ExportN5API.java index b82f0382..11eddd0f 100644 --- a/src/main/java/net/preibisch/mvrecon/process/export/ExportN5API.java +++ b/src/main/java/net/preibisch/mvrecon/process/export/ExportN5API.java @@ -170,17 +170,11 @@ public & NativeType> boolean exportImage( { if ( storageType == StorageType.N5 ) { - if ( URITools.isFile( path ) ) - driverVolumeWriter = new N5FSWriter( URITools.removeFilePrefix( path ) ); - else - driverVolumeWriter = new N5Factory().openWriter( StorageFormat.N5, path ); // cloud support, avoid dependency hell if it is a local file + driverVolumeWriter = URITools.instantiateN5Writer( StorageFormat.N5, path ); } else if ( storageType == StorageType.ZARR ) { - if ( URITools.isFile( path ) ) - driverVolumeWriter = new N5ZarrWriter( URITools.removeFilePrefix( path ) ); - else - driverVolumeWriter = new N5Factory().openWriter( StorageFormat.ZARR, path ); // cloud support, avoid dependency hell if it is a local file + driverVolumeWriter = URITools.instantiateN5Writer( StorageFormat.ZARR, path ); } else if ( storageType == StorageType.HDF5 ) { @@ -326,6 +320,8 @@ else if ( FloatType.class.isInstance( type ) ) // // save full-resolution data (s0) // + + // TODO: use Tobi's code (at least for the special cases) ex.submit(() -> grid.parallelStream().forEach( gridBlock -> { diff --git a/src/main/java/net/preibisch/mvrecon/process/resave/SpimData2Tools.java b/src/main/java/net/preibisch/mvrecon/process/resave/SpimData2Tools.java index c8c79494..27127b4d 100644 --- a/src/main/java/net/preibisch/mvrecon/process/resave/SpimData2Tools.java +++ b/src/main/java/net/preibisch/mvrecon/process/resave/SpimData2Tools.java @@ -177,7 +177,7 @@ public static String listAllTimePoints( final List timePointsToProces return t; } - public static void copyFolder( final File src, final File dest, final List< String > filesToCopy ) throws IOException + private static void copyFolder( final File src, final File dest, final List< String > filesToCopy ) throws IOException { if ( src.isDirectory() ) { diff --git a/src/main/java/util/URITools.java b/src/main/java/util/URITools.java index b97fe2ff..b330fb60 100644 --- a/src/main/java/util/URITools.java +++ b/src/main/java/util/URITools.java @@ -18,10 +18,14 @@ import org.janelia.saalfeldlab.googlecloud.GoogleCloudUtils; import org.janelia.saalfeldlab.n5.GsonKeyValueN5Reader; import org.janelia.saalfeldlab.n5.KeyValueAccess; +import org.janelia.saalfeldlab.n5.N5FSReader; +import org.janelia.saalfeldlab.n5.N5FSWriter; import org.janelia.saalfeldlab.n5.N5Reader; +import org.janelia.saalfeldlab.n5.N5Writer; import org.janelia.saalfeldlab.n5.s3.AmazonS3Utils; import org.janelia.saalfeldlab.n5.universe.N5Factory; import org.janelia.saalfeldlab.n5.universe.N5Factory.StorageFormat; +import org.janelia.saalfeldlab.n5.zarr.N5ZarrWriter; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; @@ -142,6 +146,68 @@ else if ( URITools.isS3( xmlURI ) || URITools.isGC( xmlURI ) ) } } + public static N5Writer instantiateN5Writer( final StorageFormat format, final URI uri ) + { + if ( URITools.isFile( uri ) ) + { + if ( format.equals( StorageFormat.N5 )) + return new N5FSWriter( URITools.removeFilePrefix( uri ) ); + else if ( format.equals( StorageFormat.ZARR )) + return new N5ZarrWriter( URITools.removeFilePrefix( uri ) ); + else + throw new RuntimeException( "Format: " + format + " not supported." ); + } + else + { + return new N5Factory().openWriter( format, uri ); // cloud support, avoid dependency hell if it is a local file + } + } + + public static N5Reader instantiateN5Reader( final StorageFormat format, final URI uri ) + { + if ( URITools.isFile( uri ) ) + { + if ( format.equals( StorageFormat.N5 )) + return new N5FSWriter( URITools.removeFilePrefix( uri ) ); + else if ( format.equals( StorageFormat.ZARR )) + return new N5ZarrWriter( URITools.removeFilePrefix( uri ) ); + else + throw new RuntimeException( "Format: " + format + " not supported." ); + } + else + return new N5Factory().openReader( format, uri ); // cloud support, avoid dependency hell if it is a local file + } + + public static N5Reader instantiateGuessedN5Reader( final URI uri ) + { + if ( URITools.isFile( uri ) ) + { + if ( uri.toString().toLowerCase().endsWith( ".zarr" ) ) + return instantiateN5Reader( StorageFormat.ZARR, uri ); + else if ( uri.toString().toLowerCase().endsWith( ".n5" ) ) + return instantiateN5Reader( StorageFormat.N5, uri ); + else + throw new RuntimeException( "Format for local storage of: " + uri + " could not be guessed (make it end in .n5 or .zarr)." ); + } + else + return new N5Factory().openReader( uri.toString() ); // cloud support, avoid dependency hell if it is a local file + } + + public static N5Writer instantiateGuessedN5Writer( final URI uri ) + { + if ( URITools.isFile( uri ) ) + { + if ( uri.toString().toLowerCase().endsWith( ".zarr" ) ) + return instantiateN5Writer( StorageFormat.ZARR, uri ); + else if ( uri.toString().toLowerCase().endsWith( ".n5" ) ) + return instantiateN5Writer( StorageFormat.N5, uri ); + else + throw new RuntimeException( "Format for local storage of: " + uri + " could not be guessed (make it end in .n5 or .zarr)." ); + } + else + return new N5Factory().openWriter( uri.toString() ); // cloud support, avoid dependency hell if it is a local file + } + public static SpimData2 loadSpimData( final URI xmlURI, final XmlIoSpimData2 io ) throws SpimDataException { if ( URITools.isFile( xmlURI ) )