Skip to content
Snippets Groups Projects
Commit 4ff019ac authored by Lionel GUEZ's avatar Lionel GUEZ
Browse files

Do not crash on missing edgelist

Motivation: there may be only one of the two edgelist files in the
current directory.
parent 29015b7d
No related branches found
No related tags found
No related merge requests found
......@@ -8,40 +8,44 @@ import csv
import shapefile
from os import path
from networkx import nx_agraph
import os
def read_eddy_graph(edgelist, shp_tr_dir = None, read_interp = True):
G = nx.DiGraph()
with open(edgelist) as f:
reader = csv.reader(f, delimiter = " ", skipinitialspace = True)
if os.access(edgelist, os.R_OK):
with open(edgelist) as f:
reader = csv.reader(f, delimiter = " ", skipinitialspace = True)
# Skip title lines:
next(reader)
next(reader)
# Skip title lines:
next(reader)
next(reader)
for row in reader:
k1, i1, k2, i2 = int(row[0]), int(row[1]), int(row[2]), int(row[3])
for row in reader:
k1, i1, k2, i2 = (int(row[0]), int(row[1]), int(row[2]),
int(row[3])
try:
weight = float(row[4])
except IndexError:
weight = None
G.add_edge((k1, i1), (k2, i2), weight = weight)
try:
weight = float(row[4])
except IndexError:
weight = None
if read_interp:
# Read and set attributes of interpolated eddies:
G.add_edge((k1, i1), (k2, i2), weight = weight)
# Assuming that the directory containing the interpolated
# eddies is in the same location as edgelist:
extremum = path.join(path.dirname(edgelist), "SHP_triplet", "extremum")
set_attribute(G, extremum)
if read_interp:
# Read and set attributes of interpolated eddies:
if shp_tr_dir is not None:
# Read and set attributes of Visible eddies:
extremum = path.join(shp_tr_dir, "extremum")
set_attribute(G, extremum)
# Assuming that the directory containing the interpolated
# eddies is in the same location as edgelist:
extremum = path.join(path.dirname(edgelist), "SHP_triplet",
"extremum")
set_attribute(G, extremum)
if shp_tr_dir is not None:
# Read and set attributes of Visible eddies:
extremum = path.join(shp_tr_dir, "extremum")
set_attribute(G, extremum)
return G
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment