Index: /trunk/Ohana/src/addstar/src/NewRefcat.c
===================================================================
--- /trunk/Ohana/src/addstar/src/NewRefcat.c	(revision 5348)
+++ /trunk/Ohana/src/addstar/src/NewRefcat.c	(revision 5348)
@@ -0,0 +1,45 @@
+# include "addstar.h"
+
+int NewRefcat (int BindSocket) {
+
+  int N, status;
+  AddstarClientOptions *options;
+  IOBuffer message;
+  GSCRegion *patch;
+
+  /* accept incoming data set */
+  if (!Recv_AddstarClientOptions (BindSocket, &options, &N)) {
+    fprintf (stderr, "error: problem receiving options\n");
+    goto reject;
+  }
+  if (N != 1) {
+    fprintf (stderr, "error: too many option sets (%d)\n", N);
+    goto reject;
+  }
+
+  if (!Recv_GSCRegion (BindSocket, &patch, &N)) {
+    fprintf (stderr, "error: problem receiving patch\n");
+    goto reject;
+  }
+  if (N != 1) {
+    fprintf (stderr, "error: too many patches (%d)\n", N);
+    goto reject;
+  }
+
+  status = ExpectMessage (BindSocket, 0.25, &message);
+  if (status != 0) {
+    if (VERBOSE) fprintf (stderr, "failed connection\n");
+    FreeIOBuffer (&message);
+    goto reject;
+  }
+
+  /* add to db */
+  UpdateDatabase_Refcat (options, patch, message.buffer);
+
+  close (BindSocket);
+  return (TRUE);
+
+reject:
+  close (BindSocket);
+  return (FALSE);
+}
Index: /trunk/Ohana/src/addstar/src/NewReflist.c
===================================================================
--- /trunk/Ohana/src/addstar/src/NewReflist.c	(revision 5348)
+++ /trunk/Ohana/src/addstar/src/NewReflist.c	(revision 5348)
@@ -0,0 +1,34 @@
+# include "addstar.h"
+
+int NewReflist (int BindSocket) {
+
+  int N, Nstars;
+  Stars *stars;
+  AddstarClientOptions *options;
+
+  /* accept incoming data set */
+  if (!Recv_AddstarClientOptions (BindSocket, &options, &N)) {
+    fprintf (stderr, "error: problem receiving options\n");
+    goto reject;
+  }
+  if (N != 1) {
+    fprintf (stderr, "error: too many option sets (%d)\n", N);
+    goto reject;
+  }
+
+  if (!Recv_Stars (BindSocket, &stars, &Nstars)) {
+    fprintf (stderr, "error: problem receiving star data\n");
+    goto reject;
+  }
+  fprintf (stderr, "accepted %d stars\n", Nstars);
+
+  /* add to db */
+  UpdateDatabase_Reflist (options, stars, Nstars);
+
+  close (BindSocket);
+  return (TRUE);
+
+reject:
+  close (BindSocket);
+  return (FALSE);
+}
Index: /trunk/Ohana/src/addstar/src/UpdateDatabase_Image.c
===================================================================
--- /trunk/Ohana/src/addstar/src/UpdateDatabase_Image.c	(revision 5348)
+++ /trunk/Ohana/src/addstar/src/UpdateDatabase_Image.c	(revision 5348)
@@ -0,0 +1,99 @@
+# include "addstar.h"
+
+int UpdateDatabase_Image (AddstarClientOptions *options, Image *image, Coords *mosaic, Stars *stars, int Nstars) {
+
+  int i, Noverlap, Nregions;
+  Image *overlap;
+  Catalog catalog;
+  GSCRegion *regions;
+
+  if (options[0].update) {
+    catalog.catflags = LOAD_AVES | LOAD_MEAS_META | LOAD_MISS | LOAD_SECF;
+  } else {
+    catalog.catflags = LOAD_AVES | LOAD_MEAS      | LOAD_MISS | LOAD_SECF;
+  }
+  
+  regions = NULL;
+
+  if (options[0].mode != M_IMAGE) {
+    fprintf (stderr, "error: expecting only IMAGE mode\n");
+    return (FALSE);
+  }
+
+  SetAirmassQuality (options[0].quality_airmass);
+
+  /*** find image overlaps, if needed ***/
+  Noverlap = 0;
+  overlap = NULL;
+  if (!options[0].skip_missed) { 
+    FITS_DB db;
+
+    set_db (&db);
+
+    /* XXX EAM : this should be SOFT, not XCLD */
+    lock_image_db (&db, ImageCat);
+
+    /* load images / build overlap : XXX EAM : split into two functions? */
+    overlap = gimages (&db, image, mosaic, &Noverlap);
+    unlock_image_db (&db);
+  }
+
+  /*** update catalog: average, measure, etc ***/
+
+  /* find correpsonding regions for image */
+  regions = gregion_image (image, mosaic, &Nregions);
+
+  /* reduce regions to existing subset, if necessary */
+  if (options[0].only_match || options[0].existing_regions) {
+    regions = gregion_match (regions, &Nregions);
+  }
+  if (VERBOSE) fprintf (stderr, "writing to %d regions\n", Nregions);
+
+  for (i = 0; i < Nregions; i++) {
+
+    load_pt_catalog (&catalog, &regions[i]);
+
+    /* for only_match, skip empty catalogs XXX EAM : this leaves behind empty files */
+    if ((catalog.Nave_disk == 0) && options[0].only_match) {
+      unlock_catalog (&catalog);
+      continue;
+    }
+
+    if (options[0].closest) {
+      find_matches_closest (&regions[i], stars, Nstars, &catalog, image, overlap, Noverlap, mosaic, options[0]);
+    } else {
+      find_matches (&regions[i], stars, Nstars, &catalog, image, overlap, Noverlap, mosaic, options[0]);
+    }
+
+    SetProtect (TRUE);
+    if (!options[0].only_images) wcatalog (&catalog);
+    SetProtect (FALSE);
+    unlock_catalog (&catalog);
+  }
+
+  if (options[0].calibrate) { FindCalibration (image); }
+
+  /*** load image db, save new image ***/
+  { 
+    FITS_DB db;
+
+    set_db (&db);
+
+    lock_image_db (&db, ImageCat);
+
+    /* load images / build overlap : XXX EAM : split into two functions */
+    overlap = gimages (&db, image, mosaic, &Noverlap);
+
+    if (db.dbstate == LCK_EMPTY) {
+      create_image_db (&db);
+    }
+  
+    /* write out new image */
+    dvo_image_addrows (&db, image, 1);
+    SetProtect (TRUE);
+    dvo_image_update (&db, VERBOSE);
+    SetProtect (FALSE);
+    unlock_image_db (&db);
+  }
+  return (TRUE);
+}
Index: /trunk/Ohana/src/addstar/src/UpdateDatabase_Refcat.c
===================================================================
--- /trunk/Ohana/src/addstar/src/UpdateDatabase_Refcat.c	(revision 5348)
+++ /trunk/Ohana/src/addstar/src/UpdateDatabase_Refcat.c	(revision 5348)
@@ -0,0 +1,72 @@
+# include "addstar.h"
+
+int UpdateDatabase_Refcat (AddstarClientOptions *options, GSCRegion *patch, char *refcat) {
+
+  int i, Nstars, Nsubset, Nregions;
+  Catalog catalog;
+  GSCRegion *regions;
+  Stars *stars, **subset;
+
+  if (options[0].update) {
+    catalog.catflags = LOAD_AVES | LOAD_MEAS_META | LOAD_MISS | LOAD_SECF;
+  } else {
+    catalog.catflags = LOAD_AVES | LOAD_MEAS      | LOAD_MISS | LOAD_SECF;
+  }
+  
+  if (options[0].mode != M_REFCAT) {
+    fprintf (stderr, "error: expecting only REFCAT mode\n");
+    return (FALSE);
+  }
+
+  /* check on directory, create if needed */
+  { 
+    int status, cmode;
+    struct stat filestat;
+    
+    status = stat (CATDIR, &filestat);
+    if (status == -1) {
+      fprintf (stderr, "directory %s does not exist, creating...\n", CATDIR);
+      cmode = S_IRWXU | S_IRWXG | S_IRWXO;
+      status = mkdir (CATDIR, cmode);
+      if (status == -1) {
+	fprintf (stderr, "ERROR: can't create %s\n", CATDIR);
+	exit (1);
+      }
+    } 
+  }
+
+  /*** update catalog: average, measure, etc ***/
+  
+  /* find correpsonding regions for image */
+  regions = gregion_patch (patch, &Nregions);
+
+  /* reduce regions to existing subset, if necessary */
+  if (options[0].only_match || options[0].existing_regions) {
+    regions = gregion_match (regions, &Nregions);
+  }
+  if (VERBOSE) fprintf (stderr, "writing to %d regions\n", Nregions);
+
+  for (i = 0; i < Nregions; i++) {
+
+    load_pt_catalog (&catalog, &regions[i]);
+
+    /* for only_match, skip empty catalogs XXX EAM : this leaves behind empty files */
+    if ((catalog.Nave_disk == 0) && options[0].only_match) {
+      unlock_catalog (&catalog);
+      continue;
+    }
+
+    /* load stars from corresponding catalog */
+    stars = grefcat (refcat, &regions[i], options[0].photcode, &Nstars);
+
+    subset = find_subset (&regions[i], stars, Nstars, &Nsubset);
+    find_matches_refstars (&regions[i], subset, Nsubset, &catalog, options[0]);
+    if (Nsubset) free (subset);
+
+    SetProtect (TRUE);
+    if (!options[0].only_images) wcatalog (&catalog);
+    SetProtect (FALSE);
+    unlock_catalog (&catalog);
+  }
+  return (TRUE);
+}
Index: /trunk/Ohana/src/addstar/src/UpdateDatabase_Reflist.c
===================================================================
--- /trunk/Ohana/src/addstar/src/UpdateDatabase_Reflist.c	(revision 5348)
+++ /trunk/Ohana/src/addstar/src/UpdateDatabase_Reflist.c	(revision 5348)
@@ -0,0 +1,69 @@
+# include "addstar.h"
+
+int UpdateDatabase_Reflist (AddstarClientOptions *options, Stars *stars, int Nstars) {
+
+  int i, Nsubset, Nregions;
+  Catalog catalog;
+  GSCRegion *regions;
+  Stars **subset;
+
+  if (options[0].update) {
+    catalog.catflags = LOAD_AVES | LOAD_MEAS_META | LOAD_MISS | LOAD_SECF;
+  } else {
+    catalog.catflags = LOAD_AVES | LOAD_MEAS      | LOAD_MISS | LOAD_SECF;
+  }
+  
+  if (options[0].mode != M_REFLIST) {
+    fprintf (stderr, "error: expecting only REFLIST mode\n");
+    return (FALSE);
+  }
+
+  /* check on directory, create if needed */
+  { 
+    int status, cmode;
+    struct stat filestat;
+    
+    status = stat (CATDIR, &filestat);
+    if (status == -1) {
+      fprintf (stderr, "directory %s does not exist, creating...\n", CATDIR);
+      cmode = S_IRWXU | S_IRWXG | S_IRWXO;
+      status = mkdir (CATDIR, cmode);
+      if (status == -1) {
+	fprintf (stderr, "ERROR: can't create %s\n", CATDIR);
+	exit (1);
+      }
+    } 
+  }
+
+  /*** update catalog: average, measure, etc ***/
+  
+  /* find correpsonding regions for image */
+  regions = gregion_stars (stars, Nstars, &Nregions);
+
+  /* reduce regions to existing subset, if necessary */
+  if (options[0].only_match || options[0].existing_regions) {
+    regions = gregion_match (regions, &Nregions);
+  }
+  if (VERBOSE) fprintf (stderr, "writing to %d regions\n", Nregions);
+
+  for (i = 0; i < Nregions; i++) {
+
+    load_pt_catalog (&catalog, &regions[i]);
+
+    /* for only_match, skip empty catalogs XXX EAM : this leaves behind empty files */
+    if ((catalog.Nave_disk == 0) && options[0].only_match) {
+      unlock_catalog (&catalog);
+      continue;
+    }
+
+    subset = find_subset (&regions[i], stars, Nstars, &Nsubset);
+    find_matches_refstars (&regions[i], subset, Nsubset, &catalog, options[0]);
+    if (Nsubset) free (subset);
+
+    SetProtect (TRUE);
+    if (!options[0].only_images) wcatalog (&catalog);
+    SetProtect (FALSE);
+    unlock_catalog (&catalog);
+  }
+  return (TRUE);
+}
