"""
@file    inputs.py
@author  Yun-Pang.Wang@dlr.de
@date    2007-10-25
@version $Id: inputs.py 6870 2009-03-02 15:59:30Z yunpangwang $

This script is to retrieve the assignment parameters, the OD districts and the matrix from the input files. 
Moreover, the link travel time for district connectors will be estimated.

Copyright (C) 2008 DLR/TS, Germany
All rights reserved
"""

# read the analyzed matrix         
def getMatrix(net, verbose, matrix, MatrixSum):#, mtxplfile, mtxtfile):
    matrixPshort = []
    startVertices = []
    endVertices = []
    Pshort_EffCells = 0
    periodList = []
    if verbose:
        print 'matrix:', str(matrix)                                 

    ODpairs = 0
    origins = 0
    dest= 0
    currentMatrixSum = 0.0
    skipCount = 0
    zones = 0
    smallDemandNum = 0
    for line in open(matrix):
        if line[0] == '$':
            visumCode = line[1:3]
            if visumCode != 'VM':
                skipCount += 1
        elif line[0] != '*' and line[0] != '$':
            skipCount += 1
            if skipCount == 2:
                for elem in line.split():
                    periodList.append(float(elem))
            elif skipCount > 3:
                if zones == 0:
                    for elem in line.split():
                        zones = int(elem)
                elif len(startVertices) < zones:
                    for elem in line.split():
                        if len(elem) > 0:
                            for startVertex in net.getstartVertices():
                                if startVertex.label == elem:
                                    startVertices.append(startVertex)
                            for endVertex in net.getendVertices():
                                if endVertex.label == elem:
                                    endVertices.append(endVertex)
                    origins = len(startVertices)
                    dest = len(endVertices)        
                elif len(startVertices) == zones:
                    if ODpairs % origins == 0:
                        matrixPshort.append([])
                    for item in line.split():
                        matrixPshort[-1].append(float(item))
                        ODpairs += 1
                        MatrixSum += float(item)
                        currentMatrixSum += float(item) 
                        if float(item) > 0.:
                            Pshort_EffCells += 1
                        if float(item) < 1. and float(item) > 0.:
                            smallDemandNum += 1
    begintime = int(periodList[0])
    assignPeriod = int(periodList[1]) - begintime
    smallDemandRatio = float(smallDemandNum)/float(Pshort_EffCells)
    
    if verbose:
        print 'Number of zones:', zones
        print 'Number of origins:', origins
        print 'Number of destinations:', dest
        print 'begintime:', begintime
        print 'currentMatrixSum:', currentMatrixSum        
        print 'Effective O-D Cells:', Pshort_EffCells
        print 'number of start Vertices:', net.getstartCounts()
        print 'number of end Vertices):',  net.getendCounts()
        print 'smallDemandRatio):', smallDemandRatio
    
    return matrixPshort, startVertices, endVertices, currentMatrixSum, begintime, assignPeriod, Pshort_EffCells, MatrixSum, smallDemandRatio  

# estimate the travel times on the district connectors
# assumption: all vehilces can reach the access links within 10 min from the respective traffic zone
def getConnectionTravelTime(startVertices, endVertices):
    for vertex in startVertices:
        sum = 0.0
        for edge in vertex.outEdges:     
            sum += float(edge.weight)
        for edge in vertex.outEdges:
            edge.freeflowtime = (1-float(edge.weight)/sum) * 10        
                                          
            edge.actualtime = edge.freeflowtime
    for vertex in endVertices:
        sum = 0.0
        for edge in vertex.inEdges:  
            sum += float(edge.weight)
        for edge in vertex.inEdges:
            edge.freeflowtime = (1-float(edge.weight)/sum) * 10 
                                              
            edge.actualtime = edge.freeflowtime