;function return_pixel_masks
;
;PURPOSE:
;	To return arrays that hold the pixel masks
;KEYWORDS:
;	STANDALONE:
;	0-the function was called from an external program where the 
;	  common block of KeyParams was already initialized
;	1-the function is being called from the command line, so the
;	  common block of KeyParams will be initialized within the function
;
;OUTPUTS:
;	Masks - A structure with the following fields
;	 Masks.DeadMask    - The mask containing the values of the dead pixels
;	 Masks.DeadInd     - The indices of the dead pixels
;	 Masks.DeadNum     - The number of dead pixels
;	 Masks.LeakyMask   - The mask showing the locations of the leaky pixels
;	 Masks.LeakyInd    - The indices of the leaky pixels
;	 Masks.LeakyNum    - The number of Leaky Pixels
;	 Masks.LeakyNNMask - The mask of leaky pixels and nearest neighbors
;	 Masks.LeakyNNInd  - The indices of leaky pixels and nearest neighbors
;	 Masks.LeakyNNNum  - The number of Leaky Pixels and nearest neighbors
;        Masks.HotMask    - The mask containing the values of the hot pixels
;        Masks.HotInd     - The indices of the hot pixels
;        Masks.HotNum     - The number of hot pixels
;	 Masks.GoodPixels  - The indices of the pixels that look good for use
function return_pixel_masks, StandAlone=StandAlone

If N_Elements(StandAlone) ne 0 then begin
  @KeywordStruct.pro
endif

Common KeyParams, KeyStr

;Get the DEAD pixels
Fits_Read, KeyStr.MasterDeadMapPath, DeadMask, DeadHeader
DeadInd = where(DeadMask eq 1, DeadNum)

;Get the LEAKY pixels
Fits_Read, KeyStr.MasterLeakyMapPath, LeakyMask, LeakyHeader
;Make sure they're not dead
LeakyAndDead = LeakyMask and DeadMask
LeakyMask(where(LeakyAndDead eq 1)) = 0
LeakyInd   = where(LeakyMask eq 1, LeakyNum)

LeakyNNCheck = file_search(KeyStr.MasterLeakyNNMapPath)
If LeakyNNCheck(0) eq '' then begin
   print, 'Forming Leaky Neighbor Mask'
   LeakyNNMask = BytArr(KeyStr.Naxis1, KeyStr.Naxis2)
   For LeakSubInd = 0L, LeakyNum-1 do begin
     LeakySubCol = LeakyInd(LeakSubInd) mod KeyStr.Naxis1
     LeakySubRow = LeakyInd(LeakSubInd)  /  KeyStr.Naxis1
     LeakyNNMask(LeakySubCol-1:LeakySubCol+1,LeakySubRow-1:LeakySubRow+1) = 1
   EndFor
   Fits_Write, KeyStr.MasterLeakyNNMapPath, LeakyNNMask
EndIf  

Fits_Read, KeyStr.MasterLeakyNNMapPath, LeakyNNMask, LeakyNNHeader
LeakyNNInd = where(LeakyNNMask eq 1, LeakyNNNum)
Fits_Read, KeyStr.LeakyValsPath, LeakyVals, LeakyValsHeader

;Get the HOT pixels
Fits_Read, KeyStr.HotMasterMapPath, HotMask, HotHeader
HotInd = where(HotMask eq 1, HotNum)

;Get the High Dark Current Pixels (Hot?)
Fits_Read, KeyStr.HotMasterDir+'HighDarkCurrentMask.fits', HDCMask
HDCInd = where(HDCMask eq 1, HDCNum)

;Make a master mask
MasterBadMask = bytarr(KeyStr.Naxis1,KeyStr.Naxis2)
MasterBadMask(LeakyInd)  = 1 & MasterBadMask(DeadInd) = 1
MasterBadMask(HotInd)    = 1

MasterBadMask(0:KeyStr.Ch0W-1,*)  = 1  
MasterBadMask(KeyStr.Naxis1-1-KeyStr.Ref/2:KeyStr.Naxis1-1,*) = 1
MasterBadMask(*,0:KeyStr.Ref/2) = 1 
MasterBadMask(*,KeyStr.Naxis2-1-KeyStr.Ref/2:KeyStr.Naxis2-1)   = 1

;Good pixels?
GoodPixels = where(MasterBadMask ne 1)
BadPixels  = where(MasterBadMask eq 1)

;Form the structure that will be returned
Masks = { $
	DeadMask    : DeadMask, $
	DeadInd     : DeadInd, $
	DeadNum     : DeadNum, $
        LeakyMask   : LeakyMask, $
	LeakyInd    : LeakyInd, $
	LeakyNum    : LeakyNum, $
        LeakyNNMask : LeakyNNMask, $
        LeakyNNInd  : LeakyNNInd, $
        LeakyNNNum  : LeakyNNNum, $
	LeakyVals   : LeakyVals, $
        HotMask     : HotMask, $
        HotInd      : HotInd, $
        HotNum      : HotNum, $
	HDCMask	    : HDCMask, $
	HDCInd	    : HDCInd, $
	HDCNum      : HDCNum, $
	GoodPixels  : GoodPixels, $
	BadPixels   : BadPixels, $
	BadMask	    : MasterBadMask $
	}

return, Masks

end
