# Path: src/vegindex/quantile.py
# def quantile(x, q, qtype=7, issorted=False):
#     """
#     Args:
#         x - input data
#         q - quantile
#         qtype - algorithm
#         issorted- True if x already sorted.
# 
#     Compute quantiles from input array x given q.For median,
#     specify q=0.5.
# 
#     References:
#         http://reference.wolfram.com/mathematica/ref/Quantile.html
#         http://wiki.r-project.org/rwiki/doku.php?id=rdoc:stats:quantile
# 
#     Author:
#         Ernesto P.Adorio Ph.D.
#         UP Extension Program in Pampanga, Clark Field.
#     """
# 
#     if not issorted:
#         y = sorted(x)
#     else:
#         y = x
#         if not (1 <= qtype <= 9):
#             return None  # error!
# 
#     # Parameters for the Hyndman and Fan algorithm
#     abcd = [
#         # inverse empirical distrib.function., R type 1
#         (0, 0, 1, 0),
#         # similar to type 1, averaged, R type 2
#         (0.5, 0, 1, 0),
#         # nearest order statistic,(SAS) R type 3
#         (0.5, 0, 0, 0),
#         # California linear interpolation, R type 4
#         (0, 0, 0, 1),
#         # hydrologists method, R type 5
#         (0.5, 0, 0, 1),
#         # mean-based estimate(Weibull method), (SPSS, Minitab), type 6
#         (0, 1, 0, 1),
#         # mode-based method,(S, S-Plus), R type 7
#         (1, -1, 0, 1),
#         # median-unbiased ,  R type 8
#         (1.0 / 3, 1.0 / 3, 0, 1),
#         # normal-unbiased, R type 9.
#         (3 / 8.0, 0.25, 0, 1),
#     ]
# 
#     a, b, c, d = abcd[qtype - 1]
#     n = len(x)
#     g, j = modf(a + (n + b) * q - 1)
#     if j < 0:
#         return y[0]
#     elif j >= n:
#         # oct. 8, 2010 y[n]???!! uncaught  off by 1 error!!!
#         return y[n - 1]
# 
#     j = int(floor(j))
#     if g == 0:
#         return y[j]
#     else:
#         return y[j] + (y[j + 1] - y[j]) * (c + d * g)


import argparse
import os
import numpy as np
import vegindex as vi
from configparser import ConfigParser as configparser
from datetime import datetime
from datetime import time
from datetime import timedelta
from vegindex.gcctimeseries import GCCTimeSeries
from vegindex.vegindex import daterange2
from vegindex.vegindex import get_roi_timeseries
from .quantile import quantile
            snow_flag = ND_INT
            outlierflag_gcc_mean = ND_INT
            outlierflag_gcc_50 = ND_INT
            outlierflag_gcc_75 = ND_INT
            outlierflag_gcc_90 = ND_INT

        # stats for this period should be complete - only
        # snow flags are missing data
        else:
            # find nearest image to midday (noon) on mid-interval date
            mi_ndx = midday_delta_vals.index(min(midday_delta_vals))
            midday_filename = filenames[mi_ndx]
            midday_r = r_dn_vals[mi_ndx]
            midday_g = g_dn_vals[mi_ndx]
            midday_b = b_dn_vals[mi_ndx]
            midday_gcc = gcc_vals[mi_ndx]
            midday_rcc = rcc_vals[mi_ndx]

            # get stats for this time interval
            image_count = img_cnt
            r_mean = np.nanmean(r_dn_vals)
            r_std = np.nanstd(r_dn_vals)
            g_mean = np.nanmean(g_dn_vals)
            g_std = np.nanstd(g_dn_vals)
            b_mean = np.nanmean(b_dn_vals)
            b_std = np.nanstd(b_dn_vals)
            gcc_mean = np.nanmean(gcc_vals)
            gcc_std = np.nanstd(gcc_vals)
            gcc_50 = quantile(gcc_vals, 0.5)
            gcc_75 = quantile(gcc_vals, 0.75)

# Path: src/vegindex/quantile.py
# def quantile(x, q, qtype=7, issorted=False):
#     """
#     Args:
#         x - input data
#         q - quantile
#         qtype - algorithm
#         issorted- True if x already sorted.
# 
#     Compute quantiles from input array x given q.For median,
#     specify q=0.5.
# 
#     References:
#         http://reference.wolfram.com/mathematica/ref/Quantile.html
#         http://wiki.r-project.org/rwiki/doku.php?id=rdoc:stats:quantile
# 
#     Author:
#         Ernesto P.Adorio Ph.D.
#         UP Extension Program in Pampanga, Clark Field.
#     """
# 
#     if not issorted:
#         y = sorted(x)
#     else:
#         y = x
#         if not (1 <= qtype <= 9):
#             return None  # error!
# 
#     # Parameters for the Hyndman and Fan algorithm
#     abcd = [
#         # inverse empirical distrib.function., R type 1
#         (0, 0, 1, 0),
#         # similar to type 1, averaged, R type 2
#         (0.5, 0, 1, 0),
#         # nearest order statistic,(SAS) R type 3
#         (0.5, 0, 0, 0),
#         # California linear interpolation, R type 4
#         (0, 0, 0, 1),
#         # hydrologists method, R type 5
#         (0.5, 0, 0, 1),
#         # mean-based estimate(Weibull method), (SPSS, Minitab), type 6
#         (0, 1, 0, 1),
#         # mode-based method,(S, S-Plus), R type 7
#         (1, -1, 0, 1),
#         # median-unbiased ,  R type 8
#         (1.0 / 3, 1.0 / 3, 0, 1),
#         # normal-unbiased, R type 9.
#         (3 / 8.0, 0.25, 0, 1),
#     ]
# 
#     a, b, c, d = abcd[qtype - 1]
#     n = len(x)
#     g, j = modf(a + (n + b) * q - 1)
#     if j < 0:
#         return y[0]
#     elif j >= n:
#         # oct. 8, 2010 y[n]???!! uncaught  off by 1 error!!!
#         return y[n - 1]
# 
#     j = int(floor(j))
#     if g == 0:
#         return y[j]
#     else:
#         return y[j] + (y[j + 1] - y[j]) * (c + d * g)


# Path: src/vegindex/generate_summary_timeseries.py
import argparse
import os
import numpy as np
import vegindex as vi
from configparser import ConfigParser as configparser
from datetime import datetime
from datetime import time
from datetime import timedelta
from vegindex.gcctimeseries import GCCTimeSeries
from vegindex.vegindex import daterange2
from vegindex.vegindex import get_roi_timeseries
from .quantile import quantile

            snow_flag = ND_INT
            outlierflag_gcc_mean = ND_INT
            outlierflag_gcc_50 = ND_INT
            outlierflag_gcc_75 = ND_INT
            outlierflag_gcc_90 = ND_INT

        # stats for this period should be complete - only
        # snow flags are missing data
        else:
            # find nearest image to midday (noon) on mid-interval date
            mi_ndx = midday_delta_vals.index(min(midday_delta_vals))
            midday_filename = filenames[mi_ndx]
            midday_r = r_dn_vals[mi_ndx]
            midday_g = g_dn_vals[mi_ndx]
            midday_b = b_dn_vals[mi_ndx]
            midday_gcc = gcc_vals[mi_ndx]
            midday_rcc = rcc_vals[mi_ndx]

            # get stats for this time interval
            image_count = img_cnt
            r_mean = np.nanmean(r_dn_vals)
            r_std = np.nanstd(r_dn_vals)
            g_mean = np.nanmean(g_dn_vals)
            g_std = np.nanstd(g_dn_vals)
            b_mean = np.nanmean(b_dn_vals)
            b_std = np.nanstd(b_dn_vals)
            gcc_mean = np.nanmean(gcc_vals)
            gcc_std = np.nanstd(gcc_vals)
            gcc_50 = quantile(gcc_vals, 0.5)
            gcc_75 = quantile(gcc_vals, 0.75)

            gcc_90 = quantile(gcc_vals, 0.9)