Changes

Jump to navigation Jump to search

Write grace2images

6,834 bytes added, 09:02, 18 June 2013
Created page with "== Wish == To write a conversion script that can make a conversion script == Call to the function == When we make a dispersion analysis, we write xmgrace files.<br> So we wan..."
== Wish ==
To write a conversion script that can make a conversion script

== Call to the function ==
When we make a dispersion analysis, we write xmgrace files.<br>
So we want include the writing in this analysis

=== import the function and call it ===
In specific_analyses/relax_disp/disp_data.py we import the coming new writing function

We change
<source lang="python">
from lib.software.grace import write_xy_data, write_xy_header
</source>
to
<source lang="python">
from lib.software.grace import write_xy_data, write_xy_header, write_grace2images
</source>

Then we add doc string to: def plot_disp_curves(dir=None, force=None):
<source lang="python">
"""Custom 2D Grace plotting function for the dispersion curves.

One file will be created per spin system.
</source>
to
<source lang="python">
"""Custom 2D Grace plotting function for the dispersion curves.

One file will be created per spin system.

A python "grace to PNG/EPS/SVG..." conversion script is created at the end
</source>

Then we add to end of file. We open the file, call your writing script and close the file.
<source lang="python">
# Write a python "grace to PNG/EPS/SVG..." conversion script.
# Open the file for writing.
file_name = "grace2images.py"
file = open_write_file(file_name, dir, force)

# Write the file.
write_grace2images(file=file)

# Close the file.
file.close()
</source>

=== make the writing function ===
In lib/software/grace.py we write the new function

<source lang="python">
def write_grace2images(file=None):
"""Write a python "grace to PNG/EPS/SVG..." conversion script..

The makes a conversion script to image types as PNG/EPS/SVG. The conversion is looping over a directory list of *.agr files, and making function calls to xmgrace. Successfull conversion of images depends on the compilation of xmgrace. The input is a list of image types which is wanted, f.ex: PNG EPS SVG. PNG is default.

@type file: file object
"""

# Write to file
# Hack for the chaos of " and ' in same string
t="'"

lines = [
r'#!/usr/bin/env python',"\n"
r"","\n"
r'import glob, os, sys',"\n"
r'import shlex,subprocess',"\n"
r'import argparse',"\n"
r'from itertools import chain',"\n"
r"","\n"
r"# Add functioning for argument parsing","\n"
r"parser = argparse.ArgumentParser(description='Process grace files to images')","\n"
r"# Add argument type. Destination instance is set to types.","\n"
r"parser.add_argument('-g', action='store_true', dest='relax_gui', help='Make it possible to run script through relax GUI. Run by using User-functions -> script')","\n"
r"parser.add_argument('-l', nargs='+', action='append', dest='l', help='Make in possible to run scriptif relax has logfile turned on. Run by using User-functions -> script')","\n"
r"parser.add_argument('-t', nargs='+', action='append', dest='types', help='List image types for conversion. Execute script with: python %s -t PNG EPS ...'%(sys.argv[0]), default=[])","\n"
r"","\n"
r"# Lets stop the execution and print help if no arguments are passed","\n"
r"if len(sys.argv)==1:","\n"
r" print('system argument is:',sys.argv)","\n"
r" parser.print_help()","\n"
r" sys.exit(1)","\n"
r"","\n"
r"# Parse the arguments to a Class instance object","\n"
r"args = parser.parse_args()","\n"
r"# If we run through the GUI, we cannot pass input arguments, so we make a default PNG option","\n"
r"if args.relax_gui:","\n"
r" args.types = [['PNG']]","\n"
r"","\n"
r"# The instance object will contain a list of lists. We convert this to one list.","\n"
r"types = list(chain.from_iterable(args.types))","\n"
r"","\n"
r"# A easy search for files with *.agt, is to use glob, which is pathnames matching a specified pattern according to the rules used by the Unix shell, not opening a shell","\n"
r'gracefiles = glob.glob("*.agr")',"\n"
r"","\n"
r"# For png conversion, several parameters can be passed to xmgrace. These can be altered later afterwards, and the script rerun. ","\n"
r"# The option for transparent is good for poster or insertion in color backgrounds. The ability for this, still depends on xmgrace compilation","\n"
r'if "PNG" in types:',"\n"
r' pngpar = "png.par"',"\n"
r" if not os.path.isfile(pngpar):","\n"
r' wpngpar = open(pngpar,"w")',"\n"
r' wpngpar.write(r%sDEVICE "PNG" FONT ANTIALIASING on%s+"\n")'%(t,t),"\n"
r' wpngpar.write(r%sDEVICE "PNG" OP "transparent:on"%s+"\n")'%(t,t),"\n"
r' wpngpar.write(r%sDEVICE "PNG" OP "compression:9"%s+"\n")'%(t,t),"\n"
r' wpngpar.close()',"\n"
r'',"\n"
r'# Now loop over the grace files',"\n"
r'for grace in gracefiles:',"\n"
r' # Get the filename without extension',"\n"
r' fname = grace.split(".agr")[0]',"\n"
r' if "PNG" in types:',"\n"
r' # Produce the argument string',"\n"
r' im_args = r"xmgrace -hdevice PNG -hardcopy -param %s -printfile %s.png %s"%(pngpar,fname,grace)',"\n"
r' # Split the arguments the right way, to call xmgrace',"\n"
r' im_args = shlex.split(im_args)',"\n"
r' return_code = subprocess.call(im_args)',"\n"
r' if "EPS" in types:',"\n"
r' im_args = r"xmgrace -hdevice EPS -hardcopy -printfile %s.eps %s"%(fname,grace)',"\n"
r' im_args = shlex.split(im_args)',"\n"
r' return_code = subprocess.call(im_args)',"\n"
r' if ("JPG" or "JPEG") in types:',"\n"
r' im_args = r"xmgrace -hdevice JPEG -hardcopy -printfile %s.jpg %s"%(fname,grace)',"\n"
r' im_args = shlex.split(im_args)',"\n"
r' return_code = subprocess.call(im_args)',"\n"
r' if "SVG" in types:',"\n"
r' im_args = r"xmgrace -hdevice SVG -hardcopy -printfile %s.svg %s"%(fname,grace)',"\n"
r' im_args = shlex.split(im_args)',"\n"
r' return_code = subprocess.call(im_args)',"\n"
r'',"\n"
r'',"\n"

]
file.writelines(lines)
</source>

=== Test the function ===
Then we make a small script to call the function outside relax, to see if we get the wanted output.<br>
We first append to system path, so we can import the modules in relax, and then change to a directory where
we have stored some results.

<source lang="python">
import sys,os
sys.path.append("C:\\WinPython27\\relax_disp")
os.chdir("C:\\WinPython27\\relax_results")

import lib.software.grace
reload(lib.software.grace)

file_name = "grace2images.py"
file = open(file_name,"w")
# Write the file.
lib.software.grace.write_grace2images(file=file)
file.close()

</source>

== Commit the log ==
See [[Category:FAQ]

== See also ==
[[Category:Devel_tutorial]]

Navigation menu