;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
;pro Make_Bad_Mask_Automatically_xRG
;
;PURPOSE:
;  To make a bad pixel mask from the three bad masks generated by 
;  the find_[]_mask_xrg.pro scripts. 
;
;INPUTS
;  DetStr: str
;    The name of the detector
;  ElecStr: str
;    The name of the electronics
;
;NECESSARY INPUTS:
;  These inputs must be present in their respective directories.  
;  They are created with the find_[]_mask_xrg scripts
;
;  [DataDir]/HotPix/HotMask.fits
;  [DataDir]/LeakyPix/LeakyMask.fits
;  [DataDir]/DeadPix/DeadMask.fits
;
;OUTPUTS:
;  [DataDir]/BadPix/BadMask.fits
;
;KEYWORDS:
;  TVFLAG: int
;    0 - Default, Don't plot anything
;    1 - Plot to the X window
;EXAMPLES:
;
;**************************************************************************

pro Make_Bad_Mask_Automatically_xRG, DetStr, ElecStr, Date=Date, $
  TvFlag = TvFlag

If N_Elements(TvFlag) eq 0 then TvFlag = 0
If N_Elements(Date)   eq 0 then Date   = ''

;Include KeywordStruct.pro file with all the defaults for keywords
@KeywordStruct_xRG.pro
@PlotSettings_xRG.pro

;Leaky Pix
Fits_Read, KeyStr.MasterLeakyMapPath, LeakyIm, LeakyHeader
LeakyIndices = where(LeakyIm ne 0)

;Dead Pix
Fits_Read, KeyStr.MasterDeadMapPath, DeadIm, DeadHeader
DeadIndices  = where(DeadIm ne 0)

;Hot Pix
Fits_Read, KeyStr.HotMasterMapPath, HotIm, HotHeader
HotIndices   = where(HotIm ne 0)

;Bad Mask
BadMap = bytarr(KeyStr.NAxis1, KeyStr.NAxis2)
BadMap(LeakyIndices) = 1
BadMap(DeadIndices)  = 1
BadMap(HotIndices)   = 1

Fits_Write, KeyStr.BadMasterMapPath, BadMap
stop

end
