--[[
	This grouping script partitions position emission tomography (PT) images as follows:
	Scout images in their own sequences
	Group all other images by a combination of DICOM Series instance UID, 
	view prop, series number and phase.   

	Cristian Bonciu      June 9, 2005                   MODIFIED
	-	Added IMAGE_LOCATION_PATIENT as the first sorting hint this hint is present
		only for axial, sagittal and coronal images (having the View property defined to one 
		of these values) and when present, produces the following sorting order :
          - head to foot for axials, 
          - left to right for sagittals and 
          - anterior to posterior for coronals
	Mahesh Nagarajan     October 04, 2005
	-	Changed order of sorting hints - pushed sorting hint "INSTANCE_NUMBER_TAG" 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		 May 19, 2006	
     -	Removed grouping hint for View property of MIP images to prevent them from being split incorrectly
     -	Added sorting hint for MIP images onNMI Image Index first & image number next
	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   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     
	Mahesh Nagarajan		 January 28, 2006	
     -	Splitting PT scouts by image UID seems to be a bad idea; as some of the scout identifiers appear on regular sequences; 
		sites are more likely to forgive incorrect image type but dont want to see a 150 image series split into 150 sequences
    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 - May 14, 2009
    -   Fixed the bug about sorting order by removing variables sliceLocOrder and locPatientOrder. (CRDB74300)		
--]]

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

function Execute()
-- ////////////////////////////////////////////////////////////////////////////
-- generates the hints for the sequencer as follows 
-- add IMAGE_TYPE as the grouping hint when its value is one (scout image)
-- for all other use multiple hints like series UID, view, series number, and phase 
-- ////////////////////////////////////////////////////////////////////////////

   TraceC("Group PT executing\n")
   
    -- If this is the wrong script for the supplied image...
    if (not SanityOK("PT")) then
      return nil
      -- =====================================================
    end

    local imageType = ValueForTag( IMAGE_TYPE_PROP )
    local isMIP = (imageType ~= nil and imageType == "MIP")

    -- 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 for PT - created only if (0020, 0013) REL Image Number exists
	if (HasAttr(INSTANCE_NUMBER_TAG) or HasAttr(NMI_IMAGE_INDEX)) then
		local sortHint02 = SortingHint:new( "Image Number", "IMAGE NUMBER SORT" )
		sortHint02:AddEntry( NMI_IMAGE_INDEX, ASCENDING_ORDER ) 
		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 for CT
		sortHintSet01:AddHint( sortHint02, true )
	end
	
	-- Creating a slice location sort for PT - created only if either (0020, 1041) REL Slice Location or 
	-- (3711, 110e) Image Position exist
	if (HasAttr(SLICE_LOC_TAG) or HasAttr(IMAGE_LOCATION_PATIENT_PROP)) then
		local sortHint03 = SortingHint:new( "Slice Location", "SLICE LOCATION SORT" )
		sortHint03:AddEntry( IMAGE_LOCATION_PATIENT_PROP, DESCENDING_ORDER )
		sortHint03:AddEntry( SLICE_LOC_TAG, ASCENDING_ORDER )
		
		-- If we are here, then the slice location sort was created successfully; adding it to the sort hint set for CT
		sortHintSet01:AddHint( sortHint03, true )
	end

	-- Creating an acquisition time sort for PT - 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
	
	-- Not checking for file name as it should always exist
	local sortHint05 = SortingHint:new( "File Name", "FILE NAME SORT" )
	sortHint05:AddEntry( ALI_IMAGE_FILENAME_TAG, ASCENDING_ORDER )
	sortHintSet01:AddHint( sortHint05 )
	
	TraceC( "Group PT sort hints created.\n")
	
	local groupHint01 = GroupingHint:new( ValueForTag( SERIES_DESC_PROP, "?" ) )
    
	-- Conditionally add a set of hints on non-null tags and properties used for non-scout images
	if( HasAttr(SERIES_UID_TAG) ) then
		TraceC("Grouping hint: series UID\n")
		groupHint01:AddEntry(SERIES_UID_TAG)
	end

	-- MaxN: Splitting MIP sequences by view is a bad bad idea
	if (not(isMIP)) then
		if( HasAttr(VIEW_PROP) ) then
			TraceC("Grouping hint: view property\n")
			groupHint01:AddEntry(VIEW_PROP)
		end
	end

	if( HasAttr(PHASE_PROP) ) then
		TraceC("Grouping hint: phase property\n")
		groupHint01:AddEntry(PHASE_PROP)
	end
	
    AddImage2Sequence( "PT Sequence", groupHint01, sortHintSet01 )
    
    TraceC( "Group PT execution complete.\n")

end

