#!/usr/bin/awk -f # # label_contours.awk # # This awk script generates gnuplot script containing labels to label contours # found in a file generated by a gnuplot splot command with output to the table # terminal. The output should be redirected to another file and then be loaded # into gnuplot; this results in a contour plot. # # Usage (from gnuplot): # !awk -f label_contours.awk [-v options] contour.dat > contour.gp # load 'contour.gp' # # Options: # nth=20 label every nth point on each contour # nfrom=2 start labeling at this point on each contour # center=1 center and overprint label on the contour # center=0 automagically place label bottom / right wrt the contour # textcolor=0 label text color is black # textcolor=1 1, 2, 3, ... use this linetype color for text color # textcolor=-1 use contour color for the text label # inclt=0 don't increment linetype at every contour # inclt=1 1, 2, 3, .. increment linetype at every contour, start with this offset # # Default is: nth=20 nfrom=0 center=0 textcolor=0 inclt=0 # # Example of use (from gnuplot): # set contour base; set cntrparam levels 15; unset surface # set table "contour.dat"; splot x*x+y*y; unset table # !awk -f label_contours.awk -v nth=10 textcolor=-1 inclt=1 contour.dat >tmp.gp # reset; set key outside left; load 'tmp.gp' # # # Author: Petr Mikulik # Version: July 2009 # License: public domain # # History: # July 2009: Updated for gnuplot >4.2 # April 2004: Original version BEGIN { # Setup variables # textcolor = 0 | 1 for black or linetype-like text # inclt = 0 | startup_linetype_to_increment # Initialize local variables: PI = 3.1415926536 plotindex = -1 contour_nb = -1 label_nth = (nth <= 0) ? 20 : nth if (nfrom <= 0) nfrom = 0 } NR==1 { # print "set key title '" FILENAME "'"; plotcmd = "plot '"FILENAME"'" } # Header of new contour found: $2=="Contour" { plotindex++ contour_startpt_NR = NR + 1 contour_nb = substr($3, 1, length($3)-1) # Generate a fragment of the plot command for each contour if (inclt > 0) { if ($3!="0,") plotcmd = plotcmd ",\\\n\t''" plotcmd = plotcmd sprintf(" index %i title '%s' w l lt %i", plotindex, $5, plotindex) } } # Reset start line of part of contour, if follows afterwards: NF==0 { contour_startpt_NR = NR + 1 } # Add labels at every label_nth point along a contour # (note: prev_x, prev_y must already be known for this contour): contour_nb>=0 && label_nth>0 && ((NR-contour_startpt_NR-nfrom) % label_nth) == 1 { if (textcolor == 0) color = "" else color = sprintf(" tc lt %i", (textcolor > 0) ? textcolor : plotindex); if (center) printf("set label \"%s\" at %g,%g center front%s\n", $3, $1, $2, color); else { # Position label according to derivative: dx = ($1 - prev_x) dy = ($2 - prev_y) alfa = atan2(dy, dx) # angle of the 1st derivative in [-PI; PI] alfa = int(alfa*180/PI) # in degrees, for our convenience # Where to position the label? if (fabs(alfa) < 15 || fabs(alfa) > 180-15) where = "bottom" else where = "right" # Make gnuplot's label string according to where position # (note: gnuplot cannot flush in vertical direction (top or bottom): if (where == "right") { label = " " $3 if (fabs(alfa) < 45 || fabs(alfa) > 180-45) label = " " label if (fabs(alfa) < 20 || fabs(alfa) > 180-20) label = " " label flush = "left" } else if (where == "left") { label = $3 flush = "right" } else if (where == "bottom") { # currently unused label = "\\n" $3 flush = "center" } else { # unspecified label = $3 flush = "center" } # label = label " " alfa # for testing purposes printf("set label \"%s\" at %g,%g front %s%s\n", label, $1, $2, flush, color); } } # Remember previous values of x, y { prev2_x = prev_x prev2_y = prev_y prev_x = $1 prev_y = $2 } # Write out the entire plot command END { print "\n" plotcmd } # Helper functions function sgn ( x ) { return (x == 0) ? 0 : ( (x > 0) ? 1 : -1 ) # return (x >= 0) ? 1 : -1 } function fabs ( x ) { return (x >= 0) ? x : -x } #eof label_contours.awk