From 4f105b7b2942b5d120970925bc9cda69d519c107 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 9 Nov 2010 10:22:20 -0500 Subject: [PATCH] Move gen_data_*.c from src/mandelbrot to src/plot_image & add README, Makefile. --- .../Python_Codes/plot_image.py | 120 ------------------ src/mandelbrot/gen_data.c | 20 --- src/mandelbrot/gen_data_rectangle.c | 21 --- src/mandelbrot/gen_data_sharp.c | 19 --- src/plot_image/Makefile | 25 ++++ src/plot_image/README | 11 ++ src/plot_image/gen_data_rectangle.c | 19 +++ src/plot_image/gen_data_sharp.c | 19 +++ src/plot_image/gen_data_sinusoid.c | 21 +++ src/plot_image/plot_image.py | 4 +- 10 files changed, 98 insertions(+), 181 deletions(-) delete mode 100755 content/programming_strategies/Python_Codes/plot_image.py delete mode 100644 src/mandelbrot/gen_data.c delete mode 100644 src/mandelbrot/gen_data_rectangle.c delete mode 100644 src/mandelbrot/gen_data_sharp.c create mode 100644 src/plot_image/Makefile create mode 100644 src/plot_image/README create mode 100644 src/plot_image/gen_data_rectangle.c create mode 100644 src/plot_image/gen_data_sharp.c create mode 100644 src/plot_image/gen_data_sinusoid.c diff --git a/content/programming_strategies/Python_Codes/plot_image.py b/content/programming_strategies/Python_Codes/plot_image.py deleted file mode 100755 index 09fbab1..0000000 --- a/content/programming_strategies/Python_Codes/plot_image.py +++ /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 index 665cd2e..0000000 --- a/src/mandelbrot/gen_data.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include - -#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 index ca94d9b..0000000 --- a/src/mandelbrot/gen_data_rectangle.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include - -#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 index f62aecf..0000000 --- a/src/mandelbrot/gen_data_sharp.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include - -#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 index 0000000..d0e5377 --- /dev/null +++ b/src/plot_image/Makefile @@ -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 index 0000000..59716a5 --- /dev/null +++ b/src/plot_image/README @@ -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 index 0000000..db1b920 --- /dev/null +++ b/src/plot_image/gen_data_rectangle.c @@ -0,0 +1,19 @@ +#include +#include + +#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 index 0000000..c0abf3e --- /dev/null +++ b/src/plot_image/gen_data_sharp.c @@ -0,0 +1,19 @@ +#include +#include + +#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 index 0000000..7c27c66 --- /dev/null +++ b/src/plot_image/gen_data_sinusoid.c @@ -0,0 +1,21 @@ +#include +#include + +#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))); + } + } +} diff --git a/src/plot_image/plot_image.py b/src/plot_image/plot_image.py index 188a73d..0262cfd 100755 --- a/src/plot_image/plot_image.py +++ b/src/plot_image/plot_image.py @@ -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) -- 2.26.2