# include "ppSimSequence.h"

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

    bool status, setLevel, setRange;

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

    // optional details
    float level = psMetadataLookupF32 (&setLevel, sequence, "BIAS.LEVEL");
    float range = psMetadataLookupF32 (&setRange, sequence, "BIAS.RANGE");

    int nImages = psMetadataLookupS32 (&status, sequence, "NIMAGES");

    // loop over the filters & exposure times
    int nImage = 0;
    for (int i = 0; i < nImages; i++) {
	    
      psString command = NULL;

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

      if (setLevel) psStringAppend (&command, " -biaslevel %f", level);
      if (setRange) psStringAppend (&command, " -biasrange %f", range);

      // 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;
}
