Index: /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/Makefile
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/Makefile	(revision 37433)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/Makefile	(revision 37434)
@@ -1,3 +1,3 @@
-default: uniphot setphot setphot_client setfwhm setposangle setposangle_client setastrom setastrom_client fiximids fiximids_client fixstkids fixstkids_client ckids ckids_client
+default: uniphot setphot setphot_client setfwhm setposangle setposangle_client setastrom setastrom_client setgalmodel setgalmodel_client fiximids fiximids_client fixstkids fixstkids_client ckids ckids_client
 help:
 @echo "make options: uniphot setphot setfwhm default help install default (uniphot setphot setfwhm)"
@@ -25,4 +25,6 @@
 setastrom: $(BIN)/setastrom.$(ARCH)
 setastrom_client: $(BIN)/setastrom_client.$(ARCH)
+setgalmodel: $(BIN)/setgalmodel.$(ARCH)
+setgalmodel_client: $(BIN)/setgalmodel_client.$(ARCH)
 fiximids: $(BIN)/fiximids.$(ARCH)
 fiximids_client: $(BIN)/fiximids_client.$(ARCH)
@@ -32,5 +34,5 @@
 ckids_client: $(BIN)/ckids_client.$(ARCH)
 
-install: $(DESTBIN)/uniphot $(DESTBIN)/setfwhm $(DESTBIN)/setphot $(DESTBIN)/setphot_client $(DESTBIN)/setposangle $(DESTBIN)/setposangle_client $(DESTBIN)/setastrom $(DESTBIN)/setastrom_client $(DESTBIN)/fiximids $(DESTBIN)/fiximids_client $(DESTBIN)/fixstkids $(DESTBIN)/fixstkids_client $(DESTBIN)/ckids $(DESTBIN)/ckids_client
+install: $(DESTBIN)/uniphot $(DESTBIN)/setfwhm $(DESTBIN)/setphot $(DESTBIN)/setphot_client $(DESTBIN)/setposangle $(DESTBIN)/setposangle_client $(DESTBIN)/setastrom $(DESTBIN)/setastrom_client $(DESTBIN)/setgalmodel $(DESTBIN)/setgalmodel_client $(DESTBIN)/fiximids $(DESTBIN)/fiximids_client $(DESTBIN)/fixstkids $(DESTBIN)/fixstkids_client $(DESTBIN)/ckids $(DESTBIN)/ckids_client
 
 UNIPHOT = \
@@ -126,6 +128,28 @@
 $(SRC)/initialize_setastrom.$(ARCH).o
 
-$(SETASTROM_CLIENT): $(INC)/setastrom.h
-$(BIN)/setastrom_client.$(ARCH): $(SETASTROM_CLIENT)
+$(SETGALMODEL_CLIENT): $(INC)/setgalmodel.h
+$(BIN)/setgalmodel_client.$(ARCH): $(SETGALMODEL_CLIENT)
+
+SETGALMODEL = \
+$(SRC)/setgalmodel.$(ARCH).o	    \
+$(SRC)/initialize_setgalmodel.$(ARCH).o \
+$(SRC)/galmodel_correction.$(ARCH).o \
+$(SRC)/update_dvo_setgalmodel.$(ARCH).o \
+$(SRC)/update_catalog_setgalmodel.$(ARCH).o \
+$(SRC)/SetSignals.$(ARCH).o	    \
+$(SRC)/Shutdown.$(ARCH).o	    
+
+$(SETGALMODEL): $(INC)/setgalmodel.h
+$(BIN)/setgalmodel.$(ARCH): $(SETGALMODEL)
+
+SETGALMODEL_CLIENT = \
+$(SRC)/setgalmodel_client.$(ARCH).o	    \
+$(SRC)/update_dvo_setgalmodel.$(ARCH).o \
+$(SRC)/update_catalog_setgalmodel.$(ARCH).o \
+$(SRC)/galmodel_correction.$(ARCH).o \
+$(SRC)/initialize_setgalmodel.$(ARCH).o
+
+$(SETGALMODEL_CLIENT): $(INC)/setgalmodel.h
+$(BIN)/setgalmodel_client.$(ARCH): $(SETGALMODEL_CLIENT)
 
 SETFWHM =                           \
Index: /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/include/setgalmodel.h
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/include/setgalmodel.h	(revision 37434)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/include/setgalmodel.h	(revision 37434)
@@ -0,0 +1,43 @@
+# include <ohana.h>
+# include <dvo.h>
+# include <signal.h>
+
+/* global variables set in parameter file */
+# define DVO_MAX_PATH 1024
+char         ImageCat[DVO_MAX_PATH];
+char        *CATDIR;
+int          HOST_ID;
+char        *HOSTDIR;
+int          VERBOSE;
+int          UPDATE;
+int          PARALLEL;
+int          PARALLEL_MANUAL;
+int          PARALLEL_SERIAL;
+char        *UPDATE_CATFORMAT;
+char        *SINGLE_CPT;
+
+SkyRegion    UserPatch;
+
+/***** prototypes ****/
+int           main                            PROTO((int argc, char **argv));
+
+void          GetConfig                       PROTO((char *config, char *field, char *format, int N, void *ptr));
+void          ConfigInit                      PROTO((int *argc, char **argv));
+
+void          usage_setgalmodel                 PROTO(());
+void          initialize_setgalmodel            PROTO((int argc, char **argv));
+int           args_setgalmodel                  PROTO((int argc, char **argv));
+
+void          usage_setgalmodel_client          PROTO(());
+void          initialize_setgalmodel_client     PROTO((int argc, char **argv));
+int           args_setgalmodel_client           PROTO((int argc, char **argv));
+
+// int           Shutdown                        PROTO((char *format, ...) OHANA_FORMAT(printf, 1, 2) );
+// void          TrapSignal                      PROTO((int sig));
+// void          SetProtect                      PROTO((int mode));
+// int           SetSignals                      PROTO((void));
+
+int           update_dvo_setgalmodel            PROTO((char *corrfile));
+int           update_dvo_setgalmodel_parallel   PROTO((SkyTable *sky, char *corrfile));
+
+int           update_catalog_setgalmodel        PROTO((Catalog *catalog));
Index: /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/galmodel_correction.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/galmodel_correction.c	(revision 37434)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/galmodel_correction.c	(revision 37434)
@@ -0,0 +1,6 @@
+# include "setgalmodel.h"
+
+int galmodel_correction () {
+
+  return (TRUE);
+}
Index: /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/initialize_setgalmodel.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/initialize_setgalmodel.c	(revision 37434)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/initialize_setgalmodel.c	(revision 37434)
@@ -0,0 +1,270 @@
+# include "setgalmodel.h"
+
+void usage_setgalmodel () {
+    fprintf (stderr, "USAGE: setgalmodel (corrections) [options]\n");
+    fprintf (stderr, "  options:\n");
+    fprintf (stderr, "    -v : verbose mode\n");
+    fprintf (stderr, "    -region Rmin Rmax Dmin Dmax\n");
+    fprintf (stderr, "    -update-catformat (format) : change database schema on output\n");
+    fprintf (stderr, "    -update : actually write results to detections tables\n");
+    fprintf (stderr, "    -parallel : run in parallel mode\n");
+    fprintf (stderr, "    -h     : this help list\n");
+    fprintf (stderr, "    -help  : this help list\n");
+    fprintf (stderr, "    --h    : this help list\n");
+    fprintf (stderr, "    --help : this help list\n");
+    fprintf (stderr, "    Note that the dvo db can be specified by -D CATDIR (directory)\n");
+    exit (2);
+}
+
+void initialize_setgalmodel (int argc, char **argv) {
+
+  int N;
+  struct stat statbuffer;
+  char CatdirPhotcodeFile[256];
+
+  if ((N = get_argument (argc, argv, "-h"))) usage_setgalmodel();
+  if ((N = get_argument (argc, argv, "-help"))) usage_setgalmodel();
+  if ((N = get_argument (argc, argv, "--h"))) usage_setgalmodel();
+  if ((N = get_argument (argc, argv, "--help"))) usage_setgalmodel();
+
+  /* are these set correctly? */
+  ConfigInit (&argc, argv);
+  args_setgalmodel (argc, argv);
+
+  if (stat (CATDIR, &statbuffer)) {
+    fprintf (stderr, "error accessing dvo database directory '%s'\n", CATDIR);
+    exit (1);
+  }
+
+  // load the photcode table
+  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
+  if (!LoadPhotcodes (CatdirPhotcodeFile, NULL, FALSE)) {
+    fprintf (stderr, "error loading photcode table %s\n", CatdirPhotcodeFile);
+    exit (1);
+  }
+}
+
+int args_setgalmodel (int argc, char **argv) {
+
+  int N;
+
+  SINGLE_CPT = NULL;
+  if ((N = get_argument (argc, argv, "-cpt"))) {
+    remove_argument (N, &argc, argv);
+    SINGLE_CPT = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  /* specify portion of the sky : allow default of all sky? */
+  UserPatch.Rmin = 0;
+  UserPatch.Rmax = 360;
+  UserPatch.Dmin = -90;
+  UserPatch.Dmax = +90;
+  if ((N = get_argument (argc, argv, "-region"))) {
+    remove_argument (N, &argc, argv);
+    UserPatch.Rmin = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.Rmax = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.Dmin = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.Dmax = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+  } 
+
+  UPDATE_CATFORMAT = NULL;
+  if ((N = get_argument (argc, argv, "-update-catformat"))) {
+    remove_argument (N, &argc, argv);
+    UPDATE_CATFORMAT = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  VERBOSE = FALSE;
+  if ((N = get_argument (argc, argv, "-v"))) {
+    VERBOSE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  PARALLEL = FALSE;
+  if ((N = get_argument (argc, argv, "-parallel"))) {
+    PARALLEL = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  // this is a test mode : rather than launching the remote jobs and waiting for completion,
+  // setgalmodel will simply list the remote command and wait for the user to signal completion
+  PARALLEL_MANUAL = FALSE;
+  if ((N = get_argument (argc, argv, "-parallel-manual"))) {
+    PARALLEL = TRUE; // -parallel-manual implies -parallel
+    PARALLEL_MANUAL = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  // this is a test mode : rather than launching the setgalmodel_client jobs remotely, they are 
+  // run in serial via 'system'
+  PARALLEL_SERIAL = FALSE;
+  if ((N = get_argument (argc, argv, "-parallel-serial"))) {
+    if (PARALLEL_MANUAL) {
+      fprintf (stderr, "ERROR: cannot mix -parallel-manual and -parallel-serial\n");
+      exit (1);
+    }
+    PARALLEL = TRUE; // -parallel-serial implies -parallel
+    PARALLEL_SERIAL = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  UPDATE = FALSE;
+  if ((N = get_argument (argc, argv, "-update"))) {
+    remove_argument (N, &argc, argv);
+    UPDATE = TRUE;
+  }
+
+  if (argc != 2) usage_setgalmodel();
+
+  return (TRUE);
+}
+
+void usage_setgalmodel_client () {
+  fprintf (stderr, "USAGE: setgalmodel_client (correction) -hostID (hostID) -catdir (catdir) -hostdir (hostdir) [options]\n");
+  fprintf (stderr, "  options:\n");
+  fprintf (stderr, "    -region Rmin Rmax Dmin Dmax\n");
+  fprintf (stderr, "    -update-catformat (format) : change database schema on output\n");
+  fprintf (stderr, "    -v : verbose mode\n");
+  fprintf (stderr, "    -update : actually write results to detections tables\n");
+  exit (2);
+}
+
+void initialize_setgalmodel_client (int argc, char **argv) {
+
+  int N;
+  struct stat statbuffer;
+  char CatdirPhotcodeFile[256];
+
+  if ((N = get_argument (argc, argv, "-h"))) usage_setgalmodel_client();
+  if ((N = get_argument (argc, argv, "-help"))) usage_setgalmodel_client();
+  if ((N = get_argument (argc, argv, "--h"))) usage_setgalmodel_client();
+  if ((N = get_argument (argc, argv, "--help"))) usage_setgalmodel_client();
+
+  SetZeroPoint (25.0); // XXX is this needed?
+  args_setgalmodel_client (argc, argv);
+
+  if (stat (CATDIR, &statbuffer)) {
+    fprintf (stderr, "error accessing dvo database directory '%s'\n", CATDIR);
+    exit (1);
+  }
+
+  // load the photcode table : XXX needed?
+  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
+  if (!LoadPhotcodes (CatdirPhotcodeFile, NULL, FALSE)) {
+    fprintf (stderr, "error loading photcode table %s\n", CatdirPhotcodeFile);
+    exit (1);
+  }
+}
+
+int args_setgalmodel_client (int argc, char **argv) {
+
+  int N;
+
+  /* specify portion of the sky : allow default of all sky? */
+  UserPatch.Rmin = 0;
+  UserPatch.Rmax = 360;
+  UserPatch.Dmin = -90;
+  UserPatch.Dmax = +90;
+  if ((N = get_argument (argc, argv, "-region"))) {
+    remove_argument (N, &argc, argv);
+    UserPatch.Rmin = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.Rmax = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.Dmin = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.Dmax = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+  } 
+
+  UPDATE_CATFORMAT = NULL;
+  if ((N = get_argument (argc, argv, "-update-catformat"))) {
+    remove_argument (N, &argc, argv);
+    UPDATE_CATFORMAT = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  VERBOSE = FALSE;
+  if ((N = get_argument (argc, argv, "-v"))) {
+    VERBOSE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  UPDATE = FALSE;
+  if ((N = get_argument (argc, argv, "-update"))) {
+    remove_argument (N, &argc, argv);
+    UPDATE = TRUE;
+  }
+
+  HOST_ID = 0;
+  if ((N = get_argument (argc, argv, "-hostID"))) {
+    remove_argument (N, &argc, argv);
+    HOST_ID = atoi (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+  if (!HOST_ID) usage_setgalmodel_client();
+
+  HOSTDIR = NULL;
+  if ((N = get_argument (argc, argv, "-hostdir"))) {
+    remove_argument (N, &argc, argv);
+    HOSTDIR = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+  if (!HOSTDIR) usage_setgalmodel_client();
+
+  if ((N = get_argument (argc, argv, "-catdir"))) {
+    remove_argument (N, &argc, argv);
+    CATDIR = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+  if (!CATDIR) usage_setgalmodel_client();
+
+  if (argc != 2) usage_setgalmodel_client();
+  return (TRUE);
+}
+
+void ConfigInit (int *argc, char **argv) {
+
+  double ZERO_POINT;
+  char  *config, *file;
+
+  /*** load configuration info ***/
+  file = SelectConfigFile (argc, argv, "ptolemy");
+  config = LoadConfigFile (file);
+  if (config == (char *) NULL) {
+    fprintf (stderr, "ERROR: can't find configuration file %s\n", file);
+    if (file != (char *) NULL) free (file);
+    exit (0);
+  }
+  if (VERBOSE) fprintf (stderr, "loaded config file: %s\n", file);
+
+  // force CATDIR to be absolute (so parallel mode will work)
+  char *tmpcatdir = NULL;
+  ALLOCATE (tmpcatdir, char, DVO_MAX_PATH);
+  GetConfig (config, "CATDIR",                 "%s",  0, tmpcatdir);
+  CATDIR = abspath (tmpcatdir, DVO_MAX_PATH);
+  free (tmpcatdir);
+
+  sprintf (ImageCat, "%s/Images.dat", CATDIR);
+
+  ScanConfig (config, "ZERO_PT",                "%lf", 0, &ZERO_POINT);
+  SetZeroPoint (ZERO_POINT);
+
+  free (config);
+  free (file);
+}
+
+void GetConfig (char *config, char *field, char *format, int N, void *ptr) {
+
+  char *status;
+
+  status = ScanConfig (config, field, format, N, ptr);
+  if (status == NULL) {
+    fprintf (stderr, "error in config, cannot find %s\n", field);
+    exit (1);
+  }
+  return;
+}
Index: /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/setgalmodel.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/setgalmodel.c	(revision 37434)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/setgalmodel.c	(revision 37434)
@@ -0,0 +1,23 @@
+# include "setgalmodel.h"
+
+int main (int argc, char **argv) {
+
+  int status;
+
+  /* get configuration info, args, lockfile */
+  initialize_setgalmodel (argc, argv);
+
+  // argv[1] holds the correction file
+  status = update_dvo_setgalmodel (argv[1]);
+
+  if (!status) exit (1);
+  exit (0);
+}
+  
+
+/* setgalmodel : set starpar.uRA,uDEC based on starpar.DM
+
+   ** load images
+   ** load catalogs
+   ** update detection (& averages?)
+ */
Index: /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/setgalmodel_client.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/setgalmodel_client.c	(revision 37434)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/setgalmodel_client.c	(revision 37434)
@@ -0,0 +1,20 @@
+# include "setgalmodel.h"
+
+// setgalmodel_client is run on a remote host and is responsible for updating the catalogs
+// owned by that host.  
+
+// setgalmodel_client -hostname (hostname) -catdir (catdir) -hostdir (hostdir) -images (images)
+// load the SkyTable and HostTable from (catdir) [contains the host responsibilities]
+// load the image data from (images) [contains the zp infomation to apply]
+// loop over tables from SkyTable with my host ID
+// the calling program tells the client which host they are (or host ID?); we do not actually have to run this on the real host
+
+int main (int argc, char **argv) {
+
+  // get configuration info, args, lockfile (set CATDIR, HOST_ID, HOSTDIR, IMAGES)
+  initialize_setgalmodel_client (argc, argv);
+
+  update_dvo_setgalmodel (argv[1]);
+
+  exit (0);
+}
Index: /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/update_catalog_setgalmodel.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/update_catalog_setgalmodel.c	(revision 37434)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/update_catalog_setgalmodel.c	(revision 37434)
@@ -0,0 +1,109 @@
+# include "setgalmodel.h"
+
+static double iFkap = 1.0 / 4.74047; // (km/sec/kpc) / (milli-arcsec/ year)
+static double A_oort = +14.5;  // km/sec/kpc
+static double B_oort = -13.0;  // km/sec/kpc
+
+static double U_sol  =  9.44; // km/sec
+static double V_sol  = 11.90; // km/sec
+static double W_sol  =  7.20; // km/sec
+
+int update_catalog_setgalmodel (Catalog *catalog) {
+
+  off_t i, j, m;
+
+  // choose this number more judiciously...
+  time_t timeRef = ohana_date_to_sec("2011/05/11,00:00:00");
+
+  Average *average = catalog[0].average;
+
+  // first set the proper motion based on Galactic rotation and solar motion
+  for (i = 0; i < catalog[0].Naverage; i++) {
+
+    if (average[i].Nstarpar == 0) continue;
+
+    m = average[i].starparOffset;
+    StarPar *starpar = &catalog[0].starpar[m];
+    double distance = NAN; // function of starpar->DM;
+
+    double glon = starpar->galLon * RAD_DEG;
+    double glat = starpar->galLat * RAD_DEG;
+
+    double uL_gal = (A_oort * cos(2.0*glon) + B_oort) * cos(glat) * iFkap;
+    double uB_gal = -0.5*A_oort * sin(2.0*glon) * sin(2.0*glat) * iFkap;
+
+    double uL_sol = (U_sol * sin(glon) - V_sol * cos(glon)) * iFkap / distance;
+    double uB_sol = ((U_sol * cos(glon) + V_sol * sin(glon))*sin(glat) - W_sol*cos(glat)) * iFkap / distance;
+
+    double uL = uL_gal + uL_sol;
+    double uB = uB_gal + uB_sol;
+
+    // c11, c22 are from http://arxiv.org/pdf/1306.2945v2.pdf
+    double c11 = NAN;
+    double c22 = NAN;
+
+    double uR = c11 * uL + c22 * uB;
+    double uD = c11 * uB - c22 * uL;
+
+    starpar->uRA  = uR; // XXX units? (mas / year? arcsec / year?)
+    starpar->uDEC = uD;
+
+    m = average[i].measureOffset;
+    Measure *measure = &catalog[0].measure[m];
+
+    // use the model uR,uD values to move the expected position based on the reference date;
+    for (j = 0; j < average[i].Nmeasure; j++) {
+
+      int Nm = m + j;
+      double dtime = (measure[Nm].t - timeRef) / 86400.0 / 365.25;
+
+      // do not modify the original Xccd,Yccd values
+      measure[Nm].RoffGAL = uR * dtime; // XXX divide by dcos? project and calculate?
+      measure[Nm].DoffGAL = uD * dtime; // XXX divide by dcos? project and calculate?
+    }
+  }
+
+  return (TRUE);
+}
+
+// I am using the galactic rotation and solar motion model to predict the reflex proper
+// motion of stars in the database. 
+
+/*****
+
+From Siegfreid Roeser:
+
+Dear Gene,
+
+ please find attached our approach for galactic rotation and
+ solar motion that we assumed in our approach last fall.
+(Hope I understood correctly what you requested at the
+telecon). If not, please come back.
+
+      fkap = 4.74047d-0   {km/s/kpc}/{mas/y}
+
+      gl,gb  is galactic longitude resp. latitude
+
+
+      PARAMETER (distance= 1.d0,Aoort=14.5d0 ,Boort=-13.d0  )
+                      kpc           km/s/kpc   km/s/kpc
+      PARAMETER (Usol = 9.44d0, Vsol =11.90d0, Wsol = 7.20d0)
+                                   km/s
+C
+C      Gal. Rotation
+C
+      proper motions in longitude/latitude
+
+      emulgal = (AOORT*dcos(2.d0*gl)+BOORT)*dcos(gb)/fkap
+      emubgal = -0.5d0*AOORT*dsin(2.d0*gl)*dsin(2.d0*gb)/fkap
+c
+c     Solar Motion
+cc
+      emulsol = Usol*dsin(gl) - Vsol*dcos(gl)
+      emulsol = emulsol/fkap/distance
+      emubsol = Usol*dsin(gb)*dcos(gl)+Vsol*dsin(gb)*dsin(gl)
+      emubsol = emubsol -Wsol*dcos(gb)
+      emubsol = emubsol/fkap/distance
+
+
+******/
Index: /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/update_dvo_setgalmodel.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/update_dvo_setgalmodel.c	(revision 37434)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/uniphot/src/update_dvo_setgalmodel.c	(revision 37434)
@@ -0,0 +1,139 @@
+# include "setgalmodel.h"
+
+int update_dvo_setgalmodel (char *corrfile) {
+
+  SkyTable *sky = NULL;
+  SkyList *skylist = NULL;
+  Catalog catalog;
+  off_t i;
+
+  // load the current sky table (layout of all SkyRegions) 
+  sky = SkyTableLoadOptimal (CATDIR, NULL, NULL, FALSE, -1, VERBOSE);
+  SkyTableSetFilenames (sky, CATDIR, "cpt");
+  
+  if (PARALLEL && !HOST_ID) {
+    int status = update_dvo_setgalmodel_parallel (sky, corrfile);
+    return status;
+  }
+
+  // determine the populated SkyRegions overlapping the requested area (default depth)
+  if (SINGLE_CPT) {
+      skylist = SkyRegionByCPT (sky, SINGLE_CPT);
+  } else {
+      skylist = SkyListByPatch (sky, -1, &UserPatch);
+  }
+  myAssert (skylist, "ooops!");
+
+  // update measurements for each populated catalog
+  for (i = 0; i < skylist[0].Nregions; i++) {
+
+    // does this host ID match the desired location for the table?
+    if (!HostTableTestHost(skylist[0].regions[i], HOST_ID)) continue;
+
+    char localFilename[1024];
+    snprintf (localFilename, 1024, "%s/%s.cpt", HOSTDIR, skylist->regions[i]->name);
+
+    // set up the basic catalog info
+    catalog.filename  = HOST_ID ? localFilename : skylist[0].filename[i];
+    catalog.catformat = DVO_FORMAT_UNDEF; // not needed since we skip empty catalogs
+    catalog.catmode   = DVO_MODE_UNDEF;	  // not needed since we skip empty catalogs
+    catalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
+    catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
+
+    if (!dvo_catalog_open (&catalog, skylist[0].regions[i], VERBOSE, "w")) {
+      fprintf (stderr, "ERROR: failure reading catalog %s\n", catalog.filename);
+      exit (1);
+    }
+    if (!catalog.Naves_disk) {
+      if (VERBOSE) fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
+      dvo_catalog_unlock (&catalog);
+      dvo_catalog_free (&catalog);
+      continue;
+    }
+
+    update_catalog_setgalmodel (&catalog);
+
+    if (!UPDATE) {
+      dvo_catalog_unlock (&catalog);
+      dvo_catalog_free (&catalog);
+      continue;
+    }
+    
+    // modify the output format as desired (ignore current format on disk)
+    if (UPDATE_CATFORMAT) {
+      catalog.catformat = dvo_catalog_catformat (UPDATE_CATFORMAT);
+    }
+
+    if (VERBOSE) fprintf (stderr, "saving catalog %s\n", catalog.filename);
+    
+    dvo_catalog_save (&catalog, VERBOSE); 
+    dvo_catalog_unlock (&catalog);
+    dvo_catalog_free (&catalog);
+  }
+  return (TRUE);
+}      
+
+# define DEBUG 1
+
+int update_dvo_setgalmodel_parallel (SkyTable *sky, char *corrfile) {
+
+  // now launch the setgalmodel_client jobs to the parallel hosts
+
+  char *abscorrfile = abspath (corrfile, DVO_MAX_PATH);
+
+  // load the list of hosts
+  HostTable *table = HostTableLoad (CATDIR, sky->hosts);
+
+  int i;
+  for (i = 0; i < table->Nhosts; i++) {
+
+    // ensure that the paths are absolute path names
+    char *tmppath = abspath (table->hosts[i].pathname, DVO_MAX_PATH);
+    free (table->hosts[i].pathname);
+    table->hosts[i].pathname = tmppath;
+
+    char command[1024];
+    snprintf (command, 1024, "setgalmodel_client %s -hostID %d -catdir %s -hostdir %s -region %f %f %f %f", 
+	      abscorrfile, table->hosts[i].hostID, CATDIR, table->hosts[i].pathname, 
+	      UserPatch.Rmin, UserPatch.Rmax, UserPatch.Dmin, UserPatch.Dmax);
+
+    fprintf (stderr, "command: %s\n", command);
+
+    char tmpline[1024];
+    if (VERBOSE)       	  { snprintf (tmpline, 1024, "%s -v",                	command); 		    strcpy (command, tmpline); }
+    if (UPDATE)        	  { snprintf (tmpline, 1024, "%s -update",           	command); 		    strcpy (command, tmpline); }
+    if (UPDATE_CATFORMAT) { snprintf (tmpline, 1024, "%s -update-catformat %s", command, UPDATE_CATFORMAT); strcpy (command, tmpline); }
+
+    if (PARALLEL_MANUAL) continue;
+
+    if (PARALLEL_SERIAL) {
+      int status = system (command);
+      if (status) {
+	fprintf (stderr, "ERROR running setgalmodel_client\n");
+	exit (2);
+      }
+    } else {
+      // launch the job on the remote machine (no handshake)
+      int errorInfo = 0;
+      int pid = rconnect ("ssh", table->hosts[i].hostname, command, table->hosts[i].stdio, &errorInfo, FALSE);
+      if (!pid) {
+	if (DEBUG) fprintf (stderr, "failure to start %s (error %d)\n", table->hosts[i].hostname, errorInfo);
+	exit (1);
+      }
+      table->hosts[i].pid = pid; // save for future reference
+    }
+  }
+  free (abscorrfile);
+
+  // wait for the remote jobs to be completed
+  if (PARALLEL_MANUAL) {
+    fprintf (stderr, "run the setgalmodel_client commands above.  when these are done, hit return\n");
+    getchar();
+  }
+  if (!PARALLEL_MANUAL && !PARALLEL_SERIAL) {
+    int status = HostTableWaitJobsGetIO (table, __FILE__, __LINE__, VERBOSE);
+    if (!status) return FALSE;
+  }
+
+  return (TRUE);
+}      
