from pathlib import Path
from scipy.interpolate import interp1d
import numpy as np

from msdsl.rf import s4p_to_step

THIS_DIR = Path(__file__).resolve().parent
TOP_DIR = THIS_DIR.parent.parent

COMPARISON_FILE = 'peters_01_0605_B1_thru.s4p'

COMPARISON_TOVER = 0.1e-12

COMPARISON_TDUR = 10e-9

COMPARISON_XDATA = [
    2.00e-09, 2.01e-09, 2.02e-09, 2.03e-09, 2.04e-09, 2.05e-09, 2.06e-09, 2.07e-09, 2.08e-09, 2.09e-09,
    2.10e-09, 2.11e-09, 2.12e-09, 2.13e-09, 2.14e-09, 2.15e-09, 2.16e-09, 2.17e-09, 2.18e-09, 2.19e-09,
    2.20e-09, 2.21e-09, 2.22e-09, 2.23e-09, 2.24e-09, 2.25e-09, 2.26e-09, 2.27e-09, 2.28e-09, 2.29e-09,
    2.30e-09, 2.31e-09, 2.32e-09, 2.33e-09, 2.34e-09, 2.35e-09, 2.36e-09, 2.37e-09, 2.38e-09, 2.39e-09,
    2.40e-09, 2.41e-09, 2.42e-09, 2.43e-09, 2.44e-09, 2.45e-09, 2.46e-09, 2.47e-09, 2.48e-09, 2.49e-09,
    2.50e-09, 2.51e-09, 2.52e-09, 2.53e-09, 2.54e-09, 2.55e-09, 2.56e-09, 2.57e-09, 2.58e-09, 2.59e-09,
    2.60e-09, 2.61e-09, 2.62e-09, 2.63e-09, 2.64e-09, 2.65e-09, 2.66e-09, 2.67e-09, 2.68e-09, 2.69e-09,
    2.70e-09, 2.71e-09, 2.72e-09, 2.73e-09, 2.74e-09, 2.75e-09, 2.76e-09, 2.77e-09, 2.78e-09, 2.79e-09,
    2.80e-09, 2.81e-09, 2.82e-09, 2.83e-09, 2.84e-09, 2.85e-09, 2.86e-09, 2.87e-09, 2.88e-09, 2.89e-09,
    2.90e-09, 2.91e-09, 2.92e-09, 2.93e-09, 2.94e-09, 2.95e-09, 2.96e-09, 2.97e-09, 2.98e-09, 2.99e-09,
    3.00e-09, 3.01e-09, 3.02e-09, 3.03e-09, 3.04e-09, 3.05e-09, 3.06e-09, 3.07e-09, 3.08e-09, 3.09e-09,
    3.10e-09, 3.11e-09, 3.12e-09, 3.13e-09, 3.14e-09, 3.15e-09, 3.16e-09, 3.17e-09, 3.18e-09, 3.19e-09,
    3.20e-09, 3.21e-09, 3.22e-09, 3.23e-09, 3.24e-09, 3.25e-09, 3.26e-09, 3.27e-09, 3.28e-09, 3.29e-09,
    3.30e-09, 3.31e-09, 3.32e-09, 3.33e-09, 3.34e-09, 3.35e-09, 3.36e-09, 3.37e-09, 3.38e-09, 3.39e-09,
    3.40e-09, 3.41e-09, 3.42e-09, 3.43e-09, 3.44e-09, 3.45e-09, 3.46e-09, 3.47e-09, 3.48e-09, 3.49e-09,
    3.50e-09, 3.51e-09, 3.52e-09, 3.53e-09, 3.54e-09, 3.55e-09, 3.56e-09, 3.57e-09, 3.58e-09, 3.59e-09,
    3.60e-09, 3.61e-09, 3.62e-09, 3.63e-09, 3.64e-09, 3.65e-09, 3.66e-09, 3.67e-09, 3.68e-09, 3.69e-09,
    3.70e-09, 3.71e-09, 3.72e-09, 3.73e-09, 3.74e-09, 3.75e-09, 3.76e-09, 3.77e-09, 3.78e-09, 3.79e-09,
    3.80e-09, 3.81e-09, 3.82e-09, 3.83e-09, 3.84e-09, 3.85e-09, 3.86e-09, 3.87e-09, 3.88e-09, 3.89e-09,
    3.90e-09, 3.91e-09, 3.92e-09, 3.93e-09, 3.94e-09, 3.95e-09, 3.96e-09, 3.97e-09, 3.98e-09, 3.99e-09,
    4.00e-09
]

COMPARISON_YDATA = [
    -0.0024284965177232163, -0.0025258034975130426, -0.00262530131708786, -0.0026377144850399338,
    -0.0025113087817993512, -0.0023049105775929364, -0.002164866258496961, -0.002210337554403723,
    -0.002421010996463318, -0.002634038764233514, -0.002670978485361768, -0.002497132700280082,
    -0.0022702188097917537, -0.002216171296070835, -0.002422403117026467, -0.002732291147131853,
    -0.0028572345283530013, -0.002619087923836228, -0.0020517976540216786, -0.0011053688328478736,
    0.0010393122569787507, 0.006666469052205385, 0.019651566915632072, 0.04461128413586859,
    0.08519545808812988, 0.14222653035323018, 0.21265755815633008, 0.29003580043764277,
    0.3664290829035581, 0.43499658536662783, 0.49202318740506507, 0.5375144455519761, 0.5742125740195771,
    0.6056969954601125, 0.6346179954354738, 0.6618897886250497, 0.6870233290361907, 0.7091291639713331,
    0.7278457216269115, 0.7436566787936149, 0.7575507823740196, 0.770400317394081, 0.7825355319183765,
    0.7937626317092986, 0.8037213619113166, 0.8122623712073024, 0.8195702485755081, 0.8259914763112522,
    0.8317527556239167, 0.8368116325078789, 0.8409492100667857, 0.8440146710110406, 0.8461198220457283,
    0.8476332429139357, 0.8489845571157791, 0.8504309888236287, 0.8519580683324245, 0.8533798771743847,
    0.8545591063258953, 0.855590112028882, 0.8568261379700702, 0.8587452727607315, 0.8617507852015089,
    0.8660231765076581, 0.8714825598303119, 0.8778372763290722, 0.8846523649549297, 0.8913928914722556,
    0.8974523137928876, 0.9022116485294115, 0.9051588763182451, 0.9060426405810592, 0.9049853968733684,
    0.9024804143371944, 0.8992506999177994, 0.8960230495363251, 0.8933189748234018, 0.8913558604677805,
    0.8900921682240042, 0.889375500772014, 0.8891037630852593, 0.8893114503012348, 0.8901405253948554,
    0.8917209423725383, 0.8940354140617599, 0.8968522037541476, 0.8997744087027381, 0.902391732455248,
    0.9044623727846384, 0.9060294734279501, 0.907403727094814, 0.9090103460426376, 0.9111708241770301,
    0.913927669158789, 0.9170005727934443, 0.919894623635226, 0.922103053503182, 0.9233028774958857,
    0.9234549041770428, 0.9227781304900433, 0.9216344387551078, 0.9203942887453878, 0.9193434171427084,
    0.9186506568322375, 0.9183795287500415, 0.918514154181342, 0.9189827495905671, 0.9196803734979541,
    0.9204976918755897, 0.9213517081340168, 0.9222019946952664, 0.9230385010506021, 0.9238467048251988,
    0.9245771077275047, 0.925148508986991, 0.925491663394712, 0.9256071081432646, 0.9255943284056096,
    0.9256247956687176, 0.9258697110636565, 0.9264260589025685, 0.9272859452280833, 0.9283621447318978,
    0.9295420024680494, 0.9307247937165422, 0.9318182868042983, 0.9327116856775411, 0.9332690907797115,
    0.9333748498374097, 0.933016297491681, 0.9323460258425642, 0.9316627874974778, 0.9312967791313135,
    0.9314521490238252, 0.9320987592293082, 0.9329845856668684, 0.9337691700325887, 0.9342032836934432,
    0.9342510250905791, 0.9340873400178287, 0.9339812077295578, 0.9341410755490528, 0.9346131881286116,
    0.9352813268636024, 0.9359512304431018, 0.9364586079323102, 0.9367400718923778, 0.9368419841899855,
    0.9368822378112068, 0.9369977085139112, 0.9373013315400159, 0.9378540105016578, 0.9386472966933299,
    0.9395981218863195, 0.9405652938209238, 0.9413939556140499, 0.9419762266374518, 0.9422977585560056,
    0.9424396665646563, 0.9425292187230404, 0.9426663925527071, 0.94287183678349, 0.9430889830338611,
    0.9432362067343246, 0.943270295907327, 0.943215808583266, 0.9431414161663697, 0.9431042218262451,
    0.9431063311939107, 0.9430973876756895, 0.9430209355252819, 0.9428688902216569, 0.9427023228140816,
    0.9426220546923895, 0.9427094130112266, 0.9429782868925911, 0.9433700220537617, 0.9437916797437347,
    0.9441698038076589, 0.9444859966714728, 0.9447784343708449, 0.945118288332281, 0.9455821797022868,
    0.946234467003473, 0.9471166281040084, 0.9482323576767672, 0.9495248007161452, 0.9508590771309924,
    0.9520320127834603, 0.9528204486751757, 0.9530545227208639, 0.9526811613934134, 0.9517830884035554,
    0.9505423128982529, 0.9491689600508987, 0.9478346040889727, 0.9466419073614913, 0.9456358413896552,
    0.9448356790246695, 0.9442590560825345, 0.9439228089907333
]

def test_s4p():
    # read S4P file
    t_step, v_step = s4p_to_step(TOP_DIR / COMPARISON_FILE, COMPARISON_TOVER, COMPARISON_TDUR)

    # build function
    f_step = interp1d(t_step, v_step)

    # calculate step response at known points
    v_meas = f_step(COMPARISON_XDATA)

    # perform the comparison
    assert np.all(np.isclose(v_meas, COMPARISON_YDATA))

from pathlib import Path
from scipy.interpolate import interp1d
import numpy as np

from msdsl.rf import s4p_to_step

THIS_DIR = Path(__file__).resolve().parent
TOP_DIR = THIS_DIR.parent.parent

COMPARISON_FILE = 'peters_01_0605_B1_thru.s4p'

COMPARISON_TOVER = 0.1e-12

COMPARISON_TDUR = 10e-9

COMPARISON_XDATA = [
    2.00e-09, 2.01e-09, 2.02e-09, 2.03e-09, 2.04e-09, 2.05e-09, 2.06e-09, 2.07e-09, 2.08e-09, 2.09e-09,
    2.10e-09, 2.11e-09, 2.12e-09, 2.13e-09, 2.14e-09, 2.15e-09, 2.16e-09, 2.17e-09, 2.18e-09, 2.19e-09,
    2.20e-09, 2.21e-09, 2.22e-09, 2.23e-09, 2.24e-09, 2.25e-09, 2.26e-09, 2.27e-09, 2.28e-09, 2.29e-09,
    2.30e-09, 2.31e-09, 2.32e-09, 2.33e-09, 2.34e-09, 2.35e-09, 2.36e-09, 2.37e-09, 2.38e-09, 2.39e-09,
    2.40e-09, 2.41e-09, 2.42e-09, 2.43e-09, 2.44e-09, 2.45e-09, 2.46e-09, 2.47e-09, 2.48e-09, 2.49e-09,
    2.50e-09, 2.51e-09, 2.52e-09, 2.53e-09, 2.54e-09, 2.55e-09, 2.56e-09, 2.57e-09, 2.58e-09, 2.59e-09,
    2.60e-09, 2.61e-09, 2.62e-09, 2.63e-09, 2.64e-09, 2.65e-09, 2.66e-09, 2.67e-09, 2.68e-09, 2.69e-09,
    2.70e-09, 2.71e-09, 2.72e-09, 2.73e-09, 2.74e-09, 2.75e-09, 2.76e-09, 2.77e-09, 2.78e-09, 2.79e-09,
    2.80e-09, 2.81e-09, 2.82e-09, 2.83e-09, 2.84e-09, 2.85e-09, 2.86e-09, 2.87e-09, 2.88e-09, 2.89e-09,
    2.90e-09, 2.91e-09, 2.92e-09, 2.93e-09, 2.94e-09, 2.95e-09, 2.96e-09, 2.97e-09, 2.98e-09, 2.99e-09,
    3.00e-09, 3.01e-09, 3.02e-09, 3.03e-09, 3.04e-09, 3.05e-09, 3.06e-09, 3.07e-09, 3.08e-09, 3.09e-09,
    3.10e-09, 3.11e-09, 3.12e-09, 3.13e-09, 3.14e-09, 3.15e-09, 3.16e-09, 3.17e-09, 3.18e-09, 3.19e-09,
    3.20e-09, 3.21e-09, 3.22e-09, 3.23e-09, 3.24e-09, 3.25e-09, 3.26e-09, 3.27e-09, 3.28e-09, 3.29e-09,
    3.30e-09, 3.31e-09, 3.32e-09, 3.33e-09, 3.34e-09, 3.35e-09, 3.36e-09, 3.37e-09, 3.38e-09, 3.39e-09,
    3.40e-09, 3.41e-09, 3.42e-09, 3.43e-09, 3.44e-09, 3.45e-09, 3.46e-09, 3.47e-09, 3.48e-09, 3.49e-09,
    3.50e-09, 3.51e-09, 3.52e-09, 3.53e-09, 3.54e-09, 3.55e-09, 3.56e-09, 3.57e-09, 3.58e-09, 3.59e-09,
    3.60e-09, 3.61e-09, 3.62e-09, 3.63e-09, 3.64e-09, 3.65e-09, 3.66e-09, 3.67e-09, 3.68e-09, 3.69e-09,
    3.70e-09, 3.71e-09, 3.72e-09, 3.73e-09, 3.74e-09, 3.75e-09, 3.76e-09, 3.77e-09, 3.78e-09, 3.79e-09,
    3.80e-09, 3.81e-09, 3.82e-09, 3.83e-09, 3.84e-09, 3.85e-09, 3.86e-09, 3.87e-09, 3.88e-09, 3.89e-09,
    3.90e-09, 3.91e-09, 3.92e-09, 3.93e-09, 3.94e-09, 3.95e-09, 3.96e-09, 3.97e-09, 3.98e-09, 3.99e-09,
    4.00e-09
]

COMPARISON_YDATA = [
    -0.0024284965177232163, -0.0025258034975130426, -0.00262530131708786, -0.0026377144850399338,
    -0.0025113087817993512, -0.0023049105775929364, -0.002164866258496961, -0.002210337554403723,
    -0.002421010996463318, -0.002634038764233514, -0.002670978485361768, -0.002497132700280082,
    -0.0022702188097917537, -0.002216171296070835, -0.002422403117026467, -0.002732291147131853,
    -0.0028572345283530013, -0.002619087923836228, -0.0020517976540216786, -0.0011053688328478736,
    0.0010393122569787507, 0.006666469052205385, 0.019651566915632072, 0.04461128413586859,
    0.08519545808812988, 0.14222653035323018, 0.21265755815633008, 0.29003580043764277,
    0.3664290829035581, 0.43499658536662783, 0.49202318740506507, 0.5375144455519761, 0.5742125740195771,
    0.6056969954601125, 0.6346179954354738, 0.6618897886250497, 0.6870233290361907, 0.7091291639713331,
    0.7278457216269115, 0.7436566787936149, 0.7575507823740196, 0.770400317394081, 0.7825355319183765,
    0.7937626317092986, 0.8037213619113166, 0.8122623712073024, 0.8195702485755081, 0.8259914763112522,
    0.8317527556239167, 0.8368116325078789, 0.8409492100667857, 0.8440146710110406, 0.8461198220457283,
    0.8476332429139357, 0.8489845571157791, 0.8504309888236287, 0.8519580683324245, 0.8533798771743847,
    0.8545591063258953, 0.855590112028882, 0.8568261379700702, 0.8587452727607315, 0.8617507852015089,
    0.8660231765076581, 0.8714825598303119, 0.8778372763290722, 0.8846523649549297, 0.8913928914722556,
    0.8974523137928876, 0.9022116485294115, 0.9051588763182451, 0.9060426405810592, 0.9049853968733684,
    0.9024804143371944, 0.8992506999177994, 0.8960230495363251, 0.8933189748234018, 0.8913558604677805,
    0.8900921682240042, 0.889375500772014, 0.8891037630852593, 0.8893114503012348, 0.8901405253948554,
    0.8917209423725383, 0.8940354140617599, 0.8968522037541476, 0.8997744087027381, 0.902391732455248,
    0.9044623727846384, 0.9060294734279501, 0.907403727094814, 0.9090103460426376, 0.9111708241770301,
    0.913927669158789, 0.9170005727934443, 0.919894623635226, 0.922103053503182, 0.9233028774958857,
    0.9234549041770428, 0.9227781304900433, 0.9216344387551078, 0.9203942887453878, 0.9193434171427084,
    0.9186506568322375, 0.9183795287500415, 0.918514154181342, 0.9189827495905671, 0.9196803734979541,
    0.9204976918755897, 0.9213517081340168, 0.9222019946952664, 0.9230385010506021, 0.9238467048251988,
    0.9245771077275047, 0.925148508986991, 0.925491663394712, 0.9256071081432646, 0.9255943284056096,
    0.9256247956687176, 0.9258697110636565, 0.9264260589025685, 0.9272859452280833, 0.9283621447318978,
    0.9295420024680494, 0.9307247937165422, 0.9318182868042983, 0.9327116856775411, 0.9332690907797115,
    0.9333748498374097, 0.933016297491681, 0.9323460258425642, 0.9316627874974778, 0.9312967791313135,
    0.9314521490238252, 0.9320987592293082, 0.9329845856668684, 0.9337691700325887, 0.9342032836934432,
    0.9342510250905791, 0.9340873400178287, 0.9339812077295578, 0.9341410755490528, 0.9346131881286116,
    0.9352813268636024, 0.9359512304431018, 0.9364586079323102, 0.9367400718923778, 0.9368419841899855,
    0.9368822378112068, 0.9369977085139112, 0.9373013315400159, 0.9378540105016578, 0.9386472966933299,
    0.9395981218863195, 0.9405652938209238, 0.9413939556140499, 0.9419762266374518, 0.9422977585560056,
    0.9424396665646563, 0.9425292187230404, 0.9426663925527071, 0.94287183678349, 0.9430889830338611,
    0.9432362067343246, 0.943270295907327, 0.943215808583266, 0.9431414161663697, 0.9431042218262451,
    0.9431063311939107, 0.9430973876756895, 0.9430209355252819, 0.9428688902216569, 0.9427023228140816,
    0.9426220546923895, 0.9427094130112266, 0.9429782868925911, 0.9433700220537617, 0.9437916797437347,
    0.9441698038076589, 0.9444859966714728, 0.9447784343708449, 0.945118288332281, 0.9455821797022868,
    0.946234467003473, 0.9471166281040084, 0.9482323576767672, 0.9495248007161452, 0.9508590771309924,
    0.9520320127834603, 0.9528204486751757, 0.9530545227208639, 0.9526811613934134, 0.9517830884035554,
    0.9505423128982529, 0.9491689600508987, 0.9478346040889727, 0.9466419073614913, 0.9456358413896552,
    0.9448356790246695, 0.9442590560825345, 0.9439228089907333
]

def test_s4p():
    # read S4P file
    t_step, v_step = s4p_to_step(TOP_DIR / COMPARISON_FILE, COMPARISON_TOVER, COMPARISON_TDUR)

    # build function
    f_step = interp1d(t_step, v_step)

    # calculate step response at known points
    v_meas = f_step(COMPARISON_XDATA)

    # perform the comparison
    assert np.all(np.isclose(v_meas, COMPARISON_YDATA))

# read S4P file # build function # calculate step response at known points # perform the comparison