Scripting Resources for DigitalMicrograph™

banner

Dave Mitchell's DigitalMicrograph™ Scripting Website

Home | Scripts | Examples | Functions | Recent Updates | Tutorials | Resources | Publications | Consulting | Projects | Contact & Bio | Private | Search

 

Example: Creating Hard Code from a Text File
Function
Reads in numbers from a text file and outputs them into the Results window as lines of code which can be pasted into a script to create equivalent settings in the Global Info.
Version
version:20100611, v1.0
Author
D. R. G. Mitchell
Acknowledgements
Comments
The data text file should contain one number per line, delimited by carriage returns. It can be created with NotePad or similar and should be a .txt file. Clear the Results window before running this script. If the code output in the Results window is pasted into a script and run, the numbers will be added to the Global Info as 'Temporary Tag:Data Point 1:XXX' etc, where XXX is the first value in the text file. This is useful for creating default data sets such as listings of d-spacings, scattering factors etc. For very long listings of data (>50 points), this approach will lead to code bloat. Consider creating a standalone settings file and adding the data to the Global Info from that (see the script 'Example Reading Data From a Text File and Creating a Settings File').
System Requirements
Should be compatible with all recent versions of DigitalMicrograph.
Known Issues
Supported
Yes
Included Files
Main script file.
Source Code

// Example script to show how to read from a text file. The text file is simply a file

// created in a simple text editor or a script window, where each line is delimited

// by a carriage return. The script will expect numbers rather than text.

// Create a simple text file with several lines of numbers.

// Save the text file to the hard disk then open it with this script.

 

// version:20100611, v1.0

// D. R. G. Mitchell, v1.0, June 2010

// adminnospam@dmscripting.com (remove the nospam to make this work)

 

// It is necessary to open a file for reading (openfileforreading()) to read from it.

// It must also be closed at the conclusion of reading, otherwise

// it will not be released to other attempts to do anything with it.

// The Try/Catch routine should ensure that it is closed after any

// error. The readfileline() function will read the file line by line and return

// 1 for every line it reads successfully. It will return 0 at the

// end of the file and this is used to detect the end of file. For more information

// search for 'File Input and Output' in DM's online help.

 

// In this example, the data are read in from a text file and are written to the results window in the format

// of lines of script code. The code is designed to create the relevant tags in the Global Info.

// A check is made to see if the tag already exists - it is only created if it does not already exist.

// This code can then simply be cut and pasted from the results window into the script, complete with the newly added data.

// Clear the results window before running this script.

 

// This method is useful for small volumes of data (eg <50 points) where it is appropriate to create a self-contained

// script which has the data hard coded into statements which create the tags ie the

// script creates its own settings in the Global Info, rather than accessing data

// either installed manually at installation or by an installer script.

 

// Where large libraries of data need to be accessed, hard coding in this manner results in code bloat. Here

// it is more appropriate to create a standalone settings file, and load that into the Global Info when the script

// is installed.

 

 

// variables

 

string path

number filereference

string thisline

number counter=1

number spacing

result("\n\n\n")

 

 

// Select the file

 

showalert("Open a text file containing numerical data - carriage return delimited",2)

if(!opendialog(path)) exit(0)

documentwindow reswin=getresultswindow(0)

 

// Try/Catch to open the file for reading but close it in case of error.

try

{

filereference=openfileforreading(path)

number readok=1

// While ever the end of file has not been reached (readok=0) read the next line

// and output it to the results

while(readok==1)

{

readok=readfileline(filereference, thisline)

if(val(thisline)!=0)

{

// Create the lines of code in the Results window

result("\nif(!getpersistentnumbernote(\"Temporary Tag:Data Point "+counter+"\",spacing))")

result("\n\t{")

result("\n\t\tsetpersistentnumbernote(\"Temporary Tag:Data Point "+counter+"\","+val(thisline)+")")

result("\n\t}"+"\n\n")

counter=counter+1

}

}

}

catch // there was an error - close the file

{

closefile(filereference)

showalert("Sorry there was an error",2)

exit(0)

}

// The file read has concluded, close the file for reading.

 

closefile(filereference)

reswin.windowselect()