Scripting Resources for DigitalMicrograph™ |

In general, any script involving more than a few lines of code should be written as one or more functions. Think of a function like a vending machine. You put in certain inputs - an amount of cash, some keypad presses and a few judicious thumps. In return you get specifc outputs such as a chocolate bar (if you are lucky). You do not care how the vending machine works. All you need to know is what inputs are required and what outputs it can return to you.

Functions should have no external dependencies ie they should use no variables other than those passed into them. This makes operation of the function completely transparent, and allows it to be copy-and-pasted directly into new scripts. Provided the right inputs and outputs are used, the function will work seamlessly in any script. All variables used within the function are local to it, so there are no conflicts with other parts of your script. You can use the same variable name inside and outside a function. What happens inside a function is completely invisible to the rest of the script. The only exception to this is variables which are passed by reference. This is useful when more than one variable needs to be returned by a function. The reference variables are passed into the function, the values are modifed and are then available to the rest of the script. A detailed description of how to write and use functions can be found in the Scripting Digital Micrograph tutorial.

A good way to develop scripts is to break the script operations down into specfic tasks - continually sub-dividing tasks into simpler and simpler blocks - the simpler the better. For example you may write a function for finding and closing all images a particular scripts creates. This function may be used at several points in your script, such as when initialising the display, when resetting the script (especially if it is a dialog) and perhaps when shutting down a script. At each use the function can be called by a single line, as opposed to cloning the relevant code at each point of use. This avoids code bloat and makes the operation of the script easy to follow and simple to debug. Most importantly, you will be able to re-use functions in your scripts greatly speeding up development - think of them as prefabricated parts of a building. Maximum flexibility is achieved by keeping functions short and simple and avoiding external dependencies. It is better to break down a task into functions A, B and C, and then call them in that order, rather than creating single large function for all three operations.

The functions listed here have some additional code in the main section of the script which provides the function with test data, then calls the function to illustrate how it works. Functions can be copy-and-pasted directly into users' own scripts. Alternatively, they may be installed into DigitalMicrograph as Library files - see theTutorial on Installing Scripts for information on this. In general, it is better to include any functions you call in your scripts, that way they are more portable. A script which calls a function installed as a Library file, will only work on instances of DigitalMicrograph where the Library file is present.

## Curves/Interpolation

- Create 2D Gaussian:
Creates a 2D Gaussian blob image for fitting to a diffracton spot or atomic column.- Fast Cubic Spline:
Parameterised (fast) cubic spline calculates the series of constatnts A, B, C and D in the equation y=A + Bx + cx^2 + dx^3. The array of constants is calculated in a single slow step by the function calculatesplineconstants(). The array of constants is then passed to a second function (fastcubicspline()) which uses them in a loop for quick calculation a series of interpolated values.- Find Centre of Gravity:
Function to find the centre of gravity of the passed in image.- Gaussian Fit:
This function will fit a Gaussian to a peak (identified by having a region of interest drawn across it). The fitted peak and the various peak parameters are returned.- Interpolate Peak Inflection Point:
A function which computes the maximum of a smooth curve drawn through three points. Use this for obtaining sub-pixel precision in locating peak maxima.- Linear Regression:
Function to perform a linear regression on an array of data passed in to it. Calculates the constants a and b in y=a+bx, the 95% confidence interval and the R parameter.- Natural and Constrained Cubic Spline:
Function to calculate the natural cubic spline of a data set. This draws a smooth curve through a series of data points. There is also a constrainedcubicspline() function which has clamped ends. This avoids the oscillations that can sometimes appear at the end of spline fitted curves or where data bunches. However, the constrainedcubicspline() function does this at the expense of smoothness.

## Data Conversion

- Convert a Hexadecimal String into a Number:
A function which will convert the passed in string from hexadecimal and return its equivalent value as a decimal number.

## Display

- Excise Central 2n x 2n Region:
Function to excise the largest possible central square (power of 2) region of an image.- Excise largest area rectangle from a rotated image:
Function which will excise the largest possible rectangular area from an image which has been rotated.- Rotate and Crop:
A function which applies a user specified rotation to the passed in image. Black space is added during rotation. This function will calculate the maximum square area on the image which excludes the added black space.- Scale and Maximise Image:
Scales the image displayed size to a particular value, then maximises the image within that display.- Zoom to New Size:
Function to zoom the passed in image to the size specified and centre it within the image frame.## Import/Export

- Read Bruker EDS Spectrum Text File: This function will
read in an EDS spectrum text file saved using Bruker's Esprit software.

## Measurement

- Standard Deviation:
Function to compute the standard deviation (sigma n-1) of an image, or a set of values passed in as pixel values in an image. The number of data points (n), the mean and the sum are also returned.- Recursive Refinement:
Function to refine a set of values by removing outliers until a target error is reached or a minimum number of points remain.