Index: /tags/ipp-20141024/ippconfig/gpc1/ppStack.config
===================================================================
--- /tags/ipp-20141024/ippconfig/gpc1/ppStack.config	(revision 37987)
+++ /tags/ipp-20141024/ippconfig/gpc1/ppStack.config	(revision 37988)
@@ -277,4 +277,189 @@
 
 
+
+
+STACK_CNP_PV3_IQ_1DG  METADATA       ## revised -- based on STACK_THREEPI/STACK_MD_DEEP_1DG
+    OUTPUT.NOCOMP           BOOL  TRUE 
+    OUTPUT.LOGFLUX          BOOL  FALSE
+    STACK.TYPE              STR   DEEP_STACK
+    PSF.INPUT.CLIP.SIMPLE   BOOL  True
+    PSF.INPUT.MAX           F32   10.0    #  
+    PSF.INPUT.CLIP.NSIGMA   F32   100.0
+    PSF.INPUT.THRESH        F32   NAN
+    PSF.INPUT.ASYMMETRY     F32   NAN
+    MATCH.REJ        F32    4.0
+    SAFE		 BOOL	F
+    PSF.TARGET.AS.MAX        BOOL  TRUE   # Set the target PSF FWHM as the maximum of accepted input FWHM values.
+    PSF.TARGET.AS.MAX.EPSILON F32 0.1     # set the target PSF FWHM larger than the maximum input. Target = eps + max(input)
+    THRESHOLD.MASK  F32     0.1          # Threshold for mask deconvolution (0..1)
+    COMBINE.ITER    F32     0.5          # Number of rejection iterations per input
+    COMBINE.REJ     F32     3.0          # Rejection threshold in combination (sigma)
+    COMBINE.SYS     F32     0.1          # Relative systematic error in combination
+    COMBINE.DISCARD F32     0.2          # Discard fraction for Olympic weighted mean
+    IMAGE.REJ       F32     0.1          # Rejected pixel fraction threshold for rejecting entire image
+    NMINPIX         S32     3            # Minimum input per pixel     
+    MASK.VAL        STR     SUSPECT,MASK.VALUE,CONV.BAD,GHOST # Mask value of input bad pixels 
+    MASK.BLANKBORDER S32    20              # Mask blank border in final stack output
+    BSCALEOFFSET    BOOL    FALSE           # MUST be FALSE for PV3
+END
+
+STACK_CNP_PV3_REF_1DG  METADATA  ## revised CNP PV3 REF stack --
+    OUTPUT.NOCOMP            BOOL  TRUE   # pointed field -- no compression, no logflux 
+    OUTPUT.LOGFLUX           BOOL  FALSE  # 
+    OUTPUT.REPLICATE         BOOL  TRUE
+    STACK.TYPE               STR   DEEP_STACK  
+    PSF.INPUT.CLIP.SIMPLE    BOOL  True
+    PSF.INPUT.MAX            F32   10.0   # CNP large FWHM, center/edge vary 1-2 pix? 
+    PSF.INPUT.CLIP.NSIGMA    F32   0.0    # sample typically on rising side of distribution 
+    PSF.INPUT.THRESH         F32   NAN
+    PSF.INPUT.ASYMMETRY      F32   NAN
+    MATCH.REJ                F32   4.0    # need ot tune?
+    SAFE                     BOOL  FALSE
+    PSF.TARGET.AS.MAX        BOOL  TRUE   # Set the target PSF FWHM as the maximum of accepted input FWHM values.
+    PSF.TARGET.AS.MAX.EPSILON F32 0.1     # set the target PSF FWHM larger than the maximum input. Target = eps + max(input)
+    THRESHOLD.MASK  F32     0.1          # Threshold for mask deconvolution (0..1)
+    COMBINE.ITER    F32     0.5          # Number of rejection iterations per input
+    COMBINE.REJ     F32     3.0          # Rejection threshold in combination (sigma)
+    COMBINE.SYS     F32     0.1          # Relative systematic error in combination
+    COMBINE.DISCARD F32     0.2          # Discard fraction for Olympic weighted mean
+    IMAGE.REJ       F32     0.1          # Rejected pixel fraction threshold for rejecting entire image
+    NMINPIX         S32     3            # Minimum input per pixel
+    MASK.VAL        STR     SUSPECT,MASK.VALUE,CONV.BAD,GHOST # Mask value of input bad pixels 
+    MASK.BLANKBORDER S32    20           # Mask blank border in final stack output
+    BSCALEOFFSET    BOOL    FALSE        # MUST be FALSE for PV3
+END
+
+STACK_CNP_PV3_DEEP_1DG  METADATA   ## -- 
+    OUTPUT.NOCOMP           BOOL  TRUE
+    OUTPUT.LOGFLUX          BOOL  FALSE
+    OUTPUT.REPLICATE        BOOL  TRUE
+    STACK.TYPE              STR   DEEP_STACK
+    PSF.INPUT.CLIP.SIMPLE   BOOL  True
+    PSF.INPUT.MAX           F32   16.0    # large FWHM images up to 20 --  
+    PSF.INPUT.CLIP.NSIGMA   F32   100.0   # do not want to clip really
+    PSF.INPUT.THRESH        F32   NAN
+    PSF.INPUT.ASYMMETRY     F32   NAN
+    MATCH.REJ               F32   4.0     # need ot tune?
+    SAFE                    BOOL  FALSE
+    PSF.TARGET.AS.MAX  BOOL  TRUE
+    PSF.TARGET.AS.MAX.EPSILON F32 0.1
+    THRESHOLD.MASK  F32     0.1             # Threshold for mask deconvolution (0..1)
+    COMBINE.ITER    F32     0.5             # Number of rejection iterations per input
+    COMBINE.REJ     F32     3.0             # Rejection threshold in combination (sigma)
+    COMBINE.SYS     F32     0.1             # Relative systematic error in combination
+    COMBINE.DISCARD F32     0.2             # Discard fraction for Olympic weighted mean
+    IMAGE.REJ       F32     0.1             # Rejected pixel fraction threshold for rejecting entire image
+    NMINPIX         S32     3               # Minimum input per pixel -- more and get crazy structures.. 
+    MASK.VAL        STR     SUSPECT,MASK.VALUE,CONV.BAD,GHOST # Mask value of input bad pixels -- many, dont trust SUSPECT
+    MASK.BLANKBORDER S32    20              # Mask blank border in final stack output
+    BSCALEOFFSET    BOOL    FALSE           # MUST be FALSE for PV3 (PV2 bias hack fix)
+END
+
+
+STACK_MD_PV3_NIGHT_1DG  METADATA   ## Revised MD 8 input NIGHT stacks for PV3 -- not complete
+    OUTPUT.NOCOMP             BOOL  FALSE
+    OUTPUT.LOGFLUX            BOOL  FALSE
+    PSF.INPUT.CLIP.SIMPLE     BOOL  True
+    PSF.INPUT.MAX             F32   12.0    # what FWHM does diffim become mostly useless, if any?
+    PSF.INPUT.CLIP.NSIGMA     F32   1000.0  # want to use up to MAX, but sig could possibly be small?
+    PSF.INPUT.THRESH        F32   NAN
+    PSF.INPUT.ASYMMETRY     F32   NAN
+    MATCH.REJ               F32   4.0   # need ot tune?
+    SAFE                    BOOL  FALSE
+    ## 
+    PSF.TARGET.AS.MAX       BOOL  TRUE      # Set the target PSF FWHM as the maximum of accepted input FWHM values.
+    PSF.TARGET.AS.MAX.EPSILON F32 0.1       # Amount to set the target PSF FWHM larger than the maximum input. Target = eps + max(input)
+    THRESHOLD.MASK  F32     0.1             # Threshold for mask deconvolution (0..1)
+    COMBINE.ITER    F32     0.5             # Number of rejection iterations per input
+    COMBINE.REJ     F32     3.0             # Rejection threshold in combination (sigma)
+    COMBINE.SYS     F32     0.1             # Relative systematic error in combination
+    COMBINE.DISCARD F32     0.2             # Discard fraction for Olympic weighted mean
+    IMAGE.REJ       F32     0.1             # Rejected pixel fraction threshold for rejecting entire image
+    NMINPIX         S32     4               # Minimum input per pixel
+    MASK.VAL        STR     SUSPECT,MASK.VALUE,CONV.BAD,GHOST # Mask value of input bad pixels 
+    MASK.BLANKBORDER S32    20              # Mask blank border in final stack output
+    BSCALEOFFSET    BOOL    FALSE           # MUST be FALSE for PV3
+END
+
+STACK_MD_PV3_IQ_1DG       METADATA  ## revised config for MD PV3 IQ stacks -- not complete
+    OUTPUT.NOCOMP           BOOL  TRUE
+    OUTPUT.LOGFLUX          BOOL  FALSE
+    OUTPUT.REPLICATE        BOOL  TRUE
+    STACK.TYPE              STR   DEEP_STACK  ## needed?
+    PSF.INPUT.CLIP.SIMPLE   BOOL  True
+    PSF.INPUT.MAX           F32   10.0   # center/edge can vary 1-2 pix, prefer <6 pix but needs to be <10 pix for edges until code improved..
+    PSF.INPUT.CLIP.NSIGMA   F32   100.0  # any change to inputs can greatly impact edge filling factor..
+    PSF.INPUT.THRESH        F32   NAN
+    PSF.INPUT.ASYMMETRY     F32   NAN
+    MATCH.REJ               F32   4.0   # need ot tune?
+    SAFE                    BOOL  FALSE
+    PSF.TARGET.AS.MAX       BOOL  TRUE      # Set the target PSF FWHM as the maximum of accepted input FWHM values.
+    PSF.TARGET.AS.MAX.EPSILON F32 0.1       # Amount to set the target PSF FWHM larger than the maximum input. Target = eps + max(input)
+    THRESHOLD.MASK  F32     0.1             # Threshold for mask deconvolution (0..1)
+    COMBINE.ITER    F32     0.5             # Number of rejection iterations per input
+    COMBINE.REJ     F32     3.0             # Rejection threshold in combination (sigma)
+    COMBINE.SYS     F32     0.1             # Relative systematic error in combination
+    COMBINE.DISCARD F32     0.2             # Discard fraction for Olympic weighted mean
+    IMAGE.REJ       F32     0.1             # Rejected pixel fraction threshold for rejecting entire image
+    NMINPIX         S32     4               # Minimum input per pixel
+    MASK.VAL        STR     SUSPECT,MASK.VALUE,CONV.BAD,GHOST # Mask value of input bad pixels
+    MASK.BLANKBORDER S32    20              # Mask blank border in final stack output
+    BSCALEOFFSET    BOOL    FALSE           # MUST be FALSE for PV3
+END
+
+STACK_MD_PV3_REF_1DG       METADATA  ## revised config for MD PV3 refstacks -- not complete
+    OUTPUT.NOCOMP           BOOL  TRUE
+    OUTPUT.LOGFLUX          BOOL  FALSE
+    OUTPUT.REPLICATE        BOOL  TRUE
+    STACK.TYPE              STR   DEEP_STACK  ## needed?
+    PSF.INPUT.CLIP.SIMPLE   BOOL  True
+    PSF.INPUT.MAX           F32   7.0   # center/edge can vary 1-2 pix, prefer <6 pix but needs to be <7 pix for edges
+    PSF.INPUT.CLIP.NSIGMA   F32   1.0   # sample typically on rising side of distribution (set even smaller?)
+    PSF.INPUT.THRESH        F32   NAN
+    PSF.INPUT.ASYMMETRY     F32   NAN
+    MATCH.REJ               F32   4.0   # need ot tune?
+    SAFE                    BOOL  FALSE
+    PSF.TARGET.AS.MAX       BOOL  TRUE      # Set the target PSF FWHM as the maximum of accepted input FWHM values.
+    PSF.TARGET.AS.MAX.EPSILON F32 0.1       # Amount to set the target PSF FWHM larger than the maximum input. Target = eps + max(input)
+    THRESHOLD.MASK  F32     0.1             # Threshold for mask deconvolution (0..1)
+    COMBINE.ITER    F32     0.5             # Number of rejection iterations per input
+    COMBINE.REJ     F32     3.0             # Rejection threshold in combination (sigma)
+    COMBINE.SYS     F32     0.1             # Relative systematic error in combination
+    COMBINE.DISCARD F32     0.2             # Discard fraction for Olympic weighted mean
+    IMAGE.REJ       F32     0.1             # Rejected pixel fraction threshold for rejecting entire image
+    NMINPIX         S32     4               # Minimum input per pixel
+    MASK.VAL        STR     SUSPECT,MASK.VALUE,CONV.BAD,GHOST # Mask value of input bad pixels
+    MASK.BLANKBORDER S32    20              # Mask blank border in final stack output
+    BSCALEOFFSET    BOOL    FALSE           # MUST be FALSE for PV3
+END
+
+STACK_MD_PV3_DEEP_1DG     METADATA   ## revised config for MD PV3 deepstack -- not complete
+    OUTPUT.NOCOMP           BOOL  TRUE
+    OUTPUT.LOGFLUX          BOOL  FALSE
+    OUTPUT.REPLICATE        BOOL  TRUE
+    STACK.TYPE              STR   DEEP_STACK
+    PSF.INPUT.CLIP.SIMPLE   BOOL  True
+    PSF.INPUT.MAX           F32   10.0    #  
+    PSF.INPUT.CLIP.NSIGMA   F32   100.0
+    PSF.INPUT.THRESH        F32   NAN
+    PSF.INPUT.ASYMMETRY     F32   NAN
+    MATCH.REJ               F32   4.0     # need ot tune?
+    SAFE                    BOOL  FALSE
+    PSF.TARGET.AS.MAX  BOOL  TRUE
+    PSF.TARGET.AS.MAX.EPSILON F32 0.1
+    THRESHOLD.MASK  F32     0.1             # Threshold for mask deconvolution (0..1)
+    COMBINE.ITER    F32     0.5             # Number of rejection iterations per input
+    COMBINE.REJ     F32     3.0             # Rejection threshold in combination (sigma)
+    COMBINE.SYS     F32     0.1             # Relative systematic error in combination
+    COMBINE.DISCARD F32     0.2             # Discard fraction for Olympic weighted mean
+    IMAGE.REJ       F32     0.1             # Rejected pixel fraction threshold for rejecting entire image
+    NMINPIX         S32     30              # Minimum input per pixel 
+    MASK.VAL        STR     SUSPECT,MASK.VALUE,CONV.BAD,GHOST # Mask value of input bad pixels
+    MASK.BLANKBORDER S32    20              # Mask blank border in final stack output
+    BSCALEOFFSET    BOOL    FALSE           # MUST be FALSE for PV3
+END
+
+
+
 # Recipe overrides for STACK
 STACKPHOT METADATA
Index: /tags/ipp-20141024/ippconfig/gpc1/psastro.config
===================================================================
--- /tags/ipp-20141024/ippconfig/gpc1/psastro.config	(revision 37987)
+++ /tags/ipp-20141024/ippconfig/gpc1/psastro.config	(revision 37988)
@@ -398,4 +398,157 @@
 END
 
+MD_ASTRO METADATA
+   ZERO.POINT.BY.EXPOSURE      BOOL  TRUE
+   ZERO.POINT.USE.MEAN         BOOL  TRUE
+   DVO.GETSTAR.MAX.RHO         F32   6000.0  # was globally changed to 10k, seems >6k more than 1600 NREF
+   PSASTRO.GRID.NREF.MAX       S32   1600    # max stars for grid search from ref catalog
+   PSASTRO.GRID.NRAW.MAX       S32   800     # max stars for grid search from raw/image  
+   DVO.GETSTAR.MIN.MAG         F32   12.0    # leave similar to default, MD in marginal weather and can lose 1-3 mags 
+   DVO.GETSTAR.MIN.MAG.INST    F32   -25.0
+   PSASTRO.CATDIR              STR   PS1.REF.20140713
+   ## refcat may be okay astrom but not faint phot -- so need to be able to further limit by ZP 
+   ## -- concern w/ new refcat >20 extra sources w/ larger merr over-dominating scatter in ZP 
+   ## -- CLAMP will limit both astrom and zp -- 
+   REFSTAR_CLAMP_MAG_MAX       F32   22.0
+   REFSTAR.ZP.MIN.MAG          F32   12.0
+   REFSTAR.ZP.MAX.MAG          F32   19.0
+   ## for ZP need extra limit by filter to avoid poorer photometry scatter on faint end
+   ## -- cannot restrict on bright end due to possible marginal nights
+   ## -- set for merr<0.02-0.03 in refcat, ~similar for each MD field
+#   PHOTCODE.DATA MULTI UPDATE
+   PHOTCODE.DATA MULTI   
+   PHOTCODE.DATA METADATA
+     FILTER   STR g
+     PHOTCODE STR g
+     REFSTAR.ZP.MAX.MAG    F32  19.0
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR r
+     PHOTCODE STR r
+     REFSTAR.ZP.MAX.MAG    F32  19.0
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR i
+     PHOTCODE STR i
+     REFSTAR.ZP.MAX.MAG    F32  18.5
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR z
+     PHOTCODE STR z
+     REFSTAR.ZP.MAX.MAG    F32  18.5
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR y
+     PHOTCODE STR y
+     REFSTAR.ZP.MAX.MAG    F32  17.5
+   END
+END
+
+
+CNP_ASTRO_PV3 METADATA
+   PSASTRO.MAX.ALLOWED.FWHM    F32   25  # larger number typical >12, so include 2x larger
+   PSASTRO.CATDIR              STR   PS1.REF.20140713
+   PSASTRO.GRID.MIN.ANGLE      F32   -20
+   PSASTRO.GRID.MAX.ANGLE      F32   +20
+   PSASTRO.GRID.DEL.ANGLE      F32   0.5
+   PSASTRO.FIELD.PADDING       F32   0.5
+   DVO.GETSTAR.MAX.RHO         F32   5000.0  # globally changed to 10k, huge numbers in soln and larger sigma
+   DVO.GETSTAR.MIN.MAG         F32   8.0     # can go brighter in marginal nights (like MD done every night as possible) 
+   PSASTRO.GRID.NREF.MAX       S32   1600    # defaults 
+   PSASTRO.GRID.NRAW.MAX       S32   800    
+   PSASTRO.MIN.INST.MAG.RAW       F32     -15.0   # min instrumental magnitude for stars accepted for fitting
+   PSASTRO.MAX.INST.MAG.RAW       F32      -8.5   # reduce instrumental faint end a bit (cuts most if rho>5k anyways)
+   PSASTRO.GRID.MIN.INST.MAG.RAW  F32     -15.0   # min instrumental magnitude for stars accepted for grid search
+   PSASTRO.GRID.MAX.INST.MAG.RAW  F32      -8.0   # max instrumental magnitude for stars accepted for grid search
+
+   ZERO.POINT.USE.MEAN         BOOL  TRUE  
+   ## refcat -- want to be able to further limit for ZP, general settings 
+   ## -- concern w/ new refcat >20 extra sources w/ larger merr can over-dominating scatter in ZP 
+   ## -- CLAMP will limit both astrom and zp -- CNP are shorter exposures 
+   REFSTAR_CLAMP_MAG_MAX       F32   20.0  
+   REFSTAR.ZP.MIN.MAG          F32   8.0   
+   REFSTAR.ZP.MAX.MAG          F32   19.0
+   ## for ZP need extra restriction by filter to avoid poorer photometry scatter on faint end
+   ## -- ZP is by exposure so can easily reduce numbers  
+   ## -- cannot restrict on bright end due to possible marginal nights
+   ## -- set for merr<0.03 in refcat, similar to MD
+#   PHOTCODE.DATA MULTI UPDATE
+   PHOTCODE.DATA MULTI   
+   PHOTCODE.DATA METADATA
+     FILTER   STR g
+     PHOTCODE STR g
+     #REFSTAR.ZP.MAX.MAG    F32  19.0
+     REFSTAR.ZP.MAX.MAG    F32  18.0  # changed after PV3.20141111 run to match raw merr
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR r
+     PHOTCODE STR r
+     REFSTAR.ZP.MAX.MAG    F32  18.0
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR i
+     PHOTCODE STR i
+     REFSTAR.ZP.MAX.MAG    F32  18.0
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR z
+     PHOTCODE STR z
+     REFSTAR.ZP.MAX.MAG    F32  18.0
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR y
+     PHOTCODE STR y
+     REFSTAR.ZP.MAX.MAG    F32  17.0
+   END
+END
+
+MD_ASTRO_PV3 METADATA
+   ZERO.POINT.BY.EXPOSURE      BOOL  TRUE
+   ZERO.POINT.USE.MEAN         BOOL  TRUE
+   DVO.GETSTAR.MAX.RHO         F32   6000.0  # was globally changed to 10k
+   PSASTRO.GRID.NREF.MAX       S32   1600    # max stars for grid search from ref catalog
+   PSASTRO.GRID.NRAW.MAX       S32   800     # max stars for grid search from raw/image  
+   DVO.GETSTAR.MIN.MAG         F32   12.0    # leave similar to default, MD in marginal weather and can lose 1-3 mags 
+   DVO.GETSTAR.MIN.MAG.INST    F32   -25.0
+   PSASTRO.CATDIR              STR   PS1.REF.20140713
+   ## refcat may be okay astrom but not faint phot -- so need to be able to further limit by ZP 
+   ## -- concern w/ new refcat >20 extra sources w/ larger merr over-dominating scatter in ZP 
+   ## -- CLAMP will limit both astrom and zp -- 
+   REFSTAR_CLAMP_MAG_MAX       F32   22.0
+   REFSTAR.ZP.MIN.MAG          F32   12.0
+   REFSTAR.ZP.MAX.MAG          F32   19.0
+   ## for ZP need extra limit by filter to avoid poorer photometry scatter on faint end
+   ## -- cannot restrict on bright end due to possible marginal nights
+   ## -- set for merr<0.02-0.03 in refcat, ~similar for each MD field
+#   PHOTCODE.DATA MULTI UPDATE
+   PHOTCODE.DATA MULTI   
+   PHOTCODE.DATA METADATA
+     FILTER   STR g
+     PHOTCODE STR g
+     REFSTAR.ZP.MAX.MAG    F32  19.0
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR r
+     PHOTCODE STR r
+     REFSTAR.ZP.MAX.MAG    F32  19.0
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR i
+     PHOTCODE STR i
+     REFSTAR.ZP.MAX.MAG    F32  18.5
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR z
+     PHOTCODE STR z
+     REFSTAR.ZP.MAX.MAG    F32  18.5
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR y
+     PHOTCODE STR y
+     REFSTAR.ZP.MAX.MAG    F32  17.5
+   END
+END
+
+
 TEST_REFCAT METADATA
    PSASTRO.CATDIR		STR /data/ipp064.0/ipp/ippRefs/catdir.refcat.20120524.v0
@@ -526,8 +679,49 @@
 END
 
+## MD version
+## revised setup for DEEP/MDS stacks with range of inputs, should work for N~2(edges)--500 inputs with large MIN/MAX.INST.MAG.RAW
+STATICSKY_MD_DEEPCAL   METADATA
+   #PSASTRO.CATDIR                  STR   PS1.REF.20120524  # reset here to ensure same catalog is used when reprocessing set uniformly
+   #PSASTRO.CATDIR                  STR   PS1.REF.20140605  # ensure same catalog is used when reprocessing MD set uniformly (BAD catalog)
+   PSASTRO.CATDIR                  STR   PS1.REF.20140713 
+   REFSTAR_MASK                    BOOL  FALSE
+   PSASTRO.SAVE.REFMATCH           BOOL  FALSE
+   PSASTRO.FIX.CHIPS               BOOL  FALSE
+   PSASTRO.USE.MODEL               BOOL  FALSE
+   PSASTRO.MOSAIC.MODE             BOOL  FALSE
+   ZERO.POINT.USE.MEAN             BOOL  TRUE
+   PSASTRO.FIELD.PADDING           F32   0.01   # skycells are well defined, don't need large padding. 1% of 6400 is 64 pixels
+   PSASTRO.MAX.NRAW                S32   1500   # include more than MAX.NREF to account for extras like false-positives
+   PSASTRO.MAX.NREF            	   S32   1000   # if edge skycell ~10% then try ~50 to match. increase to help odd bulk offsets
+   PSASTRO.MIN.INST.MAG.RAW        F32   -25.0  # 
+   PSASTRO.MAX.INST.MAG.RAW        F32   -5.0
+   PSASTRO.GRID.SEARCH 		   BOOL  FALSE  # skycell/stack and astrom well known, when used could cause problems and not help
+   #PSASTRO.GRID.NRAW.MAX           S32   2000
+   #PSASTRO.GRID.NREF.MAX 	    S32   2000
+   #PSASTRO.GRID.MIN.INST.MAG.RAW   F32   -21.0
+   #PSASTRO.GRID.MAX.INST.MAG.RAW   F32   -14.0
+   DVO.GETSTAR.MAX.RHO		   F32	 6000.0  # N/sqdeg, slightly larger than MAX.NREF 
+   DVO.GETSTAR.MIN.MAG.INST        F32   -25.0   # set absurd range to cover large range of exptime, then clamp...
+   DVO.GETSTAR.MIN.MAG		   F32	 16.5    # only used if keyword problem so also using CLAMP_MAG_MIN
+   REFSTAR_CLAMP_MAG_MIN 	   F32	 16.5    # saturation in MD ~14 even in y-band, so toss out brighter in the list. seeing offset in brighter stars
+   DVO.GETSTAR.FIXED.ZEROPT        F32   0.0     # not really needed, exptime overly dominates
+  
+   ## for skycell/stack astrom well known, radius shouldn't need to be as large as 90 pixel default. in deeper stacks can be problems
+   PSASTRO.MATCH.RADIUS.N0         F32   5 
+   PSASTRO.MATCH.RADIUS.N1 	   F32   3 
+   PSASTRO.MATCH.RADIUS.N2 	   F32   1
+   PSASTRO.MATCH.RADIUS.N3 	   F32   0.5
+   PSASTRO.MATCH.FIT.NITER 	   S32   4 
+   PSASTRO.MAX.ERROR       	   F32   0.2  # in arcsec units, still liberal limit for well known astrometry for skycells
+   PSASTRO.MIN.NSTAR       	   S32   10   # edges may not have many and require special treatment 
+
+   KH.CORRECT.APPLY              BOOL     FALSE
+END
+
+
 ## if defined for MD, needs to have MD name in group so shouldn't be modified except for MD work
-## -- ad hoc limit for NREF due to issues fainter than 19 mag in all filters and brighter than 17.0 mag (except y ~16.5) in MD
-## -- better solution and other issues need to really be dealt with for PV3
-STATICSKY_MD_DEEPCAL   METADATA
+## -- was ad hoc limit for NREF~150, rho~6000 due to issues fainter than 19 mag in all filters and brighter than 17.0 mag (except y ~16.5) in MD
+## -- for PV3 add refcat min/max mag REFSTAR.ZP.MIN.MAG for ZP range only 
+STATICSKY_MD_PV3_DEEPCAL   METADATA
    PSASTRO.CATDIR                  STR   PS1.REF.20140713
    REFSTAR_MASK                    BOOL  FALSE
@@ -539,5 +733,5 @@
    PSASTRO.FIELD.PADDING           F32   0.01   # skycells are well defined, don't need large padding. 1% of 6400 is 64 pixels
    PSASTRO.MAX.NRAW                S32   1500   # include more than MAX.NREF to account for extras like false-positives
-   PSASTRO.MAX.NREF                S32   150    # if edge skycell ~10% then try ~50 to match. increase to help odd bulk offsets
+   PSASTRO.MAX.NREF                S32   500    # if edge skycell ~10% then try ~50 to match. increase to help odd bulk offsets
    PSASTRO.MIN.INST.MAG.RAW        F32   -25.0  # 
    PSASTRO.MAX.INST.MAG.RAW        F32   -5.0
@@ -546,6 +740,6 @@
    DVO.GETSTAR.MAX.RHO             F32   6000.0  # N/sqdeg, slightly larger than MAX.NREF 
    DVO.GETSTAR.MIN.MAG.INST        F32   -25.0   # set absurd range to cover large range of exptime, then clamp...
-   DVO.GETSTAR.MIN.MAG             F32   17.0    # only used if keyword problem so also using CLAMP_MAG_MIN
-   REFSTAR_CLAMP_MAG_MIN           F32   17.0    # saturation in MD ~14 even in y-band, so toss out brighter in the list. seeing offset in brighter stars
+   DVO.GETSTAR.MIN.MAG             F32   15.0    # only used if keyword problem so also using CLAMP_MAG_MIN
+   REFSTAR_CLAMP_MAG_MIN           F32   15.0    # saturation in MD ~14 even in y-band, so toss out brighter in the list. offset in brighter stars
    DVO.GETSTAR.FIXED.ZEROPT        F32   0.0     # not really needed, exptime overly dominates
 
@@ -559,4 +753,41 @@
    PSASTRO.MIN.NSTAR               S32   10   # edges may not have many and require special treatment 
 
-   KH.CORRECT.APPLY              BOOL     FALSE
-END
+   KH.CORRECT.APPLY                BOOL     FALSE
+
+   REFSTAR.ZP.MIN.MAG      F32  15.0  # general refcat zp min/max setting -- need to use photcode below
+   REFSTAR.ZP.MAX.MAG      F32  19.0
+   ## set min to avoid bright turnover and max to limit merr<0.04
+   ## -- 
+#   PHOTCODE.DATA MULTI UPDATE
+   PHOTCODE.DATA MULTI
+   PHOTCODE.DATA METADATA
+     FILTER   STR g
+     PHOTCODE STR g
+     REFSTAR.ZP.MIN.MAG    F32  17.0  #
+     REFSTAR.ZP.MAX.MAG    F32  20.0  #
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR r
+     PHOTCODE STR r
+     REFSTAR.ZP.MIN.MAG    F32  17.0  #
+     REFSTAR.ZP.MAX.MAG    F32  19.0  #
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR i 
+     PHOTCODE STR i
+     REFSTAR.ZP.MIN.MAG    F32  17.0  #
+     REFSTAR.ZP.MAX.MAG    F32  19.0  #
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR z
+     PHOTCODE STR z
+     REFSTAR.ZP.MIN.MAG    F32  16.0  #
+     REFSTAR.ZP.MAX.MAG    F32  18.0  #
+   END
+   PHOTCODE.DATA METADATA
+     FILTER   STR y
+     PHOTCODE STR y
+     REFSTAR.ZP.MIN.MAG    F32  15.0  #
+     REFSTAR.ZP.MAX.MAG    F32  17.0  #
+   END
+END
Index: /tags/ipp-20141024/ippconfig/recipes/nightly_science.config
===================================================================
--- /tags/ipp-20141024/ippconfig/recipes/nightly_science.config	(revision 37987)
+++ /tags/ipp-20141024/ippconfig/recipes/nightly_science.config	(revision 37988)
@@ -87,4 +87,17 @@
 END
 TARGETS METADATA
+  NAME         STR SNIa
+  DISTRIBUTION STR SNIa.test
+  TESS         STR RINGS.V3
+  OBSMODE      STR SNIa
+  STACKABLE   BOOL FALSE
+  DIFFABLE    BOOL FALSE
+  OFFNIGHT_DIFFS BOOL FALSE
+  ## change days till cleanup
+#  CHIP           S16  7
+#  WARP           S16  7
+#  DIFF           S16  7
+END
+TARGETS METADATA
   NAME      STR  MD01
   DISTRIBUTION STR MD01
@@ -95,4 +108,5 @@
   MIN_STACK U16 8
   DIFFABLE  BOOL FALSE
+  REDUCTION STR MD_DEFAULT
   DIST      S16  30
 END
@@ -106,4 +120,5 @@
   MIN_STACK U16 8
   DIFFABLE  BOOL FALSE
+  REDUCTION STR MD_DEFAULT
   DIST      S16 30
 END
@@ -117,4 +132,5 @@
   MIN_STACK U16 8
   DIFFABLE  BOOL FALSE
+  REDUCTION STR MD_DEFAULT
   DIST      S16 30
 END
@@ -128,4 +144,5 @@
   MIN_STACK U16 8
   DIFFABLE  BOOL FALSE
+  REDUCTION STR MD_DEFAULT
   DIST      S16 30
 END
@@ -139,5 +156,10 @@
   MIN_STACK U16 8
   DIFFABLE  BOOL FALSE
-  DIST      S16 30
+  REDUCTION STR MD_DEFAULT
+  DIST      S16 30
+  ## change days till cleanup
+  #CHIP S16 60
+  WARP S16 60
+  DIFF S16 60
 END
 TARGETS METADATA
@@ -150,5 +172,10 @@
   MIN_STACK U16 8
   DIFFABLE  BOOL FALSE
-  DIST      S16 30
+  REDUCTION STR MD_DEFAULT
+  DIST      S16 30
+  ## change days till cleanup
+  #CHIP S16 60
+  WARP S16 60
+  DIFF S16 60
 END
 TARGETS METADATA
@@ -161,5 +188,10 @@
   MIN_STACK U16 8
   DIFFABLE  BOOL FALSE
-  DIST      S16 30
+  REDUCTION STR MD_DEFAULT
+  DIST      S16 30
+  ## change days till cleanup
+  #CHIP S16 60
+  WARP S16 60
+  DIFF S16 60
 END
 TARGETS METADATA
@@ -172,5 +204,10 @@
   MIN_STACK U16 8
   DIFFABLE  BOOL FALSE
-  DIST      S16 30
+  REDUCTION STR MD_DEFAULT
+  DIST      S16 30
+  ## change days till cleanup
+  #CHIP S16 60
+  WARP S16 60
+  DIFF S16 60
 END
 TARGETS METADATA
@@ -183,4 +220,5 @@
   MIN_STACK U16 8
   DIFFABLE  BOOL FALSE
+  REDUCTION STR MD_DEFAULT
   DIST      S16 30
 END
@@ -194,4 +232,5 @@
   MIN_STACK U16 8
   DIFFABLE  BOOL FALSE
+  REDUCTION STR MD_DEFAULT
   DIST      S16 30
 END
Index: /tags/ipp-20141024/ippconfig/recipes/ppStack.config
===================================================================
--- /tags/ipp-20141024/ippconfig/recipes/ppStack.config	(revision 37987)
+++ /tags/ipp-20141024/ippconfig/recipes/ppStack.config	(revision 37988)
@@ -169,4 +169,5 @@
 STACK_THREEPI_ALT  METADATA
 END
+
 STACK_CNP  METADATA
 END
@@ -199,4 +200,28 @@
 STACK_MDTHREEPI_DEEP_1DG   METADATA
 END
+
+STACK_MD_PV3_IQ_1DG    METADATA
+END
+
+STACK_MD_PV3_REF_1DG    METADATA
+END
+
+STACK_MD_PV3_DEEP_1DG   METADATA
+END
+
+STACK_MD_PV3_NIGHT_1DG  METADATA
+    STACK.TYPE      STR     NIGHTLY_STACK
+END
+
+STACK_CNP_PV3_IQ_1DG    METADATA
+END
+
+STACK_CNP_PV3_REF_1DG    METADATA
+END
+
+STACK_CNP_PV3_DEEP_1DG   METADATA
+END
+
+
 
 
Index: /tags/ipp-20141024/ippconfig/recipes/psastro.config
===================================================================
--- /tags/ipp-20141024/ippconfig/recipes/psastro.config	(revision 37987)
+++ /tags/ipp-20141024/ippconfig/recipes/psastro.config	(revision 37988)
@@ -174,4 +174,9 @@
 REFSTAR_CLAMP_MAG_MAX           F32 30.0
 
+# restrict ref catalog for zero point determination 
+# -- can be put into photcode groups as well
+REFSTAR.ZP.MAX.MAG  F32  30.0
+REFSTAR.ZP.MIN.MAG  F32  -3.0
+
 EXTRACT_MAX_MAG                 F32 -15.0
 
@@ -276,4 +281,13 @@
 END
 
+MD_ASTRO METADATA
+END
+
+CNP_ASTRO_PV3 METADATA
+END
+
+MD_ASTRO_PV3 METADATA
+END
+
 SYNTH_CAT METADATA
 END
@@ -301,2 +315,5 @@
 END
 
+STATICSKY_MD_PV3_DEEPCAL   METADATA
+END
+
Index: /tags/ipp-20141024/ippconfig/recipes/reductionClasses.mdc
===================================================================
--- /tags/ipp-20141024/ippconfig/recipes/reductionClasses.mdc	(revision 37987)
+++ /tags/ipp-20141024/ippconfig/recipes/reductionClasses.mdc	(revision 37988)
@@ -200,5 +200,29 @@
 END
 
-
+# MD basic/nightly science analysis
+MD_DEFAULT		METADATA
+	CHIP_PPIMAGE	  STR	  CHIP
+	CHIP_PSPHOT	  STR	  CHIP
+	WARP_PSWARP	  STR	  WARP
+	STACK_PPSTACK	  STR	  STACK
+	STACK_PPSUB	  STR	  STACK
+	STACK_PSPHOT	  STR	  STACK
+	DIFF_PPSUB	  STR	  DIFF
+	DIFF_PSPHOT	  STR	  DIFF
+	JPEG_BIN1	  STR	  PPIMAGE_J1
+	JPEG_BIN2	  STR	  PPIMAGE_J2
+	FAKEPHOT	  STR	  FAKEPHOT
+	ADDSTAR		  STR	  ADDSTAR
+	PSASTRO		  STR	  MD_ASTRO
+	STACKPHOT_PSPHOT  STR     STACKPHOT
+	STACKPHOT_PPSUB   STR     STACKPHOT
+	STACKPHOT_PPSTACK STR     STACKPHOT
+	STACKPHOT_SINGLE_PSPHOT  STR     STACKPHOT_SINGLE
+	BACKGROUND_PPBACKGROUND	STR	BACKGROUND
+	BACKGROUND_PSWARP	STR	BACKGROUND
+        PSVIDEOPHOT             STR     PSVIDEOPHOT
+        STATICSKY_CALIBRATION   STR     STATICSKY_CAL
+        FULLFORCE_PSPHOT  STR   FULLFORCE_WARP
+END
 
 # basic science analysis
@@ -569,4 +593,12 @@
 	JPEG_BIN1	STR	PPIMAGE_J1
 	JPEG_BIN2	STR	PPIMAGE_J2
+END
+
+# specific for PV3 WSdiff processing to do inverse phot
+WARPSTACK_PV3       METADATA
+        DIFF_PPSUB      STR     WARPSTACK_PV3
+        DIFF_PSPHOT     STR     DIFF
+        JPEG_BIN1       STR     PPIMAGE_J1
+        JPEG_BIN2       STR     PPIMAGE_J2
 END
 
@@ -716,4 +748,83 @@
       STATICSKY_CALIBRATION   STR     STATICSKY_MD_DEEPCAL
 END
+
+## MD stacks -- PV3 version (NO bscale correction and other changes)
+MD_PV3_NIGHT_STACK_1DG         METADATA
+      STACK_PPSTACK   STR      STACK_MD_PV3_NIGHT_1DG
+      STACK_PPSUB     STR      STACK_1DG
+      STACK_PSPHOT    STR      STACK
+      STACKPHOT_PSPHOT        STR     STACKPHOT
+      STATICSKY_CALIBRATION   STR     STATICSKY_NIGHTCAL
+END
+
+MD_PV3_IQ_STACK_1DG            METADATA
+      STACK_PPSTACK           STR     STACK_MD_PV3_IQ_1DG
+      STACK_PPSUB             STR     STACK_1DG
+      STACK_PSPHOT            STR     STACK
+      STACKPHOT_PSPHOT        STR     STACKPHOT
+      STACKPHOT_PPSUB         STR     STACKPHOT_1DG 
+      STACKPHOT_PPSTACK       STR     STACKPHOT 
+      STACKPHOT_SINGLE_PSPHOT STR     STACKPHOT_SINGLE
+      STATICSKY_CALIBRATION   STR     STATICSKY_MD_PV3_DEEPCAL 
+END
+
+MD_PV3_REF_STACK_1DG            METADATA
+      STACK_PPSTACK           STR     STACK_MD_PV3_REF_1DG
+      STACK_PPSUB             STR     STACK_1DG
+      STACK_PSPHOT            STR     STACK
+      STACKPHOT_PSPHOT        STR     STACKPHOT
+      STACKPHOT_PPSUB         STR     STACKPHOT_1DG 
+      STACKPHOT_PPSTACK       STR     STACKPHOT 
+      STACKPHOT_SINGLE_PSPHOT STR     STACKPHOT_SINGLE
+      STATICSKY_CALIBRATION   STR     STATICSKY_MD_PV3_DEEPCAL 
+END
+
+MD_PV3_DEEP_STACK_1DG            METADATA
+      STACK_PPSTACK           STR     STACK_MD_PV3_DEEP_1DG
+      STACK_PPSUB             STR     STACK_1DG
+      STACK_PSPHOT            STR     STACK
+      STACKPHOT_PSPHOT        STR     STACKPHOT
+      STACKPHOT_PPSUB         STR     STACKPHOT_1DG 
+      STACKPHOT_PPSTACK       STR     STACKPHOT 
+      STACKPHOT_SINGLE_PSPHOT STR     STACKPHOT_SINGLE
+      STATICSKY_CALIBRATION   STR     STATICSKY_MD_PV3_DEEPCAL 
+END
+
+## CNP stacks -- PV3 version (NO bscale correction and other changes)
+CNP_PV3_IQ_STACK_1DG            METADATA
+      STACK_PPSTACK           STR     STACK_CNP_PV3_IQ_1DG
+      STACK_PPSUB             STR     STACK_1DG
+      STACK_PSPHOT            STR     STACK
+      STACKPHOT_PSPHOT        STR     STACKPHOT
+      STACKPHOT_PPSUB         STR     STACKPHOT_1DG 
+      STACKPHOT_PPSTACK       STR     STACKPHOT 
+      STACKPHOT_SINGLE_PSPHOT STR     STACKPHOT_SINGLE
+      STATICSKY_CALIBRATION   STR     STATICSKY_MD_PV3_DEEPCAL 
+END
+
+CNP_PV3_REF_STACK_1DG            METADATA
+      STACK_PPSTACK           STR     STACK_CNP_PV3_REF_1DG
+      STACK_PPSUB             STR     STACK_1DG
+      STACK_PSPHOT            STR     STACK
+      STACKPHOT_PSPHOT        STR     STACKPHOT
+      STACKPHOT_PPSUB         STR     STACKPHOT_1DG 
+      STACKPHOT_PPSTACK       STR     STACKPHOT 
+      STACKPHOT_SINGLE_PSPHOT STR     STACKPHOT_SINGLE
+      STATICSKY_CALIBRATION   STR     STATICSKY_MD_PV3_DEEPCAL 
+END
+
+CNP_PV3_DEEP_STACK_1DG            METADATA
+      STACK_PPSTACK           STR     STACK_CNP_PV3_DEEP_1DG
+      STACK_PPSUB             STR     STACK_1DG
+      STACK_PSPHOT            STR     STACK
+      STACKPHOT_PSPHOT        STR     STACKPHOT
+      STACKPHOT_PPSUB         STR     STACKPHOT_1DG 
+      STACKPHOT_PPSTACK       STR     STACKPHOT 
+      STACKPHOT_SINGLE_PSPHOT STR     STACKPHOT_SINGLE
+      STATICSKY_CALIBRATION   STR     STATICSKY_MD_PV3_DEEPCAL 
+END
+
+
+
 
 
@@ -1311,4 +1422,53 @@
         STATICSKY_CALIBRATION   STR     STATICSKY_CAL
 END
+
+CNP_PV3     METADATA
+        CHIP_PPIMAGE      STR     CHIP
+        CHIP_PSPHOT       STR     CHIP
+        WARP_PSWARP       STR     WARP
+        STACK_PPSTACK     STR     STACK
+        STACK_PPSUB       STR     STACK
+        STACK_PSPHOT      STR     STACK
+        DIFF_PPSUB        STR     DIFF
+        DIFF_PSPHOT       STR     DIFF
+        JPEG_BIN1         STR     PPIMAGE_J1
+        JPEG_BIN2         STR     PPIMAGE_J2
+        FAKEPHOT          STR     FAKEPHOT
+        ADDSTAR           STR     ADDSTAR
+        PSASTRO           STR     CNP_ASTRO_PV3
+        STACKPHOT_PSPHOT  STR     STACKPHOT
+        STACKPHOT_PPSUB   STR     STACKPHOT
+        STACKPHOT_PPSTACK STR     STACKPHOT
+        STACKPHOT_SINGLE_PSPHOT  STR     STACKPHOT_SINGLE
+        BACKGROUND_PPBACKGROUND STR     BACKGROUND
+        BACKGROUND_PSWARP       STR     BACKGROUND
+        PSVIDEOPHOT             STR     PSVIDEOPHOT
+        STATICSKY_CALIBRATION   STR     STATICSKY_CAL
+END
+
+MD_PV3     METADATA
+        CHIP_PPIMAGE      STR     CHIP
+        CHIP_PSPHOT       STR     CHIP
+        WARP_PSWARP       STR     WARP
+        STACK_PPSTACK     STR     STACK_1DG
+        STACK_PPSUB       STR     STACK
+        STACK_PSPHOT      STR     STACK_1DG
+        DIFF_PPSUB        STR     DIFF
+        DIFF_PSPHOT       STR     DIFF
+        JPEG_BIN1         STR     PPIMAGE_J1
+        JPEG_BIN2         STR     PPIMAGE_J2
+        FAKEPHOT          STR     FAKEPHOT
+        ADDSTAR           STR     ADDSTAR
+        PSASTRO           STR     MD_ASTRO_PV3
+        STACKPHOT_PSPHOT  STR     STACKPHOT
+        STACKPHOT_PPSUB   STR     STACKPHOT
+        STACKPHOT_PPSTACK STR     STACKPHOT
+        STACKPHOT_SINGLE_PSPHOT  STR     STACKPHOT_SINGLE
+        BACKGROUND_PPBACKGROUND STR     BACKGROUND
+        BACKGROUND_PSWARP       STR     BACKGROUND
+        PSVIDEOPHOT             STR     PSVIDEOPHOT
+        STATICSKY_CALIBRATION   STR     STATICSKY_MD_PV3_DEEPCAL
+END
+
 
 # random user tests (may not be stable or consistent)
Index: /tags/ipp-20141024/psastro/src/psastro.h
===================================================================
--- /tags/ipp-20141024/psastro/src/psastro.h	(revision 37987)
+++ /tags/ipp-20141024/psastro/src/psastro.h	(revision 37988)
@@ -154,7 +154,8 @@
 
 bool 		  psastroZeroPoint (pmConfig *config);
-psVector         *psastroZeroPointReadoutAccum(psVector *dMag, pmReadout *readout, float exptime);
+psVector         *psastroZeroPointReadoutAccum(psVector *dMag, pmReadout *readout, float exptime, float refminMag, float refmaxMag);
 bool              psastroZeroPointAnalysis (psMetadata *header, psVector *dMag, float zeropt, psMetadata *recipe);
 bool 		  psastroZeroPointFromRecipe (float *zeropt, float *exptime, float *ghostMaxMag, pmFPA *fpa, psMetadata *recipe);
+bool              psastroZeroPointRefMagLimitFromRecipe (float *refminMag, float *refmaxMag, pmFPA *fpa, psMetadata *recipe);
 
 psStats          *psastroStatsPercentile (psVector *myVector, psMetadata *recipe);
Index: /tags/ipp-20141024/psastro/src/psastroChooseRefstars.c
===================================================================
--- /tags/ipp-20141024/psastro/src/psastroChooseRefstars.c	(revision 37987)
+++ /tags/ipp-20141024/psastro/src/psastroChooseRefstars.c	(revision 37988)
@@ -66,7 +66,8 @@
     psF32 clampMagMin = psMetadataLookupF32 (&status, recipe, "REFSTAR_CLAMP_MAG_MIN");
     if (!status) clampMagMin = -5.0;
-    //MEH test
+    //MEH adding max option as well -- really should not be hardcoded..
     psF32 clampMagMax = psMetadataLookupF32 (&status, recipe, "REFSTAR_CLAMP_MAG_MAX");
     if (!status) clampMagMax = 30.0;
+    psWarning("Will skip/clamp refstar with brighter/fainter magnitude %f/%f\n",clampMagMin,clampMagMax); 
 
 
@@ -113,12 +114,16 @@
 
                     if (ref->Mag < clampMagMin) {
-                        psWarning("Skipping refstar with abusrd magnitude %f ra: %f dec: %f\n",
-                            ref->Mag, RAD_TO_DEG(ref->sky->r), RAD_TO_DEG(ref->sky->d));
+                        //psWarning("Skipping refstar with abusrd magnitude %f ra: %f dec: %f\n",
+                        //    ref->Mag, RAD_TO_DEG(ref->sky->r), RAD_TO_DEG(ref->sky->d));
+                        psTrace ("psastro", 6, "Skipping by clamp refstar with bright magnitude %f ra: %f dec: %f\n",
+			         ref->Mag, RAD_TO_DEG(ref->sky->r), RAD_TO_DEG(ref->sky->d));
                         goto skip;
                     }
-		    //MEH test
+		    //MEH option for max mag clamp also -- need to change psWarning (to log) to trace and above for bright..
                     if (ref->Mag > clampMagMax) {
-                        psWarning("Skipping by clamp refstar with faint magnitude %f ra: %f dec: %f\n",
-                            ref->Mag, RAD_TO_DEG(ref->sky->r), RAD_TO_DEG(ref->sky->d));
+                        //psWarning("Skipping by clamp refstar with faint magnitude %f ra: %f dec: %f\n",
+                        //    ref->Mag, RAD_TO_DEG(ref->sky->r), RAD_TO_DEG(ref->sky->d));
+			psTrace ("psastro", 6, "Skipping by clamp refstar with faint magnitude %f ra: %f dec: %f\n",
+			         ref->Mag, RAD_TO_DEG(ref->sky->r), RAD_TO_DEG(ref->sky->d));
                         goto skip;
                     }
Index: /tags/ipp-20141024/psastro/src/psastroZeroPoint.c
===================================================================
--- /tags/ipp-20141024/psastro/src/psastroZeroPoint.c	(revision 37987)
+++ /tags/ipp-20141024/psastro/src/psastroZeroPoint.c	(revision 37988)
@@ -20,4 +20,5 @@
     pmCell *cell = NULL;
     pmReadout *readout = NULL;
+    float refminMag,refmaxMag;
 
     // select the current recipe
@@ -59,6 +60,15 @@
     }
 
+    // MEH -- RefMagLimit for ZP with photcode option (merr probably better but not an option) 
+    // -- defualt is to use general config value (no hardcode..)
+    if (!psastroZeroPointRefMagLimitFromRecipe (&refminMag, &refmaxMag, fpa, recipe)) {
+        psLogMsg ("psastro", PS_LOG_INFO, "failed to load ref min,maxMag data from recipe");
+	return false;
+    }
+
     // if we measure the zero point by exposure, accumulate the dMag values here:
     psVector *dMag = NULL;
+    // MEH -- also the number of refcat sources used
+    int zpt_nref = 0;
 
     float fpaZP = 0.0;                  // Average zero point
@@ -81,10 +91,16 @@
 
                 // calculate dMag for the matched stars
-                dMag = psastroZeroPointReadoutAccum (dMag, readout, exptime);
-
+                //dMag = psastroZeroPointReadoutAccum (dMag, readout, exptime);
+                // MEH -- additional limits should be available for ZP if just robust mean and w/ stack skycal 
+		dMag = psastroZeroPointReadoutAccum (dMag, readout, exptime, refminMag, refmaxMag);
                 if (!byExposure) {
                     // calculate dMag for the matched stars just for this readout (well, chip)
                     psMetadata *header = psMetadataLookupMetadata (&status, readout->analysis, "PSASTRO.HEADER");
                     psastroZeroPointAnalysis (header, dMag, zeropt, recipe);
+		    // MEH -- per readout N refcat, min/max mag  -- added here, not part of median/edge process
+		    zpt_nref += dMag->n;
+		    psMetadataAddS32 (header, PS_LIST_TAIL, "ZPT_NREF", PS_META_REPLACE, "N refcat sources for zero point", dMag->n);
+		    psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_MIN", PS_META_REPLACE, "min refcat mag for zero point", refminMag);
+		    psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_MAX", PS_META_REPLACE, "max refcat mag for zero point", refmaxMag);
                     psFree (dMag);
                     dMag = NULL;
@@ -95,4 +111,5 @@
                         numZP++;
                     }
+
 
 
@@ -110,4 +127,6 @@
         }
         psastroZeroPointAnalysis (header, dMag, zeropt, recipe);
+	//MEH -- all N refcat sources here
+	zpt_nref = dMag->n;
         psFree (dMag);
         dMag = NULL;
@@ -141,9 +160,12 @@
                     // calculate dMag for the matched stars just for this readout (well, chip)
                     psMetadata *header = psMetadataLookupMetadata (&status, readout->analysis, "PSASTRO.HEADER");
-
                     psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_OBS", PS_META_REPLACE, "measured zero point",  zptObs);
                     psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_REF", PS_META_REPLACE, "reference zero point", zptRef);
                     psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_ERR", PS_META_REPLACE, "error on zero point",  zptErr);
                     psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_OFF", PS_META_REPLACE, "zero point offset",    zptOff);
+                    //MEH -- per readout  N refcat, MIN/MAX for consistency
+                    psMetadataAddS32 (header, PS_LIST_TAIL, "ZPT_NREF", PS_META_REPLACE, "N refcat sources for zero point", zpt_nref);
+		    psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_MIN", PS_META_REPLACE, "min refcat mag for zero point", refminMag);
+		    psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_MAX", PS_META_REPLACE, "max refcat mag for zero point", refmaxMag);
                 }
             }
@@ -151,4 +173,14 @@
     }
 
+    // MEH add N refcat, min/max mag to primary header 
+    psMetadata *header = psMetadataLookupMetadata (&status, fpa->analysis, "PSASTRO.HEADER");
+    if (!header) {
+	header = psMetadataAlloc ();
+	psMetadataAddMetadata (fpa->analysis, PS_LIST_TAIL, "PSASTRO.HEADER",  PS_META_REPLACE, "psastro header stats", header);
+	psFree (header);
+    }
+    psMetadataAddS32 (header, PS_LIST_TAIL, "ZPT_NREF", PS_META_REPLACE, "Total N refcat sources for zero point", zpt_nref);
+    psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_MIN", PS_META_REPLACE, "min refcat mag for zero point", refminMag);
+    psMetadataAddF32 (header, PS_LIST_TAIL, "ZPT_MAX", PS_META_REPLACE, "max refcat mag for zero point", refmaxMag);
 
     psMetadataItem *item = psMetadataLookup(fpa->concepts, "FPA.ZP");
@@ -162,5 +194,5 @@
  * accumulate the dMag values from this readout
  */
-psVector *psastroZeroPointReadoutAccum(psVector *dMag, pmReadout *readout, float exptime) {
+psVector *psastroZeroPointReadoutAccum(psVector *dMag, pmReadout *readout, float exptime, float refminMag, float refmaxMag) {
 
     bool status;
@@ -191,6 +223,9 @@
       // XXX I should be applying the airmass term here or in psastroLoadRefstars
       // (ie, ref->Mag would be a prediction for a given star.
-      float value = ref->Mag - raw->Mag - 2.5*log10(exptime);
-      psVectorAppend (dMag, value);
+      // MEH -- only append if in allowed ref mag range -- may want merr but not available..
+      if ( ref->Mag >= refminMag && ref->Mag <= refmaxMag ){
+        float value = ref->Mag - raw->Mag - 2.5*log10(exptime);
+        psVectorAppend (dMag, value);
+      }
     }
     return dMag;
@@ -490,5 +525,9 @@
                 continue;
             }
-        }
+	    //MEH null is a pain.. so only log if set
+            psLogMsg ("psastro", PS_LOG_INFO, "found GHOST_MAX_MAG %f",*ghostMaxMag);
+        }
+	//MEH what zpt is set to 
+        psLogMsg ("psastro", PS_LOG_INFO, "found ZEROPT  %f",*zeropt);
         psFree (iter);
         return true;
@@ -497,2 +536,75 @@
     return false;
 }
+
+
+
+bool psastroZeroPointRefMagLimitFromRecipe (float *refminMag, float *refmaxMag, pmFPA *fpa, psMetadata *recipe) {
+
+    bool status;
+    float tmpmin,tmpmax;
+
+    // MEH -- duplicate psastroZeroPointFromRecipe for ZeroPoint adding RefMag limits with photcode option for extra restrictions
+    // must have non-photcode dependent setting -- use as default rather than a hardcoded value 
+    *refminMag = psMetadataLookupF32 (&status, recipe, "REFSTAR.ZP.MIN.MAG");
+    //if (!status) *refminMag = -5.0;
+    if (!status) {
+        psLogMsg ("psastro", PS_LOG_INFO, "RefMagLimit is missing REFSTAR.ZP.MIN.MAG");
+        return false;
+    }
+    *refmaxMag = psMetadataLookupF32 (&status, recipe, "REFSTAR.ZP.MAX.MAG");
+    //if (!status) *refmaxMag = 30.0;
+    if (!status) {
+        psLogMsg ("psastro", PS_LOG_INFO, "RefMagLimit is missing REFSTAR.ZP.MAX.MAG");
+        return false;
+    }
+
+    // select the filter; default to any photcode and mag limit otherwise
+    char *filter = psMetadataLookupStr (&status, fpa->concepts, "FPA.FILTERID");
+    if (!status) ESCAPE ("RefMagLimit missing FPA.FILTER in concepts");
+
+    // need to select the PHOTCODE.DATA folder that matches our filter
+    psMetadataItem *item = psMetadataLookup (recipe, "PHOTCODE.DATA");
+    if (!item) ESCAPE ("RefMagLimit PHOTCODE.DATA folders missing");
+    if (item->type != PS_DATA_METADATA_MULTI) ESCAPE ("RefMagLimit PHOTCODE.DATA not a multi");
+
+    // PHOTCODE.DATA is a multi of metadata items
+    psListIterator *iter = psListIteratorAlloc(item->data.list, PS_LIST_HEAD, false);
+
+    psMetadataItem *refItem = NULL;
+    while ((refItem = psListGetAndIncrement (iter))) {
+        if (refItem->type != PS_DATA_METADATA) ESCAPE ("RefMagLimit PHOTCODE.DATA entry is not a metadata folder");
+
+        char *refFilter = psMetadataLookupStr (&status, refItem->data.md, "FILTER");
+        if (!status) {
+	    // always seems to happen once, commented out in ZeroPoint above as well
+            //psLogMsg ("psastro", PS_LOG_INFO, "RefMagLimit PHOTCODE.DATA recipe folder is missing FILTER");
+            continue;
+        }
+
+        // does this entry match the current filter?
+        if (strcmp (refFilter, filter)) continue;
+
+        psLogMsg ("psastro", PS_LOG_DETAIL, "RefMagLimit PHOTCODE.DATA found for filter %s", filter);
+
+        // -- may set one or other or both -- no reason to log if found or often not
+        tmpmin = psMetadataLookupF32 (&status, refItem->data.md, "REFSTAR.ZP.MIN.MAG");
+	if (status) {
+	    *refminMag = tmpmin;
+	} 
+        tmpmax = psMetadataLookupF32 (&status, refItem->data.md, "REFSTAR.ZP.MAX.MAG");
+	if (status) {
+	    *refmaxMag = tmpmax;
+	}
+        // once finds filter, should free and return true but multiple groups cause issue so just go through all groups now
+        //psFree (iter);
+        //return true;
+    }
+
+    // log out set values
+    psLogMsg ("psastro", PS_LOG_INFO, "using REFSTAR.ZP.MIN.MAG %f",*refminMag);
+    psLogMsg ("psastro", PS_LOG_INFO, "using REFSTAR.ZP.MAX.MAG %f",*refmaxMag);
+
+    psFree (iter);
+    return true;
+}
+
