//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
const std::vector<size_t>& RigCaseCellResultsData::cellScalarValuesHistogram(size_t scalarResultIndex)
{
    CVF_ASSERT(scalarResultIndex < resultCount());

    // Extend array and cache vars

    if (scalarResultIndex >= m_histograms.size() )
    {
        m_histograms.resize(resultCount());
        m_p10p90.resize(resultCount(), std::make_pair(HUGE_VAL, HUGE_VAL));
    }

    if (m_histograms[scalarResultIndex].size())
    {
        return m_histograms[scalarResultIndex];

    }

    double min;
    double max;
    size_t nBins = 100;
    this->minMaxCellScalarValues( scalarResultIndex, min, max );
    RigHistogramCalculator histCalc(min, max, nBins, &m_histograms[scalarResultIndex]);

    if (scalarResultIndex == m_combinedTransmissibilityResultIndex)
    {
        size_t tranX, tranY, tranZ;
        if (findTransmissibilityResults(tranX, tranY, tranZ))
        {
            for (size_t tsIdx = 0; tsIdx < this->timeStepCount(scalarResultIndex); tsIdx++)
            {
                histCalc.addData(m_cellScalarResults[tranX][tsIdx]);
                histCalc.addData(m_cellScalarResults[tranY][tsIdx]);
                histCalc.addData(m_cellScalarResults[tranZ][tsIdx]);
            } 
        }
    }
    else
    {
        for (size_t tsIdx = 0; tsIdx < this->timeStepCount(scalarResultIndex); tsIdx++)
        {
            std::vector<double>& values = m_cellScalarResults[scalarResultIndex][tsIdx];

            histCalc.addData(values);
        } 
    }

    m_p10p90[scalarResultIndex].first = histCalc.calculatePercentil(0.1);
    m_p10p90[scalarResultIndex].second = histCalc.calculatePercentil(0.9);

    return m_histograms[scalarResultIndex];
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
const std::vector<size_t>& RigStatisticsDataCache::cellScalarValuesHistogram()
{
	if (m_histogram.size() == 0)
	{
		double min;
		double max;
		size_t nBins = 100;
		this->minMaxCellScalarValues(min, max);

		RigHistogramCalculator histCalc(min, max, nBins, &m_histogram);

		m_statisticsCalculator->addDataToHistogramCalculator(histCalc);

		m_p10 = histCalc.calculatePercentil(0.1);
		m_p90 = histCalc.calculatePercentil(0.9);
	}

	return m_histogram;
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(RigStatisticsMath, HistogramPercentiles)
{
    std::vector<double> values;
    values.push_back(HUGE_VAL);
    values.push_back(2788.2723335651900);
    values.push_back(-22481.0927881701000);
    values.push_back(68778.6851686236000);
    values.push_back(-76092.8157632591000);
    values.push_back(6391.97999909729003);
    values.push_back(65930.1200169780000);
    values.push_back(-27696.2320267235000);
    values.push_back(HUGE_VAL);
    values.push_back(HUGE_VAL);
    values.push_back(96161.7546348456000);
    values.push_back(73875.6716288563000);
    values.push_back(80720.4378655615000);
    values.push_back(-98649.8109937874000);
    values.push_back(99372.9362079615000);
    values.push_back(HUGE_VAL);
    values.push_back(-57020.4389966513000);


    double min, max, range, mean, stdev;
    RigStatisticsMath::calculateBasicStatistics(values, &min, &max, &range, &mean, &stdev);

    std::vector<size_t> histogram;
    RigHistogramCalculator histCalc(min, max, 100, &histogram);
    histCalc.addData(values);
    std::vector<double> pVals;
    double p10, p50, p90;
    p10 = histCalc.calculatePercentil(0.1);
    p50 = histCalc.calculatePercentil(0.5);
    p90 = histCalc.calculatePercentil(0.9);

    EXPECT_DOUBLE_EQ( -76273.240559989776, p10);
    EXPECT_DOUBLE_EQ( 5312.1312871307755  , p50);
    EXPECT_DOUBLE_EQ( 94818.413022321271 , p90);
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
const std::vector<size_t>& RigReservoirCellResults::cellScalarValuesHistogram(size_t scalarResultIndex)
{
    CVF_ASSERT(scalarResultIndex < resultCount());

    // Extend array and cache vars

    if (scalarResultIndex >= m_histograms.size() )
    {
        m_histograms.resize(resultCount());
        m_p10p90.resize(resultCount(), std::make_pair(HUGE_VAL, HUGE_VAL));
    }

    if (m_histograms[scalarResultIndex].size())
    {
        return m_histograms[scalarResultIndex];

    }

    double min;
    double max;
    size_t nBins = 100;
    this->minMaxCellScalarValues( scalarResultIndex, min, max );
    RigHistogramCalculator histCalc(min, max, nBins, &m_histograms[scalarResultIndex]);

    for (size_t tsIdx = 0; tsIdx < this->timeStepCount(scalarResultIndex); tsIdx++)
    {
        std::vector<double>& values = m_cellScalarResults[scalarResultIndex][tsIdx];

        histCalc.addData(values);
    } 

    m_p10p90[scalarResultIndex].first = histCalc.calculatePercentil(0.1);
    m_p10p90[scalarResultIndex].second = histCalc.calculatePercentil(0.9);

    return m_histograms[scalarResultIndex];
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RimEclipseStatisticsCaseEvaluator::evaluateForResults(const QList<ResSpec>& resultSpecification)
{
    CVF_ASSERT(m_destinationCase);
    
    // First build the destination result data structures to receive the statistics

    for (int i = 0; i < resultSpecification.size(); i++)
    {
        RifReaderInterface::PorosityModelResultType poroModel = resultSpecification[i].m_poroModel;
        RimDefines::ResultCatType resultType = resultSpecification[i].m_resType;
        QString resultName = resultSpecification[i].m_resVarName;

        size_t activeCellCount = m_destinationCase->activeCellInfo(poroModel)->reservoirActiveCellCount();
        RigCaseCellResultsData* destCellResultsData = m_destinationCase->results(poroModel);

        // Placeholder data used to be created here,
        // this is now moved to RimIdenticalGridCaseGroup::loadMainCaseAndActiveCellInfo()


        // Create new result data structures to contain the statistical values
        std::vector<QString> statisticalResultNames;

        statisticalResultNames.push_back(createResultNameMin(resultName));
        statisticalResultNames.push_back(createResultNameMax(resultName));
        statisticalResultNames.push_back(createResultNameMean(resultName));
        statisticalResultNames.push_back(createResultNameDev(resultName));
        statisticalResultNames.push_back(createResultNameRange(resultName));

        if (m_statisticsConfig.m_calculatePercentiles)
        {
            statisticalResultNames.push_back(createResultNamePVal(resultName, m_statisticsConfig.m_pMinPos));
            statisticalResultNames.push_back(createResultNamePVal(resultName, m_statisticsConfig.m_pMidPos));
            statisticalResultNames.push_back(createResultNamePVal(resultName, m_statisticsConfig.m_pMaxPos));
        }

        if (activeCellCount > 0)
        {
            for (size_t i = 0; i < statisticalResultNames.size(); ++i)
            {
                addNamedResult(destCellResultsData, resultType, statisticalResultNames[i], activeCellCount);
            }
        }
    }

    // Start the loop that calculates the statistics

    caf::ProgressInfo progressInfo(m_timeStepIndices.size(), "Computing Statistics");

    for (size_t timeIndicesIdx = 0; timeIndicesIdx < m_timeStepIndices.size(); timeIndicesIdx++)
    {
        size_t timeStepIdx = m_timeStepIndices[timeIndicesIdx];

        for (size_t gridIdx = 0; gridIdx < m_destinationCase->gridCount(); gridIdx++)
        {
            RigGridBase* grid = m_destinationCase->grid(gridIdx);

            for (int resSpecIdx = 0; resSpecIdx < resultSpecification.size(); resSpecIdx++)
            {
                RifReaderInterface::PorosityModelResultType poroModel = resultSpecification[resSpecIdx].m_poroModel;
                RimDefines::ResultCatType resultType = resultSpecification[resSpecIdx].m_resType;
                QString resultName = resultSpecification[resSpecIdx].m_resVarName;

                size_t activeCellCount = m_destinationCase->activeCellInfo(poroModel)->reservoirActiveCellCount();

                if (activeCellCount == 0) continue;

                RigCaseCellResultsData* destCellResultsData = m_destinationCase->results(poroModel);

                size_t dataAccessTimeStepIndex = timeStepIdx;

                // Always evaluate statistics once, and always use time step index zero
                if (resultType == RimDefines::STATIC_NATIVE)
                {
                    if (timeIndicesIdx > 0) continue;

                    dataAccessTimeStepIndex = 0;
                }

                // Build data access objects for source scalar results

                cvf::Collection<RigResultAccessor> sourceDataAccessList;
                for (size_t caseIdx = 0; caseIdx < m_sourceCases.size(); caseIdx++)
                {
                    RimEclipseCase* sourceCase = m_sourceCases.at(caseIdx);

                    // Trigger loading of dataset
                    sourceCase->results(poroModel)->findOrLoadScalarResultForTimeStep(resultType, resultName, dataAccessTimeStepIndex);

                    cvf::ref<RigResultAccessor> resultAccessor = RigResultAccessorFactory::createResultAccessor(sourceCase->reservoirData(), gridIdx, poroModel, dataAccessTimeStepIndex, resultName, resultType);
                    if (resultAccessor.notNull())
                    {
                        sourceDataAccessList.push_back(resultAccessor.p());
                    }
                }

                // Build data access objects for destination scalar results
                // Find the created result container, if any, and put its resultAccessor into the enum indexed destination collection

                cvf::Collection<RigResultModifier> destinationDataAccessList;
                std::vector<QString> statisticalResultNames(STAT_PARAM_COUNT);

                statisticalResultNames[MIN] = createResultNameMin(resultName);
                statisticalResultNames[MAX] = createResultNameMax(resultName);
                statisticalResultNames[RANGE] = createResultNameRange(resultName);
                statisticalResultNames[MEAN] = createResultNameMean(resultName);
                statisticalResultNames[STDEV] = createResultNameDev(resultName);
                statisticalResultNames[PMIN] = createResultNamePVal(resultName, m_statisticsConfig.m_pMinPos);
                statisticalResultNames[PMID] = createResultNamePVal(resultName, m_statisticsConfig.m_pMidPos);
                statisticalResultNames[PMAX] = createResultNamePVal(resultName, m_statisticsConfig.m_pMaxPos);

                for (size_t stIdx = 0; stIdx < statisticalResultNames.size(); ++stIdx)
                {
                    size_t scalarResultIndex = destCellResultsData->findScalarResultIndex(resultType, statisticalResultNames[stIdx]);

                    cvf::ref<RigResultModifier> resultModifier = RigResultModifierFactory::createResultModifier(m_destinationCase, grid->gridIndex(), poroModel, dataAccessTimeStepIndex, scalarResultIndex);
                    destinationDataAccessList.push_back(resultModifier.p());
                }

                 std::vector<double> statParams(STAT_PARAM_COUNT, HUGE_VAL);
                 std::vector<double> values(sourceDataAccessList.size(), HUGE_VAL);
                // Loop over the cells in the grid, get the case values, and calculate the cell statistics 
#pragma omp parallel for schedule(dynamic) firstprivate(statParams, values)
                for (int cellIdx = 0; static_cast<size_t>(cellIdx) < grid->cellCount(); cellIdx++)
                {

                    size_t reservoirCellIndex = grid->reservoirCellIndex(cellIdx);
                    if (m_destinationCase->activeCellInfo(poroModel)->isActive(reservoirCellIndex))
                    {
                        // Extract the cell values from each of the cases and assemble them into one vector

                        

                        bool foundAnyValidValues = false;
                        for (size_t caseIdx = 0; caseIdx < sourceDataAccessList.size(); caseIdx++)
                        {
                            double val = sourceDataAccessList.at(caseIdx)->cellScalar(cellIdx);
                            values[caseIdx] = val;

                            if (val != HUGE_VAL)
                            {
                                foundAnyValidValues = true;
                            }
                        }

                        // Do the real statistics calculations
                       

                        if (foundAnyValidValues)
                        {
                            RigStatisticsMath::calculateBasicStatistics(values, &statParams[MIN], &statParams[MAX], &statParams[RANGE], &statParams[MEAN], &statParams[STDEV]);

                            // Calculate percentiles
                            if (m_statisticsConfig.m_calculatePercentiles )
                            {
                                if (m_statisticsConfig.m_pValMethod == RimEclipseStatisticsCase::NEAREST_OBSERVATION)
                                {
                                    std::vector<double> pValPoss;
                                    pValPoss.push_back(m_statisticsConfig.m_pMinPos);
                                    pValPoss.push_back(m_statisticsConfig.m_pMidPos);
                                    pValPoss.push_back(m_statisticsConfig.m_pMaxPos);
                                    std::vector<double> pVals = RigStatisticsMath::calculateNearestRankPercentiles(values, pValPoss);
                                    statParams[PMIN] = pVals[0];
                                    statParams[PMID] = pVals[1];
                                    statParams[PMAX] = pVals[2];
                                }
                                else if (m_statisticsConfig.m_pValMethod == RimEclipseStatisticsCase::HISTOGRAM_ESTIMATED)
                                {
                                    std::vector<size_t> histogram;
                                    RigHistogramCalculator histCalc(statParams[MIN], statParams[MAX], 100, &histogram);
                                    histCalc.addData(values);
                                    statParams[PMIN] = histCalc.calculatePercentil(m_statisticsConfig.m_pMinPos);
                                    statParams[PMID] = histCalc.calculatePercentil(m_statisticsConfig.m_pMidPos);
                                    statParams[PMAX] = histCalc.calculatePercentil(m_statisticsConfig.m_pMaxPos);
                                }
                                else if (m_statisticsConfig.m_pValMethod == RimEclipseStatisticsCase::INTERPOLATED_OBSERVATION)
                                {
                                    std::vector<double> pValPoss;
                                    pValPoss.push_back(m_statisticsConfig.m_pMinPos);
                                    pValPoss.push_back(m_statisticsConfig.m_pMidPos);
                                    pValPoss.push_back(m_statisticsConfig.m_pMaxPos);
                                    std::vector<double> pVals = RigStatisticsMath::calculateInterpolatedPercentiles(values, pValPoss);
                                    statParams[PMIN] = pVals[0];
                                    statParams[PMID] = pVals[1];
                                    statParams[PMAX] = pVals[2];
                                }
                                else
                                {
                                    CVF_ASSERT(false);
                                }
                            }
                        }

                        // Set the results into the results data structures

                        for (size_t stIdx = 0; stIdx < statParams.size(); ++stIdx)
                        {
                            if (destinationDataAccessList[stIdx].notNull())
                            {
                                destinationDataAccessList[stIdx]->setCellScalar(cellIdx, statParams[stIdx]);
                            }
                        }
                    }
                }
            }
        }

        // When one time step is completed, free memory and clean up
        // Microsoft note: On Windows, the maximum number of files open at the same time is 512
        // http://msdn.microsoft.com/en-us/library/kdfaxaay%28vs.71%29.aspx

        for (size_t caseIdx = 0; caseIdx < m_sourceCases.size(); caseIdx++)
        {
            RimEclipseCase* eclipseCase = m_sourceCases.at(caseIdx);

            if (!eclipseCase->reservoirViews.size())
            {
                eclipseCase->results(RifReaderInterface::MATRIX_RESULTS)->cellResults()->freeAllocatedResultsData();
                eclipseCase->results(RifReaderInterface::FRACTURE_RESULTS)->cellResults()->freeAllocatedResultsData();
            }

            // Todo : These calls really do nothing right now the access actually closes automatically in ert i belive ...
            eclipseCase->results(RifReaderInterface::MATRIX_RESULTS)->readerInterface()->close();
            eclipseCase->results(RifReaderInterface::FRACTURE_RESULTS)->readerInterface()->close();
        }

        progressInfo.setProgress(timeIndicesIdx);
    }
}