;pro Update_Pixel_Mask
;
;PURPOSE:
;	To open up a pixel mask for either dead, hot, or leaky pixels and 
;	modify it. 
;INPUTS:
;
;OUTPUTS:
;
;KEYWORDS:
;       ROOTDIR: 
;         The Directory holding the directories of Raw files
;       DATE:
;         The Date during which they were taken.  Also the subdirectory that 
;         the files are in.
;       REDUCEDDIR:
;         The directory where the processed images and plots will be placed in.
;       BLOCKLENGTH:
;         An offset for files that were written improperly.  The readu 
;         function reads the values with this offset in place.
;       OBJECTNAME:
;         The string that indicates what files are being sought.  The filenames
;         should begin with this string ('Dark', 'Flat', etc.).
;       NREADS:
;         The number of reads (NAXIS3) that are present in the file.  This
;         is also a string in the filename that will be used as a regular
;         expression in searching for matched files.
;       FIRSTREAD:
;         The first read to be used in the difference 
;             Diff = LastRead - FirstRead
;       LASTREAD: 
;         The last read to be used in the difference
;       THISFILTER:
;         The filter through which the files were taken. ('I', 'G', or 'Y')
;	OVERRIDE: 
;	  Do the processing even if the output file already exists
;	ALLFILES:
;	  1 - Process all files matching the #Reads, Filter, and Object
;	  0 - Only process one file (the first by default)
;**************************************************************************

function Update_Pixel_Mask, MaskType, NewMaskValues, TotalReads, MaskDir, $
			    ObjectType

;Check to see if a dead pixel map already exists
If MaskType eq 'Dead' then begin
  MaskFile = File_Search(MaskDir+'DeadPixelMap_'+ObjectType+'.fits')
Endif

;If it already exists, make a header and a new file
If MaskFile eq '' then begin 
  ;Write a .fits file showing the pixels with value dependent on how many 
  ;times they were flagged through the stack of images
  MkHdr, MaskHeader, NewMaskValues
  FxAddPar, MaskHeader, 'NREADS', TotalReads, $
	  'Total Number of reads pixels were examined in '
  FxAddPar, MaskHeader, 'NUMFILES', 1, $
	  'Total Number of files used to create mask'
  FxAddPar, MaskHeader, 'MAXPIXVL', TotalReads - 1, $
	  'Maximum value a pixel will have (dead from all reads)'
  Fits_Write, MaskDir+'DeadPixelMap_'+ObjectType+'.fits', $
	  NewMaskValues, MaskHeader

EndIf else begin
  
  Fits_Read, MaskFile, MaskValues, MaskHeader
  FxAddPar, MaskHeader, 'NREADS',long(SxPar(MaskHeader,'NREADS'))+TotalReads, $
	  'Total Number of reads pixels were examined in '
  FxAddPar, MaskHeader, 'NUMFILES',long(SxPar(MaskHeader,'NUMFILES')) + 1, $
          'Total Number of files used to create mask'
  FxAddPar, MaskHeader, 'MAXPIXVL',long(SxPar(MaskHeader,'MAXPIXVL'))+TotalReads-1,$
	  'Maximum value a pixel will have (dead from all reads)'
  MaskValues = MaskValues + NewMaskValues
  Fits_Write, MaskFile, MaskValues, MaskHeader

EndElse

return, 1

stop

end
