Move MS*.c from mandelbrot/MS-codes/ to mandelbrot/.
[parallel_computing.git] / src / monte_carlo / plot_image.py
1 #! /usr/bin/env python
2
3 from __future__ import division
4 from pylab import *
5 import sys
6 import Numeric
7
8
9 #
10 #   plot_image.py
11 #
12 #   Step 1: make this file executable
13 #
14 #           chmod +x plot_image.py
15 #
16 #   Step 2: pipe data in python script
17 #
18 #           ./gen_data | ./plot_image -s nx ny -c nc -t 'image title'
19 #
20 #   with optional arguments
21 #          -s nx ,ny    image size [16x16]
22 #          -c nc        number of contour levels [none]
23 #          -t ' '       image title ['some like it hot']
24 #
25 #   additional:  -g  gray map  [jet map]
26 #                -h  hot map
27 #
28 #   ref: matplotlib web site
29 #                                             Michel Vallieres, 2007
30 #
31
32                         # dummy function to initialize Z
33 def func3(x,y):
34     return 0.005*x*y
35
36                         # defaults
37 mycontours = 0
38 nx = 16
39 ny = 16
40 mytitle = 'Some like it hot'
41 mymap = cm.jet
42
43                         # parse command line arguments
44 n = len( sys.argv )
45 i = 1
46 while i < n:
47     if sys.argv[i].find("s") == 1:
48         nx = int( sys.argv[i+1] )
49         ny = int( sys.argv[i+2] )
50         i = i + 2
51     elif sys.argv[i].find("c") == 1:
52         mycontours = int( sys.argv[i+1] )
53         i = i + 1
54     elif sys.argv[i].find("t") == 1:
55         mytitle = sys.argv[i+1]
56         i = i + 1
57     elif sys.argv[i].find("g") == 1:
58         mymap = cm.gray
59     elif sys.argv[i].find("h") == 1:
60         mymap = cm.hot
61     else:
62         print " Syntax:    script -s nx ny -c "
63     i = i + 1
64
65                        # identification
66 print "Plot_image"
67 print "Title:            ", mytitle
68 print "Image size:       ", nx, ny
69 print "# countour lines: ", mycontours
70
71
72                        # set grid
73 x = range( nx )
74 y = range( ny )
75
76 X,Y = meshgrid( x, y )
77
78 Z = func3( X, Y )
79
80                        # read in data
81 for j in y:
82    for i in x:
83       Z[j,i] = input()
84
85                        # min & max
86 min_data = Z[0,0]
87 max_data = Z[0,0]
88 for i in x:
89    for j in y:
90       if Z[j,i] < min_data:
91           min_data = Z[j,i]
92       if Z[j,i] > max_data:
93           max_data = Z[j,i]
94
95 print "Data range:       ", min_data, max_data
96
97
98                        # colored image
99 im = imshow( Z, interpolation='bilinear', origin='lower',
100             cmap=mymap, extent=(1,nx-1.0,1,ny-1.0) )
101
102                        # contour lines
103 if mycontours > 0:
104     dcont = ( max_data - min_data ) / ( mycontours - 1 )
105     cset = contour( Z, arange(min_data,max_data,dcont),
106                origin='lower',
107                linewidths=2,
108                extent=(0,nx-1,0,ny-1)
109                )
110
111     clabel( cset, inline=1, fmt='%1.1f', fontsize=10 )
112
113
114                        # render picture
115 axis('off')
116
117 colorbar()
118 title( mytitle )
119 show()