macro 'Import ND2 Series' { /* * Import_ND2_Series_6.txt * 12.02.17 7:08 * N. Vischer * https://sils.fnwi.uva.nl/bcb/objectj/examples/Import_ND2_files/ * We use: * ImageJ 1.51i * Loaded Bio-Formats 5.3.2 from: * (But got version 5.1.8) * * - converts ND2 series file into individual TIFF timelapse stacks. * - the macro ask for a source .nd2 file. * - each series is saved as one tiff * - we use short names like "Ser_65892_03.tif", where "65892" is the checksum of the long name * and "03" the series number * - the TIFFs are saved in a newly created folder besides the ND2 file, or * elsewhere if desired */ prefix = "Ser"; requires("1.51k"); run("Close All"); run("Bio-Formats Macro Extensions"); Ext.getVersionNumber(bfVersion); Ext.getBuildDate(date); srcFullPath = File.openDialog("(BioFormats " + bfVersion +") Select an .nd2 File"); srcDir = File.getParent(srcFullPath); nd2Name = File.getName(srcFullPath); if(endsWith(nd2Name, ".nd2")) nd2Name = substring(nd2Name, 0, lengthOf(nd2Name)-4); checkSum = getCheckSum(nd2Name); b = getBoolean("Create tiffs folder in same place?", "Same", "Choose Destination"); if(b) destDir = srcDir + File.separator+ nd2Name+"_tiffs"; else destDir = getDirectory("Choose a Directory"); if (File.exists(destDir)==false) File.makeDirectory(destDir); // new directory for tiff File.makeDirectory(destDir+"dummy"); // new directory for tiff if(File.delete(destDir+"dummy") == 0) exit("Error: No Write permission?"); showProgress(0.2); showStatus("...checking ND2 structure..."); Ext.setId(srcFullPath);//-- Initializes the given path (filename). Ext.getSeriesCount(seriesCount); //-- Gets the number of image series in the active dataset. path = srcFullPath;//must be this var name first = 1; last = 1; if(seriesCount > 1){ s = getString("Import series\n(single or range):", "1-" + seriesCount); ss = split(s, "-"); first = parseInt(ss[0]); last = first; if (ss.length == 2) last = parseInt(ss[1]); } time = getTime; allSaved = ""; for (j=first; j<=last; j++) { nn = j-first + 0.5; showProgress(-nn/(last - first + 1)); run("Bio-Formats", "open=path autoscale color_mode=Default view=Hyperstack stack_order=XYCZT series_"+j); if(j < 10) sep = "0" ;else sep = ""; simpleName = prefix + "_" + checkSum + "_" + sep +j + ".tif"; rename(simpleName); destPath = destDir+File.separator+simpleName; //change seconds to minutes interval = Stack.getFrameInterval(); Stack.setFrameInterval(interval); Stack.getUnits(dummy, dummy, dummy, TimeUnit, dummy); if(TimeUnit == "s" || TimeUnit== "sec"){ secs = Stack.getFrameInterval(); Stack.setFrameInterval(secs/60); Stack.setTUnit("min"); } saveAs("TIFF", destPath); allSaved += destPath + "\t"; run("Close All"); call("java.lang.System.gc"); //garbage collector } timeUsed = d2s((getTime-time)/60000, 1) + " min"; show = getBoolean(" -- Imported " + (last - first + 1) + " series in " + timeUsed + "\n \nShow all as virtual stacks?" ); if(show){ allSaved = split(allSaved, "\t"); for(jj = 0; jj < allSaved.length; jj++){ run("TIFF Virtual Stack...", "open=["+allSaved[jj]+"]"); } run("Tile"); } } //return a 5-digit string function getCheckSum(name){ code = 0; for(jj=1; jj <= lengthOf(name); jj++) code += ((sin(jj) + cos(jj)) * parseInt(charCodeAt(name, jj-1))); a = "" + abs(round(code * 1e6)) +1e6; return substring(a, lengthOf(a) - 5);//5 digits }