# include "setphot.h"

// the date/time of the image is used to find the 'season'
// the photcode is used to find the actual flat correction

int match_camcorr_to_images (Image *image, off_t Nimage, CamPhotomCorrection *camcorr) {

  int i, j;

  // we have an array of CamPhotomCorrection->matrix values, where each matrix is a flat-field image
  // the sequence is seq = ix + Nx_chip*iy + filter*Nchips + season*(Nchips*Nfilter)

  // we derive ix,iy,filter from photcode : 10143 -> ix = 4, iy = 3, filter = 1
  // season comes from the date/time

  int minCode = 10000;
  int maxCode = 10576;

  int Nmissed = 0;
  
  for (i = 0; i < Nimage; i++) {
    if (!image[i].photcode) continue; // skip PHU images

    if (image[i].photcode < minCode) continue; // skip non-gpc1-chip images
    if (image[i].photcode > maxCode) continue; // skip non-gpc1-chip images

    int found = FALSE;
    for (j = 0; !found && (j < camcorr->Nseason); j++) {
      if (image[i].tzero < camcorr->tstart[j]) continue;
      if (image[i].tzero > camcorr->tstop[j]) continue;

      int photcode = image[i].photcode;
      int iy =  photcode % 10;
      int ix = (int)(photcode / 10) % 10;
      int filter = (int)(photcode / 100) % 10;

      // we add one so photom_map_id = 0 can mean no map
      int seq = ix + iy * camcorr->Nx + filter * camcorr->Nchips + j * camcorr->Nflats;
      image[i].photom_map_id = seq + 1;
      found = TRUE;
    }
    if (!found) {
      Nmissed ++;
      fprintf (stderr, "image does not match flatcorr Table ranges: %s\n", image[i].name);
    }

    if (Nmissed > 100)  {
      fprintf (stderr, "something is wrong: too many images do not match\n");
      exit (2);
    }

  }
  return TRUE;
}
