# include "ppSim.h"
static char *defaultModel = "PS_MODEL_SERSIC";

bool ppSimInsertGalaxies (pmReadout *readout, psImage *expCorr, psArray *galaxies, pmConfig *config) {

    bool mdok;

    assert (readout);
    assert (galaxies);
    
    if (!galaxies->n) { return true; }

    pmCell *cell = readout->parent;
    pmChip *chip = cell->parent;

    // XXX this is an estimate of the sky noise based on the inputs to the image simulation.
    // XXX update this to allow the estimate based on the measured sky background
    // XXX this is missing the gain.
    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSIM_RECIPE); // Recipe

    float expTime   = psMetadataLookupF32(NULL, recipe, "EXPTIME"); // Exposure time
    float darkRate  = psMetadataLookupF32(NULL, recipe, "DARK.RATE"); // Dark rate

    float readnoise = psMetadataLookupF32(NULL, cell->concepts, "CELL.READNOISE");// CCD read noise, e
    if (isnan(readnoise)) {
	psWarning("CELL.READNOISE is not set; reverting to recipe value READNOISE.");
	readnoise = psMetadataLookupF32(&mdok, recipe, "READNOISE");
	if (!mdok) {
	    psError(PS_ERR_BAD_PARAMETER_VALUE, false, "Unable to find READNOISE in recipe.");
	    return false;
	}
    }

    float skyRate = psMetadataLookupF32(NULL, recipe, "SKY.RATE"); // Sky rate
    if (isnan(skyRate)) {
	float zp      = psMetadataLookupF32(&mdok, recipe, "ZEROPOINT"); assert (mdok);
	float scale   = psMetadataLookupF32(&mdok, recipe, "PIXEL.SCALE"); assert (mdok);
	float skyMags = psMetadataLookupF32(&mdok, recipe, "SKY.MAGS");  assert (mdok);
	skyRate = scale * scale * ppSimMagToFlux (skyMags, zp);
    }
    
    // Rough noise estimate, appropriate for entire cell (use for source radius?)
    float roughNoise = sqrtf(PS_SQR(readnoise) + (darkRate + skyRate) * expTime);

    int x0Chip = psMetadataLookupS32(NULL, chip->concepts, "CHIP.X0");
    int y0Chip = psMetadataLookupS32(NULL, chip->concepts, "CHIP.Y0");
    int xParityChip = psMetadataLookupS32(NULL, chip->concepts, "CHIP.XPARITY");
    int yParityChip = psMetadataLookupS32(NULL, chip->concepts, "CHIP.YPARITY");

    int x0Cell = psMetadataLookupS32(NULL, cell->concepts, "CELL.X0");
    int y0Cell = psMetadataLookupS32(NULL, cell->concepts, "CELL.Y0");
    int xParityCell = psMetadataLookupS32(NULL, cell->concepts, "CELL.XPARITY");
    int yParityCell = psMetadataLookupS32(NULL, cell->concepts, "CELL.YPARITY");

    int binning = psMetadataLookupS32(NULL, recipe, "BINNING"); // Binning in x and y

    // determine the galaxy model
    char *modelName = psMetadataLookupStr(&mdok, recipe, "GALAXY.MODEL"); // galaxy model name
    if (modelName == NULL) {
	modelName = defaultModel;
    }
    pmModelType type = pmModelClassGetType (modelName);
    if (type == -1) {
	psError (PS_ERR_UNKNOWN, false, "invalid model name");
        return false;
    }

    int nParam = pmModelClassParameterCount (type);

    pmPSF *psf = psMetadataLookupPtr (&mdok, chip->analysis, "PSPHOT.PSF");
    assert (psf);

    int dX = PM_CELL_TO_CHIP (0.0, x0Cell, xParityCell, binning);
    int dY = PM_CELL_TO_CHIP (0.0, y0Cell, yParityCell, binning);

    pmDetections *detections = psMetadataLookupPtr (&mdok, readout->analysis, "PSPHOT.DETECTIONS");
    if (!detections) {
	detections = pmDetectionsAlloc();
	detections->allSources = psArrayAllocEmpty (galaxies->n);
	psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_DATA_ARRAY | PS_META_REPLACE, "psphot detections", detections);
    } else {
	psMemIncrRefCounter (detections);
    }
    psArray *sources = sources = detections->allSources;

    // add sources to the readout image & weight
    for (long i = 0; i < galaxies->n; i++) {
	ppSimGalaxy *galaxy = galaxies->data[i];

	// galaxy->x,y are in fpa coordinates

	// Position on the cell and peak flux
	float xChip = PM_FPA_TO_CHIP(galaxy->x, x0Chip, xParityChip);
	float yChip = PM_FPA_TO_CHIP(galaxy->y, y0Chip, yParityChip);

	// Position on the cell and peak flux
	float xCell = PM_CHIP_TO_CELL(xChip, x0Cell, xParityCell, binning);
	float yCell = PM_CHIP_TO_CELL(yChip, y0Cell, yParityCell, binning);

	if (xCell < 0) continue;
	if (yCell < 0) continue;
	if (xCell > readout->image->numCols) continue;
	if (yCell > readout->image->numRows) continue;
	// XXX need to apply col0, row0 if readout is a subarray

	// XXX apply the expCorr to the galaxy->flux before setting the model flux

	// instantiate a model for the PSF at this location, set desired flux
	pmModel *model = pmModelAlloc (type);
	
	psF32 *PAR = model->params->data.F32;

	PAR[PM_PAR_I0]   = galaxy->peak;
	PAR[PM_PAR_SKY]  = 0.0;
	PAR[PM_PAR_XPOS] = xChip;
	PAR[PM_PAR_YPOS] = yChip;

	psEllipseAxes axes;
        axes.major       = galaxy->Rmaj;
        axes.minor       = galaxy->Rmin;
        axes.theta       = galaxy->theta;
	pmPSF_AxesToModel (PAR, axes);
	psF64 Area = 2.0 * M_PI * axes.major * axes.minor;

	if (nParam == 8) {
	    PAR[PM_PAR_7] = galaxy->index;
	}

	// set the normalization to get the desired flux
	// XXX for now, use peak: pmModelSetFlux (model, galaxy->flux);

	// XXX let the flux limit be a user-defined number of sky sigmas (not just 1.0)
	float radius = model->modelRadius (model->params, roughNoise);
	radius = PS_MAX (radius, 1.0);
	// XXX the exp(-r^0.25) models can go way out if allowed...
	radius = PS_MIN (radius, 150.0); 

	// construct a source, with model flux pixels set, based on the model
	pmSource *source = pmSourceFromModel (model, readout, radius, PM_SOURCE_TYPE_EXTENDED);

	// XXX set the mag & err values (should this be done in pmSourceFromModel?)
	// XXX i should be applying the gain and the correct effective area
	source->psfMag = -2.5*log10(galaxy->flux);
	source->extMag = -2.5*log10(galaxy->flux);
	source->errMag = sqrt(Area*PS_SQR(roughNoise) + galaxy->flux) / galaxy->flux;	
	
	// XXX add the sources to a source array

# if (0)	
	if (CONVOLVED_FIT) {

	    // select the PSF (XXX : move out of loop)
	    pmPSF *psfModel = psMetadataLookupPtr (&mdok, chip->analysis, "PSPHOT.PSF");
	    assert (psf);
	    
	    // supply the psf to the source (normalized?)
	    source->modelPSF = pmModelFromPSFforXY (psfModel, xChip, yChip, 1.0);

	    // instantiate the psf flux
	    pmSourceCachePSF (source, maskVal);

	    // convert the cached cached psf model for this source to a psKernel
	    // XXX for the moment, hard-wire the kernel to be 5x5 (2 pix radius)
	    // XXX for the moment, hard-wire the kernel to be 9x9 (4 pix radius)
	    psKernel *psf = psphotKernelFromPSF (source, psfSize);

	    // instantiate the source model flux
	    // XXX this may need to handle the offset?
	    pmSourceCacheModel (source, maskVal);

	    // make a storage buffer for the output image
	    psImage *buffer = psImageCopy (source->modelFlux, PS_TYPE_F32);

	    // convolve the psf image with the model image
	    psImageConvolveDirect (buffer, source->modelFlux, psf);

	    // save the result back in the source
	    psFree (source->modelFlux);
	    source->modelFlux = buffer;
	}
# endif

	// insert the source flux in the image
	pmSourceAddWithOffset (source, PM_MODEL_OP_FULL, 0xff, dX, dY);
	psArrayAdd (sources, 100,source);
    }

    // XXX many leaks in here, i think 
    psFree (detections);

    return true;
}

