Move gen_data_*.c from src/mandelbrot to src/plot_image & add README, Makefile.
authorW. Trevor King <wking@drexel.edu>
Tue, 9 Nov 2010 15:22:20 +0000 (10:22 -0500)
committerW. Trevor King <wking@tremily.us>
Fri, 22 Feb 2013 17:11:09 +0000 (09:11 -0800)
content/programming_strategies/Python_Codes/plot_image.py [deleted file]
src/mandelbrot/gen_data.c [deleted file]
src/mandelbrot/gen_data_rectangle.c [deleted file]
src/mandelbrot/gen_data_sharp.c [deleted file]
src/plot_image/Makefile [new file with mode: 0644]
src/plot_image/README [new file with mode: 0644]
src/plot_image/gen_data_rectangle.c [new file with mode: 0644]
src/plot_image/gen_data_sharp.c [new file with mode: 0644]
src/plot_image/gen_data_sinusoid.c [new file with mode: 0644]
src/plot_image/plot_image.py

diff --git a/content/programming_strategies/Python_Codes/plot_image.py b/content/programming_strategies/Python_Codes/plot_image.py
deleted file mode 100755 (executable)
index 09fbab1..0000000
+++ /dev/null
@@ -1,120 +0,0 @@
-#! /usr/bin/env python
-
-from __future__ import division
-from pylab import *
-import sys
-import Numeric
-
-
-#
-#   plot_image.py
-#
-#   Step 1: make this file executable
-#
-#           chmod +x plot_image.py
-#
-#   Step 2: pipe data in python script
-#
-#           ./gen_data | ./plot_image -s nx ny -c nc -t 'image title'
-#
-#   with optional arguments
-#          -s nx ,ny    image size [16x16]
-#          -c nc        number of contour levels [none]
-#          -t ' '       image title ['some like it hot']
-#
-#   additional:  -g  gray map  [jet map]
-#                -h  hot map
-#
-#   ref: matplotlib web site
-#                                             Michel Vallieres, 2007
-#
-
-                        # dummy function to initialize Z
-def func3(x,y):
-    return 0.005*x*y
-
-                        # defaults
-mycontours = 0
-nx = 16
-ny = 16
-mytitle = 'Some like it hot'
-mymap = cm.jet
-
-                        # parse command line arguments
-n = len( sys.argv )
-i = 1
-while i < n:
-    if sys.argv[i].find("s") == 1:
-        nx = int( sys.argv[i+1] )
-        ny = int( sys.argv[i+2] )
-        i = i + 2
-    elif sys.argv[i].find("c") == 1:
-        mycontours = int( sys.argv[i+1] )
-        i = i + 1
-    elif sys.argv[i].find("t") == 1:
-        mytitle = sys.argv[i+1]
-        i = i + 1
-    elif sys.argv[i].find("g") == 1:
-        mymap = cm.gray
-    elif sys.argv[i].find("h") == 1:
-        mymap = cm.hot
-    else:
-        print " Syntax:    script -s nx ny -c "
-    i = i + 1
-
-                       # identification
-print "Plot_image"
-print "Title:            ", mytitle
-print "Image size:       ", nx, ny
-print "# countour lines: ", mycontours
-
-
-                       # set grid
-x = range( nx )
-y = range( ny )
-
-X,Y = meshgrid( x, y )
-
-Z = func3( X, Y )
-
-                       # read in data
-for j in y:
-   for i in x:
-      Z[j,i] = input()
-
-                       # min & max
-min_data = Z[0,0]
-max_data = Z[0,0]
-for i in x:
-   for j in y:
-      if Z[j,i] < min_data:
-          min_data = Z[j,i]
-      if Z[j,i] > max_data:
-          max_data = Z[j,i]
-
-print "Data range:       ", min_data, max_data
-
-
-                       # colored image
-im = imshow( Z, interpolation='bilinear', origin='lower',
-            cmap=mymap, extent=(1,nx-1.0,1,ny-1.0) )
-
-                       # contour lines
-if mycontours > 0:
-    dcont = ( max_data - min_data ) / ( mycontours - 1 )
-    cset = contour( Z, arange(min_data,max_data,dcont),
-               origin='lower',
-               linewidths=2,
-               extent=(0,nx-1,0,ny-1)
-               )
-
-    clabel( cset, inline=1, fmt='%1.1f', fontsize=10 )
-
-
-                       # render picture
-axis('off')
-
-colorbar()
-title( mytitle )
-show()
-
diff --git a/src/mandelbrot/gen_data.c b/src/mandelbrot/gen_data.c
deleted file mode 100644 (file)
index 665cd2e..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-#include <stdio.h>
-#include <math.h>
-
-#define N_POINTS 200
-
-main()
-{
-    int i, j;
-    double x, y, dx, dy;
-
-    dx = 2*M_PI/N_POINTS;
-    dy = 2*M_PI/N_POINTS;
-    for (j = 0; j < N_POINTS; j++)
-       for (i = 0; i < N_POINTS; i++) {
-           x = i*dx;
-           y = j*dy;
-           printf("%f \n", 5.0*(1 + sin(x)*cos(y)));
-       }
-}
-
diff --git a/src/mandelbrot/gen_data_rectangle.c b/src/mandelbrot/gen_data_rectangle.c
deleted file mode 100644 (file)
index ca94d9b..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-#include <stdio.h>
-#include <math.h>
-
-#define N_POINTSx 300
-#define N_POINTSy 200
-
-main()
-{
-    int i, j;
-    double x, y, dx, dy;
-
-    dx = 2*M_PI/N_POINTSx;
-    dy = dx;
-    for (j = 0; j < N_POINTSy; j++)
-      for (i = 0; i < N_POINTSx; i++) {
-            x = i*dx;
-            y = j*dy;
-           printf("%f \n", 5.0*(1 + sin(x)*cos(y)));
-       }
-}
-
diff --git a/src/mandelbrot/gen_data_sharp.c b/src/mandelbrot/gen_data_sharp.c
deleted file mode 100644 (file)
index f62aecf..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-#include <stdio.h>
-#include <math.h>
-
-#define N_POINTS 200
-
-main()
-{
-    int i, j;
-    double x, y, value;
-
-    for (j = 0; j < N_POINTS; j++)
-       for (i = 0; i < N_POINTS; i++) {
-         value = 0.0;
-          if ( j > N_POINTS/2 )
-               value = 1.5;
-         printf("%f \n", value);
-       }
-}
-
diff --git a/src/plot_image/Makefile b/src/plot_image/Makefile
new file mode 100644 (file)
index 0000000..d0e5377
--- /dev/null
@@ -0,0 +1,25 @@
+CC       = /usr/bin/mpicc
+CFLAGS   =
+LD       = $(CC)
+LDFLAGS  =
+RM       = /bin/rm
+EXAMPLES = rectangle sharp sinusoid
+EXECS = $(EXAMPLES:%=gen_data_%)
+FIGURES = $(EXAMPLES:%=%.png)
+
+all: $(FIGURES)
+
+%.o : %.c
+       $(CC) -c $(CFLAGS) -o $@ $^
+
+$(EXECS) : % : %.o
+       $(LD) $(LDFLAGS) -o $@ $^
+
+$(FIGURES) : %.png : gen_data_% plot_image.py
+       ./$< | ./plot_image.py -t "$(<:%.png=%)" -s 300,200 -o "$@"
+
+clean:
+       $(RM) -f *.o $(EXECS) $(FIGURES)
+
+# Interesting Makefile sections
+#   4.12.1 Syntax of Static Pattern Rules
diff --git a/src/plot_image/README b/src/plot_image/README
new file mode 100644 (file)
index 0000000..59716a5
--- /dev/null
@@ -0,0 +1,11 @@
+`Simple image plotting.
+
+Generate some example images with::
+
+    $ make
+
+Expected output:
+
+* rectangle.png  High values in a wide, centered rectangle.
+* sharp.png      High values in the top half of the figure.
+* sinusoid.png   5*(1+sin(x)*cos(y)) for x,y in [0,2pi).
diff --git a/src/plot_image/gen_data_rectangle.c b/src/plot_image/gen_data_rectangle.c
new file mode 100644 (file)
index 0000000..db1b920
--- /dev/null
@@ -0,0 +1,19 @@
+#include <stdio.h>
+#include <math.h>
+
+#define N_X 300
+#define N_Y 200
+
+main()
+{
+       int i, j, z;
+
+       for (j = 0; j < N_Y; j++) {
+               for (i = 0; i < N_X; i++) {
+                       z = 0;
+                       if (i > N_X/5 && i < N_X*4/5 && j > N_Y/3 && j < N_Y*2/3)
+                               z = 1;
+                       printf("%d\n", z);
+               }
+       }
+}
diff --git a/src/plot_image/gen_data_sharp.c b/src/plot_image/gen_data_sharp.c
new file mode 100644 (file)
index 0000000..c0abf3e
--- /dev/null
@@ -0,0 +1,19 @@
+#include <stdio.h>
+#include <math.h>
+
+#define N_X 300
+#define N_Y 200
+
+main()
+{
+       int i, j;
+       double x, y, value;
+
+       for (j = 0; j < N_Y; j++)
+               for (i = 0; i < N_X; i++) {
+                       value = 0.0;
+                       if (j > N_Y / 2)
+                               value = 1.5;
+                       printf("%f\n", value);
+               }
+}
diff --git a/src/plot_image/gen_data_sinusoid.c b/src/plot_image/gen_data_sinusoid.c
new file mode 100644 (file)
index 0000000..7c27c66
--- /dev/null
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#include <math.h>
+
+#define N_X 300
+#define N_Y 200
+
+main()
+{
+       int i, j;
+       double x, y, dx, dy;
+
+       dx = 2 * M_PI / N_X;
+       dy = 2 * M_PI / N_Y;
+       for (j = 0; j < N_Y; j++) {
+               for (i = 0; i < N_X; i++) {
+                       x = i * dx;
+                       y = j * dy;
+                       printf("%f \n", 5.0 * (1 + sin(x) * cos(y)));
+               }
+       }
+}
index 188a73d4a1bf6a6947f874cd70d52511a0b20bf8..0262cfdad5fe2ca666d240bf843ca78cb00ca98a 100755 (executable)
@@ -63,7 +63,9 @@ def read_data(stream, nx, ny):
     """
     X,Y = pylab.meshgrid(range(nx+1), range(ny+1))
     Z = numpy.loadtxt(stream)
-    Z = Z.reshape([x-1 for x in X.shape])
+    assert Z.size == nx*ny, 'Z.size = %d != %d = %dx%d' % (
+        Z.size, nx*ny, nx, ny)
+    Z = Z.reshape([x-1 for x in X.shape])    
     return (X,Y,Z)