#!/usr/bin/env python3
"""
Gridded GLM basic functionality tests
"""

import subprocess
import os
import warnings

from testlib.common import TESTDIR, MINUTE_GRIDDER
from testlib.common import check_fileglob

def test_simple_success():
    completed_process = subprocess.run(['python',
                                        MINUTE_GRIDDER,
                                        "--goes-sector", "full",
                                        os.path.join(TESTDIR, "test-inputs/OR_GLM-L2-LCFA_G16_s20220740000000_e20220740000200_c20220740000227.nc"),
                                        os.path.join(TESTDIR, "test-inputs/OR_GLM-L2-LCFA_G16_s20220740000200_e20220740000400_c20220740000428.nc"),
                                        os.path.join(TESTDIR, "test-inputs/OR_GLM-L2-LCFA_G16_s20220740000400_e20220740001000_c20220740001027.nc"),
                                        ])
    assert completed_process.returncode == 0
    assert(check_fileglob("CG_GLM-L2-GLMF-M3_G16_s20220740000000_e20220740001000_c*.nc"))

# formerly the issue5 realtime success test
def test_realtime():
    completed_process = subprocess.run(['python',
                                        MINUTE_GRIDDER,
                                        "--goes-sector", "conus",
                                        "--create-tiles",
                                        "--realtime",
                                        os.path.join(TESTDIR, "test-inputs/OR_GLM-L2-LCFA_G16_s20220740002400_e20220740003000_c20220740003018.nc"),
                                        ])
    assert completed_process.returncode == 0
    assert(check_fileglob("CG_GLM-L2-GLMC-M3_G16_s20220740002000_e20220740003000_c*", 1))
    assert(check_fileglob("CSPP_OR_GLM-L2-GLMC-M3_G16_T*.nc", 4))
    assert(check_fileglob("CSPP_OR_GLM-L2-GLMC-M3_G16_T08_20220315000300.nc", 1))
    assert(check_fileglob("CSPP_OR_GLM-L2-GLMC-M3_G16_T05_20220315000300.nc", 1))
    assert(check_fileglob("CSPP_OR_GLM-L2-GLMC-M3_G16_T04_20220315000300.nc", 1))
    assert(check_fileglob("CSPP_OR_GLM-L2-GLMC-M3_G16_T07_20220315000300.nc", 1))

# tests for the new statistics file
def test_statistics_file():
    completed_process = subprocess.run(['python',
                                        MINUTE_GRIDDER,
                                        "--goes-sector", "full",
                                        "--create-tiles",
                                        "--realtime",
                                        os.path.join(TESTDIR, "test-inputs/OR_GLM-L2-LCFA_G16_s20220740002400_e20220740003000_c20220740003018.nc"),
                                        ])
    assert completed_process.returncode == 0
    assert(check_fileglob("CG_GLM-L2-GLMF-M3_G16_s20220740002000_e20220740003000_c*", 1))
    assert(check_fileglob("CSPP_OR_GLM-L2-GLMF-M3_G16_T08_20220315000300.nc", 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T33_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T34_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T28_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T53_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T35_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T14_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T08_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T46_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T20_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T38_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T42_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T15_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T47_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T09_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T21_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T41_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T27_20220315000300.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T40_20220315000300.nc', 1))
    assert(check_fileglob("CSPP_OR_GLM-L2-GLMF-M3_G16_statistics_e20220315000300.nc", 1))

def test_g16():
    completed_process = subprocess.run(['python',
                                        MINUTE_GRIDDER,
                                        "--create-tiles",
                                        os.path.join(TESTDIR, "test-inputs/OR_GLM-L2-LCFA_G16_s20220740000000_e20220740000200_c20220740000227.nc"),
                                        os.path.join(TESTDIR, "test-inputs/OR_GLM-L2-LCFA_G16_s20220740000200_e20220740000400_c20220740000428.nc"),
                                        os.path.join(TESTDIR, "test-inputs/OR_GLM-L2-LCFA_G16_s20220740000400_e20220740001000_c20220740001027.nc"),
                                        ])
    assert completed_process.returncode == 0
    assert(check_fileglob("CG_GLM-L2-GLMF-M3_G16_*.nc", 1))
    assert(check_fileglob("CSPP_OR_GLM-L2-GLMF-M3_G16_T*.nc", 17))

    assert(check_fileglob('CG_GLM-L2-GLMF-M3_G16_s20220740000000_e20220740001000_c*.nc', 1)) 
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T27_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T41_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T40_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T20_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T46_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T08_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T14_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T42_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T38_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T21_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T47_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T15_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T28_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T34_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T35_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T36_20220315000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G16_T33_20220315000100.nc', 1))

def test_g17():
    completed_process = subprocess.run(['python',
                                        MINUTE_GRIDDER,
                                        "--create-tiles",
                                        os.path.join(TESTDIR, "test-inputs/OR_GLM-L2-LCFA_G17_s20222631921000_e20222631921200_c20222631921214.nc"),
                                        os.path.join(TESTDIR, "test-inputs/OR_GLM-L2-LCFA_G17_s20222631921200_e20222631921400_c20222631921418.nc"),
                                        os.path.join(TESTDIR, "test-inputs/OR_GLM-L2-LCFA_G17_s20222631921400_e20222631922000_c20222631922019.nc"),
                                        ])
    assert completed_process.returncode == 0
    assert(check_fileglob("CG_GLM-L2-GLMF-M3_G17_*.nc", 1))
    assert(check_fileglob("CSPP_OR_GLM-L2-GLMF-M3_G17_T*.nc", 15))

    assert(check_fileglob('CG_GLM-L2-GLMF-M3_G17_s20222631921000_e20222631922000_c*.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G17_T49_20220920192200.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G17_T19_20220920192200.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G17_T05_20220920192200.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G17_T18_20220920192200.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G17_T30_20220920192200.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G17_T11_20220920192200.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G17_T43_20220920192200.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G17_T25_20220920192200.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G17_T14_20220920192200.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G17_T08_20220920192200.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G17_T24_20220920192200.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G17_T13_20220920192200.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G17_T41_20220920192200.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G17_T17_20220920192200.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G17_T23_20220920192200.nc', 1))

def test_g18():
    inputs = [os.path.join(TESTDIR, "test-inputs/OR_GLM-L2-LCFA_G18_s20222210000000_e20222210000200_c20222210000225.nc"),
              os.path.join(TESTDIR, "test-inputs/OR_GLM-L2-LCFA_G18_s20222210000200_e20222210000400_c20222210000424.nc"),
              os.path.join(TESTDIR, "test-inputs/OR_GLM-L2-LCFA_G18_s20222210000400_e20222210001000_c20222210001027.nc")]
    for i in inputs:
        if not os.path.exists(i):
            warnings.warn(UserWarning("GOES-18 test files are not available in this environment. GOES-18 functionality has not been tested."))
            return

    completed_process = subprocess.run(['python',
                                        MINUTE_GRIDDER,
                                        "--create-tiles",
                                        inputs[0],
                                        inputs[1],
                                        inputs[2],
                                        ])
    assert completed_process.returncode == 0
    assert(check_fileglob("CG_GLM-L2-GLMF-M3_G18_*.nc", 1))
    assert(check_fileglob("CSPP_OR_GLM-L2-GLMF-M3_G18_T*.nc", 15))

    assert(check_fileglob("CG_GLM-L2-GLMF-M3_G18_s20222210000000_e20222210001000_c*.nc", 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G18_T04_20220809000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G18_T30_20220809000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G18_T18_20220809000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G18_T29_20220809000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G18_T49_20220809000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G18_T16_20220809000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G18_T27_20220809000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G18_T23_20220809000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G18_T17_20220809000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G18_T20_20220809000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G18_T08_20220809000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G18_T11_20220809000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G18_T21_20220809000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G18_T24_20220809000100.nc', 1))
    assert(check_fileglob('CSPP_OR_GLM-L2-GLMF-M3_G18_T10_20220809000100.nc', 1))