-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagram
314 lines (277 loc) · 7.93 KB
/
diagram
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#! /usr/bin/perl
use warnings;
use strict;
use GBTrains::Schema;
use DateTime::Format::Natural;
use YAML;
my $sch = GBTrains::Schema->connect("dbi:SQLite:dbname=mca.sqlite");
my $conf = YAML::LoadFile($ARGV[0]);
my $dateparse = DateTime::Format::Natural->new(time_zone => 'Europe/London');
my ($from, $to);
if (ref($conf->{date}) eq 'ARRAY') {
($from, $to) = map $dateparse->parse_datetime($_), @{$conf->{date}};
} else {
($from, $to) = $dateparse->parse_datetime_duration($conf->{date});
}
# Axis-rendering code only works in whole hours.
$from->truncate(to => 'hour');
unless (defined $to) {
$to = $from->clone->add(days => 1);
}
$to->truncate(to => 'hour');
my $heading = $from->ymd;
if ($to->ymd ne $from->ymd) {
$heading .= " to " . $to->ymd;
}
# Convert bare TIPLOCs into ones with empty configuration.
for (@{$conf->{tiplocs}}) {
$_ = { $_ => { } } unless ref;
}
# Convert CRS codes into TIPLOCs.
for (@{$conf->{tiplocs}}) {
my $code = (keys(%$_))[0];
if (length($code) == 3) {
my $ti = $sch->resultset("ti")->search({crs_code => $code})->single;
if ($ti) {
$_ = { $ti->tiploc => $_->{$code}};
} else {
warn "couldn't get a TIPLOC for $code\n";
}
}
}
# Separate TIPLOCS into names and configurations.
my @tiploc = map((keys(%$_))[0], @{$conf->{tiplocs}});
my %tiplocconf = map(%$_, @{$conf->{tiplocs}});
sub labelcase ($) {
for (@_) {
s/\b(\w)(\w+)/$1\L$2/g;
return $_;
}
}
# Set default configuration.
for (keys %tiplocconf) {
unless (exists($tiplocconf{$_}{label})) {
my $ti = $sch->resultset("ti")->search({tiploc => $_})->single;
if ($ti) {
$tiplocconf{$_}{label} = labelcase($ti->tps_descr);
} else {
warn "couldn't get a description for $_\n";
}
}
}
my %tiplocmap;
for (my $i = 0; $i < @tiploc; $i++) {
$tiplocmap{$tiploc[$i]} = $i;
}
my $line = 0;
my $lasttime;
sub tweaktime {
my $offset = $from->delta_ms($_[0])->in_halfmins;
$offset = -$offset if $_[0] < $from;
$_[0] = $offset;
}
sub pass {
tweaktime($_[0]);
printf "%d %d %s\n", @_[0,1], $line ? 'cont' : 'start';
printf("%s\n", $_[2]) unless $line;
$line = 1
}
sub pubarrdep {
my ($arr, $dep, $stn, $act) = @_;
tweaktime($arr) if $arr;
tweaktime($dep) if $dep;
my $qual = '';
if (grep /^R$/, @$act) {
$qual = 'req'; # Request stop
}
if ($arr && $dep) {
printf "%d %d %d %d %sarrdep\n", $arr, $stn, $dep, $stn, $qual;
} elsif ($arr) {
printf "%d %d %sarr\n", $arr, $stn, $qual;
} elsif ($dep) {
printf "%d %d %sdep\n", $dep, $stn, $qual;
}
}
sub dep {
tweaktime($_[0]);
printf "%d %d dep\n", @_;
}
sub done {
if ($line) {
printf "finish\n";
}
$line = 0;
undef $lasttime;
}
print <<EOH;
%!PS-Adobe-3.1
%%Creator: diagram
%%DocumentSuppliedResources: procset bjh21-diagram 1 0
%%EndComments
%%BeginResource: procset bjh21-diagram 1 0
/bjh21-diagram
10 dict dup begin
/conv {
18 mul exch
tscale mul exch
} def
/start { gsave conv moveto } def
/cont { conv lineto } def
/adstyle { 1 setlinewidth } def
/arr { gsave newpath conv moveto -3 3 rmoveto
3 0 rlineto 0 -6 rlineto -3 0 rlineto
adstyle stroke grestore} def
/dep { gsave newpath conv moveto 3 3 rmoveto
-3 0 rlineto 0 -6 rlineto 3 0 rlineto
adstyle stroke grestore } def
/arrdep { gsave newpath conv 4 2 roll conv
2 copy moveto 0 3 rmoveto 0 -6 rlineto
4 2 roll 2 copy moveto 0 3 rmoveto 0 -6 rlineto
2 copy moveto 3 3 rmoveto 4 2 roll 2 copy 3 add exch 3 sub exch lineto
moveto -3 -3 rmoveto 3 sub exch 3 add exch lineto
adstyle stroke grestore } def
/reqarrdep { gsave newpath conv 4 2 roll conv
moveto -3 -3 rmoveto 3 3 rlineto -3 3 rlineto
3 -3 rmoveto lineto 3 3 rmoveto -3 -3 rlineto 3 -3 rlineto
adstyle stroke grestore } def
/finish { stroke grestore } def
/heading {
gsave 0 exch conv moveto 0 30 rmoveto
/Helvetica-Bold findfont 20 scalefont setfont
show grestore
} def
/labelright {
gsave 1.5 -3 rmoveto show grestore
} def
/labelup {
gsave currentpoint translate 90 rotate 0 0 moveto labelright grestore
} def
/axes {
3 dict begin
/tiplocs exch def
/hours exch def
% Major time divisions (hours)
gsave 0.5 setlinewidth 0.5 setgray
tiplocs length 1 sub
0 1 hours length 1 sub { % for
dup 120 mul 0 conv moveto dup 120 mul 2 index conv 10 add lineto
hours exch get labelup
} for
stroke
% Minor time divisions (five minutes)
0.1 setlinewidth
0 1 hours length 1 sub 12 mul {
dup 10 mul 0 conv moveto dup 10 mul 2 index conv lineto pop
} for
stroke
pop
% Space divisions
0.5 setlinewidth
0 1 tiplocs length 1 sub
{ dup 0 exch conv moveto dup hours length 1 sub 120 mul exch conv lineto
tiplocs exch get labelright } for
stroke grestore
0 0 conv hours length 1 sub 120 mul tiplocs length 1 sub conv rectclip
end
} def
end /ProcSet defineresource
%%EndResource
%%EndProlog
%%BeginSetup
<< /PageSize [ 841 595 ] >> setpagedevice
/bjh21-diagram /ProcSet findresource begin
%%EndSetup
%%Page 1 1
%%BeginPageSetup
1 setlinewidth
/Helvetica findfont 10 scalefont setfont
36 36 translate
%%EndPageSetup
userdict begin
EOH
sub DateTime::Duration::in_halfmins {
my $self = shift;
my ($m, $s) = $self->in_units('minutes', 'seconds');
return $m * 2 + $s / 30;
}
printf "gsave -18 -18 moveto currentfont 0.5 scalefont setfont
(CIF file ID: %s) show grestore\n",
$sch->resultset('hd')->single->file_id;
my $stdwidth = 72*10;
print "/tscale ", $stdwidth / $from->delta_ms($to)->in_halfmins, " def\n";
printf "(%s) %d heading\n", $heading, scalar(@tiploc);
print "[ ";
for (my $tick = $from->clone;
$tick <= $to;
$tick->add(hours => 1)) {
printf "(%s)", $tick->strftime("%H:%M");
}
print "]\n";
print "[";
for (@tiploc) {
printf "(%s)", $tiplocconf{$_}{label};
}
print "] axes\n";
my $rs;
if (exists $conf->{trains}) {
my @train = @{$conf->{trains}};
$rs = $sch->resultset('bs')->search({unique_id => \@train})
} else {
$rs = $sch->resultset('bs')->search({ 'lis.tiploc' => \@tiploc },
{ join => { crs => 'lis' },
group_by => ['me.lineno'],
having => \ "count(*) > 1"});
}
sub tzstr {
my $off = $_[0]->offset;
use integer;
return sprintf("%+03d%02d", $off / 3600, ($off / 60) % 60);
}
sub debugtime {
printf("%% %10s: %s%s\n", $_[0], $_[1], tzstr($_[1])) if $_[1];
}
while (my $bs = $rs->next) {
for (my $day = $from->clone->truncate(to => 'day')->subtract(days => 1);
$day < $to;
$day->add(days => 1)) {
my $train;
eval {
$train = $bs->instantiate($day);
};
if ($@) {
warn $bs->unique_id . " on " . $day->ymd . ": $@";
}
next unless defined $train;
printf("%% %s (%s):\n", $train->bs->unique_id, $day->ymd);
my $style = "";
$style = "1 0 0 setrgbcolor % VT service"
if $train->bs->atoc_code eq 'VT';
$style = "0 0 1 setrgbcolor % loco-hauled service"
if $train->bs->crs->first()->power_type eq 'D';
$style = "0.75 setgray % bus service"
if grep($train->bs->crs->first()->category eq $_, ('BR', 'BS'));
for my $loc ($train->stops) {
if (exists $tiplocmap{$loc->li->tiploc}) {
no warnings qw/uninitialized/;
printf("%% %-5s %-4s %-5s %-4s %-5s %s (%s)\n",
$loc->li->sched_arr_time, $loc->li->public_arr_time,
$loc->li->sched_pass,
$loc->li->public_dep_time, $loc->li->sched_dep_time,
$loc->li->tiploc, join(", ", @{$loc->li->activity}));
debugtime("sched arr", $loc->sched_arr_time);
debugtime("sched pass", $loc->sched_pass);
debugtime("sched dep", $loc->sched_dep_time);
debugtime("pub arr", $loc->public_arr_time);
debugtime("pub dep", $loc->public_dep_time);
for ($loc->sched_arr_time, $loc->sched_pass,
$loc->sched_dep_time) {
pass($_, $tiplocmap{$loc->li->tiploc}, $style) if $_;
}
pubarrdep($loc->public_arr_time, $loc->public_dep_time,
$tiplocmap{$loc->li->tiploc}, $loc->li->activity);
}
}
done;
}
}
print "showpage end\n%%EOF\n";