# include "absphot.h"

int fit_trend (Catalog *catalog) {

  // XXX define this : obs - ref vs color(obs)
  // target is the ref photcode (derived from obs photcode table entry)
  PhotCode *target; 

  int i, Nc0, Nc1, Nc2;
  float *x, *y, *dy;
  float c0, m0, dc0, dm0, c1, c2;

  ALLOCATE (x, float, catalog[0].Naverage);
  ALLOCATE (y, float, catalog[0].Naverage);
  ALLOCATE (dy, float, catalog[0].Naverage);

  // XXX I must have code that does this in another program
  // XXX perhaps relphot for the grid colors?
  Nc0 = GetPhotcodeNsec (target->code);
  Nc1 = GetPhotcodeNsec (target->c1);
  Nc2 = GetPhotcodeNsec (target->c2);

  if (catalog[0].Naverage != catalog[0].Nmeasure) abort();

  for (i = 0; i < catalog[0].Naverage; i++) {

    // XXX I probably need to apply some corrections?
    c0 = catalog[0].secfilt[i*PhotNsec + Nc0].M;
    m0 = catalog[0].measure[i].M;

    dc0 = catalog[0].secfilt[i*PhotNsec + Nc0].dM;
    dm0 = catalog[0].measure[i].dM;

    c1 = catalog[0].secfilt[i*PhotNsec + Nc1].M;
    c2 = catalog[0].secfilt[i*PhotNsec + Nc2].M;

    x[i] = c1 - c2;
    y[i] = c0 - m0;
    
    dy[i] = 1.0 / (SQ(dc0) + SQ(dm0));
  }

  // XXX define this
  // fit_vectors (x, y, dy);
  // do something with the result!

  free (x);
  free (y);
  free (dy);

}
 
