--[[
	This script is executed whenever no modality specific grouping script is available.
	This script makes no assumptions as to whether cine will be present or not, nor
	whether it is sensible to make an "*All Images*" sequences and split images
	into individual single-image sequences.  Therefore, you basically get a 
	straight-forward grouping based on DICOM series, with some attention paid
	to splitting by scout/view/series number/etc. attributes, if present.
    
	Cristian Bonciu		October 15, 2004        
	-	Created for defining the default grouping and image sorting hints for the
		rarely used modalities. The code should be based only on the properties 
		extracted by ExtractCommon.tls
     Allan Noordvyk          January 15, 2005
	-	Filled in body.
     Mahesh Nagarajan     October 04, 2005
     -	Changed order of sorting hints - pushed sorting hint for image number
		to just above "ALI_IMAGE_FILENAME_TAG" in response to
		CRDB00035707 owing to unreliable sorting results for this tag
    Mahesh Nagarajan		 January 18, 2006	
     -	Added sort hint for ID SOP Instance UID just before filename
	Mahesh Nagarajan - 31 JAN 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
	-	Removed all duplicate maketags that already exist in GroupCommon.tls
	Mahesh Nagarajan   May 17, 2007
	-	Removed the modality prefix and sort suffix from sort names as it looked stupid
    Mahesh Nagarajan - 26 JUL 2007     
    -   Changing the "table position" hint to "slice location"; the latter is more generic as it refers to the location of 
        the slice in general and not specifically if it is relative to the table or patient space.
    -   Removing the "default" sort and making "image number" sort the default   
    Shudong Liu - 8 OCT 2008
    -   Updated the script to be consistent with the living sequencing script. "Slice Location" is set as 
	default sort.
    Shudong Liu		November 13, 2008
         Customize the script for site Excel Imaging to create "All Image" sequence for BD images.	(CRDB67420)
    Shudong Liu    Feb. 18, 2009
         Removed "*All Images*" sequence for HMI 11.6		 
]]--

-- ////////////////////////////////////////////////////////////////////////////
-- end common scripting context 
-- ////////////////////////////////////////////////////////////////////////////

-- ////////////////////////////////////////////////////////////////////////////    
-- definitions of the DICOM elements IDs used in this script
-- ////////////////////////////////////////////////////////////////////////////    
function DeclareTags()    
end

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

-- ////////////////////////////////////////////////////////////////////////////
-- commonly used strings definitions
-- ////////////////////////////////////////////////////////////////////////////    
  
-- ////////////////////////////////////////////////////////////////////////////
-- grouping hints for the rarely used modalities
-- ////////////////////////////////////////////////////////////////////////////

function Execute()

    DeclareTags()
    
    TraceC("Group Other executing...\n")

    -- Note that since this is a generic script covering multiple modalities
    -- we do not do a modality sanity check.
	
    -- Define constants so intended sort order, when used, is more readable.
    local ASCENDING_ORDER = true
    local DESCENDING_ORDER = false
    
	-- Not checking for file name as it should always exist
	local sortHint01 = SortingHint:new( "File Name", "FILE NAME SORT" )
	sortHint01:AddEntry( ALI_IMAGE_FILENAME_TAG, ASCENDING_ORDER )
		
	-- 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 ) 
    
    -- Creating an image number sort - 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, ASCENDING_ORDER ) 
		
		-- If we are here, then the image number sort was created successfully; adding it to the sort hint set
		sortHintSet01:AddHint( sortHint02, true )
	end
	
	-- Creating a slice location sort - created only if either (0020, 1041) REL Slice Location
	if (HasAttr(SLICE_LOC_TAG)) then
		local sortHint03 = SortingHint:new( "Slice Location", "SLICE LOCATION SORT" )
		sortHint03:AddEntry( SLICE_LOC_TAG, true )
		
		-- If we are here, then the slice location sort was created successfully; adding it to the sort hint set
		sortHintSet01:AddHint( sortHint03, true )
	end
	
	-- Creating an acquisition time sort - created only if (0008, 0032) ID Acquisition Time  or 
	-- (0008, 0033) ID Image Time exist
	if (HasAttr(ACQUISITON_TIME_TAG) or HasAttr(IMAGE_ACQUISITON_TIME_TAG)) then
		local sortHint04 = SortingHint:new( "Acquisition Time", "ACQUISITION TIME SORT" )
	    sortHint04:AddEntry( ACQUISITON_DATETIME_TAG, ASCENDING_ORDER )
		sortHint04:AddEntry( ACQUISITON_DATE_TAG, ASCENDING_ORDER )
		sortHint04:AddEntry( ACQUISITON_TIME_TAG, ASCENDING_ORDER )
		sortHint04:AddEntry( IMAGE_ACQUISITION_DATE_TAG, ASCENDING_ORDER )
		sortHint04:AddEntry( IMAGE_ACQUISITON_TIME_TAG, ASCENDING_ORDER )
		
		-- If we are here, then the acquisition time sort was created successfully; adding it to the sort hint set for CT
		sortHintSet01:AddHint( sortHint04 )
	end	
	
	local groupHint01 = GroupingHint:new( ValueForTag( SERIES_DESC_PROP, "?" ) )

    if( HasAttr(SERIES_UID_TAG) ) then
        groupHint01:AddEntry(SERIES_UID_TAG)
    end

    if( HasAttr(VIEW_PROP) ) then
        groupHint01:AddEntry(VIEW_PROP)
    end

    if( HasAttr(SERIES_NUMBER_TAG) ) then
        groupHint01:AddEntry(SERIES_NUMBER_TAG)
    end

    -- If the image is a scout...
    if( ValueForTag(IMAGE_TYPE_PROP) == "Scout" ) then
		-- Try to keep scouts grouped by similar image orientations as well
		-- N.B. we don't do this for regular series because they can curve
		-- to follow anatomy.
		if( HasAttr(IMAGE_ORIENTATION_PATIENT_TAG) ) then
			groupHint01:AddEntry(IMAGE_ORIENTATION_PATIENT_TAG) 
		end
	end
    
    AddImage2Sequence( "Other Sequence", groupHint01, sortHintSet01 )

    TraceC("Group Other complete.\n")

    return
end

