# -*- coding: utf-8 -*-
"""
Generate the taxi flow in Chicago.


Created on Mon Jan 25 16:19:18 2016

@author: kok
"""


from Crime import Tract
from shapely.geometry import Point
import csv
import numpy as np

import os.path

here = os.path.dirname(os.path.abspath(__file__))





def getTaxiFlow(leaveOut = -1, normalization="bydestination"):
    """
    Retrieve taxi flow from file
    
    the self-flow is set to zero.
    
    LeaveOut define the region to be left out for evaluation. Value 
    ranges from 1 to 77
    
    normalization takes value "none/bydestination/bysource"
    """
    s = np.loadtxt("TF.csv", delimiter=",")
    n = s.shape[0]
    for i in range(n):
        s[i,i] = 0
        
    if normalization == 'bydestination':
        fsum = np.sum(s, axis=1)
        for idx, row in enumerate(s):
            s[idx] = row / fsum[idx]
    elif normalization == 'bysource':
        fsum = np.sum(s, axis=0)
        s = s / fsum
    elif normalization == 'none':
        pass
    
    if leaveOut > 0:
        s = np.delete(s, leaveOut -1, 0)
        s = np.delete(s, leaveOut -1, 1)
    return s



def generateTaxiFlow():
    """
    Generate taxi flow and write it to a file
    """
    cas = Tract.createAllCAObjects()
    n = len(cas)
    TF = np.zeros((n, n))   # taxi flow matrix
    
#    cnt = 0
    
    with open('../data/ChicagoTaxi/201401-03.txt', 'rU') as fin:
        reader = csv.reader(fin, delimiter='\t' )
        header = reader.next()
        for row in reader:
            # initialize points            
            start = Point(float(row[3]), float(row[4]))
            end = Point(float(row[5]), float(row[6]))
            
            sid = -1
            eid = -1
            for key, grid in cas.items():
                """
                grid key starts from 1
                map the start/end point of trip into grids to get flow
                """
                if grid.polygon.contains(start):
                    sid = key - 1
                if grid.polygon.contains(end):
                    eid = key - 1
                if sid != -1 and eid != -1:
                    break
            
            TF[sid, eid] += 1
#            cnt += 1
#            if (cnt > 1000):
#                break

    np.savetxt(here + "/TF.csv", TF, delimiter="," )






if __name__ == '__main__':
    
#    generateTaxiFlow()
    s = getTaxiFlow(usePercentage=False)
@@ -335,19 +335,35 @@
 ination%22
+, gridLevel='ca'
 ):%0A
-
     %22%22%22%0A
@@ -601,24 +601,54 @@
 ce%22%0A    %22%22%22%0A
+    if gridLevel == 'ca':%0A    
     s = np.l
@@ -679,16 +679,101 @@
 er=%22,%22)%0A
+    elif gridLevel == 'tract':%0A        s = np.loadtxt(%22TF_tract.csv%22, delimiter=%22,%22)%0A
     n = 
@@ -1267,16 +1267,30 @@
 axiFlow(
+gridLevel='ca'
 ):%0A    %22
@@ -1342,24 +1342,54 @@
 ile%0A    %22%22%22%0A
+    if gridLevel == 'ca':%0A    
     cas = Tr
@@ -1413,16 +1413,91 @@
 jects()%0A
+    elif gridLevel == 'tract':%0A        cas = Tract.createAllTractObjects()%0A
     n = 
@@ -1553,24 +1553,61 @@
 matrix%0A    %0A
+    ordKey = sorted(cas.keys())%0A    %0A
 #    cnt = 0
@@ -2257,31 +2257,41 @@
       sid = 
-key - 1
+ordKey.index(key)
 %0A           
@@ -2356,15 +2356,25 @@
 d = 
-key - 1
+ordKey.index(key)
 %0A   
@@ -2562,35 +2562,158 @@
 eak%0A
-%0A    np.savetxt(here + %22/TF
+    if gridLevel == 'ca':%0A        np.savetxt(here + %22/TF.csv%22, TF, delimiter=%22,%22 )%0A    elif gridLevel == 'tract':%0A        np.savetxt(here + %22/TF_tract
 .csv

generate taxi flow at tract level