Index: trunk/archive/scripts/phase2.pl
===================================================================
--- trunk/archive/scripts/phase2.pl	(revision 2519)
+++ trunk/archive/scripts/phase2.pl	(revision 2520)
@@ -2,5 +2,7 @@
 
 sub usage {
-    print STDERR "USAGE: phase2.pl\n";
+    # input is foo.fits
+    # outputs are outroot.im.fits (image) outroot.ob.fits (object list), outroot.log, etc
+    print STDERR "USAGE: phase2 (input) (outroot)\n";
     exit 2;
 }
@@ -8,76 +10,50 @@
 if (@ARGV != 4) { &usage; }
 
-# phase2.pl has extensive intelligence about data sources
+# base file needs to be hardwired, from environment, or based on program name
+$base   = psMetadataParseConfig (NULL, basefile, FALSE);  
 
-# input: filename.fits
-# output: flattened image: filename.f.fits? filename.flt? (chosen externally?)
-# output: extracted stars: filename.p.tbl
-# output: constructed kernel: filename.k.fits
+# probably defined in the base file?
+$site   = psMetadataParseConfig (NULL, sitefile, FALSE);  
 
-# determine camera from image or externally?
+# this should be optionally specified as part of the command-line options
+# alternatively, this should be derived from the input file header data 
+$camera = psMetadataParseConfig (NULL, camerafile, FALSE);
 
-# load configuration information:
+# we need a way to define an optional alternative recipe
+$recipe = psMetadataParseConfig (NULL, recipefile, FALSE);  # defined in the camera file
 
-$config = psMetadataReadFile (NULL, $configfile);
+# load only the headers from the given file input a list of headers
+@headers = psFITSReadHeaderSet ($input);
 
-# check image:
-# is it a FITS image?
-# find camera
-# find mode:
-#  one cell per extension
-#  one chip per extension
-#  one FPA per extension
+# check the validity of the given set of headers based on expectactions for this camera
+# ** where does camera come from?
+# ** how do we do this if the camera is derived from the header info?
+$status = pmCameraValidateHeaders ($headers, $camera);
 
-@chips = getChips ($input);
-foreach $chip () {
-    @cells = getCells ($input, $chip);
-}
+# this gets a bit tenuous: what do we do with the collection?  
 
+# construct a complete, empty (ie, no pixel data) FPA structure for this camera / header set
+# we need to construct the output container.  is it big enough to fit in memory?  assume so?
+$FPA = pmFPAfromHeader ($headers, $camera);
 
+### now we work on individual readouts:
 
-# construct kernel
+# we have the FPA, already validated
+# as part of the FPA, we have access to a specified READOUT and we know the corresponding EXTNAME
 
-# load bias
-# load dark 
-# apply bias and dark
+# load in the complete readout:
+$image    = psImageReadSection (NULL, 0, 0, 0, 0, 0, $extname, $Nreadout, $input);
 
-# all possible detrend operations
-#   - bias
-#   - dark
-#   - trim
-#   - mask bad pixels (astrometry independent)
-#   - mask artifacts (astrometry dependent)
-#   - flatten
-#   - fringe correct
+# load in the complete corresponding bias image (this should be done once per cell)
+# in this form, we are implying that the bias image is guaranteed to have the corresponding EXTNAME
+$bias     = psImageReadSection (NULL, 0, 0, 0, 0, 0, $extname, 0, $biasname);
 
-# convolution kernel is applied to:
-#   - flat
-#   - fringe
-#   - bias?
+# overscan options:
+# - overscan is defined by the recipe (BIAS.OVERSCAN has form [Xs,Xe:Ys:Ye])
+# - overscan is defined by the header (BIAS.OVERSCAN has value BIASSEC)
+# - overscan is not used (BIAS.OVERSCAN has value NONE)
 
-#
-
-
-$out = pmSubtractBias ($in, $fit);
-
-
-##### example: single chip, single cell, single readout:
-
-# input filename: $input
-
-# bias recipe:
-#   bias image: filename
-#   overscan: header BIASSE
-
-# bias subtraction options:
-# - use a bias image?
-# - 
-
-
-
-$image    = psImageReadSection (NULL, 0, 0, 0, 0, 0, $extname, -1, $input);
-$metadata = psMetadataReadHeader (NULL, $extname, -1, $input);
-
-$metaitem = psMetadataLookup ($metadata, "BIASSEC");
+# bias section from header:
+$ = psMetadataLookup ($readout->metadata, "BIASSEC");
 $overscan = psImageSubsection ($image, $metaitem->data.void);
 
@@ -88,6 +64,2 @@
 $image_b = pmSubtractBias ($image, NULL, $overlist, PM_OVERSCAN_COLUMNS, $stats, 0, PM_FIT_NONE, $bias);
 
-
-
-#######################
-
