; $Id: $SSW/proba2/swap/idl/prep/p2sw_hdrcoms.pro#1 $
;
; Copyright (c) Trinity College Dublin. All rights reserved.
;+
; NAME:
;	P2SW_HDRCOMS
;
; PURPOSE:
;	This function creates a PROBA2/SWAP FITS string header 
;   	containing FITS keyword tags, values, and descriptions.
;
; CATEGORY:
;	PROBA2/SWAP calibration.
;
; CALLING SEQUENCE:
; 
;	Result = P2SW_HDRCOMS( Index, Image, Head_in [, /NO_ROTATE, $
;   	    	    	    	    /VERBOSE, _EXTRA=ex ] )
;
; INPUTS:
;	Index:	FITS header in structure format for PROBA2/SWAP image 
;               to be processed.
;
;	Image:	PROBA2/SWAP image to be processed.
;
;	Head_in:    PROBA2/SWAP Level-0 FITS string header for input 
;   	    	    image.
;
; KEYWORD PARAMETERS:
;	NO_ROTATE:  Set this keyword to indicate that no image rotation 
;   	    	    correction has been applied
;
;	VERBOSE: Set this keyword to print run-status information to 
;                standard output
;
; OUTPUTS:
;	This function returns a string format FITS header with PROBA2/SWAP 
;       keyword descriptions and COMMENT spacer lines between FITS keyword 
;   	groups. Input Index structure has DATE_OBS removed.
;
; PROCEDURE:
;	Input Index structure containing FITS keyword tags and values is 
;   	converted into FITS string format header. FITS keyword descriptions 
;       are extracted from the input Level-0 string header for the image 
;   	being processed. COMMENT spacer lines are reinserted into the string 
;   	header array to group similar FITS keywords together. String header 
;   	includes DATE-OBS reconstructed from DATE_D$OBS (from application 
;   	of STRUCT2FITSHEAD) and no instance of DATE_OBS.
;
; MODIFICATION HISTORY:
; 	Written by:	D. Shaun Bloomfield, 6-Apr-2010.
;   	Apr 2010    	Added COMMENT spacer line insertion.
;   	    	    	Added NO_ROTATE dependence to WCSNAME description.
;   	May 2010    	Fixed DATE_D$OBS to DATE-OBS keyword handling.
;   	    	    	Finalized IDL-format description.
;   	    	    	Added handling of SWXCEN and SWYCEN keywords.
;       Jun 2014        Changed SWAVINT units for clarity (dbs)
;-

FUNCTION p2sw_hdrcoms, index, image, head_in, $
    	    	    	no_rotate=no_rotate, verbose=verbose, $
                        version = version, _extra=ex

    ;---  Check keyword inputs
    no_rotate = KEYWORD_SET(no_rotate)
    verbose = KEYWORD_SET(verbose)
    	
	;---  Remove DATE_OBS
	index = REM_TAG( index, 'DATE_OBS' )
	
    	;---  Determine FITS keyword string tagnames
    	ind_tags = STRUPCASE( TAG_NAMES(index) )
    	;---  Determine number of index tagnames
    	num_tags = N_ELEMENTS(ind_tags)
    	;---  Create output string array for FITS keyword descriptions
    	head_str_coms = STRARR(num_tags)
    	
    ;---  Trim 
    num_head = N_ELEMENTS(head_in)
    temp_tags = STRARR( num_head )
    temp_coms = STRARR( num_head )
    FOR i=0, num_head-1 DO BEGIN
    	temp_tags[i] = STRTRIM( ( STRSPLIT( head_in[i],'=', /EXTRACT ) )[ 0 ] )
	temp_coms[i] = ( STRSPLIT( head_in[i],' /', /EXTRACT, /REGEX, COUNT=str_num ) )[ (str_num-1) > 0 ]
    ENDFOR
    
    	
    	;---  Fill string array with Level-0 FITS keyword descriptions present in index structure
    	FOR i=0, num_tags-1 DO BEGIN
    	    ;---  Locate element position of chosen index tag in template tag array
    	    loc = WHERE( temp_tags EQ ind_tags[i], loc_cnt )
    	    ;---  If chosen index tag exists in template tag array, copy template 
    	    ;---  tag string description into output array
    	    IF ( loc_cnt GE 1 ) THEN head_str_coms[i] = temp_coms[loc]
    	ENDFOR
    	
    	;---  Fill string array with newly added Level-1 FITS keyword descriptions present in index structure
    	loc = WHERE( ind_tags EQ 'DATE_D$OBS' )
	IF ( loc NE -1 ) THEN head_str_coms[loc] = temp_coms[ WHERE( temp_tags EQ 'DATE-OBS' ) ]
	loc = WHERE( ind_tags EQ 'TEMPDARK' )
	IF ( loc NE -1 ) THEN head_str_coms[loc] = ' [Celsius] temperature used in dark subtraction'
    	loc = WHERE( ind_tags EQ 'CRVAL1' )
	IF ( loc NE -1 ) THEN head_str_coms[loc] = ' [arcsec] reference point WCS axis X'
    	loc = WHERE( ind_tags EQ 'CRVAL2' )
	IF ( loc NE -1 ) THEN head_str_coms[loc] = ' [arcsec] reference point WCS axis Y'
	loc = WHERE( ind_tags EQ 'SWXCEN' )
	IF ( loc NE -1 ) THEN head_str_coms[loc] = ' [pixel] axis 1 location of solar center in lv0'
	loc = WHERE( ind_tags EQ 'SWYCEN' )
	IF ( loc NE -1 ) THEN head_str_coms[loc] = ' [pixel] axis 2 location of solar center in lv0'
	loc = WHERE( ind_tags EQ 'SWAVINT' )
	IF ( loc NE -1 ) THEN head_str_coms[loc] = ' [DN/s/pixel] avg intensity in calibrated image'
    	loc = WHERE( ind_tags EQ 'WCSNAME' )
	IF ( loc NE -1 ) THEN head_str_coms[loc] = ' aligned with '+(['solar','ecliptic'])[no_rotate]+' North'
	
	;---  Combine index structure tagnames, values, and header string descriptions
	head_out = STRUCT2FITSHEAD( index, image, COMMENTS=head_str_coms )
	
	;---  Correct the STRUCT2FITSHEAD stripping of SIMPLE, BITPIX, NAXIS, NAXIS1, and NAXIS2 descriptions
	stripped_tags = [ 'SIMPLE', 'BITPIX', 'NAXIS', 'NAXIS1', 'NAXIS2' ]
	MATCH, ind_tags, stripped_tags, ind_loc, stripped_loc, COUNT=match_cnt
	IF ( match_cnt GE 1 ) THEN $
	    FOR i=0, match_cnt-1 DO BEGIN
	    	tmp_str = head_out[ ind_loc[i] ]
		STRPUT, tmp_str, head_str_coms[ ind_loc[i] ], 32
		head_out[ ind_loc[i] ] = tmp_str
	    ENDFOR
		
	
	;---  Add comment '---' separators back into the FITS header after these tagnames
	pre_com_tags = [ 'NAXIS2', 'FILE_TAR', 'DATE_D$OBS', 'WAVELNTH', 'SWAVINT', 'REBIN', $
	    	    	  'SWYCEN', 'GEOD_LON', 'TEMPDARK', 'NPRESLZW', 'DATASUM', 'HISTORY' ]
	;---  Determine array indices in both STRING arrays which match each other
	MATCH, ind_tags, pre_com_tags, ind_loc, pre_com_loc, COUNT=match_cnt
	pre_com_tags[ WHERE( pre_com_tags EQ 'DATE_D$OBS' ) ] = 'DATE-OBS'
	;---  Define comment separator content
	com_sep = '------------------------------------------------------------------------'
    	;---  If matching element indices found
    	IF ( match_cnt GE 1 ) THEN $
    	    ;---  For each comment to be added
    	    FOR j = 0, match_cnt-1 DO $
    	    	;---  Add comment '---' spacers after found instances of tag names provided 
    	    	SXADDPAR, head_out, 'COMMENT', com_sep, '', AFTER=pre_com_tags[ pre_com_loc[j] ]
	
	;---  Warn users that the current Level-1 correction is a beta version

        ; format version comment
        comment_text = 'This is a level-1 SWAP FITS file produced by p2sw_prep ' + Version + ' at the Royal '

	SXADDPAR, head_out, 'COMMENT', comment_text, '', AFTER='COMMENT'
	SXADDPAR, head_out, 'COMMENT', '  Observatory of Belgium. If you have difficulty with this file or wish ', '', AFTER='COMMENT'
	SXADDPAR, head_out, 'COMMENT', '  to make suggestions for improvements, please contact the SWAP ', '', AFTER='COMMENT'
	SXADDPAR, head_out, 'COMMENT', '  Instrument Team via email at swap_lyra@oma.be.', '', AFTER='COMMENT'
	SXADDPAR, head_out, 'COMMENT', com_sep, '', AFTER='COMMENT'
    
    RETURN, head_out

END
