Index: trunk/Ohana/src/addstar/include/addstar.h
===================================================================
--- trunk/Ohana/src/addstar/include/addstar.h	(revision 4988)
+++ trunk/Ohana/src/addstar/include/addstar.h	(revision 5014)
@@ -124,5 +124,5 @@
 int        Shutdown               PROTO((char *message, ...); ) 
 void       TrapSignal             PROTO((int sig));
-short      airmass                PROTO((short secz_image, double ra, double dec, double st, double latitude));
+float      airmass                PROTO((float secz_image, double ra, double dec, double st, double latitude));
 void       aregion                PROTO((GSCRegion *region, FILE *f, double ra, double dec));
 int        args                   PROTO((int argc, char **argv));
Index: trunk/Ohana/src/addstar/src/airmass.c
===================================================================
--- trunk/Ohana/src/addstar/src/airmass.c	(revision 4988)
+++ trunk/Ohana/src/addstar/src/airmass.c	(revision 5014)
@@ -1,5 +1,5 @@
 # include "addstar.h"
 
-short airmass (short secz_image, double ra, double dec, double st, double latitude) {
+float airmass (float secz_image, double ra, double dec, double st, double latitude) {
 
   double hour, cosz, secz;
@@ -13,7 +13,5 @@
   hour = 15.0*st - ra;
   cosz = sin (RAD_DEG*dec) * sin (RAD_DEG*latitude) + cos (RAD_DEG*dec) * cos (RAD_DEG*hour) * cos (RAD_DEG*latitude);
-  
-  /* modify to match storage units (millimags) */
-  secz = 1000.0 / cosz;
+  secz = 1.000 / cosz;
   return (secz);
 }
Index: trunk/Ohana/src/addstar/src/calibrate.c
===================================================================
--- trunk/Ohana/src/addstar/src/calibrate.c	(revision 4988)
+++ trunk/Ohana/src/addstar/src/calibrate.c	(revision 5014)
@@ -95,6 +95,6 @@
   if (MaxN == -1) {
     fprintf (stderr, "no clean stars\n");
-    image[0].Mcal = 10000;
-    image[0].dMcal = 10000;
+    image[0].Mcal_PS = 10.000;
+    image[0].dMcal_PS = 10.000;
     return;
   }
@@ -137,6 +137,6 @@
   if (Nkeep < 5) {
     fprintf (stderr, "too few stars\n");
-    image[0].Mcal = 10000;
-    image[0].dMcal = 10000;
+    image[0].Mcal_PS = 10.000;
+    image[0].dMcal_PS = 10.000;
     return;
   }
@@ -169,14 +169,12 @@
     Mw = W1 / W2;
     fprintf (stderr, "N: %.0f, mean: %f, wt mean: %f, stdev: %f, precision: %f\n", N, M1, Mw, M2, M2 / sqrt (N));
-    image[0].Mcal = 1000 * M1;
-    image[0].dMcal = 1000 * M2 / sqrt (N);
+    image[0].Mcal_PS = M1;
+    image[0].dMcal_PS = M2 / sqrt (N);
     image[0].Mxxxx = N;
   } else {
     fprintf (stderr, "too few stars\n");
-    image[0].Mcal = 10000;
-    image[0].dMcal = 10000;
+    image[0].Mcal_PS = 10.000;
+    image[0].dMcal_PS = 10.000;
     image[0].Mxxxx = 0;
   }
 }
-
-/*** image.Mcal is still in millimags ***/
Index: trunk/Ohana/src/addstar/src/find_matches.c
===================================================================
--- trunk/Ohana/src/addstar/src/find_matches.c	(revision 4988)
+++ trunk/Ohana/src/addstar/src/find_matches.c	(revision 5014)
@@ -12,6 +12,6 @@
   Missing *tmpmissing;
   Coords tcoords;
-  int Nsecfilt, Nsec, MTIME;
-  float Mcat, *Mval;
+  int Nsecfilt, Nsec;
+  float Mcat, *Mval, MTIME;
 
   /* photcode data - must by of type DEP, (PRI, SEC) - probably should restrict to DEP */
@@ -143,5 +143,5 @@
 	
       /* calculate accurate per-star airmass */
-      secz = airmass (image[0].secz, stars[N].R, stars[N].D, SiderealTime, Latitude);
+      secz = airmass (image[0].secz_PS, stars[N].R, stars[N].D, SiderealTime, Latitude);
       
       /** add measurements for this star **/
@@ -151,5 +151,5 @@
       catalog[0].measure[Nmeas].M_PS        = MIN (stars[N].M + MTIME, NO_MAG);
       catalog[0].measure[Nmeas].dM_PS       = MIN (stars[N].dM, NO_ERR);  /* error in input files stored in thousandths of mag */
-      catalog[0].measure[Nmeas].Mcal_PS     = 0.001*image[0].Mcal; /* XXX image.Mcal still millimags */
+      catalog[0].measure[Nmeas].Mcal_PS     = image[0].Mcal_PS;
       catalog[0].measure[Nmeas].t           = image[0].tzero + 1e-4*stars[N].Y*image[0].trate;  /* trate is in 0.1 msec / row */
       catalog[0].measure[Nmeas].averef      = n;
@@ -252,5 +252,5 @@
     if (!IN_CATALOG (stars[N].R, stars[N].D)) continue;
 
-    secz = airmass (image[0].secz, stars[N].R, stars[N].D, SiderealTime, Latitude);
+    secz = airmass (image[0].secz_PS, stars[N].R, stars[N].D, SiderealTime, Latitude);
 
     catalog[0].average[Nave].R_PS      = stars[N].R;
@@ -277,5 +277,5 @@
     catalog[0].measure[Nmeas].M_PS        = MIN (stars[N].M + MTIME, NO_MAG);
     catalog[0].measure[Nmeas].dM_PS       = MIN (stars[N].dM, NO_ERR);
-    catalog[0].measure[Nmeas].Mcal_PS  	  = 0.001*image[0].Mcal;
+    catalog[0].measure[Nmeas].Mcal_PS  	  = image[0].Mcal_PS;
     catalog[0].measure[Nmeas].t        	  = image[0].tzero + 1e-4*stars[N].Y*image[0].trate; /* trate is in 0.1 msec / row */
     catalog[0].measure[Nmeas].averef   	  = Nave;
Index: trunk/Ohana/src/addstar/src/gcatalog.c
===================================================================
--- trunk/Ohana/src/addstar/src/gcatalog.c	(revision 4988)
+++ trunk/Ohana/src/addstar/src/gcatalog.c	(revision 5014)
@@ -6,6 +6,5 @@
   char mode;
 
-  /* no autodetect for CATMODE yet */
-  strcpy (catalog[0].catmode, CATMODE);
+  /* CATMODE and CATFORMAT determined from catalog */
 
   /* read catalog header */
Index: trunk/Ohana/src/addstar/src/gstars.c
===================================================================
--- trunk/Ohana/src/addstar/src/gstars.c	(revision 4988)
+++ trunk/Ohana/src/addstar/src/gstars.c	(revision 5014)
@@ -4,5 +4,5 @@
 
   FILE *f;
-  int j, N, Nbytes;
+  int j, N, Nbytes, extend;
   int itmp, hour, min;
   char *c, photcode[64], line[80];
@@ -76,11 +76,12 @@
   image[0].exptime = tmp;
   
+  /*** why are we no longer using APMIFIT?? ***/
   tmp = 0;
   /* fits_scan (&header, "APMIFIT",  "%lf", 1, &tmp); */
-  image[0].apmifit = tmp * 1000.0;
+  image[0].apmifit_PS = tmp;
 
   tmp = 0;
   /* fits_scan (&header, "dAPMIFIT", "%lf", 1, &tmp); */
-  image[0].dapmifit = tmp * 1000.0;
+  image[0].dapmifit_PS = tmp;
 
   tmp = 0;
@@ -110,5 +111,5 @@
   tmp = 0;
   fits_scan (&header, AirmassKeyword, "%lf", 1, &tmp);
-  image[0].secz = MIN (NO_MAG, 1000*tmp);
+  image[0].secz_PS = MIN (NO_MAG, tmp);
 
   if (!fits_scan (&header, CCDNumKeyword, "%d", 1, &itmp)) {
@@ -122,5 +123,5 @@
 
   /* secz is in units milli-airmass */
-  image[0].Mcal = 0.0;
+  image[0].Mcal_PS = 0.0;
   image[0].Xm   = NO_MAG;
   image[0].code = 0;
@@ -162,4 +163,22 @@
   fseek (f, header.size, SEEK_SET); 
 
+  /* read from FITS table or from text table */
+  extend = FALSE;
+  fits_scan (&header, "EXTEND",  "%t", 1, &extend);
+  if (extend) {
+    Nbytes = fits_matrix_size (&header);
+    fseek (f, Nbytes, SEEK_CUR); 
+    rdstars = rfits (f, &image[0].nstar);
+  } else {
+    /* allocate space for stars */
+    if (!fits_scan (&header, "NSTARS", "%d", 1, &image[0].nstar)) {
+      fprintf (stderr, "ERROR: failed to find NSTARS\n");
+      exit (1);
+    }
+    rdstars = rtext (f, &image[0].nstar);
+  }
+  fclose (f);
+
+  /* 
   if (FITS_INPUT) {
     Nbytes = fits_matrix_size (&header); 
@@ -170,4 +189,5 @@
   }
   fclose (f);
+  */
 
   /* modify resulting star list */
Index: trunk/Ohana/src/addstar/src/image-db.c
===================================================================
--- trunk/Ohana/src/addstar/src/image-db.c	(revision 4988)
+++ trunk/Ohana/src/addstar/src/image-db.c	(revision 5014)
@@ -39,4 +39,5 @@
   fits_init_header (&db[0].header);
 
+  /* set the recommended mode */
   if (!strcmp (CATMODE, "RAW")) {
     /* make header a fake image */
@@ -46,8 +47,22 @@
     db[0].header.Naxis[1] = 1;
     fits_create_header (&db[0].header);
+    db[0].mode = DVO_MODE_RAW;
   }
 
   if (!strcmp (CATMODE, "MEF") || !strcmp (CATMODE, "SPLIT")) {
+    db[0].header.extend   = TRUE;
     fits_create_header (&db[0].header);
+    db[0].mode = DVO_MODE_MEF;
+  }
+
+  /* set the recommended format */
+  db[0].format = DVO_FORMAT_UNDEF;
+  if (!strcmp (CATFORMAT, "INTERNAL"))  db[0].format = DVO_FORMAT_INTERNAL;
+  if (!strcmp (CATFORMAT, "LONEOS"))    db[0].format = DVO_FORMAT_LONEOS;
+  if (!strcmp (CATFORMAT, "ELIXIR"))    db[0].format = DVO_FORMAT_ELIXIR;
+  if (!strcmp (CATFORMAT, "PANSTARRS")) db[0].format = DVO_FORMAT_PANSTARRS;
+  if (db[0].format == DVO_FORMAT_UNDEF) {
+    fprintf (stderr, "invalid output catalog format\n");
+    exit (1);
   }
 
@@ -58,4 +73,8 @@
   fits_modify (&db[0].header, "ZERO_PT", "%lf", 1, ZeroPt);
   fits_modify (&db[0].header, "NIMAGES", "%d", 1, 0);
+
+  if (db[0].format == DVO_FORMAT_INTERNAL)  fits_modify (&db[0].header, "FORMAT", "%s", 1, "INTERNAL");
+  if (db[0].format == DVO_FORMAT_LONEOS)    fits_modify (&db[0].header, "FORMAT", "%s", 1, "LONEOS");
+  if (db[0].format == DVO_FORMAT_ELIXIR)    fits_modify (&db[0].header, "FORMAT", "%s", 1, "ELIXIR");
+  if (db[0].format == DVO_FORMAT_PANSTARRS) fits_modify (&db[0].header, "FORMAT", "%s", 1, "PANSTARRS");
 }
-
Index: trunk/Ohana/src/addstar/src/mkcatalog.c
===================================================================
--- trunk/Ohana/src/addstar/src/mkcatalog.c	(revision 4988)
+++ trunk/Ohana/src/addstar/src/mkcatalog.c	(revision 5014)
@@ -8,5 +8,30 @@
   if (VERBOSE) fprintf (stderr, "new catalog file: %s\n", region[0].filename);
 
+  /* set the recommended CATFORMAT */
+  catalog[0].catformat = DVO_FORMAT_UNDEF;
+  if (!strcmp (CATFORMAT, "INTERNAL"))  catalog[0].catformat = DVO_FORMAT_INTERNAL;
+  if (!strcmp (CATFORMAT, "LONEOS"))    catalog[0].catformat = DVO_FORMAT_LONEOS;
+  if (!strcmp (CATFORMAT, "ELIXIR"))    catalog[0].catformat = DVO_FORMAT_ELIXIR;
+  if (!strcmp (CATFORMAT, "PANSTARRS")) catalog[0].catformat = DVO_FORMAT_PANSTARRS;
+  if (catalog[0].catformat == DVO_FORMAT_UNDEF) {
+    fprintf (stderr, "invalid output catalog format\n");
+    exit (1);
+  }
+
+  /* set the recommended CATMODE */
+  catalog[0].catmode = DVO_MODE_RAW;
+  if (!strcmp (CATMODE, "RAW"))  catalog[0].catmode = DVO_MODE_RAW;
+  if (!strcmp (CATMODE, "MEF"))  catalog[0].catmode = DVO_MODE_MEF;
+
   fits_init_header (&catalog[0].header);
+
+  if (catalog[0].catmode == DVO_MODE_RAW) {
+    /* make header a fake image */
+    catalog[0].header.bitpix   = 16;
+    catalog[0].header.Naxes    = 2;
+    catalog[0].header.Naxis[0] = 1;
+    catalog[0].header.Naxis[1] = 1;
+  }
+
   fits_create_header (&catalog[0].header);
   
@@ -34,15 +59,3 @@
   ALLOCATE (catalog[0].secfilt, SecFilt, 1);
   catalog[0].Nsecfilt = GetPhotcodeNsecfilt ();
-
-  /* set catalog mode (raw, mef, etc) - no autodetect for CATMODE yet */
-  strcpy (catalog[0].catmode, CATMODE);
-
-  /* set catalog format (elixir, loneos, etc) */
-  if (!strcmp (CATFORMAT, "INTERNAL")) {
-    sprintf (catalog[0].average_format, "DVO_AVERAGE");
-    sprintf (catalog[0].measure_format, "DVO_MEASURE");
-  } else {
-    sprintf (catalog[0].average_format, "DVO_AVERAGE_%s", CATFORMAT);
-    sprintf (catalog[0].measure_format, "DVO_MEASURE_%s", CATFORMAT);
-  }
 }
Index: trunk/Ohana/src/addstar/src/wcatalog.c
===================================================================
--- trunk/Ohana/src/addstar/src/wcatalog.c	(revision 4988)
+++ trunk/Ohana/src/addstar/src/wcatalog.c	(revision 5014)
@@ -3,12 +3,6 @@
 void wcatalog (Catalog *catalog) {
   
-  /* set catalog format (elixir, loneos, etc) */
-  if (!strcmp (CATFORMAT, "INTERNAL")) {
-    sprintf (catalog[0].average_format, "DVO_AVERAGE");
-    sprintf (catalog[0].measure_format, "DVO_MEASURE");
-  } else {
-    sprintf (catalog[0].average_format, "DVO_AVERAGE_%s", CATFORMAT);
-    sprintf (catalog[0].measure_format, "DVO_MEASURE_%s", CATFORMAT);
-  }
+  /* output CATMODE and CATFORMAT are set by mkcatalog 
+     or by mode and format of existing data */
 
   if (!save_catalog (catalog, VERBOSE)) {
