Index: /trunk/Ohana/src/imclean/Makefile
===================================================================
--- /trunk/Ohana/src/imclean/Makefile	(revision 3411)
+++ /trunk/Ohana/src/imclean/Makefile	(revision 3412)
@@ -24,5 +24,6 @@
 $(SRC)/find_trails.$(ARCH).o	$(SRC)/find_line.$(ARCH).o \
 $(SRC)/LoadStarsDophot.$(ARCH).o $(SRC)/wstars.$(ARCH).o	\
-$(SRC)/LoadStarsSex.$(ARCH).o	$(SRC)/LoadStarsChad.$(ARCH).o	
+$(SRC)/LoadStarsSex.$(ARCH).o	$(SRC)/LoadStarsChad.$(ARCH).o	\
+$(SRC)/wfits.$(ARCH).o
 
 default: $(PROGRAM)
Index: /trunk/Ohana/src/imclean/include/imclean.h
===================================================================
--- /trunk/Ohana/src/imclean/include/imclean.h	(revision 3411)
+++ /trunk/Ohana/src/imclean/include/imclean.h	(revision 3412)
@@ -5,4 +5,5 @@
 
 int    MODE;
+int    FITS_OUTPUT;
 int    VERBOSE;
 int    RESET;
@@ -38,4 +39,19 @@
   double fx, fy, df;
   double Mgal, Map;
+} StarsOld;
+
+typedef struct {
+  float X;
+  float Y;
+  float M;
+  float dM;
+  float Mgal;
+  float Map;
+  float sky;
+  float fx;
+  float fy;
+  float df;
+  char  dophot;
+  char  dummy[3];
 } Stars;
 
Index: /trunk/Ohana/src/imclean/src/args.c
===================================================================
--- /trunk/Ohana/src/imclean/src/args.c	(revision 3411)
+++ /trunk/Ohana/src/imclean/src/args.c	(revision 3412)
@@ -21,4 +21,9 @@
   if ((N = get_argument (argc, argv, "-v"))) {
     VERBOSE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  FITS_OUTPUT = FALSE;
+  if ((N = get_argument (argc, argv, "-fits"))) {
+    FITS_OUTPUT = TRUE;
     remove_argument (N, &argc, argv);
   }
Index: /trunk/Ohana/src/imclean/src/imclean.c
===================================================================
--- /trunk/Ohana/src/imclean/src/imclean.c	(revision 3411)
+++ /trunk/Ohana/src/imclean/src/imclean.c	(revision 3412)
@@ -38,5 +38,9 @@
   fix_total (stars, Nstars, &header);
 
-  wstars (argv[3], stars, Nstars, &header); 
+  if (FITS_OUTPUT) {
+    wfits (argv[3], stars, Nstars, &header); 
+  } else {
+    wstars (argv[3], stars, Nstars, &header); 
+  }
 
   fprintf (stderr, "SUCCESS\n");
Index: /trunk/Ohana/src/imclean/src/wfits.c
===================================================================
--- /trunk/Ohana/src/imclean/src/wfits.c	(revision 3412)
+++ /trunk/Ohana/src/imclean/src/wfits.c	(revision 3412)
@@ -0,0 +1,108 @@
+# include "imclean.h"
+int ConvertStars (Stars *data, int size, int nitems);
+
+void wfits (char *filename, Stars *stars, int Nstars, Header *header) {
+
+  int i;
+  Matrix matrix;
+  Header theader;
+  FTable table;
+
+  header[0].extend = TRUE;
+  header[0].Naxes = 0;
+  fits_modify (header, "NAXIS",   "%d", 1, 0);
+  fits_modify (header, "EXTEND",  "%t", 1, TRUE);
+  fits_modify (header, "NEXTEND", "%d", 1, 1);
+
+  /* add in some keywords to specify the datatype & software version? */
+
+  /* create (empty) data matrix */
+  fits_create_matrix (header, &matrix);
+    
+  /* create bintable header */
+  fits_create_table_header (&theader, "BINTABLE", "SMPFILE");
+
+  /* define bintable layout */
+  fits_define_bintable_column (&theader, "E",    "X_PIX",      "x coord",              "pixels",                         1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "Y_PIX",      "y coord",              "pixels",                         1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "MAG_RAW",    "inst mags",            "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "MAG_ERR",    "inst mag error",       "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "MAG_GAL",    "galaxy mag",           "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "MAG_AP",     "aperture mag",         "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "LOG_SKY",    "log-10 of sky",        "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "FWHM_X",     "semi-major",           "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "FWHM_Y",     "semi-minor",           "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "E",    "THETA",      "ellipse angle",        "mags",                           1.0, 0.0); 
+  fits_define_bintable_column (&theader, "B",    "DOPHOT",     "dophot type",          "",                              1.0, 0.0);
+  fits_define_bintable_column (&theader, "3A",   "DUMMY",      "padding",              "",                              1.0, 0.0);
+
+  /* create table, add data values */
+  fits_create_table (&theader, &table);
+
+  ConvertStars (stars, sizeof (Stars), Nstars);
+  fits_add_rows (&table, (char *) stars, Nstars, sizeof (Stars));
+
+  fits_write_header  (filename, header);
+  fits_write_matrix  (filename, &matrix);
+  fits_write_Theader (filename, &theader);
+  fits_write_table   (filename, &table);
+  exit (0);
+}
+
+# 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
+
+# define STARS_SIZE 44
+
+int ConvertStars (Stars *data, int size, int nitems) {
+
+  int i;
+  unsigned char *byte, tmp;
+
+# ifdef BYTE_SWAP
+
+  if (size != STARS_SIZE) {
+    fprintf (stderr, "mismatch in type sizes (Stars) %d vs %d\n", size, STARS_SIZE);
+    return (FALSE);
+  }
+
+  byte = (char *) data;
+  for (i = 0; i < nitems; i++, byte += size) {
+    SWAP_WORD (0);   /* R */
+    SWAP_WORD (4);   /* Y */
+    SWAP_WORD (8);   /* M */
+    SWAP_WORD (12);  /* dM */
+    SWAP_WORD (16);  /* Mgal */
+    SWAP_WORD (20);  /* Map */
+    SWAP_WORD (24);  /* sky */
+    SWAP_WORD (28);  /* fx */
+    SWAP_WORD (32);  /* fy */
+    SWAP_WORD (36);  /* df */
+  }
+  return (TRUE);
+
+# else
+  return (TRUE);
+# endif  
+} 
+
