1 /*=========================================================================
4 Module: $RCSfile: SplitIntoDirectories.cxx,v $
6 Date: $Date: 2011/08/25 14:37:05 $
7 Version: $Revision: 1.11 $
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] [seriedescr] ",
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 " seriedescr : SerieDescription+SerieNumber use for directory name ",
74 " (instead of SeriesInstanceUID) ",
75 " skel : name skeleton eg : patName_1.nema -> skel=patName_ ",
76 " noshadowseq: user doesn't want to load Private Sequences ",
77 " noshadow : user doesn't want to load Private groups (odd number) ",
78 " noseq : user doesn't want to load Sequences ",
79 " verbose : user wants to run the program in 'verbose mode' ",
80 " debug : *developer* wants to run the program in 'debug mode' ",
85 // Respect this order while creating 'UserFileIdentifier'
86 // (mind the order of the 'AddSeriesDetail' !)
98 std::cout << "... inside " << argv[0] << std::endl;
100 // ----- Initialize Arguments Manager ------
102 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
104 if (argc == 1 || am->ArgMgrDefined("usage"))
106 am->ArgMgrUsage(usage); // Display 'usage'
111 const char *dirNamein;
112 dirNamein = am->ArgMgrGetString("dirin",".");
114 const char *dirNameout;
115 dirNameout = am->ArgMgrGetString("dirout",".");
117 int loadMode = GDCM_NAME_SPACE::LD_ALL;
118 if ( am->ArgMgrDefined("noshadowseq") )
119 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOWSEQ;
122 if ( am->ArgMgrDefined("noshadow") )
123 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOW;
124 if ( am->ArgMgrDefined("noseq") )
125 loadMode |= GDCM_NAME_SPACE::LD_NOSEQ;
128 if (am->ArgMgrDefined("debug"))
129 GDCM_NAME_SPACE::Debug::DebugOn();
131 bool verbose = ( 0 != am->ArgMgrDefined("verbose") );
132 bool listonly = ( 0 != am->ArgMgrDefined("listonly") );
133 bool seriedescr = ( 0 != am->ArgMgrDefined("seriedescr") );
136 int *seriesToKeep = am->ArgMgrGetListOfInt("keep", &nbSeriesToKeep);
138 int *seriesToDrop = am->ArgMgrGetListOfInt("drop", &nbSeriesToDrop);
140 if ( nbSeriesToKeep!=0 && nbSeriesToDrop!=0)
142 std::cout << "KEEP and DROP are mutually exclusive !" << std::endl;
147 bool hasSkel = ( 0 != am->ArgMgrDefined("hasSkel") );
150 skel = am->ArgMgrGetString("skel");
153 const char *input = am->ArgMgrGetString("input","DCM");
155 // if unused Param we give up
156 if ( am->ArgMgrPrintUnusedLabels() )
158 am->ArgMgrUsage(usage);
162 delete am; // we don't need Argument Manager any longer
164 // ----- Begin Processing -----
167 // --> Check supposed-to-be-directory names
169 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNamein) )
171 std::cout << "KO : [" << dirNamein << "] is not a Directory."
178 std::cout << "OK : [" << dirNamein << "] is a Directory." << std::endl;
181 std::string systemCommand;
183 std::cout << "Check for output directory :[" << dirNameout << "]."
185 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNameout) ) // dirout not found
187 std::string strDirNameout(dirNameout); // to please gcc 4
188 systemCommand = "mkdir \"" +strDirNameout + "\""; // create it!
190 std::cout << systemCommand << std::endl;
191 system (systemCommand.c_str());
192 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNameout) ) // be sure it worked
194 std::cout << "KO : not a dir : [" << dirNameout << "] (creation failure ?)"
200 std::cout << "Directory [" << dirNameout << "] created." << std::endl;
205 std::cout << "Output Directory [" << dirNameout
206 << "] already exists; Used as is."
209 // --> End of checking supposed-to-be-directory names
211 std::string strDirNamein(dirNamein);
212 // true ; get recursively the list of files
213 GDCM_NAME_SPACE::DirList dirList(strDirNamein, true);
217 std::cout << "------------List of found files ------------" << std::endl;
219 std::cout << std::endl;
223 // ======================================= The job starts here =========================
225 GDCM_NAME_SPACE::DirListType fileNames;
226 fileNames = dirList.GetFilenames();
228 GDCM_NAME_SPACE::SerieHelper *s; // Needed to use SerieHelper::AddSeriesDetail()
229 s = GDCM_NAME_SPACE::SerieHelper::New();
231 std::string token = "%%%"; // Hope it's enough!
233 GDCM_NAME_SPACE::File *f;
234 std::vector<std::string> tokens;
235 std::vector<std::string> tokensForFileName;
238 std::cout << "------------------Print Break levels-----------------" << std::endl;
240 std::string userFileIdentifier;
245 // While coding the various AddSeriesDetail,
246 // respect the order you choosed in 'enum Index' !
252 IND_StudyInstanceUID,
253 IND_SerieInstanceUID,
254 IND_SerieDescription,
259 s->AddSeriesDetail(0x0010, 0x0010, false); // Patient's Name (false : no convert)
261 // You may prefer 0020 0010 Study ID
263 // s->AddSeriesDetail(0x0020, 0x0010, true);
264 // Avoid using 0008 0020 Study Date,
265 // since you may have more than one study, for a given Patient, at a given Date!
266 // or the field may be empty!
267 s->AddSeriesDetail(0x0020, 0x000d, false); // Study Instance UID (false : no convert)
269 // You may prefer 0020 0011 Series Number
271 // s->AddSeriesDetail(0x0020, 0x0011, true);
272 s->AddSeriesDetail(0x0020, 0x000e, false); // Series Instance UID (false : no convert)
274 s->AddSeriesDetail(0x0008, 0x103e, false); // Serie Description
275 s->AddSeriesDetail(0x0020, 0x0011, false); // Serie Number (more than 1 serie may have the same Ser.Nbr don't 'convert!)
277 // Feel free to add more fields, if they can help a suitable (for you)
280 // Loop on all the gdcm-readable files
281 for (GDCM_NAME_SPACE::DirListType::iterator it = fileNames.begin();
282 it != fileNames.end();
285 f = GDCM_NAME_SPACE::File::New();
286 f->SetLoadMode(loadMode);
287 f->SetFileName( *it );
289 std::cout << "Try[" << *it << "]\n";
291 if (!f->Document::IsReadable())
294 std::cout << "File : [" << *it << "] not gdcm-readable -> skipped !" << std::endl;
298 std::cout << "Loaded!\n";
299 std::string strSeriesNumber;
303 // keep only requested Series
305 if (nbSeriesToKeep != 0)
307 strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
308 seriesNumber = atoi( strSeriesNumber.c_str() );
309 for (j=0; j<nbSeriesToKeep; j++)
311 if(seriesNumber == seriesToKeep[j])
323 // drop all unrequested Series
325 if (nbSeriesToDrop != 0)
327 strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
328 seriesNumber = atoi( strSeriesNumber.c_str() );
329 for (j=0;j<nbSeriesToDrop; j++)
331 if(seriesNumber == seriesToDrop[j])
344 userFileIdentifier=s->CreateUserDefinedFileIdentifier(f);
346 std::cout << "userFileIdentifier [" << userFileIdentifier << "]" << std::endl;
348 GDCM_NAME_SPACE::Util::Tokenize (userFileIdentifier, tokens, token);
352 ///this is a trick to build up a lexicographical compliant name :
353 /// eg : fich001.ima vs fich100.ima as opposed to fich1.ima vs fich100.ima
354 std::string name = GDCM_NAME_SPACE::Util::GetName( *it );
357 // std::cout << "name :[" << name << "]\n";
361 int imageNum; // Within FileName
362 GDCM_NAME_SPACE::Util::Tokenize (name, tokensForFileName, skel);
363 imageNum = atoi ( tokensForFileName[0].c_str() );
364 // probabely we could write something much more complicated using C++ !
365 sprintf (newName, "%s%06d.dcm", skel, imageNum);
366 tokens[IND_FileName] = newName;
367 tokensForFileName.clear();
371 tokens[IND_FileName] = name;
375 // Study Instance UID
376 // Series Instance UID
381 userFileIdentifier = tokens[IND_PatientName] + token +
382 tokens[IND_StudyInstanceUID] + token +
383 tokens[IND_SerieInstanceUID] + token +
385 tokens[IND_SerieDescription] + token +
386 tokens[IND_SerieNumber] + token +
387 tokens[IND_FileName];
389 std::cout << "[" << userFileIdentifier << "] : " << *it << std::endl;
391 // storing in a map ensures automatic sorting !
392 sf[userFileIdentifier] = f;
396 std::cout << " ==== " << std::endl;
398 std::string fullFilename, lastFilename;
399 std::string previousPatientName, currentPatientName;
400 std::string previousStudyInstanceUID, currentStudyInstanceUID;
401 std::string previousSerieInstanceUID, currentSerieInstanceUID;
403 std::string currentSerieDescription, currentSerieNumber;
405 std::string writeDir, currentWriteDir;
406 std::string currentPatientWriteDir;
407 std::string currentStudyWriteDir;
408 std::string currentSerieWriteDir;
410 std::string fullWriteFilename;
412 writeDir = GDCM_NAME_SPACE::Util::NormalizePath(dirNameout);
413 SortedFiles::iterator it2;
415 previousPatientName = "";
416 previousStudyInstanceUID = "";
417 previousSerieInstanceUID = "";
419 GDCM_NAME_SPACE::File *currentFile;
420 std::string replaceChar("_");
421 for (it2 = sf.begin() ; it2 != sf.end(); ++it2)
423 currentFile = it2->second;
425 fullFilename = currentFile->GetFileName();
426 lastFilename = GDCM_NAME_SPACE::Util::GetName( fullFilename );
428 std::cout <<" ------------------------------------------------------------------------------"
429 << std::endl << " Deal with [" << it2->first << "] : [" <<fullFilename << "]"
433 GDCM_NAME_SPACE::Util::Tokenize (it2->first, tokens, token);
435 currentPatientName = tokens[IND_PatientName];
436 currentStudyInstanceUID = tokens[IND_StudyInstanceUID];
437 currentSerieInstanceUID = tokens[IND_SerieInstanceUID];
438 currentSerieDescription = tokens[IND_SerieDescription];
439 currentSerieNumber = tokens[IND_SerieNumber];
441 if (previousPatientName != currentPatientName)
443 previousPatientName = currentPatientName;
445 std::cout << "==== new Patient [" << currentPatientName << "]" << std::endl;
447 previousPatientName = currentPatientName;
448 previousStudyInstanceUID = "";
449 previousSerieInstanceUID = "";
451 currentPatientWriteDir = writeDir + currentPatientName;
453 systemCommand = "mkdir \"" + currentPatientWriteDir + "\"";
454 if (verbose || listonly)
455 std::cout << "[" << systemCommand << "]" << std::endl;
457 system ( systemCommand.c_str() );
460 if (previousStudyInstanceUID != currentStudyInstanceUID)
462 previousStudyInstanceUID = currentStudyInstanceUID;
464 std::cout << "==== === new Study [" << currentStudyInstanceUID << "]"
467 currentStudyWriteDir = currentPatientWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
468 + currentStudyInstanceUID;
469 systemCommand = "mkdir \"" + currentStudyWriteDir + "\"";
472 std::cout << "Directory [" << currentStudyWriteDir << "] created" << std::endl;
475 std::cout << "[" << systemCommand << "]" << std::endl;
477 system (systemCommand.c_str());
480 if (previousSerieInstanceUID != currentSerieInstanceUID)
482 previousSerieInstanceUID = currentSerieInstanceUID;
484 std::cout << "=== ==== === new Serie [" << currentSerieInstanceUID << "]"
487 if (seriedescr) // more human readable!
488 currentSerieWriteDir = currentStudyWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
489 + currentSerieDescription + "_" + currentSerieNumber
490 /*+ "_" + currentSerieInstanceUID */
494 currentSerieWriteDir = currentStudyWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
495 + currentSerieInstanceUID;
497 systemCommand = "mkdir \"" + currentSerieWriteDir + "\"";
500 std::cout << "[" << systemCommand << "]" << std::endl;
502 system (systemCommand.c_str());
505 std::cout << "Directory [" << currentSerieWriteDir << "] created" << std::endl;
508 if ( GDCM_NAME_SPACE::Debug::GetDebugFlag())
509 std::cout << "--- --- --- --- --- " << it2->first << " "
510 << (it2->second)->GetFileName() << " "
511 << GDCM_NAME_SPACE::Util::GetName( fullFilename ) << std::endl;
513 // If you want to create file names of your own, here is the place!
514 // Just replace 'lastFilename' by anything that's better for you.
515 GDCM_NAME_SPACE:: Util::ReplaceSpecChar(lastFilename, replaceChar);
517 fullWriteFilename = currentSerieWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
520 systemCommand = "cp \"" + fullFilename + "\" " + fullWriteFilename;
523 std::cout << "[" << systemCommand << "]" << std::endl;
525 system (systemCommand.c_str());