;pro Photometric_Analysis
;
;PURPOSE:
;
;	This routine will run through all of the IRAF adapted routines to retrun
;	a psf and CMD for a given .fits file
;
;
;INPUTS:
;	SlopeFileName - The name of the slopefit that contains the mask
;		        of saturated pixels.  In most cases it should 
;		        be the 0th dither
;
;KEYWORDS:
;
;	MODE -
;	0 - Plot 1 set of slopes for each radius for one star
;	1 - Plot the average of the slopes vs. radius for each star
;	2 - Scatter plot the values for a given read number vs. dither
;
;CALLING SEQUENCE:
;photometric_analysis,'/nfs/slac/g/ki/ki09/lances/Reduced/07Apr26/M13/M13_G_SlopeFlatFieldedCenteredDitheredMean.fits'
;****************************************************************************
pro Photometric_Analysis, SlopeFileName, $
	 RootDir = RootDir, Date = Date, $
	 ReducedDir = ReducedDir, ObjectName = ObjectName, $
	 SubtractDark = SubtractDark, FilterStr = FilterStr, $
	 TvFlag = TvFlag, Rem60Hz = Rem60Hz, $ 
	 Offset = Offset, Mode = Mode

If N_Elements(Mode) eq 0 then Mode = 0

Common SlopeParams, SlopeStr
Common KeyParams, KeyStr
Common ColorStr, Color, ColorNum, ColorTri, ColorNumTri

;Include all of the keywords from the KeywordStruct file
@KeywordStruct.pro
@PlotSettings.pro

;Get the saturated mask from the slope file
FileParams = Return_File_Params(SlopeFileName, /slope)
Fits_Read, SlopeFileName, SlopeIm, SlopeHeader

;Determine the name of the output file
If KeyStr.TvFlag eq 2 then $
   device, filename = KeyStr.PlotDir+KeyStr.ObjectName+'_RadialPersistence.ps'

;prepare some plotting indices
Masks	      = Return_Pixel_Masks()

;Find the regions that were occupied by bright stars as opposed to 
;very high E cosmic rays or messed up pixels
HMin     = 5.           ; minimum counts necessary to see a peak for find.pro
FWHM     = 9.           ; FWHM cutoff for find.pro
RoundLim = [-1.5,1.5]     ; roundness paramtetrs for find.pro
SharpLim = [0.2,1.]     ; sharpness parameters for find.pro
PhotAper = [5.]         ; aperture radius used by aper.pro
RdNoise  = 10.          ; readnoise used by psf.pro
PhPerADU = 1.           ; number of photons per adu
PSFRad   = 13.          ; radius of the circular area in which psf will be def
FitRad   = 9.
PSFName  = KeyStr.ObjectDir+KeyStr.RawKey+'PSF.fits'

;Next attempt at parameters
gamma=0
HMin=3.
FWHM=15.
RoundLim=[-1.,1.]
SharpLim=[0.2,1.]
PSFRad=40.
Fitrad=15.
RCrit=PSFRad+FitRad
rdnoise=.4
phperadu=1.
photaper=[10.]

;IRAF like routines to find sky, stars, etc. group will remove doubly counted
Sky,  SlopeIm, SkyMode, SkySig
Find, SlopeIm, XStar, YStar, Flux, Sharp, Roundness, HMin, $
	FWHM, RoundLim, SharpLim,/silent 
If KeyStr.TvFlag eq 1 then begin
   window, xsize=768, ysize=768
   tvscl, congrid(alog(SlopeIm),512,512)
   for i=0, N_Elements(XStar)-1 do $
	TvCircle, 1, XStar[i]*512./(Size(SlopeIm))[1], $
		     YStar[i]*512./(Size(SlopeIm))[2], $
				   fsc_color('green')
EndIf

Aper, SlopeIm, XStar, YStar, Mag, ErrAp, Sky, SkyErr, 1., PhotAper, -1, $
        [0,20000.], setskyval = SkyMode, /silent
NonFinite = where(finite(SlopeIm,/nan), NumNonFinite)
GoodStars  = where(Mag lt 25)
XStar = XStar(GoodStars) & YStar = YStar(GoodStars) & Mag = Mag(GoodStars)
Flux = Flux(GoodStars) & Sky = Sky(GoodStars)

GetPSF,SlopeIm,XStar,YStar,Mag,Sky,Rdnoise,PhPerADU,Gauss,psf,$
        where(mag lt 15 and mag gt 12 and $
                XStar gt 500 and XStar lt 1100 and $
                YStar gt 1000 and YStar lt 1800),$
                PSFRad,FitRad,PSFName

Group, XStar, YStar, RCrit-35, GroupNum
ID = Indgen(N_Elements(XStar))
If NumNonFinite gt 0 then Im(NonFinite)=0.
stop

NStar, SlopeIm, ID, XStar,YStar,Mag,Sky,GroupNum,PhPerADU,rdnoise,psfname,$
        print = KeyStr.ObjectDir+KeyStr.RawKey+'NStar.txt',/debug

;GetPSF, SlopeIm, XStar, YStar, Mag, Sky, RdNoise, PhPerADU, Gauss, PSF, $
;	where(XStar gt 300 and YStar gt 3000 and Mag lt 15) , $
;	PSFRad, FitRad, PSFName, /debug
stop
;These are the approximate centroids of the stars that will be used
XStar    = XStar(Uniq(GroupNum))
YStar    = YStar(Uniq(GroupNum))
NumStars = N_Elements(XStar)

If KeyStr.TvFlag eq 2 then device, /close
stop 

End
         
