Gatan | AMETEKSkip to Main Content
No options found

Process all DigitalMicrograph files in a folder and its subfolders

DigitalMicrograph Script

Automation script for batch processing specific files from a folder structure. The script can be easily modified to perform different tasks.

Preview

void PerformBatchAction( image img )
{
	// Modify this method to act on the image 
	
	// Current action: Output the data type and some statistics
	result( "\t\t Data range [ " + min( img ) + ", " + max( img ) + "] mean = " + mean( img ) + "\n" )
		
	// Save(img) 			// Use this to resave the image
	// DeleteImage(img) 	// use this to close the image
}


// Function converts a string to lower-case characters
string ToLowerCase( string in )
{
	string out = ""
	for( number c = 0 ; c < len( in ) ; c++ )
	{
		string letter = mid( in , c , 1 )
		number n = asc( letter )
		if ( ( n > 64 ) && ( n < 91 ) )	letter = chr( n + 32 )
		out += letter
		}	
	return out
}

// Function to create a list of file entries with full path
TagGroup CreateFileList( string folder, number inclSubFolder )
{
	TagGroup filesTG = GetFilesInDirectory( folder , 1 )			// 1 = Get files, 2 = Get folders, 3 = Get both
	TagGroup fileList = NewTagList()
	
	for (number i = 0; i < filesTG.TagGroupCountTags() ; i++ )
	{
		TagGroup entryTG
		if ( filesTG.TagGroupGetIndexedTagAsTagGroup( i , entryTG ) )
		{
			string fileName
			if ( entryTG.TagGroupGetTagAsString( "Name" , fileName ) )
			{
				filelist.TagGroupInsertTagAsString( fileList.TagGroupCountTags() , PathConcatenate( folder , fileName ) )
			}
		}
	}
	
	if ( inclSubFolder )
	{
		TagGroup allFolders = GetFilesInDirectory( folder, 2 )
		number nFolders = allFolders.TagGroupCountTags()
		for ( number i = 0; i < nFolders; i++ )
		{
			string sfolder
			TagGroup entry
			allFolders.TagGroupgetIndexedTagAsTagGroup( i , entry )
			entry.TagGroupGetTagAsString( "Name" , sfolder )
			sfolder = StringToLower( sfolder )
			TagGroup SubList = CreateFileList( PathConcatenate( folder , sfolder ) , inclSubFolder )
			for ( number j = 0; j < SubList.TagGroupCountTags(); j++ )
			{
				string file
				if ( SubList.tagGroupGetIndexedTagAsString( j , file ) )
					fileList.TagGroupInsertTagAsString( Infinity() , file )
			}
		}
	}
	return fileList
}

// Function removes entries not matching in suffix
TagGroup FilterFilesList( TagGroup list, string suffix )
{
	TagGroup outList = NewTagList()
	suffix = ToLowerCase( suffix )
	for ( number i = 0 ; i < list.TagGroupCountTags() ; i++ )
	{
		string origstr
		if ( list.TagGroupGetIndexedTagAsString( i , origstr ) ) 
		{
			string str = ToLowerCase( origstr )
			number matches = 1
			if ( len( str ) >= len( suffix ) ) 		// Ensure that the suffix isn't longer than the whole string
			{
				if ( suffix == right( str , len( suffix ) ) ) // Compare suffix to end of original string
				{
					outList.TagGroupInsertTagAsString( outList.TagGroupCountTags() , origstr )	// Copy if matching
				}
			}
		}
	}
	return outList
}

// Open and process all files in a given fileList
void BatchProcessList( TagGroup fileList , string name )
{
	number nEntries = fileList.TagGroupCountTags()
	if ( nEntries > 0 )
		result( "Processing file list <" + name + "> with " + nEntries + " files.\n" )
	else
		result( "File list <" + name + "> does not contain any files.\n" )
	
	for ( number i = 0 ; i < nEntries ; i++ )
	{
		string str 
		if ( fileList.TagGroupGetIndexedTagAsString( i , str ) )
		{		
			result( "\t open: " + str + "\t" )
			image img := OpenImage( str )
			if ( img.ImageIsValid() )
			{
				// Actual batch-action
				result( "process... \n" )
				PerformBatchAction( img )
			}
			else
				result( "skipped... \n" )
		}
	}
}


// Main routine. Processes all dm3/dm4 files in a directory
void BatchProcessFilesInFolder( number includeSubFolders )
{
	string folder , outputFolder
	if ( !GetDirectoryDialog( "Select folder" , "" , folder ) ) 
		return
	
	TagGroup fileList = CreateFileList( folder, includeSubFolders ) 
	TagGroup fileListDM3 = FilterFilesList( fileList , ".dm3" )
	BatchProcessList( fileListDM3 , "DM3 list" )
	TagGroup fileListDM4 = FilterFilesList( fileList , ".dm4" )
	BatchProcessList( fileListDM4 , "DM4 list" )
}

BatchProcessFilesInFolder( 1 )