; $Id: $SSW/proba2/swap/idl/prep/p2sw_expnorm.pro#1 $
;
; Copyright (c) Trinity College Dublin. All rights reserved.
;+
; NAME:
;	P2SW_EXPNORM
;
; PURPOSE:
;	This function applies exposure-time normalization to PROBA2/SWAP 
;       images.
;
; CATEGORY:
;	PROBA2/SWAP calibration.
;
; CALLING SEQUENCE:
; 
;	Result = P2SW_EXPNORM( Index, Image [, VERSION=version, /VERBOSE, $
;    	    	    	    	    	    	_EXTRA=ex ] )
;
; INPUTS:
;	Index:	FITS header in structure format for PROBA2/SWAP image 
;               to be processed.
;
;	Image:	PROBA2/SWAP image to be processed.
;
; OPTIONAL INPUTS:
;	VERSION: Master P2SW_PREP code version number for HISTORY tag 
;   	    	 entry. DEFAULT: null string ('').
;
; KEYWORD PARAMETERS:
;	VERBOSE: Set this keyword to print run-status information to 
;                standard output
;
; OUTPUTS:
;	This function returns the input image normalized by the image 
;       exposure time. Input Index structure is updated during processing 
;   	 - e.g., BUNIT updated to reflect modification and HISTORY 
;   	appended.
;
; PROCEDURE:
;	Input image is divided by exposure time from input FITS header 
;       structure.
;
; MODIFICATION HISTORY:
; 	Written by:	D. Shaun Bloomfield, 24-Jan-2009.
;	Apr 2009	Added IDL-format description.
;	Jun 2009	Added ROB calls to send2lmat.pro for logging.
;   	    	    	Moved FITS history updating into sub-routines.
;   	May 2010    	Finalized IDL-format description.
;       Jun 2014        Change BUNIT Keyword to comply with FITS standard (dbs)
;-

FUNCTION p2sw_expnorm, index, image, $
    	    	    	version=version, verbose=verbose, _extra=ex

    prognam = 'p2sw_expnorm.pro'
    
    ;---  Check keyword inputs and set keyword-dependencies
    IF KEYWORD_SET(version) THEN version = version ELSE version = ''
    verbose = KEYWORD_SET(verbose)
    
    ;---  Extract image exposure time
    exptime = index.exptime
    
    image1 = image
    
    ;---  Apply exposure-time normalization
    image1 /= exptime
    ;---  Modify index structure to reflect exposure-time normalized pixel units
    index.bunit = 'DN/s'
    
    ;---  Set HISTORY update text
    tagval = 'Exposure time normalized data (DN/s)'
    ;---  Report if requested
    IF (verbose) THEN MESSAGE, 'HISTORY record updated:  '+tagval, /CONTINUE
    ;---  Add update text into HISTORY field of index structure
    UPDATE_HISTORY, index, tagval, CALLER='P2SW_EXPNORM '+version
    
    RETURN, image1
    
END
