from glob import glob

import ruamel_yaml as yml
import numpy as np
import xarray as xr

from tests import CloudTests
import read_data as rd
import scene as scn

# import pytest

# #################################################################### #
# TEST CASE

# data:
_datapath = '/ships19/hercules/pveglio/mvcm_viirs_hires'
_fname_mod02 = glob(f'{_datapath}/VNP02MOD.A2022173.1454.001.*.uwssec_bowtie_restored.nc')[0]
_fname_mod03 = glob(f'{_datapath}/VNP03MOD.A2022173.1454.001.*.uwssec.nc')[0]
_fname_img02 = glob(f'{_datapath}/VNP02IMG.A2022173.1454.001.*.uwssec_bowtie_restored.nc')[0]
_fname_img03 = glob(f'{_datapath}/VNP03IMG.A2022173.1454.001.*.uwssec.nc')[0]

# thresholds:
_threshold_file = '/home/pveglio/mvcm_leo/thresholds/new_thresholds.mvcm.snpp.v1.0.0.yaml'

# ancillary files:
_geos_atm_1 = 'GEOS.fpit.asm.inst3_2d_asm_Nx.GEOS5124.20220622_1200.V01.nc4'
_geos_atm_2 = 'GEOS.fpit.asm.inst3_2d_asm_Nx.GEOS5124.20220622_1500.V01.nc4'
_geos_land = 'GEOS.fpit.asm.tavg1_2d_lnd_Nx.GEOS5124.20220622_1430.V01.nc4'
_geos_ocean = 'GEOS.fpit.asm.tavg1_2d_ocn_Nx.GEOS5124.20220622_1430.V01.nc4'
_geos_constants = 'GEOS.fp.asm.const_2d_asm_Nx.00000000_0000.V01.nc4'
_ndvi_file = 'NDVI.FM.c004.v2.0.WS.00-04.177.hdf'
_sst_file = 'oisst.20220622'
_eco_file = 'goge1_2_img.v1'

# #################################################################### #


def load_paths():

    file_names = {'MOD02': f'{_fname_mod02}',
                  'MOD03': f'{_fname_mod03}',
                  'IMG02': f'{_fname_img02}',
                  'IMG03': f'{_fname_img03}',
                  'GEOS_atm_1': f'{_geos_atm_1}',
                  'GEOS_atm_2': f'{_geos_atm_2}',
                  'GEOS_land': f'{_geos_land}',
                  'GEOS_ocean': f'{_geos_ocean}',
                  'GEOS_constants': f'{_geos_constants}',
                  'NDVI': f'{_ndvi_file}',
                  'SST': f'{_sst_file}',
                  'ANC_DIR': f'{_datapath}/ancillary'
                  }

    return file_names


def test_read_data(file_names):

    with open(_threshold_file) as f:
        text = f.read()
    thresholds = yml.safe_load(text)

    sunglint_angle = thresholds['Sun_Glint']['bounds'][3]

    viirs_xr = rd.get_data(file_names, sunglint_angle)

    return viirs_xr


def test_scene(viirs_xr):

    scene_xr = xr.Dataset()

    for s in scn._scene_list:
        scene_xr[s] = (('number_of_lines', 'number_of_pixels'), scn.scene_id[s])
    scene_xr['latitude'] = viirs_xr.latitude
    scene_xr['longitude'] = viirs_xr.longitude

    viirs_data = xr.Dataset(viirs_xr, coords=scene_xr)
    viirs_data.drop_vars(['latitude', 'longitude'])

    return viirs_data


def test_test(viirs_data):

    with open(_threshold_file) as f:
        text = f.read()
    thresholds = yml.safe_load(text)

    cmin = np.zeros(viirs_data.M01.shape)

    Ocean_Day = CloudTests(viirs_data, 'Ocean_Day', thresholds)
    cmin = Ocean_Day.single_threshold_test('11BT_Test', viirs_data.M15.values, cmin)

    return cmin