#!/bin/sh # # @(#)plotcurves 1997/02/13 Anne Bennett # # Use gnuplot to print graphs of the data of interest, # i.e. principal remaining to pay on mortgage, actual and # projected. # # If an argument is given, it is assumed to be the name of # the output file; if no argument, graph should be plotted # to the terminal. # # Mortgage data will be found in files whose format is # 728854 19950715 10577535 blah blah # date-1 date-2 amount-1 label # where # date-1 = date in days-since-epoch # date-2 = human-usable date in YYYYMMDD # amount-1 = amount of capital remaining to pay, in cents # label = optional field(s) to identify curve. thisprog=mortgage-plotcurves PATH=/local/bin:/anne/accounting/bin:/bin:/usr/bin export PATH # Directory containing files of data datadir=/anne/accounting/mortgage/data # Prefix name of files containing data to be plotted: prefix=curve. # Path to gnuplot gnuplot=`which gnuplot` # Path to ndate ndate=`which ndate` # Need a tmp file for gnuplot commands: cmd_file=/tmp/mortgage-plotcurves-cmd.$$ # And a bunch of tmp files for massaged data: tmp_prefix=/tmp/mortgage-plotcurves-tmp.$$ # ============== Parse arguments if [ $# -eq 1 ]; then to_file=yes ps_file="$1" echo "${thisprog}: sending output to file '${ps_file}'." elif [ $# -eq 0 ]; then to_file=no else echo "usage: ${thisprog} [outfile]" exit 1 fi # Go to directory with files to be graphed. cd ${datadir} # Make list of those files files="${prefix}*" # ============== Build the command file for gnuplot, preface. # General settings to send output to PostScript file # (if appropriate) and to graph with lines. touch ${cmd_file} chmod 755 ${cmd_file} echo >> ${cmd_file} '#!'${gnuplot} echo >> ${cmd_file} '#' echo >> ${cmd_file} '# Automatically generated by '${thisprog}' on '`date` echo >> ${cmd_file} '' if [ "${to_file}" = "yes" ]; then echo >> ${cmd_file} 'set terminal postscript' echo >> ${cmd_file} 'set output "'${ps_file}'"' fi echo >> ${cmd_file} 'set data style lines' #echo >> ${cmd_file} 'set grid' echo >> ${cmd_file} '' echo >> ${cmd_file} '' # Graph title echo >> ${cmd_file} 'set title "Mortgage repayment, actual and predicted"' # ============== Build the command file for gnuplot, data and curve labels. fnames="" for f in ${files} ; do # massage the data to keep only days-since-epoch date # and convert amount to dollars; also, keep the *last* # "label" field found in each file, and use it for the # curve identifier. filename=`echo ${f} | tr -dc '[A-Za-z0-9]'` fnames="${fnames} ${filename}" < ${f} awk ' { printf("%d %.2f\n", $1 , $3 / 100 ); } ' >> ${tmp_prefix}.${filename} < ${f} awk ' BEGIN { label="original" } { if ( NF > 3 ) { $1 = ""; $2 = ""; $3 = ""; label=$0; } } END { printf("%s",label) } ' >> ${tmp_prefix}.${filename}.label done # ============== Build the command file for gnuplot, axes and ranges. # Figure out x (time) minimum and maximum, and y (amount) maximum (in dollars). set `cat ${files} | awk ' BEGIN { xmin=10000000; xmax=0; ymax=0; } { if ($1 < xmin) { xmin=$1 }; if ($1 > xmax) { xmax=$1 }; if ($3 > ymax) { ymax=$3 }; } END { printf("%.0f %.0f %.0f", xmin, xmax, ymax/100); } '` xmin=$1 xmax=$2 ymax=$3 # Set graph ranges in consequence: echo >> ${cmd_file} 'set xrange ['${xmin}':'${xmax}']' echo >> ${cmd_file} 'set yrange ['0':'${ymax}']' # Graph axes: TRY TO MAKE THIS MORE GENERAL now=`${ndate} -eo` cat >> ${cmd_file} <<'HERE' set format y "%.0f" set ytics 0,10000,150000 set xtics ( \ "1995" 728840 , \ "" 729206 , \ "" 729571 , \ "" 729936 , \ "" 730301 , \ "2000" 730667 , \ "" 731032 , \ "" 731397 , \ "" 731762 , \ "" 732128 , \ "2005" 732493 , \ "" 732858 , \ "" 733223 , \ "" 733589 , \ "" 733954 , \ "2010" 734319 , \ "" 734684 , \ "" 735050 , \ "" 735415 , \ "" 735780 , \ "2015" 736145 , \ "" 736511 , \ "" 736876 , \ "" 737241 , \ "" 737606 , \ "2020" 737972 , \ HERE cat >> ${cmd_file} <> ${cmd_file} '' echo -n >> ${cmd_file} 'plot ' first="yes" for f in ${fnames} ; do if [ "${first}" = "no" ]; then echo >> ${cmd_file} ', \' echo -n >> ${cmd_file} ' ' else first=no fi echo -n >> ${cmd_file} '"'${tmp_prefix}.${f}'" using 1:2 ' echo -n >> ${cmd_file} 'title "' cat >> ${cmd_file} ${tmp_prefix}.${f}.label echo -n >> ${cmd_file} '"' done echo >> ${cmd_file} '' echo >> ${cmd_file} '' # Pause to display result if we're using a terminal if [ "${to_file}" = "no" ]; then echo >> ${cmd_file} 'pause -1 "Press to dismiss plot."' fi # Now that the command file is ready, execute it: ${cmd_file} #rm -f ${cmd_file} rm -f ${tmp_prefix}*