/* * Import_ND2_Series_7.txt * 18.02.17 0:43 * N. Vischer * https://sils.fnwi.uva.nl/bcb/objectj/examples/Import_ND2_files/ * We use: * ImageJ 1.51k * Loaded Bio-Formats 5.3.3 from: * * - converts ND2 series file into individual TIFF timelapse stacks. * - the macro ask for a source: either ONE .nd2 file., or ALL .nd2 files in chosen folder with subfolders * - if you choose ONE, you can import some or all series * - if you choose ALL, all series of all found .nd2 files will be extracted * - if you choose ALL, there is no choice or a different destination location * * - we use short names like "Ser_65892_03.tif", where "65892" is the signature (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 ONE was selected and different destination chosen. * - Note that the original file name is still in the window's top "label" field. */ var srcList = "", ; macro 'Import ND2 Series' { prefix = "Ser"; requires("1.51k"); run("Close All"); run("Bio-Formats Macro Extensions"); Ext.getVersionNumber(bfVersion); Ext.getBuildDate(date); showStatus("BioFormats version = " + bfVersion); singleFlag = getBoolean("Convert a single Nikon file or all files in (sub)folder(s)?", "Single", "All"); if(singleFlag) srcList = File.openDialog("Select an .nd2 File"); else getAllND2s(); print("\\Clear"); //print(srcList); selectWindow("Log"); srcList = split(srcList, "\n"); nFiles = srcList.length; for(jj = 0; jj 1 && singleFlag){ 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++) { nImported++; 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"; timeMsg = " -- Imported " + nImported + " series in " + timeUsed; if (singleFlag){ show = getBoolean(timeMsg + "\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"); } } else{ showMessage(timeMsg); } } //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 } // Recursively lists the files in a user-specified directory. // Open a file on the list by double clicking on it. function getAllND2s(){ dir = getDirectory("Choose a Directory "); count = 1; srcList = ""; listFiles(dir); } function listFiles(dir) { list = getFileList(dir); for (i=0; i