Skip to content

Instantly share code, notes, and snippets.

@ghutchis
Created June 30, 2017 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghutchis/bbfccd24898cd45be840954a3a6a14aa to your computer and use it in GitHub Desktop.
Save ghutchis/bbfccd24898cd45be840954a3a6a14aa to your computer and use it in GitHub Desktop.
Batch script through Igor AFM data
import glob, sys
try:
import gwy
except:
print "You need to install Gwyddion first"
sys.exit(0)
# Script adapted from the Gwyddion documentation
# http://gwyddion.net/documentation/user-guide-en/pygwy.html
# 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',
'DACFrequency']
# Go through files matching the Igor patterns
for filename in glob.glob('*.ibw'):
container = gwy.gwy_file_load(filename, gwy.RUN_NONINTERACTIVE)
gwy.gwy_app_data_browser_add(container)
# 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)
else:
sys.stderr.write('Expected one %s channel in %s but found %u.\n'
% (channel, filename, len(ids)))
gwy.gwy_app_data_browser_remove(container)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment