/** @file  pmModelClass.c
 *
 *  Functions to define and manipulate object model attributes
 *
 *  @author GLG, MHPCC
 *  @author EAM, IfA
 *
 *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2007-11-27 03:14:57 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <pslib.h>
#include "pmHDU.h"
#include "pmFPA.h"
#include "pmPeaks.h"
#include "pmMoments.h"
#include "pmResiduals.h"
#include "pmGrowthCurve.h"
#include "pmTrend2D.h"
#include "pmPSF.h"
#include "pmModel.h"
#include "pmSource.h"
#include "pmModelClass.h"
#include "pmErrorCodes.h"

// XXX shouldn't these be defined for us in pslib.h ???
double hypot(double x, double y);
double sqrt (double x);

# include "models/pmModel_GAUSS.c"
# include "models/pmModel_PGAUSS.c"
# include "models/pmModel_QGAUSS.c"
# include "models/pmModel_RGAUSS.c"
# include "models/pmModel_SERSIC.c"

static pmModelClass defaultModels[] = {
    {"PS_MODEL_GAUSS",        7, pmModelFunc_GAUSS,   pmModelFlux_GAUSS,   pmModelRadius_GAUSS,   pmModelLimits_GAUSS,   pmModelGuess_GAUSS,  pmModelFromPSF_GAUSS,  pmModelParamsFromPSF_GAUSS,  pmModelFitStatus_GAUSS},
    {"PS_MODEL_PGAUSS",       7, pmModelFunc_PGAUSS,  pmModelFlux_PGAUSS,  pmModelRadius_PGAUSS,  pmModelLimits_PGAUSS,  pmModelGuess_PGAUSS, pmModelFromPSF_PGAUSS, pmModelParamsFromPSF_PGAUSS, pmModelFitStatus_PGAUSS},
    {"PS_MODEL_QGAUSS",       8, pmModelFunc_QGAUSS,  pmModelFlux_QGAUSS,  pmModelRadius_QGAUSS,  pmModelLimits_QGAUSS,  pmModelGuess_QGAUSS, pmModelFromPSF_QGAUSS, pmModelParamsFromPSF_QGAUSS, pmModelFitStatus_QGAUSS},
    {"PS_MODEL_RGAUSS",       8, pmModelFunc_RGAUSS,  pmModelFlux_RGAUSS,  pmModelRadius_RGAUSS,  pmModelLimits_RGAUSS,  pmModelGuess_RGAUSS, pmModelFromPSF_RGAUSS, pmModelParamsFromPSF_RGAUSS, pmModelFitStatus_RGAUSS},
    {"PS_MODEL_SERSIC",       8, pmModelFunc_SERSIC,  pmModelFlux_SERSIC,  pmModelRadius_SERSIC,  pmModelLimits_SERSIC,  pmModelGuess_SERSIC, pmModelFromPSF_SERSIC, pmModelParamsFromPSF_SERSIC, pmModelFitStatus_SERSIC}
};

static pmModelClass *models = NULL;
static int Nmodels = 0;

static void ModelClassFree (pmModelClass *modelClass)
{
    if (modelClass == NULL)
        return;
    return;
}

pmModelClass *pmModelClassAlloc (int nModels)
{
    pmModelClass *modelClass = (pmModelClass *) psAlloc (nModels * sizeof(pmModelClass));
    psMemSetDeallocator(modelClass, (psFreeFunc) ModelClassFree);
    return (modelClass);
}

bool psMemCheckModelClass(psPtr ptr)
{
    PS_ASSERT_PTR(ptr, false);
    return ( psMemGetDeallocator(ptr) == (psFreeFunc) ModelClassFree);
}

void pmModelClassAdd (pmModelClass *model)
{
    if (models == NULL) {
        pmModelClassInit();
    }

    Nmodels ++;
    models = (pmModelClass *) psRealloc (models, Nmodels*sizeof(pmModelClass));
    models[Nmodels-1] = model[0];
    return;
}

bool pmModelClassInit (void)
{
    // if we do not need to init, return false;
    if (models != NULL) {
        return false;
    }

    int Nnew = sizeof (defaultModels) / sizeof (pmModelClass);

    models = pmModelClassAlloc (Nnew);
    for (int i = 0; i < Nnew; i++) {
        models[i] = defaultModels[i];
    }
    Nmodels = Nnew;
    return true;
}

pmModelClass *pmModelClassSelect (pmModelType type)
{
    if (models == NULL) {
        pmModelClassInit();
    }

    if ((type < 0) || (type >= Nmodels)) {
        psError(PS_ERR_UNKNOWN, true, "Undefined pmModelType");
        return (NULL);
    }
    return (&models[type]);
}

void pmModelClassCleanup (void)
{
    psFree (models);
    models = NULL;
    Nmodels = 0;
    return;
}

psS32 pmModelClassParameterCount (pmModelType type)
{
    if (models == NULL) {
        pmModelClassInit();
    }

    if ((type < 0) || (type >= Nmodels)) {
        psError(PS_ERR_UNKNOWN, true, "Undefined pmModelType");
        return (0);
    }
    return (models[type].nParams);
}

psS32 pmModelClassGetType (const char *name)
{
    if (models == NULL) {
        pmModelClassInit();
    }

    for (int i = 0; i < Nmodels; i++) {
        if (!strcmp(models[i].name, name)) {
            return (i);
        }
    }
    return (-1);
}

char *pmModelClassGetName (pmModelType type)
{
    if (models == NULL) {
        pmModelClassInit();
    }

    if ((type < 0) || (type >= Nmodels)) {
        psError(PS_ERR_UNKNOWN, true, "Undefined pmModelType");
        return (NULL);
    }
    return (models[type].name);
}
