#!/bin/sh # # @(#)mortgage-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=plotcurves PATH=/local/bin:/bin:/usr/bin export PATH # Path to gnuplot gnuplot=/local/bin/gnuplot # Directory containing files of data datadir=/anne/accounting/misc/mortgage-data # Prefix name of files containing data to be plotted: prefix=curve. # 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. # General settings to send output to PostScript printer # and to graph with points connected by 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} '' echo >> ${cmd_file} '' # Graph title echo >> ${cmd_file} 'set title "Mortgage repayment, actual and predicted"' # Graph axes: 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 ) set xrange [728854:737972] set yrange [0:105775.35] HERE # For each file to be graphed, must set a title, create the # x-axis labels based on field 1 of the data file, and invoke # the plotting command. 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}" cat $f | awk ' { printf("%d %.2f\n", $1 , $3 / 100 ); } ' >> ${tmp_prefix}.${filename} cat $f | awk ' BEGIN { label="original" } { if ( NF > 3 ) { $1 = ""; $2 = ""; $3 = ""; label=$0; } } END { printf("%s",label) } ' >> ${tmp_prefix}.${filename}.label done # commands to plot the massaged data with its labels echo >> ${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}*