Index: /trunk/Ohana/src/addstar/Makefile
===================================================================
--- /trunk/Ohana/src/addstar/Makefile	(revision 5346)
+++ /trunk/Ohana/src/addstar/Makefile	(revision 5347)
@@ -88,5 +88,44 @@
 $(SRC)/args_server.$(ARCH).o \
 $(SRC)/CheckPassword.$(ARCH).o \
-$(SRC)/NewImage.$(ARCH).o 
+$(SRC)/UpdateDatabase_Image.$(ARCH).o \
+$(SRC)/UpdateDatabase_Reflist.$(ARCH).o \
+$(SRC)/UpdateDatabase_Refcat.$(ARCH).o \
+$(SRC)/airmass.$(ARCH).o \
+$(SRC)/build_links.$(ARCH).o \
+$(SRC)/calibrate.$(ARCH).o \
+$(SRC)/find_matches.$(ARCH).o \
+$(SRC)/find_matches_closest.$(ARCH).o \
+$(SRC)/find_matches_refstars.$(ARCH).o \
+$(SRC)/find_subset.$(ARCH).o \
+$(SRC)/get2mass.$(ARCH).o \
+$(SRC)/get2mass_as.$(ARCH).o \
+$(SRC)/get2mass_dr2.$(ARCH).o \
+$(SRC)/getgsc.$(ARCH).o \
+$(SRC)/getusno.$(ARCH).o \
+$(SRC)/greference.$(ARCH).o \
+$(SRC)/gcatalog.$(ARCH).o \
+$(SRC)/gimages.$(ARCH).o \
+$(SRC)/gregion_image.$(ARCH).o \
+$(SRC)/gregion_match.$(ARCH).o \
+$(SRC)/gregion_patch.$(ARCH).o \
+$(SRC)/gregion_star.$(ARCH).o \
+$(SRC)/image-db.$(ARCH).o \
+$(SRC)/in_image.$(ARCH).o \
+$(SRC)/load_pt_catalog.$(ARCH).o \
+$(SRC)/load_subpix.$(ARCH).o \
+$(SRC)/mkcatalog.$(ARCH).o \
+$(SRC)/RegionOps.$(ARCH).o \
+$(SRC)/replace_match.$(ARCH).o \
+$(SRC)/SetSignals.$(ARCH).o \
+$(SRC)/sort_lists.$(ARCH).o \
+$(SRC)/update_coords.$(ARCH).o \
+$(SRC)/wcatalog.$(ARCH).o \
+$(SRC)/Shutdown.$(ARCH).o \
+$(SRC)/edge_check.$(ARCH).o \
+$(SRC)/check_permissions.$(ARCH).o \
+$(SRC)/opening_angle.$(ARCH).o \
+$(SRC)/NewImage.$(ARCH).o \
+$(SRC)/NewReflist.$(ARCH).o \
+$(SRC)/NewRefcat.$(ARCH).o 
 
 CLIENT = \
@@ -94,4 +133,5 @@
 $(SRC)/ConfigInit.$(ARCH).o \
 $(SRC)/gstars.$(ARCH).o \
+$(SRC)/grefstars.$(ARCH).o \
 $(SRC)/load_subpix.$(ARCH).o \
 $(SRC)/rfits.$(ARCH).o \
Index: /trunk/Ohana/src/addstar/doc/notes.txt
===================================================================
--- /trunk/Ohana/src/addstar/doc/notes.txt	(revision 5346)
+++ /trunk/Ohana/src/addstar/doc/notes.txt	(revision 5347)
@@ -6,7 +6,8 @@
   o address MOSAIC somehow
   o replace skyprobe mode
-  - address the cat & ref modes in client/server
-  
-  * add airmass quality to options
+  o complete server db interactions
+  o address the cat & ref modes in client/server
+  o add airmass quality to options
+
   * deal with Myyyy as sky value
   * better way to interpret flips header info for sky?
@@ -15,4 +16,23 @@
     with sorted measure tables; add this as a feature 
     of the load_catalog API?
+
+2005.10.14
+
+  the current db interactions are somewhat in efficient wrt the image
+  table.  the basic outline is as follows:
+
+  - load image overlaps (needs entire image table)
+  - find detection matches (uses overlap information for missed)
+  - save new image (needs calculated calibration value from detection
+    matches)
+  
+  currently, I access the image table twice, and both times I load the
+  entire thing.  I really only need to append in the second case.  To
+  get to that point, I need to:
+
+  - split image overlaps from image db load
+  - define image partial load (analogous with catalog LOAD_MEAS_META)
+  
+  for the moment, I'm being wasteful so I can defer this issue.
 
 2005.10.13
Index: /trunk/Ohana/src/addstar/include/addstar.h
===================================================================
--- /trunk/Ohana/src/addstar/include/addstar.h	(revision 5346)
+++ /trunk/Ohana/src/addstar/include/addstar.h	(revision 5347)
@@ -99,4 +99,5 @@
 void       TrapSignal             PROTO((int sig));
 float      airmass                PROTO((float secz_image, double ra, double dec, double st, double latitude));
+void       SetAirmassQuality      PROTO((int quality));
 void       aregion                PROTO((GSCRegion *region, FILE *f, double ra, double dec));
 AddstarClientOptions args         PROTO((int argc, char **argv, AddstarClientOptions options));
@@ -181,7 +182,12 @@
 int CheckPassword (int BindSocket);
 int NewImage (int BindSocket);
+int NewReflist (int BindSocket);
+int NewRefcat (int BindSocket);
 
-int InitServerSocket (char *hostname, SockAddress *Address);
+int InitServerSocket (SockAddress *Address);
 int WaitServerSocket (int InitSocket, SockAddress *Address, int *validIP, int Nvalid);
 int GetClientSocket (char *hostname);
 
+int UpdateDatabase_Image (AddstarClientOptions *options, Image *image, Coords *mosaic, Stars *stars, int Nstars);
+int UpdateDatabase_Reflist (AddstarClientOptions *options, Stars *stars, int Nstars);
+int UpdateDatabase_Refcat (AddstarClientOptions *options, GSCRegion *UserPatch, char *refcat);
Index: /trunk/Ohana/src/addstar/src/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/addstar/src/ConfigInit.c	(revision 5346)
+++ /trunk/Ohana/src/addstar/src/ConfigInit.c	(revision 5347)
@@ -113,5 +113,5 @@
   free (config);
   free (file);
-
+  return (options);
 }
 
Index: /trunk/Ohana/src/addstar/src/NewImage.c
===================================================================
--- /trunk/Ohana/src/addstar/src/NewImage.c	(revision 5346)
+++ /trunk/Ohana/src/addstar/src/NewImage.c	(revision 5347)
@@ -3,43 +3,54 @@
 int NewImage (int BindSocket) {
 
-  int i, Nstars, Noptions, Nimage;
+  int N, Nstars;
   Stars *stars;
   Image *image;
+  Coords *mosaic;
   AddstarClientOptions *options;
 
   /* accept incoming data set */
-  if (!Recv_AddstarClientOptions (BindSocket, &options, &Noptions)) {
-    close (BindSocket);
-    return (FALSE);
+  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_Image (BindSocket, &image, &Nimage)) {
-    close (BindSocket);
-    return (FALSE);
+  if (!Recv_Image (BindSocket, &image, &N)) {
+    fprintf (stderr, "error: problem receiving image data\n");
+    goto reject;
+  }
+  if (N != 1) {
+    fprintf (stderr, "error: invalid number of images (%d)\n", N);
+    goto reject;
   }
 
+  if (options[0].mosaic) {
+    if (!Recv_Coords (BindSocket, &mosaic, &N)) {
+      fprintf (stderr, "error: problem receiving mosaic coordinates\n");
+      goto reject;
+    }
+    if (N != 1) {
+      fprintf (stderr, "error: invalid number of mosaic coords (%d)\n", N);
+      goto reject;
+    }
+  }    
+
   if (!Recv_Stars (BindSocket, &stars, &Nstars)) {
-    close (BindSocket);
-    return (FALSE);
+    fprintf (stderr, "error: problem receiving star data\n");
+    goto reject;
   }
-  /* XXX EAM : error check : 1 option, 1 image */
+  fprintf (stderr, "accepted %s, %d stars\n", image[0].name, Nstars);
 
   /* add to db */
-
-  fprintf (stderr, "options: %d %d %d %d\n", 
-	   options[0].mode, 
-	   options[0].photcode, 
-	   options[0].timeref,
-	   options[0].skip_missed);
-
-//  for (i = 0; i < Nstars; i++) {
-//    fprintf (stderr, "R, D: %f, %f\n", stars[i].R, stars[i].D);
-//  }
-
-  for (i = 0; i < Nimage; i++) {
-    fprintf (stderr, "name: %s, %d stars\n", image[i].name, Nstars);
-  }
+  UpdateDatabase_Image (options, image, mosaic, stars, Nstars);
 
   close (BindSocket);
   return (TRUE);
+
+reject:
+  close (BindSocket);
+  return (FALSE);
 }
Index: /trunk/Ohana/src/addstar/src/SocketOps.c
===================================================================
--- /trunk/Ohana/src/addstar/src/SocketOps.c	(revision 5346)
+++ /trunk/Ohana/src/addstar/src/SocketOps.c	(revision 5347)
@@ -4,14 +4,13 @@
 # define MY_WAIT 500
 
-int InitServerSocket (char *hostname, SockAddress *Address) {
-
-  int i, status, InitSocket, length;
+int InitServerSocket (SockAddress *Address) {
+
+  int status, InitSocket, length;
+
+# if (0)
   struct hostent  *host;
   char tmpline[80], hostip[80];
 
   host = gethostbyname (hostname);
-  fprintf (stderr, "errno: %d\n", h_errno);
-  fprintf (stderr, "host: %d\n", host);
-
   bzero (hostip, 80);
   for (i = 0; i < host[0].h_length; i++) {
@@ -20,9 +19,11 @@
     if (i < host[0].h_length - 1) strcat (hostip, ".");
   }
-
-  // use this line to bind any address / port?
-  // Address.sin_addr.s_addr = INADDR_ANY;
+# endif
+  
   Address[0].sin_family = AF_INET;
   Address[0].sin_port   = MY_PORT;
+  Address[0].sin_addr.s_addr = INADDR_ANY; // use this line to bind any address / port?
+
+# if (0)  
   status = inet_aton (hostip, &Address[0].sin_addr);
   if (!status) {
@@ -30,4 +31,5 @@
     exit (2);
   }
+# endif
 
   length = sizeof(Address[0]);
@@ -52,5 +54,5 @@
   }
 
-  if (VERBOSE) fprintf (stderr, "socket listening on %s (%s:%d)\n", host[0].h_name, hostip, MY_PORT);
+  // if (VERBOSE) fprintf (stderr, "socket listening on %s (%s:%d)\n", host[0].h_name, hostip, MY_PORT);
   return (InitSocket);
 }
@@ -159,2 +161,50 @@
   return (InitSocket);
 }
+
+int InitServerSocket_Named (char *hostname, SockAddress *Address) {
+
+  int i, status, InitSocket, length;
+  struct hostent  *host;
+  char tmpline[80], hostip[80];
+
+  host = gethostbyname (hostname);
+  bzero (hostip, 80);
+  for (i = 0; i < host[0].h_length; i++) {
+    sprintf (tmpline, "%u", (0xff & host[0].h_addr[i]));
+    strcat (hostip, tmpline);
+    if (i < host[0].h_length - 1) strcat (hostip, ".");
+  }
+  
+  Address[0].sin_family = AF_INET;
+  Address[0].sin_port   = MY_PORT;
+  status = inet_aton (hostip, &Address[0].sin_addr);
+  if (!status) {
+    fprintf (stderr, "invalid address\n");
+    exit (2);
+  }
+
+  length = sizeof(Address[0]);
+
+  InitSocket = socket (PF_INET, SOCK_STREAM, 0);
+  if (InitSocket == -1) {
+    perror ("socket: ");
+    exit (2);
+  }
+
+  fprintf (stderr, "init sock: %d, len: %d\n", InitSocket, length);
+  status = bind (InitSocket, (struct sockaddr *) Address, length);
+  if (status == -1) {
+    perror ("bind: ");
+    exit (2);
+  }
+
+  status = listen (InitSocket, 10);
+  if (status == -1) {
+    perror ("listen: ");
+    exit (2);
+  }
+
+  if (VERBOSE) fprintf (stderr, "socket listening on %s (%s:%d)\n", host[0].h_name, hostip, MY_PORT);
+  return (InitSocket);
+}
+
Index: /trunk/Ohana/src/addstar/src/addstarc.c
===================================================================
--- /trunk/Ohana/src/addstar/src/addstarc.c	(revision 5346)
+++ /trunk/Ohana/src/addstar/src/addstarc.c	(revision 5347)
@@ -3,5 +3,5 @@
 int main (int argc, char **argv) {
 
-  int Nstars, Nimage;
+  int Nstars;
   int BindSocket;
   AddstarClientOptions options;
@@ -13,7 +13,4 @@
   options = args_client (argc, argv, options);
 
-  /* load data */
-  stars = gstars (argv[1], &Nstars, MOSAIC, &image);
-
   /* set up server connection */
   BindSocket = GetClientSocket (HOSTNAME);
@@ -21,9 +18,38 @@
 
   /* send new data to server */
-  SendCommand (BindSocket, 5, "IMAGE");
-  Send_AddstarClientOptions (BindSocket, &options, 1, TRUE);
-  Send_Image (BindSocket, &image, 1, FALSE);
-  Send_Stars (BindSocket, stars, Nstars, FALSE);
+  switch (options.mode) {
+    case M_IMAGE:
+      /* load data */
+      stars = gstars (argv[1], &Nstars, options.photcode, &image);
 
-  exit (0);
+      /* send data to server */
+      SendCommand (BindSocket, 5, "IMAGE");
+      Send_AddstarClientOptions (BindSocket, &options, 1, TRUE);
+      Send_Image (BindSocket, &image, 1, FALSE);
+      if (options.mosaic) {
+	Send_Coords (BindSocket, MOSAIC, 1, FALSE);
+      }
+      Send_Stars (BindSocket, stars, Nstars, FALSE);
+      exit (0);
+
+    case M_REFLIST:
+      /* load data */
+      stars = grefstars (argv[1], options.photcode, &Nstars);
+      
+      /* send data to server */
+      SendCommand (BindSocket, 5, "REFLS");
+      Send_AddstarClientOptions (BindSocket, &options, 1, TRUE);
+      Send_Stars (BindSocket, stars, Nstars, FALSE);
+      exit (0);
+
+    case M_REFCAT:
+      /* send data to server */
+      SendCommand (BindSocket, 5, "REFCT");
+      Send_AddstarClientOptions (BindSocket, &options, 1, TRUE);
+      Send_GSCRegion (BindSocket, &UserPatch, 1, TRUE);
+      SendMessage (BindSocket, argv[1]);
+
+      exit (0);
+  }
+  exit (1);
 }
Index: /trunk/Ohana/src/addstar/src/addstard.c
===================================================================
--- /trunk/Ohana/src/addstar/src/addstard.c	(revision 5346)
+++ /trunk/Ohana/src/addstar/src/addstard.c	(revision 5347)
@@ -14,5 +14,5 @@
 
   VERBOSE = TRUE;
-  InitSocket = InitServerSocket (argv[1], &Address);
+  InitSocket = InitServerSocket (&Address);
   
   while (1) {
@@ -25,5 +25,5 @@
     if (!CheckPassword (BindSocket)) continue;
     
-    /* accept command */
+    /* accept command : XXX EAM : long-enough timeout? */
     status = ExpectCommand (BindSocket, 5, 0.1, &message);
     if (status != 0) {
@@ -36,6 +36,16 @@
     /* message options */
     if (!strcmp (message.buffer, "IMAGE")) {
-      fprintf (stderr, "New Image\n");
+      fprintf (stderr, "Image\n");
       NewImage (BindSocket);
+      continue;
+    }
+    if (!strcmp (message.buffer, "REFLS")) {
+      fprintf (stderr, "Reflist\n");
+      NewReflist (BindSocket);
+      continue;
+    }
+    if (!strcmp (message.buffer, "REFCT")) {
+      fprintf (stderr, "Refcat\n");
+      NewRefcat (BindSocket);
       continue;
     }
Index: /trunk/Ohana/src/addstar/src/airmass.c
===================================================================
--- /trunk/Ohana/src/addstar/src/airmass.c	(revision 5346)
+++ /trunk/Ohana/src/addstar/src/airmass.c	(revision 5347)
@@ -1,5 +1,5 @@
 # include "addstar.h"
 
-static AirmassQuality = FALSE;
+static int AirmassQuality = FALSE;
 
 void SetAirmassQuality (int quality) {
Index: /trunk/Ohana/src/addstar/src/args_client.c
===================================================================
--- /trunk/Ohana/src/addstar/src/args_client.c	(revision 5346)
+++ /trunk/Ohana/src/addstar/src/args_client.c	(revision 5347)
@@ -5,5 +5,4 @@
   
   int N;
-  int QUALITY_AIRMASS;
 
   /* check for help request */
@@ -148,8 +147,8 @@
   /*** optional situations ***/
   /* choose high quality airmass vs low quality airmass (per-star vs per-image) */
-  QUALITY_AIRMASS = FALSE;
+  options.quality_airmass = FALSE;
   if ((N = get_argument (argc, argv, "-quality-airmass"))) {
     remove_argument (N, &argc, argv);
-    QUALITY_AIRMASS = TRUE;
+    options.quality_airmass = TRUE;
   }
   /* choose high quality airmass vs low quality airmass (per-star vs per-image) */
@@ -162,5 +161,5 @@
   if ((N = get_argument (argc, argv, "-skyprobe"))) {
     remove_argument (N, &argc, argv);
-    QUALITY_AIRMASS = TRUE;
+    options.quality_airmass = TRUE;
     SUBPIX = TRUE;
   }
Index: /trunk/Ohana/src/addstar/src/args_server.c
===================================================================
--- /trunk/Ohana/src/addstar/src/args_server.c	(revision 5346)
+++ /trunk/Ohana/src/addstar/src/args_server.c	(revision 5347)
@@ -51,9 +51,8 @@
   }
 
-  if (argc != 2) {
-    fprintf (stderr, "USAGE: addstard (host)\n");
+  if (argc != 1) {
+    fprintf (stderr, "USAGE: addstard\n");
     exit (2);
   }
-  return (TRUE);
 }
 
@@ -61,5 +60,5 @@
 
   fprintf (stderr, "USAGE\n");
-  fprintf (stderr, "  addstard (host)\n");
+  fprintf (stderr, "  addstard\n");
   fprintf (stderr, "  -force                	  : force read of database with inconsistent info\n");
   fprintf (stderr, "  -v                    	  : verbose mode\n");
Index: /trunk/Ohana/src/addstar/src/find_matches.c
===================================================================
--- /trunk/Ohana/src/addstar/src/find_matches.c	(revision 5346)
+++ /trunk/Ohana/src/addstar/src/find_matches.c	(revision 5347)
@@ -18,5 +18,6 @@
   gettimeofday (&start, NULL);
 
-  code = GetPhotcodebyCode (options.photcode);
+  /* XXX EAM : options.photcode overridden by image.source.... */
+  code = GetPhotcodebyCode (image[0].source);
 
   /* photcode data - must by of type DEP, (PRI, SEC) - probably should restrict to DEP */
Index: /trunk/Ohana/src/addstar/src/gcatalog.c
===================================================================
--- /trunk/Ohana/src/addstar/src/gcatalog.c	(revision 5346)
+++ /trunk/Ohana/src/addstar/src/gcatalog.c	(revision 5347)
@@ -4,5 +4,4 @@
   
   int Nsecfilt, status;
-  char mode;
 
   /* CATMODE and CATFORMAT determined from catalog */
Index: /trunk/Ohana/src/addstar/src/grefstars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/grefstars.c	(revision 5346)
+++ /trunk/Ohana/src/addstar/src/grefstars.c	(revision 5347)
@@ -35,2 +35,10 @@
   return (stars);
 }
+
+/* stars.found is set here to FALSE.
+   gregion_stars uses this value as a boolean to decide if the star
+     has been associated with a region.
+   gregion_stars resets stars.found to -1
+   find_match_refstars uses stars.found to identify the seq number of the
+     star which is found.  it requires an initial value of -1
+*/
Index: /trunk/Ohana/src/addstar/src/gregion_star.c
===================================================================
--- /trunk/Ohana/src/addstar/src/gregion_star.c	(revision 5346)
+++ /trunk/Ohana/src/addstar/src/gregion_star.c	(revision 5347)
@@ -25,7 +25,11 @@
     }
     Nregions ++;
-    CHECK_REALLOCATE (regions, GSCRegion, Nregions, NREGIONS, 100);
+    CHECK_REALLOCATE (regions, GSCRegion, NREGIONS, Nregions, 100);
     
   }
+
+  /* reset to -1 for all stars: required start for find_match_refstars */
+  for (i = 0; i < Nstars; i++) stars[i].found = -1; 
+
   *nregions = Nregions;
   return (regions);
