Skip to content

Instantly share code, notes, and snippets.

@lambdalisue
Last active November 24, 2016 08:41
Show Gist options
  • Save lambdalisue/5945629 to your computer and use it in GitHub Desktop.
Save lambdalisue/5945629 to your computer and use it in GitHub Desktop.
A script to enable batch processing on Gwyddion
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
#
# Author: Alisue (lambdalisue@hashnote.net)
# URL: http://hashnote.net/
# Date: 2013-07-06
#
# (C) 2013 hashnote.net, Alisue
#
import os
import re
import gwy
import gwyutils
BATCH_PROCESSES = (
'polylevel',
'line_correct_median',
'level',
'fix_zero',
)
DATA_FIELD_PATTERN = re.compile(r'^/(?P<i>\d+)/data$')
def batch_process(filename, BATCH_PROCESSES):
# open file
container = gwy.gwy_file_load(filename, gwy.RUN_NONINTERACTIVE)
# add container to Gwyddion data browser
gwy.gwy_app_data_browser_add(container)
# iterate keys to find data field
for key in container.keys_by_name():
m = DATA_FIELD_PATTERN.match(key)
if not m:
continue
i = int(m.group('i'))
gwy.gwy_app_data_browser_select_data_field(container, i)
# call processes
for process in BATCH_PROCESSES:
gwy.gwy_process_func_run(process, container, gwy.RUN_IMMEDIATE)
# save container
root, ext = os.path.splitext(filename)
if ext == '.gwy':
filename = "%s_m.gwy" % root
else:
filename = "%s.gwy" % filename
gwy.gwy_file_save(container, filename, gwy.RUN_NONINTERACTIVE)
# save as PNG
filename = "%s.png" % root
gwyutils.save_dfield_to_png(container, "/0/data", filename,
gwy.RUN_NONINTERACTIVE)
# remove from Gwyddion data browser
gwy.gwy_app_data_browser_remove(container)
def get_current_container_directory():
container = gwy.gwy_app_data_browser_get_current(gwy.APP_CONTAINER)
filename = container.get_string_by_name("/filename")
return os.path.dirname(filename)
if __name__ == '__main__':
import glob
settings = gwy.gwy_app_settings_get()
# get working directory from current container
working = get_current_container_directory()
working = os.path.abspath(working)
print "=" * 80
print
print " Gwyddion Batch Process"
print
print " Working Directory:"
print " %s" % working
print
print " Process List:"
for process in BATCH_PROCESSES:
print " + %s" % process
print
print "=" * 80
print
# iterate all files inside of working directory
for filename in glob.iglob("%s/*" % working):
if not os.path.isfile(filename):
continue
basename = os.path.basename(filename)
try:
print " - '%s'" % basename
batch_process(filename, BATCH_PROCESSES)
except Exception, e:
# fail silently
print " * '%s' (%s)" % (basename, e)
print
print "Done."
Gwyddioでバッチファイル的に処理させるためのスクリプトです。 BATCH_PROCESSESに処理したい名前をリスト(Gwyddion - Module List参照。名前中のスペースはアンダーバーに変換して使用)してやれば勝手にすべて適用してくれます。適用先フォルダは現在アクティブなコンテナ(ファイル)が存在しているフォルダです。 なので下記のようなワークフローになります。処理したいファイルをGwyddionで開くData Process > Pygwy Consoleを開くPygwy Consoleにてbatchprocess.pyを開く実行を押すデフォルトの設定では拡張子が.gwyの場合は_mをファイル名のおしりにつけて保存。 それ以外の場合は.gwyをつけて保存するようになっています。ライセンス: MIT License
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment