Skip to content

Commit cd281c1

Browse files
authored
Merge pull request #13 from wsp-sag/feature-nwp-transit-veh
Added function to read transit vehicles from NWPs
2 parents 4c4172d + f936f62 commit cd281c1

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

balsa/routines/io/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
read_nwp_transit_line_attributes, read_nwp_transit_network,
99
read_nwp_transit_result_summary,
1010
read_nwp_transit_segment_results,
11-
read_nwp_transit_station_results)
11+
read_nwp_transit_station_results, read_nwp_transit_vehicles)
1212
from .omx import read_omx, to_omx

balsa/routines/io/nwp.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,42 @@ def read_nwp_transit_segment_results(nwp_fp: Union[str, PathLike]) -> pd.DataFra
411411
segments = segments[['line', 'inode', 'jnode', 'seg_seq', 'loop', 'boardings', 'alightings', 'volume']].copy()
412412

413413
return segments
414+
415+
416+
def read_nwp_transit_vehicles(nwp_fp: Union[str, PathLike]) -> pd.DataFrame:
417+
"""A function to read the transit vehicles from a Network Package file (exported from Emme using the TMG Toolbox)
418+
into DataFrames.
419+
420+
Args:
421+
nwp_fp (str | PathLike): File path to the network package.
422+
423+
Returns:
424+
pd.DataFrame: DataFrame containing the transit vehicles.
425+
"""
426+
nwp_fp = Path(nwp_fp)
427+
if not nwp_fp.exists():
428+
raise FileNotFoundError(f'File `{nwp_fp.as_posix()}` not found.')
429+
430+
with zipfile.ZipFile(nwp_fp) as zf:
431+
# Get header
432+
header = None
433+
for i, line in enumerate(zf.open('vehicles.202'), start=1):
434+
line = line.strip().decode('utf-8')
435+
if line.startswith('c'):
436+
continue # Skip comment lines
437+
if line.startswith('t vehicles'):
438+
header = i
439+
440+
# Read data
441+
data_types = {
442+
'id': int, 'description': str, 'mode': str, 'fleet_size': int, 'seated_capacity': float,
443+
'total_capacity': float, 'cost_time_coeff': float, 'cost_distance_coeff': float, 'energy_time_coeff': float,
444+
'energy_distance_coeff': float, 'auto_equivalent': float
445+
}
446+
vehicles = pd.read_csv(
447+
zf.open('vehicles.202'), index_col='id', usecols=data_types.keys(), dtype=data_types, skiprows=header,
448+
quotechar="'", delim_whitespace=True
449+
)
450+
vehicles.index.name = 'veh_id'
451+
452+
return vehicles

0 commit comments

Comments
 (0)