# Path: writeit.py
# def writePipes(fn,conns, xs,ys, Ns, Ls, Mrs, Ds, jt, bt, bv, r, h0s, q0s, T, M, a, elevs):
#     '''write .inp and .config files from scratch--be careful that the input parameter conns is actually a valid network structure!'''
#     #conns is an array of size Nx2; each row represents two junctions connecting a single edge
#     Np = np.shape(conns)[0]
#     Nn = len(jt)
#     #first write a fake .inp to get network connectivity
#     ftemp = '../indata/fakeinp.inp'
#     with open(ftemp, 'w') as ft:
#         ft.write('[TITLE]\n\n\n[JUNCTIONS]\n;ID              	Elev        	Demand      	Pattern\n')
#         for k in range(Nn):
#             ft.write("%d %15s %2.3f %15s0 %30s ;\n"%(k," ",elevs[k]," "," "))
#         ft.write('\n[PIPES]\n;ID              	Node1           	Node2           	Length      	Diameter    	Roughness   	MinorLoss   	Status\n')
#         for k in range(Np):
#             ft.write("%s %15s %s %15s %s %15s %4.1d %15s %2.2f %15s %1.4f\n" % \
#                                 (k," ",conns[k,0]," ", conns[k,1]," ", Ls[k]," ", Ds[k]," ", Mrs[k]))
#         ft.write('\n[COORDINATES]\n;Node            	X-Coord         	Y-Coord\n')
#         for k in range(Nn):
#             ft.write('%d %15s  %.2f %15s %.2f\n'%(k,' ', xs[k], ' ', ys[k]))
#         print ftemp
#     (fi, fc) = rewritePipes(fn, ftemp, Ns, Ls, Mrs, Ds, jt, bt, bv, r, h0s, q0s, T, M, a, elevs)
#     return (fi, fc)


import time
import numpy as np
import pickle
import sys
import networkx as nx
import os
        import matplotlib.colors as colors  
from scipy import stats
from matplotlib import rc
from matplotlib.mlab import find
from  matplotlib import pyplot as plt
from allthethings import PyNetwork, PyPipe_ps
from allthethings import PyBC_opt_dh
from writeit import writePipes
    elif case ==3:
        conns = np.array([[0,1],[1,2],[1,3],[2,4], [3,4],[4,5]])
        nodeTypes = getNodeTypes(conns)
        Np = np.shape(conns)[0]
        Nn = len(np.unique(conns))
        Ls = [50,150,25,25,25,50]
        Ns = [Ls[k]*2 for k in range(Np)]
        Mrs = [0.007]*Np
        Ds = [0.1]*Np
        jt = nodeTypes
        bt = [1]*Nn
        #reflect everything except at inlet node
        r = [1]*Nn
        r[0]=0
        bv = [0]*Nn
        #specify pressure here
        bv[0] = 0.015473914995245055
        bt[0]=0
        h0s = [0.01]*Np
        h0s[0]=1
        #h0s = [1.,2.,3.,4.,5.,1.]
        q0s = [0.00]*Np
        T = 600
        a = 10
        elevs = [0]*Nn
        descrip= "pressurizing network with loop. specify pressure at inlet (nonconstant)"
    
    (xs,ys) = getCoordinates(conns)
    dx = [Ls[i]/float(Ns[i]) for i in range(Np)] 
    M = int(T*a/(max(dx)*.8))

# Path: writeit.py
# def writePipes(fn,conns, xs,ys, Ns, Ls, Mrs, Ds, jt, bt, bv, r, h0s, q0s, T, M, a, elevs):
#     '''write .inp and .config files from scratch--be careful that the input parameter conns is actually a valid network structure!'''
#     #conns is an array of size Nx2; each row represents two junctions connecting a single edge
#     Np = np.shape(conns)[0]
#     Nn = len(jt)
#     #first write a fake .inp to get network connectivity
#     ftemp = '../indata/fakeinp.inp'
#     with open(ftemp, 'w') as ft:
#         ft.write('[TITLE]\n\n\n[JUNCTIONS]\n;ID              	Elev        	Demand      	Pattern\n')
#         for k in range(Nn):
#             ft.write("%d %15s %2.3f %15s0 %30s ;\n"%(k," ",elevs[k]," "," "))
#         ft.write('\n[PIPES]\n;ID              	Node1           	Node2           	Length      	Diameter    	Roughness   	MinorLoss   	Status\n')
#         for k in range(Np):
#             ft.write("%s %15s %s %15s %s %15s %4.1d %15s %2.2f %15s %1.4f\n" % \
#                                 (k," ",conns[k,0]," ", conns[k,1]," ", Ls[k]," ", Ds[k]," ", Mrs[k]))
#         ft.write('\n[COORDINATES]\n;Node            	X-Coord         	Y-Coord\n')
#         for k in range(Nn):
#             ft.write('%d %15s  %.2f %15s %.2f\n'%(k,' ', xs[k], ' ', ys[k]))
#         print ftemp
#     (fi, fc) = rewritePipes(fn, ftemp, Ns, Ls, Mrs, Ds, jt, bt, bv, r, h0s, q0s, T, M, a, elevs)
#     return (fi, fc)


# Path: examples/sanity_check.py
import time
import numpy as np
import pickle
import sys
import networkx as nx
import os
        import matplotlib.colors as colors  
from scipy import stats
from matplotlib import rc
from matplotlib.mlab import find
from  matplotlib import pyplot as plt
from allthethings import PyNetwork, PyPipe_ps
from allthethings import PyBC_opt_dh
from writeit import writePipes

    elif case ==3:
        conns = np.array([[0,1],[1,2],[1,3],[2,4], [3,4],[4,5]])
        nodeTypes = getNodeTypes(conns)
        Np = np.shape(conns)[0]
        Nn = len(np.unique(conns))
        Ls = [50,150,25,25,25,50]
        Ns = [Ls[k]*2 for k in range(Np)]
        Mrs = [0.007]*Np
        Ds = [0.1]*Np
        jt = nodeTypes
        bt = [1]*Nn
        #reflect everything except at inlet node
        r = [1]*Nn
        r[0]=0
        bv = [0]*Nn
        #specify pressure here
        bv[0] = 0.015473914995245055
        bt[0]=0
        h0s = [0.01]*Np
        h0s[0]=1
        #h0s = [1.,2.,3.,4.,5.,1.]
        q0s = [0.00]*Np
        T = 600
        a = 10
        elevs = [0]*Nn
        descrip= "pressurizing network with loop. specify pressure at inlet (nonconstant)"
    
    (xs,ys) = getCoordinates(conns)
    dx = [Ls[i]/float(Ns[i]) for i in range(Np)] 
    M = int(T*a/(max(dx)*.8))

    (fi, fc) = writePipes(fn,conns, xs,ys, Ns, Ls, Mrs, Ds, jt, bt, bv, r, h0s, q0s, T, M, a, elevs)