source: misclib/dcm_info.m @ 188

Last change on this file since 188 was 127, checked in by tjniskan, 9 years ago
  • Added some sorting options to dcm_info.m

M misclib/dcm_info.m

File size: 4.4 KB
Line 
1function dcm_info(pathname,add_params)
2% Print some information from DICOM files exported from PACS
3
4% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
5%
6% Department of Physics, Department of Neurobiology
7% University of Kuopio, FINLAND
8%
9% This program may be used under the terms of the GNU General Public
10% License version 2.0 as published by the Free Software Foundation
11% and appearing in the file LICENSE.TXT included in the packaging of
12% this program.
13%
14% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
15% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16
17% Default parameters
18Param = {'SeriesDescription',...
19  'AcqTime'};
20Param_length = [25 8];
21SortByAcqTime = true;
22
23if nargin==0 || isempty(pathname)
24  try
25    tmp_dir=getpref('Aedes','GetDcmDir');
26  catch
27    tmp_dir = '';
28  end
29  pathname=uigetdir(tmp_dir);
30  if isequal(pathname,0)
31    % Canceled
32    return
33  end
34  setpref('Aedes','GetDcmDir',pathname)
35elseif nargin>2
36  error('Too many input argumetns')
37end
38
39% Make sure that pathname is a cell array
40if ischar(pathname)
41  pathname = {pathname};
42end
43
44if nargin==2
45  Param = {Param{:} add_params{:}};
46  Param_length = [Param_length ones(1,length(add_params))*15];
47end
48total_width = 7+sum(Param_length)+2*length(Param_length)+6;
49
50% DCM file to read (the first one)
51fname = 'i0000_0000b.dcm';
52
53for ii=1:length(pathname)
54  % Get the im_(number) folders
55  s=dir(pathname{ii});
56  fpath = {s([s(:).isdir]).name};
57
58  % Find the im_(number folders)
59  tmp=regexp(fpath,'^im_\d{1,2}$');
60  ind=~cellfun(@isempty,tmp);
61  if any(ind)
62    fp={fpath{find(ind)}};
63   
64    % Sort fp
65    [tmp_fp,ind2]=sort(regexprep(fp,'(^.*_)(\d{1})$','$10$2'));
66    fp=fp(ind2);
67   
68    %fprintf(1,'***************************************************\n')
69    fprintf(1,'%s\n',repmat('*',1,total_width))
70    fprintf(1,'%s\n\n',pathname{ii})
71    fprintf(1,'%7s',' ')
72    fprintf(1,'%s  ',l_trunkStr(Param{1},Param_length(1)))
73    fprintf(1,'%s  ',l_trunkStr(Param{2},Param_length(2)))
74
75   
76    for tt=3:length(Param)
77      fprintf(1,'%s  ',l_trunkStr(Param{tt},Param_length(tt)));
78    end
79    fprintf(1,'%s\n',l_trunkStr('nFiles',6))
80    fprintf(1,'%s\n',repmat('-',1,total_width))
81
82    dir_info={};
83    AcqTime=[];
84    for kk=1:length(fp)
85      % Get the number of files in the current folder
86      s=dir(fullfile(pathname{ii},fp{kk}));
87      fn={s(~[s(:).isdir]).name};
88      nFiles = length(find(~cellfun(@isempty,regexp(fn,'^i\d{4}_\d{4}b.dcm$'))));
89
90      % Read the dicom headers
91      try
92        hdr=dicominfo(fullfile(pathname{ii},fp{kk},fname));
93      catch
94        dir_info{kk}=sprintf('Could not read: %s\n',fullfile(pathname{ii},fp{kk},fname));
95        continue
96      end
97      dir_info{kk}='';
98      dir_info{kk} = [dir_info{kk} sprintf('%5s: ',fp{kk})];
99      dir_info{kk} = [dir_info{kk} sprintf('%s  ',l_trunkStr(hdr.SeriesDescription,25))];
100      dir_info{kk} = [dir_info{kk} sprintf('%s:%s:%s  ',...
101        hdr.AcquisitionTime(1:2),...
102        hdr.AcquisitionTime(3:4),...
103        hdr.AcquisitionTime(5:6))];
104      AcqTime(kk)=str2num([hdr.AcquisitionTime(1:2),...
105        hdr.AcquisitionTime(3:4),...
106        hdr.AcquisitionTime(5:6)]);
107      %fprintf(1,'%5s: ',fp{kk});
108      %fprintf(1,'%s  ',l_trunkStr(hdr.SeriesDescription,25));
109      %fprintf(1,'%s:%s:%s  ',hdr.AcquisitionTime(1:2),...
110      %  hdr.AcquisitionTime(3:4),...
111      %  hdr.AcquisitionTime(5:6));
112
113      for tt=3:length(Param)
114        if not(isfield(hdr,Param{tt}))
115          str = 'N/A';
116        else
117          if isnumeric(hdr.(Param{tt}))
118            str = num2str(hdr.(Param{tt})(:).');
119          else
120            str = num2str(hdr.(Param{tt}));
121          end
122        end
123        dir_info{kk} = [dir_info{kk} ...
124          sprintf('%s  ',l_trunkStr(str,Param_length(tt)))];
125      end
126       dir_info{kk} = [dir_info{kk} ...
127         sprintf('(%d)\n',nFiles)];
128    end
129    if SortByAcqTime
130      [tmp,sort_ind]=sort(AcqTime);
131      dir_info=dir_info(sort_ind);
132    end
133    for jj=1:length(dir_info)
134      fprintf(dir_info{jj});
135    end
136    fprintf('%s\n\n\n',repmat('*',1,total_width));
137  else
138    % Print warning and continue to next iteration
139    fprintf(1,'\nWarning: Cannot find im_# folders from\n');
140    fprintf(1,'%s\n',pathname{ii});
141    fprintf(1,'Skipping folder...\n\n\n')
142    continue
143  end
144
145end
146
147function str_out=l_trunkStr(str,len)
148
149str_out = fliplr(sprintf(['%',num2str(len),'s'],fliplr(str(1:min(end,len)))));
150
Note: See TracBrowser for help on using the repository browser.

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008