mailr20196 - in /branches/relax_disp: lib/software/grace.py specific_analyses/relax_disp/disp_data.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on June 18, 2013 - 17:29:
Author: bugman
Date: Tue Jun 18 17:29:16 2013
New Revision: 20196

URL: http://svn.gna.org/viewcvs/relax?rev=20196&view=rev
Log:
Fix for bug #20916, (https://gna.org/bugs/?20916) - Suggestion for python 
script for PNG/EPS/SVG conversion of grace files.

Troels Linnet provided this patch, and was discovered during work on a 
Windows 7 system: telinnet
aaattt bio_dot_ku_dot _dk

This patch will provide a grace2images.py file in each folder where a call to
specific_analyses/relax_disp/disp_data.py is called.

It is called in: def plot_disp_curves(dir=None, force=None):
and call the function: lib.software.grace.script_grace2images()

The conversion script can be executed in linux and windows, if the PATH to 
xmgrace has been
specified. It will look in a folder for grace files of ending *.agr and by 
default convert to PNG.

One can also convert to EPS and SVG. Propably more options could be added, as 
PDF. The conversion
depends on xmgrace compilation, and so PNG conversion is for fast inspection 
of graphs in folder,
and EPS for further external conversion to PDF etc.

The patch, the output file, and small script to test is attached.

I miss to make the file executable in relax, so the script can be executed 
directly in Linux.


Modified:
    branches/relax_disp/lib/software/grace.py
    branches/relax_disp/specific_analyses/relax_disp/disp_data.py

Modified: branches/relax_disp/lib/software/grace.py
URL: 
http://svn.gna.org/viewcvs/relax/branches/relax_disp/lib/software/grace.py?rev=20196&r1=20195&r2=20196&view=diff
==============================================================================
--- branches/relax_disp/lib/software/grace.py (original)
+++ branches/relax_disp/lib/software/grace.py Tue Jun 18 17:29:16 2013
@@ -1,6 +1,7 @@
 
###############################################################################
 #                                                                            
 #
 # Copyright (C) 2003-2013 Edward d'Auvergne                                  
 #
+# Copyright (C) 2013 Troels E. Linnet                                        
 #
 #                                                                            
 #
 # This file is part of the program relax (http://www.nmr-relax.com).         
 #
 #                                                                            
 #
@@ -26,6 +27,86 @@
 import pipe_control
 from pipe_control import pipes
 import specific_analyses
+
+
+def script_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. Successful 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.
+
+    @keyword file:          The file object to write the data to.
+    @type file:             file object
+    """
+
+    # Write to file
+    file.write("#!/usr/bin/env python\n")
+    file.write("\n")
+    file.write("import glob, os, sys\n")
+    file.write("import shlex,subprocess\n")
+    file.write("import argparse\n")
+    file.write("from itertools import chain\n")
+    file.write("\n")
+    file.write("# Add functioning for argument parsing\n")
+    file.write("parser = argparse.ArgumentParser(description='Process grace 
files to images')\n")
+    file.write("# Add argument type. Destination instance is set to 
types.\n")
+    file.write("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")
+    file.write("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")
+    file.write("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")
+    file.write("\n")
+    file.write("# Lets stop the execution and print help if no arguments are 
passed\n")
+    file.write("if len(sys.argv)==1:\n")
+    file.write("    print('system argument is:',sys.argv)\n")
+    file.write("    parser.print_help()\n")
+    file.write("    print('Performing a default PNG conversion')\n")
+    file.write("\n")
+    file.write("# Parse the arguments to a Class instance object\n")
+    file.write("args = parser.parse_args()\n")
+    file.write("# If we run through the GUI, we cannot pass input arguments, 
so we make a default PNG option\n")
+    file.write("if args.relax_gui:\n")
+    file.write("    args.types = [['PNG']]\n")
+    file.write("# If no input arguments, we make a default PNG option\n")
+    file.write("if len(args.types) == 0:\n")
+    file.write("    args.types = [['PNG']]\n")
+    file.write("\n")
+    file.write("# The instance object will contain a list of lists. We 
convert this to one list.\n")
+    file.write("types = list(chain.from_iterable(args.types))\n")
+    file.write("\n")
+    file.write("# A easy search for files with *.agr, 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")
+    file.write("gracefiles = glob.glob(\"*.agr\")\n")
+    file.write("\n")
+    file.write("# For png conversion, several parameters can be passed to 
xmgrace. These can be altered later afterwards, and the script rerun. \n")
+    file.write("# The option for transparent is good for poster or insertion 
in color backgrounds. The ability for this, still depends on xmgrace 
compilation\n")
+    file.write("if \"PNG\" in types:\n")
+    file.write("    pngpar = \"png.par\"\n")
+    file.write("    if not os.path.isfile(pngpar):\n")
+    file.write("        wpngpar = open(pngpar,\"w\")\n")
+    file.write("        wpngpar.write(\"DEVICE \\\"PNG\\\" FONT ANTIALIASING 
on\\n\")\n")
+    file.write("        wpngpar.write(\"DEVICE \\\"PNG\\\" OP 
\\\"transparent:on\\\"\\n\")\n")
+    file.write("        wpngpar.write(\"DEVICE \\\"PNG\\\" OP 
\\\"compression:9\\\"\\n\")\n")
+    file.write("        wpngpar.close()\n")
+    file.write("\n")
+    file.write("# Now loop over the grace files\n")
+    file.write("for grace in gracefiles:\n")
+    file.write("    # Get the filename without extension\n")
+    file.write("    fname = grace.split(\".agr\")[0]\n")
+    file.write("    if \"PNG\" in types:\n")
+    file.write("        # Produce the argument string\n")
+    file.write("        im_args = r\"xmgrace -hdevice PNG -hardcopy -param 
%s -printfile %s.png %s\"%(pngpar,fname,grace)\n")
+    file.write("        # Split the arguments the right way, to call 
xmgrace\n")
+    file.write("        im_args = shlex.split(im_args)\n")
+    file.write("        return_code = subprocess.call(im_args)\n")
+    file.write("    if \"EPS\" in types:\n")
+    file.write("        im_args = r\"xmgrace -hdevice EPS -hardcopy 
-printfile %s.eps %s\"%(fname,grace)\n")
+    file.write("        im_args = shlex.split(im_args)\n")
+    file.write("        return_code = subprocess.call(im_args)\n")
+    file.write("    if (\"JPG\" or \"JPEG\") in types:\n")
+    file.write("        im_args = r\"xmgrace -hdevice JPEG -hardcopy 
-printfile %s.jpg %s\"%(fname,grace)\n")
+    file.write("        im_args = shlex.split(im_args)\n")
+    file.write("        return_code = subprocess.call(im_args)\n")
+    file.write("    if \"SVG\" in types:\n")
+    file.write("        im_args = r\"xmgrace -hdevice SVG -hardcopy 
-printfile %s.svg %s\"%(fname,grace)\n")
+    file.write("        im_args = shlex.split(im_args)\n")
+    file.write("        return_code = subprocess.call(im_args)\n")
 
 
 def write_xy_data(data, file=None, graph_type=None, norm=False):

Modified: branches/relax_disp/specific_analyses/relax_disp/disp_data.py
URL: 
http://svn.gna.org/viewcvs/relax/branches/relax_disp/specific_analyses/relax_disp/disp_data.py?rev=20196&r1=20195&r2=20196&view=diff
==============================================================================
--- branches/relax_disp/specific_analyses/relax_disp/disp_data.py (original)
+++ branches/relax_disp/specific_analyses/relax_disp/disp_data.py Tue Jun 18 
17:29:16 2013
@@ -2,6 +2,7 @@
 #                                                                            
 #
 # Copyright (C) 2004-2013 Edward d'Auvergne                                  
 #
 # Copyright (C) 2009 Sebastien Morin                                         
 #
+# Copyright (C) 2013 Troels E. Linnet                                        
 #
 #                                                                            
 #
 # This file is part of the program relax (http://www.nmr-relax.com).         
 #
 #                                                                            
 #
@@ -40,7 +41,7 @@
 from lib.io import get_file_path, open_write_file
 from lib.list import count_unique_elements, unique_elements
 from lib.physical_constants import g1H, return_gyromagnetic_ratio
-from lib.software.grace import write_xy_data, write_xy_header
+from lib.software.grace import write_xy_data, write_xy_header, 
script_grace2images
 from pipe_control import pipes
 from pipe_control.mol_res_spin import exists_mol_res_spin_data, return_spin, 
spin_loop
 from pipe_control.result_files import add_result_file
@@ -365,6 +366,7 @@
 
     One file will be created per spin system.
 
+    A python "grace to PNG/EPS/SVG..." conversion script is created at the 
end
 
     @keyword dir:           The optional directory to place the file into.
     @type dir:              str
@@ -504,6 +506,17 @@
 
         # Add the file to the results file list.
         add_result_file(type='grace', label='Grace', file=file_path)
+
+    # 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.
+    script_grace2images(file=file)
+
+    # Close the file.
+    file.close()
 
 
 def plot_exp_curves(file=None, dir=None, force=None, norm=None):




Related Messages


Powered by MHonArc, Updated Tue Jun 18 17:40:02 2013