# include "ppSimSequence.h"

bool ppSimSequenceFlat (FILE *simfile, FILE *inject, psMetadata *sequence, int nSeq, psRandom *rng, const char *path, const char *basename, const char *injectCommand) {

    bool status;

    char *camera = psMetadataLookupStr (&status, sequence, "CAMERA");

    // determine the filters & exposure times
    char *filterList = psMetadataLookupStr (&status, sequence, "FILTERS");
    psArray *filters = psStringSplitArray (filterList, ",: ", false);

    psVector *exptimes = psMetadataLookupPtr (&status, sequence, "EXPTIMES");

    if (filters->n != exptimes->n) {
	psLogMsg ("ppSimSequence", PS_LOG_WARN, "mis-match in filter and exptime lists");
	exit (1);
    }

    // number of images for each filter, exptime set
    int nSetup = psMetadataLookupS32 (&status, sequence, "NSETUP");

    int nImage = 0;

    // loop over the filters & exposure times
    for (int i = 0; i < filters->n; i++) {
	    
      // loop over the filters & exposure times
      for (int j = 0; j < nSetup; j++) {
	    
	psString command = NULL;

	psStringAppend (&command, "ppSim -type FLAT");
	psStringAppend (&command, " -camera %s", camera);

	psStringAppend (&command, " -filter %s", (char *) filters->data[i]);
	psStringAppend (&command, " -exptime %f", exptimes->data.F32[i]);

	// XXX need to add output filename
	psString filename = NULL;
	if (path) {
	  psStringAppend (&filename, "%s/%s.%03d.%03d", path, basename, nSeq, nImage);
	} else {
	  psStringAppend (&filename, "%s.%03d.%03d", basename, nSeq, nImage);
	}
	psStringAppend (&command, " %s", filename);


	fprintf (simfile, "%s\n", command);
	psFree (command);

	// path should be dirname/filename
	command = psStringCopy (injectCommand);
	psStringAppend (&command, " --camera %s", camera);
	psStringAppend (&command, " %s*.fits",    filename);
	fprintf (inject, "%s\n", command);
	psFree (command);
	psFree (filename);

	nImage ++;
      }
    }
    return true;
}
