--[[
   This script generates the hints for sorting the MR sequences within the study
   scouts first, then acquisition date/time when echo number
   
	Cristian Bonciu, August         2004
	-	CREATED and implemented the above functionality for development testing purposes   
	Allan Noordvyk			15 MAR 2005
     -	Added new defined property diffusion coefficient as sort hint
     -	Changed it so scouts are sorted to end, as requested by beta site.
	Mahesh Nagarajan		14 FEB 2006
     -	Removed sort hint for Image Type as it was made useless by the sort hint for thumbnail group
	Mahesh Nagarajan		31 JAN 2007
	- 	Modified syntaxes for sorting hints as part of v11.5 re-write
	-	Site customized code will be removed and re-located toSortMR.site files -> starting with 11.5, 
		unless sort orders are fundamentally flawed, all change requests to sorting schemes will be
		incorporated into a site file for the site.
	Mahesh Nagarajan		07 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.
	Shudong Liu    			25 FEB 2008
	-	Customized the script for site Lowell General Hospital to sort thumbnails with series number
	Mahesh Nagarajan		11 APR 2008
	-    Replacing the sort hint for the Phase property with Temporal PositionIdentifier;
		the latter is of type int and makes for a more sensible sorting hint
	Mahesh Nagarajan		12 JUN 2008
	-	Disabling the sort hint creation for Dubois MRI to ensure that the thumbnail ordering reflects the
		order in which the setries hit PACS
	Shudong Liu   January 6, 2009
         -	Customized for site University of Kentucky Medical Center to sort images by series time. (REI11225)		
--]]

-- Enable tracing in this module for debugging (comment out for production version)
-- AliDSTraceLevel = 1

--[[
	MaxN: This portion of code was written to use functionality of IP that is not yet 
	implemented. There is no timeline on when this will be available and the functionality 
	itself is not considered critical. This is commented out to  prevent compiling and
	execution of unnecessary code.
	
function DeclareTags()
	XAXIS_ORIENT_PROP = maketag("3711","1307")
	YAXIS_ORIENT_PROP = maketag("3711","1308")
	MIP_AXIS_PROP = maketag("3711","1309")
end

function ExtractMIPAxisMR()
-- ////////////////////////////////////////////////////////////////////////////
	if (imageType ~= MIP_VAL) then
	        return nil
	end
	
	TraceC( "Got here...Extracting MIP Axis....\n" )
	local xAxisOrientationProp = GetAttr(XAXIS_ORIENT_PROP)
	local yAxisOrientationProp = GetAttr(YAXIS_ORIENT_PROP)
	
	if (xAxisOrientationProp) then
		TraceC( "Got here: Horizontal....\n" )
		local horizontal = Attr:New()
		horizontal:AppendVal("Horizontal")
		SetAttr(MIP_AXIS_PROP, horizontal )
		return 
	elseif (yAxisOrientationProp) then
		TraceC( "Got here: Vertical....\n" )
		local vertical = Attr:New()
		vertical:AppendVal("Vertical")
		SetAttr(MIP_AXIS_PROP, vertical)
		return
	else
		TraceC( "Got here: Tags Non-Existent....\n" )
		return nil
	end
end
--]]

function Execute()
-- ////////////////////////////////////////////////////////////////////////////
-- generates the hints for sorting the MR sequences within a study as follows 
-- scouts first, then the others, by series date, time, number and UID
-- ////////////////////////////////////////////////////////////////////////////

    TraceC( "Executing sort for MR...\n" )
      
    -- N.B. Since this script is used for multiple MR based modalities (MR, MRA, MS) we
    -- don't do a single modality sanity check like in single modality scripts.
    
    local sortHint01 = SortingHint:new("MR Sequence Sort", "DEFAULT")
	
	--$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    local SiteName = GetAttr(INSTITUTION_NAME_TAG)
    if (SiteName ~= nil and BestWordMatchInAttr( SiteName, "Lowell General Hospital")) then
		sortHint01:AddEntry(SERIES_NUMBER_TAG) 	  
    end
	
	if (SiteName ~= nil and BestWordMatchInAttr( SiteName, "Dubois")) then
		TraceC( "Sort Hint creation disabled on site request.\n" )
		return nil
    end

	local sortByThumbnailProp = true
	if ( SiteName ~= nil and BestWordMatchInAttr(SiteName, "UNIVERSITY OF KENTUCKY MED CTR") ) then
		sortByThumbnailProp = false
	end	
	--$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
	
	if (sortByThumbnailProp) then	
		sortHint01:AddEntry(THUMBNAIL_GRP_PROP) 
        -- Ensures that Originals come first and Scouts go last
		-- MIPs, MPRs and other derived types come in the middle 		
	end

    sortHint01:AddEntry(SERIES_DATE_TAG)
    sortHint01:AddEntry(SERIES_TIME_TAG)
    sortHint01:AddEntry(SERIES_NUMBER_TAG)
    sortHint01:AddEntry(ECHO_TYPE_PROP)
    sortHint01:AddEntry(ECHO_NUMBER_TAG)
	sortHint01:AddEntry(TEMP_POSITION_IDENTIFIER_TAG)	
    sortHint01:AddEntry(PHASE_PROP)    
    sortHint01:AddEntry(DIFF_COEF_PROP)    
    sortHint01:AddEntry(ACQ_NUMBER_TAG)   
    sortHint01:AddEntry(SERIES_UID_TAG)
  
    SortSequencesInStudy(sortHint01) 
    
    TraceC( "Completed Sort for MR.\n" )
end


if( AliDSDebugEdit == 1 ) then
    Execute()
end
