chiark / gitweb /
can fit 139 lines on an atp -B page
[trains.git] / hostside / analyse-speeds
1 #!/bin/bash
2 usage () { cat <<END >&2; exit 1 \
3
4 usage: ./speeds-analyse <train>
5   reads: <train>-*.speeds
6   writes: <train>.speeds.ps
7   writes: <train>.speeds.record
8 END
9 }
10
11 set -e
12
13 test $# = 1 || usage
14 case "$1" in -*) usage ;; esac
15
16 train=$1; shift
17
18 o=$train.speeds
19
20 >$o.all.tmp
21
22 acceltime=100000
23 deceltime=30000
24
25 exec 3>$o.stopping.tmp
26 stops-from () {
27         printf >&3 '%d %d %s\n' $1 $(( $2 )) $3
28 }
29 . ../layout/ours.redacted.shellvars
30 . $train.manual
31 exec 3>&-
32
33 for f in address length nondetfore nondetrear acceltime deceltime; do
34 #                  [mm]   [mm]          [mm]    [ms]        [ms]
35         eval "v=\$$f"
36         if [ "x$v" = x ]; then echo >&2 "missing $train $f"; exit 1; fi
37 done
38
39 for f in $train-*.speeds; do
40         this="'$f'"
41         if head -1 $f | grep '^[A-Za-z]' >/dev/null; then
42                 this="$this using 2:3"
43         fi
44         each="$each, $this"
45         perl -pe 's/^[A-Za-z]\w*//' $f >>$o.all.tmp
46 done
47
48 gnuplot <<END
49 set pointsize 0.3
50 set terminal postscript
51 set key top left
52 set xrange [0:127]
53 set output '$o.ps.tmp'
54 set ylabel "speed [m/s]"
55 set xlabel "nmra speed step 1..126"
56 plot '$o.all.tmp' smooth sbezier notitle  $each
57 END
58
59 ed -s $o.ps.tmp <<END >/dev/null
60 /^%%Title/
61 s/\.tmp$//
62 w
63 END
64
65 max=`
66     sort -rnu $train.speeds.all.tmp |head -1 |awk '{print $1}'
67     case "${PIPESTATUS[*]}" in "0 0 0" | "141 0 0");;
68         *) echo >&2 "? ${PIPESTATUS[*]}"; exit 127;; esac
69 `
70
71 gnuplot <<END
72 set terminal table
73 set samples $max
74 set xrange [1:$max]
75 unset autoscale x
76 set output '$o.table.tmp'
77 plot '$o.all.tmp' smooth sbezier
78 END
79
80 sort -rn <$o.stopping.tmp >$o.stopping-sorted.tmp
81
82 perl <$o.stopping-sorted.tmp >$o.record.tmp -ne '
83  BEGIN {
84         $last_step= 127;
85         $last_stoptime= undef;
86
87         print   "train '$train' is '$address'".
88                 " '$nondetfore'".
89                 "+'$(( $length - $nondetfore - $nondetrear ))'".
90                 "+'$nondetrear'\n"
91                         or die $!;
92  }
93         die unless m/^(\d+)\s+(\d+)(?:\s+([-.0-9]+))?\s*$/;
94         ($step,$stopdist,$stoptime) = ($1,$2,$3);
95         die unless $step < $last_step;
96         if (defined $stoptime) {
97                 $stoptime= eval $stoptime; defined $stoptime or die "$@ ?";
98         } else {
99                 die unless $last_stoptime;
100                 $stoptime= $last_stoptime;
101         }
102         printf "train '$train' stops %3d: %4d %5d\n", $step,$stopdist,
103                 $stoptime*1000
104                         or die $!;
105         $last_step= $step;
106         $last_stoptime= $stoptime;
107 '
108
109 perl <$o.table.tmp >>$o.record.tmp -ne '
110  BEGIN {
111         use IO::Handle;
112         $nxt= 1;
113         $max= 0;
114         $speed[0]= 0;
115  }
116         next if m/^\#/; next unless m/\S/;
117         die unless m/^\s*(\d+)\s+([0-9.]+)\s/;
118         ($step,$speed) = ($1,$2);
119         die "$step $nxt" unless $step==$nxt;
120         if ($speed < $max) { push @decreases, $step; $speed= $max; }
121         $speed[$step]= $speed;
122         $max= $speed;
123         $nxt++;
124  END {
125         die "'$train': decreases at steps @decreases\n"
126                 if @decreases;
127         $steps= $nxt-1;
128         die "'$train': expected 126 steps, found $steps\n"
129                 unless $steps==126;
130         for ($step=1; $step<$nxt; $step++) {
131                 printf  "train %s step %d=%f\n",
132                         "'$train'", $step, $speed[$step]
133                                 or die $!;
134         }
135         STDOUT->error and die $!;
136         print "end\n" or die $!;
137  }
138 '
139
140 rm $o.{all,stopping,stopping-sorted}.tmp
141
142 for f in ps record; do
143         mv -f $o.$f.tmp $o.$f
144 done