--[[
    This script identifies the image properties of Positron Emission Tomography (PT) images
	
	Shudong Liu 	30 NOV 2007
	-	Added function ExtractImageTypePT() to determine whether an image type is MIP (REI4502)
--]]

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

-- ////////////////////////////////////////////////////////////////////////////    
-- definitions of the DICOM elements IDs used in this file 
-- ////////////////////////////////////////////////////////////////////////////    


function RegisterProperties()
-- ////////////////////////////////////////////////////////////////////////////
-- This function is called automatically at load of script to register any
-- modality specific properties.  Prior to IP Library build 39 it must be 
-- present even if empty.
-- Modality specific property IDs (3711,1X01)- (3711,1XFF) range
--     If modality specific properties need to be defined, then replace X in the above 
--     numeric ranges with a value that is distinct from those used by other modalities.
-- ////////////////////////////////////////////////////////////////////////////
end

-- ////////////////////////////////////////////////////////////////////////////
-- string definitions 
-- ////////////////////////////////////////////////////////////////////////////

-- ////////////////////////////////////////////////////////////////////////////
-- PT property extraction functions 
-- ////////////////////////////////////////////////////////////////////////////

-- ////////////////////////////////////////////////////////////////////////////
function ExtractImageDescriptionPT()
-- ////////////////////////////////////////////////////////////////////////////

    -- If the common function was already successful finding a good description value...
    local seriesDescProp = GetAttr( SERIES_DESC_PROP )
    if (seriesDescProp) then
        -- If it isn't just the skipping no-info placeholder...
        if (seriesDescProp:Get(1) ~= "?") then
            TraceC( "Common script found description... skipping special PT logic.\n" )
            return seriesDescProp;
            --==============================================
        end
    end
    
    TraceC( "No good description found by common script... using special PT logic.\n" ) 

    -- Try to build a description based on key components.
    local seriesDescStr
    seriesDescStr = SmartAppendWords( ValueForTag(VIEW_PROP), " ", ValueForTag(LATERALITY_PROP) )
    seriesDescStr = SmartAppendWords( seriesDescStr, " ", ValueForTag(BODY_REGION_PROP) )
      
    -- If we have an image type other than boring old "Original"...
    local imageTypeStr = ValueForTag(IMAGE_TYPE_PROP) 
    if (imageTypeStr and imageTypeStr ~= "Original" and imageTypeStr ~= "ORIGINAL") then 
        -- Append non-boring image type to custom series description
        seriesDescStr = SmartAppendWords( seriesDescStr, " ", imageTypeStr )
    end
    
    -- If we have contrast...
    if (ValueForTag(CONTRAST_PROP) == "Yes") then 
        -- Append indication that contrast is present to custom series description
        seriesDescStr = SmartAppendWords( seriesDescStr, " ", "+C")
    end
    
    -- If we had no success above...
    if (not seriesDescStr or string.len(seriesDescStr) == 0) then
        seriesDescStr = "?"
    end
    
    return NewPropWithValue( SERIES_DESC_PROP, seriesDescStr )
    
end	

-- ////////////////////////////////////////////////////////////////////////////
function ExtractImageTypePT()
-- ////////////////////////////////////////////////////////////////////////////
	-- Reprojection PT image is MIP 
    local SeriesType = GetAttr(NMI_SERIES_TYPE_TAG)
	if ( SeriesType ~= nil and BestWordMatchInAttr(SeriesType, "Reprojection")) then
		return NewPropWithValue( IMAGE_TYPE_PROP, MIP_VAL )
	end
end

-- ////////////////////////////////////////////////////////////////////////////
-- extract PT method
-- ////////////////////////////////////////////////////////////////////////////

function Execute()

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


if( AliDSDebugEdit == 1 ) then
    Execute() -- debug edit only
end
