/* Import_ND2_Series_9.txt 27.02.17 11:30 N. Vischer https://sils.fnwi.uva.nl/bcb/objectj/examples/Import_ND2_files/ Was tested with Bio-Formats 5.3.2 from: - converts ND2 series file into individual TIFF timelapse stacks. - if you choose "Single", you can import some or all series from a single .nd2 file - if you choose "All", all series from all .nd2 files in (sub)directories will be imported example: long: "20170105 168 16xbp2_inmedium001_p360.nd2" short: "Ser_658922_03.tif" where "658922" is the signature (checksum ) of the long name, and "03" the series number - Note that the original file name is still preserved the window's top "label" field. - the TIFFs are either saved in at a user-chosen destination - or at the same location as the .nd2 file. */ var srcList = "", ; macro 'Import ND2 Series' { print("\\Clear"); prefix = "Ser";//=Series requires("1.51k"); run("Close All"); run("Bio-Formats Macro Extensions"); Ext.getVersionNumber(bfVersion); Ext.getBuildDate(date); showStatus("BioFormats version: " + bfVersion); Dialog.create("Convert Nikon .nd2 to TIFF"); //Dialog.addMessage("Import using BioFormats " + bfVersion +"\n"); items = split("Convert a single .nd2 file */Convert all .nd2 files **", "/"); Dialog.addRadioButtonGroup("Nikon source files: " , items, 3, 2, items[0]) ; items2 = split("Store Tiffs beside Nikon files/Choose different destination", "/"); Dialog.addRadioButtonGroup("Tiff destination: " , items2, 3, 2, items2[0]) ; Dialog.addCheckbox("Create short names using checksum", true);; Dialog.addMessage("* allows partial conversion\n** scans subfolders\nBioFormats " + bfVersion); Dialog.show; singleFlag = indexOf(Dialog.getRadioButton(), "single") > 0; besideFlag = indexOf(Dialog.getRadioButton(), "beside") > 0; shortFlag = Dialog.getCheckbox; print(getTimeStr(), " User input: All = ", !singleFlag, " Beside = ", besideFlag, " Short = ", shortFlag); if(singleFlag){ showMessage("In the next dialog, select a single .nd2 file"); srcList = File.openDialog("Select an .nd2 File"); if(!endsWith(srcList, ".nd2")) exit("Chosen file has no '.nd2' extension"); } else getAllND2s(); print("Nikon source files:"); //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); print("Series selection: ", s); 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)); if(is("Caps Lock Set")){ waitForUser("Caps Lock is set: Click OK to continue, or press Esc to abort"); } run("Bio-Formats", "open=path autoscale color_mode=Default view=Hyperstack stack_order=XYCZT series_"+j); if(j < 10) sep = "0" ;else sep = ""; longName = nd2Name + "_" + sep +j + ".tif"; //shortName = prefix + "_" + checkSum + prepTime +"_" + sep +j + ".tif"; shortName = prefix + "_" + checkSum +"_" + sep +j + ".tif"; if(shortFlag) newName = shortName; else newName = longName; rename(newName); destPath = destDir+File.separator+newName; //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"); } print(" Saving Series #", j, ": ", destPath); selectWindow("Log"); saveAs("TIFF", destPath); selectWindow("Log"); 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; print(getTimeStr(), "=== Finished: ", timeMsg); 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 6-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)) +1e7; return substring(a, lengthOf(a) - 6);//6 digits } // Recursively lists the files in a user-specified directory. // Open a file on the list by double clicking on it. function getAllND2s(){ showMessage("In the next dialog, select a folder containing .nd2 files"); dir = getDirectory("Choose a Directory "); srcList = ""; listFiles(dir); if(srcList == "") exit("No .nd2 files were found"); } function listFiles(dir) { list = getFileList(dir); for (i=0; i