Index: /trunk/Ohana/src/libautocode/Makefile
===================================================================
--- /trunk/Ohana/src/libautocode/Makefile	(revision 4022)
+++ /trunk/Ohana/src/libautocode/Makefile	(revision 4023)
@@ -21,12 +21,12 @@
 LFLAGS	=	$(LIBS)
 
-install: $(DESTINC)/autocode.h $(DESTLIB)/libautocode.a
-	@echo done install
-
-autocode: $(INC)/autocode.h $(LIB)/libautocode.$(ARCH).a 
-	@echo done autocode
-
 # 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 \
@@ -39,4 +39,10 @@
 
 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 \
@@ -47,4 +53,12 @@
 $(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
@@ -89,8 +103,4 @@
 	@echo done with $*.o
 
-%.$(ARCH).o: %.c
-	$(CC) $(CFLAGS) -c $*.c -o $@
-	@echo done with $*.o
-
 clean:	
 	rm -f `find . -name "*.o"`
@@ -105,2 +115,3 @@
 .PRECIOUS: %.$(ARCH).o
 .PRECIOUS: $(BIN)/%.$(ARCH)
+.PRECIOUS: $(LIB)/%.$(ARCH).a
Index: /trunk/Ohana/src/libautocode/def/average.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/average.d	(revision 4023)
+++ /trunk/Ohana/src/libautocode/def/average.d	(revision 4023)
@@ -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: /trunk/Ohana/src/libautocode/def/coords.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/coords.d	(revision 4023)
+++ /trunk/Ohana/src/libautocode/def/coords.d	(revision 4023)
@@ -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: /trunk/Ohana/src/libautocode/def/image.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/image.d	(revision 4022)
+++ /trunk/Ohana/src/libautocode/def/image.d	(revision 4023)
@@ -17,19 +17,19 @@
 SUBFIELD  pc2_1,            PC2_1,                float,    	 rotation matrix
 SUBFIELD  pc2_2,            PC2_2,                float,    	 rotation matrix
-SUBFIELD  polyterms[7][2],  POLYTERMS[7][2],      float,    	 higher order warping terms
-SUBFIELD  ctype[15],        CTYPE[15],            char,     	 coordinate type
+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 int,      airmass,                   milliairmass
-FIELD 	  NX,               NX,                   short int,      image width
-FIELD 	  NY,               NY,                   short int,      image height
-FIELD 	  apmifit,          APMIFIT,              short int,      aperture correction,       millimag
-FIELD 	  dapmifit,         DAPMIFIT,             short int,      apmifit error,             millimag
-FIELD 	  source,           SOURCE,               short int,      identifier for CCD,
-FIELD 	  Mcal,             MCAL,                 short int,      calibration mag,           millimag
-FIELD 	  dMcal,            DMCAL,                short int,      error on Mcal,             millimag
-FIELD 	  Xm,               XM,                   short int,      image chisq,               10*log(value)
+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
@@ -43,21 +43,17 @@
 FIELD 	  ccdnum,           CCDNUM,               unsigned char,  CCD ID number
 FIELD 	  dummy,            DUMMY,                char[20],       unused
-FIELD 	  order,            ORDER,                short int,      Mrel 2D polynomical order 
-FIELD 	  Mx,               MX,                   short int,      Mrel polyterm
-FIELD 	  My,               MY,                   short int,      Mrel polyterm
-FIELD 	  Mxx,              MXX,                  short int,      Mrel polyterm
-FIELD 	  Mxy,              MXY,                  short int,      Mrel polyterm
-FIELD 	  Myy,              MYY,                  short int,      Mrel polyterm
-FIELD 	  Mxxx,             MXXX,                 short int,      Mrel polyterm
-FIELD 	  Mxxy,             MXXY,                 short int,      Mrel polyterm
-FIELD 	  Mxyy,             MXYY,                 short int,      Mrel polyterm
-FIELD 	  Myyy,             MYYY,                 short int,      Mrel polyterm
-FIELD 	  Mxxxx,            MXXXX,                short int,      Mrel polyterm
-FIELD 	  Mxxxy,            MXXXY,                short int,      Mrel polyterm
-FIELD 	  Mxxyy,            MXXYY,                short int,      Mrel polyterm
-FIELD 	  Mxyyy,            MXYYY,                short int,      Mrel polyterm
-FIELD 	  Myyyy,            MYYYY,                short int,      Mrel polyterm
-
-
-
-
+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: /trunk/Ohana/src/libautocode/def/measure.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/measure.d	(revision 4023)
+++ /trunk/Ohana/src/libautocode/def/measure.d	(revision 4023)
@@ -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: /trunk/Ohana/src/libautocode/def/missing.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/missing.d	(revision 4023)
+++ /trunk/Ohana/src/libautocode/def/missing.d	(revision 4023)
@@ -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: /trunk/Ohana/src/libautocode/def/photcode.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/photcode.d	(revision 4023)
+++ /trunk/Ohana/src/libautocode/def/photcode.d	(revision 4023)
@@ -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: /trunk/Ohana/src/libautocode/def/secfilt.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/secfilt.d	(revision 4023)
+++ /trunk/Ohana/src/libautocode/def/secfilt.d	(revision 4023)
@@ -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: /trunk/Ohana/src/libautocode/def/spectrum-ascii.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/spectrum-ascii.d	(revision 4022)
+++ /trunk/Ohana/src/libautocode/def/spectrum-ascii.d	(revision 4023)
@@ -3,5 +3,5 @@
 EXTNAME SPECTRUM
 TYPE    TABLE
-SIZE    328
+SIZE    262
 
 # elements of data structure / FITS table
@@ -28,12 +28,2 @@
 FIELD state,      STATE,      int[2],      raw/wav/flx/etc,       
 FIELD flag,       FLAG,       int[3],      status flags,          
-
-FIELD obstime,    START_TIME, char[20],    start time of measurement, yyyy/mm/dd,hh:mm:ss
-FIELD filter,  	  FILTER,     char[10],    filter and camera name    
-FIELD ZP,      	  ZP_OBS,     float[8.4],  measured zero point,       mag
-FIELD dZP,     	  ZP_ERR,     float[7.4],  error on zero point,       mag
-FIELD ra,      	  RA,         float[11.6], RA (J2000),                dec. degrees
-FIELD dec,     	  DEC,        float[11.6], DEC (J2000),               dec. degrees
-FIELD airmass, 	  C_AIRMASS,  float[7.3],  airmass coeff,             mag per airmass 
-FIELD sky,     	  SKY,        float[7.1],  median sky flux,           counts
-FIELD Nstar,   	  NSTAR,      int[6],      Number of stars in image,  stars
Index: /trunk/Ohana/src/libautocode/generate
===================================================================
--- /trunk/Ohana/src/libautocode/generate	(revision 4022)
+++ /trunk/Ohana/src/libautocode/generate	(revision 4023)
@@ -38,5 +38,6 @@
 
 	# there are used internally (not just a replacement)
-	if ($key eq "TYPE") { $TYPE = $value; }
+	if ($key eq "TYPE")   { $TYPE = $value;   }
+	if ($key eq "SIZE")   { $SIZE = $value;   }
 	if ($key eq "STRUCT") { $STRUCT = $value; }
 
@@ -53,6 +54,7 @@
 	}
     }
-    if ($TYPE eq "BINTABLE") { &count_bintablesize; }
-    if ($TYPE eq "TABLE") { &count_tablesize; }
+    if ($TYPE eq "BINTABLE") { $Nexpect = &count_bintablesize; }
+    if ($TYPE eq "TABLE")    { $Nexpect = &count_tablesize;    }
+    if ($Nexpect != $SIZE) { die "size mismatch: $Nexpect vs $SIZE\n"; }
 }
 
@@ -95,33 +97,63 @@
 	if ($mode[$i] eq "SUBSTRUCT") { next; }
 
-	# need to handle all structures like: float poly[5][4]
-	# add [\d\.] to match ascii-type formats
-	($type, $length) = $format[$i] =~ m|^(\w+)\[(\d+)\]|;
-	if (!$type && !$length) { $type = $format[$i]; }
-
-	if ($type eq "char") {
-	    if ($length == 0) { 
-		$pt1 = sprintf "A";
-	    } else {
-		$pt1 = sprintf "%dA", $length;
-	    }
-	}
-
-	if ($type eq "byte")   { $pt1 = "B"; }
-	if ($type eq "short")  { $pt1 = "I"; }
-	if ($type eq "int")    { $pt1 = "J"; }
-	if ($type eq "e_time") { $pt1 = "J"; }
-	if ($type eq "float")  { $pt1 = "E"; }
-	if ($type eq "double") { $pt1 = "D"; }
-
-	if ($type eq "rawshort") { $pt1 = "I"; }
+	($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 "\"%s\",   ", $pt1;
-	printf FILE "\"%s\",   ", $field[$i];
-	printf FILE "\"%s\", ",   $comment[$i];
-	printf FILE "\"%s\", 1.0, 0.0);\n", $unit[$i];
-    }
-
+	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);
 }
 
@@ -131,23 +163,30 @@
 	# 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"; }
 
-	if ($type eq "char")   { $pt1 = sprintf "A%s", $length; }
-	if ($type eq "byte")   { $pt1 = sprintf "I%s", $length; }
-	if ($type eq "short")  { $pt1 = sprintf "I%s", $length; }
-	if ($type eq "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 ($type eq "rawshort")  { $pt1 = sprintf "I%s", $length; }
+	$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 "\"%s\", ",   $pt1;
-	printf FILE "\"%s\", ",   $field[$i];
-	printf FILE "\"%s\", ",   $comment[$i];
-	printf FILE "\"%s\");\n", $unit[$i];
+	printf FILE "%-8s",      "\"$pt1\", ";
+	printf FILE "%-20s",     "\"$field[$i]\", ";
+	printf FILE "%-35s",     "\"$comment[$i]\", ";
+	printf FILE "%-20s);\n", "\"$unit[$i]\"";
     }
 
@@ -160,9 +199,11 @@
 	if ($mode[$i] eq "SUBFIELD") { next; }
 
-	($type, $length) = $format[$i] =~ m|^(\w+)\[([\d\.]+)\]|;
-	if (!$type && !$length) { $type = $format[$i]; }
-
-	if (($type eq "char") && ($length != 0)) { 
-	    $pt2 = sprintf "%s[%d];", $element[$i], $length;
+	# 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];
@@ -179,5 +220,11 @@
 }
 
+# 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++) {
@@ -185,47 +232,26 @@
 	if ($mode[$i] eq "SUBSTRUCT") { next; }
 
-	($type, $length) = $format[$i] =~ m|^(\w+)\[([\d\.]+)\]|;
-	if (!$type && !$length) { $type = $format[$i]; }
-
-	if ($type eq "char") {
-	    if ($length == 0) { 
-		$N ++;
-	    } else {
-		$N += $length;
-	    }
-	    next;   # skip byte swap command
-	}
-	if ($type eq "byte")  {
-	    $N ++;
-	    next;
-	}
-	# this is a patch for old photreg tables: provide a fix for the tables
+	($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
-	if ($type eq "rawshort")  {
-	    $N +=2;
-	    next;
-	}
-	if ($type eq "short")  {
-	    $T = "BYTE";
-	    $n = 2;
-	}
-	if ($type eq "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;
-	}
-	printf FILE "    SWAP_%s (%d); // %s\n", $T, $N, $field[$i];
-	$N += $n;
+
+	$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;
+	}
     }
 }
@@ -239,25 +265,19 @@
 
 	# add [\d\.] to match ascii-type formats
-	($type, $length) = $format[$i] =~ m|^(\w+)\[(\d+)\]|;
-	if (!$type && !$length) { 
-	    $type = $format[$i]; 
-	    $length = 0;
-	}
-
-	if ($type eq "char") {
-	    if ($length) {
-		$Nbytes += $length;
-	    } else {
-		$Nbytes ++;
-	    }
-	}
-	if ($type eq "byte")   { $Nbytes += 1; }
-	if ($type eq "short")  { $Nbytes += 2; }
-	if ($type eq "int")    { $Nbytes += 4; }
-	if ($type eq "e_time") { $Nbytes += 4; }
-	if ($type eq "float")  { $Nbytes += 4; }
-	if ($type eq "double") { $Nbytes += 8; }
-
-	if ($type eq "rawshort")  { $Nbytes += 2; }
+	($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);
@@ -271,5 +291,7 @@
 	if ($mode[$i] eq "SUBSTRUCT") { next; }
 
-	($length) = $format[$i] =~ m|^\w+\[([\d\.]+)\]|;
+	($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;
@@ -302,6 +324,6 @@
     my ($i);
     for ($i = 0; $i < @key; $i++) {
+	# if ($VERBOSE) { print "$key[$i]  -- $value[$i]\n"; }
 	if ($line =~ m|\$$key[$i]|) {
-#	    print "$key[$i]  -- $value[$i]\n";
 	    if ($value[$i] eq "") { die "missing value for required key $key[$i]\n"; }
 	    $line =~ s|\$$key[$i]|$value[$i]|g;
