Index: /trunk/Ohana/src/mosastro/Makefile
===================================================================
--- /trunk/Ohana/src/mosastro/Makefile	(revision 3400)
+++ /trunk/Ohana/src/mosastro/Makefile	(revision 3401)
@@ -56,5 +56,6 @@
 $(SRC)/GetScatter.$(ARCH).o \
 $(SRC)/clip.$(ARCH).o \
-$(SRC)/parse_time.$(ARCH).o
+$(SRC)/parse_time.$(ARCH).o \
+$(SRC)/testcoords.$(ARCH).o
 
 mosastro: $(BIN)/mosastro.$(ARCH)
@@ -102,5 +103,6 @@
 $(STD) : $(INC)/mosastro.h
 
-INSTALL = mosastro mkstandards mkobs
+# INSTALL = mosastro mkstandards mkobs
+INSTALL = mosastro
 
 # dependancy rules for binary code #########################
Index: /trunk/Ohana/src/mosastro/include/mosastro.h
===================================================================
--- /trunk/Ohana/src/mosastro/include/mosastro.h	(revision 3400)
+++ /trunk/Ohana/src/mosastro/include/mosastro.h	(revision 3401)
@@ -96,30 +96,74 @@
 char *FOCAL_PLANE;
 
+CatStats *gregions (CatStats *patch, int *nregion);
+Gradients *GetGradients ();
+Header *mkheader (int Nx, int Ny, int Nstars, Coords *coords);
+Header *mkmosaic (int Nx, int Ny, int Nstars, Coords *coords);
 StarData *getusno (CatStats *catstats, int *Nstars);
 StarData *getgsc (CatStats *catstats, int *NSTARS);
 StarData *get2mass (CatStats *catstats, int *NSTARS);
 StarData *getptolemy (CatStats *catstats, int *NSTARS);
-void GetConfig (char *config, char *field, char *format, int N, void *ptr);
 StarData *greference (int *Nrefcat);
-
-Header *mkheader (int Nx, int Ny, int Nstars, Coords *coords);
-
 StarData *gcatalog (char *filename, int *Nstars);
-
-CatStats *gregions (CatStats *patch, int *nregion);
+StarData *gptolemy (char *filename, int *NSTARS);
+double GetScatter ();
 int find_dec_bands (CatStats *area);
 int load_ra_blocks (int Ndec, CatStats *area);
 int parse_GSC_line (CatStats *tregion, char *line);
+int fake_field (double RA, double DEC);
+int LoadStars (int Nfile, char **file);
+int project_refcat (StarData *refcat, int Nrefcat);
+int deproject_stars ();
+int project_stars ();
+int deproject_raw ();
+int project_ref ();
+int init_field ();
+int load_field (char *filename);
+int init_chips ();
+int load_chips (char *filename);
+int dump_stars (FILE *f, StarData *stars, int Nstars);
+int dump_grads (Gradients *grad, char *filename);
+int dump_match ();
+int dump_rawstars ();
+int dump_refcat (StarData *refcat, int Nrefcat);
+int match (StarData *refcat, int Nrefcat);
+int mkvector (int n, int m, int norder);
+int ClipOnFP (double Nsigma);
+void GetConfig (char *config, char *field, char *format, int N, void *ptr);
 void init_regions ();
 void add_to_regions (CatStats *area);
 void area_of_region (CatStats *region);
 void set_catalog (char *catdir);
-
-StarData *gptolemy (char *filename, int *NSTARS);
-int fake_field (double RA, double DEC);
-Gradients *GetGradients ();
-void FitChips (int Norder);
 void FitChipLinear (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
 void FitChipResid (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
-double GetScatter ();
-Header *mkmosaic (int Nx, int Ny, int Nstars, Coords *coords);
+void ConfigInit (int *argc, char **argv);
+void print_help ();
+void args (int *argc, char **argv);
+void field_stats ();
+void field_combine ();
+void fit_apply_grads (Coords *distort, Coords *project, int term);
+void fit_apply_coords (Coords *coords);
+void fit_eval ();
+void fit_add (double x1, double y1, double x2, double y2);
+void fit_init (int order);
+void fit_correct_grads (Gradients *in, Gradients *out, int term);
+void fit_free ();
+void output (char *ext, char *phu);
+void FitGradients (Gradients *grad);
+void uppercase (char *string);
+e_time parse_time (Header *header);
+int sortthree (double *X, double *Y, int *Z, int N);
+StarData *getstone (CatStats *input, int *nstars);
+void ChipToSky (StarData *stars, int Nstars, Coords *coords);
+void ChipToFP (StarData *stars, int Nstars, Coords *coords);
+void FPtoTP (StarData *stars, int Nstars, Coords *coords);
+void TPtoSky (StarData *stars, int Nstars, Coords *coords);
+void SkyToTP (StarData *stars, int Nstars, Coords *coords);
+void TPtoFP (StarData *stars, int Nstars, Coords *coords);
+void FPtoChip (StarData *stars, int Nstars, Coords *coords);
+int gaussj (double **a, int n, double **b, int m);
+void FitChips (int Norder);
+void FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
+int mkpolyterm (int n, int m);
+void wchip (char *filename, Chip *data);
+void wstars (char *filename, Stars *stars, int Nstars, Header *header);
Index: /trunk/Ohana/src/mosastro/src/FitChips.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/FitChips.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/FitChips.c	(revision 3401)
@@ -19,4 +19,5 @@
   fit_init (coords[0].Npolyterms);
   for (i = 0; i < Nmatch; i++) {
+    if (raw[i].mask) continue;
     fit_add (raw[i].X, raw[i].Y, ref[i].L, ref[i].M);
   }
Index: /trunk/Ohana/src/mosastro/src/FitGradients.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/FitGradients.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/FitGradients.c	(revision 3401)
@@ -4,6 +4,5 @@
 void FitGradients (Gradients *grad) {
 
-  int i, j, Nterm, Norder;
-  double **Pf, **Qf, chisq;
+  int i, Norder;
   Gradients grfix;
 
Index: /trunk/Ohana/src/mosastro/src/GetScatter.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/GetScatter.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/GetScatter.c	(revision 3401)
@@ -21,5 +21,5 @@
     }
   }
-  dR = sqrt (dR2 / Ntotal);
+  dR = sqrt (dR2 / Ntotal) * 3600.0 * field.project.cdelt1;
   *Nscatter = Ntotal;
   return (dR);
Index: /trunk/Ohana/src/mosastro/src/LoadStars.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/LoadStars.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/LoadStars.c	(revision 3401)
@@ -4,6 +4,5 @@
 int LoadStars (int Nfile, char **file) {
 
-  Header *header;
-  int i, j, NCHIP, itmp, Nbytes, nbytes;
+  int i, j, itmp, Nbytes, nbytes;
   FILE *f;
   time_t tsval;
Index: /trunk/Ohana/src/mosastro/src/args.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/args.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/args.c	(revision 3401)
@@ -12,5 +12,4 @@
   
   int N;
-  char line[500];
 
   if (get_argument (*argc, argv, "-help") ||
Index: /trunk/Ohana/src/mosastro/src/chips.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/chips.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/chips.c	(revision 3401)
@@ -2,4 +2,10 @@
 
 /* set chip model based on initial header info */
+
+/** when the chips and field terms are treated independently, they must be of 
+    ctype PLY.  When we reach a solution, and write out a coupled set of chip 
+    and field terms, then we need to transition to WRP (chip) & DIS (field
+**/
+
 int init_chips () {
 
@@ -15,5 +21,5 @@
 
     /* bore site center guess */
-    strcpy (chip[i].map.ctype, "DEC--WRP");
+    strcpy (chip[i].map.ctype, "DEC--PLY");
     chip[i].map.crpix1 = 0.0;
     chip[i].map.crpix2 = 0.0;
@@ -53,2 +59,3 @@
   exit (1);
 }
+
Index: /trunk/Ohana/src/mosastro/src/clip.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/clip.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/clip.c	(revision 3401)
@@ -3,5 +3,5 @@
 int ClipOnFP (double Nsigma) {
 
-  int i, j, Nscatter;
+  int i, j, Nscatter, Nmask, Nkeep;
   double dL, dM, dV;
   double sigma;
@@ -9,9 +9,12 @@
 
   sigma = GetScatter (&Nscatter);
+  fprintf (stderr, "scatter (chip) : %f for %d stars\n", sigma, Nscatter);
+
+  Nmask = Nkeep = 0;
 
   for (i = 0; i < Nchip; i++) {
     raw = chip[i].raw;
     ref = chip[i].ref;
-    for (j = 0; j < chip[i].Nstars; j++) {
+    for (j = 0; j < chip[i].Nmatch; j++) {
       dL = raw[j].L - ref[j].L;
       dM = raw[j].M - ref[j].M;
@@ -19,9 +22,13 @@
       if (dV > Nsigma*sigma) {
 	raw[j].mask = TRUE;
+	Nmask ++;
       } else {
 	raw[j].mask = FALSE;	
+	Nkeep ++;
       }
     }
   }
+
+  fprintf (stderr, "Nmask: %d, Nkeep: %d\n", Nmask, Nkeep);
   return (TRUE);
 }
Index: /trunk/Ohana/src/mosastro/src/dump.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/dump.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/dump.c	(revision 3401)
@@ -66,5 +66,4 @@
 int dump_refcat (StarData *refcat, int Nrefcat) {
 
-  int i;
   FILE *f;
   f = fopen ("refcat.dat", "w");
Index: /trunk/Ohana/src/mosastro/src/fakefield.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/fakefield.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/fakefield.c	(revision 3401)
@@ -38,5 +38,5 @@
 
   /** distort only has power in polyterms **/
-  strcpy (field.distort.ctype, "DEC--WRP");
+  strcpy (field.distort.ctype, "DEC--PLY");
   field.distort.crval1 = 0.0;
   field.distort.crval2 = 0.0;
Index: /trunk/Ohana/src/mosastro/src/field.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/field.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/field.c	(revision 3401)
@@ -2,8 +2,7 @@
 
 /* determine an initial guess to field parameters from data */ 
-int init_field (int Norder) {
+int init_field () {
 
-  int i, Nterm;
-  double PS;
+  int i;
 
   if (FIELD != (char *) NULL) {
@@ -38,5 +37,5 @@
 
   /* bore site center guess */
-  strcpy (field.distort.ctype, "DEC--WRP");
+  strcpy (field.distort.ctype, "DEC--PLY");
   field.distort.crval1 = 0.0;
   field.distort.crval2 = 0.0;
@@ -89,11 +88,31 @@
 
   int i;
+  double cd1, cd2, pc11, pc12, pc21, pc22;
 
-  strcpy (field.project.ctype, "DEC--WRP");
+  /* combine boresite & distortion parameters: ctype DIS */
+  strcpy (field.project.ctype, "DEC--DIS");
+
+  cd1  = field.project.cdelt1;
+  cd2  = field.project.cdelt2;
+  pc11 = field.project.pc1_1;
+  pc12 = field.project.pc1_2;
+  pc21 = field.project.pc2_1;
+  pc22 = field.project.pc2_2;
 
   field.project.Npolyterms = field.distort.Npolyterms;
+
   for (i = 0; i < 7; i++) {
-    field.project.polyterms[i][0] = field.distort.polyterms[i][0];
-    field.project.polyterms[i][1] = field.distort.polyterms[i][1];
+    field.project.polyterms[i][0] = (pc11*cd1*field.distort.polyterms[i][0] + pc12*cd2*field.distort.polyterms[i][1]);
+    field.project.polyterms[i][1] = (pc21*cd1*field.distort.polyterms[i][0] + pc22*cd2*field.distort.polyterms[i][1]);
+  }
+  for (i = 0; i < 2; i++) {
+    field.project.polyterms[0][i] =  field.project.polyterms[0][i] / (cd1*cd1);
+    field.project.polyterms[1][i] =  field.project.polyterms[1][i] / (cd1*cd2);
+    field.project.polyterms[2][i] =  field.project.polyterms[2][i] / (cd2*cd2);
+
+    field.project.polyterms[3][i] =  field.project.polyterms[3][i] / (cd1*cd1*cd1);
+    field.project.polyterms[4][i] =  field.project.polyterms[4][i] / (cd1*cd1*cd2);
+    field.project.polyterms[5][i] =  field.project.polyterms[5][i] / (cd1*cd2*cd2);
+    field.project.polyterms[6][i] =  field.project.polyterms[6][i] / (cd2*cd2*cd2);
   }
 }
@@ -116,4 +135,6 @@
   }
  
+  /* separate field into boresite + distortion terms */
+
   /* bore site center guess */
   strcpy (field.project.ctype, "DEC--TAN");
@@ -135,5 +156,5 @@
 
   /* bore site center guess */
-  strcpy (field.distort.ctype, "DEC--WRP");
+  strcpy (field.distort.ctype, "DEC--PLY");
   field.distort.crval1 = 0.0;
   field.distort.crval2 = 0.0;
Index: /trunk/Ohana/src/mosastro/src/fitpoly.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/fitpoly.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/fitpoly.c	(revision 3401)
@@ -68,5 +68,8 @@
 
   int i, j, n, m, M, N;
-  double max;
+
+  if (Npts == 0) {
+    fprintf (stderr, "warning: no valid pts\n");
+  }
 
   i = 0;
@@ -123,7 +126,7 @@
 
   int i, j, Np, Nv, N;
-  double c10, c11, c12;
-  double c20, c21, c22;
-  double R, D;
+  double c11, c12;
+  double c21, c22;
+  double R;
 
   /* update the higher order terms */
@@ -163,5 +166,5 @@
 
   coords[0].Npolyterms = NORDER;
-  strcpy (coords[0].ctype, "DEC--WRP");
+  strcpy (coords[0].ctype, "DEC--PLY");
 }
 
@@ -174,10 +177,10 @@
 
 /* NORDER is order of gradient fit : Npolyterms is Norder + 1 */
-fit_apply_grads (Coords *distort, Coords *project, int term) {
+void fit_apply_grads (Coords *distort, Coords *project, int term) {
 
   int i, j, Np, Nv1, Nv2;
 
   distort[0].Npolyterms = NORDER + 1;
-  if (distort[0].Npolyterms > 1) strcpy (distort[0].ctype, "DEC--WRP");
+  if (distort[0].Npolyterms > 1) strcpy (distort[0].ctype, "DEC--PLY");
 
   for (i = 0; i < NPOWER + 1; i++) {
@@ -212,5 +215,5 @@
 }
 
-fit_correct_grads (Gradients *in, Gradients *out, int term) {
+void fit_correct_grads (Gradients *in, Gradients *out, int term) {
 
   int i, k, m, n;
Index: /trunk/Ohana/src/mosastro/src/gaussj.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/gaussj.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/gaussj.c	(revision 3401)
@@ -13,4 +13,5 @@
     ipiv[j] = 0;
 
+  icol = irow = 0;
   for (i = 0; i < n; i++) {
     big = 0.0;
Index: /trunk/Ohana/src/mosastro/src/greference.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/greference.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/greference.c	(revision 3401)
@@ -9,4 +9,5 @@
   if (VERBOSE) fprintf (stderr, "loading astrometric reference data from %s\n", REFCAT); 
 
+  stars = NULL;
   Nstars = 0;
   catstats.RA[0]  = field.Rmin;
Index: /trunk/Ohana/src/mosastro/src/match.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/match.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/match.c	(revision 3401)
@@ -4,5 +4,5 @@
 int match (StarData *refcat, int Nrefcat) {
 
-  int i, j, k, N, K, Ntotal;
+  int i, j, k, K, Ntotal;
   int Nmatch, NMATCH;
   double dp, dq, radius, Radius;
Index: /trunk/Ohana/src/mosastro/src/mkmosaic.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/mkmosaic.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/mkmosaic.c	(revision 3401)
@@ -3,5 +3,5 @@
 Header *mkmosaic (int Nx, int Ny, int Nstars, Coords *coords) {
 
-  int i, Nastro;
+  int Nastro;
   double Xmin, Xmax, Ymin, Ymax, tmp, Cerror, Cprecise;
   Header *header;
Index: /trunk/Ohana/src/mosastro/src/mosastro.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/mosastro.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/mosastro.c	(revision 3401)
@@ -9,4 +9,5 @@
   StarData *refcat;
   Gradients *grad;
+
 
   ConfigInit (&argc, argv);
@@ -64,4 +65,5 @@
   if ((DUMP != NULL) && !strcmp (DUMP, "fitchips")) dump_match();
 
+  /* testcoords (); */
   output (argv[2], argv[3]);
   exit (0);
Index: /trunk/Ohana/src/mosastro/src/output.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/output.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/output.c	(revision 3401)
@@ -20,4 +20,6 @@
     strcat (outname, ext);
 
+    /* convert chip from PLY to WRP (linked to field model) */
+    strcpy (chip[i].map.ctype, "DEC--WRP");
     PutCoords (&chip[i].map, &chip[i].header);
     wchip (outname, &chip[i]);
@@ -41,3 +43,25 @@
     fclose (f);
   }
+
+  { 
+
+    FILE *f;
+    double R, D;
+
+    fprintf (stderr, "starting test.dat\n");
+    f = fopen ("test.dat", "w");
+    if (f == (FILE *) NULL) {
+      fprintf (stderr, "ERROR: can't create output file \n");
+      exit (1);
+    }
+
+    RegisterMosaic (&field.project);
+    for (i = 0; i < chip[0].Nmatch; i++) {
+      XY_to_RD (&R, &D, chip[0].raw[i].X, chip[0].raw[i].Y, &chip[0].map);
+      fprintf (f, "%4d  %10.6f %10.6f  %8.2f %8.2f\n", 
+	       i, R, D, chip[0].raw[i].X, chip[0].raw[i].Y);
+    }
+    fclose (f);
+  }
+  
 }
Index: /trunk/Ohana/src/mosastro/src/parse_time.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/parse_time.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/parse_time.c	(revision 3401)
@@ -1,11 +1,12 @@
 # include "mosastro.h"
 
-parse_time (Header *header) {
+e_time parse_time (Header *header) {
 
   double jd;
   int Ny, Nf, mode;
-  int Nsec, hour, min, sec, year, month, day;
+  int hour, min, sec, year, month, day;
   char *py, *pm, *pd, *c;
   char line[256];
+  e_time Nsec;
 
   /* we want to find JD or MJD to get Nsec (seconds since 01/01/1970) */
@@ -75,4 +76,5 @@
   for (c = strchr (line, 0x2d); c != (char *) NULL; c = strchr (line, 0x2d)) { *c = ' '; }
 
+  Nf = 0;
   switch (mode) {
   case 1:
@@ -118,5 +120,5 @@
 }
 
-uppercase (char *string) {
+void uppercase (char *string) {
 
   int i;
Index: /trunk/Ohana/src/mosastro/src/project.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/project.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/project.c	(revision 3401)
@@ -2,6 +2,4 @@
 
 int project_refcat (StarData *refcat, int Nrefcat) {
-
-  int i;
 
   SkyToTP (refcat, Nrefcat, &field.project);
@@ -13,5 +11,5 @@
 int deproject_stars () {
 
-  int i, j;
+  int i;
 
   for (i = 0; i < Nchip; i++) {
@@ -23,5 +21,5 @@
 int project_stars () {
 
-  int i, j;
+  int i;
 
   for (i = 0; i < Nchip; i++) {
@@ -34,5 +32,5 @@
 int deproject_raw () {
 
-  int i, j;
+  int i;
 
   for (i = 0; i < Nchip; i++) {
@@ -46,5 +44,5 @@
 int project_ref () {
 
-  int i, j;
+  int i;
 
   for (i = 0; i < Nchip; i++) {
Index: /trunk/Ohana/src/mosastro/src/transforms.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/transforms.c	(revision 3400)
+++ /trunk/Ohana/src/mosastro/src/transforms.c	(revision 3401)
@@ -1,5 +1,5 @@
 # include "mosastro.h"
 
-ChipToSky (StarData *stars, int Nstars, Coords *coords) {
+void ChipToSky (StarData *stars, int Nstars, Coords *coords) {
   int i;
   for (i = 0; i < Nstars; i++) {
@@ -10,5 +10,5 @@
 }
 
-ChipToFP (StarData *stars, int Nstars, Coords *coords) {
+void ChipToFP (StarData *stars, int Nstars, Coords *coords) {
   int i;
   for (i = 0; i < Nstars; i++) {
@@ -17,5 +17,5 @@
 }
 
-FPtoTP (StarData *stars, int Nstars, Coords *coords) {
+void FPtoTP (StarData *stars, int Nstars, Coords *coords) {
   int i;
   for (i = 0; i < Nstars; i++) {
@@ -24,5 +24,5 @@
 }
 
-TPtoSky (StarData *stars, int Nstars, Coords *coords) {
+void TPtoSky (StarData *stars, int Nstars, Coords *coords) {
   int i;
   for (i = 0; i < Nstars; i++) {
@@ -33,5 +33,5 @@
 }
 
-SkyToTP (StarData *stars, int Nstars, Coords *coords) {
+void SkyToTP (StarData *stars, int Nstars, Coords *coords) {
   int i;
   for (i = 0; i < Nstars; i++) {
@@ -40,5 +40,5 @@
 }
 
-TPtoFP (StarData *stars, int Nstars, Coords *coords) {
+void TPtoFP (StarData *stars, int Nstars, Coords *coords) {
   int i;
   for (i = 0; i < Nstars; i++) {
@@ -47,5 +47,5 @@
 }
 
-FPtoChip (StarData *stars, int Nstars, Coords *coords) {
+void FPtoChip (StarData *stars, int Nstars, Coords *coords) {
   int i;
   for (i = 0; i < Nstars; i++) {
