# include "dvomerge.h"

/*** generate the IDmap for the given databases, and update the output image table ***/
int dvomergeImagesUpdate (IDmapType *IDmap, char *input, char *output) { 

  FITS_DB inDB;
  FITS_DB outDB;
  int    status;

  /*** load input1/Images.dat ***/
  sprintf (ImageCat, "%s/Images.dat", input);
  inDB.mode   = dvo_catalog_catmode (CATMODE);
  inDB.format = dvo_catalog_catformat (CATFORMAT);
  status       = dvo_image_lock (&inDB, ImageCat, 3600.0, LCK_SOFT);  // shorter timeout?
  if (!status) Shutdown ("ERROR: failure to lock image catalog %s", inDB.filename);

  // load the image table 
  if (inDB.dbstate == LCK_EMPTY) {
    dvo_image_unlock (&inDB); // unlock input
    IDmap->Nmap = 0;
    return TRUE;
  }
  if (!dvo_image_load (&inDB, VERBOSE, TRUE)) {
    Shutdown ("can't read input image catalog %s", inDB.filename);
  }

  /*** load output/Images.dat ***/
  sprintf (ImageCat, "%s/Images.dat", output);
  outDB.mode   = dvo_catalog_catmode (CATMODE);
  outDB.format = dvo_catalog_catformat (CATFORMAT);
  status       = dvo_image_lock (&outDB, ImageCat, 3600.0, LCK_XCLD);  // shorter timeout?
  if (!status) Shutdown ("ERROR: failure to lock image catalog %s", outDB.filename);

  /* load the image table */
  if (outDB.dbstate == LCK_EMPTY) {
    dvo_image_create (&outDB, GetZeroPoint());
  } else {
    if (!dvo_image_load (&outDB, VERBOSE, TRUE)) {
      Shutdown ("can't read output image catalog %s", outDB.filename);
    }
  }

  // convert database table to internal structure & add to output image db
  dvo_image_merge_dbs(IDmap, &outDB, &inDB);
  dvo_image_unlock (&inDB); // unlock input

  SetProtect (TRUE);
  dvo_image_save (&outDB, VERBOSE);
  SetProtect (FALSE);
  dvo_image_unlock (&outDB); // unlock output

  return TRUE;
}

/*** generate the map for the given 2 databases ***/
int dvomergeImagesGetMap (IDmapType *IDmap, char *input, char *output) { 

  FITS_DB inDB;
  FITS_DB outDB;
  int    status;

  /*** load input1/Images.dat ***/
  sprintf (ImageCat, "%s/Images.dat", input);
  inDB.mode   = dvo_catalog_catmode (CATMODE);
  inDB.format = dvo_catalog_catformat (CATFORMAT);
  status       = dvo_image_lock (&inDB, ImageCat, 5400.0, LCK_XCLD);  // shorter timeout?
  if (!status) Shutdown ("ERROR: failure to lock image catalog %s", inDB.filename);

  // load the image table
  if (inDB.dbstate == LCK_EMPTY) {
    dvo_image_unlock (&inDB); // unlock input
    IDmap->Nmap = 0;
    return TRUE;
  }

  // the input database is allowed to have no images 
  if (!dvo_image_load (&inDB, VERBOSE, TRUE)) {
    Shutdown ("can't read input image catalog %s", inDB.filename);
  }
  dvo_image_unlock (&inDB); // unlock input

  /*** load output/Images.dat ***/
  sprintf (ImageCat, "%s/Images.dat", output);
  outDB.mode   = dvo_catalog_catmode (CATMODE);
  outDB.format = dvo_catalog_catformat (CATFORMAT);
  status       = dvo_image_lock (&outDB, ImageCat, 5400.0, LCK_XCLD);  // shorter timeout?
  if (!status) Shutdown ("ERROR: failure to lock image catalog %s", outDB.filename);

  /* load the image table */
  if (outDB.dbstate == LCK_EMPTY) {
    Shutdown ("for dvomerge -continue or dvomerge_client, the output database must exist");
  } 
  if (!dvo_image_load (&outDB, VERBOSE, TRUE)) {
    Shutdown ("can't read output image catalog %s", outDB.filename);
  }
  dvo_image_unlock (&outDB); // unlock output

  // convert database table to internal structure & add to output image db
  dvo_image_match_dbs(IDmap, &outDB, &inDB);

  return TRUE;
}
