# include "ppSim.h"

// define the needed / desired I/O files
bool ppSimPhotomFiles (pmConfig *config, pmFPAfile *fakeFile, pmFPAfile *forceFile) {

    bool status = false;

    // select recipe options supplied on command line
    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);

    // optionally save the background model (small FITS image)
    if (psMetadataLookupBool(NULL, recipe, "SAVE.BACKMDL")) {
        int DX = psMetadataLookupS32 (&status, recipe, "BACKGROUND.XBIN");
        int DY = psMetadataLookupS32 (&status, recipe, "BACKGROUND.YBIN");
        pmFPAfile *output = pmFPAfileDefineFromFile (config, fakeFile, DX, DY, "PSPHOT.BACKMDL");
        if (!output) {
            psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.BACKMDL");
            return false;
        }
        output->save = true;
    }
    // optionally save the background model's standard deviation (small FITS image)
    if (psMetadataLookupBool(NULL, recipe, "SAVE.BACKMDL.STDEV")) {
        int DX = psMetadataLookupS32 (&status, recipe, "BACKGROUND.XBIN");
        int DY = psMetadataLookupS32 (&status, recipe, "BACKGROUND.YBIN");
        pmFPAfile *output = pmFPAfileDefineFromFile (config, fakeFile, DX, DY, "PSPHOT.BACKMDL.STDEV");
        if (!output) {
            psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.BACKMDL.STDEV");
            return false;
        }
        output->save = true;
    }
    // optionally save the full background image
    if (psMetadataLookupBool(NULL, recipe, "SAVE.BACKGND")) {
        pmFPAfile *output = pmFPAfileDefineFromFile (config, fakeFile,  1,  1, "PSPHOT.BACKGND");
        if (!output) {
            psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.BACKGND");
            return false;
        }
        output->save = true;
    }
    // optionally save the background-subtracted image
    if (psMetadataLookupBool(NULL, recipe, "SAVE.BACKSUB")) {
        pmFPAfile *output = pmFPAfileDefineFromFile (config, fakeFile,  1,  1, "PSPHOT.BACKSUB");
        if (!output) {
            psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.BACKSUB");
            return false;
        }
        output->save = true;
    }

    // the input sources are required for sensible measurements of the fake or force sources
    pmFPAfile *cmffile = pmFPAfileDefineFromArgs (&status, config, "PPSIM.REAL.SOURCES", "INPUT.SOURCES");
    if (!cmffile) {
      psError(PS_ERR_UNKNOWN, false, "input sources not found; required for photometry (or error with PPSIM.REAL.SOURCES filerule)");
      return false;
    }

    // the fake sources are carried on the fakeFile->fpa structures (PPSIM.FAKE.CHIP)
    pmFPAfile *outsources = pmFPAfileDefineOutput (config, fakeFile->fpa, "PPSIM.FAKE.SOURCES");
    if (!outsources) {
        psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PPSIM.FAKE.SOURCES");
        return false;
    }
    outsources->save = true;

    // the forced sources are carried on the forceFile->fpa structures (PPSIM.FORCE.CHIP)
    pmFPAfile *outforced = pmFPAfileDefineOutput (config, forceFile->fpa, "PPSIM.FORCE.SOURCES");
    if (!outforced) {
        psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PPSIM.FORCE.SOURCES");
        return false;
    }
    outforced->save = true;

    // we bind the psffile to the chip-mosaicked image so we have a reference to the image 
    pmFPAfile *psffile = pmFPAfileBindFromArgs(&status, fakeFile, config, "PSPHOT.PSF.LOAD", "PSPHOT.PSF");
    if (!psffile) {
      psError(PSPHOT_ERR_CONFIG, false, "Failed to find/build PSPHOT.PSF.LOAD");
      return false;
    }

    return true;
}
