1 /*=========================================================================
4 Module: $RCSfile: SplitIntoDirectories.cxx,v $
6 Date: $Date: 2007/09/26 16:19:54 $
7 Version: $Revision: 1.2 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
18 #include "gdcmDocEntry.h"
19 #include "gdcmDicomDir.h"
20 #include "gdcmDicomDirPatient.h"
22 #include "gdcmFileHelper.h"
23 #include "gdcmDirList.h"
24 #include "gdcmDebug.h"
25 #include "gdcmArgMgr.h"
27 #include "gdcmSerieHelper.h"
33 * - explores recursively the given directory
34 * - keeps the requested series
35 * - orders the gdcm-readable found Files
36 * according to their Patient/Study/Serie/Image characteristics
39 typedef std::map<std::string, GDCM_NAME_SPACE::File*> SortedFiles;
41 int main(int argc, char *argv[])
44 " \n SplitIntoDirectories :\n ",
45 " - explores recursively the given directory, ",
46 " - keeps the requested series / drops the unrequested series ",
47 " - orders the gdcm-readable found Files according to their ",
48 " (0x0010, 0x0010) Patient's Name ",
49 " (0x0020, 0x000d) Study Instance UID ",
50 " (0x0020, 0x000e) Series Instance UID ",
51 " - fills a tree-like structure of directories as : ",
58 " SplitIntoDirectories ",
59 " dirin=rootDirectoryName ",
60 " dirout=outputDirectoryName ",
61 " { [keep= list of seriesNumber to process] ",
62 " | [drop= list of seriesNumber to ignore] } ",
63 " [listonly] [skel] ",
64 " [noshadowseq][noshadow][noseq] [verbose] [debug] ",
66 " dirout : will be created if doesn't exist ",
67 " keep : if user wants to process a limited number of series ",
68 " he gives the list of 'SeriesNumber' (tag 0020|0011) ",
69 " drop : if user wants to ignore a limited number of series ",
70 " he gives the list of 'SeriesNumber' (tag 0020|0011) ",
71 " SeriesNumber are short enough to be human readable ",
72 " e.g : 1030,1035,1043 ",
73 " skel : name skeleton eg : patName_1.nema -> skel=patName_ ",
74 " noshadowseq: user doesn't want to load Private Sequences ",
75 " noshadow : user doesn't want to load Private groups (odd number) ",
76 " noseq : user doesn't want to load Sequences ",
77 " verbose : user wants to run the program in 'verbose mode' ",
78 " debug : *developer* wants to run the program in 'debug mode' ",
90 std::cout << "... inside " << argv[0] << std::endl;
92 // ----- Initialize Arguments Manager ------
94 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
96 if (argc == 1 || am->ArgMgrDefined("usage"))
98 am->ArgMgrUsage(usage); // Display 'usage'
103 const char *dirNamein;
104 dirNamein = am->ArgMgrGetString("dirin",".");
106 const char *dirNameout;
107 dirNameout = am->ArgMgrGetString("dirout",".");
109 int loadMode = GDCM_NAME_SPACE::LD_ALL;
110 if ( am->ArgMgrDefined("noshadowseq") )
111 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOWSEQ;
114 if ( am->ArgMgrDefined("noshadow") )
115 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOW;
116 if ( am->ArgMgrDefined("noseq") )
117 loadMode |= GDCM_NAME_SPACE::LD_NOSEQ;
120 if (am->ArgMgrDefined("debug"))
121 GDCM_NAME_SPACE::Debug::DebugOn();
123 bool verbose = ( 0 != am->ArgMgrDefined("verbose") );
124 bool listonly = ( 0 != am->ArgMgrDefined("listonly") );
127 int *seriesToKeep = am->ArgMgrGetListOfInt("keep", &nbSeriesToKeep);
129 int *seriesToDrop = am->ArgMgrGetListOfInt("drop", &nbSeriesToDrop);
131 if ( nbSeriesToKeep!=0 && nbSeriesToDrop!=0)
133 std::cout << "KEEP and DROP are mutually exclusive !" << std::endl;
138 bool hasSkel = ( 0 != am->ArgMgrDefined("hasSkel") );
141 skel = am->ArgMgrGetString("skel");
144 const char *input = am->ArgMgrGetString("input","DCM");
146 // if unused Param we give up
147 if ( am->ArgMgrPrintUnusedLabels() )
149 am->ArgMgrUsage(usage);
153 delete am; // we don't need Argument Manager any longer
155 // ----- Begin Processing -----
158 // --> Check supposed-to-be-directory names
160 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNamein) )
162 std::cout << "KO : [" << dirNamein << "] is not a Directory."
169 std::cout << "OK : [" << dirNamein << "] is a Directory." << std::endl;
172 std::string systemCommand;
174 std::cout << "Check for output directory :[" << dirNameout << "]."
176 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNameout) ) // dirout not found
178 std::string strDirNameout(dirNameout); // to please gcc 4
179 systemCommand = "mkdir " +strDirNameout; // create it!
181 std::cout << systemCommand << std::endl;
182 system (systemCommand.c_str());
183 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNameout) ) // be sure it worked
185 std::cout << "KO : not a dir : [" << dirNameout << "] (creation failure ?)"
192 std::cout << "Directory [" << dirNameout << "] created." << std::endl;
197 std::cout << "Output Directory [" << dirNameout
198 << "] already exists; Used as is."
201 // --> End of checking supposed-to-be-directory names
203 std::string strDirNamein(dirNamein);
204 // true ; get recursively the list of files
205 GDCM_NAME_SPACE::DirList dirList(strDirNamein, true);
209 std::cout << "------------List of found files ------------" << std::endl;
211 std::cout << std::endl;
215 // ======================================= The job starts here =========================
217 GDCM_NAME_SPACE::DirListType fileNames;
218 fileNames = dirList.GetFilenames();
220 GDCM_NAME_SPACE::SerieHelper *s; // Needed to use SerieHelper::AddSeriesDetail()
221 s = GDCM_NAME_SPACE::SerieHelper::New();
223 std::string token = "%%%"; // Hope it's enough!
225 GDCM_NAME_SPACE::File *f;
226 std::vector<std::string> tokens;
227 std::vector<std::string> tokensForFileName;
230 std::cout << "------------------Print Break levels-----------------" << std::endl;
232 std::string userFileIdentifier;
235 s->AddSeriesDetail(0x0010, 0x0010, false); // Patient's Name (false : no convert)
237 // You may prefer 0020 0010 Study ID
239 // s->AddSeriesDetail(0x0020, 0x0010, true);
240 // Avoid using 0008 0020 Study Date,
241 // since you may have more than one study, for a given Patient, at a given Date!
242 // or the field may be empty!
243 s->AddSeriesDetail(0x0020, 0x000d, false); // Study Instance UID (false : no convert)
246 // You may prefer 0020 0011 Series Number
248 // s->AddSeriesDetail(0x0020, 0x0011, true);
249 s->AddSeriesDetail(0x0020, 0x000e, false); // Series Instance UID (false : no convert)
251 // Feel free to add more fields, if they can help a suitable (for you)
254 // Loop on all the gdcm-readable files
255 for (GDCM_NAME_SPACE::DirListType::iterator it = fileNames.begin();
256 it != fileNames.end();
259 f = GDCM_NAME_SPACE::File::New();
260 f->SetLoadMode(loadMode);
261 f->SetFileName( *it );
264 std::string strSeriesNumber;
268 // keep only requested Series
270 if (nbSeriesToKeep != 0)
272 strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
273 seriesNumber = atoi( strSeriesNumber.c_str() );
274 for (j=0; j<nbSeriesToKeep; j++)
276 if(seriesNumber == seriesToKeep[j])
288 // drop all unrequested Series
290 if (nbSeriesToDrop != 0)
292 strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
293 seriesNumber = atoi( strSeriesNumber.c_str() );
294 for (j=0;j<nbSeriesToDrop; j++)
296 if(seriesNumber == seriesToDrop[j])
309 userFileIdentifier=s->CreateUserDefinedFileIdentifier(f);
311 GDCM_NAME_SPACE::Util::Tokenize (userFileIdentifier, tokens, token);
313 int imageNum; // Within FileName
316 ///this is a trick to build up a lexicographical compliant name :
317 /// eg : fich001.ima vs fich100.ima as opposed to fich1.ima vs fich100.ima
318 std::string name = GDCM_NAME_SPACE::Util::GetName( *it );
322 int imageNum; // Within FileName
323 GDCM_NAME_SPACE::Util::Tokenize (name, tokensForFileName, skel);
324 imageNum = atoi ( tokensForFileName[0].c_str() );
325 // probabely we could write something much more complicated using C++ !
326 sprintf (newName, "%s%06d.dcm", skel, imageNum);
327 tokens[IND_FileName] = newName;
328 tokensForFileName.clear();
332 tokens[IND_FileName] = name;
336 // Study Instance UID
337 // Series Instance UID
340 userFileIdentifier = tokens[IND_PatientName] + token +
341 tokens[IND_StudyInstanceUID] + token +
342 tokens[IND_SerieInstanceUID] + token +
343 tokens[IND_FileName] + token;
346 std::cout << "[" << userFileIdentifier << "] : " << *it << std::endl;
348 // storing in a map ensures automatic sorting !
349 sf[userFileIdentifier] = f;
353 std::cout << " " << std::endl;
355 std::string fullFilename, lastFilename;
356 std::string previousPatientName, currentPatientName;
357 std::string previousStudyInstanceUID, currentStudyInstanceUID;
358 std::string previousSerieInstanceUID, currentSerieInstanceUID;
361 std::string writeDir, currentWriteDir;
362 std::string currentPatientWriteDir;
363 std::string currentStudyWriteDir;
364 std::string currentSerieWriteDir;
366 std::string fullWriteFilename;
368 writeDir = GDCM_NAME_SPACE::Util::NormalizePath(dirNameout);
369 SortedFiles::iterator it2;
371 previousPatientName = "";
372 previousStudyInstanceUID = "";
373 previousSerieInstanceUID = "";
375 GDCM_NAME_SPACE::File *currentFile;
377 for (it2 = sf.begin() ; it2 != sf.end(); ++it2)
379 currentFile = it2->second;
381 fullFilename = currentFile->GetFileName();
382 lastFilename = GDCM_NAME_SPACE::Util::GetName( fullFilename );
384 std::cout <<" ------------------------------------------------------------------------------"
385 << std::endl << " Deal with [" << it2->first << "] : [" <<fullFilename << "]"
389 GDCM_NAME_SPACE::Util::Tokenize (it2->first, tokens, token);
391 currentPatientName = tokens[IND_PatientName];
392 currentStudyInstanceUID = tokens[IND_StudyInstanceUID];
393 currentSerieInstanceUID = tokens[IND_SerieInstanceUID];
395 if (previousPatientName != currentPatientName)
397 previousPatientName = currentPatientName;
399 std::cout << "==== new Patient [" << currentPatientName << "]" << std::endl;
401 previousPatientName = currentPatientName;
402 previousStudyInstanceUID = "";
403 previousSerieInstanceUID = "";
405 currentPatientWriteDir = writeDir + currentPatientName;
407 systemCommand = "mkdir " + currentPatientWriteDir;
409 std::cout << systemCommand << std::endl;
411 system ( systemCommand.c_str() );
414 if (previousStudyInstanceUID != currentStudyInstanceUID)
416 previousStudyInstanceUID = currentStudyInstanceUID;
418 std::cout << "==== === new Study [" << currentStudyInstanceUID << "]"
421 currentStudyWriteDir = currentPatientWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
422 + currentStudyInstanceUID;
423 systemCommand = "mkdir " + currentStudyWriteDir;
424 system (systemCommand.c_str());
428 if (previousSerieInstanceUID != currentSerieInstanceUID)
430 previousSerieInstanceUID = currentSerieInstanceUID;
432 std::cout << "=== ==== === new Serie [" << currentSerieInstanceUID << "]"
435 currentSerieWriteDir = currentStudyWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
436 + currentSerieInstanceUID;
437 systemCommand = "mkdir " + currentSerieWriteDir;
438 system (systemCommand.c_str());
441 if ( GDCM_NAME_SPACE::Debug::GetDebugFlag())
442 std::cout << "--- --- --- --- --- " << it2->first << " "
443 << (it2->second)->GetFileName() << " "
444 << GDCM_NAME_SPACE::Util::GetName( fullFilename ) << std::endl;
446 // If you want to create file names of your own, here is the place!
447 // Just replace 'lastFilename' by anything that's better for you.
448 fullWriteFilename = currentSerieWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
451 systemCommand = "cp " + fullFilename + " " + fullWriteFilename;
452 system ( systemCommand.c_str());