# include "relphot.h"

void global_stats (Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr, int nloop) {

  StatType stN, stX, stS, imN, imX, imM, imD, msM, msX, msN, msD;

  liststats_init (&stN);
  liststats_init (&stX);
  liststats_init (&stS);
  liststats_init (&imN);
  liststats_init (&imX);
  liststats_init (&imM);
  liststats_init (&imD);
  liststats_init (&msN);
  liststats_init (&msX);
  liststats_init (&msM);
  liststats_init (&msD);

  // INITTIME;

  fprintf (stderr, "\n");
  fprintf (stderr, "STATS            median     mean    sigma      min      max   Nmeas\n");

  int Ns;
  for (Ns = 0; (nloop % 3 == 2) && (Ns < Nphotcodes); Ns++) {

    int thisCode = photcodes[Ns][0].code;
    int Nsec = GetPhotcodeNsec(thisCode);
    int seccode = photcodes[Ns][0].code;

    // MARKTIME("start star stats: %f sec\n", dtime);
    stN = statsStarN (catalog, Ncatalog, Nsec, seccode, flatcorr);
    // MARKTIME("done star N: %f sec\n", dtime);
    stX = statsStarX (catalog, Ncatalog, Nsec);
    // MARKTIME("done star X: %f sec\n", dtime);
    stS = statsStarS (catalog, Ncatalog, Nsec);
    // MARKTIME("done star S: %f sec\n", dtime);
  
    fprintf (stderr, "   --- stats for %s ---\n", photcodes[Ns][0].name);
    fprintf (stderr, "meas / star:    %7.0f  %7.1f  %7.1f  %7.0f  %7.0f  %6d\n", stN.median, stN.mean, stN.sigma, stN.min, stN.max, stN.Nmeas);
    fprintf (stderr, "dMrel star:     %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n", stS.median, stS.mean, stS.sigma, stS.min, stS.max, stS.Nmeas);
    fprintf (stderr, "chisq star:     %7.1f  %7.1f  %7.1f  %7.1f  %7.1f  %6d\n", stX.median, stX.mean, stX.sigma, stX.min, stX.max, stX.Nmeas);
  }
  
  imN = statsImageN (catalog, flatcorr);
  imX = statsImageX (catalog);
  imM = statsImageM (catalog);
  imD = statsImagedM (catalog);
  
  msN = statsMosaicN (catalog, flatcorr);
  msM = statsMosaicM (catalog);
  msD = statsMosaicdM (catalog);
  msX = statsMosaicX (catalog);
  
  fprintf (stderr, "meas / image:   %7.0f  %7.0f  %7.0f  %7.0f  %7.0f  %6d\n",   imN.median, imN.mean, imN.sigma, imN.min, imN.max, imN.Nmeas);
  fprintf (stderr, "Mcal image:     %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n",   imM.median, imM.mean, imM.sigma, imM.min, imM.max, imM.Nmeas);
  fprintf (stderr, "dMcal image:    %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n",   imD.median, imD.mean, imD.sigma, imD.min, imD.max, imD.Nmeas);
  fprintf (stderr, "chisq image:    %7.1f  %7.1f  %7.1f  %7.1f  %7.1f  %6d\n",   imX.median, imX.mean, imX.sigma, imX.min, imX.max, imX.Nmeas);

  fprintf (stderr, "meas / mosaic:  %7.0f  %7.0f  %7.0f  %7.0f  %7.0f  %6d\n",   msN.median, msN.mean, msN.sigma, msN.min, msN.max, msN.Nmeas);
  fprintf (stderr, "Mcal mosaic:    %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n",   msM.median, msM.mean, msM.sigma, msM.min, msM.max, msM.Nmeas);
  fprintf (stderr, "dMcal mosaic:   %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n",   msD.median, msD.mean, msD.sigma, msD.min, msD.max, msD.Nmeas);
  fprintf (stderr, "chisq mosaic:   %7.1f  %7.1f  %7.1f  %7.1f  %7.1f  %6d\n",   msX.median, msX.mean, msX.sigma, msX.min, msX.max, msX.Nmeas);
}


