def conditionError(inverse,surfaces,neighbors,distances,fname=False,disp=-1,params={},savepath=False,title=False,show=True):
    conditions = []
    levels = []
    errors = []
    fram = []
    print(params)
    params.setdefault("upperbound",1000)
    params.setdefault("reflevel",1000)
    if savepath:
        try:
            os.makedirs(savepath)
        except FileExistsError as e:
            print(e)
    for lowlevel in range(params["upperbound"]+200,2600,200):
        params.update({"lowerbound":lowlevel})
        print(params)
        out = inverttools.invert(inverse,surfaces,neighbors,distances,params=params)
        inv = out["surfaces"]
        columndict = out["coldict"]
        svds = out["svddecomp"]
        A = out["matrixsetup"]
        e = out["errors"]
        meta = out["metadata"]
        if inv:
            s = np.diag((1.0/svds[1]))
            condition = s[0]/s[-1]
            conditions.append(condition)
            errors.append(np.sum(np.abs(e[1])))
            levels.append(lowlevel)
            i,o = graph.transportLine(inv,(-24.71,83.60),(27.496,80.06),2400,False,show=False)
            fram.append(abs(abs(i)-abs(o))) 
            inv = nstools.streamFuncToUV(inv,neighbors,distances)
            #fram.append(abs(distanceFromKnown(inv))) 
            if lowlevel ==disp:
                coupleinvert = nstools.streamFuncToUV(inv,neighbors,distances)
                #graph.transportLine(coupleinvert,(-24.71,83.60),(27.496,80.06),2400,True)
                graph.graphVectorField(inv,"uabs","vabs","z")
    fig, (ax1,ax3) = plt.subplots(2,1)
    ax1.scatter(levels,errors,label="error")
    ax1.set_ylabel("R")
    ax2 = ax1.twinx() 
    ax2.scatter(levels,conditions,color="red",label = "matrix condition")
    ax2.set_ylabel("Matrix Condition")
    ax1.set_xlabel("Lowest Neutral Surface")
    if not title:
        fig.suptitle(params["reflevel"])
    else:
        fig.suptitle(title)
    ax3.scatter(levels,fram)
    ax3.set_ylabel("Net transport through Fram Strait in Sv")
    ax3.set_xlabel("Lowest Neutral Surface")
    plt.legend()
    fig.set_size_inches(16.5,12)
    if savepath and not fname:
        plt.savefig(savepath+"ns"+str(params["reflevel"])+".png")
    if savepath and fname:
        plt.savefig(savepath+str(fname)+".png")
    if show:
        plt.show()
    plt.close()
def sensitivityRunCompress(infilename, outfilename):
    with open(infilename, 'rb') as infile:
        [out, neighbors, distances] = pickle.load(infile)
    inv = out["surfaces"]
    inv = nstools.addOldUnits(inv)
    inv = nstools.twoCReference(inv)
    inv = nstools.streamFuncToUV(inv, neighbors, distances)
    inv = nstools.externalReference(inv, "data/yomaha_1000.nc")
    surfaces = surfaceCompress(inv)
    errors = out["errors"]
    metadata = out["metadata"]
    with open(outfilename, 'wb') as outfile:
        pickle.dump([surfaces, errors, metadata], outfile)
def decayScaleSensitivity(originalsurfaces,neighbors,distances):
    f = open("h0log.txt", "w")
    flat_kvs={}
    for H_0 in range(500,3000,500):
        surfaces = copy.deepcopy(originalsurfaces)
        surfaces = nstools.addParametersToSurfaces(brasil,surfaces,neighbors,distances,H_0=H_0)
        # graph.NSGAMCompare(preinterpsurfaces,surfaces,-30,-180,180)

        # print(surfaces.keys())
        params = {"reflevel":int(2200),"upperbound":1000,"lowerbound":4200,\
                "mixs":{"kvo":True,"kvb":True,"kh":True},"debug":False,\
                  "3point":True,"edgeguard":True,"H_0":H_0}
        # Conditions
        # All mixing: 201235
        # No mixing: 147
        # Kv0 only: 147
        # KvH and Kv0 only: 148
        # KvH and Kv0 only with out edgeguard (tm): 489

        out= inverttools.invert("coupled",surfaces,neighbors,distances,params=params)
        inv = out["surfaces"]
        condition = int(out["metadata"]["condition"])
        err = int(out["metadata"]["error"])
        inv = nstools.streamFuncToUV(inv,neighbors,distances)
        print("#"*10+str(H_0),file=f)
        print(out["metadata"],file=f)
        #result = nstools.transportDiagnostics(inv)
        #print(result,file=f)
        kvs = []
        for l in inv.keys():
            kvs += list(inv[l]["data"]["kv"])
        kvs = np.asarray(kvs).flatten()
        flat_kvs[str(H_0)+"|"+str(condition)+"|"+str(err)] = kvs
        print("kv: ,"+str(np.nanmean(kvs)),file=f)
        print("% negative: ,"+str(np.sum(kvs<0)/np.sum(~np.isnan(kvs))),file=f)
        del out
        del inv
        del surfaces

    fig, (ax1,ax2) = plt.subplots(1,2)
    bins=range(-10,0)
    for k in flat_kvs.keys():
        ax1.hist(np.log10(np.abs(flat_kvs[k][flat_kvs[k]>0])),bins,histtype='step',label=k)
        ax2.hist(np.log10(np.abs(flat_kvs[k][flat_kvs[k]<0])),bins,histtype='step',label=k)
    plt.legend()
    plt.show()
    f.close()
def weightingSensitivity(originalsurfaces,neighbors,distances):
    flat_kvs={}
    originalsurfaces = nstools.addParametersToSurfaces(brasil,originalsurfaces,neighbors,distances,H_0=500)
    for o in [-1,0,1]:
        for b in [-1,0,1]:
            for h in [0]:
                surfaces = copy.deepcopy(originalsurfaces)
                # graph.NSGAMCompare(preinterpsurfaces,surfaces,-30,-180,180)

                # print(surfaces.keys())
                params = {"reflevel":int(2062),"upperbound":1000,"lowerbound":4200,\
                        "scalecoeffs":{"Ar":0.05,"kvo":5*10**(-6+o),"kvb":5*10**(-5+b),"kh":5*10**(2+h)},\
                        "mixs":{"kvo":True,"kvb":True,"kh":True},"debug":False,\
                          "3point":True,"edgeguard":True,"H_0":500}
                # Conditions
                # All mixing: 201235
                # No mixing: 147
                # Kv0 only: 147
                # KvH and Kv0 only: 148
                # KvH and Kv0 only with out edgeguard (tm): 489

                out= inverttools.invert("coupled",surfaces,neighbors,distances,params=params)
                inv = out["surfaces"]
                condition = out["metadata"]["condition"]
                inv = nstools.streamFuncToUV(inv,neighbors,distances)
                result = nstools.transportDiagnostics(inv)
                print(result)
                kvs = []
                for l in inv.keys():
                    kvs += list(inv[l]["data"]["kv"])

                kvs = np.asarray(kvs).flatten()
                flat_kvs[str(o)+str(b)+str(h)+"|"+str(int(condition))] = kvs
                del out
                del inv
                del surfaces

    fig, (ax1,ax2) = plt.subplots(1,2)
    bins=range(-10,0)
    for k in flat_kvs.keys():
        ax1.hist(np.log10(np.abs(flat_kvs[k][flat_kvs[k]>0])),bins,histtype='step',label=k)
        ax2.hist(np.log10(np.abs(flat_kvs[k][flat_kvs[k]<0])),bins,histtype='step',label=k)
    plt.legend()
    plt.show()
    f.close()
    reflevels = surfaces.keys()
    for k in reflevels:
        if int(k) > 1200 and int(k) < 4000:
            with open('data/run0/withparams.pickle', 'rb') as outfile:
                [surfaces, neighbors, distances] = pickle.load(outfile)
            params = {"reflevel":int(k),"upperbound":1000,"lowerbound":4000,\
                    "mixs":{"kvo":True,"kvb":True,"kh":True},"debug":False,\
                    "3point":True,"edgeguard":True,"H_0":1000}
            out = inverttools.invert("coupled",
                                     surfaces,
                                     neighbors,
                                     distances,
                                     params=params)

            inv = out["surfaces"]
            inv = nstools.streamFuncToUV(inv, neighbors, distances)
            with open('sens/reflevel/{}.pickle'.format(k), 'wb') as outfile:
                pickle.dump([out, neighbors, distances], outfile)

if switch["H_0"]:
    ######################
    ###### H_0 Testing
    ######################

    for H_0 in [200, 500, 1000, 1500, 2000]:
        with open('data/run0/interpedbrasil.pickle', 'rb') as outfile:
            [surfaces, neighbors, distances] = pickle.load(outfile)
        surfaces, neighbors, distances = nstools.addParametersToSurfaces(
            brasil, surfaces, neighbors, distances, H_0=H_0)
        params = {"reflevel":2000,"upperbound":1000,"lowerbound":4000,\
                "mixs":{"kvo":True,"kvb":True,"kh":True},"debug":False,\