--[[
   This grouping script partition computed tomography (CT) 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,        August-September 2004        CREATED and  
     -	implemented the functionality from pseudo-code prototypes provided by Allan 
     Allan Noordvyk			04 MAY 2005
     -	Added missing DeclareTags() call and fix PATIENT_POS_TAG defn to fix CRDB00032128
     Cristian Bonciu			09 JUN 2005                  
     -	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   
     Allan Noordvyk			18 JUL 2005
     -	CRDB00027821: Added split by Image Type as well for non-scouts so that Original and Derived 
		images would not be intermixed.   
     Allan Noordvyk			30 SEP 2005
     -	Added *All Images* series. 
     Allan Noordvyk			01 OCT 2005
     -	Added sort reversal for Axial neuro studies (requested by Gwinnett and U. Wisconsin).
     Mahesh Nagarajan		04 OCT 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
     Allan Noordvyk			05 OCT 2005
     -	Fixed merge oddities related to last two changes being done on different versions of the script.  All 
		series sorting not quite correct due to stray comment block, use of old sliceOrder variable instead of
		sliceLocorder, and hard-coded patient location order rather than using new locPatientOrder variable.
     Allan Noordvyk			07 OCT 2005
     -	Changes below are related to STRQ1572 Sequencing: Improper sequencing for MIP images and other 
		issues observed in situ at Gwinnett Medical
     -	Adjusted sort of MIP's to not try to do fancy spatial stuff since this results in odd image orders
     -	Shortened "POST FOSSA" to "POST FOS" and added "POSTFOS" for better matching of abbreviations
     Mahesh Nagarajan		30 OCT 2005
	-	Added grouping hint for Series Number for study observed in Prince William hospital; where a sequence 
		had the same series UID for all images but two sets of seried numbers that had to be split.
     Mahesh Nagarajan		01 NOV 2005
	-	Removed grouping of scouts in individual sequences on request by Mercy chicago; set grouping of scouts 
		to View property instead.
     Mahesh Nagarajan		10 NOV 2005
     -	Modified sorting order for sort hint IMAGE_LOCATION_PATIENT_PROP; set sorting order to 
		ascending for Coronal images, and descending for axials and sagittals.
     - 	Added exception for Elkhart General hospital, Axials are sorted from toe to head in this case.
     Allan Noordvyk			13 NOV 2005
     -	Fixed reference to undeclared INSTITUTION_NAME attribute tag (now defined in GroupCommon.tls
		as INSTITUTION_NAME_TAG
     Allan Noordvyk			14 NOV 2005
     -	Added DELAYED as grouping criteria to handle case of Toshiba Aquillion sending delayed slices in same 
		series as original slices.
     Mahesh Nagarajan		17 NOV 2005
     - 	Re-introduced site customized sorting for Coronal images at Elkhart; long-term solution for site customization
		is yet to be determined
     Mahesh Nagarajan		22 NOV 2005
     -	Added grouping hint for ACQ Filter Type (0018,1160)
     Mahesh Nagarajan		12 DEC 2005
     -	On request by Tina Penn (PACS Admin for Mountainview Medical Center, NM) added site customized sorting 
		scheme for Sagittals and Coronals; Sagittals to scroll from right to left and Coronals from back to front.
     Mahesh Nagarajan		20 DEC 2005
     -	Added support for CT MIPs after encountering such a sequence in a study at Suburban Radiology
     Mahesh Nagarajan		29 DEC 2005
     -	Added Cristian's fix for over-splitting sequences at Clinton Memorial
     Mahesh Nagarajan		13 JAN 2006
     -	Added splitting hints for (0018, 1100) ACQ Reconstruction Diameter and (0018, 0022) ACQ Scan Options 
     Mahesh Nagarajan		18 JAN 2006
     -	New support for Phase property: If REL Acquisition Number exists but Phase property is NULL; it means 
		that REL Acquisition Number was rejected for Phase but this may result in multiphase sequences interleaving.
		For such situations, the primary sorting hint is set to be Image Number so that the multi-phase sequences 
		follow one another
     - Added sort hint for ID SOP Instance UID just before filename
     Mahesh Nagarajan		24 FEB 2006
     -	More problems stemming from Toshiba Aquilion devices that append image number to series description for each 
		image. For such studies, "series description" is now disabled as a grouping hint.
     Mahesh Nagarajan		03 APR 2006
     -	Added new grouping hint for DICOM tag (0018, 1210) ACQ Convolution Kernel based on interleaved sequences 
		observed at Provena St. Joseph
     -	Potential multi-phase sequences (phase information is not reliable) are ordered by image number i.e. order 
          of acquisition; this does not work so well if the site does not like the order in which the image is acquired. On
          request by such sites, the ordering can be reversed.
     -	Added site-customized sort routines to ensure right-to-left ordering of sagittal images 	 
     Mahesh Nagarajan		21 APR 2006
     -	Turns out that Toshiba cannot get it right when it comes to series descriptions; found another device model 
		"Asteion" that incorrectly adds image number to series description. Ignoring series description as a grouping 
		hint for this model as well.
     Mahesh Nagarajan		02 MAY 2006
     -	Sorting Perfusion sequences by image number to ensure that different runs come one after another
     Mahesh Nagarajan		12 MAY 2006
	-	MAJOR SCRIPT REVAMP; please compare with earlier version on VSS if necessary
     -	Removed site customized sorting and replaced it with sorting by image number
     Mahesh Nagarajan		27 MAY 2006
     -	Removed Series Description as a grouping hint; relying on image number sorting to remove potential over-splits
     - Grouping hints on ACQ tags should apply only to images with image type "Original"
     Mahesh Nagarajan		16 JUNE 2006
     -	Removing grouping hint for (0018, 1210) ACQ Convolution Kernel; the split caused by this tag was found to be 
		undesirable at Elkhart
	Mahesh Nagarajan		16 JUNE 2006
     -	Finally giving up on (0018, 1100) ACQ Reconstruction Diameter as a grouping hint; all attempts to use this tag have 
		resulted in failure sooner or later
	Mahesh Nagarajan		18 SEP 2006
	-	Adding a splitting hint for (0008, 0008) ID Image Type for CT devices at U of W only 
	-	This ridiculous split gains popularity as St. Josephs asks for it too...
	Mahesh Nagarajan		26 JAN 2007
	-	The ridiculous tweak mentioned in the previous comment required some more tweaking as it was not catching all 
		instances. This hacked grouping split now splits on image type tag and view property.
	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.
	-	Site customized code will be removed and re-located to GroupCT.site files -> starting with 11.5, unless sort orders
		or splitting hints are fundamentally flawed, all change requests to sorting and grouping schemes will be incorporated
		into a site file for the site.
	-	"All Images" will no longer be created in the sequencing scripts; HRS will take responsibility for generating this sequence
	- 	Removing the split on "phase property"; this will go in the site customization file.
	- 	Modified syntaxes for grouping and sorting hints as part of v11.5 re-write
	Mahesh Nagarajan		17 MAY 2007
	-	Added new sorting category called "Perfusion Sort"; this will sort images first by slice lcoation and then by acquisition time.
	-	Changed the sort names; removed the modality prefix and sort suffix as it looks 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"
	Mahesh Nagarajan		05 JUL 2007
	-	Cardiac studies at Excel Imaging having over-splitting issues casued by the scan options tag; this spliter
        is now disabled for cardiac studies
    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		13 NOV 2007
	-	Tweaked the generation of the "Phase Sort"; this should be generated if the (0020,0012) REL Acquisition Number tag is 
		present. The presence of this tag does not necessarily mean that the phase property will be generated but will still need 
		to be used for sorting
	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
	Mahesh Nagarajan		18 MAR 2008
	-	Removing Resurrection Med. Ctr. from list of sites that require splitting the  MIP/MPR scouts away from the rest of the sequence	
	Shudong Liu			9 MAY 2008
	-	Customized for site Roswell Park Cancer Institute to add back the group hints (0018, 1100) ACQ Reconstruction Diameter 
	     and (0018, 1210) ACQ Convolution Kernel.
	Mahesh Nagarajan - 10 JUN 2008
	-	Disabling splits by image type for Scouts at Suburban Imaging as it splits up reconstructions	
	Shudong Liu     	         8 OCT 2008
	-	Updated the script to be consistent with the living sequencing script. PHASE_PROP is added back as group hint.
		(CRDB67100)
	-	Commented out the support for Enhanced Multi-Frame Objects. HRS 11 does not support EMF yet. 		
	Shudong Liu - 6 JAN 2009
	-	Customized for site University of Kentucky Medical Center to sort images by series time. (REI11225)
	Shudong Liu    Feb. 18, 2009
	- Removed "*All Images*" sequence for HMI 11.6
	Shudong Liu	  20 APR 2009
	-	Update to fix the issue of MIP and MPR images (CRDB 73715)	
	James Zhang     June 19, 2009
     -  Added group hint ALI_DERIVED_IDENTIFIER_TAG for CRDB75126.	  
    James Zhang - 8 FEB 2010
    - Customized for site Centre Hospitalier Saint Denis to seperate Arterial time and venous phase images,
      as per CRDB00083950(REI 17921)
    James Zhang - 16 MAR 2010
    - Customized for France site CHU Clermont Ferrand to seperate arterial from venous phase images,
      as per CRDB00085035(REI 18054)
    James Zhang - 11 JUN 2010
    - Added 2 more institution names for France site CHU Clermont Ferrand,
      as per CRDB00088231(REI 18054)
--]]

function DeclareTags()
    ACQ_FILTER_TYPE_TAG = maketag( "0018", "1160" )
    ACQ_CONV_KERNEL_TAG = maketag( "0018", "1210" ) 
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( "PHASE SORT", "Phase" )
    RegisterHintType( "FILE NAME SORT", "File Name" )	
	RegisterHintType( "PERFUSION SORT", "Perfusion" )
end

function Execute()
-- ////////////////////////////////////////////////////////////////////////////
-- This function will create grouping and sorting hints for CT Sequencing 
-- ////////////////////////////////////////////////////////////////////////////

   TraceC("Group CT executing\n")
   
    -- If this is the wrong script for the supplied image...
    if (not SanityOK("CT")) then
		return nil
		-- =====================================================
    end
	
	-- If this is an EMF CT object, let the proper script handle it
	local EMF_CT_CLASS_UID = "1.2.840.10008.5.1.4.1.1.2.1" 
    local sopClassAttr = GetAttr(SOP_CLASS_UID_TAG)
	
	--if (sopClassAttr~=nil and BestMatchInAttr(sopClassAttr, EMF_CT_CLASS_UID) ) then
	--	return nil
	--	-- =====================================================		
	--end
	
    DeclareTags()
    
    --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	-- The following code is to set up the parameters for sorting by slice location.
	
	-- Define constants so intended sort order, when used, is more readable.
    local ASCENDING_ORDER = true
    local DESCENDING_ORDER = false    
    
    -- For most views and patient positions, these slice orders will give you expected results
    local sliceLocOrder = ASCENDING_ORDER
    local locPatientOrder = DESCENDING_ORDER
	
	-- The default sort order for slice location is Axial ->Top to Bottom; Sagittal -> Left to Right
	-- and Coronal -> Front to Back
	if (ValueForTag( VIEW_PROP ) == "Coronal") then
		locPatientOrder =  ASCENDING_ORDER
	else
		locPatientOrder =  DESCENDING_ORDER
	end
	   
    -- If the view is axial and body region is head; preferred sort is Bottom to Top
    if (ValueForTag( VIEW_PROP ) == "Axial" ) then
    
        -- If this series covers the patient's head region...
        local bodyRegion = ValueForTag( BODY_REGION_PROP )
        if (bodyRegion ~= nil and 
                 (string.find(bodyRegion,"HEAD",1,true) or 
                  string.find(bodyRegion,"BRAIN",1,true) or
                  string.find(bodyRegion,"CEREBRUM",1,true) or
                  string.find(bodyRegion,"POST FOS",1,true) or
                  string.find(bodyRegion,"POSTFOS",1,true) or
                  string.find(bodyRegion,"ORBIT",1,true) or
                  string.find(bodyRegion,"MANDIBLE",1,true) or
                  string.find(bodyRegion,"SKULL",1,true) or
                  string.find(bodyRegion,"TEMPORAL",1,true) or
                  string.find(bodyRegion,"JAW",1,true) or
                  string.find(bodyRegion,"FACE",1,true) or
                  string.find(bodyRegion,"FACIAL",1,true) or
                  string.find(bodyRegion,"MAXILLA",1,true) or
                  string.find(bodyRegion,"NOSE",1,true) or
                  string.find(bodyRegion,"NASAL",1,true) or
                  string.find(bodyRegion,"SINUS",1,true) or
                  string.find(bodyRegion,"ZYGMATIC",1,true) or
                  string.find(bodyRegion,"NEURO",1,true))) then
                                    
            -- Want inferior to superior sort if using Location Patient for sorting
            locPatientOrder = ASCENDING_ORDER
    
		else  -- still want superior to inferior sort

			-- If unable to use Location Patient for sorting, need to adjust how we interpret Slice Location
			-- For HFS/HFP positions of body regions not related to Head, must reverse this sort
			if ( ValueForTag( PATIENT_POS_TAG ) == "HFS" or ValueForTag( PATIENT_POS_TAG ) == "HFP" ) then
				sliceLocOrder = DESCENDING_ORDER
			end

		end
	end
    --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	-- Note image type; used later on to seperate scouts from regular images
    local imageType = ValueForTag( IMAGE_TYPE_PROP )
    local isOriginal = (imageType ~= nil and imageType == "Original")
    local isMIP = (imageType ~= nil and imageType == "MIP")
    local isMPR = (imageType ~= nil and imageType == "MPR")
    
	-- 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 CT - 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 for CT
		sortHintSet01:AddHint( sortHint02, true )
	end
	
	-- Creating a slice location sort for CT - 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, locPatientOrder )
		sortHint03:AddEntry( SLICE_LOC_TAG, sliceLocOrder )
		
		-- If we are here, then the slice location sort was created successfully; adding it to the sort hint set for CT
		sortHintSet01:AddHint( sortHint03 )
	end
	
	-- Creating an acquisition time sort for CT - 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
	
	-- Creating a phase sort for CT - created only if (3711, 1109) Phase Property exists
	if (HasAttr(PHASE_PROP) or HasAttr(ACQ_NUMBER_TAG)) then
		local sortHint05 = SortingHint:new( "Phase", "PHASE SORT" )
		sortHint05:AddEntry( PHASE_PROP, ASCENDING_ORDER )
		sortHint05:AddEntry( ACQ_NUMBER_TAG, ASCENDING_ORDER )
		sortHint05:AddEntry( IMAGE_LOCATION_PATIENT_PROP, locPatientOrder )
		sortHint05:AddEntry( SLICE_LOC_TAG, sliceLocOrder )
		-- Alternates just in case slice location is not available
		sortHint05:AddEntry( INSTANCE_NUMBER_TAG, ASCENDING_ORDER ) 
		sortHint05:AddEntry( ALI_IMAGE_FILENAME_TAG, ASCENDING_ORDER )
		
		-- If we are here, then the phase sort was created successfully; adding it to the sort hint set for CT
		sortHintSet01:AddHint( sortHint05 )
	end
	
	-- Creating a perfusion sort for CT - only if (0020, 1041) REL Slice Location or 
	-- (3711, 110e) Image Position exist, and 0008, 0032) ID Acquisition Time  or 
	-- (0008, 0033) ID Image Time exist
	if ((HasAttr(SLICE_LOC_TAG) or HasAttr(IMAGE_LOCATION_PATIENT_PROP)) and
		(HasAttr(ACQUISITON_TIME_TAG) or HasAttr(IMAGE_ACQUISITON_TIME_TAG))) then
	
		local sortHint06 = SortingHint:new( "Perfusion", "PERFUSION SORT" )

		sortHint06:AddEntry( IMAGE_LOCATION_PATIENT_PROP, locPatientOrder )
		if (isADCmAPS == false) then
			-- This check ensures that the sort hint for SLICE_LOC_TAG is ignored for ADC MAPS series from GE devices
			sortHint06:AddEntry( SLICE_LOC_TAG, sliceOrder )
		end	
		
	    sortHint06:AddEntry( ACQUISITON_DATETIME_TAG, ASCENDING_ORDER )
		sortHint06:AddEntry( ACQUISITON_DATE_TAG, ASCENDING_ORDER )
		sortHint06:AddEntry( ACQUISITON_TIME_TAG, ASCENDING_ORDER )
		sortHint06:AddEntry( IMAGE_ACQUISITION_DATE_TAG, ASCENDING_ORDER )
		sortHint06:AddEntry( IMAGE_ACQUISITON_TIME_TAG, ASCENDING_ORDER )
		
		sortHintSet01:AddHint( sortHint06 )
	end
	
    TraceC( "Group CT sort hints created.\n")
	
	local groupHint01 = GroupingHint:new( ValueForTag( SERIES_DESC_PROP, "?" ) )

 	if( HasAttr(ALI_DERIVED_IDENTIFIER_TAG) ) then
		groupHint01:AddEntry(ALI_DERIVED_IDENTIFIER_TAG)
	else
        if( HasAttr(PHASE_PROP) ) then
            TraceC("Grouping hint: phase property\n")
            groupHint01:AddEntry(PHASE_PROP)
        end	

        if( HasAttr(SERIES_UID_TAG) ) then
            TraceC("Grouping hint: series UID tag\n")
            groupHint01:AddEntry(SERIES_UID_TAG)
        end	

        --$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        -- MaxN: Site customized code to prevent scouts from splitting by view at Resurrection
        local dontSplitScoutsByView = false
        local InstName = GetAttr( INSTITUTION_NAME_TAG )	
        if ( InstName ~= nil and BestWordMatchInAttr(InstName, "Resurrection", "Suburban") ) then
            dontSplitScoutsByView = true
        end
        --$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        
        -- If we are dealing with a scout....
        if( imageType == "Scout"  and dontSplitScoutsByView == false) then
            TraceC( "Image being treated as a scout.\n")

            if( HasAttr(VIEW_PROP) ) then
                TraceC("Grouping hint: view property\n")
                groupHint01:AddEntry(VIEW_PROP)
            end

        else   -- handling a non-scout image
            -- Conditionally add a set of hints on non-null tags and properties used for non-scout images
            TraceC( "Image being treated as a non-scout.\n")
        
            if( HasAttr(SERIES_NUMBER_TAG) ) then
                TraceC("Grouping hint: series number tag\n")
                groupHint01:AddEntry(SERIES_NUMBER_TAG)
            end        
                    
            if( HasAttr(DELAYED_PROP) ) then
                TraceC("Grouping hint: delayed property\n")
                groupHint01:AddEntry(DELAYED_PROP)
            end
            
            --$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
            UNIV_WISC_VAL = "University of Wisconsin"
            ALT_WISC_VAL1 = "UW Hosp"
            ALT_WISC_VAL2 = "U.W. Hosp."
            ALT_WISC_VAL3 = "University of WI"
            ALT_WISC_VAL4 = "UW East"
            ALT_WISC_VAL5 = "UW Research"
            ALT_WISC_VAL6 = "UW Health"
            ALT_WISC_VAL7 = "V.A.M.C Madison"
            ALT_WISC_VAL8 = "UW Madison"
            ALT_WISC_VAL9 = "UWHOSPTL"
            ALT_WISC_VAL10 = "UWHC"
            ST_JOE_MED_CTR_VAL = "Saint Joseph Medical Center"
            WHIDDEN_MEM_VAL = "Whidden Memorial"
            CAMBRIDGE_VAL = "Cambridge Hospital"
            SOMMERVILLE_VAL = "Sommerville Hospital"
            OTTAWA_VAL = "Ottawa"
            ST_MARYS_VAL = "ST MARYS MEDICAL CENTER"
            ST_MARYS_VAL2 = "ST Marys Hospital"
            PHOEBE_PUTNEY_VAL = "Phoebe Putney"
            ROSWELL_PARK_CANCER_INSTITUTE = "Roswell Park Cancer Institute"
            
            if (InstName ~= nil and BestMatchInAttr(InstName,
                    UNIV_WISC_VAL, ALT_WISC_VAL1, ALT_WISC_VAL2,
                    ALT_WISC_VAL3, ALT_WISC_VAL4, ALT_WISC_VAL5, 
                    ALT_WISC_VAL6, ALT_WISC_VAL7, ALT_WISC_VAL8,
                    ALT_WISC_VAL9, ALT_WISC_VAL10, ST_JOE_MED_CTR_VAL,
                    WHIDDEN_MEM_VAL, CAMBRIDGE_VAL, SOMMERVILLE_VAL,
                    OTTAWA_VAL, ST_MARYS_VAL, ST_MARYS_VAL2,
                    PHOEBE_PUTNEY_VAL )) then
                if (isMIP or isMPR) then
                --****************************************************************	
                -- MaxN, 30 JUNE 2006: -
                -- Splitting out MIP scouts for MIP/MPR sequences, as these scouts cause
                -- problems with display protocol setup, linking/registration and MPR 
                --****************************************************************	
                    if( HasAttr(VIEW_PROP) ) then
                          TraceC("Grouping hint: view property\n")
                          groupHint01:AddEntry(VIEW_PROP)
                    end

                    if( HasAttr(IMAGE_TYPE_TAG) ) then
                          TraceC("Grouping hint: image type tag\n")
                          groupHint01:AddEntry(IMAGE_TYPE_TAG)
                    end
                end	
            end
            
            if ( not(isMIP) and  
                ( InstName ~= nil and BestMatchInAttr( InstName, ROSWELL_PARK_CANCER_INSTITUTE)) ) then
                
                if( HasAttr(ACQ_RECON_DIAMETER_TAG) ) then
                      TraceC("Grouping hint: Recon. Diameter property\n")
                      groupHint01:AddEntry(ACQ_RECON_DIAMETER_TAG)
                end
                       
                if( HasAttr(ACQ_CONV_KERNEL_TAG) ) then
                      TraceC("Grouping hint: Convolution Kernel property\n")
                      groupHint01:AddEntry(ACQ_CONV_KERNEL_TAG)
                end	  
            end
            --$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

            if ( isOriginal and (ValueForTag(BODY_REGION_PROP) ~= "Heart") ) then
            --******************************************************************
            -- MaxN: The following splitting criteria are reactionary in the sense 
            -- that these were added to fix problems. Studies need to be monitored
            -- to ensure that these grouping hints dont cause problems later on.
            -- MaxN (27 May 2006): These criteria cause problems of incorrect
            -- splitting within reformatted sequences are are now applied only to
            -- images with image type "Original"
            -- MaxN (05-July-2007): These criteria cause problems in cardiac
            -- studies where the image acquired have different scan options		
            
                if( HasAttr(ACQ_FILTER_TYPE_TAG) ) then
                    TraceC("Grouping hint: filter type tag\n")
                    groupHint01:AddEntry(ACQ_FILTER_TYPE_TAG)
                end
                
                if( HasAttr(SCAN_OPTIONS_TAG) ) then
                    TraceC("Grouping hint: scan options tag\n")
                    groupHint01:AddEntry(SCAN_OPTIONS_TAG)
                end
            --******************************************************************
            end

            -- Customization: seperates arterial from venous phase images by REL Acquisition Number
            local bGroupByAcqNumber = false
			if (InstName ~= nil and HasAttr(ACQ_NUMBER_TAG)) then
                if (BestMatchInAttr(InstName, "Centre Hospitalier St Denis")) then
                    local descStr = ValueForTag(SERIES_DESC_PROP, "?")
                    if ("ARTERIEL / PORTAL/TARDIF" == descStr) then
                        bGroupByAcqNumber = true
                    end
                end

                local CHU_VAL1 = "C.H.U Clermont  Fd   Hotel Dieu"
                local CHU_VAL2 = "Pole Radio CHU Clermont" -- for both "Fd HE" &. "Fd GM"
                local CHU_VAL3 = "CHU Estaing"
                
                if (not(bGroupByAcqNumber) and BestMatchInAttr(InstName, CHU_VAL1, CHU_VAL2, CHU_VAL3)) then
                    bGroupByAcqNumber = true
                end
            end
            if (bGroupByAcqNumber) then
                groupHint01:AddEntry(ACQ_NUMBER_TAG) 
            end
        end
    end
    
    AddImage2Sequence( "CT Sequence", groupHint01, sortHintSet01 )
	
    TraceC( "Group CT execution complete.\n")

end

