--[[
	This  script handles sequence grouping for OT, RG, RTIMAGE, SC, 
	BI, CS, DG, DM, EC, ECG, EPS, ES, FA, FS, GM, IO, PX, SM, ST, TG, XC,
	and similiar modality images which will still frames only and consist
	of a small number of images.

     Allan Noordvyk,    January 19, 2005  
     -	Created based on old ExtractOT version 
     Allan Noordvyk,    Oct. 7, 2005
     -	dropped image UID splitting to prevent accidents at sites
          where large image count studies are sent to this script.
    Mahesh Nagarajan    Jan 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
	Mahesh Nagarajan	Apr 23, 2007
	-	Added special treatment of bookmark images from Mecha4D - autosave bookmarks
		are detected and grouped on series description, manual bookmarks are treated as
		regular SC images and split out into individual sequences
	Mahesh Nagarajan   May 17, 2007
	-	Removed the modality prefix and sort suffix from sort names as it looked stupid
	Mahesh Nagarajan   May 28, 2007
	- 	Bringing back the "All Images" sequence until the corresponding HRS feature gets 
		implemented; this will get one sort order called "Default"	
	Shudong Liu    Feb. 18, 2009
	- 	Removed "*All Images*" sequence for HMI 11.6		
--]]

function RegisterSortingHintTypes( )
-- ////////////////////////////////////////////////////////////////////////////
-- This function will register the various sort orders that are created for NM
-- ////////////////////////////////////////////////////////////////////////////
    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()

    TraceC("Group Generic Still executing...\n")

-- ////////////////////////////////////////////////////////////////////////////
-- generates the hints for the sequencer as follows 
-- add IMAGE_UID_TAG as one grouping hint to clasify each image in its own sequence
-- add STUDY_UID_TAG as the grouping hint for the "All images" sequence
-- ////////////////////////////////////////////////////////////////////////////
	
    -- Note that since this is a generic script covering multiple modalities
    -- we do not do a modality sanity check.
	
	-- MaxN: Note that the default sort for stills 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" )
    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(INSTANCE_NUMBER_TAG, true)
    sortHint01:AddEntry(IMAGE_UID_TAG, true)
    sortHint01:AddEntry(ALI_IMAGE_FILENAME_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 stills - 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 stills
		sortHintSet01:AddHint( sortHint02 )
	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 )
	
	 --******************************************************************************
	-- MaxN: Special work-flow for SC images from  Mecha
	-- "SC" images coming from Mecha should be displayed as follows:-
	--		- manually saved bookmark images should be shown as single image per sequence
	--		- auto-save bookmark images should be displayed in the same sequence
	-- A mecha bookmark image is determined as follows
	--		- modality is "SC"
	--		- device manufacturer is "TomTec Imaging Systems"
	--		- series description contains the strings "Mecha4D" and "autosave bookmark"
	
	MECHA_VAL = "Mecha4D"
	AUTO_BKMRK_VAL = "autosave bookmark"
	local seriesDescTag = GetAttr(SERIES_DESC_TAG)
		
	if (ValueForTag(MODALITY_TAG) =="SC" and 
		ValueForTag(DEVICE_MANUFACTURER_TAG) == "TomTec Imaging Systems" and
		(seriesDescTag~=nil and BestWordMatchInAttr(seriesDescTag, MECHA_VAL) 
									and BestWordMatchInAttr(seriesDescTag, AUTO_BKMRK_VAL))	) then
		
		-- if we are here, images are Mecha bookmarks
		local mechaGrpHint =  GroupingHint:new( ValueForTag( SERIES_DESC_PROP, "?" ) )	
	
		-- Auto-save bookmarks are stored in a single sequence; btw, dont need to check for the
		-- presence of series description tag as this is done in the if statement
		mechaGrpHint:AddEntry( SERIES_DESC_TAG )
		
		AddImage2Sequence( "Still Sequence", mechaGrpHint, sortHintSet01 )

		-- leave this script to prevent execution of code further below
		return
	end
	 --******************************************************************************
		
	-- Regular work-flow follows here
	local groupHint01 = GroupingHint:new( ValueForTag( SERIES_DESC_PROP, "?" ) )	
    
	-- add series UID as the grouping hint ensuring the creation of a new sequence per DICOM series
     groupHint01:AddEntry( SERIES_UID_TAG ) -- don't check it since it should be always there 
      
	AddImage2Sequence( "Still Sequence", groupHint01, sortHintSet01 )
    
    TraceC("Group Generic Still complete.\n")

end
