Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Batch script through Igor AFM data
import glob, sys
import gwy
print "You need to install Gwyddion first"
# Script adapted from the Gwyddion documentation
# Set up parameters for the 'align_rows' function.
settings = gwy.gwy_app_settings_get()
settings['/module/linematch/direction'] = int(gwy.ORIENTATION_HORIZONTAL)
settings['/module/linematch/do_extract'] = False
settings['/module/linematch/do_plot'] = False
settings['/module/linematch/method'] = 2
print 'Filename\tChannel\tRa\tRms\tSkewness\tKurtosis'
# expected PFM channels (we'll append the "Retrace" string later)
channels = [ 'Height', 'DACAmplitude', 'DACPhase', 'DACQFactor',
# Go through files matching the Igor patterns
for filename in glob.glob('*.ibw'):
container = gwy.gwy_file_load(filename, gwy.RUN_NONINTERACTIVE)
# Find channel(s) called 'Height', expecting to find one.
for channel in channels:
ids = gwy.gwy_app_data_browser_find_data_by_title(container, channel + 'Retrace')
if len(ids) == 1:
i = ids[0]
# Select the channel and run some functions.
gwy.gwy_app_data_browser_select_data_field(container, i)
gwy.gwy_process_func_run('align_rows', container, gwy.RUN_IMMEDIATE)
gwy.gwy_process_func_run('flatten_base', container, gwy.RUN_IMMEDIATE)
# Extract simple statistics and print them.
data_field = container[gwy.gwy_app_get_data_key_for_id(i)]
avg, ra, rms, skew, kurtosis = data_field.get_stats()
print '%s\t%s\t%g\t%g\t%g\t%g' % (filename, channel, ra, rms, skew, kurtosis)
# save a PNG
slices = filename[:-4].split('_')
if len(slices) < 3: continue
# we have a good filename, so save the PNG
pngFile = '%s_%s_%s_%s.png' % (slices[0], slices[1], channel, slices[2])
gwy.gwy_file_save(container, pngFile, gwy.RUN_NONINTERACTIVE)
sys.stderr.write('Expected one %s channel in %s but found %u.\n'
% (channel, filename, len(ids)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment