# include "addstar.h"

main (argc, argv)
int argc;
char **argv;
{

  int i, j, Nstars, Nimage, Nregions;
  Average *average;
  Measure *measure;
  Missing *missing;
  Catalog catalog;
  GSCRegion region;
  FILE *f;
  
  if (argc < 4) {
    fprintf (stderr, "USAGE: dumpcat catalog output mode(0,1,2,3)\n");
    clear_lockfile ();
    exit (0);
  }

  strcpy (region.filename, argv[1]);

  gcatalog (&region, &catalog);

  f = fopen (argv[2], "w");
  if (f == (FILE *) NULL) {
    fprintf (stderr, "failed to open file %s for write\n", argv[2]);
    clear_lockfile ();
    exit (0);
  }

  average = catalog.average;
  for (i = 0; i < catalog.Naverage; i++) {
    fprintf (f, "%10.6f %10.6f %7.3f %3d %3d %8d %d\n", average[i].R, average[i].D, 
	     0.001*average[i].M, average[i].Nm, average[i].Nn, average[i].offset, catalog.measure[average[i].offset].t);
    if ((atof(argv[3]) == 1) || (atof(argv[3]) == 3)) {
      measure = &catalog.measure[average[i].offset];
      for (j = 0; j < average[i].Nm; j++) {
	fprintf (f, "  %d %d %f %f %d\n", measure[j].dR, measure[j].dD, 0.001*measure[j].M, 
		  measure[j].t, measure[j].average);
      }
    }
    if ((atof(argv[3]) == 2) || (atof(argv[3]) == 3)) {
      if (average[i].Nn > 0) {
	missing = &catalog.missing[average[i].missing];
	for (j = 0; j < average[i].Nn; j++) {
	  fprintf (f, "  %f\n", missing[j].t);
	}
      } else { 
	fprintf (f, "no missing obs\n");
      }
    }
  }

    

  fclose (f);
    

}
