# include "dvomerge.h"

// dvomerge (catdir.in1) and (catdir.in2) to (catdir.output) [-region Rmin Rmax Dmin Dmax] [
// merge sources from (catdir.from) into (catdir.to)
int main (int argc, char **argv) {

  int i, j, Ns, Ne, depth1, depth2;
  SkyTable *outsky, *insky1, *insky2;
  SkyList *inlist;
  Catalog incatalog, outcatalog;
  char filename[256], *input1, *input2, *output;

  SetSignals ();
  ConfigInit (&argc, argv);

  if (argc != 6) usage();
  if (strcasecmp (argv[2], "and")) usage();
  if (strcasecmp (argv[4], "to")) usage();

  input1 = argv[1];
  input2 = argv[3];
  output = argv[5];

  // the first input define the photcode table & db layout
  sprintf (filename, "%s/Photcodes.dat", input1);
  if (!LoadPhotcodes (filename, NULL, FALSE)) {
    fprintf (stderr, "error loading photcode table %s\n", filename);
    exit (1);
  }
  // save the photcodes in the output catdir
  sprintf (filename, "%s/Photcodes.dat", output);
  if (!check_file_access (filename, TRUE, TRUE, TRUE)) {
    fprintf (stderr, "error creating output catdir %s\n", output);
    exit (1);
  }
  if (!SavePhotcodesFITS (filename)) {
    fprintf (stderr, "error saving photcode table %s\n", filename);
    exit (1);
  }

  // load the sky table for the existing database
  insky1 = SkyTableLoadOptimal (input1, NULL, NULL, FALSE, SKY_DEPTH_HST, VERBOSE);
  SkyTableSetFilenames (insky1, input1, "cpt");

  insky2 = SkyTableLoadOptimal (input2, NULL, NULL, FALSE, SKY_DEPTH_HST, VERBOSE);
  SkyTableSetFilenames (insky2, input2, "cpt");

  // generate an output table populated at the desired depth
  outsky = SkyTableLoadOptimal (output, NULL, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
  SkyTableSetFilenames (outsky, output, "cpt");

  // loop over the populatable output tables; check for data in input1 and/or input2
  // in the corresponding regions

  SkyTablePopulatedRange (&Ns, &Ne, insky1, 0);
  depth1 = insky1[0].regions[Ns].depth;
  
  SkyTablePopulatedRange (&Ns, &Ne, insky2, 0);
  depth2 = insky2[0].regions[Ns].depth;

  // loop over the populatable output regions
  for (i = 0; i < outsky[0].Nregions; i++) {
    if (!outsky[0].regions[i].table) continue;
    fprintf (stderr, "output: %s\n", outsky[0].regions[i].name);

    // load / create output catalog
    LoadCatalog (&outcatalog, &outsky[0].regions[i], outsky[0].filename[i], "w");

    // combine only tables at equal or larger depth
      
    // load in all of the tables from input1 for this region
    inlist = SkyListByBounds (insky1, depth1, outsky[0].regions[i].Rmin, outsky[0].regions[i].Rmax, outsky[0].regions[i].Dmin, outsky[0].regions[i].Dmax);
    for (j = 0; j < inlist[0].Nregions; j++) {
      fprintf (stderr, "input 1: %s\n", inlist[0].regions[j][0].name);

      // load input catalog (1)
      LoadCatalog (&incatalog, inlist[0].regions[j], inlist[0].filename[j], "r");

      // skip empty input catalogs
      if (!incatalog.Naves_disk) {
	dvo_catalog_unlock (&incatalog);
	dvo_catalog_free (&incatalog);
	continue;
      }
      merge_catalogs_new (&outsky[0].regions[i], &outcatalog, &incatalog);
      dvo_catalog_unlock (&incatalog);
      dvo_catalog_free (&incatalog);
    }
    SkyListFree (inlist);

    // load in all of the tables from input2 for this region
    inlist = SkyListByBounds (insky2, depth2, outsky[0].regions[i].Rmin, outsky[0].regions[i].Rmax, outsky[0].regions[i].Dmin, outsky[0].regions[i].Dmax);
    for (j = 0; j < inlist[0].Nregions; j++) {
      fprintf (stderr, "input 2: %s\n", inlist[0].regions[j][0].name);

      // load input catalog (1)
      LoadCatalog (&incatalog, inlist[0].regions[j], inlist[0].filename[j], "r");

      // skip empty input catalogs
      if (!incatalog.Naves_disk) {
	dvo_catalog_unlock (&incatalog);
	dvo_catalog_free (&incatalog);
	continue;
      }
      merge_catalogs_old (&outsky[0].regions[i], &outcatalog, &incatalog, RADIUS);
      dvo_catalog_unlock (&incatalog);
      dvo_catalog_free (&incatalog);
    }
    SkyListFree (inlist);

    outcatalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
    dvo_catalog_save (&outcatalog, VERBOSE);
    dvo_catalog_unlock (&outcatalog);
    dvo_catalog_free (&outcatalog);
  }

  // save the output sky table copy
  sprintf (filename, "%s/SkyTable.fits", output);
  check_file_access (filename, TRUE, TRUE, VERBOSE);
  if (!SkyTableSave (outsky, filename)) {
    fprintf (stderr, "ERROR: failed to save sky table for %s\n", output);
    exit (1);
  }

  exit (0);
}

/* 

   for the upgrade of the synth database, at a minimum I need to use an output format that uses
   doubles for the astrometry.  Optionally I should merge in the 2MASS photometry measurements, since that
   would be quite useful for comparison.  

   generality questions:
   * optional



   */

