/** @file  pmSourceExtendedPars.c
 *
 *  Functions to define and manipulate sources on images
 *
 *  @author GLG, MHPCC
 *  @author EAM, IfA: significant modifications.
 *
 *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2009-02-06 02:31:25 $
 *
 *  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 "pmFPAMaskWeight.h"
#include "pmSpan.h"
#include "pmFootprint.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"

static void pmSourceExtendedParsFree (pmSourceExtendedPars *pars) {
    if (!pars) return;

    psFree(pars->profile);
    psFree(pars->annuli);
    psFree(pars->isophot);
    psFree(pars->petrosian);
    psFree(pars->kron);
    return;
}

pmSourceExtendedPars *pmSourceExtendedParsAlloc () {
    pmSourceExtendedPars *pars = (pmSourceExtendedPars *) psAlloc(sizeof(pmSourceExtendedPars));
    psMemSetDeallocator(pars, (psFreeFunc) pmSourceExtendedParsFree);

    pars->profile = NULL;
    pars->annuli = NULL;
    pars->isophot = NULL;
    pars->petrosian = NULL;
    pars->kron = NULL;

    return pars;
}

bool psMemCheckSourceExtendedPars(psPtr ptr)
{
    PS_ASSERT_PTR(ptr, false);
    return ( psMemGetDeallocator(ptr) == (psFreeFunc) pmSourceExtendedParsFree);
}


static void pmSourceRadialProfileFree (pmSourceRadialProfile *profile) {
    if (!profile) return;

    psFree(profile->radius);
    psFree(profile->flux);
    psFree(profile->variance);
    return;
}

pmSourceRadialProfile *pmSourceRadialProfileAlloc () {

    pmSourceRadialProfile *profile = (pmSourceRadialProfile *) psAlloc(sizeof(pmSourceRadialProfile));
    psMemSetDeallocator(profile, (psFreeFunc) pmSourceRadialProfileFree);

    profile->radius = NULL;
    profile->flux = NULL;
    profile->variance = NULL;

    return profile;
}

bool psMemCheckSourceRadialProfile(psPtr ptr)
{
    PS_ASSERT_PTR(ptr, false);
    return ( psMemGetDeallocator(ptr) == (psFreeFunc) pmSourceRadialProfileFree);
}


static void pmSourceIsophotalValuesFree (pmSourceIsophotalValues *isophot) {
    if (!isophot) return;
    return;
}

pmSourceIsophotalValues *pmSourceIsophotalValuesAlloc () {

    pmSourceIsophotalValues *isophot = (pmSourceIsophotalValues *) psAlloc(sizeof(pmSourceIsophotalValues));
    psMemSetDeallocator(isophot, (psFreeFunc) pmSourceIsophotalValuesFree);

    isophot->mag = 0.0;
    isophot->magErr = 0.0;
    isophot->rad = 0.0;
    isophot->radErr = 0.0;

    return isophot;
}


bool psMemCheckSourceIsophotalValues(psPtr ptr)
{
    PS_ASSERT_PTR(ptr, false);
    return ( psMemGetDeallocator(ptr) == (psFreeFunc) pmSourceIsophotalValuesFree);
}


static void pmSourcePetrosianValuesFree (pmSourcePetrosianValues *petrosian) {
    if (!petrosian) return;
    return;
}

pmSourcePetrosianValues *pmSourcePetrosianValuesAlloc () {

    pmSourcePetrosianValues *petrosian = (pmSourcePetrosianValues *) psAlloc(sizeof(pmSourcePetrosianValues));
    psMemSetDeallocator(petrosian, (psFreeFunc) pmSourcePetrosianValuesFree);

    petrosian->mag = 0.0;
    petrosian->magErr = 0.0;
    petrosian->rad = 0.0;
    petrosian->radErr = 0.0;

    return petrosian;
}


bool psMemCheckSourcePetrosianValues(psPtr ptr)
{
    PS_ASSERT_PTR(ptr, false);
    return ( psMemGetDeallocator(ptr) == (psFreeFunc) pmSourcePetrosianValuesFree);
}

static void pmSourceKronValuesFree (pmSourceKronValues *kron) {
    if (!kron) return;
    return;
}

pmSourceKronValues *pmSourceKronValuesAlloc () {

    pmSourceKronValues *kron = (pmSourceKronValues *) psAlloc(sizeof(pmSourceKronValues));
    psMemSetDeallocator(kron, (psFreeFunc) pmSourceKronValuesFree);

    kron->mag = 0.0;
    kron->magErr = 0.0;
    kron->rad = 0.0;
    kron->radErr = 0.0;

    return kron;
}


bool psMemCheckSourceKronValues(psPtr ptr)
{
    PS_ASSERT_PTR(ptr, false);
    return ( psMemGetDeallocator(ptr) == (psFreeFunc) pmSourceKronValuesFree);
}


static void pmSourceAnnuliFree (pmSourceAnnuli *annuli) {
    if (!annuli) return;

    psFree (annuli->flux);
    psFree (annuli->fluxErr);
    psFree (annuli->fluxVar);

    return;
}

pmSourceAnnuli *pmSourceAnnuliAlloc () {

    pmSourceAnnuli *annuli = (pmSourceAnnuli *) psAlloc(sizeof(pmSourceAnnuli));
    psMemSetDeallocator(annuli, (psFreeFunc) pmSourceAnnuliFree);

    annuli->flux = NULL;
    annuli->fluxErr = NULL;
    annuli->fluxVar = NULL;

    return annuli;
}


bool psMemCheckSourceAnnuli(psPtr ptr)
{
    PS_ASSERT_PTR(ptr, false);
    return ( psMemGetDeallocator(ptr) == (psFreeFunc) pmSourceAnnuliFree);
}
