Index: /tags/libautocode-1-1/Ohana/src/libautocode/.cvsignore
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/.cvsignore	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/.cvsignore	(revision 4781)
@@ -0,0 +1,3 @@
+include
+lib
+src
Index: /tags/libautocode-1-1/Ohana/src/libautocode/Makefile
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/Makefile	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/Makefile	(revision 4781)
@@ -0,0 +1,114 @@
+default: autocode
+help:
+	@echo "make options: install autocode (default)"
+
+include ../../Configure
+
+HOME	=	$(ROOT)/src/libautocode
+SRC	=	$(HOME)/src
+BIN	=	$(HOME)/bin
+DEF	=	$(HOME)/def
+LIB	=	$(HOME)/lib
+INC	=	$(HOME)/include
+MAN	= 	$(HOME)/doc
+DESTLIB	=	$(LLIB)
+DESTINC	=	$(LINC)
+DESTMAN	=	$(LMAN)
+
+INCS	=	-I$(INC) -I$(LINC)
+LIBS	= 	-L$(LLIB)
+CFLAGS	=	$(INCS) -D$(ARCH)
+LFLAGS	=	$(LIBS)
+
+# remember to add entries here to libohana/Makefile:AOBJ as well
+LIBOBJ = \
+$(SRC)/coords.$(ARCH).o \
+$(SRC)/average.$(ARCH).o \
+$(SRC)/secfilt.$(ARCH).o \
+$(SRC)/measure.$(ARCH).o \
+$(SRC)/missing.$(ARCH).o \
+$(SRC)/photcode.$(ARCH).o \
+$(SRC)/image.$(ARCH).o \
+$(SRC)/regimage.$(ARCH).o \
+$(SRC)/detreg.$(ARCH).o \
+$(SRC)/photreg.$(ARCH).o \
+$(SRC)/photreg-old.$(ARCH).o \
+$(SRC)/smpdata.$(ARCH).o \
+$(SRC)/spectrum.$(ARCH).o \
+$(SRC)/spectrum-ascii.$(ARCH).o
+
+LIBINC = \
+$(INC)/coords.h \
+$(INC)/average.h \
+$(INC)/secfilt.h \
+$(INC)/measure.h \
+$(INC)/missing.h \
+$(INC)/photcode.h \
+$(INC)/image.h \
+$(INC)/regimage.h \
+$(INC)/detreg.h \
+$(INC)/photreg.h \
+$(INC)/photreg-old.h \
+$(INC)/smpdata.h \
+$(INC)/spectrum.h \
+$(INC)/spectrum-ascii.h
+
+objects: $(LIBOBJ)
+
+install: $(DESTINC)/autocode.h $(DESTLIB)/libautocode.a
+	@echo done install
+
+autocode: $(INC)/autocode.h $(LIB)/libautocode.$(ARCH).a 
+	@echo done autocode
+
+$(LIBOBJ): $(INC)/autocode.h
+
+$(INC)/autocode.h: $(LIBINC)
+	cat $(DEF)/common.h >  $(INC)/autocode.h
+	cat $(LIBINC)       >> $(INC)/autocode.h
+	@echo "# endif"     >> $(INC)/autocode.h
+	@echo done with libautocode.a
+
+$(DESTINC)/autocode.h: $(INC)/autocode.h
+	@if [ ! -d $(DESTINC) ]; then mkdir -p $(DESTINC); fi
+	rm -f $(DESTINC)/autocode.h
+	cp $(INC)/autocode.h $(DESTINC)/
+	@echo installed autocode.h
+
+$(LIB)/libautocode.$(ARCH).a: $(LIBOBJ)
+	@if [ ! -d $(LIB) ]; then mkdir -p $(LIB); fi
+	rm -f $(LIB)/libautocode.$(ARCH).a
+	ar rcv $(LIB)/libautocode.$(ARCH).a $(LIBOBJ)
+	$(RANLIB) $(LIB)/libautocode.$(ARCH).a
+	@echo done with libautocode.$(ARCH).a
+
+$(DESTLIB)/libautocode.a: $(LIB)/libautocode.$(ARCH).a 
+	@if [ ! -d $(DESTLIB) ]; then mkdir -p $(DESTLIB); fi
+	rm -f  $(DESTLIB)/libautocode.a
+	cp $(LIB)/libautocode.$(ARCH).a $(DESTLIB)/libautocode.a
+	@echo installed libautocode.a
+
+$(INC)/%.h: $(DEF)/%.d $(DEF)/autocode.h
+	@if [ ! -d $(INC) ]; then mkdir -p $(INC); fi
+	generate $< $(DEF)/autocode.h $@
+
+$(SRC)/%.c: $(DEF)/%.d $(DEF)/autocode.c
+	@if [ ! -d $(SRC) ]; then mkdir -p $(SRC); fi
+	generate $< $(DEF)/autocode.c $@
+
+%.$(ARCH).o: %.c
+	$(CC) $(CFLAGS) -c $*.c -o $@
+
+clean:	
+	rm -f `find . -name "*.o"`
+	rm -f `find . -name "*.a"`
+	rm -f `find . -name "*~"`
+	rm -f `find . -name "#*"`
+	rm -f `find src -name "*.c"`
+	rm -f `find include -name "*.h"`
+# 	this is an unusual target!  it deletes .c and .h files!
+
+.PRECIOUS: $(SRC)/%.c
+.PRECIOUS: %.$(ARCH).o
+.PRECIOUS: $(BIN)/%.$(ARCH)
+.PRECIOUS: $(LIB)/%.$(ARCH).a
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/autocode.c
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/autocode.c	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/autocode.c	(revision 4781)
@@ -0,0 +1,71 @@
+# include "autocode.h"
+
+int fits_convert_$STRUCT ($STRUCT *data, int size, int nitems) {
+
+  int i;
+  unsigned char *byte, tmp;
+
+  if (size != $SIZE) { 
+    fprintf (stderr, "ERROR: mismatch in data types $STRUCT: %d vs %d\n",
+	     size, $SIZE);
+    exit (1);
+  }
+
+  /* provide initial values to avoid compiler warnings for non-BYTE_SWAP arch */
+  i = tmp = 0;
+  byte = NULL;
+
+# ifdef BYTE_SWAP
+  byte = (char *) data;
+  for (i = 0; i < nitems; i++, byte += $SIZE) {
+    /** BYTE SWAP **/
+  }
+# endif  
+
+  return (TRUE);
+} 
+
+/*** add test of EXTNAME and header-defined columns? ***/
+$STRUCT *fits_table_get_$STRUCT (FTable *ftable, int *Ndata) {
+
+  $STRUCT *data;
+
+  *Ndata = ftable[0].header[0].Naxis[1];
+  data = ($STRUCT *) ftable[0].buffer;
+  fits_convert_$STRUCT (data, sizeof ($STRUCT), *Ndata);
+  
+  return (data);
+}
+
+int fits_table_set_$STRUCT (FTable *ftable, $STRUCT *data, int Ndata) {
+
+  Header *header;
+
+  header = ftable[0].header;
+
+  /* create table header */
+  fits_create_table_header (header, "$TYPE", "$EXTNAME");
+
+  /* define table layout */
+  /** TABLE DEFINITION **/
+
+  /* create table */
+  fits_create_table (header, ftable);
+
+  /* add data values */
+  fits_convert_$STRUCT (data, sizeof ($STRUCT), Ndata);
+  fits_add_rows (ftable, (char *) data, Ndata, sizeof ($STRUCT));
+
+  return (TRUE);
+}
+
+int fits_table_mkheader_$STRUCT (Header *header) {
+
+  /* create table header */
+  fits_create_table_header (header, "$TYPE", "$EXTNAME");
+
+  /* define table layout */
+  /** TABLE DEFINITION **/
+
+  return (TRUE);
+}
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/autocode.h
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/autocode.h	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/autocode.h	(revision 4781)
@@ -0,0 +1,7 @@
+
+/** STRUCT DEFINITION **/
+
+$STRUCT *fits_table_get_$STRUCT (FTable *table, int *Ndata);
+int      fits_table_set_$STRUCT (FTable *ftable, $STRUCT *data, int Ndata);
+int      fits_table_mkheader_$STRUCT (Header *header);
+int      fits_convert_$STRUCT ($STRUCT *data, int size, int nitems);
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/average-loneos.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/average-loneos.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/average-loneos.d	(revision 4781)
@@ -0,0 +1,17 @@
+STRUCT  AverageLoneos
+EXTNAME DVO_AVERAGE_LONEOS
+TYPE    BINTABLE
+SIZE    28
+
+# elements of data structure / FITS table
+
+FIELD R,              RA,         float,            RA,                	       	  decimal degrees 
+FIELD D,              DEC,        float,            DEC,               	       	  decimal degrees 
+FIELD M,              MAG,        short,            primary mag,       	       	  millimag
+FIELD Nm,             NMEAS,      unsigned short,   number of measures
+FIELD Nn,             NMISS,      unsigned short,   number of missings
+FIELD Xp,             SIGMA_POS,  short, 	    position scatter,   	  1/100 arcsec
+FIELD Xm,             CHISQ_MAG,  short, 	    chisq for primary mag,        [1000*value]  ?
+FIELD code,           code,       unsigned short,   ID code (star, ghost, etc)
+FIELD offset,         offset,     int,     	    offset to first measurement
+FIELD missing,        missing,    int,     	    offset to first missing obs
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/average-panstarrs.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/average-panstarrs.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/average-panstarrs.d	(revision 4781)
@@ -0,0 +1,19 @@
+STRUCT  AveragePanstarrs
+EXTNAME DVO_AVERAGE_PANSTARRS
+TYPE    BINTABLE
+SIZE    40
+
+# elements of data structure / FITS table
+
+FIELD R,              RA,         double,           RA,                	       	  decimal degrees 
+FIELD D,              DEC,        double,           DEC,               	       	  decimal degrees 
+FIELD M,              MAG,        short,            primary mag,       	       	  millimag
+FIELD Nm,             NMEAS,      unsigned short,   number of measures
+FIELD Nn,             NMISS,      unsigned short,   number of missings
+FIELD Xp,             SIGMA_POS,  short, 	    position scatter,   	  1/100 arcsec
+FIELD Xm,             CHISQ_MAG,  short, 	    chisq for primary mag,        [1000*value]  ?
+FIELD code,           code,       unsigned short,   ID code (star, ghost, etc)
+FIELD offset,         offset,     int,     	    offset to first measurement
+FIELD missing,        missing,    int,     	    offset to first missing obs
+FIELD dM,             MAG_ERR,    short,            error on primary mag,         [1000*log(value)] ?
+FIELD Xg,             Xg,         short,            best chisq value
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/average.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/average.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/average.d	(revision 4781)
@@ -0,0 +1,19 @@
+STRUCT  Average
+EXTNAME DVO_AVERAGE
+TYPE    BINTABLE
+SIZE    32
+
+# elements of data structure / FITS table
+
+FIELD R,              RA,         float,            RA,                	       	  decimal degrees 
+FIELD D,              DEC,        float,            DEC,               	       	  decimal degrees 
+FIELD M,              MAG,        short,            primary mag,       	       	  millimag
+FIELD Nm,             NMEAS,      unsigned short,   number of measures
+FIELD Nn,             NMISS,      unsigned short,   number of missings
+FIELD Xp,             SIGMA_POS,  short, 	    position scatter,   	  1/100 arcsec
+FIELD Xm,             CHISQ_MAG,  short, 	    chisq for primary mag,        [1000*value]  ?
+FIELD code,           code,       unsigned short,   ID code (star, ghost, etc)
+FIELD offset,         offset,     int,     	    offset to first measurement
+FIELD missing,        missing,    int,     	    offset to first missing obs
+FIELD dM,             MAG_ERR,    short,            error on primary mag,         [1000*log(value)] ?
+FIELD Xg,             Xg,         short,            best chisq value
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/common.h
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/common.h	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/common.h	(revision 4781)
@@ -0,0 +1,50 @@
+# include <ohana.h>
+
+# ifndef AUTOCODE_H
+# define AUTOCODE_H
+/* matched endif is added by Makefile */
+
+# ifndef TRUE
+# define TRUE (1)
+# define FALSE (0)
+# endif
+
+# 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 e_time unsigned int
+# define rawshort short
+
+/*** rawshort is used to handle the broken pre-autocode photreg tables
+     fix the tables and remove this
+***/
+
+/*** 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 
+***/
+
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/coords.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/coords.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/coords.d	(revision 4781)
@@ -0,0 +1,19 @@
+STRUCT Coords
+EXTNAME COORDS
+TYPE BINTABLE
+SIZE 120
+
+# elements of the Coords structure
+FIELD  crval1,           CRVAL1,               double,   	 coordinate at reference pixel
+FIELD  crval2,           CRVAL2,               double,  	 coordinate at reference pixel
+FIELD  crpix1,           CRPIX1,               float,   	 coordinate of reference pixel
+FIELD  crpix2,           CRPIX2,               float,   	 coordinate of reference pixel
+FIELD  cdelt1,           CDELT1,               float,   	 degrees per pixel
+FIELD  cdelt2,           CDELT2,               float,    	 degrees per pixel
+FIELD  pc1_1,            PC1_1,                float,    	 rotation matrix
+FIELD  pc1_2,            PC1_2,                float,    	 rotation matrix
+FIELD  pc2_1,            PC2_1,                float,    	 rotation matrix
+FIELD  pc2_2,            PC2_2,                float,    	 rotation matrix
+FIELD  polyterms,        POLYTERMS,            float[7][2],	 higher order warping terms
+FIELD  ctype,            CTYPE,                char[15],      coordinate type
+FIELD  Npolyterms,       NPOLYTERMS,           char,     	 order of polynomial
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/detreg.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/detreg.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/detreg.d	(revision 4781)
@@ -0,0 +1,21 @@
+STRUCT  DetReg
+EXTNAME DETREND_DATABASE
+TYPE    BINTABLE
+SIZE    416
+
+# elements of data structure / FITS table
+
+FIELD tstart,         START_TIME, e_time,    start time of measurement, seconds since 1970 Jan 01 UT
+FIELD tstop,          STOP_TIME,  e_time,    stop time of measurement,  seconds since Jan 1, 1970 UT
+FIELD treg,           REG_TIME,   e_time,    time of registration,      seconds since Jan 1, 1970 UT
+FIELD exptime,        EXPTIME,    float,     exposure time,             seconds
+FIELD type,           IMAGETYP,   int,       detrend type number
+FIELD filter,         FILTER,     int,       filter number
+FIELD ccd,            CCDNUM,     int,       ccd number
+FIELD Nentry,         VERSION,    int,       image version number
+FIELD Norder,         ORDER,      int,       selection order
+FIELD mode,           MODE,       char,      image mode
+FIELD altpath,        ALTPATH,    char,      available on alt db paths, true for data on alt db paths
+FIELD dummy,          RESERVED,   char[58],  space for additions,       for future expansion
+FIELD label,          LABEL,      char[64],  data label
+FIELD filename,       PATH,       char[256], filename in db
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/image.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/image.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/image.d	(revision 4781)
@@ -0,0 +1,59 @@
+STRUCT Image
+EXTNAME DVO_IMAGES
+TYPE BINTABLE
+SIZE 240
+
+# elements of the image structure
+
+SUBSTRUCT coords,           COORDS,               Coords,        astrometric data
+SUBFIELD  crval1,           CRVAL1,               double,   	 coordinate at reference pixel
+SUBFIELD  crval2,           CRVAL2,               double,  	 coordinate at reference pixel
+SUBFIELD  crpix1,           CRPIX1,               float,   	 coordinate of reference pixel
+SUBFIELD  crpix2,           CRPIX2,               float,   	 coordinate of reference pixel
+SUBFIELD  cdelt1,           CDELT1,               float,   	 degrees per pixel
+SUBFIELD  cdelt2,           CDELT2,               float,    	 degrees per pixel
+SUBFIELD  pc1_1,            PC1_1,                float,    	 rotation matrix
+SUBFIELD  pc1_2,            PC1_2,                float,    	 rotation matrix
+SUBFIELD  pc2_1,            PC2_1,                float,    	 rotation matrix
+SUBFIELD  pc2_2,            PC2_2,                float,    	 rotation matrix
+SUBFIELD  polyterms,        POLYTERMS,            float[7][2],	 higher order warping terms
+SUBFIELD  ctype,            CTYPE,                char[15],      coordinate type
+SUBFIELD  Npolyterms,       NPOLYTERMS,           char,     	 order of polynomial
+
+FIELD 	  tzero,            TZERO,                e_time,         readout time (row 0)
+FIELD 	  nstar,            NSTAR,                unsigned int,   number of stars on image
+FIELD 	  secz,             SECZ,                 short,      	  airmass,                   milliairmass
+FIELD 	  NX,               NX,                   short,      	  image width
+FIELD 	  NY,               NY,                   short,      	  image height
+FIELD 	  apmifit,          APMIFIT,              short,      	  aperture correction,       millimag
+FIELD 	  dapmifit,         DAPMIFIT,             short,      	  apmifit error,             millimag
+FIELD 	  source,           SOURCE,               short,      	  identifier for CCD,
+FIELD 	  Mcal,             MCAL,                 short,      	  calibration mag,           millimag
+FIELD 	  dMcal,            DMCAL,                short,      	  error on Mcal,             millimag
+FIELD 	  Xm,               XM,                   short,      	  image chisq,               10*log(value)
+FIELD 	  name,             NAME,                 char[32],       name of original image 
+FIELD 	  detection_limit,  DETECTION_LIMIT,      unsigned char,  detection limit,           10*mag
+FIELD 	  saturation_limit, SATURATION_LIMIT,     unsigned char,  saturation limit,          10*mag
+FIELD 	  cerror,           CERROR,               unsigned char,  astrometric error,         50*arcsec
+FIELD 	  fwhm_x,           FWHM_X,               unsigned char,  PSF x width,               25*arcsec
+FIELD 	  fwhm_y,           FWHM_Y,               unsigned char,  PSF y width,               25*arcsec
+FIELD 	  trate,            TRATE,                unsigned char,  scan rate,                 100 usec/pixel
+FIELD 	  exptime,          EXPTIME,              float,          exposure time,             seconds
+FIELD 	  code,             CODE,                 char,           image quality flag
+FIELD 	  ccdnum,           CCDNUM,               unsigned char,  CCD ID number
+FIELD 	  dummy,            DUMMY,                char[20],       unused
+FIELD 	  order,            ORDER,                short,      	  Mrel 2D polynomical order 
+FIELD 	  Mx,               MX,                   short,      	  Mrel polyterm
+FIELD 	  My,               MY,                   short,      	  Mrel polyterm
+FIELD 	  Mxx,              MXX,                  short,      	  Mrel polyterm
+FIELD 	  Mxy,              MXY,                  short,      	  Mrel polyterm
+FIELD 	  Myy,              MYY,                  short,      	  Mrel polyterm
+FIELD 	  Mxxx,             MXXX,                 short,      	  Mrel polyterm
+FIELD 	  Mxxy,             MXXY,                 short,      	  Mrel polyterm
+FIELD 	  Mxyy,             MXYY,                 short,      	  Mrel polyterm
+FIELD 	  Myyy,             MYYY,                 short,      	  Mrel polyterm
+FIELD 	  Mxxxx,            MXXXX,                short,      	  Mrel polyterm
+FIELD 	  Mxxxy,            MXXXY,                short,      	  Mrel polyterm
+FIELD 	  Mxxyy,            MXXYY,                short,      	  Mrel polyterm
+FIELD 	  Mxyyy,            MXYYY,                short,      	  Mrel polyterm
+FIELD 	  Myyyy,            MYYYY,                short,      	  Mrel polyterm
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/measure-loneos.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/measure-loneos.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/measure-loneos.d	(revision 4781)
@@ -0,0 +1,16 @@
+STRUCT  MeasureLoneos
+EXTNAME DVO_MEASURE_LONEOS
+TYPE    BINTABLE
+SIZE    20
+
+# elements of data structure / FITS table
+
+FIELD dR,             D_RA,       short,          RA offset,                	  1/100 arcsec
+FIELD dD,             D_DEC,      short,          DEC offset,               	  1/100 arcsec
+FIELD M,              MAG,        short,          catalog mag,       	       	  millimag
+FIELD Mcal,           Mcal,       short,          image cal mag,	          millimag
+FIELD dM,             dM,         unsigned char,  mag error,                      millimag
+FIELD dophot,         dophot,     char,           dophot type
+FIELD source,         source,     unsigned short, photcode
+FIELD t,              t,          unsigned int,   time in seconds (UNIX)
+FIELD averef,         averef,     unsigned int,   reference to average entry      
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/measure.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/measure.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/measure.d	(revision 4781)
@@ -0,0 +1,23 @@
+STRUCT  Measure
+EXTNAME DVO_MEASURE
+TYPE    BINTABLE
+SIZE    32
+
+# elements of data structure / FITS table
+
+FIELD dR,             D_RA,       short,          RA offset,                	  1/100 arcsec
+FIELD dD,             D_DEC,      short,          DEC offset,               	  1/100 arcsec
+FIELD M,              MAG,        short,          catalog mag,       	       	  millimag
+FIELD Mcal,           Mcal,       short,          image cal mag,	          millimag
+FIELD Mgal,           Mgal,       short,          galaxy mag,			  millimag
+FIELD airmass,        airmass,    short,          (airmass - 1),		  milliairmass
+FIELD FWx,            FWx,        short,          object fwhm major axis,         1/100 of arcsec 
+FIELD dM,             dM,         unsigned char,  mag error,                      millimag
+FIELD fwy,            fwy,        unsigned char,  object fwhm minor/major ratio
+FIELD theta,          theta,      unsigned char,  angle wrt ccd X dir,            (0xff/360) deg
+FIELD dophot,         dophot,     char,           dophot type
+FIELD source,         source,     unsigned short, photcode
+FIELD t,              t,          unsigned int,   time in seconds (UNIX)
+FIELD averef,         averef,     unsigned int,   reference to average entry      
+FIELD dt,             dt,         short,          exposure time,                  2500*log(exptime)
+FIELD flags,          flags,      unsigned short, flags for various uses  
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/missing.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/missing.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/missing.d	(revision 4781)
@@ -0,0 +1,7 @@
+STRUCT  Missing
+EXTNAME DVO_MISSING
+TYPE    BINTABLE
+SIZE    4
+
+# elements of data structure / FITS table
+FIELD t,               TIME,        unsigned int,    time in seconds
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/photcode.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/photcode.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/photcode.d	(revision 4781)
@@ -0,0 +1,18 @@
+STRUCT  PhotCode
+EXTNAME DVO_PHOTCODE
+TYPE    BINTABLE
+SIZE    77
+
+# elements of data structure / FITS table
+FIELD  code,        CODE,        unsigned short, code number (stored in Measure.source) 
+FIELD  name,        NAME,        char[32],	 name for filter combination 
+FIELD  type,        TYPE,        char,	      	 PRI/SEC/DEP/REF 
+FIELD  C,           C_LAM,       short,	      	 primary phot calibration terms (millimags) 
+FIELD  dC,          C_LAM_ERR,   short,	      	 primary phot calibration terms (millimags) 
+FIELD  dX,          X_ERR,       short,	      	 primary phot calibration terms (millimags) 
+FIELD  K,           K,           float,	      	 secondary phot calibration terms (millimags) 
+FIELD  c1,          C1,          int,	      	 color is average.M[c1] - average.M[c2] 
+FIELD  c2,          C2,          int,	      	 color is average.M[c1] - average.M[c2] 
+FIELD  equiv,       EQUIV,       int,	      	 this dependent filter is equivalent to equiv PRI/SEC
+FIELD  Nc,          NC,          int,	      	 number of color terms 
+FIELD  X,           X,           float[4],    	 color terms X[0]*mc + X[1]*mc^2 + X[2]*mc^3, etc 
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/photreg-old.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/photreg-old.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/photreg-old.d	(revision 4781)
@@ -0,0 +1,18 @@
+STRUCT PhotParsOld
+EXTNAME ZERO_POINTS
+TYPE BINTABLE
+SIZE 100
+
+# elements of data structure / FITS table
+
+FIELD ZP,    	  ZP_OBS,     float,       measured zero point,       mag
+FIELD ZPo,   	  ZP_REF,     float, 	   nominal zero point,        mag
+FIELD dZP,   	  ZP_ERR,     float, 	   error on zero point,       mag
+FIELD K,     	  C_AIRMASS,  float, 	   airmass coeff,             mag per airmass
+FIELD X,     	  C_COLOR,    float, 	   color coeff,               mag per mag
+FIELD tstart,	  START_TIME, e_time,      start time of measurement, seconds since 1 Jan 1970 UT
+FIELD tstop, 	  STOP_TIME,  e_time,      stop time of measurement,  seconds since 1 Jan 1970 UT
+FIELD c1,    	  C1_CODE,    short, 	   code 1 for color,          photcode
+FIELD c2,    	  C2_CODE,    short, 	   code 2 for color,          photcode
+FIELD photcode,   PHOTCODE,   short, 	   photcode,                  photcode
+FIELD label,      LABEL,      char[66],    data label
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/photreg.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/photreg.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/photreg.d	(revision 4781)
@@ -0,0 +1,21 @@
+STRUCT PhotPars
+EXTNAME ZERO_POINTS_3.0
+TYPE BINTABLE
+SIZE 108
+
+# elements of data structure / FITS table
+
+FIELD ZP,    	  ZP_OBS,     float,       measured zero point,       mag
+FIELD ZPo,   	  ZP_REF,     float, 	   nominal zero point,        mag
+FIELD dZP,   	  ZP_ERR,     float, 	   error on zero point,       mag
+FIELD K,     	  C_AIRMASS,  float, 	   airmass coeff,             mag per airmass
+FIELD X,     	  C_COLOR,    float, 	   color coeff,               mag per mag
+FIELD tstart,	  START_TIME, e_time,      start time of measurement, seconds since 1 Jan 1970 UT
+FIELD tstop, 	  STOP_TIME,  e_time,      stop time of measurement,  seconds since 1 Jan 1970 UT
+FIELD c1,    	  C1_CODE,    short, 	   code 1 for color,          photcode
+FIELD c2,    	  C2_CODE,    short, 	   code 2 for color,          photcode
+FIELD photcode,   PHOTCODE,   short, 	   photcode,                  photcode
+FIELD label,      LABEL,      char[64],    data label
+FIELD refcode,    REFCODE,    rawshort,	   photcode,                  photcode
+FIELD Ntime,      N_TIME,     int, 	   number of times
+FIELD Nmeas,      N_MEAS,     int, 	   number of measurements
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/regimage.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/regimage.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/regimage.d	(revision 4781)
@@ -0,0 +1,40 @@
+STRUCT  RegImage
+EXTNAME IMAGE_DATABASE
+TYPE    BINTABLE
+SIZE    360
+
+# elements of data structure / FITS table
+
+FIELD filename,         FILE,       char[64],     filename in db
+FIELD pathname,     	PATH,       char[128],    fullpath in db
+FIELD filter,       	FILTER,     char[32],     filter name
+FIELD instrument,   	INSTRUMENT, char[32],     instrument
+FIELD ccd,	        CCD,        char,   	  ccd identifier
+FIELD mode,	        MODE,       char,   	  mef/split/etc
+FIELD type,	        TYPE,       char,   	  object/flat/bias/etc
+FIELD flag,	        FLAG,       char,   	  data flags
+FIELD seqtime,          SEQTIME,    float,        exposure time per slice,   seconds
+FIELD seq,	        SEQ,        char,   	  sequence number
+FIELD junk,	        JUNK,       char[19],     space for expansion
+FIELD exptime,	        EXPTIME,    float,  	  exposure time,             seconds
+FIELD airmass,	        AIRMASS,    float,  	  airmass
+FIELD sky,	        SKY,        float,  	  background level,          counts / pixel
+FIELD bias,	        BIAS,       float,  	  bias level,                counts / pixel
+FIELD fwhm,	        FWHM,       float,  	  image quality,             pixels
+FIELD telfocus,	        TELFOCUS,   float,  	  telescope focus,           microns
+FIELD xprobe,	        XPROBE,     float,  	  bonnette probe x pos,      microns
+FIELD yprobe,	        YPROBE,     float,  	  bonnette probe y pos,      microns
+FIELD zprobe,	        ZPROBE,     float,  	  bonnette focus,            microns
+FIELD dettemp,	        DETTEMP,    float,  	  detector temperature,      deg celcius
+FIELD teltemp_0,       	TELTEMP0,   float,  	  other temperature,         deg celcius
+FIELD teltemp_1,       	TELTEMP1,   float,  	  other temperature,         deg celcius
+FIELD teltemp_2,       	TELTEMP2,   float,  	  other temperature,         deg celcius
+FIELD teltemp_3,       	TELTEMP3,   float,  	  other temperature,         deg celcius
+FIELD rotangle,	        ROTANGLE,   float,  	  camera rotation angle,     degrees
+FIELD ra,	        RA,         float,  	  image ra,                  degrees
+FIELD dec,	        DEC,        float,  	  image dec,                 degrees
+FIELD obstime,	        OBS_TIME,   e_time, 	  time of measurement,       seconds since 01 Jan 1970 UT
+FIELD regtime,	        REG_TIME,   e_time, 	  time of registration,      seconds since 01 Jan 1970 UT
+
+# take care of the memory padding boundaries when using 'junk' for new elements
+# which are not of type char!
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/secfilt.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/secfilt.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/secfilt.d	(revision 4781)
@@ -0,0 +1,9 @@
+STRUCT  SecFilt
+EXTNAME DVO_SECFILT
+TYPE    BINTABLE
+SIZE    6
+
+# elements of data structure / FITS table
+FIELD  M,  MAG,      short,                other mags,       millimags
+FIELD  Xm, MAG_CHI,  short,                chisq on mag
+FIELD  dM, MAG_ERR,  short,                scatter on mag
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/smpdata.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/smpdata.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/smpdata.d	(revision 4781)
@@ -0,0 +1,19 @@
+# name of structure type
+STRUCT  SMPData
+EXTNAME SMPFILE
+TYPE    BINTABLE
+SIZE    44
+
+# elements of data structure / FITS table
+FIELD X,      X_PIX,      float,    x coord,              pixels
+FIELD Y,      Y_PIX,      float,    y coord,              pixels
+FIELD M,      MAG_RAW,    float,    inst mags,            mags
+FIELD dM,     MAG_ERR,    float,    inst mag error,       mags
+FIELD Mgal,   MAG_GAL,    float,    galaxy mag,           mags
+FIELD Map,    MAG_AP,     float,    aperture mag,         mags
+FIELD sky,    LOG_SKY,    float,    log-10 of sky,        cnts/sec
+FIELD fx,     FWHM_X,     float,    semi-major,           pixels
+FIELD fy,     FWHM_Y,     float,    semi-minor,           pixels
+FIELD df,     THETA,      float,    ellipse angle,        degrees
+FIELD dophot, DOPHOT,     char,     dophot type,          none
+FIELD dummy,  DUMMY,      char[3],  padding,              none
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/spectrum-ascii.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/spectrum-ascii.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/spectrum-ascii.d	(revision 4781)
@@ -0,0 +1,29 @@
+# name of structure type
+STRUCT  SpectrumASCII
+EXTNAME SPECTRUM
+TYPE    TABLE
+SIZE    262
+
+# elements of data structure / FITS table
+FIELD filename,   FILENAME,   char[32],    filename in db,        
+FIELD pathname,   PATHNAME,   char[64],    fullpath in db,        
+FIELD instrument, INSTRUMENT, char[16],    instrument,            
+FIELD telescope,  TELESCOPE,  char[16],    telescope,             
+FIELD objname,    OBJNAME,    char[16],    object name,           
+FIELD extname,    EXTNAME,    char[16],    extname in file,       
+
+FIELD ra,         RA,         float[10.6], ra,                    degrees
+FIELD dec,        DEC,        float[10.6], dec,                   degrees
+FIELD exptime,    EXPTIME,    float[6.1],  exposure time,         seconds
+FIELD airmass,    AIRMASS,    float[5.3],  airmass,               none
+FIELD Ws,         Ws,         float[7.2],  spectral range start,  Angstrom
+FIELD We,         We,         float[7.2],  spectral range end,    Angstrom
+FIELD dW,         dW,         float[7.2],  spectral resolution,   Angstrom / pix
+	       			      
+FIELD Nspec,      NSPECTRA,   int[3],      number of spectra,     none
+FIELD obstime,    OBS_TIME,   char[20],    time of measurement,   yyyy/mm/dd
+FIELD regtime,    REG_TIME,   char[20],    time of registration,  yyyy/mm/dd
+	       			      
+FIELD mode,       MODE,       int[2],      phu/mef/ext,           
+FIELD state,      STATE,      int[2],      raw/wav/flx/etc,       
+FIELD flag,       FLAG,       int[3],      status flags,          
Index: /tags/libautocode-1-1/Ohana/src/libautocode/def/spectrum.d
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/def/spectrum.d	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/def/spectrum.d	(revision 4781)
@@ -0,0 +1,31 @@
+STRUCT  Spectrum
+EXTNAME SPECTRUM_DATABASE
+TYPE    BINTABLE
+SIZE    216
+
+# elements of data structure / FITS table
+	       			      
+FIELD ra,         RA,         float,     ra,                    degrees
+FIELD dec,        DEC,        float,     dec,                   degrees
+FIELD exptime,    EXPTIME,    float,     exposure time,         seconds
+FIELD airmass,    AIRMASS,    float,     airmass,               
+FIELD Ws,         Ws,         float,     spectral range start,  Angstrom
+FIELD We,         We,         float,     spectral range end,    Angstrom
+FIELD dW,         dW,         float,     spectral resolution,   Angstrom / pix
+	       			      
+FIELD Nspec,      NSPECTRA,   int,       number of spectra,     
+FIELD obstime,    OBS_TIME,   int,       time of measurement,   seconds since 1 Jan 1970 UT
+FIELD regtime,    REG_TIME,   int,       time of registration,  seconds since 1 Jan 1970 UT
+	       			      
+FIELD mode,       MODE,       char,      phu/mef/ext           
+FIELD state,      STATE,      char,      raw/wav/flx/etc       
+FIELD flag,       FLAG,       char,      status flags
+FIELD extra,      EXTRA,      char[13],  room for expansion
+
+FIELD pathname,   PATHNAME,   char[64],  fullpath in db
+FIELD filename,   FILENAME,   char[32],  filename in db
+FIELD extname,    EXTNAME,    char[16],  extname in file,       
+
+FIELD instrument, INSTRUMENT, char[16],  instrument,            
+FIELD telescope,  TELESCOPE,  char[16],  telescope,             
+FIELD objname,    OBJNAME,    char[16],  object name,           
Index: /tags/libautocode-1-1/Ohana/src/libautocode/doc/ChangeLog.txt
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/doc/ChangeLog.txt	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/doc/ChangeLog.txt	(revision 4781)
@@ -0,0 +1,6 @@
+
+libautocode-1-1
+  added the measure, average entries to autocode tables
+
+libautocode-1-0
+  import to CVS
Index: /tags/libautocode-1-1/Ohana/src/libautocode/doc/autocode.txt
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/doc/autocode.txt	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/doc/autocode.txt	(revision 4781)
@@ -0,0 +1,71 @@
+
+This directory contains a collection of autocoded FITS I/O routines
+used to define FITS Table DB functions.  
+
+The autocoder program is a perl script 'generate'.  This scripts takes
+as input a schema file and a template source code file, and produces
+an output file from these two inputs.  A single schema file is meant
+to be associated with a single data concept which is being coded.  For
+example, in the case of the FITS DB tables, a single schema file
+defines a single FITS table, or equivalently, a single C structure
+representing a row in that FITS table.
+
+Schema File
+
+The schema file currently consists of two types of information.
+
+First, there are simple keyword / name pairs which the autocoder
+simply applies as a direct replacement anywhere in the template.  For
+example, the keyword NAME replaces any instance of "$NAME" in the
+template with the corresponding value.  
+
+Second, there is a special type of keyword: FIELD.  The FIELD entry
+defines an entry in the data structure.  The FIELD entry is followed
+by the following comma-separated pieces of information:
+
+- the element name
+- the element data type
+- the element description
+- the element physical unit
+
+Template File
+
+The template file is a source code in whatever language is desired.
+Within the template, the defined keywords may be used as desired,
+wherever needed.  In addition, there are special directives which
+invoke additional special behavior.  All special directives have the
+form of comments within their target language.  They are replaced with
+a block of code in that target language, normally one which iterates
+over the FIELD elements of the schema file.  As such, the directive
+appropriate for one language should not be used within code for a
+different language.  Here is a list of existing special directives:
+
+/** STRUCT DEFINITION **/
+
+This tells the autocoder to create a structure definition for the
+schema.  The structure definition creates a structure with elements
+based on the FIELD entries, with the name $NAME (NOTE: this is perhaps
+making too much of an assumption.  we could require the template to
+provide the wrapper: "typedef struct { } $NAME;" and have the
+autocoder only insert the field lines).  
+
+/** TABLE DEFINITION **/
+
+This tells the autocoder to output the lines which add a column to an
+exiting empty table.  The assumption is that the template contains
+code to initialize a table.  It then invokes the table definition
+code, which defines each of the appropriate columns.  This is perhaps
+followed by some code which finalizes the table definition.  (Note:
+the code output by the autocoder in this block is not very flexible:
+it the current example, it explicitly uses the Elixir FITS Table
+functions, and refers to some data elements in the template code.  The
+latter could possibly be more abstracted with the keyword / value
+pairs, but in any case, the autocoded lines will have to be
+constructed somewhat by hand for a specific API set.
+
+/** BYTE SWAP **/
+
+This tells the autocoder to output code defining the byte swaps
+appropriat to the data structure.  Again, the resulting code depends a
+bit on the intended use and API set.
+
Index: /tags/libautocode-1-1/Ohana/src/libautocode/doc/notes.txt
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/doc/notes.txt	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/doc/notes.txt	(revision 4781)
@@ -0,0 +1,113 @@
+
+FITS I/O APIs
+
+  - I/O to a binary FITS table
+  - one extension per file?
+  - no locking
+  - always read/write to/from matched structure
+  - user needs to supply idiosyncratic conversions
+
+  - basic APIs:
+    - fits_read_datatable_NAME
+    - fits_write_datatable_NAME
+    - fits_IO_convert_NAME
+    - fits_fread_datatable_NAME
+    - fits_fwrite_datatable_NAME
+
+  
+
+FITS db APIs
+
+Basic Rules:
+
+  - All db files are Binary FITS tables
+  - There is only one extension per file
+  - The PHU contains basic information like status & date
+  - There is no data array for the PHU (NAXIS = 0)
+
+typedef struct {
+  FILE *f;
+  char *filename;
+  Header header;
+  FTable ftable;
+  int Nrows;
+  int Ncols;
+  int Nbyte;
+  int lockstate;
+  int lockreq;
+  NAME *data;
+} NAME_DB;
+
+NAME_DB *NAME_DB_Init (NAME_DB *db);
+
+  - 
+
+NAME_DB *NAME_DB_Load (char *filename, int lockreq);
+
+  - lock the database file
+  - load PHU
+  - load the header
+  - validate the EXTNAME
+  - validate the header
+  - load the data into ftable
+  - convert to memory format
+
+  * if SOFT & database file does not exist (file is empty), 
+    - return structure w/ 0 entries
+
+  * if HARD & database file does not exist (file is empty), 
+    - open, lock, return structure w/ 0 entries
+
+  * if database file cannot be locked
+    - return an error (NULL)
+
+  * if header does not validate: 
+    - unlock file
+    - return an error (NULL)
+
+NAME_DB *NAME_DB_Loadset (char *filename, int lockreq, int start, int Nrows);
+
+  - lock the database file
+  - load PHU
+  - load the header
+  - validate the EXTNAME
+  - validate the header
+  - load the data into vtable
+  - convert to memory format
+
+  * if database file does not exist (file is empty), 
+    - open, lock, return structure w/ 0 entries
+  * if requested entries don't exist
+    - unlock file
+    - return an error (NULL)
+
+NAME_DB *NAME_DB_Save ();
+
+  - if Nrows = 0, delete the file
+
+  - write PHU
+  - write header
+  - write ftable
+
+  - if vtable: 
+    - update 
+  - if ftable: 
+    - update 
+
+NAME_DB *NAME_DB_Saveset ();
+
+  - if Nrows = 0, delete the file
+
+  - write PHU
+  - write header
+  - write ftable
+
+  - if vtable: 
+    - update 
+  - if ftable: 
+    - update 
+
+NAME_DB *NAME_DB_Free ();
+
+  - unlock file
+
Index: /tags/libautocode-1-1/Ohana/src/libautocode/generate
===================================================================
--- /tags/libautocode-1-1/Ohana/src/libautocode/generate	(revision 4781)
+++ /tags/libautocode-1-1/Ohana/src/libautocode/generate	(revision 4781)
@@ -0,0 +1,341 @@
+#!/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);
+	
+	&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;
+	}
+    }
+    if ($TYPE eq "BINTABLE") { $Nexpect = &count_bintablesize; }
+    if ($TYPE eq "TABLE")    { $Nexpect = &count_tablesize;    }
+    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 
+
