import importlib.util
#The default paths for windows, linux and mac
spec_win = importlib.util.spec_from_file_location('lumapi', 'C:\\Program Files\\Lumerical\\2020a\\api\\python\\lumapi.py')
spec_lin = importlib.util.spec_from_file_location('lumapi', "/opt/lumerical/2020a/api/python/lumapi.py")
spec_mac = importlib.util.spec_from_file_location('luampi', "/Applications/Lumerical/FDTD Solutions/FDTD Solutions.app/Contents/API/Python/lumapi.py")
#Functions that perform the actual loading
lumapi = importlib.util.module_from_spec(spec_win) # 
spec.loader.exec_module(lumapi)

from matplotlib import pyplot as pyplot

ic = lumapi.INTERCONNECT("run_monte_carlo.icp")

ic.addsweep(2)
MC_name = "MC_script"
ic.setsweep("Monte Carlo analysis", "name", MC_name)

setup = {
    "number of trials": 200.0,
    "enable seed": 1.0,
    "seed": 1.0,
    "Variation": "Both",
    "type": "Parameters",
}

for k, v in setup.items():
    ic.setsweep(MC_name, k, v)

sweep_parameters = [
    { 
        "Name": "cpl_2",
        "Parameter": "::Root Element::WC2::coupling coefficient 1",
        "Value": ic.getnamed("WC2", "coupling coefficient 1"),
        "Distribution": {
            "type": "gaussian",
            "variation": 0.02
        }
    },

    {
        "Name": "cpl_3",
        "Parameter": "::Root Element::WC3::coupling coefficient 1",
        "Value": ic.getnamed("WC3", "coupling coefficient 1"),
        "Distribution": {
            "type": "uniform",
            "variation": 0.04
        }
    },

    {
        "Name": "wgd_model",
        "Model": "WGD::group index 1",
        "Value": ic.getnamed("SW1", "group index 1"),
        "Process": {
            "type": "uniform",
            "variation": 1.02
        },
        "Mismatch": {
            "type": "gaussian",
            "variation": 0.1
        }
    },

    {
        "Name": "wgd_corr",
        "Parameters": "SW1_group_index_1,SW2_group_index_1,SW3_group_index_1",
        "Value": 0.95
    }
]

sweep_results = [
    {
        "Name": "fsr",
        "Result": "::Root Element::Optical Network Analyzer::input 2/mode 1/peak/free spectral range",
        "Estimation": True,
        "Min": 1.0e11,
        "Max": 2.0e11
    },

    {
        "Name": "bd",
        "Result": "::Root Element::Optical Network Analyzer::input 1/mode 1/peak/bandwidth",
        "Estimation": False
    },

    {
        "Name": "gain",
        "Result": "::Root Element::Optical Network Analyzer::input 1/mode 1/peak/gain",
        "Estimation": False
    }
]

for parameter in sweep_parameters:
    ic.addsweepparameter(MC_name, parameter)

for result in sweep_results:
    ic.addsweepresult(MC_name, result)

ic.runsweep(MC_name)

fsr = ic.getsweepresult(MC_name, "analysis/results/histogram/fsr")
fsrCount = fsr["count"][0][0]
fsr = fsr["fsr"][0][0]

pdf = ic.getsweepresult(MC_name, "analysis/results/pdf/fsr")
pdfCount = pdf["count"][0][0]
pdfFsr = pdf["fsr"][0][0]

pyplot.bar(fsr/1e9, fsrCount)
pyplot.plot(pdfFsr/1e9, pdfCount)
pyplot.xlabel("fsr (GHz)")
pyplot.ylabel("count")
pyplot.show()
