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 ...@@ -8,40 +8,44 @@ import csv
import shapefile import shapefile
from os import path from os import path
from networkx import nx_agraph from networkx import nx_agraph
import os
def read_eddy_graph(edgelist, shp_tr_dir = None, read_interp = True): def read_eddy_graph(edgelist, shp_tr_dir = None, read_interp = True):
G = nx.DiGraph() G = nx.DiGraph()
with open(edgelist) as f: if os.access(edgelist, os.R_OK):
reader = csv.reader(f, delimiter = " ", skipinitialspace = True) with open(edgelist) as f:
reader = csv.reader(f, delimiter = " ", skipinitialspace = True)
# Skip title lines: # Skip title lines:
next(reader) next(reader)
next(reader) next(reader)
for row in reader: for row in reader:
k1, i1, k2, i2 = int(row[0]), int(row[1]), int(row[2]), int(row[3]) k1, i1, k2, i2 = (int(row[0]), int(row[1]), int(row[2]),
int(row[3])
try: try:
weight = float(row[4]) weight = float(row[4])
except IndexError: except IndexError:
weight = None weight = None
G.add_edge((k1, i1), (k2, i2), weight = weight)
if read_interp: G.add_edge((k1, i1), (k2, i2), weight = weight)
# Read and set attributes of interpolated eddies:
# Assuming that the directory containing the interpolated if read_interp:
# eddies is in the same location as edgelist: # Read and set attributes of interpolated eddies:
extremum = path.join(path.dirname(edgelist), "SHP_triplet", "extremum")
set_attribute(G, extremum)
if shp_tr_dir is not None: # Assuming that the directory containing the interpolated
# Read and set attributes of Visible eddies: # eddies is in the same location as edgelist:
extremum = path.join(shp_tr_dir, "extremum") extremum = path.join(path.dirname(edgelist), "SHP_triplet",
set_attribute(G, extremum) "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 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