# include "ppSim.h"

// XXX add bounds to the inputs?
bool ppSimMakeDark (pmReadout *readout, pmConfig *config) {

    bool mdok;

    psImage *signal = readout->image;
    psImage *variance = readout->variance;

    psMetadata *recipe = psMetadataLookupMetadata(&mdok, config->recipes, PPSIM_RECIPE); // Recipe

    bool dark = psMetadataLookupBool(&mdok, recipe, "DARK"); // Generate a DARK?
    if (!dark) return true;

    float darkRate = psMetadataLookupF32(&mdok, recipe, "DARK.RATE"); // Dark rate
    if (isnan(darkRate)) {
      psError(PS_ERR_UNKNOWN, false, "missing DARK.RATE\n");
      return false;
    }

    float expTime  = psMetadataLookupF32(&mdok, recipe, "EXPTIME"); // Exposure time
    if (isnan(expTime)) {
      psError(PS_ERR_UNKNOWN, false, "missing EXPTIME\n");
      return false;
    }

    psTrace("ppSim", 6, "darkRate: %f, expTime: %f\n", darkRate, expTime);

    for (int y = 0; y < signal->numRows; y++) {
        for (int x = 0; x < signal->numCols; x++) {

            // Dark current
            float darkCurrent = darkRate * expTime; // Dark current accumulated
            signal->data.F32[y][x] += darkCurrent;
            variance->data.F32[y][x] += darkCurrent;
        }
    }
    return true;
}

