--[[
    This script generates the hints for sorting the CR sequences within the study
    "all images" sequence first, "single image" sequences after
     Cristian Bonciu			27 SEP 2004
	-	CREATED and implemented the above functionality for development testing purposes
	Allan Noordvyk     		16 SEP 2005
     -	Added modality as first sort criteria so that like modality series a grouped 
		together in combo studies even when simultaneous acquisition was done (or acquistion
		time information is missing or uniform).
	Mahesh Nagarajan		06 OCT 2005
     -	Added two additional sort hints for 'study date' and 'study time' for fluoro 
		studies where the series information was missing resulting in improper
		sorting of sequences
	Allan Noordvyk			07 OCT 2005
     -	Dropped modality sanity check so that script could be used with scanned films 
		identified as "XR" instead of "CR" modality at Gwinnet
     Mahesh Nagarajan		28 MAR 2006
     -	Added sort hint for thumbnail group to ensure that thumbnails corresponding to CAD
		images appeared at the end 
	Mahesh Nagarajan  		18 APR 2006	 
     -	Increased the priority of sort hint for file name; this ensures that the 
		thumbnails are in the same order they were sent.
	Michael Lai 				15 DEC 2006
     -	Move ACQUISITION_TIME_TAG as the top priority in thumbnails ordering for
		the site "Central Kansas Medical Center"
     Michael Lai 				29 DEC 2006
     -	Added checking for image time tage for "St Francis Hospital"
     Cartik S. Sharma 		17 JAN 2007
     -	Added checking for image time tag for " Trover Foundation - RMC"
	Mahesh  Nagarajan 		05 FEB 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 		14 MAY 2007
	-	The default sorting order for CR thumbnails is changed to give preference to acquisition time;
		this is the most common preference for sorting these thumbnails.
	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.
     Mahesh Nagarajan		15 APR 2008
	-	Added SouthWestern Mississippi to list of institutions where image acquisition time
		is given priority for sorting.
	Mahesh Nagarajan		26 MAY 2008
	-	Added St. Lukes Episcopal to list of institutions where image acquisition time
		is given priority for sorting.
	Mahesh Nagarajan		06 JUN 2008
	-	Added Bayshore medical to list of institutions where image number is given priority for sorting.			
	Mahesh Nagarajan		08 JUL 2008
	-	Added Huntsville to list of institutions where image acquisition time
		is given priority for sorting.	
	Shudong Liu    Oct. 27, 2008
	  - Customized for Kosair Childrens Hospital to give image file name lower priority.
	Shudong Liu    Nov. 10,  2008
	  - Customized for Monongolia General Hospital to sort images by acquisition time.	
	Shudong Liu    Dec. 17,  2008
	  - Customized for Pocono Medical Center to sort images by acquisition time. (CRDB69932)
	Shudong Liu    Jan. 14, 2009
	  - Customized for CHRISTUS St. Elizabeth Hospital (CRDB70500) and site CSSS GATINEAU (CRDB70464)  
	    to sort images by acquisition time. 
	Shudong Liu    Jan. 28, 2009
	  - Customized for ELKHART GENERAL HOSPITAL to sort images by acquisition time. (CRDB70838)	
	Shudong Liu    Apr. 20, 2009
	  - Customized for PROVENA MERCY HEALTH CENTER to sort images by acquisition time. (CRDB73728 or REI12170)  	  
    James Zhang     July 14, 2009 
    - Customized for Central DuPage Hospital to sort All Images by acquisition data & time. (REI 13447/CRDB00077322 )
    James Zhang     Nov 12, 2009 
      - Customized for Centre hospitalier de Verdun to sort images by acquisition data & time (REI 16190/CRDB00080776).
    James Zhang     Feb 23, 2010 
      - Customized for Jewish General Hospital to sort by series number (REI 18026/CRDB84398).
    James Zhang     Mar 24, 2010
      - Customized for Lisbon Area Health Services to sort by series number (REI 18658/CRDB85180).
    James Zhang     Mar 25, 2010 
      - Customized for RUIS - Abitibi to sort by series number (REI 18478/CRDB85099).
    James Zhang     April 10, 2010
      - Customized for Froedtert Memorial Lutheran Hospital to sort thumbnails
        by series descriptions, as per CRDB00085829(REI 14733/15810/18806).
--]]

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

function Execute()
-- ////////////////////////////////////////////////////////////////////////////
-- generates the hints for sorting the CR sequences within a study as follows 
-- single image sequences first, all images sequence last 
-- ////////////////////////////////////////////////////////////////////////////
 
    TraceC( "Executing sort for CR...\n" )
    	
    local sortHint01 = SortingHint:new("CR Sequence Sort", "DEFAULT")

    sortHint01:AddEntry(MODALITY_PROP)   -- Used to group CR separately from RF in combo exams
    sortHint01:AddEntry(THUMBNAIL_GRP_PROP)
    sortHint01:AddEntry(ID_STUDY_DATE_TAG)
    sortHint01:AddEntry(ID_STUDY_TIME_TAG)
	
    --$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    CENTRALKANSAS_VAL = "Central Kansas Medical Center"
    STFRANCIS_VAL = "St Francis Hospital"
    TROVERFOUND_VAL="Trover Foundation"
    MEMORIAL_VAL = "Memorial Hospital"
    CENTRA_STATE_VAL = "Centra State"
    ABILENE_VAL = "Abilene"
    OTTAWA_VAL = "Ottawa"
    HCA_TOWER_VAL = "Tower Diagnostic Center CR75"
    MENORAH_MED_CTR_VAL = "Menorah Medical"
    WEST_JEFFERSON_MEDCTR_VAL = "West Jefferson Medical Center" 
    HACKLEY_HOSPITAL_VAL = "Hackley Hospital" 
    SWMISS_REG_VAL = "Southwest Mississippi"
    STLUKES_EPIS_VAL = "St. Luke's Woodlands"
    BAYSHORE_MED_VAL = "Bayshore Medical"	
    HUNTSVILLE_VAL = "Huntsville"
    KOSAIR_CHILDRENS_VAL = "Kosair Childrens Hospital"	
    MONONGOLIA_GENERAL_VAL = "Monongolia General Hospital"
    POCONO_MEDICAL_CENTER_VAL = "Pocono Medical Center"
    CHRISTUS_ST_ELIZABETH = "CHRISTUS St. Elizabeth Hospital"
    CSSS_GATINEAU_VAL = "CSSS GATINEAU"
    ELKHART_GENERAL_HOSPITAL = "ELKHART GENERAL HOSPITAL"
    PROVENA_MERCY_HEALTH = "PROVENA MERCY HEALTH CENTER"
    CENTRAL_DUPAGE_HOSPITAL_VAL = "Central DuPage Hospital"
    VERDUN_VAL = "CH VERDUN"
    JEWISH_GENERAL_VAL = "Jewish General Hospital"
    LISBON_VAL = "Lisbon Area Health Services"
    CSSSRN_VAL = "CSSS ROUYN-NORANDA"
    -- Froedtert wants both old and new names
    FROEDTERT_VAL1 = "Froedtert Memorial Lutheran Hospital"
    FROEDTERT_VAL2 = "FROEDTERT HOSPITAL"
	
    local SiteName = GetAttr(INSTITUTION_NAME_TAG)
    if (SiteName ~= nil and BestWordMatchInAttr(SiteName,
        CENTRALKANSAS_VAL, STFRANCIS_VAL, 
        TROVERFOUND_VAL, MEMORIAL_VAL, CENTRA_STATE_VAL, 
        ABILENE_VAL, OTTAWA_VAL, HCA_TOWER_VAL, SWMISS_REG_VAL,
        WEST_JEFFERSON_MEDCTR_VAL, HACKLEY_HOSPITAL_VAL, STLUKES_EPIS_VAL,
        HUNTSVILLE_VAL, MONONGOLIA_GENERAL_VAL,
        POCONO_MEDICAL_CENTER_VAL, CHRISTUS_ST_ELIZABETH,
        CSSS_GATINEAU_VAL, PROVENA_MERCY_HEALTH,
        CENTRAL_DUPAGE_HOSPITAL_VAL, VERDUN_VAL))
    then
        sortHint01:AddEntry(ACQUISITON_DATETIME_TAG)
        sortHint01:AddEntry(ACQUISITON_DATE_TAG)
        sortHint01:AddEntry(ACQUISITON_TIME_TAG) 
        sortHint01:AddEntry(IMAGE_ACQUISITON_TIME_TAG) 	  
    end
	
	if (SiteName ~= nil and BestWordMatchInAttr( SiteName, MENORAH_MED_CTR_VAL, BAYSHORE_MED_VAL )) then
	    sortHint01:AddEntry(INSTANCE_NUMBER_TAG) 
	end
	
	if (SiteName ~= nil and BestWordMatchInAttr(SiteName,
        KOSAIR_CHILDRENS_VAL, ELKHART_GENERAL_HOSPITAL, JEWISH_GENERAL_VAL,
        LISBON_VAL, CSSSRN_VAL))
    then
		sortHint01:AddEntry(SERIES_DATE_TAG)
        -- Series Time of some sites is not reliable
        local strSiteName = ValueForTag(INSTITUTION_NAME_TAG)
        if (JEWISH_GENERAL_VAL ~= strSiteName and CSSSRN_VAL ~= strSiteName) then
            sortHint01:AddEntry(SERIES_TIME_TAG)
        end
	    sortHint01:AddEntry(SERIES_NUMBER_TAG)
	    sortHint01:AddEntry(ACQUISITON_DATETIME_TAG)
	    sortHint01:AddEntry(ACQUISITON_DATE_TAG)
	    sortHint01:AddEntry(ACQUISITON_TIME_TAG)
	    sortHint01:AddEntry(IMAGE_ACQUISITON_TIME_TAG)
	    sortHint01:AddEntry(PROJECTION_PROP)    
	    sortHint01:AddEntry(LATERALITY_PROP)   
	    sortHint01:AddEntry(INSTANCE_NUMBER_TAG)   
	    sortHint01:AddEntry(SERIES_UID_TAG)
	end
    
	if (SiteName ~= nil and BestWordMatchInAttr(SiteName, FROEDTERT_VAL1, FROEDTERT_VAL2)) then
	    sortHint01:AddEntry(SERIES_DESC_TAG) 
	end
	--$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

	sortHint01:AddEntry(ALI_IMAGE_FILENAME_TAG)	
    sortHint01:AddEntry(ACQUISITON_DATETIME_TAG)
    sortHint01:AddEntry(ACQUISITON_DATE_TAG)
    sortHint01:AddEntry(ACQUISITON_TIME_TAG)
    sortHint01:AddEntry(IMAGE_ACQUISITON_TIME_TAG)
    sortHint01:AddEntry(SERIES_DATE_TAG)
    sortHint01:AddEntry(SERIES_TIME_TAG)
    sortHint01:AddEntry(SERIES_NUMBER_TAG)
    sortHint01:AddEntry(PROJECTION_PROP)    -- try to get PA's ahead of Lat's
    sortHint01:AddEntry(LATERALITY_PROP)    -- try to get Left and Right images lumped together
    sortHint01:AddEntry(INSTANCE_NUMBER_TAG)   
    sortHint01:AddEntry(SERIES_UID_TAG)
    sortHint01:AddEntry(IMAGE_UID_TAG)
    
    SortSequencesInStudy(sortHint01) 
    
    TraceC( "Completed sort for CR.\n" )
end

