You are here: Home / Tools & Utilities / Python / Python script for automatically generating a basic Tioga script

Python script for automatically generating a basic Tioga script

Here is a mildly interactive python script for reading in a MESA log or profile, automatically storing the data in a two-dimensional array (even with non-standard columns), and producing a rudimentary Tioga script which can be tweaked to make nice plots pretty easily.

tioga_starter.py — Python Source, 3 kB (3872 bytes)

File contents

# tioga_starter.py 
# Adapated from an earlier version by Bill Wolf.
filename = raw_input('Enter the log file from which you want to make a plot: ')
line_skip = 6
x_var = raw_input('Enter the x variable to plot (e.g. star_age or log_Teff for logs; mass or logR for profiles): ') 
y_var = raw_input('Enter the y variable to plot (e.g. log_L for logs; logT or logRho for profiles): ')
tioga_name = raw_input('Enter the name of the tioga file you wish to create (omit extension) : ')
f = open(filename, 'r')
filelines = f.readlines()
header = filelines[line_skip-1]
f.close()
import shlex
header_names = shlex.split(header)
f = open(tioga_name + '.rb', 'w')
f.write("require 'Tioga/FigureMaker'\n\n")
f.write('    class MyFigures\n\n')
f.write('        include Tioga\n')
f.write('        include FigureConstants\n\n')
f.write('        def t\n')
f.write('            @figure_maker\n')
f.write('        end\n\n')
f.write('        def initialize\n')
f.write('            @figure_maker = FigureMaker.default\n')
f.write('            t.tex_preamble = t.tex_preamble + "\\n\\t\\\\usepackage{amsmath}\\\usepackage{verbatim}\\n"\n\n')
f.write('            @margin = 0.1\n\n')
f.write('            #Note: variables start w/ lower case letter, consts are upper case\n\n')
for x in header_names:
    f.write('            @' + x + ' = Dvector.new()\n')
f.write('\n')
f.write('            @data_array = [ ')
for i in range(len(header_names)):
    if i < (len(header_names)-1):
        f.write('@' + header_names[i] + ', ')
    elif i == len(header_names) - 1:
        f.write('@' + header_names[i] + ' ]\n\n')
f.write("        t.def_figure('" + tioga_name + "') { " + tioga_name + " }\n\n")
f.write("        t.save_dir = './figures_out'\n")
f.write('        end\n\n')
f.write('        def plot_boundaries(xs,ys,margin,ymin=nil,ymax=nil,reverse_xaxis=false,reverse_yaxis=false)\n')
f.write('        xmin = xs.min\n')
f.write('        xmax = xs.max\n')
f.write('        ymin = ys.min if ymin == nil\n')
f.write('        ymax = ys.max if ymax == nil\n')
f.write('        width = (xmax == xmin)? 1 : xmax - xmin\n')
f.write('        height = (ymax == ymin)? 1 : ymax - ymin\n')
f.write('        left_boundary = xmin - margin * width\n')
f.write('        right_boundary = xmax + margin * width\n')
f.write('        top_boundary = ymax + margin * height\n')
f.write('        bottom_boundary = ymin - margin * height\n')
f.write('        if reverse_xaxis\n')
f.write('           tmp = left_boundary; left_boundary = right_boundary; right_boundary = tmp\n')
f.write('        end\n')
f.write('        if reverse_yaxis\n')
f.write('           tmp = top_boundary; top_boundary = bottom_boundary; bottom_boundary = tmp\n')
f.write('        end\n')
f.write('        return [ left_boundary, right_boundary, top_boundary, bottom_boundary ]\n')
f.write('        end\n\n')
f.write('        def ' + tioga_name + '\n')
f.write('        t.title_color = Black\n')
f.write('        t.xlabel_scale = 1.0\n')
f.write('        t.ylabel_scale = 1.0\n')
f.write('        t.title_scale = 1.0\n')
f.write("        t.do_box_labels('plot', 'xvar', 'yvar')\n\n\n")
f.write("        Dvector.read('" + filename + "', @data_array, " + str(line_skip+1) + ")\n")
f.write('        t.yaxis_log_values = false\n')
f.write('        t.rescale(1.5)\n')
f.write("      	t.show_plot_with_legend('legend_left_margin' => 0.1,'plot_scale' => 0.7,'legend_scale' => 1,'plot_right_margin' => 0) {\n")
f.write("          	t.show_plot(plot_boundaries(@" + x_var + ",@" + y_var + ",@margin,nil,nil)) {\n")
f.write("			t.line_width = 1.0\n")
f.write("			t.show_polyline(@" + x_var + ", @" + y_var + ", Red,'yvar')\n")
f.write("          				}\n")
f.write('        }\n')
f.write('        end\n\n')

f.write('        MyFigures.new\n')
f.write('    end')
f.close()