Make TcGenDeriv warning-free
[ghc-hetmet.git] / utils / parallel / ps-scale-y.pl
1 #!/usr/local/bin/perl
2 ##############################################################################
3 # Time-stamp: <Wed Jul 24 1996 22:19:02 Stardate: [-31]7859.44 hwloidl>
4 #
5 # Usage: ps-scale-y [options] <file>
6 #
7 # It is assumed that the last line of <file> is of the format:
8 #                            %% y_scaling: <f> max: <n>
9 # where <f> is a floating point number determining the amount of scaling of 
10 # the y-axis of the graph that is necessary. <n> is the real maximal number
11 # of tasks in the program (needed to rebuild y-axis). This script replaces the 
12 # definitions of the PostScript functions scale-y and unscale-y in <file> by 
13 # new definitions that do the right amount of scaling. 
14 # The y-axis is rebuilt (using the above maximal number of tasks and a copy 
15 # of the print_y_axis routine from qp2ps).
16 # If the above line doesn't exist, <file> is unchanged.
17 # This script is typically called from gr2ps.
18 #
19 ##############################################################################
20
21 require "getopts.pl";
22
23 &Getopts('hv');  
24
25 do process_options();
26
27 $tmpfile = ",t";
28 $debug = 0;
29
30 # NB: This must be the same as in qp2ps!!
31
32 $xmin = 100;
33 $xmax = 790;
34
35 $scalex = $xmin;
36 $labelx = $scalex - 45;
37 $markx =  $scalex - 30;
38 $major = $scalex - 5;
39 $majorticks = 10;
40
41 $mmax = 1;
42
43 $amax = 0;
44 $ymin = 50;
45 $ymax = 500;
46
47 # E
48 open (GET_SCALING,"cat $file | tail -1 |") || die "Can't open pipe:  $file | tail -1 |\n";
49
50 $y_scaling = 1.0;
51
52 while (<GET_SCALING>){
53     # print STDERR $_;
54     if (/^\%\%\s+y_scaling:\s+([0-9\.]+)\s+max:\s+(\d+)/) {
55         $y_scaling = $1;
56         $pmax = $2;
57         $y_translate = 1.0 - $y_scaling;
58     }
59 }
60 close (GET_SCALING);
61
62 if ( $y_scaling != 1.0 ) {
63     print STDERR "Scaling $file ($y_scaling; $pmax tasks) ...\n" if $opt_v;
64     # print STDERR "SCALING NECESSARY: y_scaling = $y_scaling; y_translate = $y_translate !\n";
65 } else {
66     # No scaling necessary!!
67     exit 0;
68 }
69
70
71 open (IN,"<$file") || die "Can't open file $file\n";
72 open (OUT,">$tmpfile") || die "Can't open file $tmpfile\n";
73
74 $skip = 0;
75 while (<IN>) {
76     $skip = 0 if $skip && /^% End Y-Axis.$/;
77     next if $skip;
78     if (/\/scale\-y/) { 
79         print OUT "/scale-y { gsave\n" .
80                   "           0 50 $y_translate mul translate\n" .
81                   "           1 $y_scaling scale } def\n";
82     }
83     elsif (/\/unscale\-y/) {  
84         print OUT "/unscale-y { grestore } def \n";
85     } else {
86         print OUT $_;
87     }
88     if (/^% Y-Axis:$/) {
89         $skip = 1;
90         do print_y_axis();
91     }
92 }
93
94 close (IN);
95 close (OUT);
96
97 rename($tmpfile,$file);
98
99 exit 0;
100
101 # ###########################################################################
102 # Same as in qp2ps (but printing to OUT)!
103 # ###########################################################################
104
105 sub print_y_axis {
106     local ($i);
107     local ($y, $smax,$majormax, $majorint);
108
109 # Y-axis label
110
111     print OUT  "% " . ("-" x 75) . "\n";
112     print OUT  "% Y-Axis (scaled):\n";
113     print OUT  "% " . ("-" x 75) . "\n";
114
115     print OUT ("%scale-y  % y-axis outside scaled area if ps-scale-y rebuilds it!\n");
116
117     print OUT ("gsave\n");
118     print OUT ("HE12 setfont\n");
119     print OUT ("(tasks)\n");
120     print OUT ("dup stringwidth pop\n");
121     print OUT ("$ymax\n");
122     print OUT ("exch sub\n");
123     print OUT ("$labelx exch\n");
124     print OUT ("translate\n");
125     print OUT ("90 rotate\n");
126     print OUT ("0 0 moveto\n");
127     print OUT ("show\n");
128     print OUT ("grestore\n");
129
130 # Scale
131
132     if ($pmax < $majorticks) {
133         $majorticks = $pmax;
134     }
135
136     print OUT ("HE12 setfont\n$scalex $ymin moveto\n$scalex $ymax lineto\n");
137     print OUT ("% Max number of tasks: $pmax\n");
138     print OUT ("% Number of ticks: $majorticks\n");
139
140     print OUT  "0.5 setlinewidth\n";
141
142     $y = $ymax; # (($pmax - $ymin)/$majorticks) * ($majorticks-$i) + $ymin;
143     print OUT ("$scalex $y moveto\n$major $y lineto\n");
144     print OUT ("$markx $y moveto\n($pmax) show\n");
145
146     $majormax = int($pmax/$majorticks)*$majorticks;
147     $smax = $majormax*(($ymax-$ymin)/$pmax)+$ymin;
148     $majorint = $majormax/$majorticks;
149
150     for($i=1; $i <= $majorticks; ++$i) {
151         $y = (($smax - $ymin)/$majorticks) * ($majorticks-$i) + $ymin;
152         $majorval = int($majorint * ($majormax/$majorint-$i));
153         print OUT ("$scalex $y moveto\n$major $y lineto\n");
154         print OUT ("$markx $y moveto\n($majorval) show\n");
155     }
156
157     # print OUT ("$xmin $ymax moveto\n10 0 rlineto\n10 0 rmoveto\n($pmax) show\n");
158     print OUT  " stroke\n";
159     print OUT  "1 setlinewidth\n";
160     print OUT ("%unscale-y\n");
161     print OUT ("% End Y-Axis (scaled).\n");
162     print OUT "% " . ("-" x 75) . "\n";
163 }
164
165 # ----------------------------------------------------------------------------
166
167 sub process_options {
168
169     if ( $opt_h ) {                      
170         open(ME,$0) || die "Can't open myself ($0): $!\n";
171         $n = 0;
172         while (<ME>) {
173             last if $_ =~ /^$/;
174             print $_;
175             $n++;
176         }
177         close(ME);
178         exit ;
179     }
180     
181     if ( $#ARGV != 0 ) {
182         print "Usage: $0 [options] <file>\n";
183         print "Use -h option to get details\n";
184         exit 1;
185     }
186
187     $file = $ARGV[0];
188 }