Index: /trunk/Ohana/src/addstar/def/AddstarClientOptions.d
===================================================================
--- /trunk/Ohana/src/addstar/def/AddstarClientOptions.d	(revision 5271)
+++ /trunk/Ohana/src/addstar/def/AddstarClientOptions.d	(revision 5271)
@@ -0,0 +1,18 @@
+STRUCT  AddstarClientOptions
+EXTNAME OPTIONS
+TYPE    BINTABLE
+SIZE    52
+
+FIELD  	  mode,             MODE,                 int,            data source mode
+FIELD  	  existing_regions, EXISTING_REGIONS,     int,            use only existing regions
+FIELD  	  only_match,       ONLY_MATCH,           int,            only update matched stars
+FIELD  	  skip_missed,      SKIP_MISSED,          int,            .
+FIELD  	  replace,          REPLACE,              int,            .
+FIELD  	  closest,          CLOSEST,              int,            .
+FIELD  	  nosort,           NOSORT,               int,            .
+FIELD  	  update,           UPDATE,               int,            .
+FIELD  	  only_images,      ONLY_IMAGES,          int,            .
+FIELD  	  calibrate,        CALIBRATE,            int,            .
+FIELD  	  mosaic,           MOSAIC,               int,            does this image require a mosaic coordinate system?
+FIELD  	  photcode,         PHOTCODE,             int,     	  photocode of input data
+FIELD  	  timeref,          TIMEREF,              e_time,  	  time/date of input data (REFLIST only?)
Index: /trunk/Ohana/src/addstar/def/Stars.d
===================================================================
--- /trunk/Ohana/src/addstar/def/Stars.d	(revision 5271)
+++ /trunk/Ohana/src/addstar/def/Stars.d	(revision 5271)
@@ -0,0 +1,21 @@
+STRUCT  Stars
+EXTNAME STARS
+TYPE    BINTABLE
+SIZE    107
+
+FIELD     X,                X,            	  double,         x coordinate on image
+FIELD     Y,                Y,            	  double,         y coordinate on image
+FIELD     R,                R,            	  double,         ra coordinate on sky
+FIELD     D,                D,            	  double,         dec coordinate on sky
+FIELD     M,                M,            	  double,         instrumental mag
+FIELD     dM,               DM,           	  double,         error on mag
+FIELD     dophot,           DOPHOT,       	  char,           dophot type code
+FIELD     sky,              SKY,          	  double,         local sky counts
+FIELD     fx,               FX,           	  double,         object FWHM x-dir
+FIELD     fy,               FY,           	  double,         object FWHM y-dir
+FIELD     df,               DF,           	  double,         object position angle
+FIELD     Mgal,             MGAL,         	  double,         alternative (galaxy) magnitude
+FIELD     Map,              MAP,          	  double,         alternative (aperture) magnitude
+FIELD     found,            FOUND,        	  int,            found in database catalog?
+FIELD     code,             CODE,         	  short,          .
+FIELD     t,                T,            	  e_time,         date/time of exposure (UNIX)
Index: /trunk/Ohana/src/addstar/def/commcode.c
===================================================================
--- /trunk/Ohana/src/addstar/def/commcode.c	(revision 5271)
+++ /trunk/Ohana/src/addstar/def/commcode.c	(revision 5271)
@@ -0,0 +1,65 @@
+# include "commbase.h"
+
+void byteswap_$STRUCT ($STRUCT *data) {
+
+  int i;
+  unsigned char *byte, tmp;
+  
+  i = 0;  // dummy usage in case !defined(BYTE_SWAP)
+  byte = (char *) data;
+
+# ifdef BYTE_SWAP
+  /** BYTE SWAP **/
+# endif
+
+}
+
+int Send_$STRUCT (int device, $STRUCT *data, int Ndata, int copy) {
+
+  int Nbytes;
+  $STRUCT *tmpdata;
+
+  Nbytes = Ndata * sizeof ($STRUCT);
+
+  if (copy) {
+    ALLOCATE (tmpdata, $STRUCT, Ndata);
+    memcpy (tmpdata, data, Nbytes);
+  } else {
+    tmpdata = data;
+  }
+
+  byteswap_$STRUCT (tmpdata);
+
+  SendCommand (device, 16, "NVALUE: %6d", Ndata);
+  SendCommand (device, 16, "NBYTES: %6d", Nbytes);
+  write (device, tmpdata, Nbytes);
+  
+  /* perform handshaking? */
+
+  return (TRUE);
+}
+
+int Recv_$STRUCT (int device, $STRUCT **data, int *Ndata) {
+
+  int ndata;
+  IOBuffer message;
+  $STRUCT *tmpdata;
+
+  ExpectCommand (device, 16, 1.0, &message);
+  sscanf (message.buffer, "%*s %d", &ndata);
+  FreeIOBuffer (&message);
+  
+  /* what is reasonable for timeout? */
+  ExpectMessage (device, 1.0, &message);
+  
+  tmpdata = ($STRUCT *) message.buffer;
+  byteswap_$STRUCT (tmpdata);
+
+  /* double-check data length? */
+  /* perform handshaking? */
+
+  *Ndata = ndata;
+  *data = tmpdata;
+
+  return (TRUE);
+}
Index: /trunk/Ohana/src/addstar/def/commcode.h
===================================================================
--- /trunk/Ohana/src/addstar/def/commcode.h	(revision 5271)
+++ /trunk/Ohana/src/addstar/def/commcode.h	(revision 5271)
@@ -0,0 +1,6 @@
+
+/** STRUCT DEFINITION **/
+
+void byteswap_$STRUCT ($STRUCT *data);
+int Send_$STRUCT (int device, $STRUCT *data, int Ndata, int copy);
+int Recv_$STRUCT (int device, $STRUCT **data, int *Ndata);
Index: /trunk/Ohana/src/addstar/def/common.h
===================================================================
--- /trunk/Ohana/src/addstar/def/common.h	(revision 5271)
+++ /trunk/Ohana/src/addstar/def/common.h	(revision 5271)
@@ -0,0 +1,6 @@
+# ifndef COMMCODE_H
+# define COMMCODE_H
+/* matched endif is added by Makefile */
+
+# define e_time unsigned int
+
Index: /trunk/Ohana/src/addstar/generate
===================================================================
--- /trunk/Ohana/src/addstar/generate	(revision 5271)
+++ /trunk/Ohana/src/addstar/generate	(revision 5271)
@@ -0,0 +1,349 @@
+#!/usr/bin/env perl
+
+$VERBOSE = 0;
+if ($ARGV[0] eq "-v") { $VERBOSE = 1; shift @ARGV; }
+if (@ARGV != 3) { die "generate (schema) (template) (output)\n"; }
+
+$schema   = $ARGV[0];
+$template = $ARGV[1];
+$output   = $ARGV[2];
+
+&parse_schema;
+&parse_template;
+exit 0;
+
+sub parse_schema {
+    open (FILE, "$schema");
+    @list = <FILE>;
+    close (FILE);
+
+    $NAME    = "";
+    $FILE    = "";
+    $Nfield  = 0;
+    $TIMEOUT = 0;
+
+    &init_key ("TIMEOUT");
+    &init_key ("NAME");
+    &init_key ("EXTNAME");
+    &init_key ("STRUCT");
+    &init_key ("FILE");
+    &init_key ("SIZE");
+    &init_key ("TYPE");
+
+    foreach $line (@list) {
+	chop $line;
+	($key, $value) = split (" ", $line, 2);
+	
+	# strip white space from the following
+	if ($key eq "TYPE")    { ($value) = $value =~ m|\s*(\S+)\s*|; }
+	if ($key eq "SIZE")    { ($value) = $value =~ m|\s*(\S+)\s*|; }
+	if ($key eq "STRUCT")  { ($value) = $value =~ m|\s*(\S+)\s*|; }
+	if ($key eq "EXTNAME") { ($value) = $value =~ m|\s*(\S+)\s*|; }
+
+	&set_keypair ($key, $value);
+
+	# there are used internally (not just a replacement)
+	if ($key eq "TYPE")    { $TYPE = $value;   }
+	if ($key eq "SIZE")    { $SIZE = $value;   }
+	if ($key eq "STRUCT")  { $STRUCT = $value; }
+
+	# not a simple key/value entry
+ 	if (($key eq "FIELD") || ($key eq "SUBSTRUCT") || ($key eq "SUBFIELD")) {
+	    ($element, $field, $format, $comment, $unit) = split (/,\s+/, $value, 5);
+	    if ($VERBOSE) { printf "%-20s %-20s %-15s %-35s %-10s\n", $element, $field, $format, $comment, $unit; }
+	    push @element, $element;
+	    push @field,   $field;
+	    push @format,  $format;
+	    push @comment, $comment;
+	    push @unit,    $unit;
+	    push @mode,    $key;
+	}
+    }
+    $Nexpect = 0;
+    if ($TYPE eq "BINTABLE") { $Nexpect = &count_bintablesize; }
+    if ($TYPE eq "TABLE")    { $Nexpect = &count_tablesize;    }
+    if (! $Nexpect) { die "missing valid TYPE\n"; }
+    if ($Nexpect != $SIZE) { die "size mismatch: $Nexpect vs $SIZE\n"; }
+}
+
+sub parse_template {
+    open (FILE, $template);
+    @list = <FILE>;
+    close (FILE);
+
+    open (FILE, ">$output");
+
+    foreach $line (@list) {
+	
+	&check_keypairs;
+
+	print FILE $line;
+
+	# fill in structure
+	if ($line =~ m|/\*\* STRUCT DEFINITION \*\*/|) {
+	    &write_structure;
+	}
+
+	# fill in structure
+	if ($line =~ m|/\*\* TABLE DEFINITION \*\*/|) {
+	    if ($TYPE eq "BINTABLE") { &write_bintabledefs; }
+	    if ($TYPE eq "TABLE")    { &write_tabledefs; }
+	}
+
+	# fill in swaps
+	if ($line =~ m|/\*\* BYTE SWAP \*\*/|) {
+	    &write_byteswaps;
+	}
+    }
+    close (FILE);
+}
+
+sub write_bintabledefs {
+
+    for ($i = 0; $i < @field; $i++) {
+	# skip SUBSTRUCT type of entries:
+	if ($mode[$i] eq "SUBSTRUCT") { next; }
+
+	($type, $Np) = &get_type_array ($format[$i]);
+	
+	# rawshort is a short without byteswapping
+
+	$pt1 = 0;
+	if ($type eq "char")          { $pt1 = "A"; }
+	if ($type eq "byte")   	      { $pt1 = "B"; }
+	if ($type eq "unsigned char") { $pt1 = "B"; }
+	if ($type eq "rawshort")      { $pt1 = "I"; }
+	if ($type eq "short")  	      { $pt1 = "I"; }
+	if ($type eq "unsigned short"){ $pt1 = "I"; }
+	if ($type eq "int")    	      { $pt1 = "J"; }
+	if ($type eq "unsigned int")  { $pt1 = "J"; }
+	if ($type eq "e_time") 	      { $pt1 = "J"; }
+	if ($type eq "float")  	      { $pt1 = "E"; }
+	if ($type eq "double") 	      { $pt1 = "D"; }
+	if (!$pt1) { die "unknown type $type"; }
+
+	if ($Np == 1) {
+	    $pt2 = $pt1;
+	} else {
+	    $pt2 = sprintf "%d%s", $Np, $pt1;
+	}
+
+	printf FILE "  fits_define_bintable_column (header, ";
+	printf FILE "%-8s",               "\"$pt2\", ";
+	printf FILE "%-20s",              "\"$field[$i]\", ";
+	printf FILE "%-35s",              "\"$comment[$i]\", ";
+	printf FILE "%-20s 1.0, 0.0);\n", "\"$unit[$i]\", ";
+    }
+
+}
+
+sub get_type_array {
+    
+    my ($format) = $_[0];
+    my ($type);
+    my ($Np, $N1, $N2, $N3);
+
+    $type = $N1 = $N2 = $N3 = $Np = 0;
+    if (!$type) {
+	($type, $N1, $N2, $N3) = $format =~ m|^(.w+)\[(\d+)\]\[(\d+)\]\[(\d+)\]|;
+	$Np = $N1*$N2*$N3;
+    }
+    if (!$type) {
+	($type, $N1, $N2)      = $format =~ m|^(.+)\[(\d+)\]\[(\d+)\]|;
+	$Np = $N1*$N2;
+    } 
+    if (!$type) {
+	($type, $N1)           = $format =~ m|^(.+)\[(\d+)\]|;
+	$Np = $N1;
+    }
+    if (!$type) { 
+	$type                  = $format; 
+	$Np = 1;
+    }
+    # print "type: $type, Np: $Np\n";
+    if ($Np == 0) { die "syntax error in format/array\n"; }
+    return ($type, $Np);
+}
+
+sub write_tabledefs {
+
+    for ($i = 0; $i < @field; $i++) {
+	# skip SUBSTRUCT type of entries:
+	if ($mode[$i] eq "SUBSTRUCT") { next; }
+
+	($type, $N1, $N2)      = $format[$i] =~ m|^(\w+)\[(\d+)\]\[(\d+)\]|;
+	if ($N2) { die "ASCII table cannot have multi-valued column"; }
+
+	($type, $length) = $format[$i] =~ m|^(\w+)\[([\d\.]+)\]|;
+	if (!$type && !$length) { die "format must be specified for ASCII table"; }
+
+	$pt1 = 0;
+	if ($type eq "char")   	      { $pt1 = sprintf "A%s", $length; }
+	if ($type eq "byte")   	      { $pt1 = sprintf "I%s", $length; }
+	if ($type eq "unsigned char") { $pt1 = sprintf "I%s", $length; }
+	if ($type eq "rawshort")      { $pt1 = sprintf "I%s", $length; }
+	if ($type eq "short")  	      { $pt1 = sprintf "I%s", $length; }
+	if ($type eq "unsigned short"){ $pt1 = sprintf "I%s", $length; }
+	if ($type eq "int")    	      { $pt1 = sprintf "I%s", $length; }
+	if ($type eq "unsigned int")  { $pt1 = sprintf "I%s", $length; }
+	if ($type eq "e_time") 	      { $pt1 = sprintf "I%s", $length; }
+	if ($type eq "float")  	      { $pt1 = sprintf "F%s", $length; }
+	if ($type eq "double") 	      { $pt1 = sprintf "F%s", $length; }
+	if (!$pt1) { die "unknown type $type"; }
+
+	printf FILE "  fits_define_table_column (header, ";
+	printf FILE "%-8s",      "\"$pt1\", ";
+	printf FILE "%-20s",     "\"$field[$i]\", ";
+	printf FILE "%-35s",     "\"$comment[$i]\", ";
+	printf FILE "%-20s);\n", "\"$unit[$i]\"";
+    }
+
+}
+
+sub write_structure {
+    print FILE "typedef struct {\n";
+    for ($i = 0; $i < @element; $i++) {
+	# skip SUBFIELD entries
+	if ($mode[$i] eq "SUBFIELD") { next; }
+
+	# here we only want to match the pattern [1][2][3]..[N]
+	($type, $array) = $format[$i] =~ m|^(\w+)(\[.*\])|;
+	# print "type: $type, array: $array\n";
+	if (!$type && !$array) { $type = $format[$i]; }
+
+	if ($array && (($TYPE eq "BINTABLE") || ($type eq "char"))) {
+	    $pt2 = sprintf "%s%s;", $element[$i], $array;
+	} else {
+	    $pt2 = sprintf "%s;", $element[$i];
+	}
+
+	if ($unit[$i] eq "") {
+	    $pt3 = sprintf "// %s", $comment[$i];
+	} else {
+	    $pt3 = sprintf "// %s (%s)", $comment[$i], $unit[$i];
+	}
+	printf FILE "  %-16s %-21s %s\n", $type, $pt2, $pt3;
+    }    
+    print FILE "} $STRUCT;\n";
+}
+
+# this does not work with ASCII tables, 
+# but should not be needed for ASCII tables!
+sub write_byteswaps {
+    if ($TYPE eq "TABLE") {
+	printf FILE "/*** no byteswaps for ASCII tables ***/\n";
+	return;
+    }
+    $N = 0;
+    for ($i = 0; $i < @field; $i++) {
+	# skip SUBSTRUCT type of entries:
+	if ($mode[$i] eq "SUBSTRUCT") { next; }
+
+	($type, $Np) = &get_type_array ($format[$i]);
+
+	# rawshort is a patch for old photreg tables: provide a fix for the tables
+	# some photreg tables were not byteswapped for certain columns
+
+	$n = 0;
+	if ($type eq "char") 	      { $N +=   $Np; next; }
+	if ($type eq "byte") 	      { $N +=   $Np; next; }
+	if ($type eq "unsigned char") { $N +=   $Np; next; }
+	if ($type eq "rawshort")      { $N += 2*$Np; next; }
+	if ($type eq "short")  	      { $T = "BYTE"; $n = 2; }
+	if ($type eq "unsigned short"){ $T = "BYTE"; $n = 2; }
+	if ($type eq "int")    	      { $T = "WORD"; $n = 4; }
+	if ($type eq "unsigned int")  { $T = "WORD"; $n = 4; }
+	if ($type eq "e_time") 	      { $T = "WORD"; $n = 4; }
+	if ($type eq "float")  	      { $T = "WORD"; $n = 4; }
+	if ($type eq "double") 	      { $T = "DBLE"; $n = 8; }
+	if (!$n) { die "unknown type $type"; }
+	for ($j = 0; $j < $Np; $j++) {
+	    printf FILE "    SWAP_%s (%d); // %s\n", $T, $N, $field[$i];
+	    $N += $n;
+	}
+    }
+}
+
+sub count_bintablesize {
+
+    $Nbytes = 0;
+    for ($i = 0; $i < @field; $i++) {
+	# skip SUBSTRUCT type of entries:
+	if ($mode[$i] eq "SUBSTRUCT") { next; }
+
+	# add [\d\.] to match ascii-type formats
+	($type, $Np) = &get_type_array ($format[$i]);
+
+	$valid = 0;
+	if ($type eq "char")   	       { $Nbytes += 1*$Np; $valid = 1; }
+	if ($type eq "byte")   	       { $Nbytes += 1*$Np; $valid = 1; }
+	if ($type eq "unsigned char")  { $Nbytes += 1*$Np; $valid = 1; }
+	if ($type eq "rawshort")       { $Nbytes += 2*$Np; $valid = 1; }
+	if ($type eq "short")  	       { $Nbytes += 2*$Np; $valid = 1; }
+	if ($type eq "unsigned short") { $Nbytes += 2*$Np; $valid = 1; }
+	if ($type eq "int")    	       { $Nbytes += 4*$Np; $valid = 1; }
+	if ($type eq "unsigned int")   { $Nbytes += 4*$Np; $valid = 1; }
+	if ($type eq "e_time") 	       { $Nbytes += 4*$Np; $valid = 1; }
+	if ($type eq "float")  	       { $Nbytes += 4*$Np; $valid = 1; }
+	if ($type eq "double") 	       { $Nbytes += 8*$Np; $valid = 1; }
+	if (!$valid) { die "unknown type $type"; }
+    }
+    return ($Nbytes);
+}
+
+sub count_tablesize {
+
+    $Nbytes = 0;
+    for ($i = 0; $i < @field; $i++) {
+	# skip SUBSTRUCT type of entries:
+	if ($mode[$i] eq "SUBSTRUCT") { next; }
+
+	($fpt) = $format[$i] =~ m|^\w+\[([\d\.]+)\]|;
+	$length = int($fpt);
+	# print "$format[$i], $length, $fpt\n";
+	if ($length == 0) { die "ASCII table format requires field size"; }
+	$Nbytes += $length;
+    }
+    return ($Nbytes);
+}
+
+sub init_key {
+    my ($key)   = $_[0];
+
+    push @key, $key;
+    push @value, "";
+}
+
+sub set_keypair {
+    my ($i);
+    my ($key)   = $_[0];
+    my ($value) = $_[1];
+
+    for ($i = 0; $i < @key; $i++) {
+	if ($key eq $key[$i]) {
+	    if ($value[$i] ne "") { die "key is multiply defined\n"; }
+	    $value[$i] = $value;
+	    return;
+	}
+    }
+}
+
+sub check_keypairs {
+    my ($i);
+    for ($i = 0; $i < @key; $i++) {
+	# if ($VERBOSE) { print "$key[$i]  -- $value[$i]\n"; }
+	if ($line =~ m|\$$key[$i]|) {
+	    if ($value[$i] eq "") { die "missing value for required key $key[$i]\n"; }
+	    $line =~ s|\$$key[$i]|$value[$i]|g;
+	}
+    }
+}
+
+# we need to find the structure size, including padding 
+# i'm not sure I know the answer to this: it is probably 
+# the total number of bytes rounded up to the largest 
+# data item in the structure (ie, 8 for a double, etc)
+# if we have the size, then we can double check the structure
+# against the expectation at runtime.  for the moment,
+# calculate by hand and add to def.d file 
+
Index: /trunk/Ohana/src/addstar/include/base.h
===================================================================
--- /trunk/Ohana/src/addstar/include/base.h	(revision 5271)
+++ /trunk/Ohana/src/addstar/include/base.h	(revision 5271)
@@ -0,0 +1,25 @@
+# include <ohana.h>
+
+/* socket / pipe communication buffer */
+typedef struct {
+  char *buffer;
+  int   Nalloc;
+  int   Nreset;
+  int   Nblock;
+  int   Nbuffer;
+} IOBuffer;
+
+/* IO Buffer functions */
+int InitIOBuffer (IOBuffer *buffer, int Nalloc);
+int FlushIOBuffer (IOBuffer *buffer);
+int ReadtoIOBuffer (IOBuffer *buffer, int fd);
+int EmptyIOBuffer (IOBuffer *buffer, int Nmax, int fd);
+void FreeIOBuffer (IOBuffer *buffer);
+
+/* communication functions */
+int ExpectMessage (int device, double timeout, IOBuffer *message);
+int ExpectCommand (int device, int length, double timeout, IOBuffer *buffer);
+int SendMessage (int device, char *format, ...);
+int SendCommand (int device, int length, char *format, ...);
+int SendCommandV (int device, int length, char *format, va_list argp);
+
Index: /trunk/Ohana/src/addstar/include/commbase.h
===================================================================
--- /trunk/Ohana/src/addstar/include/commbase.h	(revision 5271)
+++ /trunk/Ohana/src/addstar/include/commbase.h	(revision 5271)
@@ -0,0 +1,35 @@
+# include "base.h"
+
+# define SWAP_BYTE(X) \
+  tmp = byte[X+0]; byte[X+0] = byte[X+1]; byte[X+1] = tmp;
+# define SWAP_WORD(X) \
+  tmp = byte[X+0]; byte[X+0] = byte[X+3]; byte[X+3] = tmp; \
+  tmp = byte[X+1]; byte[X+1] = byte[X+2]; byte[X+2] = tmp;
+# define SWAP_DBLE(X) \
+  tmp = byte[X+0]; byte[X+0] = byte[X+7]; byte[X+7] = tmp; \
+  tmp = byte[X+1]; byte[X+1] = byte[X+6]; byte[X+6] = tmp; \
+  tmp = byte[X+2]; byte[X+2] = byte[X+5]; byte[X+5] = tmp; \
+  tmp = byte[X+3]; byte[X+3] = byte[X+4]; byte[X+4] = tmp;
+
+# ifdef linux
+# define BYTE_SWAP
+# endif
+
+# ifdef sid
+# define BYTE_SWAP
+# endif
+
+# ifdef dec
+# define BYTE_SWAP
+# endif
+
+/*** this file uses data types which must have fixed sizes regardless 
+     of the platform.  It originally used the basic C primitives: 
+       float, double, int, short int, unsigned long int, etc.
+     this breaks under 64 bit (and probably on other systems).
+     I should define internal data types which should be set by the 
+     use of # define statements if needed.  I will cheat for now and use
+     the time_t to replace unsigned long int in this file 
+***/
+
+# include "commcode.h"
Index: /trunk/Ohana/src/addstar/include/commcode.h
===================================================================
--- /trunk/Ohana/src/addstar/include/commcode.h	(revision 5271)
+++ /trunk/Ohana/src/addstar/include/commcode.h	(revision 5271)
@@ -0,0 +1,52 @@
+# ifndef COMMCODE_H
+# define COMMCODE_H
+/* matched endif is added by Makefile */
+
+# define e_time unsigned int
+
+
+/** STRUCT DEFINITION **/
+typedef struct {
+  int              mode;                 // data source mode
+  int              existing_regions;     // use only existing regions
+  int              only_match;           // only update matched stars
+  int              skip_missed;          // .
+  int              replace;              // .
+  int              closest;              // .
+  int              nosort;               // .
+  int              update;               // .
+  int              only_images;          // .
+  int              calibrate;            // .
+  int              mosaic;               // does this image require a mosaic coordinate system?
+  int              photcode;             // photocode of input data
+  e_time           timeref;              // time/date of input data (REFLIST only?)
+} AddstarClientOptions;
+
+void byteswap_AddstarClientOptions (AddstarClientOptions *data);
+int Send_AddstarClientOptions (int device, AddstarClientOptions *data, int Ndata, int copy);
+int Recv_AddstarClientOptions (int device, AddstarClientOptions **data, int *Ndata);
+
+/** STRUCT DEFINITION **/
+typedef struct {
+  double           X;                    // x coordinate on image
+  double           Y;                    // y coordinate on image
+  double           R;                    // ra coordinate on sky
+  double           D;                    // dec coordinate on sky
+  double           M;                    // instrumental mag
+  double           dM;                   // error on mag
+  char             dophot;               // dophot type code
+  double           sky;                  // local sky counts
+  double           fx;                   // object FWHM x-dir
+  double           fy;                   // object FWHM y-dir
+  double           df;                   // object position angle
+  double           Mgal;                 // alternative (galaxy) magnitude
+  double           Map;                  // alternative (aperture) magnitude
+  int              found;                // found in database catalog?
+  short            code;                 // .
+  e_time           t;                    // date/time of exposure (UNIX)
+} Stars;
+
+void byteswap_Stars (Stars *data);
+int Send_Stars (int device, Stars *data, int Ndata, int copy);
+int Recv_Stars (int device, Stars **data, int *Ndata);
+# endif
Index: /trunk/Ohana/src/addstar/src/CheckPassword.c
===================================================================
--- /trunk/Ohana/src/addstar/src/CheckPassword.c	(revision 5271)
+++ /trunk/Ohana/src/addstar/src/CheckPassword.c	(revision 5271)
@@ -0,0 +1,22 @@
+# include "addstar.h"
+
+int CheckPassword (int BindSocket) {
+
+  IOBuffer message;
+  int status;
+
+  status = ExpectCommand (BindSocket, strlen(PASSWORD), 0.1, &message);
+  if (status != 0) {
+    if (VERBOSE) fprintf (stderr, "failed connection\n");
+    FreeIOBuffer (&message);
+    close (BindSocket);
+    return (FALSE);
+  }
+  if (strncmp (message.buffer, PASSWORD, strlen(PASSWORD))) {
+    if (VERBOSE) fprintf (stderr, "invalid password\n");
+    close (BindSocket);
+    return (FALSE);
+  }
+  
+  return (TRUE);
+}
Index: /trunk/Ohana/src/addstar/src/CommOps.c
===================================================================
--- /trunk/Ohana/src/addstar/src/CommOps.c	(revision 5271)
+++ /trunk/Ohana/src/addstar/src/CommOps.c	(revision 5271)
@@ -0,0 +1,114 @@
+# include "base.h"
+
+int ExpectMessage (int device, double timeout, IOBuffer *message) {
+
+  int status, length;
+  IOBuffer command;
+
+  status = ExpectCommand (device, 16, timeout, &command);
+  if (status != 0) {
+    FreeIOBuffer (&command);
+    return (status);
+  }
+
+  /* buffer contains an EOL NULL, we can just sscan it */
+  sscanf (command.buffer, "%*s %d", &length);
+  FreeIOBuffer (&command);
+  
+  status = ExpectCommand (device, length, timeout, message);
+  return (status);
+}
+
+int ExpectCommand (int device, int length, double timeout, IOBuffer *buffer) {
+
+  /* read from device until we have length bytes, or timeout */
+
+  int Nread;
+  double dtime;
+  struct timespec request, remain;
+  struct timeval start, stop;
+
+  gettimeofday (&start, NULL);
+
+  /* avoid blocking on waitpid, test every 1000 usec, up to timeout msec */
+  request.tv_sec = 0;
+  request.tv_nsec = 1000000;
+
+  InitIOBuffer (buffer, length + 1);
+
+  while (buffer[0].Nbuffer < length) {
+    Nread = read (device, &buffer[0].buffer[buffer[0].Nbuffer], length - buffer[0].Nbuffer);
+    
+    if (Nread > 0) {
+      buffer[0].Nbuffer += Nread;
+      continue;
+    }
+
+    if (Nread == -1) {
+      switch (errno) {
+	case EAGAIN:
+	case EIO:
+	  /** no data available in pipe, wait a bit, check for timeout **/
+	  nanosleep (&request, &remain);
+	  break;
+	default:
+	  /** error reading from pipe **/
+	  perror ("ReadtoIOBuffer read error");
+	  return (-2);
+      }
+    }
+
+    if (Nread == 0) return (-3);
+
+    gettimeofday (&stop, NULL);
+    dtime = DTIME (stop, start);
+    if (dtime > timeout) return (-1);
+  }
+  return (0);
+}
+
+/* send a message of arbitrary size, sending the size first */
+int SendMessage (int device, char *format, ...) {
+
+  int Nbyte, status;
+  char tmp;
+  va_list argp;  
+
+  va_start (argp, format);
+  Nbyte = vsnprintf (&tmp, 0, format, argp);
+  va_end (argp);
+
+  if (!Nbyte) return (FALSE);
+
+  va_start (argp, format);
+  SendCommand (device, 16, "NBYTES: %6d", Nbyte);
+  status = SendCommandV (device, Nbyte, format, argp);
+  va_end (argp);
+  return (status);
+}
+
+int SendCommand (int device, int length, char *format, ...) {
+
+  int status;
+  va_list argp;  
+
+  va_start (argp, format);
+  status = SendCommandV (device, length, format, argp);
+  va_end (argp);
+  return (status);
+}
+  
+int SendCommandV (int device, int length, char *format, va_list argp) {
+
+  char *string;
+
+  /* I allocated and zero 1 extra byte */
+  ALLOCATE (string, char, length + 1);
+  memset (string, 0, length + 1);
+  vsnprintf (string, length + 1, format, argp);
+
+  /* fprintf (stderr, "msg: %s\n", string); */
+  write (device, string, length);
+  free (string);
+  return (TRUE);
+}
Index: /trunk/Ohana/src/addstar/src/IOBufferOps.c
===================================================================
--- /trunk/Ohana/src/addstar/src/IOBufferOps.c	(revision 5271)
+++ /trunk/Ohana/src/addstar/src/IOBufferOps.c	(revision 5271)
@@ -0,0 +1,85 @@
+# include "base.h"
+
+int InitIOBuffer (IOBuffer *buffer, int Nalloc) {
+
+  buffer[0].Nalloc = Nalloc;
+  buffer[0].Nreset = Nalloc;
+  buffer[0].Nblock = Nalloc / 2;
+  buffer[0].Nbuffer = 0;
+
+  ALLOCATE (buffer[0].buffer, char, buffer[0].Nalloc);
+  bzero (buffer[0].buffer, buffer[0].Nalloc);
+
+  return (TRUE);
+}
+
+int FlushIOBuffer (IOBuffer *buffer) {
+
+  buffer[0].Nbuffer = 0;
+  buffer[0].Nalloc = buffer[0].Nreset;
+  REALLOCATE (buffer[0].buffer, char, buffer[0].Nalloc);
+  bzero (buffer[0].buffer, buffer[0].Nalloc);
+
+  return (TRUE);
+}
+
+int ReadtoIOBuffer (IOBuffer *buffer, int fd) {
+
+  int Nread, Nfree;
+
+  if (fd == 0) {
+    /* pipe is closed */
+    return (0);
+  }
+
+  Nfree = buffer[0].Nalloc - buffer[0].Nbuffer;
+  if (Nfree < buffer[0].Nblock) {
+    buffer[0].Nalloc += 2*buffer[0].Nblock;
+    REALLOCATE (buffer[0].buffer, char, buffer[0].Nalloc);
+    Nfree = buffer[0].Nalloc - buffer[0].Nbuffer;
+    bzero (buffer[0].buffer + buffer[0].Nbuffer, Nfree);
+  }
+
+  Nread = read (fd, &buffer[0].buffer[buffer[0].Nbuffer], buffer[0].Nblock);
+
+  if (Nread >= 0) {
+    buffer[0].Nbuffer += Nread;
+    return (Nread);
+  }
+
+  if (Nread == -1) {
+    switch (errno) {
+    case EAGAIN:
+    case EIO:
+      /** no data available in pipe **/
+      return (-1);
+    default:
+      /** error reading from pipe **/
+      perror ("ReadtoIOBuffer read error");
+      return (-2);
+    }
+  }
+  return (Nread);
+}
+
+/* read until buffer is empty (Nmax retries) */
+int EmptyIOBuffer (IOBuffer *buffer, int Nmax, int fd) {
+
+  int i, status;
+
+  status = -1;
+  for (i = 0; (status != 0) && (i < Nmax); i++) {
+    status = ReadtoIOBuffer (buffer, fd);
+    if (status == -1) usleep (10000);
+    if (status > 0) i = 0;
+  }
+  if (status == -1) return (FALSE);
+  return (TRUE);
+}
+
+void FreeIOBuffer (IOBuffer *buffer) {
+
+  if (buffer[0].buffer != (char *) NULL) {
+    free (buffer[0].buffer);
+  }
+}
Index: /trunk/Ohana/src/addstar/src/NewImage.c
===================================================================
--- /trunk/Ohana/src/addstar/src/NewImage.c	(revision 5271)
+++ /trunk/Ohana/src/addstar/src/NewImage.c	(revision 5271)
@@ -0,0 +1,41 @@
+# include "addstar.h"
+
+int NewImage (int BindSocket) {
+
+  int i, Nstars, Noptions;
+  Stars *stars;
+  AddstarClientOptions *options;
+
+  /* accept incoming data set */
+  if (!Recv_AddstarClientOptions (BindSocket, &options, &Noptions)) {
+    close (BindSocket);
+    return (FALSE);
+  }
+
+# if (0)
+  if (!Recv_Image (BindSocket, &image)) {
+    close (BindSocket);
+    return (FALSE);
+  }
+# endif
+
+  if (!Recv_Stars (BindSocket, &stars, &Nstars)) {
+    close (BindSocket);
+    return (FALSE);
+  }
+
+  /* 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);
+  }
+
+  close (BindSocket);
+  return (TRUE);
+}
Index: /trunk/Ohana/src/addstar/src/SocketOps.c
===================================================================
--- /trunk/Ohana/src/addstar/src/SocketOps.c	(revision 5271)
+++ /trunk/Ohana/src/addstar/src/SocketOps.c	(revision 5271)
@@ -0,0 +1,158 @@
+# include "addstar.h"
+
+# define MY_PORT 2000
+# define MY_WAIT 500
+
+int InitServerSocket (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, "%3u", (0xff & host[0].h_addr[i]));
+    strcat (hostip, tmpline);
+    if (i < host[0].h_length - 1) strcat (hostip, ".");
+  }
+
+  // use this line to bind any address / port?
+  // Address.sin_addr.s_addr = INADDR_ANY;
+  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);
+}
+
+int WaitServerSocket (int InitSocket, SockAddress *Address, int *validIP, int Nvalid) {
+
+  int i, BindSocket, length;
+  SockAddress Address_in;
+  u_int32_t addr;
+
+  Address_in = Address[0];
+
+  length = sizeof(Address_in);
+
+  /* this is a blocking wait; use in a separate thread */
+  fcntl (InitSocket, F_SETFL, !O_NONBLOCK); 
+
+  fprintf (stderr, "init sock: %d, len: %d\n", InitSocket, length);
+  BindSocket = accept (InitSocket, (struct sockaddr *) &Address_in, &length);
+  fprintf (stderr, "bind sock: %d\n", BindSocket);
+  if (BindSocket == -1) {
+    perror ("accept: ");
+    exit (2);
+  }
+
+  addr = Address_in.sin_addr.s_addr;
+  if (VERBOSE) {
+    fprintf (stderr, "incoming connection from: ");
+    fprintf (stderr, " %u", (0xff & (addr >>  0)));
+    fprintf (stderr, ".%u", (0xff & (addr >>  8)));
+    fprintf (stderr, ".%u", (0xff & (addr >> 16)));
+    fprintf (stderr, ".%u", (0xff & (addr >> 24)));
+    fprintf (stderr, "\n");
+  }
+
+  if (Nvalid == 0) goto accepted;
+
+  for (i = 0; i < Nvalid; i++) {
+    /* valid IP addresses may be machines (120.90.121.142) or 
+       class C networks (120.90.121.0) */
+       
+    /* for machine, address must match */
+    if ((0xff & (validIP[i] >> 24)) != 0) {
+      if (addr == validIP[i]) goto accepted;
+    }
+
+    /* for network, lower three bytes of address must match */
+    if ((0xff & (validIP[i] >> 24)) == 0) {
+      if ((0x00ffffff & addr) == validIP[i]) goto accepted;
+    }
+  }
+
+  if (VERBOSE) fprintf (stderr, "connection rejected\n");
+  close (BindSocket);
+  return (-1);
+
+accepted:
+  if (VERBOSE) fprintf (stderr, "connection accepted\n");
+  fcntl (BindSocket, F_SETFL, O_NONBLOCK); 
+  return (BindSocket);
+}
+
+int GetClientSocket (char *hostname) {
+
+  int i, status, InitSocket, length;
+  SockAddress Address;
+  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, ".");
+  }
+
+  if (VERBOSE) {
+    fprintf (stderr, "trying %s (%s:%d)...", host[0].h_name, hostip, MY_PORT);
+  }
+
+  Address.sin_family = AF_INET;
+  Address.sin_port   = MY_PORT;
+  status = inet_aton (hostip, &Address.sin_addr);
+  if (!status) {
+    fprintf (stderr, "invalid address\n");
+    exit (2);
+  }
+
+  length = sizeof(Address);
+
+  InitSocket = socket (PF_INET, SOCK_STREAM, 0);
+  if (InitSocket == -1) {
+    perror ("socket: ");
+    exit (2);
+  }
+
+  status = connect (InitSocket, (struct sockaddr *) &Address, length);
+  if (status == -1) {
+    perror ("connect: ");
+    exit (2);
+  }
+
+  if (VERBOSE) fprintf (stderr, "connected\n");
+  fcntl (InitSocket, F_SETFL, O_NONBLOCK); 
+  return (InitSocket);
+}
Index: /trunk/Ohana/src/addstar/src/addstarc.c
===================================================================
--- /trunk/Ohana/src/addstar/src/addstarc.c	(revision 5271)
+++ /trunk/Ohana/src/addstar/src/addstarc.c	(revision 5271)
@@ -0,0 +1,42 @@
+# include "addstar.h"
+
+int main (int argc, char **argv) {
+
+  int Nstars;
+  int BindSocket;
+  AddstarClientOptions options;
+  Stars *stars;
+
+  /* load config and options */
+  ConfigInit (&argc, argv);
+  options = args_client (argc, argv);
+
+# if (0)
+  /* load data */
+  stars = gstars (argv[1], &Nstars, &image);
+# endif
+
+  /* set up server connection */
+  BindSocket = GetClientSocket (HOSTNAME);
+  SendCommand (BindSocket, strlen(PASSWORD), PASSWORD);
+
+  ALLOCATE (stars, Stars, 2);
+  Nstars = 2;
+
+  stars[0].R = 10.0;
+  stars[0].D = 40.0;
+
+  stars[1].R = 25.0;
+  stars[1].D = 60.0;
+
+  /* send new data to server */
+  SendCommand (BindSocket, 5, "IMAGE");
+  Send_AddstarClientOptions (BindSocket, &options, 1, TRUE);
+  Send_Stars (BindSocket, stars, Nstars, FALSE);
+
+# if (0)
+  SendImage (BindSocket, &image);
+# endif
+
+  exit (0);
+}
Index: /trunk/Ohana/src/addstar/src/addstard.c
===================================================================
--- /trunk/Ohana/src/addstar/src/addstard.c	(revision 5271)
+++ /trunk/Ohana/src/addstar/src/addstard.c	(revision 5271)
@@ -0,0 +1,47 @@
+# include "addstar.h"
+
+int main (int argc, char **argv) {
+
+  int status, InitSocket, BindSocket;
+  SockAddress Address;
+  IOBuffer message;
+
+  ConfigInit (&argc, argv);
+  args_server (argc, argv);
+
+  /* if we separate the incoming data from db update, spawn db thread here */
+
+  VERBOSE = TRUE;
+  InitSocket = InitServerSocket (argv[1], &Address);
+  
+  while (1) {
+
+    /* wait for clients to make connection */
+    BindSocket = WaitServerSocket (InitSocket, &Address, VALID_IP, NVALID);
+    if (BindSocket == -1) continue;
+
+    /* validate : wait for password */
+    if (!CheckPassword (BindSocket)) continue;
+    
+    /* accept command */
+    status = ExpectCommand (BindSocket, 5, 0.1, &message);
+    if (status != 0) {
+      if (VERBOSE) fprintf (stderr, "failed connection\n");
+      FreeIOBuffer (&message);
+      close (BindSocket);
+      continue;
+    }
+
+    /* message options */
+    if (!strcmp (message.buffer, "IMAGE")) {
+      fprintf (stderr, "New Image\n");
+      NewImage (BindSocket);
+      continue;
+    }
+    if (!strcmp (message.buffer, "EXIT")) {
+      fprintf (stderr, "Exit\n");
+      exit (2);
+    }
+  }    
+  exit (1);
+}
Index: /trunk/Ohana/src/addstar/src/args_client.c
===================================================================
--- /trunk/Ohana/src/addstar/src/args_client.c	(revision 5271)
+++ /trunk/Ohana/src/addstar/src/args_client.c	(revision 5271)
@@ -0,0 +1,251 @@
+# include "addstar.h"
+void help (void);
+
+AddstarClientOptions args_client (int argc, char **argv) {
+  
+  int N;
+  AddstarClientOptions options;  // used to pass options to server
+
+  /* check for help request */
+  if (get_argument (argc, argv, "-help") ||
+      get_argument (argc, argv, "-h")) {
+    help ();
+  }
+
+  /*** check for command line options ***/
+
+  /* basic mode: image, list, refcat */
+  options.mode = M_IMAGE;
+  if ((N = get_argument (argc, argv, "-ref"))) {
+    options.mode = M_REFLIST;
+    remove_argument (N, &argc, argv);
+  }
+  if ((N = get_argument (argc, argv, "-cat"))) {
+    options.mode = M_REFCAT;
+    remove_argument (N, &argc, argv);
+  }
+
+  /*** provide additional data ***/ 
+  /* restrict to a portion of the sky? (REFCAT only) */
+  UserPatch.RA[0] = 0;
+  UserPatch.RA[1] = 360;
+  UserPatch.DEC[0] = -90;
+  UserPatch.DEC[1] = +90;
+  if ((N = get_argument (argc, argv, "-region"))) {
+    remove_argument (N, &argc, argv);
+    UserPatch.RA[0] = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.RA[1] = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.DEC[0] = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.DEC[1] = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+  /* override any header PHOTCODE values */
+  options.photcode = 0;
+  if ((N = get_argument (argc, argv, "-p"))) {
+    remove_argument (N, &argc, argv);
+    options.photcode = GetPhotcodeCodebyName (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+  /* provide a time for dataset */
+  options.timeref = 0; 
+  if ((N = get_argument (argc, argv, "-time"))) {
+    time_t tmp;
+    remove_argument (N, &argc, argv);
+    if (!str_to_time (argv[N], &tmp)) { 
+      fprintf (stderr, "syntax error in time\n");
+      exit (1);
+    }
+    options.timeref = tmp;
+    remove_argument (N, &argc, argv);
+  }
+  /* provide a mosaic for distortion */
+  MOSAIC = NULL;
+  options.mosaic = FALSE;
+  if ((N = get_argument (argc, argv, "-mosaic"))) {
+    Header header;
+    ALLOCATE (MOSAIC, Coords, 1);
+
+    remove_argument (N, &argc, argv);
+    if (!fits_read_header (argv[N], &header)) {
+      fprintf (stderr, "ERROR: can't read header for mosaic %s\n", argv[N]);
+      exit (1);
+    }
+    if (!GetCoords (MOSAIC, &header)) {
+      fprintf (stderr, "ERROR: no astrometric solution in header\n");
+      exit (1);
+    }
+    if (strcmp(&MOSAIC[0].ctype[4], "-DIS")) {
+      fprintf (stderr, "ERROR: not a mosaic distortion header\n");
+      exit (1);
+    }
+    remove_argument (N, &argc, argv);
+    fits_free_header (&header);
+    options.mosaic = TRUE;
+  }
+  
+  /*** modify behavior ***/
+  /* only add to existing objects */
+  options.existing_regions = FALSE;
+  if ((N = get_argument (argc, argv, "-existing-regions"))) {
+    options.existing_regions = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* only add to existing objects */
+  options.only_match = FALSE;
+  if ((N = get_argument (argc, argv, "-only-match"))) {
+    options.only_match = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* don't add missed pts to Missed table (image only) */
+  options.skip_missed = FALSE;
+  if ((N = get_argument (argc, argv, "-missed"))) {
+    options.skip_missed = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* replace measurement, don't duplicate (ref/cat only) */
+  options.replace = FALSE;
+  if ((N = get_argument (argc, argv, "-replace"))) {
+    options.replace = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* use 'closest star' matching, rather than traditional method */
+  options.closest = FALSE;
+  if ((N = get_argument (argc, argv, "-closest"))) {
+    options.closest = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  /* don't re-sort the measure sequence */
+  options.nosort = FALSE;
+  if ((N = get_argument (argc, argv, "-nosort"))) {
+    options.nosort = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* only add new rows (-update) or re-write complete measure table (forces -nosort) */
+  options.update = FALSE;
+  if ((N = get_argument (argc, argv, "-update"))) {
+    options.update = TRUE;
+    options.nosort = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  /* only add image potion to image table */
+  options.only_images = FALSE;
+  if ((N = get_argument (argc, argv, "-image"))) {
+    options.only_images = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* apply average zpt offset calibration (image only) */
+  options.calibrate = FALSE;
+  if ((N = get_argument (argc, argv, "-cal"))) {
+    options.calibrate = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  /*** optional situations ***/
+  /* treat input data specially for skyprobe (calibration, subpix) */ 
+  SKYPROBE = FALSE;
+  if ((N = get_argument (argc, argv, "-skyprobe"))) {
+    SKYPROBE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* define 2MASS quality flags to keep */
+  SELECT_2MASS_QUALITY = NULL;
+  if ((N = get_argument (argc, argv, "-2massquality"))) {
+    remove_argument (N, &argc, argv);
+    SELECT_2MASS_QUALITY = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+  /* accept bad header astrometry */
+  ACCEPT_ASTROM = FALSE;
+  if ((N = get_argument (argc, argv, "-accept"))) {
+    ACCEPT_ASTROM = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* force read of image database with mismatched NSTARS & size */ 
+  FORCE_READ = FALSE;
+  if ((N = get_argument (argc, argv, "-force"))) {
+    FORCE_READ = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* over-ride autointerpretation of input data format */ 
+  TEXTMODE = FALSE;
+  if ((N = get_argument (argc, argv, "-textmode"))) {
+    TEXTMODE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  /* extra error messages */
+  VERBOSE = FALSE;
+  if ((N = get_argument (argc, argv, "-v"))) {
+    VERBOSE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  DUMP = NULL;
+  if ((N = get_argument (argc, argv, "-dump"))) {
+    remove_argument (N, &argc, argv);
+    DUMP = strcreate(argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  if (argc != 2) {
+    fprintf (stderr, "USAGE: addstarc (filename)\n");
+    exit (2);
+  }
+  return (options);
+}
+
+void help () {
+
+  fprintf (stderr, "USAGE\n");
+  fprintf (stderr, "  addstar (filename)\n");
+  fprintf (stderr, "     add specified image (cmp format) to database\n\n");
+  fprintf (stderr, "  addstar -ref (filename)");
+  fprintf (stderr, "     add ASCII data (ra dec mag dmag) to database\n\n");
+  fprintf (stderr, "  addstar -cat (catalog)");
+  fprintf (stderr, "     add data from catalog (USNO/2MASS/GSC) to database\n\n");
+
+  fprintf (stderr, "  optional flags:\n");
+  fprintf (stderr, "  -region ra ra dec dec 	  : only add data in specified region (-ref mode only)\n");
+  fprintf (stderr, "  -p (photcode)         	  : specify photcode (override header)\n");
+  fprintf (stderr, "  -time (YYYY/MM/DD,HH:MM:SS) : specify date/time (override header)\n");
+  fprintf (stderr, "  -mosaic (filename)    	  : identify associated mosaic frame for chip image\n");
+  fprintf (stderr, "  -fits                 	  : input file is FITS table, not TEXT table\n");
+  fprintf (stderr, "  -existing-regions           : only add measurements to existing catalog files\n");
+  fprintf (stderr, "  -only-match           	  : only add measurements to existing objects\n");
+  fprintf (stderr, "  -missed               	  : skipped 'missed' entries\n");
+  fprintf (stderr, "  -replace              	  : replace time/photcode measurements (no duplication)\n");
+  fprintf (stderr, "  -closest             	  : use closest-star algorith\n");
+  fprintf (stderr, "  -nosort             	  : don't re-sort the measure entries (improves speed)\n");
+  fprintf (stderr, "  -update             	  : only update the new rows (foreces -nosort)\n");
+  fprintf (stderr, "  -image                	  : only insert image data\n");
+  fprintf (stderr, "  -cal                  	  : perform zero-point calibration\n");
+  fprintf (stderr, "  -skyprobe             	  : specify skyprobe mode\n");
+  fprintf (stderr, "  -accept               	  : accept bad astrometry from header\n");
+  fprintf (stderr, "  -force                	  : force read of database with inconsistent info\n");
+  fprintf (stderr, "  -v                    	  : verbose mode\n");
+  fprintf (stderr, "  -dump (mode)          	  : output test data\n");
+  fprintf (stderr, "  -help                 	  : this list\n");
+  fprintf (stderr, "  -h                    	  : this list\n\n");
+  exit (2);
+}
+
+/** addstar modes:
+ 
+    addstar (image.smp)  - add cmp/smp image data to db
+    addstar -ref (file.dat) (photcode) 
+    addstar -cat (USNO/2MASS/GSC) -region (ra dec - ra dec)
+
+    -replace : ref/cat - replace existing match (photcode/time)
+    -match   : ref/cat - only add measures to existing averages
+
+    ref types: 
+    ASCII - RA,DEC,M,dM in a table
+
+    addstar 
+
+**/
+
Index: /trunk/Ohana/src/addstar/src/args_server.c
===================================================================
--- /trunk/Ohana/src/addstar/src/args_server.c	(revision 5271)
+++ /trunk/Ohana/src/addstar/src/args_server.c	(revision 5271)
@@ -0,0 +1,248 @@
+# include "addstar.h"
+void help (void);
+
+int args_server (int argc, char **argv) {
+  
+  int N;
+
+  /* check for help request */
+  if (get_argument (argc, argv, "-help") ||
+      get_argument (argc, argv, "-h")) {
+    help ();
+  }
+
+  /* basic mode: image, list, refcat */
+  MODE = M_IMAGE;
+  if ((N = get_argument (argc, argv, "-ref"))) {
+    MODE = M_REFLIST;
+    remove_argument (N, &argc, argv);
+  }
+  if ((N = get_argument (argc, argv, "-cat"))) {
+    MODE = M_REFCAT;
+    remove_argument (N, &argc, argv);
+  }
+
+  /*** provide additional data ***/ 
+  /* restrict to a portion of the sky? (REFCAT only) */
+  UserPatch.RA[0] = 0;
+  UserPatch.RA[1] = 360;
+  UserPatch.DEC[0] = -90;
+  UserPatch.DEC[1] = +90;
+  if ((N = get_argument (argc, argv, "-region"))) {
+    remove_argument (N, &argc, argv);
+    UserPatch.RA[0] = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.RA[1] = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.DEC[0] = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.DEC[1] = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+  /* override any header PHOTCODE values */
+  thiscode = NULL;
+  if ((N = get_argument (argc, argv, "-p"))) {
+    remove_argument (N, &argc, argv);
+    thiscode = GetPhotcodebyName (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+  /* provide a time for dataset */
+  TIMEREF = 0; 
+  if ((N = get_argument (argc, argv, "-time"))) {
+    remove_argument (N, &argc, argv);
+    if (!str_to_time (argv[N], &TIMEREF)) { 
+      fprintf (stderr, "syntax error in time\n");
+      exit (1);
+    }
+    remove_argument (N, &argc, argv);
+  }
+  /* provide a mosaic for distortion */
+  MOSAIC = NULL;
+  if ((N = get_argument (argc, argv, "-mosaic"))) {
+    Header header;
+    ALLOCATE (MOSAIC, Coords, 1);
+
+    remove_argument (N, &argc, argv);
+    if (!fits_read_header (argv[N], &header)) {
+      fprintf (stderr, "ERROR: can't read header for mosaic %s\n", argv[N]);
+      exit (1);
+    }
+    if (!GetCoords (MOSAIC, &header)) {
+      fprintf (stderr, "ERROR: no astrometric solution in header\n");
+      exit (1);
+    }
+    if (strcmp(&MOSAIC[0].ctype[4], "-DIS")) {
+      fprintf (stderr, "ERROR: not a mosaic distortion header\n");
+      exit (1);
+    }
+    remove_argument (N, &argc, argv);
+    fits_free_header (&header);
+  }
+  FITS_INPUT = FALSE;
+  if ((N = get_argument (argc, argv, "-fits"))) {
+    FITS_INPUT = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  
+  /*** modify behavior ***/
+  /* only add to existing objects */
+  EXISTING_REGIONS = FALSE;
+  if ((N = get_argument (argc, argv, "-existing-regions"))) {
+    EXISTING_REGIONS = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* only add to existing objects */
+  ONLY_MATCH = FALSE;
+  if ((N = get_argument (argc, argv, "-only-match"))) {
+    ONLY_MATCH = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* don't add missed pts to Missed table (image only) */
+  SKIP_MISSED = FALSE;
+  if ((N = get_argument (argc, argv, "-missed"))) {
+    SKIP_MISSED = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* replace measurement, don't duplicate (ref/cat only) */
+  REPLACE = FALSE;
+  if ((N = get_argument (argc, argv, "-replace"))) {
+    REPLACE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* use 'closest star' matching, rather than traditional method */
+  CLOSEST = FALSE;
+  if ((N = get_argument (argc, argv, "-closest"))) {
+    CLOSEST = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  /* don't re-sort the measure sequence */
+  NOSORT = FALSE;
+  if ((N = get_argument (argc, argv, "-nosort"))) {
+    NOSORT = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* only add new rows (-update) or re-write complete measure table (forces -nosort) */
+  UPDATE = FALSE;
+  if ((N = get_argument (argc, argv, "-update"))) {
+    UPDATE = TRUE;
+    NOSORT = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  /* only add image potion to image table */
+  ONLY_IMAGES = FALSE;
+  if ((N = get_argument (argc, argv, "-image"))) {
+    ONLY_IMAGES = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* apply average zpt offset calibration (image only) */
+  CALIBRATE = FALSE;
+  if ((N = get_argument (argc, argv, "-cal"))) {
+    CALIBRATE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* define 2MASS quality flags to keep */
+  SELECT_2MASS_QUALITY = NULL;
+  if ((N = get_argument (argc, argv, "-2massquality"))) {
+    remove_argument (N, &argc, argv);
+    SELECT_2MASS_QUALITY = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  /*** optional situations ***/
+  /* treat data specially for skyprobe (calibration, subpix) */ 
+  SKYPROBE = FALSE;
+  if ((N = get_argument (argc, argv, "-skyprobe"))) {
+    SKYPROBE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* accept bad header astrometry */
+  ACCEPT_ASTROM = FALSE;
+  if ((N = get_argument (argc, argv, "-accept"))) {
+    ACCEPT_ASTROM = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* force read of image database with mismatched NSTARS & size */ 
+  FORCE_READ = FALSE;
+  if ((N = get_argument (argc, argv, "-force"))) {
+    FORCE_READ = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* force read of image database with mismatched NSTARS & size */ 
+  TEXTMODE = FALSE;
+  if ((N = get_argument (argc, argv, "-textmode"))) {
+    TEXTMODE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  /* extra error messages */
+  VERBOSE = FALSE;
+  if ((N = get_argument (argc, argv, "-v"))) {
+    VERBOSE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  DUMP = NULL;
+  if ((N = get_argument (argc, argv, "-dump"))) {
+    remove_argument (N, &argc, argv);
+    DUMP = strcreate(argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  if (argc != 2) {
+    fprintf (stderr, "USAGE: addstard (host)\n");
+    exit (2);
+  }
+  return (TRUE);
+}
+
+void help () {
+
+  fprintf (stderr, "USAGE\n");
+  fprintf (stderr, "  addstar (filename)\n");
+  fprintf (stderr, "     add specified image (cmp format) to database\n\n");
+  fprintf (stderr, "  addstar -ref (filename)");
+  fprintf (stderr, "     add ASCII data (ra dec mag dmag) to database\n\n");
+  fprintf (stderr, "  addstar -cat (catalog)");
+  fprintf (stderr, "     add data from catalog (USNO/2MASS/GSC) to database\n\n");
+
+  fprintf (stderr, "  optional flags:\n");
+  fprintf (stderr, "  -region ra ra dec dec 	  : only add data in specified region (-ref mode only)\n");
+  fprintf (stderr, "  -p (photcode)         	  : specify photcode (override header)\n");
+  fprintf (stderr, "  -time (YYYY/MM/DD,HH:MM:SS) : specify date/time (override header)\n");
+  fprintf (stderr, "  -mosaic (filename)    	  : identify associated mosaic frame for chip image\n");
+  fprintf (stderr, "  -fits                 	  : input file is FITS table, not TEXT table\n");
+  fprintf (stderr, "  -existing-regions           : only add measurements to existing catalog files\n");
+  fprintf (stderr, "  -only-match           	  : only add measurements to existing objects\n");
+  fprintf (stderr, "  -missed               	  : skipped 'missed' entries\n");
+  fprintf (stderr, "  -replace              	  : replace time/photcode measurements (no duplication)\n");
+  fprintf (stderr, "  -closest             	  : use closest-star algorith\n");
+  fprintf (stderr, "  -nosort             	  : don't re-sort the measure entries (improves speed)\n");
+  fprintf (stderr, "  -update             	  : only update the new rows (foreces -nosort)\n");
+  fprintf (stderr, "  -image                	  : only insert image data\n");
+  fprintf (stderr, "  -cal                  	  : perform zero-point calibration\n");
+  fprintf (stderr, "  -skyprobe             	  : specify skyprobe mode\n");
+  fprintf (stderr, "  -accept               	  : accept bad astrometry from header\n");
+  fprintf (stderr, "  -force                	  : force read of database with inconsistent info\n");
+  fprintf (stderr, "  -v                    	  : verbose mode\n");
+  fprintf (stderr, "  -dump (mode)          	  : output test data\n");
+  fprintf (stderr, "  -help                 	  : this list\n");
+  fprintf (stderr, "  -h                    	  : this list\n\n");
+  exit (2);
+}
+
+/** addstar modes:
+ 
+    addstar (image.smp)  - add cmp/smp image data to db
+    addstar -ref (file.dat) (photcode) 
+    addstar -cat (USNO/2MASS/GSC) -region (ra dec - ra dec)
+
+    -replace : ref/cat - replace existing match (photcode/time)
+    -match   : ref/cat - only add measures to existing averages
+
+    ref types: 
+    ASCII - RA,DEC,M,dM in a table
+
+    addstar 
+
+**/
+
