import numpy as np
import photon_stream as ps
import photon_stream_production as psp
import pkg_resources
import os

runinfo_path = pkg_resources.resource_filename(
    'photon_stream_production',
    os.path.join('tests', 'resources', 'runinfo_20161115_to_20170103.csv')
)

drs_fRunID_for_obs_run = psp.drs_run._drs_fRunID_for_obs_run


def test_drs_run_assignment():

    ri = psp.runinfo.read(runinfo_path)
    ro = psp.drs_run.assign_drs_runs(ri)

    ri = ri[(ri.fNight > 20161229) & (ri.fNight <= 20170102)]
    ro = ro[(ro.fNight > 20161229) & (ro.fNight <= 20170102)]

    for i, row in ri.iterrows():
        assert row.fNight == ro.loc[i, 'fNight']
        assert row.fRunID == ro.loc[i, 'fRunID']

        if row.fRunTypeKey == psp.runinfo.OBSERVATION_RUN_TYPE_KEY:

            first_method_drs_run_id = drs_fRunID_for_obs_run(
                runinfo=ri,
                fNight=row.fNight,
                fRunID=row.fRunID
            )
            second_method_drs_run_id = ro.loc[i, 'DrsRunID']

            if np.isnan(first_method_drs_run_id):
                assert np.isnan(second_method_drs_run_id)
            else:
                assert first_method_drs_run_id == second_method_drs_run_id

[((9, 4, 9, 74), 'os.path.join', 'os.path.join', ({(9, 17, 9, 24): '"""tests"""', (9, 26, 9, 37): '"""resources"""', (9, 39, 9, 73): '"""runinfo_20161115_to_20170103.csv"""'}, {}), "('tests', 'resources', 'runinfo_20161115_to_20170103.csv')", False, 'import os\n'), ((17, 9, 17, 39), 'photon_stream_production.runinfo.read', 'psp.runinfo.read', ({(17, 26, 17, 38): 'runinfo_path'}, {}), '(runinfo_path)', True, 'import photon_stream_production as psp\n'), ((18, 9, 18, 40), 'photon_stream_production.drs_run.assign_drs_runs', 'psp.drs_run.assign_drs_runs', ({(18, 37, 18, 39): 'ri'}, {}), '(ri)', True, 'import photon_stream_production as psp\n'), ((36, 15, 36, 48), 'numpy.isnan', 'np.isnan', ({(36, 24, 36, 47): 'first_method_drs_run_id'}, {}), '(first_method_drs_run_id)', True, 'import numpy as np\n'), ((37, 23, 37, 57), 'numpy.isnan', 'np.isnan', ({(37, 32, 37, 56): 'second_method_drs_run_id'}, {}), '(second_method_drs_run_id)', True, 'import numpy as np\n')]