-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclassHobyah.py
executable file
·4335 lines (4061 loc) · 221 KB
/
classHobyah.py
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! python3
#
# Copyright 2020-2024, Ewan Bennett
#
# All rights reserved.
#
# Released under the BSD 2-clause licence (SPDX identifier: BSD-2-Clause)
#
# email: [email protected]
#
# A class for manipulating data from SI binary files generated by Hobyah.
#
# The class has methods for the following:
# * getting transient data at one point in the tunnels (GetTransientData).
#
# * getting fixed data along a route at one time (GetFixedRouteData).
#
# * getting fan curve data and system characteristic data (GetFanDataTransient).
#
# * printing the contents of the variables in the binary file in a
# way that makes sense to human eyes (PrettyClassPrint).
#
# Various other routines exist to do ancillary stuff like checking that
# the location to plot at is valid.
import sys
import os
import math
import re # regular expressions
import generics as gen # general routines
import UScustomary as USc # imperial to metric conversion
import pickle
import itertools
try:
import numpy as np
except ModuleNotFoundError:
print("> Ugh, can't process this run because Python's\n"
'> "numpy" library is not installed on this computer.\n'
'> Please get your local IT guru to install it, then\n'
'> try again. If you are your IT guru, good luck!\n'
)
if __name__ == "__main__":
# Only call sys.exit() if we are running it directly (unlikely).
sys.exit()
try:
import pandas as pd
except ModuleNotFoundError:
print("> Ugh, can't process this run because Python's\n"
'> "pandas" library is not installed on this computer.\n'
'> Please get your local IT guru to install it, then\n'
'> try again. If you are your IT guru, good luck!\n'
)
if __name__ == "__main__":
sys.exit()
class Hobyahdata:
'''A class for holding the fixed and transient data from a Hobyah
run. All the fixed properties of the segments, subsegments,
nodes, routes, fires, fans, jet fans, trains etc.
It is fed the path and name of a binary file, opens it and
returns its data. Various errors are written to the screen
and to a logfile, because this class is used in programs that
have a logfile already open.'''
# Initialize the data.
def __init__(self, file_path, file_name, log, complain = True,
line_number = 1, line_text = "no line of input"):
'''Take a folder name, a file name, an already open log file
and a couple of arguments that can be used in error messages.
Load the contents into dictionaries and pandas dataframes.
Parameters:
file_path str The name of a folder
file_name str The name of a .hbn file
log handle The handle of a log file.
complain bool If False, don't complain if this is an
SES .sbn file. If True, do complain.
line_number int The line number that was read that
prompted this call to open the file
(used in error messages).
line_text str The text of the line being read that
prompted this call to open the file
(used in error messages).
Returns:
HobyahData class All the fixed and transient data
in the .hbn file if we successfully
read the file. None if an error
occurred.
'''
# Set the file path. We do some cleaning up and add a trailing
# slash if needed (I keep forgetting it when running in the
# interpreter).
if file_path == '':
# Set the current directory as the file path.
self.file_path = os.getcwd()
else:
self.file_path = file_path
if len(self.file_path) > 1 and self.file_path[-1] not in "/\\":
# Add a trailing directory separator
self.file_path = self.file_path + '/'
self.file_name = file_name
self.log = log
# Read the fixed and transient data from the file. The routine
# returns a value identifying if this file was read successfully or not.
# (None if faulty, True if OK).
self.success = self._ReadHobyahData(complain)
if self.success is None:
return(None)
else:
# Make a dictionary of the settable options options. The result is
# a description that can be used in error messages. These are
# all settings from form 1C or form 1G, now held in
# self.settings_dict.
self.opt_descrip = {"version": "version (in the settings block)",
}
# Make a dictionary of requirements and data for each plot type.
# These include the version numbers of Hobyah that can plot them,
# the options that must be set in order for the property to be
# available in the output, and a definition of where the property
# is plotted.
# This also records if they are signed (i.e. they need to be
# multiplied by -1 if the tunnels are back-to-front in routes).
# Temperatures are not signed, air velocities and volume
# flows are.
self.properties = {
"elevations":{"versions": (1,),
"solver": ("moc2",),
"place": "not used",
"descrip": "elevations",
"conversion": "dist1",
"signed": False,
"transient": False,
"curve_types": ("profile",),
"complexity": "simple",
"var_name": self.routes_dict,
"x_list": "elevgrad_chs",
"y_list": "elevations",
},
"gradients":{"versions": (1,),
"solver": ("moc2",),
"place": "not used",
"descrip": "gradients",
"conversion": "dist1",
"signed": False,
"transient": False,
"curve_types": ("profile",),
"complexity": "double_up1",
"var_name": self.routes_dict,
"x_list": "elevgrad_chs",
"y_list": "gradients2",
},
"speedlimits":{"versions": (1,),
"solver": ("moc2",),
"place": "not used",
"descrip": "speed limits",
"conversion": "speed1",
"signed": False,
"transient": False,
"curve_types": ("profile",),
"complexity": "double_up1",
"var_name": self.routes_dict,
"x_list": "speed_chs",
"y_list": "speed_plots",
},
"lanes": {"versions": (1,),
"solver": ("moc2",),
"place": "not used",
"descrip": "speed limits",
"conversion": "speed1",
"signed": False,
"transient": False,
"curve_types": ("profile",),
"complexity": "double_up1",
"var_name": self.routes_dict,
"x_list": "lane_chs",
"y_list": "lane_plots",
},
# This is track radius, which is not used in Hobyah.
# It is written to SES routes in form 8C and is used
# in the SES train performance calculation (curved
# track adds a small amount of extra resistance due to
# the wheels grinding on the curved track). We include
# it as a plot to ensure that the data we pass to SES
# is not corrupted.
"radius": {"versions": (1,),
"solver": ("moc2",),
"place": "not used",
"descrip": "track radius",
"conversion": "dist1",
"signed": False,
"transient": False,
"curve_types": ("profile",),
"complexity": "double_up1",
"var_name": self.routes_dict,
"x_list": "radii_chs",
"y_list": "radii_plots",
},
# This assigns energy sectors for traction power
# performance calculations to different sections of
# track. It is is not used in Hobyah, only in SES.
# Hobyah writes this data to SES form 8C if there are
# "SESdata" blocks in the Hobyah input file.
"sectors": {"versions": (1,),
"solver": ("moc2",),
"place": "not used",
"descrip": "track sectors",
"conversion": "null",
"signed": False,
"transient": False,
"curve_types": ("profile",),
"complexity": "double_up1",
"var_name": self.routes_dict,
"x_list": "sectors_chs",
"y_list": "sectors_plots",
},
# This is a coasting allowed/coasting forbidden switch
# used in the traction power performance calculations.
# It is is not used in Hobyah, only in SES.
# Hobyah writes this data to SES form 8C if there are
# "SESdata" blocks in the Hobyah input file.
"coasting": {"versions": (1,),
"solver": ("moc2",),
"place": "not used",
"descrip": "coasting rule",
"conversion": "null",
"signed": False,
"transient": False,
"curve_types": ("profile",),
"complexity": "double_up1",
"var_name": self.routes_dict,
"x_list": "coasting_chs",
"y_list": "coasting_plots",
},
# This is a location-specific regenerative braking factor
# used in the traction power performance calculations.
# It is is not used in Hobyah, only in SVS. Not SES;
# just SVS.
# Hobyah writes this data to SES form 8C if there are
# "SESdata" blocks in the Hobyah input file and the
# SES input file type is "SVS".
"regenfrac":{"versions": (1,),
"solver": ("moc2",),
"place": "not used",
"descrip": "regen braking fraction",
"conversion": "null",
"signed": False,
"transient": False,
"curve_types": ("profile",),
"complexity": "double_up1",
"var_name": self.routes_dict,
"x_list": "regenfractions_chs",
"y_list": "regenfractions_plots",
},
"area": {"versions": (1,),
"solver": ("moc2",),
"place": 0, # The index to area in segments_consts
"descrip": "tunnel area",
"conversion": "area",
"signed": False,
"transient": False,
"curve_types": ("profile",),
"complexity": "double_up1",
"var_name": "in segments",
"open_air": 0.0 # The value to plot if outside the tunnel
},
"perimeter":{"versions": (1,),
"solver": ("moc2",),
"place": 1, # The index to perimeter in segments_consts
"descrip": "tunnel perimeter",
"conversion": "dist1",
"signed": False,
"transient": False,
"curve_types": ("profile",),
"complexity": "double_up1",
"var_name": "in segments",
"open_air": 0.0
},
"d_h": {"versions": (1,),
"solver": ("moc2",),
"place": 2,
"descrip": "hydraulic diameter",
"conversion": "dist1",
"signed": False,
"transient": False,
"curve_types": ("profile",),
"complexity": "double_up1",
"var_name": "in segments",
"open_air": 0.0
},
"roughness":{"versions": (1,),
"solver": ("moc2",),
"place": 3,
"descrip": "tunnel roughness",
"conversion": "dist1",
"signed": False,
"transient": False,
"curve_types": ("profile",),
"complexity": "double_up1",
"var_name": "in segments",
"open_air": 0.0
},
"fanning": {"versions": (1,),
"solver": ("moc2",),
"place": 3,
"descrip": "fanning friction factor",
"conversion": "dist1",
"signed": False,
"transient": False,
"curve_types": ("profile",),
"complexity": "double_up1",
"var_name": "in segments",
"open_air": 0.0
},
"darcy": {"versions": (1,),
"solver": ("moc2",),
"place": 3,
"descrip": "darcy friction factor",
"conversion": "dist1",
"signed": False,
"transient": False,
"curve_types": ("profile",),
"complexity": "double_up1",
"var_name": "in segments",
"open_air": 0.0
},
"atkinson": {"versions": (1,),
"solver": ("moc2",),
"place": 3,
"descrip": "atkinson friction factor",
"conversion": "dist1",
"signed": False,
"transient": False,
"curve_types": ("profile"),
"complexity": "double_up1",
"var_name": "in segments",
"open_air": 0.0
},
"celerity": {"versions": (1,),
"solver": ("moc2",),
"place": "segment",
"descrip": "speed of sound",
"conversion": "speed1",
"signed": False,
"transient": True,
"curve_types": ("transient", "profile"),
"var_name": self.c_bin,
"open_air": self.settings_dict["c_atm"], # The value to plot if outside the tunnel
},
"velocity": {"versions": (1,),
"solver": ("moc2",),
"place": "segment",
"descrip": "air velocity",
"conversion": "speed1",
"signed": True,
"transient": True,
"curve_types": ("transient", "profile"),
"var_name": self.v_bin,
"open_air": 0.0, # The value to plot if outside the tunnel
},
"density": {"versions": (1,),
"solver": ("moc2",),
"place": "segment",
"descrip": "air density",
"conversion": "dens1",
"signed": False,
"transient": True,
"curve_types": ("transient", "profile"),
"var_name": self.dens_bin,
"open_air": self.settings_dict["rho_atm"], # The value to plot if outside the tunnel
},
"pstat": {"versions": (1,),
"solver": ("moc2",),
"place": "segment",
"descrip": "static pressure",
"conversion": "press1",
"signed": False,
"transient": True,
"curve_types": ("transient", "profile"),
"var_name": self.p_stat_bin,
"open_air": 0.0
},
"ptot": {"versions": (1,),
"solver": ("moc2",),
"place": "segment",
"descrip": "total pressure",
"conversion": "press1",
"signed": False,
"transient": True,
"curve_types": ("transient", "profile"),
"var_name": self.p_tot_bin,
"open_air": 0.0
},
"pstatabs": {"versions": (1,),
"solver": ("moc2",),
"place": "segment",
"descrip": "absolute static pressure",
"conversion": "press1",
"signed": False,
"transient": True,
"curve_types": ("transient", "profile"),
"var_name": self.p_statabs_bin,
"open_air": self.settings_dict["p_atm"]
},
"ptotabs": {"versions": (1,),
"solver": ("moc2",),
"place": "segment",
"descrip": "absolute total pressure",
"conversion": "press1",
"signed": False,
"transient": True,
"curve_types": ("transient", "profile"),
"var_name": self.p_totabs_bin,
"open_air": self.settings_dict["p_atm"]
},
"volflow": {"versions": (1,),
"solver": ("moc2",),
"place": "segment",
"descrip": "volume flow",
"conversion": "volflow",
"signed": True,
"transient": True,
"curve_types": ("transient", "profile"),
"var_name": self.q_bin,
"open_air": 0.0,
},
"massflow": {"versions": (1,),
"solver": ("moc2",),
"place": "segment",
"descrip": "mass flow",
"conversion": "massflow",
"signed": True,
"transient": True,
"curve_types": ("transient", "profile"),
"var_name": self.m_bin,
"open_air": 0.0,
},
"system": {"versions": (1,),
"solver": ("moc2",),
"place": ("fan1", "fan2",),
"descrip": "system characteristic",
"conversion": "press2",
"Xconversion": "volflow",
"signed": False,
"transient": True,
"curve_types": ("fandata",),
"var_name": None,
},
"dutypoint":{"versions": (1,),
"solver": ("moc2",),
"place": ("fan1", "fan2",),
"descrip": "fan duty point",
"conversion": "press2",
"Xconversion": "volflow",
"signed": False,
"transient": True,
"curve_types": ("fandata",),
"var_name": None,
},
"fanchar": {"versions": (1,),
"solver": ("moc2",),
"place": ("fan1", "fan2",),
"descrip": "fan characteristic",
"conversion": "press2",
"Xconversion": "volflow",
"signed": False,
"transient": True,
"curve_types": ("fandata",),
"var_name": None,
},
"system-cursed": {"versions": (1,),
"solver": ("moc2",),
"place": ("fan1", "fan2",),
"descrip": "system characteristic",
"conversion": "press2",
"Xconversion": "volflow",
"signed": False,
"transient": True,
"curve_types": ("fandata",),
"var_name": None,
},
"fanchar-cursed": {"versions": (1,),
"solver": ("moc2",),
"place": ("fan1", "fan2",),
"descrip": "fan characteristic", # static pressure
"conversion": "press2",
"Xconversion": "volflow",
"signed": False,
"transient": True,
"curve_types": ("fandata",),
"var_name": None,
},
"area_d": {"versions": (1,),
"solver": ("moc2",),
"place": ("damper1",),
"descrip": "area",
"conversion": "area",
"signed": False,
"transient": True,
"curve_types": ("transient",),
"var_name": self.areas_bin,
},
"zeta_bf": {"versions": (1,),
"solver": ("moc2",),
"place": ("damper1",),
"descrip": "k-factor (+ve flow)",
"conversion": "null",
"signed": False,
"transient": True,
"curve_types": ("transient",),
"var_name": self.zetas_bf_bin,
},
"zeta_fb": {"versions": (1,),
"solver": ("moc2",),
"place": ("damper1",),
"descrip": "k-factor (-ve flow)",
"conversion": "null",
"signed": False,
"transient": True,
"curve_types": ("transient",),
"var_name": self.zetas_fb_bin,
},
"r_bf": {"versions": (1,),
"solver": ("moc2",),
"place": ("damper1", "damper2",),
"descrip": "resistance (+ve flow)",
"conversion": "atk",
"signed": False,
"transient": True,
"curve_types": ("transient",),
"var_name": self.Rs_bf_bin,
},
"r_fb": {"versions": (1,),
"solver": ("moc2",),
"place": ("damper1", "damper2", ),
"descrip": "resistance (-ve flow)",
"conversion": "atk",
"signed": False,
"transient": True,
"curve_types": ("transient",),
"var_name": self.Rs_fb_bin,
},
# This next is the change in static pressure across
# fans and dampers. The area on each side is the
# same, so the velocities on each side are similar
# There are negligible velocity differences across
# dampers and low changes across tunnel ventilation
# fans (which are usually <4 kPa fan total pressure
# rise). The change in velocity across fans is low
# enough that the change in fan static pressure rise
# can be treated as the change in fan total pressure
# rise too. This would no longer be the case if
# someone used the program to model blowers instead
# of fans, or dampers with very high pressure drops.
# But in systems like that, the change in temperature
# becomes an issue and a fully non-homentropic code
# should probably be used.
"pdiff": {"versions": (1,),
"solver": ("moc2",),
"place": ("damper1", "damper2", "tunnelfan"),
"descrip": "pressure change",
"conversion": "press1",
"signed": True,
"transient": True,
"curve_types": ("transient",),
"var_name": self.p_diff_dict,
},
# These are skeleton properties intrinsic to trains.
# "speed": {"versions": (1,),
# "solver": ("moc2",),
# "place": "train",
# "descrip": "speed of",
# "conversion": "speed2", # km/h and mph
# "signed": False,
# "transient": True,
# "curve_types": ("transient",),
# "var_name": self.tr_speeds_bin,
# },
# "speedms": {"versions": (1,),
# "solver": ("moc2",),
# "place": "train",
# "descrip": "speed of",
# "conversion": "speed1", # m/s and fpm
# "signed": False,
# "transient": True,
# "curve_types": ("transient",),
# "var_name": self.tr_speeds_bin,
# },
# "accel": {"versions": (1,),
# "solver": ("moc2",),
# "place": "train",
# "descrip": "acceleration of",
# "conversion": "accel",
# "signed": False,
# "transient": True,
# "curve_types": ("transient",),
# "var_name": self.tr_accels_bin,
# },
# "down_ch": {"versions": (1,),
# "solver": ("moc2",),
# "place": "train",
# "descrip": "location of the down end of",
# "conversion": "dist1",
# "signed": False,
# "transient": True,
# "curve_types": ("transient",),
# "var_name": self.tr_down_bin,
# },
# "up_ch": {"versions": (1,),
# "solver": ("moc2",),
# "place": "train",
# "descrip": "location of the up end of",
# "conversion": "dist1",
# "signed": False,
# "transient": True,
# "curve_types": ("transient",),
# "var_name": self.tr_up_bin,
# },
# These skeleton properties for traffic in segments.
# If a file has a "traffictypes" block with traffic
# names in it, "_flow" or "dens" are appended to each
# name and the dictionary below is copied as a new
# entry with that name as the key. If your file has
# a vehicle type named "HGV" the keys become
# "HGV_flow" and "HGV_dens".
# We put a "#" in these keys so that they can't be
# directly accessed in Hobyah plots.
"#flow": {"versions": (1,),
"solver": ("moc2",),
"place": "route",
"descrip": " flowrate (veh/hr)",
"conversion": "null",
"signed": False,
"transient": True,
"curve_types": ("transient", "profile",),
"var_name": "#fixed vehicle stuff",
},
"#dens": {"versions": (1,),
"solver": ("moc2",),
"place": "route",
"descrip": " density (veh/km)",
"conversion": "trafdens",
"signed": False,
"transient": True,
"curve_types": ("transient", "profile",),
"var_name": "#fixed vehicle stuff",
},
"tot_flow": {"versions": (1,),
"solver": ("moc2",),
"place": "route",
"descrip": "total flowrate (veh/hr)",
"conversion": "null",
"signed": False,
"transient": True,
"curve_types": ("transient", "profile",),
"var_name": "#fixed vehicle stuff",
},
"tot_dens": {"versions": (1,),
"solver": ("moc2",),
"place": "route",
"descrip": "total density (veh/km)",
"conversion": "trafdens",
"signed": False,
"transient": True,
"curve_types": ("transient", "profile",),
"var_name": "#fixed vehicle stuff",
},
# Add a fake property that triggers error 7042.
"_t7042": {"versions": (1,),
"solver": ("moc4",),
"place": ("segment", ),
"descrip": "fake property to cause an error",
"conversion": "null",
"signed": False,
"transient": True,
"curve_types": ("transient", "profile", ),
"var_name": self.Rs_fb_bin,
},
}
# Now make a list of things that we may want negative versions
# of: the flows, velocities and gauge pressures (no point doing
# it for absolute pressures).
# These are all multiplied by -1 when the values are generated.
rev_props = ["-massflow", "-pdiff", "-pstat", "-ptot",
"-velocity", "-volflow",]
# Now add entries for the reversed properties.
for new_key in rev_props:
# Prepend a negative sign to the description and set
# an entry for the negated property.
vector_prop = self.properties[new_key[1:]].copy()
descrip = vector_prop["descrip"]
vector_prop.__setitem__("descrip", "-" + descrip)
self.properties.__setitem__(new_key, vector_prop)
# Create traffic properties for each type of vehicle in
# the file and store the names in a list of keywords that
# access traffic properties. Also create a list of the
# names of the traffic.
self.traffic_prop = ["tot_flow", "tot_dens"]
self.veh_types = []
flow_dict = self.properties["#flow"]
dens_dict = self.properties["#dens"]
for key in self.vehcalc_dict.keys():
self.veh_types.append(key)
flow_dict.__setitem__("descrip", key + " flowrate (veh/hr)")
dens_dict.__setitem__("descrip", key + " density (veh/km)")
name = key + "_flow"
self.properties.__setitem__(name, flow_dict.copy())
self.traffic_prop.append(name)
name = key + "_dens"
self.properties.__setitem__(name, dens_dict.copy())
self.traffic_prop.append(name)
# Build two lists of the keys to properties. The first is
# just a list of them. The second is a list with the test
# entry (or entries) removed (test entries start with an
# underscore) and the Atkinson resistance plot properties
# changed from e.g., "r_bf" to "R_bf" (it just seems weird
# to be using a lowercase 'r' in a property that most people
# using this will be entirely unfamiliar with and, when they
# look it up in mine ventilation papers, will see 'R'. The
# list is used in error messages.
self.availables = list(self.properties.keys())
self.availables.sort()
# Remove hidden properties like "_t7042" and "#dens" and
# ignore the reversed properties (we want them at the end).
interim = [entry for entry in self.availables if
entry[0] not in ('_', '#', '-')]
# Capitalise the Atkinson resistance terms.
# self.plottables = [entry.capitalize() if entry[:2] == 'r_'
# else entry
# for entry in interim]
self.plottables = interim
# Add the names of the reversed properties at the end.
self.availables.extend(rev_props)
self.plottables.extend(rev_props)
# Make a list of transient properties that apply at dampers.
self.damper_props = []
# Make a list of the properties that apply at fans.
self.fan_props = []
# Make a list of the properties that apply at trains and are
# intrinsic to trains.
self.train1_props = []
for plot_type in self.plottables:
# Filter out the properties in which "place" is an index
# in segments_source.
place = self.properties[plot_type.lower()]["place"]
if type(place) is tuple or type(place) is str:
if "damper1" in place:
self.damper_props.append(plot_type)
if "tunnelfan" in place:
self.fan_props.append(plot_type)
if "train" in place:
self.train1_props.append(plot_type)
# Make a list of properties that apply at dampers, fans
# and trains.
self.damfantrain_props = (self.damper_props + self.fan_props
+ self.train1_props)
def _ReadHobyahData(self, complain):
'''Read the fixed and transient data in the pickle file and return
the data in it. Generate lists, dictionaries and pandas
dataframes of the runtime data. There are too many to list
and constantly changing, so see the code below.
Parameters:
self class Everything in the class.
complain bool If False, don't complain if this is an
SES .sbn file. If True, do complain.
Returns:
None if an error occurred, True if all was well.
Errors:
Aborts with 7001 if the filename is empty (can only happen
called directly in an IDE).
Aborts with 7002 if the first pickled item we read was not
a string (it should be a version string along the lines of
"Hobyah.py binary version 3" or "Hobyah.py binary version 42").
Aborts with 7003 if the first pickled item we read was a
string but started with "SESconv.py binary version ".
Aborts with 7004 if the first pickled item we read was a
string but didn't start with "Hobyah.py binary version " and
end with an integer.
Aborts with 7005 if the binary file's version was too low for
this version of the class to process.
Aborts with 7006 if the binary file's version was too high.
Aborts with 7007 if the binary file doesn't exist.
Aborts with 7008 if we don't have permission to read the
binary file.
'''
# Set the binary file version number that this version of the
# class can process. We increase the number at the end each
# time we add something to the file that breaks the backwards
# compatibility with earlier binary files.
if self.file_name == "":
# The name of the binary file is empty. Complain and return.
err = ("> The name of the binary file is blank. The path is\n"
'> "' + self.file_path + '".'
)
gen.WriteError(7001, err, self.log)
return(None)
try:
with open(self.file_path + self.file_name,'rb') as pkl:
# Read the binary file version string and check it is
# not too high and not too low.
result = self._UnPickleData("the binary file version", 0, pkl)
if result is None:
# We fouled up somehow.
return(None)
else:
self.binversion_string = result
# Now check the binary file version string. We want it to
# be a string like "Hobyah.py binary version 3". First
# we complain if it is not a string, then check its contents
# then we check for lower versions (no good) and higher
# versions (also no good).
if type(self.binversion_string) is not str:
# First get a suitable slice of whatever we have here,
# it might be helpful in tracing the error. We turn
# it into a string and take the first 60 characters.
text = str(self.binversion_string).lstrip().rstrip()
if len(text) > 60:
text = text[:57] + "..."
class_text = str(type(self.binversion_string))[1:-1] \
+ " and started\n> with:"
else:
class_text = str(type(self.binversion_string))[1:-1] \
+ " and contained:"
err = ('> A binary file has a version string (the\n'
'> first thing in the binary file) that was\n'
'> not a string. The .hbn file was probably\n'
'> not created by Hobyah.py.\n'
'> The version string should have been some-\n'
'> thing like "Hobyah.py binary version 12",\n'
'> but it was of ' + class_text + '\n'
'> "' + text + '"\n'
'> The faulty file is "' + self.file_name + '".'
)
gen.WriteError(7002, err, self.log)
return(None)
# If we get to here we have a string. Check it. We want
# it to be something like "Hobyah.py binary version 3". First
# set a Boolean and split the string.
dud = False
parts = result.split()
if result[:26] == "SESconv.py binary version ":
# This is not a suitable file to be loaded into
# classHobyah, as it has all the fingerprints of a
# file intended for classSES. Someone probably
# renamed a file so it ends with ".hbn" instead
# of ".sbn".
err = ('> A binary file has a version string (the\n'
'> first thing in the binary file) that\n'
'> looks like a Hobyah.py version string,\n'
'> not an SESconv.py version string.\n'
'> The version string should have been some-\n'
'> thing like "Hobyah.py binary version 5",\n'
'> but it was\n'
'> "' + result + '"\n'
'> Someone probably renamed an SESconv.py\n'
'> ".sbn" file to end with ".hbn" instead.\n'
'> The faulty file is "' + self.file_name + '".'
)
gen.WriteError(7003, err, self.log)
return(None)
elif result[:25] != "Hobyah.py binary version ":
# The first three words were incorrect.
dud = True
elif len(parts) != 4:
# It had too many words in it.
dud = True
elif not (parts[3].isdecimal()):
# The fourth word is not all digits.
dud = True
if dud:
# Get a shortish slice of the string for the error
# message, as it could be enormous. We take the
# first 60 characters or the first 57 and add an
# ellipsis.
if len(result) <= 60:
text = result
else:
text = result[:57] + "..."
err = ('> A binary file has a version string that\n'
"> didn't"' match the required form (the words\n'
'> "Hobyah.py binary version XX" where XX is\n'
'> an integer. Instead it was\n'
'> "' + text + '"\n'
'> The faulty file is "' + self.file_name + '".\n'
'> It was not created by Hobyah.py, it just\n'
'> happens to be a pickle file that started\n'
'> with a string.'
)
gen.WriteError(7004, err, self.log)
return(None)
else:
# This does look like a Hobyah binary file. Get the
# version as a number. We don't have to try...catch
# this as we already checked that the fourth word is
# all digits.
self.binversion = int(parts[3])
# Now check the version number. The entry below is the version
# number that this version of the class can handle.
self.required = 7
if self.binversion < self.required:
err = ('> A binary file has a version number that is\n'
'> too old for this program to process correctly.\n'
'> The file is of binary version ' + str(self.binversion)
+ ' and this\n'
'> program can only handle files of version '
+ str(self.required) + '.\n'
'> Please rerun the file with a version of\n'
'> Hobyah.py that generates binary files of\n'
'> version ' + str(self.required) + '.\n'
'> The faulty file is "' + self.file_name + '".'
)
gen.WriteError(7005, err, self.log)
return(None)
elif self.binversion > self.required:
err = ('> A binary file has a version number that is\n'
'> too new for this program to process correctly.\n'
'> The file is of binary version ' + str(self.binversion)
+ ' and this\n'
'> program can only handle files of version '
+ str(self.required) + '.\n'
'> Please update your version of this program\n'
'> and its dependent modules.\n'
'> The faulty file is "' + self.file_name + '".'
)
gen.WriteError(7006, err, self.log)
return(None)
# Read the fixed data, giving the expected count of entries.
# When this changes, we need to update the counter of fixed
# entries, the list in the 'else' clause below and the list
# in the procedure PrettyClassPrint.
fcount = 27
self.fixed_data = self._UnPickleData("the fixed data", fcount, pkl)
if self.fixed_data is None:
return(None)
else:
# Unpack 'fcount' entries to their correct variables.
(self.prog_type,
self.when_who,
self.runfile_name, # Name of the .txt file that created it.
self.script_name, # Name of the script that created this
self.script_date, # Date this script was last edited
self.settings_dict,
self.sectypes_dict,
self.tunnels_dict,
self.routes_dict,
self.fanchars_dict,
self.tunnelfans_dict,
self.JFblock_dict,
self.JFcalc_dict,
self.timedlosses_dict,
self.plotcontrol_dict,
self.aero_times,
self.segment_source,
self.tuns2segs,
self.segments_consts,
self.joins_dict,
self.dists,
self.locators,
self.route2segs_dict, # See the note below for details
self.vehicles_dict,
self.vehcalc_dict,
self.r_traffic_dict,
self.t_traffic_dict,
) = self.fixed_data
# A quick note about file names for future reference:
# "self.file_name" is the name of the .hbn file and
# "self.runfile_name" is the name of the .txt file that
# created the .hbn file. We keep them separate in case
# a .hbn file is renamed and we need to distinguish
# between the name of the Hobyah input file to rerun and
# the name of the binary file we just read.
# Another note for future reference:
# and tuns2segs have keys that are the route names or
# tunnel names and return a dictionary with two keys
# in it that each return a tuple, as follows: