--[[
     Cristian Bonciu			12 OCT 2005     
	-	Created from GroupGenericCine.tls
     -	Added body region as grouping hint
     Mahesh Nagarajan		18 JAN 2006	
     -	Added sort hint for ID SOP Instance UID just before filename	
    Mahesh Nagarajan			27 JAN 2006
     -	Grouping by filename is a bad idea as when two studies are merged,
		the images with the same filename are grouped together.	
    Mahesh Nagarajan			03 MAY 2006
     -	Improving "All Images" Sequence for Fletcher-Allen
    Mahesh Nagarajan      	07 SEP 2006
     -	Tweaking sorting to reduce priority of image number sort hint
	Mahesh Nagarajan		12 FEB 2007
	-	MAJOR SCRIPT REVAMP; please compare with earlier version on VSS if necessary
	-	Implemented support for user controlled sorting on multiple attributes using prototypes
		from Mike McFarland. For more details on the sequencing engine changes, please consult 
		the document titled "Lua Application Programming Interface for Multiple Sorting 
		Criteria Sequencing. The corresponding FEATs for HRS are FEAT219 and FEAT250.
	-	"All Images" will no longer be created in the sequencing scripts; HRS will take 
		responsibility for generating this sequence
	- 	Modified syntaxes for grouping and sorting hints as part of v11.5 re-write
	Mahesh Nagarajan		17 MAY 2007
	-	Removed the modality prefix and sort suffix from sort names as it looked stupid
	Mahesh Nagarajan		28 MAY 2007
	- 	Bringing back the "All Images" sequence until the corresponding HRS feature gets implemented; 
		this will get one sort order called "Default"	
	-	Modified how still multi-frame images are handled; these should be handled similar to cines
		and not as stills
	Mahesh Nagarajan		11 FEB 2008
	-	Adding back site-specific code snippets; after a long discussion with Cristian B., it is understood that this code has to stay 
		in the code until proper site files have been installed. Such code is added back but within "dollar sign" demarcations to ensure 
		easy removal at a later date.				
	Mahesh Nagarajan		14 MAR 2008
	-	Fixed inconsistencies found between this branch (EXP) and the live sequencing script
		branch found by Shudong during code review
	Shudong Liu		September 11, 2008
	-	Added site CHRISTUS SANTA ROSA HOSPITAL into a customized code to ensure XA don't 
		get split by primary and secondary positioner angles. (CRDB66635).
		Central DuPage Hospital was also added into the list so that this branch is consistent with the live
		sequencing script.
	Shudong Liu  	  Feb. 18, 2009
	- 	Removed "*All Images*" sequence for HMI 11.6		
    James Zhang     July 16, 2009
    -  Added site customized code for University of Michigan to bypass
       primary and secondary positioner angle group hints, as per REI 14576.
--]]

function DeclareTags()
    POSITIONER_PRIMARY_ANGLE = maketag("0018","1510") 
    POSITIONER_SECONDARY_ANGLE = maketag("0018","1511") 
end

function RegisterSortingHintTypes( )
-- ////////////////////////////////////////////////////////////////////////////
-- This function will register the various sort orders that are created for XA
-- ////////////////////////////////////////////////////////////////////////////
	RegisterHintType( "ACQUISITION TIME SORT", "Acquisition Time" )
	RegisterHintType( "IMAGE NUMBER SORT", "Image Number" )
    RegisterHintType( "FILE NAME SORT", "File Name" )
    RegisterHintType( "DEFAULT SORT", "Default" )
end

function Execute()

    DeclareTags()
    
-- ////////////////////////////////////////////////////////////////////////////
-- generates the hints for the sequencer as follows 
-- use SOP instance UID for the multiframe images to generate unique sequences
-- for each multiframe image
-- use FRAME_TYPE_PROP to create and subsequently add all the stills to the same
-- sequence
-- use body region and the positioner angles to split in disctinct series
-- (when present with different values)
-- ////////////////////////////////////////////////////////////////////////////

    -- Note that since this is a generic script covering multiple modalities
    -- we do not do a modality sanity check.
	
	local groupHint01 = GroupingHint:new( ValueForTag( SERIES_DESC_PROP, "?" ) )
    
    -- Regardless of frame type, don't mix images from different series.
    if( HasAttr(SERIES_UID_TAG) ) then
            groupHint01:AddEntry(SERIES_UID_TAG) -- add series uid as primary grouping hint
    end
	
     -- Group by body region examined, when present 
    if( HasAttr(BODY_REGION_PROP) ) then
            groupHint01:AddEntry(BODY_REGION_PROP) -- add body region as grouping hint
            --descStr = ValueForTag( BODY_REGION_PROP, "?" )
    end 
	
	--$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$	
	-- James: Site customization code here - for those don't want our splits by
    --           primary and secondary positioner angles.
	local dontSplitScoutsByPosAngles = false
	local SiteName = GetAttr( INSTITUTION_NAME_TAG )	
	if (SiteName ~= nil and BestWordMatchInAttr(SiteName, "Froedtert", "Central DuPage", "CHRISTUS SANTA ROSA HOSPITAL", "University of Michigan Hospital")) then
		dontSplitScoutsByPosAngles = true
	end
	--$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
	
    if (dontSplitScoutsByPosAngles == false) then
		-- Group by positioner angles, when present 
		if( HasAttr(POSITIONER_PRIMARY_ANGLE) ) then
            groupHint01:AddEntry(POSITIONER_PRIMARY_ANGLE) 
		end 
	
		if( HasAttr(POSITIONER_SECONDARY_ANGLE) ) then
            groupHint01:AddEntry(POSITIONER_SECONDARY_ANGLE) 
		end
	end

    -- Determine frame type
    local sFrameType = ValueForTag( FRAME_TYPE_PROP, "?" )
	-- The image might be a still but still multi-frame
	local nFrames = ValueForTag(NUMBER_OF_FRAMES_TAG)
	local isStillMF = false
	if ( nFrames and nFrames > 1 and sFrameType~="Cine") then
		isStillMF = true
	end
        
    -- If the frame type is cine
    if( sFrameType == "Cine" or isStillMF == true) then
    
        -- Assign each distinct cine file to its own sequence (probably redundant with series UID, but safe)
        groupHint01:AddEntry(IMAGE_UID_TAG)    
		
		-- For cine clips, no sorting is required as the frames can be assumed to be in the right order. For
		-- safety's sake, we create a dummy sort hint on image UID - unregistered.
		local sortHint01 = SortingHint:new( "Default", "DEFAULT SORT" )
		sortHint01:AddEntry(IMAGE_UID_TAG, true)
		local sortHintSet01 = SortingHintSet:new( )
		sortHintSet01:AddHint( sortHint01, true )
		
		AddImage2Sequence( "XA Cine Sequence", groupHint01, sortHintSet01) 
      
	else -- must be a single image still
	
		local nFrames = ValueForTag(NUMBER_OF_FRAMES_TAG)
		if (nFrames and nFrames > 1) then
			groupHint01:AddEntry(IMAGE_UID_TAG) 
		end
		
		-- MaxN: Note that the default sort for XA is by acquisition time; so we do not
		-- have a seperate "default" sort scheme like we did for CT & MR. 
		local sortHint01 = SortingHint:new( "Acquisition Time", "ACQUISITION TIME SORT" )

		--$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
		local InstName = GetAttr( INSTITUTION_NAME_TAG )	
		if ( InstName ~= nil and BestWordMatchInAttr(InstName, "Froedtert") ) then
			sortHint01:AddEntry(IMAGE_ACQUISITION_DATE_TAG)  
			sortHint01:AddEntry(IMAGE_ACQUISITON_TIME_TAG)  
		end
		--$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
		
		sortHint01:AddEntry(ACQUISITON_DATETIME_TAG, true)
        sortHint01:AddEntry(ACQUISITON_DATE_TAG, true)
        sortHint01:AddEntry(ACQUISITON_TIME_TAG, true)
        sortHint01:AddEntry(IMAGE_ACQUISITON_TIME_TAG, true)  
		sortHint01:AddEntry(ALI_IMAGE_FILENAME_TAG, true)        
		sortHint01:AddEntry(INSTANCE_NUMBER_TAG, true)   
 		sortHint01:AddEntry(IMAGE_UID_TAG, true)
		
		-- Create a "Sort Hint Set" here; all alternate sorts will be added to this one if they are created
		local sortHintSet01 = SortingHintSet:new( )
		sortHintSet01:AddHint( sortHint01, true )		
		
		-- Creating an image number sort for XA - created only if (0020, 0013) REL Image Number exists
		if (HasAttr(INSTANCE_NUMBER_TAG)) then
			local sortHint02 = SortingHint:new( "Image Number", "IMAGE NUMBER SORT" )
			sortHint02:AddEntry( INSTANCE_NUMBER_TAG, true ) 
		
			-- If we are here, then the image number sort was created successfully; adding it to the sort hint set for XA
			sortHintSet01:AddHint( sortHint02 )

			--$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
			local InstName = GetAttr( INSTITUTION_NAME_TAG )	
			if ( InstName ~= nil and BestWordMatchInAttr(InstName, "Cookeville") ) then
				sortHintSet01:AddHint( sortHint02, true )
			end
			--$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
		end

		-- Not checking for file name as it should always exist
		local sortHint03 = SortingHint:new( "File Name", "FILE NAME SORT" )
		sortHint03:AddEntry( ALI_IMAGE_FILENAME_TAG, true )
		sortHintSet01:AddHint( sortHint03 )		
		
		AddImage2Sequence( "XA Still Sequence", groupHint01, sortHintSet01 )
        
    end
	
end

