Scripts

This page is the place to collect user-contributed PyGwy/PyConsole scripts. To add a script, please use the "Post"-Button at the bottom of the page and include a link to your contribution in the list below.


Related

Wiki: Home

  • amunizp
    amunizp
    2012-08-25

    Batch Reporting your Images from gwy file

    This script was written as a learning tool so sorry for the sloppiness. It exports to a plain text *.tex that can then be compiled with LaTeX typesetting (pdf latex was tested). Attached is the sample output using the the example gwy provided in the gwyddion website.

    First you need the template

    This will be a *.tex file.

    \documentclass[10pt,a4paper,notitlepage]{report}
    \usepackage[latin1]{inputenc}
    \usepackage{amsmath}
    \usepackage{amsfonts}
    \usepackage{amssymb}
    \usepackage{graphicx}
    \usepackage{hyperref}
    \author{Gwyddion User-Name}
    \title{GwyddionReport}
    \begin{document}
    \maketitle
    
    \listoffigures
    
    \chapter{Example Output}
    \section{Introduction}
    This pretends to be the main bit of your report.
    You will have your normal disclaimers on how this is
    a batch report to be quickly presented. Options could
    include make this a Beammer to have a quick slideshow
    to show your results. 
    \begin{figure}[hbtp]
    \includegraphics[width = \textwidth]{exported-png-0.png}
    \caption{A gwyddion Figure}
    \end{figure}
    
    \chapter{Your Data}
    \section{Figures}
    \include{report}
    
    \end{document}
    

    Next you need the script

    This can be a *.py file that you import to your gwyddion console. Tested on a Linux machine running Trisquel (ubuntu Free derivative)

    #####
    #This is GPL licence (the latest version you can find normally): 
    #it is a cheap copy of a script found in the gwyddion.net documentation
    # pretends to be a quick way to do a batch report using LaTeX but could be done 
    # with minor editing for an HTML report. 
    ####
    # OBJECTIVE:  you have your gwy file with all the images just like you like them.
    #instead of saving them one by one and then attempt to put them in your report 
    # one by one we just do it all in one batch file. 
    import gwyutils
    # base name
    basePath = "Documentos/gwyddion/"
    baseFile = "exported-png-%d.png"
    basename = basePath + baseFile
    #basename = "Documentos/gwyddion/exported-png-%d.png"
    i = 0
    # get list of available containers
    cons = gwy.gwy_app_data_browser_get_containers()
    # create tex file
    texf = open(basePath +'report.tex', 'w')
    # save strings for adding to the report
    beginFigure = r"\begin{figure}[hbtp]"
    includegraphics = r"\includegraphics[width = \textwidth]{"
    caption = r"\caption{"
    # iterate thru containers and datafields
    for c in cons:
      # get directory of datafields where key is key in container
      dfields = gwyutils.get_data_fields_dir(c)
      for key in dfields.keys():
        # get processed datafield object
        datafield = dfields[key]
        # retrieve datafield number in container from key (for example '/0/data')
        datafield_id = key.split('/')[1]
        # set palette of datafield
        c.set_string_by_name("/%s/base/palette" % datafield_id, "Gwyddion.net")
        # invert datafield values (processing section) 
        #datafield.invert(0, 0, 1)
        # request redraw before export
        datafield.data_changed()
        # export datafield to png, for the first time show export dialog
        if i == 0:
          gwyutils.save_dfield_to_png(c, key, basename % i, gwy.RUN_INTERACTIVE)
        else :
          gwyutils.save_dfield_to_png(c, key, basename % i, gwy.RUN_NONINTERACTIVE)
        #write to report
        #texf.append(basename % i)
        print basename % i
        #textf.write("beginFigure")
        texf.write(beginFigure)
        texf.write('\n')
        texf.write(includegraphics)
        texf.write(baseFile % i)
        texf.write("}")
        texf.write('\n')
        texf.write(caption)
        texf.write(baseFile % i)
        texf.write("}")    
        texf.write('\n')
        texf.write(r'\label{fig:')
        texf.write(baseFile % i)
        texf.write("}")    
        texf.write('\n')
        texf.write("\\end{figure}")
        texf.write('\n')
        i += 1
        # request redraw datawindows with datafields
        datafield.data_changed()
    texf.close()
    
    #TODO:
    # would append be better than write?
    # strings as variables a little too much?
    # find out how to get the meta data as a caption.
    # find out how to add graphs and spectroscopies to the file. 
    # more elegant way to add scripts?
    # Run pdflatex from this console to have a working pdf file at the end. 
    # query for path or use the path that your file is in. 
    # choose tex, HTML or XML output. 
    # make it a module?
    # DONE: make the name of the file separate from the path to be able to use it as a caption.
    # DONE: use the figure enviorment on files
    

    Remember to edit your path (or add a msg box of some sort to browse for your path)

    Remember that this script will output in png format hence it will work with pdflatex. Also note the pngs and tex files will be in the same filder as your gwy file.
    Please let me know if you would like me to add or change something. Please improve it!

     
    Last edit: David Nečas 2012-08-27
    Attachments
  • Christoph Zeh
    Christoph Zeh
    2012-12-28

    Removing channels from data browser

    This script shows how to remove (delete) a number of channels from the data browser, beginning from the last channel. The number of channels can be selected by the variable:
    nbr_of_last_channels_to_delete

    import gwy, gwyutils
    
    nbr_of_last_channels_to_delete = 3
    save_file = False
    
    c = gwyutils.get_current_container()
    dfd = gwyutils.get_data_fields_dir(c)
    
    # change behaviour of data browser to leave file
    # open when no visible object is left
    gwy.gwy_app_data_browser_set_keep_invisible(c, True)
    
    # sort data field keys with respect to channel number
    # please check out the documentation for Python's lambda functions
    # and sorted() for more info
    dfd_k = sorted(dfd.keys(), key = lambda x : int(x.split('/')[1]))
    
    for i in range(nbr_of_last_channels_to_delete):
        # make last data field invisible to avoid data browser crash
        c.set_boolean_by_name(dfd_k[-1]+'/visible', False)
        # remove last data field and associated data from container
        c.remove_by_prefix(dfd_k[-1][0:3])
        # remove last item from key list
        dfd_k.pop()
    
    if save_file is True:
        # save the current container (file)
        gwy.gwy_app_file_save()
    
     
    Last edit: Christoph Zeh 2012-12-28