# include "addstar.h"

int load2mass_catalog (Catalog *catalog, Stars *stars, int Nstars) {

  int i, j, Nsec, Nmeas, Nave, NMEAS, NAVE;

  Nsec = GetPhotcodeNsecfilt ();
  Nave = catalog[0].Naverage;
  Nmeas = catalog[0].Nmeasure;
   
  NAVE = Nave + 100;
  NMEAS = Nmeas + 100;
  REALLOCATE (catalog[0].average, Average, NAVE);
  REALLOCATE (catalog[0].secfilt, SecFilt, NAVE*Nsec);
  REALLOCATE (catalog[0].measure, Measure, NMEAS);

  for (i = 0; i < Nstars; i+=3) {

    // construct an average object for this object
    // XXX for now, the output objects will have limited astrometric interpretation...
    // XXX every 3 stars represents 3 measurements and 1 average
    catalog[0].average[Nave].R     = stars[i].R;
    catalog[0].average[Nave].D     = stars[i].D;
    catalog[0].average[Nave].dR    = 0;
    catalog[0].average[Nave].dD    = 0;
    catalog[0].average[Nave].uR    = 0;
    catalog[0].average[Nave].uD    = 0;
    catalog[0].average[Nave].duR   = 0;
    catalog[0].average[Nave].duD   = 0;
    catalog[0].average[Nave].P     = 0;
    catalog[0].average[Nave].dP    = 0;

    // XXX for now, set the average mag data to NULL
    catalog[0].average[Nave].M         = NO_MAG;
    catalog[0].average[Nave].dM        = NO_MAG;
    catalog[0].average[Nave].Nm        = 0;
    catalog[0].average[Nave].Nn        = 0;
    catalog[0].average[Nave].Xp        = NO_MAG;
    catalog[0].average[Nave].Xm        = NO_MAG;
    catalog[0].average[Nave].Xg        = NO_MAG;
    catalog[0].average[Nave].offset    = Nmeas;
    catalog[0].average[Nave].missing   = -1;
    catalog[0].average[Nave].code      = 0;

    for (j = 0; j < Nsec; j++) {
      catalog[0].secfilt[Nave*Nsec+j].M_PS  = NO_MAG;
      catalog[0].secfilt[Nave*Nsec+j].dM_PS = NO_MAG;
      catalog[0].secfilt[Nave*Nsec+j].Xm    = NO_MAG;
    }

    // we now have the min chisq row. use this to supply the other filter values....
    for (j = 0; j < 3; j++) {
      catalog[0].measure[Nmeas].dR_PS       = 0.0;
      catalog[0].measure[Nmeas].dD_PS       = 0.0;
      catalog[0].measure[Nmeas].M_PS        = MIN (stars[i+j].M,  NO_MAG);
      catalog[0].measure[Nmeas].dM_PS       = MIN (stars[i+j].dM,  NO_ERR);
      catalog[0].measure[Nmeas].Mcal_PS     = 0;
      catalog[0].measure[Nmeas].t           = stars[i+j].t;
      catalog[0].measure[Nmeas].averef      = Nave;
      catalog[0].measure[Nmeas].source      = stars[i+j].code;
      catalog[0].measure[Nmeas].dophot      = 0;
      catalog[0].measure[Nmeas].flags       = 0;
      catalog[0].measure[Nmeas].dt_PS       = 0xffff;

      catalog[0].measure[Nmeas].Mgal_PS     = NO_MAG;
      catalog[0].measure[Nmeas].airmass_PS  = 0;
      catalog[0].measure[Nmeas].FWx         = NO_MAG;
      catalog[0].measure[Nmeas].FWy         = NO_ERR;
      catalog[0].measure[Nmeas].theta       = NO_ERR;

      catalog[0].average[Nave].Nm++;
      Nmeas ++;
      CHECK_REALLOCATE (catalog[0].measure, Measure, NMEAS, Nmeas, 100);
    }

    Nave ++;
    if (Nave >= NAVE) {
      NAVE += 100;
      REALLOCATE (catalog[0].average, Average, NAVE);
      REALLOCATE (catalog[0].secfilt, SecFilt, NAVE*Nsec);
    }
  }
  catalog[0].Naverage = Nave;
  catalog[0].Nmeasure = Nmeas;
  return (TRUE);
}
