tshm
12/10/2010 - 1:23 PM

dicom sample

dicom sample

#!env ruby
require 'fileutils'
require 'rubygems'
require 'dicom'
require 'ap'
SUID_TAG = "0020,000E"
LATE_TAG = "0020,0062"
VIEW_TAG = "0018,5101"
ACQN_TAG = "0020,0012"

files = Dir["*_SET*/DICOMOBJ/SYN*"]
#ap files; exit

#h = Hash.new([])
h = {}
files.each do |fn|
  o = DICOM::DObject.new(fn)
  next unless o.read_success
  id = o.value(SUID_TAG)
  lr = o.value(LATE_TAG)
  view = o.value(VIEW_TAG)
  next unless view
  h[id]=[] unless h[id]
  h[id] << {:lr => lr, :view => view, :fn => fn, :an => o.value(ACQN_TAG)}
end

i = 0
h.each do |k,v|
  v.each do |hh|
    ap name = "out/%04d_%s%s.dcm" % [i, hh[:lr], hh[:view]]
    if File.exists?(name)
      FileUtils.mv(name, name.gsub(/\.dcm/, '_0.dcm'))
    end
    FileUtils.cp(hh[:fn] ,name)
  end
  i+=1
end