Index: /tags/addspphot-1-0/Ohana/src/addspphot/Makefile
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/Makefile	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/Makefile	(revision 2549)
@@ -0,0 +1,63 @@
+include ../../Configure
+HOME 	=	$(ROOT)/src/addspphot
+PROGRAM =       addspphot
+
+BIN	=	$(HOME)/bin
+INC	= 	$(HOME)/include
+SRC	=	$(HOME)/src
+MAN	=	$(HOME)/doc
+DESTBIN	=	$(LBIN)
+DESTLIB	=	$(LLIB)
+DESTINC	=	$(LINC)
+DESTMAN	=	$(LMAN)
+
+#  
+INCS	= 	-I$(INC) -I$(LINC) -I$(XINC)
+LIBS	= 	-L$(LLIB) -lFITS -lohana -lm 
+CFLAGS	=	-o $*.$(ARCH).o $(INCS)
+CCFLAGS	=	$(INCS) $(LIBS) 
+
+ADDSPPHOT = \
+$(SRC)/addspphot.$(ARCH).o 	$(SRC)/gcatalog.$(ARCH).o   \
+$(SRC)/gregions.$(ARCH).o 	$(SRC)/gimages.$(ARCH).o    \
+$(SRC)/wimage.$(ARCH).o   	$(SRC)/gstars.$(ARCH).o     \
+$(SRC)/find_matches.$(ARCH).o 	$(SRC)/wcatalog.$(ARCH).o   \
+$(SRC)/sort_lists.$(ARCH).o 	$(SRC)/ConfigInit.$(ARCH).o 	\
+$(SRC)/aregion.$(ARCH).o	$(SRC)/load_subpix.$(ARCH).o\
+$(SRC)/check_permissions.$(ARCH).o $(SRC)/calibrate.$(ARCH).o \
+$(SRC)/args.$(ARCH).o		$(SRC)/parse_time.$(ARCH).o \
+$(SRC)/mkcatalog.$(ARCH).o	$(SRC)/airmass.$(ARCH).o
+
+OBJ = $(ADDSPPHOT)
+
+default: $(PROGRAM)
+
+$(ADDSPPHOT): $(INC)/addstar.h
+
+# dependancy rules for binary code ##########################
+$(PROGRAM): $(BIN)/$(PROGRAM).$(ARCH)
+
+$(BIN)/$(PROGRAM).$(ARCH): $(OBJ)
+	@if [ ! -d $(BIN) ]; then mkdir -p $(BIN); fi
+	$(CC) $(OBJ) -o $(BIN)/$(PROGRAM).$(ARCH) $(CCFLAGS)
+
+install: $(DESTBIN)/$(PROGRAM)
+
+$(DESTBIN)/$(PROGRAM): $(BIN)/$(PROGRAM).$(ARCH)
+	@if [ ! -d $(DESTBIN) ]; then mkdir -p $(DESTBIN); fi
+	rm -f $(DESTBIN)/$(PROGRAM)
+	cp $(BIN)/$(PROGRAM).$(ARCH) $(DESTBIN)/$(PROGRAM)
+
+# utilities #################################################
+clean:	
+	rm -f $(BIN)/*.$(ARCH)
+	rm -f `find . -name "*.o"`
+	rm -f `find . -name "*~"`
+	rm -f `find . -name "#*"`
+
+.SUFFIXES: .$(ARCH).o
+
+.c.$(ARCH).o:
+	$(CC) $(CFLAGS) -c $<
+
+
Index: /tags/addspphot-1-0/Ohana/src/addspphot/bin/.cvsignore
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/bin/.cvsignore	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/bin/.cvsignore	(revision 2549)
@@ -0,0 +1,1 @@
+*.linux *.lin64 *.sol *.sun *.sid *.hp *.irix
Index: /tags/addspphot-1-0/Ohana/src/addspphot/doc/database.txt
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/doc/database.txt	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/doc/database.txt	(revision 2549)
@@ -0,0 +1,91 @@
+
+\begin{verbatim}
+
+typedef struct {
+  float R;                    /* RA  in decimal degrees */
+  float D;                    /* DEC in decimal degrees */
+  short int M;                /* thousandths of mag (-32.000 to 32.000 valid range) */
+  unsigned short int Nm;      /* number of measurements */
+  unsigned short int Xp, Xm;  /* chisq values in tenths */
+  unsigned int offset;        /* offset to first Measure-ment */
+} Average;                    /* = 20 bytes / average */
+
+typedef struct {
+  char dR, dD;                /* tenths of arcsec (-12.7 to +12.7 valid range) */
+  short int M;                /* thousandths of mag (-32.000 to 32.000 valid range) */
+  unsigned char dM;           /* thousandths of mag (0.000 -- 0.255 valid range) */
+  float t;                    /* time in seconds (what is reference?) */
+  unsigned int average;       /* reference to corresponding Average entry, 
+				 upper byte of value contains flags.
+				 limit of 16,777,215 stars (Naverage) 
+				 in a file (=0xFFFFFF).
+				 flags = average & 0x1000000 */
+} Measure;                    /* = 13 bytes / measure */
+
+# define BLEND_IMAGE   0X01000000
+# define BLEND_CATALOG 0X02000000
+# define UPPER_LIMIT   0X04000000
+# define CALIBRATED    0X08000000
+
+\end{verbatim}
+
+The above two structures define the entries in the photometry
+database.  The database consists of a large number of files
+representing a small patch on the sky (roughly 1.5 degree$^2$ in most
+places).  These files are organized into directories representing
+bands of Declination.  A reference file determines the coordinate
+boundaries for each of the files so that a given point on the sky can
+unambiguously be associated with a specific file in a specific
+directory.  The sky coordinates for each file is the same as those
+used by the HST Guide Star catalog, except for the region around the
+North celestial pole, for which all stars are included in a single
+file.  
+
+Within a given file, the data are stored in a binary format, with an
+ASCII FITS-like header.  The header is examply in the format of a
+normal FITS header, but with the exception that all files have a fixed
+number of header blocks (for now 3 blocks = 8640 bytes).  This is done
+to speed loading the header and finding the beginning of the binary
+data.  The number of 3 blocks seems quite generous, as currently only
+a few FITS keywords have been defined for each file, basically
+keywords to define the number of stars and the total number of
+measurements stored in the file, as well as values to define the RA
+and DEC range of the file.  
+
+The first section of data following the header blocks consists of
+average measurements for each uniquely observed star.  Each star
+occupies 20 bytes, the size of the Average structure defined above.
+The Average structure contains the average Ra, Dec, and Magnitude for
+the star, as well as the number of measurements, and \chisq\ values
+for the magnitude and position.  Finally, there is a 32 bit integer
+which defines the offset to the first measurement for this star.  This
+offset is defined as the number of Measure records from the start of
+the Measure structure.
+
+The second section of data, following the Average data contains all
+measurements for each star listed in Average.  Each measurement
+occupies 13 bytes, the size of the Measure structure.  This structure
+contains the difference of this position from the average RA and DEC,
+and the instrumental magnitude of this measurement (in the units
+defined by the fstat program, which give m = -2.5*log(cts) + Mo, where
+Mo is currently 24.5 [10/15]).  There is also the magnitude error for
+this measurement, the time of the measurement (in seconds relative to
+a to-be-determined zero point), and a reference to the entry in the
+Average structure so we can relate a given measurement with a given star.
+This last entry also includes a byte of flags, of which only 4 have
+currently been defined.  This means the Average offset can only be as
+large as  16,777,215 (0xffffff), limiting the possible number of stars
+allowed in a given file.  This does not seem like a long term problem,
+though:  aside from the fact that this number is very large and we
+only expect in the vicinity of 20,000 stars per file, the file can
+easily be divided into pieces at a later date if needed.  this last
+step is trivial, consisting of splitting the data up into smaller
+RA,DEC regions, and updating the reference catalog.  With the above
+definitions for the Average and Measure structures, we will typically
+expect 20,000 * 15 measurements per year and 20,000 average entries in
+a given file.  This implies a file size of about 4.3 MB at the end of
+a year.  It is also possible that we will choose to split the files up
+in the future if the number of measurements makes their size
+unweildy.  4.3 MB is sufficiently small that this is not a problem,
+but after only 5 years, the files will be over 21 MB each, getting to
+be fairly significant.  
Index: /tags/addspphot-1-0/Ohana/src/addspphot/doc/description.txt
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/doc/description.txt	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/doc/description.txt	(revision 2549)
@@ -0,0 +1,94 @@
+
+ADDSTAR:
+
+This program takes a photometry file, with astrometry, provided by
+earlier routines and incorporates the stellar measurements into the
+photometry database.  This routine does NOT try to calibrate or check
+the photometry, nor does it try to improve the coordinate
+determinations or calculate chisquares for position or photometry -
+those tasks are performed by other routines.
+
+Addstar starts by loading in the photometry file and converting the
+pixel coordinates to sky coordinates using the astrometry information
+in the header of the file.  It also gets the detection limits and
+stores the image astrometry information.
+
+Next, addstar determines which of all the previous images overlaps
+with this image and also which of the catalog regions overlaps with
+this image.
+
+Addstar considers each catalog region in turn, and compares the
+positions of stars in the catalog with stars in the image.  If a
+catalog star is detected, a new measurement is added to the catalog.
+If a catalog star is not detected, but is in the field of view of this
+image, the detection limit for this image is added to the list of
+measurements as an upper limit.  If the image contains a star which is
+not already included in the catalog, the star is added to the catalog,
+and all previous images are checked to see if they included this
+location.  If so, upper limits are added to the list of measurements.
+
+To deal with blended images, we have taken the philosophy that all
+measurements compatible with the coordinates of a given star should be
+included in the list of measurements.  This assumes that programs or people
+downstream will figure out which is the "correct" measurement.  To
+this end, if a catalog star is consistent with multiple measurements,
+they are all added to the catalog measurement and the measurements are
+given a flag to say that there was blending in the catalog (ie, it is
+probably the catalog star which is a blend).  If multiple catalog
+stars are consistent with the same image star, that measurement is
+added to each catalog star, and given a flag to say that there was
+blending on the image.
+
+After all catalogs have been checked, updated, and written to disk,
+the image is added to the database of images.
+
+The format for a catalog region file is as follows:
+
+-- 
+Header
+--
+Averages
+--
+Measurements
+--
+
+The Header follows the format of a FITS header but always stored with
+3 blocks to speed up read time: 2880 x 3 bytes.  Two important
+keywords in the header: NSTARS & NMEAS
+
+All average values are stored consecutively in a single block.  They
+are written as an array with NSTARS elements of the Average structure
+defined below.  The individual measurements follow the averages as a
+block.  To find a measurement for a given star, you need to get the
+value of the offset for the star.  this is the element number for the
+first measurement of this star, and are followed by next measurements
+for this star, until the value Nm is reached.
+
+Here are the structures being used for the average and measurement
+values:
+
+typedef struct {
+  float R;                    /* RA  in decimal degrees */
+  float D;                    /* DEC in decimal degrees */
+  short int M;                /* thousandths of mag (-32.000 to 32.000 valid range) */
+  unsigned short int Nm;      /* number of measurements */
+  unsigned short int Xp, Xm;  /* chisq values in tenths */
+  unsigned int offset;        /* offset to first measurement */
+} Average;                    /* = 20 bytes / average */
+
+typedef struct {
+  char dR, dD;                /* tenths of arcsec (-12.7 to +12.7 valid range) */
+  short int M;                /* thousandths of mag (-32.000 to 32.000 valid range) */
+  unsigned char dM;           /* thousandths of mag (0.000 -- 0.255 valid range) */
+  float t;                    /* time in seconds (what is reference?) */
+  unsigned int average;       
+  /* reference to corresponding average entry, upper byte stores flags:
+   limit of 16,777,215 stars (Naverage) in a file (=0xFFFFFF) 
+   flags: average & 0x1000000 */
+} Measure;                    /* = 13 bytes / measure */
+
+/* flags for measurements: */
+# define BLEND_IMAGE   0X01000000
+# define BLEND_CATALOG 0X02000000
+# define UPPER_LIMIT   0X04000000
+# define CALIBRATED    0X08000000
Index: /tags/addspphot-1-0/Ohana/src/addspphot/etc/phottemp.cat
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/etc/phottemp.cat	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/etc/phottemp.cat	(revision 2549)
@@ -0,0 +1,296 @@
+SIMPLE  =                    F  / ASTROCAM PHOTOMETRY FILE                    \
+BITPIX  =                   16 /                                              \
+NAXIS   =                    2  / NUMBER OF AXES                              \
+NAXIS1  =                 1106  / NUMBER OF COLUMNS                           \
+NAXIS2  =                 1024  / NUMBER OF ROWS                              \
+BSCALE  =             1.000000 /                                              \
+BZERO   =             0.000000 /                                              \
+DATE    = '19/12/94'            / UT Date of file creation (DD/MM/YY)         \
+ORIGIN  = 'MDM Observatory'     / Michigan-Dartmouth-MIT                      \
+LATITUDE=              31.9500  / Latitude (degrees N)                        \
+LONGITUD=            -111.6150  / Longitude (degrees E)                       \
+OBSERVER= 'Metzger '            / Name of observer                            \
+TELESCOP= '1.3m McGraw-Hill'    / Telescope used for observation              \
+INSTRUME= 'Charlotte Direct'    / Instrument used for observation             \
+DETECTOR= 'Charlotte/Tek 1024^2 CCD'  / Detector used for observation         \
+FRAME   =                   32  / Frame number of observation                 \
+CCDPICNO=                   32  / Frame number of observation                 \
+OBJECT  = 'ocl0327 '            / Name of object                              \
+IMAGETYP= 'OBJECT  '            / Type of observation                         \
+EXPTIME =              250.000  / Integration time (seconds)                  \
+DARKTIME=              250.067  / Dark current time (seconds)                 \
+DATE-OBS= '19/12/94'            / UT Date of observation (DD/MM/YY)           \
+UT      = ' 05:25:44.00'        / Universal time (UTC) at exposure start      \
+JD      =       2449705.726204                                                \
+RA      =           50.700      / Right Ascension                             \
+DEC     =           89.000      / Declination                                 \
+DIRECTN =                 -1    / Moving South                                \
+EQUINOX =             1950.000  / Equinox of RA and DEC                       \
+HA      = ' 02:14:06.72'        / Hour angle at start                         \
+ST      = ' 03:49:36.85'        / Sidereal time at start                      \
+ZD      = ' 36:26:07.17'        / Zenith distance (degrees)                   \
+AIRMASS =                1.243  / Airmass at start                            \
+FILTER  = 'I KP    '            / Filter description                          \
+GAIN    =                3.350  / Nominal gain (e-/ADU)                       \
+SECPIX1 =                0.508  / Arcseconds per pixel in fast dir            \
+SECPIX2 =                0.508  / Arcseconds per pixel in slow dir            \
+CCDBIN1 =                    1  / On-chip column binning (fast dir)           \
+CCDBIN2 =                    1  / On-chip row binning (slow dir)              \
+GPROBE  = '  5000.00   7000.00'  / Guide probe X Y                            \
+DATASEC = '[51:1074,1:1024]'    / Image area of frame                         \
+CCDSEC  = '[1:1074,1:1024]'     / Image area relative to full chip            \
+BIASSEC = '[1080:1106,1:1023]'  / Overscan area of frame                      \
+UNSIGN  =                    T /                                              \
+NSTARS  =                    0  / NUMBER OF stars                             \
+CTYPE1  = 'RA---SIN          ' /                                               
+CTYPE2  = 'DEC--SIN          ' /                                               
+CDELT1  =             0.000733 /                                               
+CDELT2  =             0.000733 /                                               
+CRVAL1  =            50.191418 /                                               
+CRVAL2  =            88.998663 /                                               
+CRPIX1  =          1028.644173 /                                               
+CRPIX2  =           503.129830 /                                               
+PC001001=             0.999906 /                                               
+PC001002=             0.012081 /                                               
+PC002001=            -0.011936 /                                               
+PC002002=             0.999950 /                                               
+END                                                                           \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+ 246.5  352.1 13.000 020 1 3.2
+ 246.5  352.1 14.100 020 1 3.2
+ 279.4  328.9 13.800 020 1 3.2
+ 187.3  238.8  9.800 020 1 3.2
+ 187.3  238.8  9.800 020 1 3.2
+ 436.6  413.5 14.300 020 1 3.2
+   9.3  856.5 13.000 020 1 3.2
+   9.3  856.2 13.600 020 1 3.2
+ 319.7  697.9 12.000 020 1 3.2
+ 319.7  697.7 12.100 020 1 3.2
+ 244.3    9.5 14.100 020 1 3.2
+ 284.7  334.4 14.000 020 1 3.2
+ 127.1  122.8 14.200 020 1 3.2
+ 140.2  791.1 13.700 020 1 3.2
+ 140.2  790.8 14.400 020 1 3.2
+ 131.7  636.3 13.800 020 1 3.2
+ 423.8  700.6 13.800 020 1 3.2
+ 423.7  700.7 12.500 020 1 3.2
+ 266.2  290.7 13.800 020 1 3.2
+ 266.0  290.7 13.100 020 1 3.2
+ 570.7  721.6 13.600 020 1 3.2
+ 570.7  721.6 13.100 020 1 3.2
+ 548.7  673.0 13.700 020 1 3.2
+ 264.2  219.7 12.800 020 1 3.2
+ 264.0  219.7 12.100 020 1 3.2
+ 161.9  797.4 11.900 020 1 3.2
+ 162.0  797.2 12.200 020 1 3.2
+ 505.8  851.8 13.700 020 1 3.2
+  88.2  419.0 11.400 020 1 3.2
+  88.2  419.0 11.800 020 1 3.2
+ 426.9  613.2  9.400 020 1 3.2
+ 427.4  613.5  7.600 020 1 3.2
+ 326.1  595.2 12.100 020 1 3.2
+ 326.2  595.1 12.600 020 1 3.2
+ 444.4  845.2 14.200 020 1 3.2
+ 254.1  916.8 14.500 020 1 3.2
+ 502.6  882.4 14.200 020 1 3.2
+ 128.3  275.7 12.300 020 1 3.2
+ 128.3  275.7 12.900 020 1 3.2
+ 230.9  554.8 13.300 020 1 3.2
+ 230.9  554.7 14.200 020 1 3.2
+  47.5  685.2 13.500 020 1 3.2
+  47.5  684.9 14.200 020 1 3.2
+ 382.6  658.7 14.000 020 1 3.2
+ 182.2  330.3 14.200 020 1 3.2
+ 245.1  548.3 12.600 020 1 3.2
+ 245.3  548.0 13.200 020 1 3.2
+  50.3  849.0 13.700 020 1 3.2
+  50.5  848.8 14.300 020 1 3.2
+  17.9  772.7 14.200 020 1 3.2
+ 499.8  712.8 13.500 020 1 3.2
+ 499.8  712.6 14.400 020 1 3.2
+ 294.5  349.4 14.000 020 1 3.2
+ 497.7  747.8 11.000 020 1 3.2
+ 497.5  747.8 10.900 020 1 3.2
+ 109.7  323.5 14.000 020 1 3.2
+  59.5  917.6 14.300 020 1 3.2
+  59.4  917.8 13.700 020 1 3.2
+  82.2  880.5 13.800 020 1 3.2
+ 145.1  341.2 14.500 020 1 3.2
+ 452.7  592.4 14.400 020 1 3.2
+ 623.4  820.0 13.700 020 1 3.2
+ 609.7  829.3 13.500 020 1 3.2
+ 481.8  938.3 14.400 020 1 3.2
+ 617.0  792.7 13.500 020 1 3.2
+ 183.2  539.7 14.400 020 1 3.2
+ 133.9  679.6 14.400 020 1 3.2
+ 423.7  667.8  6.500 020 1 3.2
+ 434.7  111.9 13.200 020 1 3.2
+ 434.5  111.9 13.200 020 1 3.2
+ 434.6  111.9 14.200 020 1 3.2
+1222.3  684.2 13.900 020 1 3.2
+1077.6  282.5 14.000 020 1 3.2
+1077.8  282.5 13.400 020 1 3.2
+ 720.8  562.1 14.100 020 1 3.2
+ 721.1  561.9 14.400 020 1 3.2
+1182.1  944.4 12.100 020 1 3.2
+1181.9  944.4 11.800 020 1 3.2
+1181.9  944.4 12.300 020 1 3.2
+ 761.2  423.1 14.300 020 1 3.2
+ 761.0  423.1 14.100 020 1 3.2
+ 926.7  436.8 13.400 020 1 3.2
+ 926.6  436.8 14.200 020 1 3.2
+1209.1   45.0 13.200 020 1 3.2
+1209.2   43.6 12.800 020 1 3.2
+1135.2  653.1  8.100 020 1 3.2
+1135.6  653.4  8.200 020 1 3.2
+1123.5  735.8 12.000 020 1 3.2
+1123.6  735.8 11.800 020 1 3.2
+1123.7  735.7 11.600 020 1 3.2
+ 730.7   28.6 12.100 020 1 3.2
+ 730.7   28.6 12.200 020 1 3.2
+ 783.1  267.5 14.200 020 1 3.2
+ 783.2  267.5 13.400 020 1 3.2
+ 876.2  462.7 13.800 020 1 3.2
+ 876.4  462.7 14.200 020 1 3.2
+ 917.2  399.9 13.700 020 1 3.2
+ 917.3  399.9 13.200 020 1 3.2
+1227.5  218.4 10.400 020 1 3.2
+1227.7  218.4 10.500 020 1 3.2
+1022.7  146.0 13.300 020 1 3.2
+1022.5  146.0 14.100 020 1 3.2
+1324.7   72.3 12.900 020 1 3.2
+1324.6   72.3 13.200 020 1 3.2
+ 743.2  379.4 14.300 020 1 3.2
+ 742.9  379.4 13.900 020 1 3.2
+1255.9  544.0 12.600 020 1 3.2
+1256.0  543.9 12.300 020 1 3.2
+ 848.9  251.1 13.200 020 1 3.2
+ 849.0  251.1 12.700 020 1 3.2
+ 495.6  147.4 11.400 020 1 3.2
+ 495.6  147.4 11.300 020 1 3.2
+ 495.5  147.4 11.000 020 1 3.2
+1016.4  735.5 14.200 020 1 3.2
+1016.3  735.7 13.900 020 1 3.2
+ 795.2  675.9 14.400 020 1 3.2
+ 887.2  968.8 10.800 020 1 3.2
+ 626.8  726.9 14.400 020 1 3.2
+ 718.6  800.1  9.700 020 1 3.2
+ 675.4  301.6 14.400 020 1 3.2
+ 801.6  436.8 14.200 020 1 3.2
+1066.4  870.0 13.700 020 1 3.2
+1066.5  869.9 13.800 020 1 3.2
+ 644.5  662.9  9.900 020 1 3.2
+ 639.1  760.4 13.000 020 1 3.2
+ 822.7  663.1 12.800 020 1 3.2
+ 822.7  663.1 13.100 020 1 3.2
+ 677.2  716.2 12.900 020 1 3.2
+ 765.6  641.5 13.200 020 1 3.2
+ 765.7  641.4 13.500 020 1 3.2
+ 998.3  425.8 14.400 020 1 3.2
+ 713.7  716.2 13.800 020 1 3.2
+ 814.7  887.6  2.000 020 1 3.2
+ 808.7  884.1  8.200 020 1 3.2
+1812.3  259.3 14.500 020 1 3.2
+1655.9  655.4 12.200 020 1 3.2
+1655.9  655.4 12.100 020 1 3.2
+1577.2  839.6 11.500 020 1 3.2
+1577.2  839.6 11.400 020 1 3.2
+1643.7  597.0 14.100 020 1 3.2
+1552.8  251.1 10.100 020 1 3.2
+1552.8  251.1 10.300 020 1 3.2
+1410.0  999.9 13.900 020 1 3.2
+1335.6  687.0 13.000 020 1 3.2
+1335.8  686.8 12.700 020 1 3.2
+1471.4  262.0 14.200 020 1 3.2
+1479.6  608.3 14.200 020 1 3.2
+1478.1  211.5 12.000 020 1 3.2
+1478.3  211.5 12.000 020 1 3.2
+1552.5   46.4 14.500 020 1 3.2
+2042.4  548.3 10.800 020 1 3.2
+2042.6  548.3 11.000 020 1 3.2
+1448.0  915.7 14.100 020 1 3.2
+1462.3  989.2 13.000 020 1 3.2
+1462.3  989.0 12.700 020 1 3.2
+1316.9  787.3 14.100 020 1 3.2
+1787.6  883.5 12.200 020 1 3.2
+1787.6  883.5 12.200 020 1 3.2
+1979.1  300.3 12.900 020 1 3.2
+1979.1  300.3 12.900 020 1 3.2
+1677.2  106.4 13.100 020 1 3.2
+1677.0  106.4 13.300 020 1 3.2
+1324.2  429.9  9.200 020 1 3.2
+1324.2  429.9  9.000 020 1 3.2
+1462.8  214.3 13.700 020 1 3.2
+1462.8  214.3 14.200 020 1 3.2
+1405.4   95.5 11.700 020 1 3.2
+1405.2   95.5 11.600 020 1 3.2
+1604.8   90.0 11.200 020 1 3.2
+1604.8   90.0 11.300 020 1 3.2
+1921.5  738.1 13.500 020 1 3.2
+1921.5  738.1 13.500 020 1 3.2
+1459.8  473.6 13.400 020 1 3.2
+1459.7  473.6 13.800 020 1 3.2
+1438.7   77.8 12.200 020 1 3.2
+1438.6   77.8 12.200 020 1 3.2
+1355.0  114.6 13.000 020 1 3.2
+1354.9  114.6 13.200 020 1 3.2
+1941.4  940.7 13.000 020 1 3.2
+1941.6  940.7 12.900 020 1 3.2
+1994.0  800.8 13.900 020 1 3.2
+1993.8  800.8 14.000 020 1 3.2
+2024.4  907.0 13.700 020 1 3.2
+2024.4  907.0 13.800 020 1 3.2
+2035.7  881.6 13.900 020 1 3.2
+2035.9  881.6 13.800 020 1 3.2
+1994.0  924.2 12.100 020 1 3.2
+1994.0  924.2 12.000 020 1 3.2
Index: /tags/addspphot-1-0/Ohana/src/addspphot/etc/template.cat
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/etc/template.cat	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/etc/template.cat	(revision 2549)
@@ -0,0 +1,296 @@
+SIMPLE  =                    F  / ASTROCAM PHOTOMETRY FILE                    \
+BITPIX  =                   16 /                                              \
+NAXIS   =                    2  / NUMBER OF AXES                              \
+NAXIS1  =                 1106  / NUMBER OF COLUMNS                           \
+NAXIS2  =                 1024  / NUMBER OF ROWS                              \
+BSCALE  =             1.000000 /                                              \
+BZERO   =             0.000000 /                                              \
+DATE    = '19/12/94'            / UT Date of file creation (DD/MM/YY)         \
+ORIGIN  = 'MDM Observatory'     / Michigan-Dartmouth-MIT                      \
+LATITUDE=              31.9500  / Latitude (degrees N)                        \
+LONGITUD=            -111.6150  / Longitude (degrees E)                       \
+OBSERVER= 'Metzger '            / Name of observer                            \
+TELESCOP= '1.3m McGraw-Hill'    / Telescope used for observation              \
+INSTRUME= 'Charlotte Direct'    / Instrument used for observation             \
+DETECTOR= 'Charlotte/Tek 1024^2 CCD'  / Detector used for observation         \
+FRAME   =                   32  / Frame number of observation                 \
+CCDPICNO=                   32  / Frame number of observation                 \
+OBJECT  = 'ocl0327 '            / Name of object                              \
+IMAGETYP= 'OBJECT  '            / Type of observation                         \
+EXPTIME =              250.000  / Integration time (seconds)                  \
+DARKTIME=              250.067  / Dark current time (seconds)                 \
+DATE-OBS= '19/12/94'            / UT Date of observation (DD/MM/YY)           \
+UT      = ' 05:25:44.00'        / Universal time (UTC) at exposure start      \
+JD      =       2449705.726204                                                \
+RA      =           50.700      / Right Ascension                             \
+DEC     =           89.000      / Declination                                 \
+DIRECTN =                 -1    / Moving South                                \
+EQUINOX =             1950.000  / Equinox of RA and DEC                       \
+HA      = ' 02:14:06.72'        / Hour angle at start                         \
+ST      = ' 03:49:36.85'        / Sidereal time at start                      \
+ZD      = ' 36:26:07.17'        / Zenith distance (degrees)                   \
+AIRMASS =                1.243  / Airmass at start                            \
+FILTER  = 'I KP    '            / Filter description                          \
+GAIN    =                3.350  / Nominal gain (e-/ADU)                       \
+SECPIX1 =                0.508  / Arcseconds per pixel in fast dir            \
+SECPIX2 =                0.508  / Arcseconds per pixel in slow dir            \
+CCDBIN1 =                    1  / On-chip column binning (fast dir)           \
+CCDBIN2 =                    1  / On-chip row binning (slow dir)              \
+GPROBE  = '  5000.00   7000.00'  / Guide probe X Y                            \
+DATASEC = '[51:1074,1:1024]'    / Image area of frame                         \
+CCDSEC  = '[1:1074,1:1024]'     / Image area relative to full chip            \
+BIASSEC = '[1080:1106,1:1023]'  / Overscan area of frame                      \
+UNSIGN  =                    T /                                              \
+NSTARS  =                    0  / NUMBER OF stars                             \
+CTYPE1  = 'RA---SIN          ' /                                               
+CTYPE2  = 'DEC--SIN          ' /                                               
+CDELT1  =             0.000733 /                                               
+CDELT2  =             0.000733 /                                               
+CRVAL1  =            50.191418 /                                               
+CRVAL2  =            88.998663 /                                               
+CRPIX1  =          1028.644173 /                                               
+CRPIX2  =           503.129830 /                                               
+PC001001=             0.999906 /                                               
+PC001002=             0.012081 /                                               
+PC002001=            -0.011936 /                                               
+PC002002=             0.999950 /                                               
+END                                                                           \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+                                                                              \
+ 246.5  352.1 13.000 020 1 3.2
+ 246.5  352.1 14.100 020 1 3.2
+ 279.4  328.9 13.800 020 1 3.2
+ 187.3  238.8  9.800 020 1 3.2
+ 187.3  238.8  9.800 020 1 3.2
+ 436.6  413.5 14.300 020 1 3.2
+   9.3  856.5 13.000 020 1 3.2
+   9.3  856.2 13.600 020 1 3.2
+ 319.7  697.9 12.000 020 1 3.2
+ 319.7  697.7 12.100 020 1 3.2
+ 244.3    9.5 14.100 020 1 3.2
+ 284.7  334.4 14.000 020 1 3.2
+ 127.1  122.8 14.200 020 1 3.2
+ 140.2  791.1 13.700 020 1 3.2
+ 140.2  790.8 14.400 020 1 3.2
+ 131.7  636.3 13.800 020 1 3.2
+ 423.8  700.6 13.800 020 1 3.2
+ 423.7  700.7 12.500 020 1 3.2
+ 266.2  290.7 13.800 020 1 3.2
+ 266.0  290.7 13.100 020 1 3.2
+ 570.7  721.6 13.600 020 1 3.2
+ 570.7  721.6 13.100 020 1 3.2
+ 548.7  673.0 13.700 020 1 3.2
+ 264.2  219.7 12.800 020 1 3.2
+ 264.0  219.7 12.100 020 1 3.2
+ 161.9  797.4 11.900 020 1 3.2
+ 162.0  797.2 12.200 020 1 3.2
+ 505.8  851.8 13.700 020 1 3.2
+  88.2  419.0 11.400 020 1 3.2
+  88.2  419.0 11.800 020 1 3.2
+ 426.9  613.2  9.400 020 1 3.2
+ 427.4  613.5  7.600 020 1 3.2
+ 326.1  595.2 12.100 020 1 3.2
+ 326.2  595.1 12.600 020 1 3.2
+ 444.4  845.2 14.200 020 1 3.2
+ 254.1  916.8 14.500 020 1 3.2
+ 502.6  882.4 14.200 020 1 3.2
+ 128.3  275.7 12.300 020 1 3.2
+ 128.3  275.7 12.900 020 1 3.2
+ 230.9  554.8 13.300 020 1 3.2
+ 230.9  554.7 14.200 020 1 3.2
+  47.5  685.2 13.500 020 1 3.2
+  47.5  684.9 14.200 020 1 3.2
+ 382.6  658.7 14.000 020 1 3.2
+ 182.2  330.3 14.200 020 1 3.2
+ 245.1  548.3 12.600 020 1 3.2
+ 245.3  548.0 13.200 020 1 3.2
+  50.3  849.0 13.700 020 1 3.2
+  50.5  848.8 14.300 020 1 3.2
+  17.9  772.7 14.200 020 1 3.2
+ 499.8  712.8 13.500 020 1 3.2
+ 499.8  712.6 14.400 020 1 3.2
+ 294.5  349.4 14.000 020 1 3.2
+ 497.7  747.8 11.000 020 1 3.2
+ 497.5  747.8 10.900 020 1 3.2
+ 109.7  323.5 14.000 020 1 3.2
+  59.5  917.6 14.300 020 1 3.2
+  59.4  917.8 13.700 020 1 3.2
+  82.2  880.5 13.800 020 1 3.2
+ 145.1  341.2 14.500 020 1 3.2
+ 452.7  592.4 14.400 020 1 3.2
+ 623.4  820.0 13.700 020 1 3.2
+ 609.7  829.3 13.500 020 1 3.2
+ 481.8  938.3 14.400 020 1 3.2
+ 617.0  792.7 13.500 020 1 3.2
+ 183.2  539.7 14.400 020 1 3.2
+ 133.9  679.6 14.400 020 1 3.2
+ 423.7  667.8  6.500 020 1 3.2
+ 434.7  111.9 13.200 020 1 3.2
+ 434.5  111.9 13.200 020 1 3.2
+ 434.6  111.9 14.200 020 1 3.2
+1222.3  684.2 13.900 020 1 3.2
+1077.6  282.5 14.000 020 1 3.2
+1077.8  282.5 13.400 020 1 3.2
+ 720.8  562.1 14.100 020 1 3.2
+ 721.1  561.9 14.400 020 1 3.2
+1182.1  944.4 12.100 020 1 3.2
+1181.9  944.4 11.800 020 1 3.2
+1181.9  944.4 12.300 020 1 3.2
+ 761.2  423.1 14.300 020 1 3.2
+ 761.0  423.1 14.100 020 1 3.2
+ 926.7  436.8 13.400 020 1 3.2
+ 926.6  436.8 14.200 020 1 3.2
+1209.1   45.0 13.200 020 1 3.2
+1209.2   43.6 12.800 020 1 3.2
+1135.2  653.1  8.100 020 1 3.2
+1135.6  653.4  8.200 020 1 3.2
+1123.5  735.8 12.000 020 1 3.2
+1123.6  735.8 11.800 020 1 3.2
+1123.7  735.7 11.600 020 1 3.2
+ 730.7   28.6 12.100 020 1 3.2
+ 730.7   28.6 12.200 020 1 3.2
+ 783.1  267.5 14.200 020 1 3.2
+ 783.2  267.5 13.400 020 1 3.2
+ 876.2  462.7 13.800 020 1 3.2
+ 876.4  462.7 14.200 020 1 3.2
+ 917.2  399.9 13.700 020 1 3.2
+ 917.3  399.9 13.200 020 1 3.2
+1227.5  218.4 10.400 020 1 3.2
+1227.7  218.4 10.500 020 1 3.2
+1022.7  146.0 13.300 020 1 3.2
+1022.5  146.0 14.100 020 1 3.2
+1324.7   72.3 12.900 020 1 3.2
+1324.6   72.3 13.200 020 1 3.2
+ 743.2  379.4 14.300 020 1 3.2
+ 742.9  379.4 13.900 020 1 3.2
+1255.9  544.0 12.600 020 1 3.2
+1256.0  543.9 12.300 020 1 3.2
+ 848.9  251.1 13.200 020 1 3.2
+ 849.0  251.1 12.700 020 1 3.2
+ 495.6  147.4 11.400 020 1 3.2
+ 495.6  147.4 11.300 020 1 3.2
+ 495.5  147.4 11.000 020 1 3.2
+1016.4  735.5 14.200 020 1 3.2
+1016.3  735.7 13.900 020 1 3.2
+ 795.2  675.9 14.400 020 1 3.2
+ 887.2  968.8 10.800 020 1 3.2
+ 626.8  726.9 14.400 020 1 3.2
+ 718.6  800.1  9.700 020 1 3.2
+ 675.4  301.6 14.400 020 1 3.2
+ 801.6  436.8 14.200 020 1 3.2
+1066.4  870.0 13.700 020 1 3.2
+1066.5  869.9 13.800 020 1 3.2
+ 644.5  662.9  9.900 020 1 3.2
+ 639.1  760.4 13.000 020 1 3.2
+ 822.7  663.1 12.800 020 1 3.2
+ 822.7  663.1 13.100 020 1 3.2
+ 677.2  716.2 12.900 020 1 3.2
+ 765.6  641.5 13.200 020 1 3.2
+ 765.7  641.4 13.500 020 1 3.2
+ 998.3  425.8 14.400 020 1 3.2
+ 713.7  716.2 13.800 020 1 3.2
+ 814.7  887.6  2.000 020 1 3.2
+ 808.7  884.1  8.200 020 1 3.2
+1812.3  259.3 14.500 020 1 3.2
+1655.9  655.4 12.200 020 1 3.2
+1655.9  655.4 12.100 020 1 3.2
+1577.2  839.6 11.500 020 1 3.2
+1577.2  839.6 11.400 020 1 3.2
+1643.7  597.0 14.100 020 1 3.2
+1552.8  251.1 10.100 020 1 3.2
+1552.8  251.1 10.300 020 1 3.2
+1410.0  999.9 13.900 020 1 3.2
+1335.6  687.0 13.000 020 1 3.2
+1335.8  686.8 12.700 020 1 3.2
+1471.4  262.0 14.200 020 1 3.2
+1479.6  608.3 14.200 020 1 3.2
+1478.1  211.5 12.000 020 1 3.2
+1478.3  211.5 12.000 020 1 3.2
+1552.5   46.4 14.500 020 1 3.2
+2042.4  548.3 10.800 020 1 3.2
+2042.6  548.3 11.000 020 1 3.2
+1448.0  915.7 14.100 020 1 3.2
+1462.3  989.2 13.000 020 1 3.2
+1462.3  989.0 12.700 020 1 3.2
+1316.9  787.3 14.100 020 1 3.2
+1787.6  883.5 12.200 020 1 3.2
+1787.6  883.5 12.200 020 1 3.2
+1979.1  300.3 12.900 020 1 3.2
+1979.1  300.3 12.900 020 1 3.2
+1677.2  106.4 13.100 020 1 3.2
+1677.0  106.4 13.300 020 1 3.2
+1324.2  429.9  9.200 020 1 3.2
+1324.2  429.9  9.000 020 1 3.2
+1462.8  214.3 13.700 020 1 3.2
+1462.8  214.3 14.200 020 1 3.2
+1405.4   95.5 11.700 020 1 3.2
+1405.2   95.5 11.600 020 1 3.2
+1604.8   90.0 11.200 020 1 3.2
+1604.8   90.0 11.300 020 1 3.2
+1921.5  738.1 13.500 020 1 3.2
+1921.5  738.1 13.500 020 1 3.2
+1459.8  473.6 13.400 020 1 3.2
+1459.7  473.6 13.800 020 1 3.2
+1438.7   77.8 12.200 020 1 3.2
+1438.6   77.8 12.200 020 1 3.2
+1355.0  114.6 13.000 020 1 3.2
+1354.9  114.6 13.200 020 1 3.2
+1941.4  940.7 13.000 020 1 3.2
+1941.6  940.7 12.900 020 1 3.2
+1994.0  800.8 13.900 020 1 3.2
+1993.8  800.8 14.000 020 1 3.2
+2024.4  907.0 13.700 020 1 3.2
+2024.4  907.0 13.800 020 1 3.2
+2035.7  881.6 13.900 020 1 3.2
+2035.9  881.6 13.800 020 1 3.2
+1994.0  924.2 12.100 020 1 3.2
+1994.0  924.2 12.000 020 1 3.2
Index: /tags/addspphot-1-0/Ohana/src/addspphot/include/addstar.h
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/include/addstar.h	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/include/addstar.h	(revision 2549)
@@ -0,0 +1,87 @@
+# include <ohana.h>
+# include <loneos.h>
+# include <signal.h>
+
+typedef struct {
+  double X;
+  double Y;
+  double R;
+  double D;
+  double M, dM;
+  char   dophot;
+  double sky;
+  double fx, fy, df;
+  double Mgal, Map;
+  int found;
+} Stars;
+
+/* global variables set in parameter file */
+char   ImageCat[256];
+char   ImageTemplate[256];
+char   CatTemplate[256];
+char   GSCFILE[256];
+char   CATDIR[256];
+double DEFAULT_RADIUS, NSIGMA, SNLIMIT;
+double ALPHA, ZeroPt;
+double Latitude, SiderealTime;
+int    VERBOSE, FORCE;
+int    XOVERSCAN, YOVERSCAN;
+int    ACCEPT_ASTROM;
+
+PhotCode *thiscode;
+
+char DateKeyword[64];
+char DateMode[64];
+char UTKeyword[64];
+char MJDKeyword[64];
+char JDKeyword[64];
+char ExptimeKeyword[64];
+char AirmassKeyword[64];
+char CCDNumKeyword[64];
+char STKeyword[64];
+char SubpixDatafile[256];
+
+int CalReference;
+int CalColor;
+int CALIBRATE;
+int DUMP_MATCHES;
+int ONLY_IMAGES;
+int SKIP_MISSED;
+
+double get_subpix (double x, double y);
+void load_subpix ();
+double scat_subpix (double x, double y);
+
+int Shutdown ();  
+void TrapSignal (int sig);
+int main (int argc, char **argv);
+int SetSignals ();
+int gcatalog (Catalog *catalog);
+GSCRegion *gregions (Image *image, int *Nregions);
+Image *gimages (FILE *f, Image *image, int *Npimage);
+int edge_check (double *x1, double *y1, double *x2, double *y2);
+double opening_angle (double x1, double y1, double x2, double y2, double x3, double y3);
+void wimage (FILE *f, int dbstate, Image *image, int Nstars);
+Stars *gstars (char *file, int *NSTARS, Image *image);
+void find_matches (GSCRegion *region, Stars *stars, int Nstars, Catalog *catalog, Image *image, Image *pimage, int Nimage);
+int in_image (double r, double d, Image *image);
+void wcatalog (Catalog *catalog);
+void ConfigInit (int *argc, char **argv);
+int hms_to_deg (double *h0, double *h1, double *d0, double *d1, char *string);
+void aregion (GSCRegion *region, FILE *f, double ra, double dec);
+void wimage (FILE *f, int dbstate, Image *image, int Nstars);
+void check_permissions (char *basefile);
+void make_backup (char *filename);
+void help ();
+int args (int argc, char **argv);
+int parse_time (Header *header);
+void uppercase (char *string);
+void mkcatalog (GSCRegion *region, Catalog *catalog);
+double airmass (double ra, double dec, double st, double latitude);
+
+void SaveCalibration (float M, float dM, float Mr, float dMr, float Mc, float A, int N, float ra, float dec, float x, float y);
+void InitCalibration ();
+void FindCalibration (Image *image);
+void fsort (float *X, int N);
+void sort_lists (float *X, float *Y, int *S, int N);
+void fsort2 (float *X, float *Y, int N);
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/ConfigInit.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/ConfigInit.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/ConfigInit.c	(revision 2549)
@@ -0,0 +1,59 @@
+# include "addstar.h"
+
+void ConfigInit (int *argc, char **argv) {
+
+  char *config, *file;
+  char RadiusWord[80];
+  char PhotCodeFile[256];
+
+  /*** load configuration info ***/
+  file = SelectConfigFile (argc, argv, "ptolemy");
+  config = LoadConfigFile (file);
+  if (config == (char *) NULL) {
+    fprintf (stderr, "ERROR: can't find configuration file %s\n", file);
+    if (file != (char *) NULL) free (file);
+    exit (1);
+  }
+  if (VERBOSE) fprintf (stderr, "loaded config file: %s\n", file);
+
+  ScanConfig (config, "RADIUS",                 "%s", 0,  RadiusWord);
+  if (!strcasecmp (RadiusWord, "header")) {
+    DEFAULT_RADIUS = 0;
+  } else {
+    DEFAULT_RADIUS = atof (RadiusWord);
+  }
+  ScanConfig (config, "NSIGMA",                 "%lf", 0, &NSIGMA);
+  ScanConfig (config, "ALPHA",                  "%lf", 0, &ALPHA);
+  ScanConfig (config, "XOVERSCAN",              "%d",  0, &XOVERSCAN);
+  ScanConfig (config, "YOVERSCAN",              "%d",  0, &YOVERSCAN);
+  ScanConfig (config, "IMAGE_CATALOG",          "%s",  0, ImageCat);
+  ScanConfig (config, "IMAGE_CATALOG_TEMPLATE", "%s",  0, ImageTemplate);
+  ScanConfig (config, "CATALOG_TEMPLATE",       "%s",  0, CatTemplate);
+  ScanConfig (config, "GSCFILE",                "%s",  0, GSCFILE);
+  ScanConfig (config, "CATDIR",                 "%s",  0, CATDIR);
+  ScanConfig (config, "MIN_SN_FSTAT",           "%lf", 0, &SNLIMIT);
+  ScanConfig (config, "PHOTCODE_FILE",          "%s",  0, PhotCodeFile);
+
+  ScanConfig (config, "DATE-KEYWORD",           "%s",  0, DateKeyword);
+  ScanConfig (config, "DATE-MODE",              "%s",  0, DateMode);
+  ScanConfig (config, "UT-KEYWORD",             "%s",  0, UTKeyword);
+  ScanConfig (config, "MJD-KEYWORD",            "%s",  0, MJDKeyword);
+  ScanConfig (config, "JD-KEYWORD",             "%s",  0, JDKeyword);
+
+  ScanConfig (config, "EXPTIME-KEYWORD",        "%s",  0, ExptimeKeyword);
+  ScanConfig (config, "AIRMASS-KEYWORD",        "%s",  0, AirmassKeyword);
+  ScanConfig (config, "CCDNUM-KEYWORD",         "%s",  0, CCDNumKeyword);
+
+  ScanConfig (config, "ST-KEYWORD",             "%s",  0, STKeyword);
+  ScanConfig (config, "OBSERVATORY-LATITUDE",   "%lf", 0, &Latitude);
+  ScanConfig (config, "SUBPIX_DATAFILE",        "%s",  0, SubpixDatafile);
+
+  if (!LoadPhotcodes (PhotCodeFile)) {
+    fprintf (stderr, "error loading photcodes\n");
+    exit (1);
+  }
+
+  free (config);
+  free (file);
+
+}
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/addspphot.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/addspphot.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/addspphot.c	(revision 2549)
@@ -0,0 +1,222 @@
+# include "addstar.h"
+
+/* these variables are needed by Shutdown */
+static FILE *f = (FILE *) NULL;
+static int Protect = FALSE;
+static int Trapped = FALSE;
+
+/* clean up open / locked files before shutting down */
+int Shutdown () {  
+  int dbstate;
+
+  Protect = TRUE;
+  fclearlockfile (ImageCat, f, LCK_HARD, &dbstate);
+  fprintf (stderr, "ERROR: addstar halted\n");
+  exit (1);
+}
+
+void TrapSignal (int sig) {
+    fprintf (stderr, "trapped signal %d, exiting gracefully\n", sig);
+    if (sig == 11) {
+      fprintf (stderr, "seg fault\n");
+      exit (1);
+    }
+    if (Protect) {
+      Trapped = TRUE;
+      fprintf (stderr, "blocking until protected sections are clear\n");
+      return;
+    }
+    Shutdown ();
+}    
+
+int main (int argc, char **argv) {
+
+  int i, mode, dbstate;
+  int Nstars, Nimage, Nregions, Nmissed;
+  Stars *stars, *gstars();
+  Image image, *pimage, *gimages();
+  GSCRegion *region, *gregions();
+  Catalog catalog;
+  
+  SetSignals ();
+  args (argc, argv);
+
+  load_subpix ();
+
+  stars = gstars (argv[1], &Nstars, &image);
+  if (Nstars == 0) {
+    if (VERBOSE) fprintf (stderr, "no stars in data file, skipping\n");
+    fprintf (stderr, "SUCCESS\n");
+    exit (0);
+  }
+
+  /* lock the image catalog */
+  check_permissions (ImageCat);
+  f = fsetlockfile (ImageCat, 3600.0, LCK_HARD, &dbstate);
+  if ((f == (FILE *) NULL) && (dbstate != LCK_EMPTY)) {
+    fprintf (stderr, "ERROR: can't lock image catalog\n");
+    exit (1);
+  }
+  fseek (f, 0, SEEK_SET);
+
+  /* load images */
+  if (dbstate == LCK_EMPTY) {
+    Nimage = 0;
+  } else {
+    pimage = gimages (f, &image, &Nimage);
+  }
+  region = gregions (&image, &Nregions);
+  fprintf (stderr, "region %f %f %f %f\n", region[0].RA[0], region[0].RA[1], region[0].DEC[0], region[0].DEC[1]);
+
+  for (i = 0; i < Nregions; i++) {
+    check_permissions (region[i].filename);
+  }
+
+  if (CALIBRATE) { InitCalibration (); }
+
+  for (i = 0; i < Nregions; i++) {
+    catalog.filename = region[i].filename;  /* don't free region before catalog! */
+    fprintf (stderr, "adding to %s\n", region[i].filename);
+    
+    switch (lock_catalog (&catalog, LCK_XCLD)) {
+    case 0:
+      fprintf (stderr, "ERROR: can't lock file %s\n", catalog.filename);
+      exit (1);
+    case 1:
+      gcatalog (&catalog); /* load from disk */
+      break;
+    case 2:
+      mkcatalog (&region[i], &catalog); /* fills in new header info */
+      break;
+    }
+    find_matches (&region[i], stars, Nstars, &catalog, &image, pimage, Nimage);
+
+    /* protect wcatalog from interrupt signals */
+    Protect = TRUE;
+    if (!DUMP_MATCHES && !ONLY_IMAGES) wcatalog (&catalog);
+    if (Trapped) Shutdown ();
+    Protect = FALSE;
+    unlock_catalog (&catalog);
+  }
+
+  if (CALIBRATE) { FindCalibration (&image); }
+  if (DUMP_MATCHES) Shutdown ();
+
+  for (Nmissed = i = 0; i < Nstars; i++) {
+    if (stars[i].found == -1) {
+      fprintf (stderr, "%d %f %f %f %f\n", i, stars[i].R, stars[i].D, stars[i].M, stars[i].dM);
+      Nmissed ++;
+    }
+  }
+  if (Nmissed) fprintf (stderr, "WARNING: %d stars in image were missed!\n", Nmissed);
+
+  /* protect wimage from interrupt signals */
+  Protect = TRUE;
+  wimage (f, dbstate, &image, Nstars); 
+  if (Trapped) Shutdown ();
+  Protect = FALSE;
+
+  fclearlockfile (ImageCat, f, LCK_HARD, &dbstate);
+
+  mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
+  chmod (ImageCat, mode);
+
+  fprintf (stderr, "SUCCESS\n");
+  exit (0);
+
+}
+
+int SetSignals () {
+
+  int i;
+
+  /* disable almost all signal interrupts */
+  for (i = 0; i < 36; i++) {
+    switch (i) {
+      /* can't redirect this signals */
+    case SIGKILL:    /* kill -9: cannot be caught or ignored */
+    case SIGSTOP:    /* SIGSTOP: cannot be caught or ignored */
+      /* ignore these signals */
+    case SIGCHLD:    /* child halted: ignore */
+    case SIGPWR:     /* power failure - why ignore this? */
+    case SIGWINCH:   /* window resized */
+    case SIGCONT:    /* continue - maintain this action */
+    case SIGTSTP:    /* stop signal sent from tty - why ignore? */
+    case SIGURG:     /* socket signal, ignore this */
+      break;
+      
+    default:
+      signal (i, TrapSignal);
+    }
+  }
+  return (TRUE);
+}
+/*
+
+       Signal     Value     Action   Comment
+       -------------------------------------------------------------------------
+       SIGHUP        1        A      Hangup detected on controlling terminal
+                                     or death of controlling process
+       SIGINT        2        A      Interrupt from keyboard
+       SIGQUIT       3        A      Quit from keyboard
+       SIGILL        4        A      Illegal Instruction
+       SIGABRT       6        C      Abort signal from abort(3)
+       SIGFPE        8        C      Floating point exception
+       SIGKILL       9       AEF     Kill signal
+       SIGSEGV      11        C      Invalid memory reference
+       SIGPIPE      13        A      Broken pipe: write to pipe with no readers
+       SIGALRM      14        A      Timer signal from alarm(2)
+       SIGTERM      15        A      Termination signal
+       SIGUSR1   30,10,16     A      User-defined signal 1
+       SIGUSR2   31,12,17     A      User-defined signal 2
+       SIGCHLD   20,17,18     B      Child stopped or terminated
+       SIGCONT   19,18,25            Continue if stopped
+       SIGSTOP   17,19,23    DEF     Stop process
+       SIGTSTP   18,20,24     D      Stop typed at tty
+       SIGTTIN   21,21,26     D      tty input for background process
+       SIGTTOU   22,22,27     D      tty output for background process
+
+       Next various other signals.
+
+       Signal       Value     Action   Comment
+       ---------------------------------------------------------------------
+       SIGTRAP        5         CG     Trace/breakpoint trap
+       SIGIOT         6         CG     IOT trap. A synonym for SIGABRT
+       SIGEMT       7,-,7       G
+       SIGBUS      10,7,10      AG     Bus error
+       SIGSYS      12,-,12      G      Bad argument to routine (SVID)
+       SIGSTKFLT    -,16,-      AG     Stack fault on coprocessor
+       SIGURG      16,23,21     BG     Urgent condition on socket (4.2 BSD)
+       SIGIO       23,29,22     AG     I/O now possible (4.2 BSD)
+       SIGPOLL                  AG     A synonym for SIGIO (System V)
+       SIGCLD       -,-,18      G      A synonym for SIGCHLD
+       SIGXCPU     24,24,30     AG     CPU time limit exceeded (4.2 BSD)
+       SIGXFSZ     25,25,31     AG     File size limit exceeded (4.2 BSD)
+       SIGVTALRM   26,26,28     AG     Virtual alarm clock (4.2 BSD)
+       SIGPROF     27,27,29     AG     Profile alarm clock
+       SIGPWR      29,30,19     AG     Power failure (System V)
+       SIGINFO      29,-,-      G      A synonym for SIGPWR
+       SIGLOST      -,-,-       AG     File lock lost
+       SIGWINCH    28,28,20     BG     Window resize signal (4.3 BSD, Sun)
+       SIGUNUSED    -,31,-      AG     Unused signal
+       (Here - denotes that a signal is absent; there where three values are given, the first one is usually  valid  for  alpha  and
+       sparc,  the  middle  one  for i386 and ppc, the last one for mips. Signal 29 is SIGINFO / SIGPWR on an alpha but SIGLOST on a
+       sparc.)
+
+       The letters in the "Action" column have the following meanings:
+
+       A      Default action is to terminate the process.
+
+       B      Default action is to ignore the signal.
+
+       C      Default action is to dump core.
+
+       D      Default action is to stop the process.
+
+       E      Signal cannot be caught.
+
+       F      Signal cannot be ignored.
+
+       G      Not a POSIX.1 conformant signal.
+
+*/
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/airmass.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/airmass.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/airmass.c	(revision 2549)
@@ -0,0 +1,17 @@
+# include "addstar.h"
+
+double airmass (double ra, double dec, double st, double latitude) {
+
+  double hour, cosz, secz;
+
+  /* ra, dec, latitude in dec deg; st in dec hours */
+
+  /* hour : hour angle in degrees */
+  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);
+  
+  secz = 1.0 / cosz;
+
+  return (secz);
+
+}
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/aregion.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/aregion.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/aregion.c	(revision 2549)
@@ -0,0 +1,174 @@
+# include "addstar.h"
+
+double BigDecBounds[] = {0.0, 7.5, 15.0, 22.5, 30.0, 37.5, 45.0, 
+			 52.5, 60.0, 67.5, 75.0, 82.5, 90.0,
+			 0.0, -7.5, -15.0, -22.5, -30.0, -37.5, -45.0, 
+			 -52.5, -60.0, -67.5, -75.0, -82.5, -90.0};
+char *DecSections[] = {"N0000", "N0730", "N1500", "N2230", "N3000", "N3730", "N4500", 
+		       "N5230", "N6000", "N6730", "N7500", "N8230", "weirdness", 
+		       "S0000", "S0730", "S1500", "S2230", "S3000", "S3730", "S4500", 
+		       "S5230", "S6000", "S6730", "S7500", "S8230", "weirdness"};
+
+char *Dec2Sections[] = {"n0000", "n0730", "n1500", "n2230", "n3000", "n3730", "n4500", 
+			"n5230", "n6000", "n6730", "n7500", "n8230", "weirdness", 
+			"s0000", "s0730", "s1500", "s2230", "s3000", "s3730", "s4500", 
+			"s5230", "s6000", "s6730", "s7500", "s8230", "weirdness"};
+
+char *disk[] = {"disk 1", "disk 1", "disk 1", "disk 1", "disk 1", "disk 1", "disk 1", 
+		"disk 1", "disk 1", "disk 1", "disk 1", "disk 1", "weirdness", 
+		"disk 1", "disk 2", "disk 2", "disk 2", "disk 2", "disk 2", "disk 2", 
+		"disk 2", "disk 2", "disk 2", "disk 2", "disk 2", "weirdness"};
+
+int NBigRASections [] = {48, 47, 45, 43, 40, 36, 32, 28, 21, 15, 9, 3, 3, 48, 47, 45, 43, 40, 36, 32, 28, 21, 15, 9, 3, 3};
+
+int NDecLines[] = {593, 584, 551, 530, 522, 465, 406, 362, 280, 198, 123, 24, 
+                   0, 597, 578, 574, 577, 534, 499, 442, 376, 294, 212, 144, 48};
+
+# define DEBUG 1
+
+/* find region file which contains ra, dec */
+void aregion (GSCRegion *region, FILE *f, double ra, double dec) {
+  
+  char buffer[28800], temp[50], file[256];
+  double RA0, RA1, DEC0, DEC1;
+  int i, NBigDec, NLINES, done;
+  
+  while (ra < 0) { ra += 360.0; }
+  while (ra >= 360.0) { ra -= 360.0; }
+
+  if (dec >= 86.25) {
+    sprintf (file, "%s/n8230/pole.cpt", CATDIR);
+    region[0].DEC[0] = 86.25;
+    region[0].DEC[1] = 93.75;
+    region[0].RA[0] =  0.0;
+    region[0].RA[1] =  360.0;
+    strcpy (region[0].filename, file);
+    return;
+  }
+    
+  NBigDec = -1;
+  for (i = 0; i < 12; i++) {
+    if ((dec >= BigDecBounds[i]) && (dec < BigDecBounds[i+1])) {
+      NBigDec = i;
+      break;
+    }
+  }
+  if (NBigDec < 0) {
+    for (i = 13; i < 24; i++) {
+      if ((dec < BigDecBounds[i]) && (dec >= BigDecBounds[i+1])) {
+	NBigDec = i;
+	break;
+      }
+    }
+  }
+  if (NBigDec < 0) {
+    fprintf (stderr, "dec out of range: %f\n", dec);
+  }
+    
+  NLINES = 0;
+  for (i = 0; i < NBigDec; i++) {
+    NLINES += NDecLines[i];
+  }
+  fseek (f, 5*2880 + 48*NLINES, SEEK_SET);
+      
+  done = FALSE;
+  Fread (buffer, 1, 48*NDecLines[NBigDec], f, "char");
+  for (i = 0; !done && (i < NDecLines[NBigDec]); i++) {
+    strncpy (temp, &buffer[i*48], 48);
+    temp[49] = 0;
+    hms_to_deg (&RA0, &RA1, &DEC0, &DEC1, &temp[7]);
+    if (RA1 < RA0) RA1 += 360.0;
+    if ((dec >= 0) && (dec >= DEC0) && (dec < DEC1) && (ra >= RA0) && (ra < RA1)) {
+      done = TRUE;
+    }
+    if ((dec < 0) && (dec < DEC0) && (dec >= DEC1) && (ra >= RA0) && (ra < RA1)) {
+      done = TRUE;
+    }
+  }
+
+  if (!done) {
+    fprintf (stderr, "ERROR in search: %f %f\n", ra, dec);
+    exit (1);
+  }
+  temp[5] = 0;
+  sprintf (file, "%s/%s/%s.cpt", CATDIR, Dec2Sections[NBigDec], &temp[1]);
+  if (DEC0 < DEC1) {
+    region[0].DEC[0] = DEC0;
+    region[0].DEC[1] = DEC1;
+  } else {
+    region[0].DEC[0] = DEC1;
+    region[0].DEC[1] = DEC0;
+  }     
+  region[0].RA[0] = RA0;
+  region[0].RA[1] = RA1;
+  strcpy (region[0].filename, file);
+  return;
+}
+
+
+/**********/
+int hms_to_deg (h0, h1, d0, d1, string) 
+     char *string;
+     double *h0, *h1, *d0, *d1;
+{
+  
+  int flag_d0, flag_d1, flag_h0, flag_h1;
+  double tmp;
+  
+  *d0 = *h0 = *d1 = *h1 = 0;
+
+  flag_h0 = dparse (h0, 1, string);
+  flag_h1 = dparse (h1, 4, string);
+  flag_d0 = dparse (d0, 7, string);
+  flag_d1 = dparse (d1, 9, string);
+  *h0 *= flag_h0;
+  *h1 *= flag_h1;
+  *d0 *= flag_d0;
+  *d1 *= flag_d1;
+
+  dparse (&tmp, 2, string);
+  *h0 += tmp/60.0;
+  dparse (&tmp, 3, string);
+  *h0 += tmp/3600.0;
+  
+  dparse (&tmp, 5, string);
+  *h1 += tmp/60.0;
+  dparse (&tmp, 6, string);
+  *h1 += tmp/3600.0;
+  
+  dparse (&tmp, 8, string);
+  *d0 += tmp/60.0;
+
+  dparse (&tmp, 10, string);
+  *d1 += tmp/60.0;
+
+  *h0 *= 15*flag_h0;
+  *h1 *= 15*flag_h1;
+  *d0 *= flag_d0;
+  *d1 *= flag_d1;
+
+  return (TRUE);
+}
+
+
+/*
+      if (buffer[i*48 + 19] == ' ') continue;
+      strncpy (temp, &buffer[i*48 + 19], 20);       temp[20] = 0;
+      hms_to_deg (&RA,  &DEC, temp, ' ', 3);
+
+
+  dBigRA = 360.0 / (int)(0.5 + 48*cos(3.1415927*0.5*(BigDecBounds[NBigDec] + BigDecBounds[NBigDec + 1])/180.0));
+
+  NBigRA = ra / dBigRA;
+
+  NBig = NBigRA;
+  for (i = 0; (i < 12) && (i < NBigDec); i++) {
+    NBig += NBigRASections[i];
+  }
+  for (i = 13; (i < 24) && (i < NBigDec); i++) {
+    NBig += NBigRASections[i];
+  }
+
+
+  fprintf (stderr, "%d %d %d %d %f %f -> %f %f %f\n", NBigDec, NBigRA, NBigRASections[NBigDec], NBig, ra, dec, BigDecBounds[NBigDec], BigDecBounds[NBigDec + 1], dBigRA);
+*/
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/args.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/args.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/args.c	(revision 2549)
@@ -0,0 +1,113 @@
+# include "addstar.h"
+# define NARGS 2  /* minimum is: addstar (filename) */
+
+void help () {
+
+  fprintf (stderr, "USAGE: addstar (filename)\n");
+  fprintf (stderr, "  optional flags:\n");
+  fprintf (stderr, "  -v (verbose mode)\n");
+  fprintf (stderr, "  -p (photcode) - define the photcode\n");
+  fprintf (stderr, "\n"); 
+  exit (2);
+
+}
+
+int args (int argc, char **argv) {
+  
+  int N;
+
+  /* check for help request */
+  if (get_argument (argc, argv, "-help") ||
+      get_argument (argc, argv, "-h")) {
+    help ();
+  }
+
+  /* configuration info */
+  ConfigInit (&argc, argv);
+
+  /* check for command line options */
+  VERBOSE = FALSE;
+  if ((N = get_argument (argc, argv, "-v"))) {
+    VERBOSE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  FORCE = FALSE;
+  if ((N = get_argument (argc, argv, "-force"))) {
+    FORCE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  ACCEPT_ASTROM = FALSE;
+  if ((N = get_argument (argc, argv, "-accept"))) {
+    ACCEPT_ASTROM = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  thiscode = NULL;
+  if ((N = get_argument (argc, argv, "-p"))) {
+    remove_argument (N, &argc, argv);
+    thiscode = GetPhotcodebyName (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+  ONLY_IMAGES = FALSE;
+  if ((N = get_argument (argc, argv, "-image"))) {
+    ONLY_IMAGES = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  DUMP_MATCHES = FALSE;
+  if ((N = get_argument (argc, argv, "-dump"))) {
+    DUMP_MATCHES = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  SKIP_MISSED = FALSE;
+  if ((N = get_argument (argc, argv, "-missed"))) {
+    SKIP_MISSED = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  CALIBRATE = FALSE;
+  if ((N = get_argument (argc, argv, "-cal"))) {
+    CALIBRATE = TRUE;
+    remove_argument (N, &argc, argv);
+    CalReference = GetPhotcodeCodebyName (argv[N]);
+    if (!CalReference) {
+      fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", argv[N]);
+      exit (1);
+    }
+    remove_argument (N, &argc, argv);
+    CalColor = GetPhotcodeCodebyName (argv[N]);
+    if (!CalColor) {
+      fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", argv[N]);
+      exit (1);
+    }
+    remove_argument (N, &argc, argv);
+  }
+  if (DUMP_MATCHES && !CALIBRATE) {
+    fprintf (stderr, "-dump only valid -with -cal\n");
+    exit (1);
+  }
+
+  /*
+  Subpix.C0 = 0.06;
+  Subpix.C1 = -0.00015;
+  Subpix.P0 = 0.55;
+  if ((N = get_argument (argc, argv, "-subpix"))) {
+    remove_argument (N, &argc, argv);
+    Subpix.C0 = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    Subpix.C1 = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    Subpix.P0 = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+  */
+  return (TRUE);
+}
+
+
+/***
+
+ it is silly to define CalReference and CalColor on the command line
+ these are *already* define in the photcode table.  
+ CalColor1 = thiscode[0].c1
+ CalColor2 = thiscode[0].c2
+ CalReference = thiscode[0].equiv
+
+***/
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/calibrate.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/calibrate.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/calibrate.c	(revision 2549)
@@ -0,0 +1,166 @@
+# include "addstar.h"
+
+static int Ncal, NCAL;
+static float *Mobs, *dMobs, *Mref, *dMref, *Cref, *Airm;
+static float *RA, *DEC, *X, *Y;
+int *Nstar;
+
+void InitCalibration () {
+
+    fprintf (stderr, "calibrating the image...  %d\n", CalReference);
+    Ncal = 0;
+    NCAL = 1000;
+    ALLOCATE (Mobs,  float,  NCAL);
+    ALLOCATE (dMobs, float, NCAL);
+    ALLOCATE (Mref,  float,  NCAL);
+    ALLOCATE (dMref,  float,  NCAL);
+    ALLOCATE (Cref,  float,  NCAL);
+    ALLOCATE (Airm,  float,  NCAL);
+    ALLOCATE (Nstar, int, NCAL);
+    ALLOCATE (RA, float, NCAL);
+    ALLOCATE (DEC, float, NCAL);
+    ALLOCATE (X, float, NCAL);
+    ALLOCATE (Y, float, NCAL);
+}
+  
+void SaveCalibration (float M, float dM, float Mr, float dMr, float Mc, float A, int N, float ra, float dec, float x, float y) {
+
+  Mobs[Ncal] = M;
+  dMobs[Ncal] = dM;
+  Mref[Ncal] = Mr;
+  dMref[Ncal] = dMr;
+  Cref[Ncal] = Mc;
+  Airm[Ncal] = A;
+  Nstar[Ncal] = N;
+
+  RA[Ncal] = ra;
+  DEC[Ncal] = dec;
+  X[Ncal] = x;
+  Y[Ncal] = y;
+  Ncal ++;
+
+  if (Ncal == NCAL) {
+    NCAL += 1000;
+    REALLOCATE (dMobs, float, NCAL);
+    REALLOCATE (Mobs,  float, NCAL);
+    REALLOCATE (Mref,  float, NCAL);
+    REALLOCATE (dMref,  float, NCAL);
+    REALLOCATE (Cref,  float, NCAL);
+    REALLOCATE (Airm,  float, NCAL);
+    REALLOCATE (Nstar, int,   NCAL);
+    REALLOCATE (RA, float, NCAL);
+    REALLOCATE (DEC, float, NCAL);
+    REALLOCATE (X, float, NCAL);
+    REALLOCATE (Y, float, NCAL);
+  }
+}
+
+void FindCalibration (Image *image) {
+
+  int i, MaxN, *Nlist, Nkeep;
+  float N, M1, M2, Klam, Clam, Xlam, Mabs, *Dmag, *dDmag;
+  float dMo, dMr, Mw, Dmed, W1, W2, NSigma;
+  
+  /* reject multiple matched-stars */
+  /* find maximum value of Nstar[] */
+  MaxN = -1;
+  for (i = 0; i < Ncal; i++) {
+    MaxN = MAX (Nstar[i], MaxN);
+  }
+  if (MaxN == -1) {
+    fprintf (stderr, "no clean stars\n");
+    image[0].Mcal = 10000;
+    image[0].dMcal = 10000;
+    return;
+  }
+  /* create a hash array from Nstar[] entries */
+  ALLOCATE (Nlist, int, MAX (1, MaxN + 1));
+  bzero (Nlist, MAX (0, MaxN*sizeof(int)));
+  for (i = 0; i < Ncal; i++) {
+    Nlist[Nstar[i]] ++;
+  }
+  
+  /* accumulate delta mags */
+  ALLOCATE (Dmag, float, Ncal);
+  ALLOCATE (dDmag, float, Ncal);
+  Nkeep = 0;
+  Klam = thiscode[0].K;
+  Clam = thiscode[0].C;
+  Xlam = thiscode[0].X[0];
+  for (i = 0; i < Ncal; i++) {
+    /* if this entry has too many (or two few?) matches, skip it */
+    if (Nlist[Nstar[i]] != 1) continue;
+    Mabs = 0.001*(Mobs[i] + Klam*(Airm[i] - 1000) + Clam + Xlam*(Mref[i] - Cref[i])) - ZeroPt;
+
+    /* note: subpix correction is applied in gstars */
+
+    if (DUMP_MATCHES) 
+      fprintf (stdout, "%d  %6.3f %6.3f %6.3f  %10.6f %10.6f  %7.2f %7.2f  %7.2f  %7.2f\n", 
+	       i, Mabs, 0.001*Mref[i], 0.001*Cref[i], RA[i], DEC[i], X[i], Y[i], Airm[i], 0.001*dMobs[i]);
+
+    /* skip stars brighter than 8.0 */
+    if (Mabs > 9.0) continue;
+    if (Mabs < 5.0) continue;
+    
+    dMr = MAX (0.005, 0.001*dMref[i]);
+    dMo = MAX (0.005, 0.001*dMobs[i]);
+      
+    Dmag[Nkeep]  = (Mabs - 0.001*Mref[i]);
+    dDmag[Nkeep] = (dMr*dMr + dMo*dMo);
+    Nkeep ++;
+  }
+
+  if (Nkeep < 5) {
+    fprintf (stderr, "too few stars\n");
+    image[0].Mcal = 10000;
+    image[0].dMcal = 10000;
+    return;
+  }
+  fsort2 (Dmag, dDmag, Nkeep);
+
+  /* take sort list of Dmag, find median */
+  Dmed = Dmag[(int)(0.5*Nkeep)];
+
+  /* exclude points with abs(Dmag - Dmed) / dDmag > 2.5 */
+
+  /* find lo (Dmed - 0.1) + hi (Dmed + 0.1) 
+  lo = -1; hi = -1;
+  for (i = 0; (lo == -1) && (i < Nkeep); i++) { if (Dmed - Dmag[i] < 0.1) lo = i; }
+  for (i = Nkeep - 1; (hi == -1) && (i >= 0); i--) { if (Dmag[i] - Dmed < 0.1) hi = i; }
+  if (lo == -1) lo = 0;
+  if (hi == -1) hi = Nkeep - 1;
+  */
+
+  /* accumulate delta mags (25% - 75% of clipped range) */
+  W1 = 0.0;
+  W2 = 0.0;
+  M1 = 0.0;
+  M2 = 0.0;
+  N  = 0.0;
+  for (i = 0; i < Nkeep; i++) {
+    NSigma = fabs (Dmag[i] - Dmed) / sqrt (dDmag[i]);
+    if (NSigma > 2.5) continue;
+    W1 += Dmag[i] / dDmag[i];
+    W2 += 1 / dDmag[i];
+    M1 += Dmag[i];
+    M2 += SQ (Dmag[i]);
+    N  += 1.0; 
+  }
+
+  if (N > 1) {
+    M1 = M1 / N;
+    M2 = sqrt (fabs(M2/N - M1*M1));
+    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].Mxxxx = N;
+  } else {
+    fprintf (stderr, "too few stars\n");
+    image[0].Mcal = 10000;
+    image[0].dMcal = 10000;
+    image[0].Mxxxx = 0;
+  }
+}
+
+
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/check_permissions.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/check_permissions.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/check_permissions.c	(revision 2549)
@@ -0,0 +1,90 @@
+# include "addstar.h"
+
+void check_permissions (char *basefile) {
+  
+  char *c, dir[256], filename[256];
+  struct stat filestat;
+  uid_t uid;
+  gid_t gid;
+  int status, cmode;
+
+  uid = getuid();
+  gid = getgid();
+
+  /* check permission to write to directory */
+  sprintf (filename, "%s", basefile);
+  c = strrchr (filename, '/');
+  if (c == (char *) NULL) {
+    strcpy (dir, ".");
+  } else {
+    *c = 0;
+    strcpy (dir, filename);
+  }
+  status = stat (dir, &filestat);
+  if (status == -1) {
+    fprintf (stderr, "directory %s does not exist, creating...\n", dir);
+    cmode = S_IRWXU | S_IRWXG | S_IRWXO;
+    status = mkdir (dir, cmode);
+    if (status == -1) {
+      fprintf (stderr, "ERROR: can't create %s\n", dir);
+      exit (1);
+    }
+  } 
+  status = stat (dir, &filestat);
+  if (((uid == filestat.st_uid) && (filestat.st_mode & S_IRWXU)) ||
+      ((gid == filestat.st_gid) && (filestat.st_mode & S_IRWXG)) || 
+      (filestat.st_mode & S_IRWXO)) {
+  } else {
+    fprintf (stderr, "ERROR: can't write to %s\n", dir);
+    exit (1);
+  }
+  
+  /* check permission to write to file */
+  sprintf (filename, "%s", basefile);
+  status = stat (filename, &filestat);
+  if (status == 0) { /* file exists, are permissions OK? */
+    if (((uid == filestat.st_uid) && (filestat.st_mode & S_IRUSR) && (filestat.st_mode & S_IWUSR)) ||
+	((gid == filestat.st_gid) && (filestat.st_mode & S_IRGRP) && (filestat.st_mode & S_IWGRP)) || 
+	((filestat.st_mode & S_IROTH) && (filestat.st_mode & S_IWOTH))) {
+    } else {
+      fprintf (stderr, "ERROR: can't write to %s\n", filename);
+      exit (1);
+    }
+  }
+  
+  /* check permission to write to backup file */
+  sprintf (filename, "%s~", basefile);
+  status = stat (filename, &filestat);
+  if (status == 0) { /* file exists, are permissions OK? */
+    if (((uid == filestat.st_uid) && (filestat.st_mode & S_IRUSR) && (filestat.st_mode & S_IWUSR)) ||
+	((gid == filestat.st_gid) && (filestat.st_mode & S_IRGRP) && (filestat.st_mode & S_IWGRP)) || 
+	((filestat.st_mode & S_IROTH) && (filestat.st_mode & S_IWOTH))) {
+    } else {
+      fprintf (stderr, "ERROR: can't write to %s\n", filename);
+      exit (1);
+    }
+  }
+  
+}
+
+/* uses cp only */
+void make_backup (char *filename) {
+
+  int status, cmode;
+  struct stat filestat;
+  char line [256];
+
+  status = stat (filename, &filestat);
+  if (status == 0) { /* file exists, make backup copy */
+    sprintf (line, "cp %s %s~", filename, filename);
+    status = system (line);
+    if (status) {
+      fprintf (stderr, "ERROR: unable to create %s~, exiting\n", filename);
+      exit (1);
+    }
+    cmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
+    sprintf (line, "%s~", filename);
+    chmod (line, cmode);
+  }
+  
+}
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/find_matches.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/find_matches.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/find_matches.c	(revision 2549)
@@ -0,0 +1,519 @@
+# include "addstar.h"
+
+# define IN_CATALOG(R,D) ( \
+((D) >= region[0].DEC[0]) && ((D) < region[0].DEC[1]) && \
+((R) >= region[0].RA[0])  && ((R) < region[0].RA[1]))
+
+void find_matches (GSCRegion *region, Stars *stars, int Nstars, Catalog *catalog, Image *image, Image *pimage, int Nimage) {
+
+  double secz;
+  int i, j, k, n, m, N, first_j, found, found0, found1;
+  double X, Y, RADIUS, RADIUS2;
+  float *X1, *Y1, *X2, *Y2, CalM, CalC, dCalM, *Xs, *Ys;
+  float dX, dY, dR;
+  int *N1, *N2,  *next, *next_miss, last, last_miss;
+  int Nave, NAVE, Nmeas, NMEAS, Nmiss, NMISS, Nmatch;
+  Measure *tmpmeasure;
+  Missing *tmpmissing;
+  Coords tcoords;
+  int Nsecfilt, Nsec, MTIME;
+  short int Mrel, *Mval;
+
+  /** allocate local arrays **/
+  ALLOCATE (X1, float, Nstars);
+  ALLOCATE (Y1, float, Nstars);
+  ALLOCATE (N1, int, Nstars);
+
+  /** allocate local arrays **/
+
+  Nsecfilt = GetPhotcodeNsecfilt ();
+
+  Nave = catalog[0].Naverage;
+  NAVE = Nave + 1000;
+  ALLOCATE (X2, float, NAVE);
+  ALLOCATE (Y2, float, NAVE);
+  ALLOCATE (N2, int, NAVE);
+  ALLOCATE (Xs, float, NAVE);
+  ALLOCATE (Ys, float, NAVE);
+  ALLOCATE (catalog[0].found, int, NAVE);
+  REALLOCATE (catalog[0].average, Average, NAVE);
+  REALLOCATE (catalog[0].secfilt, SecFilt, NAVE*catalog[0].Nsecfilt);
+  /* for secfilt j and star i, secfilt[i*Nsecfilt+j] */
+
+  Nmatch = 0;
+  Nmeas = catalog[0].Nmeasure;
+  NMEAS = Nmeas + 1000;
+  ALLOCATE (next, int, NMEAS);
+  REALLOCATE (catalog[0].measure, Measure, NMEAS);
+  
+  Nmiss = catalog[0].Nmissing;
+  NMISS = Nmiss + 1000;
+  ALLOCATE (next_miss, int, NMISS);
+  REALLOCATE (catalog[0].missing, Missing, NMISS);
+  
+  /* check on photcode */
+  Nsec = GetPhotcodeNsec (thiscode[0].code);
+
+  /* project onto rectilinear grid with 1 arcsec pixels, sort by X */
+  /* reference for coords is this image */
+  tcoords = image[0].coords;
+  tcoords.cdelt1 = tcoords.cdelt2 = 1.0 / 3600.0;
+  tcoords.pc1_1 = tcoords.pc2_2 = 1.0;
+  tcoords.pc1_2 = tcoords.pc2_1 = 0.0;
+  strcpy (tcoords.ctype, "RA---TAN");
+  
+  for (i = 0; i < Nstars; i++) {
+    fRD_to_XY (&X1[i], &Y1[i], stars[i].R, stars[i].D, &tcoords);
+    N1[i] = i;
+  }
+  if (Nstars > 1) sort_lists (X1, Y1, N1, Nstars);
+  
+  for (i = 0; i < Nave; i++) {
+    fRD_to_XY (&Xs[i], &Ys[i], catalog[0].average[i].R, catalog[0].average[i].D, &image[0].coords);
+    fRD_to_XY (&X2[i], &Y2[i], catalog[0].average[i].R, catalog[0].average[i].D, &tcoords);
+    N2[i] = i;
+    catalog[0].found[N2[i]] = -1;
+  }
+  if (Nave > 1) sort_lists (X2, Y2, N2, Nave);
+
+  /* set up pointers for linked list of measurements */
+  for (i = 0; i < Nmeas - 1; i++) {
+    next[i] = i+1;
+  }
+  next[i] = -1;
+  last = i;
+
+  for (i = 0; i < Nmiss - 1; i++) {
+    next_miss[i] = i+1;
+  }
+  next_miss[i] = -1;
+  last_miss = i;
+
+  /* choose a radius for matches */
+  /* 0.02 corrects cerror to arcsec from storage units */
+  if (DEFAULT_RADIUS == 0) {
+    RADIUS = NSIGMA * 0.02 * image[0].cerror;
+  } else {
+    RADIUS = DEFAULT_RADIUS;
+  }
+  RADIUS2 = RADIUS*RADIUS;
+  /* correct instrumental mags for exposure time */
+  if (image[0].exptime > 0) {
+    MTIME = 2500*log10(image[0].exptime);
+  } else {
+    MTIME = 0;
+  }
+
+  /** find matched stars **/
+  for (i = j = 0; (i < Nstars) && (j < Nave); ) {
+    
+    dX = X1[i] - X2[j];
+    /* fprintf (stderr, "%f   %f %f  %f %f    %f %f  %f %f\n", dX, X1[i], Y1[i], X2[j], Y2[j], stars[i].R, stars[i].D, catalog[0].average[j].R, catalog[0].average[j].D); */
+
+    if (dX <= -2*RADIUS) {
+      i++;
+      continue;
+    }
+    if (dX >= 2*RADIUS) {
+      j++;
+      continue;
+    }
+
+    /* negative dX: j is too large, positive dX, i is too large */
+    first_j = j;
+    for (; (dX > -2*RADIUS) && (j < Nave); j++) {
+      dX = X1[i] - X2[j];
+      dY = Y1[i] - Y2[j];
+      dR = dX*dX + dY*dY;
+      if (dR < RADIUS2) {  /* new measurement of this star */
+	Nmatch ++;
+	/** insert star in measurement list */
+	n = N2[j];
+	N = N1[i];
+	m = catalog[0].average[n].offset;  /* first measurement of this star */
+	for (k = 0; k < catalog[0].average[n].Nm - 1; k++)
+	  m = next[m];
+	next[Nmeas] = next[m];
+	next[m] = Nmeas;
+	if (next[Nmeas] == -1) { /* last just was moved */
+	  last = Nmeas;
+	}
+	
+	/* calculate accurate per-star airmass */
+	secz = 1000 * airmass (stars[N].R, stars[N].D, SiderealTime, Latitude);
+	if (CALIBRATE) {
+	  found = FALSE;
+	  found0 = FALSE;
+	  found1 = FALSE;
+	  m = catalog[0].average[n].offset;  /* first measurement of this star */
+	  for (k = 0; !found && (k < catalog[0].average[n].Nm); k++) {
+	    if (catalog[0].measure[m+k].source == CalReference) { found0 = TRUE; CalM = catalog[0].measure[m+k].M; dCalM = catalog[0].measure[m+k].dM; }
+	    if (catalog[0].measure[m+k].source == CalColor)     { found1 = TRUE; CalC = catalog[0].measure[m+k].M; }
+	    if (found0 && found1) {
+	      found = TRUE;
+	      SaveCalibration (MIN (NO_MAG, 1000*stars[N].M + MTIME), 
+			       MIN (255, stars[N].dM),
+			       CalM, dCalM, CalC,
+			       secz, N, stars[N].R, stars[N].D, Xs[n], Ys[n]);
+	    }
+	  }
+	}
+	      
+	/** add measurements for this star **/
+	catalog[0].measure[Nmeas].dR     = 360000.0*(catalog[0].average[n].R - stars[N].R);
+	catalog[0].measure[Nmeas].dD     = 360000.0*(catalog[0].average[n].D - stars[N].D);
+	catalog[0].measure[Nmeas].M      = MIN (NO_MAG, 1000*stars[N].M + MTIME);
+	catalog[0].measure[Nmeas].Mcal   = image[0].Mcal;
+	catalog[0].measure[Nmeas].dM     = MIN (255, stars[N].dM);  /* error in input files stored in thousandths of mag */
+	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;
+	catalog[0].measure[Nmeas].source = image[0].source;  /* photometry source */
+	catalog[0].measure[Nmeas].dophot = stars[N].dophot;  
+	catalog[0].measure[Nmeas].flags  = 0;
+	catalog[0].measure[Nmeas].dt     = MTIME;
+
+	catalog[0].measure[Nmeas].Mgal     = MIN (NO_MAG, 1000*stars[N].Mgal + MTIME);
+	catalog[0].measure[Nmeas].airmass  = secz;
+	catalog[0].measure[Nmeas].FWx      = MIN (NO_MAG, 100*stars[N].fx);
+	catalog[0].measure[Nmeas].fwy      = MIN (255, 100*(stars[N].fy / stars[N].fx));
+	catalog[0].measure[Nmeas].theta    = MIN (255, (255/360)*stars[N].df);
+	/* refers to same number as first measurement */
+	
+	/* check for entries in the secfilt lists */
+	Mrel = catalog[0].measure[Nmeas].M + catalog[0].measure[Nmeas].airmass * thiscode[0].K;
+	Mval = (Nsec == -1) ? &catalog[0].average[n].M : &catalog[0].secfilt[n*Nsecfilt+Nsec].M;
+	if (*Mval == NO_MAG) *Mval = Mrel;
+
+	/*** handle multiple stars */
+	/* this image star matches more than one catalog star */
+	if (stars[N].found > -1) {
+	  catalog[0].measure[stars[N].found].flags |= BLEND_IMAGE;
+	  catalog[0].measure[Nmeas].flags |= BLEND_IMAGE;
+	} 
+	if (stars[N].found == -2) { /* this image star matches a catalog star on a neighboring catalog */
+	  catalog[0].measure[Nmeas].flags |= BLEND_IMAGE_NEIGHBOR;
+	} 
+	if (stars[N].found == -1) { /* this image star matches only this catalog star */
+	  stars[N].found = Nmeas;  /* save first match, in case coincidences are found */
+	}
+	/* this catalog star matches more than one image star */
+	if (catalog[0].found[n] > -1) {
+	  catalog[0].measure[catalog[0].found[n]].flags |= BLEND_CATALOG;
+	  catalog[0].measure[Nmeas].flags |= BLEND_CATALOG;
+	} else {
+	  catalog[0].found[n] = Nmeas;
+	}
+
+	catalog[0].average[n].Nm ++;
+	Nmeas ++;
+	if (Nmeas == NMEAS) {
+	  NMEAS = Nmeas + 1000;
+	  REALLOCATE (next, int, NMEAS);
+	  REALLOCATE (catalog[0].measure, Measure, NMEAS);
+	}
+
+	/* update values of Ro, Do */
+	if (catalog[0].average[n].Nm > 2) {
+	  double R, D, r, d, r2, d2, Npt;
+	  Npt = r = d = r2 = d2 = 0;
+	  m = catalog[0].average[n].offset;  /* first measurement of this star */
+	  for (k = 0; k < catalog[0].average[n].Nm; k++) {
+	    if (catalog[0].measure[m].t == 0) {
+	      m = next[m];
+	      continue;
+	    }
+	    R = catalog[0].measure[m].dR;
+	    D = catalog[0].measure[m].dD;
+	    r += R;
+	    d += D;
+	    r2 += R*R;
+	    d2 += D*D;
+	    m = next[m];
+	    Npt += 1.0;
+	  }
+	  if (Npt > 2) {
+	    r = r / Npt;  /* these are corrections in 1/100 arcsec to RA and DEC */
+	    d = d / Npt;
+	    R = r2 / Npt - r*r;
+	    D = d2 / Npt - d*d;
+	    /* Xp is scatter in position in hundredths of arcsec */
+	    catalog[0].average[n].Xp = sqrt (D + R / SQ(cos(catalog[0].average[n].D*RAD_DEG)));
+	    m = catalog[0].average[n].offset;  /* first measurement of this star */
+	    for (k = 0; k < catalog[0].average[n].Nm; k++) {
+	      catalog[0].measure[m].dR = catalog[0].measure[m].dR - r;
+	      catalog[0].measure[m].dD = catalog[0].measure[m].dD - d;
+	      m = next[m];
+	    }
+	    catalog[0].average[n].R = catalog[0].average[n].R - r / 360000.0;
+	    catalog[0].average[n].D = catalog[0].average[n].D - d / 360000.0;
+	  }
+	}
+      }
+    }
+    j = first_j;
+    i++;
+  }
+
+  /* add reference for undetected catalog stars */
+  for (j = 0; (j < Nave) && !SKIP_MISSED; j++) {
+    n = N2[j];
+    if (catalog[0].found[n] < 0) { 
+      /* should the catalog star be on this image? project into image coords */
+      if (in_image (catalog[0].average[n].R, catalog[0].average[n].D, image)) {
+	/* find last missing meas */
+	if (catalog[0].average[n].Nn < 1) { /* no previous missing obs */
+	  catalog[0].average[n].missing = Nmiss;
+	  next_miss[last_miss] = Nmiss;
+	  next_miss[Nmiss] = -1;
+	  last_miss = Nmiss;
+	} else {
+	  m = catalog[0].average[n].missing;
+	  for (k = 0; k < catalog[0].average[n].Nn - 1; k++)
+	    m = next_miss[m];
+	  next_miss[Nmiss] = next_miss[m];
+	  next_miss[m] = Nmiss;
+	  if (next_miss[Nmiss] == -1) { /* last just was moved */
+	    last_miss = Nmiss;
+	  }
+	}
+	RD_to_XY (&X, &Y, catalog[0].average[n].R, catalog[0].average[n].D, &image[0].coords);	  
+	catalog[0].missing[Nmiss].t  = image[0].tzero + 1e-4*Y*image[0].trate;  /* trate is in 0.1 msec / row */
+	catalog[0].average[n].Nn ++;
+	Nmiss ++;
+	if (Nmiss == NMISS) {
+	  NMISS = Nmiss + 1000;
+	  REALLOCATE (next_miss, int, NMISS);
+	  REALLOCATE (catalog[0].missing, Missing, NMISS);
+	}
+      }
+    }
+  }
+
+  /* incorporate unmatched image stars, if this star is in field of this catalog */
+  for (i = 0; i < Nstars; i++) {
+    N = N1[i];
+    if ((stars[N].found < 0) && IN_CATALOG (stars[N].R, stars[N].D)) {
+      catalog[0].average[Nave].R = stars[N].R;
+      catalog[0].average[Nave].D = stars[N].D;
+      catalog[0].average[Nave].M = NO_MAG;
+      for (j = 0; j < Nsecfilt; j++) {
+	catalog[0].secfilt[Nave*Nsecfilt+j].M  = NO_MAG;
+	catalog[0].secfilt[Nave*Nsecfilt+j].Xm = NO_MAG;
+      }
+      
+      secz = 1000 * airmass (stars[N].R, stars[N].D, SiderealTime, Latitude);
+
+      catalog[0].average[Nave].Nm        = 1;
+      catalog[0].average[Nave].Nn        = 0;
+      catalog[0].average[Nave].Xp        = NO_MAG;
+      catalog[0].average[Nave].Xm        = NO_MAG;
+      catalog[0].average[Nave].dM        = NO_MAG;
+      catalog[0].average[Nave].offset    = Nmeas;
+      catalog[0].average[Nave].missing   = -1;
+      catalog[0].average[Nave].code      = 0;
+
+      catalog[0].measure[Nmeas].dR       = 0.0;
+      catalog[0].measure[Nmeas].dD       = 0.0;
+      catalog[0].measure[Nmeas].M        = MIN (NO_MAG, 1000.0*stars[N].M + MTIME);
+      catalog[0].measure[Nmeas].Mcal     = image[0].Mcal;
+      catalog[0].measure[Nmeas].dM       = MIN (255, stars[N].dM);
+      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;
+      catalog[0].measure[Nmeas].source   = image[0].source;  /* photometry source */
+      catalog[0].measure[Nmeas].dophot   = stars[N].dophot;  
+      catalog[0].measure[Nmeas].flags    = 0;
+      catalog[0].measure[Nmeas].dt       = MTIME;
+
+      catalog[0].measure[Nmeas].Mgal     = MIN (NO_MAG, 1000.0*stars[N].Mgal + MTIME);
+      catalog[0].measure[Nmeas].airmass  = secz;
+      catalog[0].measure[Nmeas].FWx      = MIN (0x7fff, 100*stars[N].fx);
+      catalog[0].measure[Nmeas].fwy      = MIN (255, 100*(stars[N].fy / stars[N].fx));
+      catalog[0].measure[Nmeas].theta    = MIN (255, (255/360)*stars[N].df);
+
+      Mrel = catalog[0].measure[Nmeas].M + catalog[0].measure[Nmeas].airmass * thiscode[0].K;
+      Mval = (Nsec == -1) ? &catalog[0].average[Nave].M : &catalog[0].secfilt[Nave*Nsecfilt+Nsec].M;
+      if (*Mval == NO_MAG) *Mval = Mrel;
+
+      stars[N].found = Nmeas;
+      next[last] = Nmeas;
+      next[Nmeas] = -1;
+      last = Nmeas;
+      Nmeas ++;
+      if (Nmeas == NMEAS) {
+	NMEAS = Nmeas + 1000;
+	REALLOCATE (next, int, NMEAS);
+	REALLOCATE (catalog[0].measure, Measure, NMEAS);
+      }
+
+      /** now add references from all previous non-detection observations of this spot on the sky */
+      for (j = 0; (j < Nimage) && !SKIP_MISSED; j++) {
+	if (in_image (catalog[0].average[Nave].R, catalog[0].average[Nave].D, &pimage[j])) {
+	  if (catalog[0].average[Nave].Nn < 1) {
+	    catalog[0].average[Nave].missing = Nmiss;
+	  }
+	  next_miss[last_miss] = Nmiss;
+	  next_miss[Nmiss] = -1;
+	  last_miss = Nmiss;
+	  /* this can now be done exactly */
+	  RD_to_XY (&X, &Y, catalog[0].average[Nave].R, catalog[0].average[Nave].D, &pimage[j].coords);	  
+	  catalog[0].missing[Nmiss].t  = pimage[j].tzero + 1e-4*Y*pimage[j].trate;  /* rough guess at time */
+	  catalog[0].average[Nave].Nn ++;
+	  Nmiss ++;
+	  if (Nmiss == NMISS) {
+	    NMISS = Nmiss + 1000;
+	    REALLOCATE (next_miss, int, NMISS);
+	    REALLOCATE (catalog[0].missing, Missing, NMISS);
+	  }
+  
+	}
+      }
+      Nave ++;
+      if (Nave == NAVE) {
+	NAVE = Nave + 1000;
+	REALLOCATE (catalog[0].average, Average, NAVE);
+	REALLOCATE (catalog[0].secfilt, SecFilt, NAVE*catalog[0].Nsecfilt);
+      }
+    }
+  }
+      
+  free (catalog[0].found);
+  REALLOCATE (catalog[0].average, Average, Nave);
+  REALLOCATE (catalog[0].measure, Measure, Nmeas);
+
+  /* fix order of Measure (memory intensive, but fast) */
+  N = 0; 
+  ALLOCATE (tmpmeasure, Measure, Nmeas);
+  for (i = 0; i < Nave; i++) {
+    n = catalog[0].average[i].offset;
+    catalog[0].average[i].offset = N;
+    for (k = 0; k < catalog[0].average[i].Nm; k++, N++) {
+      tmpmeasure[N] = catalog[0].measure[n]; 
+      tmpmeasure[N].averef = i;
+      n = next[n];
+    }
+  }
+  free (catalog[0].measure);
+  catalog[0].measure = tmpmeasure;
+    
+  /* fix order of Missing (memory intensive, but fast) */
+  N = 0; 
+  ALLOCATE (tmpmissing, Missing, Nmiss);
+  for (i = 0; i < Nave; i++) {
+    if (catalog[0].average[i].Nn > 0) {
+      n = catalog[0].average[i].missing;
+      catalog[0].average[i].missing = N;
+      for (k = 0; k < catalog[0].average[i].Nn; k++, N++) {
+	tmpmissing[N] = catalog[0].missing[n]; 
+	n = next_miss[n];
+      }
+    }
+  }
+  free (catalog[0].missing);
+  catalog[0].missing = tmpmissing;
+
+  /* note stars which have been found in this catalog */
+  for (i = 0; i < Nstars; i++) {
+    if (stars[i].found > -1) {
+      stars[i].found = -2;
+    }
+  }
+
+  catalog[0].Naverage = Nave;
+  catalog[0].Nmeasure = Nmeas;
+  catalog[0].Nmissing = Nmiss;
+  if (VERBOSE) fprintf (stderr, "Nstars, Nave, Nmeas, Nmiss: %d %d %d %d, (%d matches)\n", Nstars, Nave, Nmeas, Nmiss, Nmatch);
+}
+
+int in_image (double r, double d, Image *image) {
+
+  double X, Y;
+
+  RD_to_XY (&X, &Y, r, d, &image[0].coords);
+  if (((X) >= 0) && ((X) < image[0].NX) && ((Y) >= 0) && ((Y) < image[0].NY))
+    return (TRUE);
+  else 
+    return (FALSE);
+
+}
+
+/* 
+   notes:
+
+     for finding if a catalog star is in an image or an image star is in the catalog:
+       
+       catalogs have boundaries defined by RA and DEC, but they may curve in projection
+       images have boundaries which are lines in pixels coords, but curve in RA and DEC
+
+       catalog[0].found[Ncat] but stars[Nstar].found
+
+       */
+
+     
+# ifdef OLDJUNKHERE
+
+ 
+  /* fix measurement list order */
+  for (i = 0; i < Nmeas - 1; i++) {
+    if (next[i] != i + 1) {
+      n0 = next[i];
+      n1 = next[i+1];
+      n2 = next[n0];
+      t = i;
+      while ((t != -1) && (next[t] != i+1))
+	t = next[t];
+      if ((t == -1) || (n2 == i+1)) {
+	t = n2;
+      }
+      next[i] = i+1;
+      next[i+1] = n2;
+      next[n0] = n1;
+      next[t] = n0;
+      tmpmeasure = catalog[0].measure[n0];
+      catalog[0].measure[n0] = catalog[0].measure[i+1];
+      catalog[0].measure[i+1] = tmpmeasure;
+    }
+  }
+ 
+  /* fix offset references */
+  n = (0x00ffffff & catalog[0].measure[0].average);
+  catalog[0].average[n].offset = 0;
+  for (i = 1; i < Nmeas; i++) {
+    if (n != (0x00ffffff & catalog[0].measure[i].average)) {
+      n = (0x00ffffff & catalog[0].measure[i].average);
+      catalog[0].average[n].offset = i;
+    }
+  }
+
+  /* fix missing list order */
+  for (i = 0; i < Nmiss - 1; i++) {
+    if (next_miss[i] != i + 1) {
+      n0 = next_miss[i];
+      n1 = next_miss[i+1];
+      n2 = next_miss[n0];
+      t = i;
+      while ((t != -1) && (next_miss[t] != i+1))
+	t = next_miss[t];
+      if ((t == -1) || (n2 == i+1)) {
+	t = n2;
+      }
+      next_miss[i] = i+1;
+      next_miss[i+1] = n2;
+      next_miss[n0] = n1;
+      next_miss[t] = n0;
+      tmpmissing = catalog[0].missing[n0];
+      catalog[0].missing[n0] = catalog[0].missing[i+1];
+      catalog[0].missing[i+1] = tmpmissing;
+    }
+  }
+ 
+  /* fix offset references */
+  n = (0x00ffffff & catalog[0].measure[0].average);
+  catalog[0].average[n].offset = 0;
+  for (i = 1; i < Nmeas; i++) {
+    if (n != (0x00ffffff & catalog[0].measure[i].average)) {
+      n = (0x00ffffff & catalog[0].measure[i].average);
+      catalog[0].average[n].offset = i;
+    }
+  }
+
+# endif
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/gcatalog.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/gcatalog.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/gcatalog.c	(revision 2549)
@@ -0,0 +1,45 @@
+# include "addstar.h"
+
+int gcatalog (Catalog *catalog) {
+  
+  int Nsecfilt;
+  char mode;
+
+  /* read catalog header */
+  mode = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
+  if (!load_catalog (catalog, mode, VERBOSE)) {
+    fprintf (stderr, "ERROR: failure loading catalog\n");
+    exit (1);
+  }
+
+  /* check Nsecfile value, update if needed */
+  Nsecfilt = GetPhotcodeNsecfilt ();
+  if (catalog[0].Nsecfilt < Nsecfilt) {
+
+    int i, j, Nextra, in, out;
+    SecFilt *insec, *outsec;
+
+    Nextra = Nsecfilt - catalog[0].Nsecfilt;
+    insec = catalog[0].secfilt;
+    ALLOCATE (outsec, SecFilt, catalog[0].Naverage * Nsecfilt);
+    for (in = out = i = 0; i < catalog[0].Naverage; i++) {
+      for (j = 0; j < catalog[0].Nsecfilt; j++, in++, out++) {
+	outsec[out].M = insec[in].M;
+	outsec[out].Xm = insec[in].Xm;
+      }
+      for (j = 0; j < Nextra; j++, out++) {
+	outsec[out].M = NO_MAG;
+	outsec[out].Xm = NO_MAG;
+      }
+    }
+    free (catalog[0].secfilt);
+    catalog[0].secfilt = outsec;
+    catalog[0].Nsecfilt = Nsecfilt;
+  }
+
+  if (catalog[0].Nsecfilt > Nsecfilt) {
+    fprintf (stderr, "ERROR: can't reduce number of secondary filters\n");
+    exit (1);
+  }
+  return (TRUE);
+}
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/gimages.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/gimages.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/gimages.c	(revision 2549)
@@ -0,0 +1,180 @@
+# include "addstar.h"
+
+Image *gimages (FILE *f, Image *image, int *Npimage) {
+  
+  int i, j, k, Nimage, addtolist, size;
+  int NTIMAGE, Ntimage, ntimage;
+  int NPIMAGE, npimage;
+  Image *timage, *pimage;
+  Coords tcoords;
+  Header header;
+  double r, d;
+  double Xi[5], Yi[5], Xo[5], Yo[5];  /* image and original corners */
+  double zeropt;
+  struct stat filestatus;
+  
+  fseek (f, 0, SEEK_SET);
+
+  /* read header */
+  if (!fits_load_header (f, &header)) {
+    if (VERBOSE) fprintf (stderr, "can't read image catalog %s\n", ImageCat);
+    Shutdown ();
+  }
+
+  fits_scan (&header, "ZERO_PT", "%lf", 1, &zeropt);
+  if (fabs (ZeroPt - zeropt) > 1e-4) {
+    fprintf (stderr, "zero point in image (%f:%s) inconsistent with zero point in catalog (%f)\n",
+	     ZeroPt, image[0].name, zeropt);
+    Shutdown ();
+  }
+
+  /* project onto rectilinear grid with 1 arcsec pixels */
+  /* we keep the original crpix1,2 and crref1,2 */
+  tcoords = image[0].coords;
+  tcoords.cdelt1 = tcoords.cdelt2 = 1.0 / 3600.0;
+  tcoords.pc1_1 = tcoords.pc2_2 = 1.0;
+  tcoords.pc1_2 = tcoords.pc2_1 = 0.0;
+  strcpy (tcoords.ctype, "RA---TAN");
+
+  /* define original corners */
+  Xo[0] = 0;           Yo[0] = 0;
+  Xo[1] = image[0].NX; Yo[1] = 0;
+  Xo[2] = image[0].NX; Yo[2] = image[0].NY;
+  Xo[3] = 0;           Yo[3] = image[0].NY;
+  Xo[4] = 0;           Yo[4] = 0;  /* so we can make a loop easily */
+  for (j = 0; j < 5; j++) {
+    XY_to_RD (&r, &d, Xo[j], Yo[j], &image[0].coords);
+    RD_to_XY (&Xo[j], &Yo[j], r, d, &tcoords);
+  }
+  
+  /* advance to data segment */ 
+  fseek (f, header.size, SEEK_SET); 
+
+  Nimage = 0;
+  fits_scan (&header, "NIMAGES", "%d", 1, &Nimage);
+  /* check that file size makes sense */
+  if (stat (ImageCat, &filestatus) == -1) {
+    fprintf (stderr, "failed to get status of image catalog\n");
+    Shutdown ();
+  }
+  size = Nimage*sizeof(Image) + header.size;
+  if (size != filestatus.st_size) {
+    int Ndata;
+
+    Ndata = (filestatus.st_size - header.size) / sizeof (Image);
+    fprintf (stderr, "image catalog has inconsistent size\n");
+    fprintf (stderr, "header: %d images, data: %d images\n", Nimage, Ndata);
+    if (!FORCE) Shutdown ();
+    
+    Nimage = MIN (Nimage, Ndata);
+    fprintf (stderr, "using %d images\n", Nimage);
+  } 
+
+  NTIMAGE = 100;
+  ALLOCATE (timage, Image, NTIMAGE);
+
+  npimage = 0;
+  NPIMAGE = 20;
+  ALLOCATE (pimage, Image, NPIMAGE);
+
+  for (Ntimage = 0; Ntimage < Nimage; Ntimage += ntimage) {
+    ntimage = Fread (timage, sizeof(Image), NTIMAGE, f, "image");
+    for (i = 0; i < ntimage; i++) {
+      addtolist = FALSE;
+      /* define image corners */
+      Xi[0] = 0;            Yi[0] = 0;
+      Xi[1] = timage[i].NX; Yi[1] = 0;
+      Xi[2] = timage[i].NX; Yi[2] = timage[i].NY;
+      Xi[3] = 0;            Yi[3] = timage[i].NY;
+      Xi[4] = 0;            Yi[4] = 0;  /* so we can make a loop easily */
+      /* transform to tcoords */
+      for (j = 0; j < 5; j++) {
+	XY_to_RD (&r, &d, Xi[j], Yi[j], &timage[i].coords);
+	RD_to_XY (&Xi[j], &Yi[j], r, d, &tcoords);
+      }
+      /* check if any edges cross */
+      for (j = 0; (j < 4) && !addtolist; j++) {
+	for (k = 0; (k < 4) && !addtolist; k++) {
+	  addtolist |= edge_check (&Xi[j], &Yi[j], &Xo[k], &Yo[k]);
+	}
+      }
+      if (addtolist) {
+	pimage[npimage] = timage[i];
+	npimage ++;
+	if (npimage == NPIMAGE) {
+	  NPIMAGE += 20;
+	  REALLOCATE (pimage, Image, NPIMAGE);
+	}
+      }
+    }
+
+    if (ntimage == 0) {
+      fprintf (stderr, "ERROR: expected %d images, only found %d\n", Nimage, Ntimage);
+      Shutdown ();
+    }
+  }
+
+  if (VERBOSE) fprintf (stderr, "found %d overlapping images\n", npimage);
+
+  *Npimage = npimage;
+  return (pimage);
+
+}
+  
+int edge_check (x1, y1, x2, y2)
+double *x1, *y1, *x2, *y2;
+{
+
+  double theta1, theta2;
+  double Theta1, Theta2;
+
+  theta1 = opening_angle (x1[0], y1[0], x2[0], y2[0], x1[1], y1[1]); 
+  theta2 = opening_angle (x1[0], y1[0], x2[0], y2[0], x2[1], y2[1]); 
+
+  if (theta1*theta2 < 0.0) {
+    return (FALSE);
+  }
+
+  if (fabs(theta1) < fabs(theta2)) {
+    return (FALSE);
+  }
+
+  Theta1 = theta1;
+  Theta2 = theta2;
+  theta1 = opening_angle (x2[0], y2[0], x1[1], y1[1], x2[1], y2[1]); 
+  theta2 = opening_angle (x2[0], y2[0], x1[1], y1[1], x1[0], y1[0]); 
+  
+ 
+  if (theta1*theta2 < 0.0) {
+    return (FALSE);
+  }
+
+  if (fabs(theta1) < fabs(theta2)) {
+    return (FALSE);
+  }
+
+  return (TRUE);
+
+}
+
+double opening_angle (x1, y1, x2, y2, x3, y3)
+double x1, y1, x2, y2, x3, y3;
+{
+
+  double dx1, dy1, dx2, dy2, ct, st, theta;
+
+  dx1 = x1 - x2;
+  dy1 = y1 - y2;
+  
+  dx2 = x3 - x2;
+  dy2 = y3 - y2;
+  
+  ct = (dx1*dx2 + dy1*dy2);
+  st = (dx1*dy2 - dx2*dy1);
+
+  theta = atan2 (st, ct);
+
+  return (theta);
+
+}
+
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/gregions.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/gregions.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/gregions.c	(revision 2549)
@@ -0,0 +1,56 @@
+# include "addstar.h"
+
+GSCRegion *gregions (image, Nregions)
+Image *image;
+int *Nregions;
+{
+  
+  GSCRegion *region;
+  FILE *f;
+  double x, y;
+  double dec, ra;
+  int i, j, done, nregion, NREGION;
+  
+  f = fopen (GSCFILE, "r");
+  if (f == NULL) {
+    fprintf (stderr, "ERROR: can't find GSC region file %s\n", GSCFILE);
+    exit (1);
+  }
+  
+  /* find regions at image corners */
+  NREGION = 10;
+  ALLOCATE (region, GSCRegion, NREGION);
+  nregion = 0;
+
+  /* look for new regions on grid across image */ 
+  for (x = 0.0; x <= 1.0; x+=0.1) {
+    for (y = 0.0; y <= 1.0; y+=0.1) {
+      XY_to_RD (&ra, &dec, image[0].NX*(1.1*x - 0.05), image[0].NY*(1.1*y - 0.05), &image[0].coords);
+      aregion (&region[nregion], f, ra, dec);
+      done = FALSE;
+      for (j = 0; (j < nregion) && !done; j++) {
+	if (!strcmp (region[nregion].filename, region[j].filename)) {
+	  nregion --;
+	  done = TRUE;
+	}
+      }
+      nregion ++;
+      if (nregion == NREGION) {
+	NREGION += 10;
+	REALLOCATE (region, GSCRegion, NREGION);
+      }
+    }
+  }
+
+  if (VERBOSE) {
+    fprintf (stderr, "found %d region files:\n", nregion);
+    for (i = 0; i < nregion; i++) {
+      fprintf (stderr, "  %d %s\n", i, region[i].filename);
+    }
+  }
+  *Nregions = nregion;
+  
+  fclose (f);
+  return (region);
+  
+}
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/gstars.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/gstars.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/gstars.c	(revision 2549)
@@ -0,0 +1,295 @@
+# include "addstar.h"
+# define D_NSTARS 1000
+# define BYTES_STAR 66
+# define BLOCK 1000
+#include <sys/time.h>
+#include <time.h>
+
+Stars *gstars (char *file, int *NSTARS, Image *image) {
+
+  FILE *f;
+  Header header;
+  int j, N, Ninstar;
+  int nbytes, Nbytes;
+  Stars *stars;
+  char *buffer, *c, *c2, photcode[64];
+  double tmp, dMs;
+  int done, doneread, Nskip, Nextra, itmp;
+  int hour, min;
+  double sec;
+  char line[80];
+
+  if (!fits_read_header (file, &header)) {
+    fprintf (stderr, "ERROR: can't read header for %s\n", file);
+    exit (1);
+  }
+
+  /* open file */
+  f = fopen (file, "r");
+  if (f == NULL) {
+    fprintf (stderr, "ERROR: can't read data from %s\n", file);
+    exit (1);
+  }
+  fseek (f, header.size, SEEK_SET); 
+
+  /* find image rootname */
+  c = strrchr (file, 0x2f);
+  if (c == (char *) NULL) {
+    strcpy (image[0].name, file);
+  } else { 
+    strcpy (image[0].name, (c+1));
+  }
+
+  /* get astrometry information */
+  if (!GetCoords (&image[0].coords, &header)) {
+    fprintf (stderr, "ERROR: no astrometric solution in header\n");
+    exit (1);
+  }
+  while (image[0].coords.crval1 < 0) image[0].coords.crval1 += 360.0;
+  while (image[0].coords.crval1 > 360.0) image[0].coords.crval1 -= 360.0;
+  itmp = 0;
+  fits_scan (&header, "NASTRO",   "%d", 1, &itmp);
+  if ((itmp == 0) && !ACCEPT_ASTROM) {
+    fprintf (stderr, "ERROR: bad astrometric solution in header\n");
+    exit (1);
+  }
+
+  /* get ST */
+  fits_scan (&header, STKeyword, "%s", 1, line);
+  /* remove ':' characters */
+  for (c = strchr (line, ':'); c != (char *) NULL; c = strchr (line, ':')) { *c = ' '; }
+  sscanf (line, "%d %d %lf", &hour, &min, &sec);
+  SiderealTime = hour + min/60.0 + sec/3600.0;
+
+  /* CERROR in data file is in arcsec */
+  if (!fits_scan (&header, "CERROR",   "%lf", 1, &tmp)) tmp = 1.0;
+  image[0].cerror = tmp * 50.0;
+  fits_scan (&header, "NAXIS1",   "%hd", 1, &image[0].NX); 
+  fits_scan (&header, "NAXIS2",   "%hd", 1, &image[0].NY);
+ 
+  /* get photcode from header */
+  if (thiscode == NULL) {
+    fits_scan (&header, "PHOTCODE", "%s", 1, photcode);
+    thiscode = GetPhotcodebyName (photcode);
+  }
+  if (thiscode == NULL) {
+    fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", photcode);
+    exit (1);
+  }
+  image[0].source = thiscode[0].code;
+
+  image[0].NX -= XOVERSCAN;
+  image[0].NY -= YOVERSCAN;
+  fits_scan (&header, ExptimeKeyword,  "%lf", 1, &tmp);
+  image[0].exptime = tmp;
+  
+  tmp = 0;
+  /* fits_scan (&header, "APMIFIT",  "%lf", 1, &tmp); */
+  image[0].apmifit = tmp * 1000.0;
+
+  tmp = 0;
+  /* fits_scan (&header, "dAPMIFIT", "%lf", 1, &tmp); */
+  image[0].dapmifit = tmp * 1000.0;
+
+  tmp = 0;
+  fits_scan (&header, "FLIMIT",   "%lf", 1, &tmp);
+  image[0].detection_limit = tmp * 10.0;
+
+  tmp = 0;
+  fits_scan (&header, "FSATUR",   "%lf", 1, &tmp);
+  image[0].saturation_limit = tmp * 10.0;
+
+  tmp = 0;
+  fits_scan (&header, "FWHM_X",   "%lf", 1, &tmp);
+  image[0].fwhm_x = tmp * 25.0 * image[0].coords.cdelt1 * 3600.0;
+
+  tmp = 0;
+  fits_scan (&header, "FWHM_Y",   "%lf", 1, &tmp);
+  image[0].fwhm_y = tmp * 25.0 * image[0].coords.cdelt1 * 3600.0;
+
+  if (!fits_scan (&header, "TZERO",   "%d",  1, &image[0].tzero)) {
+    image[0].tzero = parse_time (&header);
+  }
+
+  tmp = 0;
+  fits_scan (&header, "TRATE",   "%lf", 1, &tmp);
+  image[0].trate = 10000 * tmp;
+
+  tmp = 0;
+  fits_scan (&header, AirmassKeyword, "%lf", 1, &tmp);
+  image[0].secz = MIN (NO_MAG, 1000*tmp);
+
+  if (!fits_scan (&header, CCDNumKeyword, "%d", 1, &itmp)) {
+    image[0].ccdnum = 0xff;
+  } else {
+    image[0].ccdnum = itmp;
+  }
+
+  fits_scan (&header, "ZERO_PT", "%lf", 1, &ZeroPt);
+
+  /* secz is in units milli-airmass */
+  image[0].Mcal = 0.0;
+  image[0].Xm   = NO_MAG;
+  image[0].code = 0;
+  bzero (image[0].dummy, sizeof(image[0].dummy));
+
+  /* find expected number of stars */
+  fits_scan (&header, "NSTARS", "%d", 1, &image[0].nstar);
+  if (image[0].nstar == 0) {
+    fprintf (stderr, "ERROR: can't get NSTARS from header\n");
+    exit (1);
+  }
+  ALLOCATE (stars, Stars, image[0].nstar);
+
+  /* temporary:  get sky background from Flips data */
+  {
+    char *p;
+    int sky;
+    
+    fits_scan (&header, "HISTORY", "%S", 10, line);
+    p = strstr (line, "|I=");
+    if (p != (char *) NULL) {
+      p += 3;
+      sky = atoi (p);
+    }
+    image[0].Myyyy = MIN (sky - 0x8000, 0xffff);
+  }
+
+  /* load in stars by blocks of 1000 */
+  N = 0;
+  ALLOCATE (buffer, char, (BLOCK*BYTES_STAR) + 1);
+  buffer[BLOCK*BYTES_STAR] = 0;
+  Nextra = 0;
+  doneread = FALSE;
+  while (!doneread) {
+    Nbytes = BYTES_STAR * BLOCK - Nextra;
+    nbytes = Fread (&buffer[Nextra], 1, Nbytes, f, "char");
+    if (nbytes == 0) {
+      doneread = TRUE;
+      continue;
+    }
+    nbytes += Nextra;
+    /* check line-by-line integrity */
+    c = buffer;
+    done = FALSE;
+    while ((c < buffer + nbytes) && (!done)) { 
+      for (c2 = c; *c2 == '\n'; c2++);
+      if (c2 > c) { /* extra return chars */
+	memmove (c, c2, (int)(buffer + nbytes - c2));
+	Nskip = c2 - c;
+	nbytes -= Nskip;
+	bzero (buffer + nbytes, Nskip);
+	if (VERBOSE) fprintf (stderr, "deleted %d extra return chars\n", Nskip);
+      }
+      c2 = strchr (c, '\n');
+      if (c2 == (char *) NULL) {
+	done = TRUE;	
+	continue;
+      }
+      c2++;
+      if ((c2 - c) != BYTES_STAR) { /* bad line, delete it */
+	memmove (c, c2, (int)(buffer + nbytes - c2));
+	Nskip = c2 - c;
+	nbytes -= Nskip;
+	bzero (buffer + nbytes, Nskip);
+	if (VERBOSE) fprintf (stderr, "deleted line, %d extra chars\n", Nskip);
+      } else {
+	c = c2;
+      }
+    }
+    Ninstar = nbytes / BYTES_STAR;
+    Nextra = nbytes % BYTES_STAR;
+    for (j = 0; j < Ninstar; j++, N++) {
+      dparse (&stars[N].X,  1, &buffer[j*BYTES_STAR]);
+      dparse (&stars[N].Y,  2, &buffer[j*BYTES_STAR]);
+      dparse (&stars[N].M,  3, &buffer[j*BYTES_STAR]);
+      dparse (&stars[N].dM, 4, &buffer[j*BYTES_STAR]);
+      dparse (&tmp,         5, &buffer[j*BYTES_STAR]);
+      stars[N].dophot = tmp;
+      dparse (&stars[N].Mgal, 7, &buffer[j*BYTES_STAR]);
+      dparse (&stars[N].Map,  8, &buffer[j*BYTES_STAR]);
+      dparse (&stars[N].fx,   9, &buffer[j*BYTES_STAR]);
+      dparse (&stars[N].fy,  10, &buffer[j*BYTES_STAR]);
+      dparse (&stars[N].df,  11, &buffer[j*BYTES_STAR]);
+
+      XY_to_RD (&stars[N].R, &stars[N].D, stars[N].X, stars[N].Y, &image[0].coords);
+      while (stars[N].R < 0.0) stars[N].R += 360.0;
+      while (stars[N].R >= 360.0) stars[N].R -= 360.0;
+      stars[N].found = -1;
+
+      dMs = get_subpix (stars[N].X, stars[N].Y);
+      stars[N].M    -= dMs;
+      stars[N].Mgal -= dMs;
+      stars[N].Map  -= dMs;
+      dMs = 1000.0*scat_subpix (stars[N].X, stars[N].Y);
+      stars[N].dM = hypot (stars[N].dM, dMs);
+    }
+  }
+
+  image[0].nstar = N;
+  REALLOCATE (stars, Stars, image[0].nstar);
+  if (VERBOSE) fprintf (stderr, "read %d stars from target file\n", image[0].nstar);
+  *NSTARS = image[0].nstar;
+
+  return (stars);
+}
+
+
+
+
+# if (0)
+  /* left over fraction of a block */
+  nbytes = Fread (buffer, 1, (Nbytes % (BLOCK*BYTES_STAR)), f, "char");
+  if (nbytes != (Nbytes % (BLOCK*BYTES_STAR))) {
+    fprintf (stderr, "ERROR: failed to read in stars (2)\n");
+    exit (1);
+  }
+  /* check line-by-line integrity */
+  Nnew = 0;
+  c = buffer;
+  done = FALSE;
+  while ((c < buffer + nbytes) && (!done)) { 
+    for (c2 = c; *c2 == '\n'; c2++);
+    if (c2 > c) { /* extra return chars */
+      memmove (c, c2, (int)(buffer + nbytes - c2));
+      Nnew += c2 - c;
+      fprintf (stderr, "deleting %d extra return chars\n", (int)(c2-c));
+    }
+    c2 = strchr (c, '\n');
+    if (c2 == (char *) NULL) {
+      if (Nnew > 0) {
+	nnew = Fread (&buffer[nbytes-Nnew], 1, Nnew, f, "char");
+	if (nnew != Nnew) {
+	  fprintf (stderr, "ERROR: failed to read in stars (1)\n");
+	  exit (1);
+	}
+      } else {
+	done = TRUE;	
+      }
+      continue;
+    }
+    c2++;
+    if ((c2 - c) != BYTES_STAR) {
+      /* this is a bad line, delete it, adjust data in buffer, 
+	 and be ready to read the rest of the last line */
+      memmove (c, c2, (int)(buffer + nbytes - c2));
+      Nnew += c2 - c;
+      fprintf (stderr, "deleting bad line with %d chars\n", (int)(c2-c));
+    } else {
+      c = c2;
+    }
+  }
+  for (j = 0; j < nbytes / BYTES_STAR; j++, nstar++, N++) {
+    dparse (&stars[N].X, 1, &buffer[j*BYTES_STAR]);
+    dparse (&stars[N].Y, 2, &buffer[j*BYTES_STAR]);
+    dparse (&stars[N].M, 3, &buffer[j*BYTES_STAR]);
+    dparse (&stars[N].dM, 4, &buffer[j*BYTES_STAR]);
+    XY_to_RD (&stars[N].R, &stars[N].D, stars[N].X, stars[N].Y, &image[0].coords);
+    while (stars[N].R < 0.0) stars[N].R += 360.0;
+    while (stars[N].R >= 360.0) stars[N].R -= 360.0;
+    dparse (&tmp, 5, &buffer[j*BYTES_STAR]);
+    stars[N].dophot = tmp;
+    stars[N].found = -1;
+  }
+
+# endif
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/load_subpix.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/load_subpix.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/load_subpix.c	(revision 2549)
@@ -0,0 +1,53 @@
+# include "addstar.h"
+
+typedef struct {
+  double Amp;
+  double Phase;
+  double dM;
+} SubPixFix;
+
+static int Nsubpix;
+static SubPixFix *Subpix;
+
+void load_subpix () {
+
+  int i;
+  FILE *f;
+
+  Nsubpix = 40;
+  ALLOCATE (Subpix, SubPixFix, Nsubpix);
+
+  f = fopen (SubpixDatafile, "r");
+  if (f == (FILE *) NULL) { 
+    fprintf (stderr, "can't load subpix datafile %s\n", SubpixDatafile);
+    Shutdown ();
+  }
+  for (i = 0; i < Nsubpix; i++) {
+    fscanf (f, "%*s %*s %lf %lf %lf %*s\n", 
+	    &Subpix[i].Amp, &Subpix[i].Phase, &Subpix[i].dM);
+  }
+  fclose (f);
+
+}
+
+double get_subpix (double x, double y) {
+
+  int bin;
+  double dy, dM;
+
+  dy = y - (int)(y);
+  bin = 5 * (int)(x/100) + (int)(y/100);
+  dM = Subpix[bin].Amp*sin(2*3.14159*dy + Subpix[bin].Phase);
+  return (dM);
+}
+
+double scat_subpix (double x, double y) {
+
+  int bin;
+  double dy, dM;
+
+  dy = y - (int)(y);
+  bin = 5 * (int)(x/100) + (int)(y/100);
+  dM = Subpix[bin].dM;
+  return (dM);
+}
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/mkcatalog.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/mkcatalog.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/mkcatalog.c	(revision 2549)
@@ -0,0 +1,38 @@
+# include "addstar.h"
+# include <time.h>
+
+void mkcatalog (GSCRegion *region, Catalog *catalog) {
+  
+  char filename[64], line[64];
+  struct tm *local;
+  struct timeval now;
+
+  if (VERBOSE) fprintf (stderr, "new catalog file: %s\n", region[0].filename);
+  if (!fits_read_header (CatTemplate, &catalog[0].header)) {
+    fprintf (stderr, "ERROR: can't find template header %s\n", CatTemplate);
+    exit (1);
+  }
+  
+  /* assign header values for RA, DEC, DATE, etc */
+  fits_modify (&catalog[0].header, "RA0", "%lf", 1, region[0].RA[0]);
+  fits_modify (&catalog[0].header, "DEC0", "%lf", 1, region[0].DEC[0]);
+  fits_modify (&catalog[0].header, "RA1", "%lf", 1, region[0].RA[1]);
+  fits_modify (&catalog[0].header, "DEC1", "%lf", 1, region[0].DEC[1]);
+  gettimeofday (&now, NULL);
+  local = localtime (&now.tv_sec);
+  sprintf (line, "%02d/%02d/%02d", local[0].tm_year, local[0].tm_mon + 1, local[0].tm_mday); 
+  fits_modify (&catalog[0].header, "DATE", "%s", 1, line);
+  strcpy (filename, CatTemplate);
+  ALLOCATE (catalog[0].average, Average, 1);
+  ALLOCATE (catalog[0].measure, Measure, 1);
+  ALLOCATE (catalog[0].missing, Missing, 1);
+  catalog[0].Naverage = catalog[0].Nmeasure = catalog[0].Nmissing = 0;
+
+  /* setup secondary filters to match photcodes */
+  ALLOCATE (catalog[0].secfilt, SecFilt, 1);
+  catalog[0].Nsecfilt = GetPhotcodeNsecfilt ();
+
+  /* Nsecfilt is number of filters.  Number of entries in array is
+     Nsecfilt * Naverage.  At this point, N entries == 0 */
+
+}
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/parse_time.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/parse_time.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/parse_time.c	(revision 2549)
@@ -0,0 +1,126 @@
+# include "addstar.h"
+
+int parse_time (Header *header) {
+
+  double jd;
+  int Ny, Nf, mode;
+  int Nsec, hour, min, sec, year, month, day;
+  char *py, *pm, *pd, *c;
+  char line[256];
+
+  /* we want to find JD or MJD to get Nsec (seconds since 01/01/1970) */
+
+  /* try JD first */
+  if (strcasecmp (JDKeyword, "NONE")) {
+    uppercase (JDKeyword);
+    fits_scan (header, JDKeyword, "%lf", 1, &jd);
+    Nsec = (jd - 2440587.5)*86400;
+    return (Nsec);
+  }
+
+  /* try MJD next */
+  if (strcasecmp (MJDKeyword, "NONE")) {
+    uppercase (MJDKeyword);
+    fits_scan (header, MJDKeyword, "%lf", 1, &jd);
+    Nsec = (jd - 40587.0)*86400;
+    return (Nsec);
+  }
+    
+  /* get UT and DATE */
+  uppercase (UTKeyword);
+  fits_scan (header, UTKeyword, "%s", 1, line);
+  /* remove ':' characters */
+  for (c = strchr (line, 0x3a); c != (char *) NULL; c = strchr (line, 0x3a)) { *c = ' '; }
+  sscanf (line, "%d %d %d", &hour, &min, &sec);
+
+  /* parse mode line */
+  uppercase (DateMode);
+  for (Ny = 0, c = strchr (DateMode, 'Y'); c != (char ) NULL; c = strchr (c + 1, 'Y'), Ny++);
+  if ((Ny != 2) && (Ny != 4)) {
+    fprintf (stderr, "error in DATE-MODE format: %s\n", DateMode);
+    exit (1);
+  }
+  py = strchr (DateMode, 'Y');
+  pm = strchr (DateMode, 'M');
+  pd = strchr (DateMode, 'D');
+  if ((py == (char *) NULL) || (pm == (char *) NULL) || (pd == (char *) NULL)) {
+    fprintf (stderr, "error in DATE-MODE format: %s\n", DateMode);
+    exit (1);
+  }
+  if ((py > pm) && (py < pd)) {
+    fprintf (stderr, "error in DATE-MODE format: %s\n", DateMode);
+    exit (1);
+  }
+  if ((py > pd) && (py < pm)) {
+    fprintf (stderr, "error in DATE-MODE format: %s\n", DateMode);
+    exit (1);
+  }
+  mode = 0;
+  if ((py < pm) && (pm < pd)) { mode = 1; }  /* yyyy-mm-dd */
+  if ((py < pm) && (pm > pd)) { mode = 2; }  /* yyyy-dd-mm */
+  if ((py > pm) && (pm < pd)) { mode = 3; }  /* mm-dd-yyyy */
+  if ((py > pm) && (pm > pd)) { mode = 4; }  /* dd-mm-yyyy */
+  if (!mode) {
+    fprintf (stderr, "error in DATE-MODE format: %s\n", DateMode);
+    exit (1);
+  }
+
+  /* parse date entry */
+  uppercase (DateKeyword);
+  fits_scan (header, DateKeyword, "%s",  1, line);
+  /* remove possible separators: ':', '/' '.', '-' */
+  for (c = strchr (line, 0x3a); c != (char *) NULL; c = strchr (line, 0x3a)) { *c = ' '; }
+  for (c = strchr (line, 0x2f); c != (char *) NULL; c = strchr (line, 0x2f)) { *c = ' '; }
+  for (c = strchr (line, 0x2e); c != (char *) NULL; c = strchr (line, 0x2e)) { *c = ' '; }
+  for (c = strchr (line, 0x2d); c != (char *) NULL; c = strchr (line, 0x2d)) { *c = ' '; }
+
+  switch (mode) {
+  case 1:
+    Nf = sscanf (line, "%d %d %d", &year, &month, &day);
+    break;
+  case 2:
+    Nf = sscanf (line, "%d %d %d", &year, &day, &month);
+    break;
+  case 3:
+    Nf = sscanf (line, "%d %d %d", &month, &day, &year);
+    break;
+  case 4:
+    Nf = sscanf (line, "%d %d %d", &day, &month, &year);
+    break;
+  }
+  if (Nf != 3) {
+    fprintf (stderr, "error in date entry (%s) or DATE-MODE format (%s)\n", line, DateMode);
+    exit (1);
+  }
+
+  if (year > 1000) {
+    if (Ny == 2) {
+      fprintf (stderr, "warning: mode line claims 2 digit year, but 4 digit year found\n");
+    }
+  } else {
+    if (Ny == 4) {
+      fprintf (stderr, "warning: mode line claims 4 digit year, but 2 digit year found\n");
+    }
+    if (year < 50) year += 100;
+    year += 1900;
+  }    
+
+  /* convert yy.mm.dd hh.mm.ss to Nsec since 1970 (jd = 2440587.5) */
+  /* note that in this section, tm_mon has range 1-12, unlike for gmtime () */
+  jd = day - 32075 + (int)(1461*(year + 4800 + (int)(((month)-14)/12))/4)
+    + (int)(367*((month) - 2 - (int)(((month) - 14)/12)*12)/12)
+    - (int)(3*(int)((year + 4900 + (int)(((month) - 14)/12))/100)/4) - 0.5;
+  /* jd is the julian day of the whole day only not the time */
+  Nsec = (jd - 2440587.5)*86400 + 3600.0*hour + min*60.0 + sec;
+  
+  return (Nsec);
+
+}
+
+void uppercase (char *string) {
+
+  int i;
+    
+  for (i = 0; i < strlen (string); i++) string[i] = toupper (string[i]);
+
+}
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/sort_lists.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/sort_lists.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/sort_lists.c	(revision 2549)
@@ -0,0 +1,120 @@
+
+void sort_lists (float *X, float *Y, int *S, int N) {
+
+  int l,j,ir,i;
+  double tX, tY, tS;
+  
+  l = N >> 1;
+  ir = N - 1;
+  for (;;) {
+    if (l > 0) {
+      l--;
+      tX = X[l];
+      tY = Y[l];
+      tS = S[l];
+    }
+    else {
+      tX = X[ir];
+      X[ir] = X[0];
+      tY = Y[ir];
+      Y[ir] = Y[0];
+      tS = S[ir];
+      S[ir] = S[0];
+      if (--ir == 0) {
+	X[0] = tX;
+	Y[0] = tY;
+	S[0] = tS;
+	return;
+      }
+    }
+    i = l;
+    j = (l << 1) + 1;
+    while (j <= ir) {
+      if (j < ir && X[j] < X[j+1]) j++;
+      if (tX < X[j]) {
+	X[i] = X[j];
+	Y[i] = Y[j];
+	S[i] = S[j];
+	j += (i=j) + 1;
+      }
+      else j = ir + 1;
+    }
+    X[i] = tX;
+    Y[i] = tY;
+    S[i] = tS;
+  }
+}
+
+void fsort (float *X, int N) {
+
+  int l,j,ir,i;
+  float tX;
+  
+  l = N >> 1;
+  ir = N - 1;
+  for (;;) {
+    if (l > 0) {
+      l--;
+      tX = X[l];
+    }
+    else {
+      tX = X[ir];
+      X[ir] = X[0];
+      if (--ir == 0) {
+	X[0] = tX;
+	return;
+      }
+    }
+    i = l;
+    j = (l << 1) + 1;
+    while (j <= ir) {
+      if (j < ir && X[j] < X[j+1]) j++;
+      if (tX < X[j]) {
+	X[i] = X[j];
+	j += (i=j) + 1;
+      }
+      else j = ir + 1;
+    }
+    X[i] = tX;
+  }
+}
+
+void fsort2 (float *X, float *Y, int N) {
+
+  int l,j,ir,i;
+  float tX, tY;
+  
+  l = N >> 1;
+  ir = N - 1;
+  for (;;) {
+    if (l > 0) {
+      l--;
+      tX = X[l];
+      tY = Y[l];
+    }
+    else {
+      tX = X[ir];
+      tY = Y[ir];
+      X[ir] = X[0];
+      Y[ir] = Y[0];
+      if (--ir == 0) {
+	X[0] = tX;
+	Y[0] = tY;
+	return;
+      }
+    }
+    i = l;
+    j = (l << 1) + 1;
+    while (j <= ir) {
+      if (j < ir && X[j] < X[j+1]) j++;
+      if (tX < X[j]) {
+	X[i] = X[j];
+	Y[i] = Y[j];
+	j += (i=j) + 1;
+      }
+      else j = ir + 1;
+    }
+    X[i] = tX;
+    Y[i] = tY;
+  }
+}
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/wcatalog.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/wcatalog.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/wcatalog.c	(revision 2549)
@@ -0,0 +1,35 @@
+# include "addstar.h"
+
+void wcatalog (Catalog *catalog) {
+  
+  /* we'll leave these for now, but they are LONEOS specific */
+  fits_modify (&catalog[0].header, "MARKSTAR", "%t", 1, FALSE);
+  fits_modify (&catalog[0].header, "ADDUSNO", "%t", 1, FALSE);
+  fits_modify (&catalog[0].header, "MARKROCK", "%t", 1, FALSE);
+
+  if (!save_catalog (catalog, VERBOSE)) {
+    fprintf (stderr, "ERROR: failure saving catalog\n");
+    exit (1);
+  }
+
+  /* free, initialize data structures */
+
+  if (catalog[0].Naverage) {
+    free (catalog[0].average); 
+    catalog[0].Naverage = 0;
+  }
+  if (catalog[0].Nmeasure) {
+    free (catalog[0].measure); 
+    catalog[0].Nmeasure = 0;
+  }
+  if (catalog[0].Nmissing) {
+    free (catalog[0].missing); 
+    catalog[0].Nmissing = 0;
+  }
+  if (catalog[0].Nsecfilt) {
+    free (catalog[0].secfilt); 
+    catalog[0].Nsecfilt = 0;
+  }
+  fits_free_header (&catalog[0].header);
+}
+
Index: /tags/addspphot-1-0/Ohana/src/addspphot/src/wimage.c
===================================================================
--- /tags/addspphot-1-0/Ohana/src/addspphot/src/wimage.c	(revision 2549)
+++ /tags/addspphot-1-0/Ohana/src/addspphot/src/wimage.c	(revision 2549)
@@ -0,0 +1,54 @@
+# include "addstar.h"
+
+void wimage (FILE *f, int dbstate, Image *image, int Nstars) {
+  
+  int Nimages, status, offset;
+  Header header;
+
+  /* place file pointer at beginning of file */
+  fseek (f, 0, SEEK_SET);
+
+  if (dbstate == LCK_EMPTY) {
+    if (VERBOSE) fprintf (stderr, "can't find %s, creating a new one\n", ImageCat);
+    if (!fits_read_header (ImageTemplate, &header)) {
+      fprintf (stderr, "ERROR: can't find template header %s\n", ImageTemplate);
+      exit (1);
+    }
+    /* assign Zero Point provided by first image in dB */
+    fits_modify (&header, "ZERO_PT", "%lf", 1, ZeroPt);
+  } else {
+    if (!fits_load_header (f, &header)) {
+      fprintf (stderr, "ERROR: can't read image header %s\n", ImageCat);
+      exit (1);
+    }
+  }
+  
+  Nimages = 0;
+  fits_scan (&header, "NIMAGES", "%d", 1, &Nimages);
+  Nimages ++;
+  fits_modify (&header, "NIMAGES", "%d", 1, Nimages);
+
+  /* position to begining of file to write header */
+  fseek (f, 0, SEEK_SET);
+  status = Fwrite (header.buffer, 1, header.size, f, "char");
+  if (status != header.size) {
+    fprintf (stderr, "ERROR: failed writing data to image header\n");
+    exit (1);
+  }
+
+  /* position to end of data array */
+  offset = (Nimages - 1)*sizeof(Image) + header.size;
+  fseek (f, offset, SEEK_SET);
+
+  status = Fwrite (image, sizeof(Image), 1, f, "image");
+  if (status != 1) {
+    fprintf (stderr, "ERROR: failed writing data to image catalog\n");
+    exit (1);
+  }
+}
+
+/* the image we add in this routine is always a new image.  
+   We only need to do two things:
+     1) append the data for the image to the end of the file
+     2) update the NIMAGES field in the header (which means, read in header, re-write)
+ */
