from time import gmtime, strftime
%matplotlib inline
import utilsNetwork

"""
Explanation: Exposure model: Shortest path between origin and destination
This IPython notebook processes a shapefile, and creates an exposure model.
Each asset has a typology associated, that will be defined based on an external .csv file or on the length of the asset.
End of explanation
"""


networkShp = '/home/openquake/GEM/Lifelines/Building_NetworkNew/Input_files/ShortestPathSpain/mo_FINAL.shp'
networkShp = '/home/openquake/GEM/Lifelines/Building_NetworkNew/Input_files/railwayPathSines/railway.shp'
#networkShp = '/media/sf_Shared_Folder/Paper_Scenarios/testFilesExposure/highways-part.shp'

(shpAdj,maxNumConn) = utilsNetwork.shp_adj(networkShp)

"""
Explanation: Specify the location of the vector GIS file containing the network:
End of explanation
"""


resultsFolder = '/media/sf_Shared_Folder/Paper_Scenarios/2016-09-Revision/' 

"""
Explanation: Specify the location of the folder where the results will be saved
End of explanation
"""


maxDist = 20
maxDistBr = 20

shpAdjMaxDist = utilsNetwork.divide_edges(shpAdj,maxNumConn,maxDist)

"""
Explanation: Specify maxDist, the maximum distance between 2 points in the network (in km). If an edge is bigger than maxDist, it will be divided in two, creating a new point.
Note: only verifies if 1st or 2nd relation node distances are > maxDist
End of explanation
"""


#Option A
bridgesShp = '/media/sf_Shared_Folder/Paper_Scenarios/testFilesExposure/highways-partBr.shp'
bridgesShp = '/home/openquake/GEM/Lifelines/Building_NetworkNew/Input_files/ShortestPathSpain/mo_br.shp'

#brAdj = utilsNetwork.shp_adj_br(bridgesShp,maxDistBr,resultsFolder)
#brRows = utilsNetwork.one_node_per_bridge(brAdj,shpAdjMaxDist,resultsFolder)

##### OR #####

#Option B
brNodes = '../Input_files/railwayPathSines/bridges.csv'
brRows = utilsNetwork.find_br_rows_closest_coord(shpAdjMaxDist, brNodes)
#brRows = utilsNetwork.find_br_rows_exact_coord(shpAdjMaxDist,brNodes)

"""
Explanation: Choose between option A or B:
A: Provide a vector GIS file containing the location of the bridges (bridges_shp)
OR
B: Provide a .csv file containing the assets coordinates (one node per asset) (brNodes).
In this option, if you have the exact coordinates of the assets in the network, use find_br_rows_exact_coord (faster).
Else, if you want to search the closest node to the given coordinates, use find_br_rows_closest_coord
End of explanation
"""


#Option A
#brPerpNodes = '/media/sf_Shared_Folder/testFilesExposure/br_links_nodes_mo.csv'
brPerpNodes = '/home/openquake/GEM/Lifelines/Building_NetworkNew/Input_files/ShortestPathSpain/br_nodes+links_nodes_mo.csv'
brPerpNodes = '/home/openquake/GEM/Lifelines/Building_NetworkNew/Input_files/railwayPathSines/viaducts.csv'

#brPerpRows = utilsNetwork.find_br_rows_exact_coord(shpAdjMaxDist,brPerpNodes)
brPerpRows = utilsNetwork.find_br_rows_closest_coord(shpAdjMaxDist,brPerpNodes)

#Option B
#brPerpRows = 0

"""
Explanation: Option A: If assets (viaducts) that cross the network are to be considered, provide a .csv file with the coordinates of these assets (brPerpNodes)
Option B: If these assets are not considered
End of explanation
"""


limit_length1 = 40.
limit_length2 = 150.
adj = utilsNetwork.sim_adj(shpAdjMaxDist,brRows,brPerpRows,maxDist,limit_length1,limit_length2)

(nodes, edges, weights) = utilsNetwork.save_files_networkx(adj,maxNumConn,resultsFolder)

"""
Explanation: In this case we assume four typologies of assets: three typologies of bridges and one of viaducts 
FF1: Single span bridges (length < 40m)
FF2: Multi-span medium length (40m < length < 150m)
FF3: Multi-span major bridges (length > 150m)
FF4: Viaducts
End of explanation
"""


#Source coordinates - Lisbon
#lon1 = -9.142685
#lat1 = 38.736946
#Source coordinates - Spain
#lon1 = -7.035
#lat1 = 38.883
#Source coordinates - Sines Port
lon1 = -8.836
lat1 = 37.939
#Destination coordinates - Factory
lon2 = -7.979
lat2 = 37.575

(source, destination) = utilsNetwork.node_in_out(adj,lon1,lat1,lon2,lat2)

"""
Explanation: Shortest path
Specify the coordinates of the source and destination, to calculate the shortest path between these locations.
End of explanation
"""


import networkx as nx
#Directed graph:
#G = nx.DiGraph()
#Undirected graph:
G = nx.Graph()    
(plotN,G,pos,pngLimits) = utilsNetwork.draw_network(nodes, edges, weights, G)
utilsNetwork.draw_nodes(source,destination,G,pos,pngLimits)

saveImage = False
filename = './pathLx.png'
utilsNetwork.shortest_path(G,source,destination,pos,filename,saveImage,resultsFolder,pngLimits)

"""
Explanation: Choose between a directed or undirected graph (if the direction of the edges is taken into account or not)
End of explanation
"""


path1 = '/media/sf_Shared_Folder/Paper_Scenarios/2016-09-Revision/path1.txt'
nodes = '/media/sf_Shared_Folder/Paper_Scenarios/2016-09-Revision/network-nodes.txt'

path_number = 1
utilsNetwork.path_coord(path1,nodes,path_number)


#Option A: Select only the nodes that are bridges
path1FF = utilsNetwork.path_br_ff(path1,adj,path_number,resultsFolder)

##### OR #####

#Option B: Select all the nodes (bridges and pavement)
#path1FF = utilsNetwork.path_ff(path1,adj,path_number,resultsFolder)

"""
Explanation: Option A: if the exposure model will only include point assets (the assets previously specified) 
OR 
Option B: if the exposure model will include point and linear assets
End of explanation
"""


exposureTxt = '/media/sf_Shared_Folder/Paper_Scenarios/2016-09-Revision/path1-exposure.txt'

utilsNetwork.exposure_1path(exposureTxt,resultsFolder)

"""
Explanation: To create the a .csv file with the coordinates of the assets, specify the file created in the previous step.
End of explanation
"""


