Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

zcav capture

// allows you to run zcav (zoned angular constant velocity) utility included with bonnie++ and automatically plot the results with gnuplot. this one is a real time saver for me.

   1  
   2  #!/bin/bash
   3  #
   4  # ZCAV Capture  btodd3
   5  # version
   6  ver=1.3
   7  
   8  # plot temp file
   9  pscript=/tmp/plotscript
  10  
  11  function make_script () {
  12  echo '# zcav plot script' >> $pscript
  13  echo >> $pscript
  14  echo '# plot format' >> $pscript
  15  #comment terminal line out and uncomment pause line below to send plot to screen
  16  echo set terminal png picsize 1024 768 >> $pscript
  17  echo set key outside below >> $pscript
  18  echo set key box >> $pscript
  19  echo set grid x y2 >> $pscript
  20  echo set xtics >> $pscript
  21  echo set mxtics >> $pscript
  22  echo set ytics >> $pscript
  23  echo set mytics >> $pscript
  24  echo set y2tics nomirror >> $pscript
  25  echo >> $pscript
  26  echo '# labels' >> $pscript
  27  echo set title '"Zoned Constant Angular Velocity (ZCAV) Results\n'${mfg}' '${capacity}' '${interface}'\nModel - '${model}'\nSerial Number - '${serialnum}'"' >> $pscript
  28  echo set xlabel "'Block #'" >> $pscript
  29  echo set ylabel "'K/s'" >> $pscript
  30  echo set y2label "'Read Time'" >> $pscript
  31  echo set label "'Plot Style - "${style}"'" at screen 0.07, screen 0.01 >> $pscript
  32  echo >> $pscript
  33  echo '# plot it' >> $pscript
  34  echo plot "'${zdata}'" using 1:2 "${ptype}" title "'K/s'" axis x1y1 , "'${zdata}'" using 1:3 "${ptype}" title "'Time'" axis x1y2 >> $pscript
  35  echo >> $pscript
  36  #echo pause -1 >> $pscript
  37  echo '# undo what was done' >> $pscript
  38  echo reset >> $pscript
  39  }
  40  
  41  function demo_mode () {
  42  mfg=demo
  43  model=demo
  44  capacity=demo
  45  interface=demo
  46  serialnum=demo
  47  device=demo
  48  zdata="/usr/local/bin/zsample.dat"
  49  zplot=/root/Desktop/demo-zcavplot.png
  50  echo "--- zcavcap Demo Mode ---"
  51  read -p "Enter plot style (1-Normal, 2-Bezier, 4-Unique): " demo_plot_style
  52  
  53  # plot style
  54  case "$demo_plot_style" in
  55  1)
  56  ptype=""
  57  style="Normal"
  58  ;;
  59  2)
  60  ptype="smooth bezier"
  61  style="Bezier"
  62  ;;
  63  4)
  64  ptype="smooth unique"
  65  style="Unique"
  66  ;;
  67  *)
  68  ptype=""
  69  style="Normal"
  70  ;;
  71  esac
  72  
  73  read -p "The following data file will be plotted. (/usr/local/bin/zsample.dat)"
  74  make_script
  75  echo "Generating Plot..."
  76  gnuplot> $zplot $pscript
  77  read -p "Plot script was not deleted. Do you want to delete it now? (1-Yes, 2-No): " pdelete
  78  
  79  # delete plot script or not
  80  case "$pdelete" in
  81  1)
  82  rm -f $pscript
  83  exit 0
  84  ;;
  85  2)
  86  exit 0
  87  ;;
  88  esac
  89  }
  90  
  91  # demo mode or not?
  92  if [ -n "$1" ] && [ "$1" == "--demo" ] ; then
  93  	demo=1
  94  else
  95  	demo=0
  96  fi
  97  
  98  if [ $demo -ne 0 ]; then
  99      demo_mode
 100  fi
 101  
 102  function replot () {
 103  echo "--- zcavcap Replot ---"
 104  read -p "Enter the unit's Manufacturer: " mfg
 105  read -p "Enter the unit's model: " model
 106  read -p "Enter the unit's capacity (i.e. 80GB): " capacity
 107  read -p "Enter the unit's interface type (i.e. SAS, SATA): " interface
 108  read -p "Enter the unit's serial number: " serialnum
 109  read -p "Enter the data file to plot (i.e. /root/Dekstop/sample.dat): " zdata
 110  read -p "Enter plot style (1-Normal, 2-Bezier, 3-Csplines, 4-Unique, 5-Frequency): " replot_style
 111  zplot=/root/Desktop/"${serialnum}-zcavplot.png"
 112  
 113  # replot style
 114  case "$replot_style" in
 115  1)
 116  ptype=""
 117  style="Normal"
 118  ;;
 119  2)
 120  ptype="smooth bezier"
 121  style="Bezier"
 122  ;;
 123  3)
 124  ptype="smooth csplines"
 125  style="Csplines"
 126  ;;
 127  4)
 128  ptype="smooth unique"
 129  style="Unique"
 130  ;;
 131  5)
 132  ptype="smooth frequency"
 133  style="Frequency"
 134  ;;
 135  *)
 136  ptype=""
 137  style="Normal"
 138  ;;
 139  esac
 140  
 141  read -p "The following data file will be plotted. $zdata"
 142  make_script
 143  echo "Generating Plot..."
 144  gnuplot> $zplot $pscript
 145  read -p "Plot script was not deleted. Do you want to delete it now? (1-Yes, 2-No): " pdelete
 146  
 147  # delete plot script or not
 148  case "$pdelete" in
 149  1)
 150  rm -f $pscript
 151  exit 0
 152  ;;
 153  2)
 154  exit 0
 155  ;;
 156  esac
 157  }
 158  
 159  # replot or not?
 160  if [ -n "$1" ] && [ "$1" == "--replot" ] ; then
 161  	rplt=1
 162  else
 163  	rplt=0
 164  fi
 165  
 166  if [ $rplt -ne 0 ]; then
 167      replot
 168  fi
 169  
 170  # trap CTRL-C to clean up temp files
 171  trap "rm -f $pscript ; exit 0" 2
 172  
 173  # start
 174  echo
 175  echo "ZCAV Capture v$ver"
 176  
 177  # uniques
 178  read -p "Enter the unit's Manufacturer: " mfg
 179  read -p "Enter the unit's model: " model
 180  read -p "Enter the unit's capacity (i.e. 80GB): " capacity
 181  read -p "Enter the unit's interface type (i.e. SAS, SATA): " interface
 182  read -p "Enter the unit's serial number: " serialnum
 183  read -p "Enter the unit's device name (i.e. /dev/hdc): " device
 184  read -p "Enter plot style (1-Normal, 2-Bezier, 3-Csplines, 4-Unique, 5-Frequency): " plot_style
 185  read -p "Enter mode (1-Read, 2-Write): " mode
 186  
 187  # setup
 188  zplot=/root/Desktop/"${serialnum}-zcavplot.png"
 189  zdata=/root/Desktop/"${serialnum}.dat"
 190  
 191  # plot style
 192  case "$plot_style" in
 193  1)
 194  ptype=""
 195  style="Normal"
 196  ;;
 197  2)
 198  ptype="smooth bezier"
 199  style="Bezier"
 200  ;;
 201  3)
 202  ptype="smooth csplines"
 203  style="Csplines"
 204  ;;
 205  4)
 206  ptype="smooth unique"
 207  style="Unique"
 208  ;;
 209  5)
 210  ptype="smooth frequency"
 211  style="Frequency"
 212  ;;
 213  *)
 214  ptype=""
 215  style="Normal"
 216  ;;
 217  esac
 218  
 219  # mode
 220  case "$mode" in
 221  1) md=""
 222  modeid="Read mode"
 223  ;;
 224  2) md="-w"
 225  modeid="Write mode"
 226  ;;
 227  *) md=""
 228  modeid="Read mode"
 229  ;;
 230  esac
 231  
 232  # begin capture
 233  echo "Running ZCAV in $modeid - this could take a while depending on disk size and interface..."
 234  zcav $md $device >> $zdata
 235  make_script
 236  echo "Generating Plot..."
 237  gnuplot> $zplot $pscript
 238  rm -f $pscript
 239  sleep 1
 240  echo "Done."

gnuplot sinus example (produce .eps file for inclusion in latex)

This is a very simple gnuplot script to plot a sinus curve and save it to "sinus.eps"

If you saved the code to a file sinus.gnuplot, you can run it with the command:
gnuplot < sinus.gnuplot

The file can then be viewed by ghostview, or converted to pdf via the command "epstopdf"

I found *lots* of help on gnuplot on this website:
http://t16web.lanl.gov/Kawano/gnuplot/index-e.html

   1  
   2  set term postscript eps color blacktext "Helvetica" 24
   3  set output 'sinus.eps'
   4  set ylabel 'sin(x)'
   5  set xlabel 'x'
   6  plot sin(x)
   7  set output
   8  quit

generate .eps figure with python by using gnuplot

this is a sample python script to generate a bode plot (nothing fancy).

Mathematical data is computed using scipy (=scientific python) and numpy (=powerful multidimensional array to use with scipy).

Data is handled to gnuplot by the package gnuplot-py (http://gnuplot-py.sourceforge.net). Tu use gnuplot-py with the new scipy/numpy packages you have to replace "Numeric" by "numpy" in all source files of gnuplot-py before installing. Examples provided by the package won't work, but you can use it without problem

if you want to *automatically* use that in latex see my Makefile code snippets

   1  
   2  #!/usr/bin/python
   3  # -*- coding: utf-8 -*-
   4  
   5  import scipy, numpy, Gnuplot
   6  
   7  N = 1000
   8  x = numpy.randn(N)
   9  y = numpy.randn(N)
  10  X = numpy.dft.fft(x)[1:N/2+1]
  11  Y = numpy.dft.fft(y)[1:N/2+1]
  12  S = X * Y.conj()
  13  S_abs = abs(S)
  14  S_phase = numpy.arctan(S.imag / S.real)
  15  
  16  # 100 Hz
  17  fsamp = 100.
  18  freq = numpy.arange(1,N/2+1) * fsamp / N
  19  
  20  plot = Gnuplot.Gnuplot()
  21  data_abs = Gnuplot.Data(freq, S_abs, with='lines')
  22  data_phase = Gnuplot.Data(freq, S_phase, with='lines')
  23  plot('set term postscript eps color blacktext "Helvetica" 24')
  24  plot("set output 'bode.eps'")
  25  plot('set multiplot')
  26  plot('set size 1.0,0.5')
  27  plot('set origin 0.0,0.5')
  28  plot("set ylabel 'amplitude'")
  29  plot('set logscale y')
  30  plot.plot(data_abs)
  31  plot('set origin 0.0,0.0')
  32  plot("set ylabel 'phase'")
  33  plot("set xlabel 'Frequency (Hz)'")
  34  plot('set nologscale y')
  35  plot.plot(data_phase)
  36  plot('set nomultiplot')
  37  plot('set output')
  38  plot('quit')

Makefile for images in latex

This is a Makefile to build images for use by pdflatex from .gnuplot command files and .py python scripts. It is supposed that the gnuplot/python files produce .eps images when launched

   1  
   2  all: sinus.pdf sincos.pdf sine.pdf sine2.pdf sincos2.pdf bode.pdf
   3  
   4  .PHONY: all clean
   5  
   6  # we want all .gnuplot files to be 'made' into .eps files
   7  # the % replaces any name
   8  # in the rule: 	$< replaces the source
   9  # 				$@ replaces the target
  10  # example: convert $< $@
  11  # 
  12  %.eps: %.gnuplot
  13  	gnuplot < $<
  14  
  15  %.eps: %.py
  16  	python $<
  17  
  18  %.pdf: %.eps
  19  	epstopdf $<
  20  	
  21  clean:
  22  	rm -f *.eps *.pdf
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS