]> Creatis software - gdcm.git/blobdiff - Example/SplitIntoDirectories.cxx
Avoid poluating 'Series Descr" with full path name
[gdcm.git] / Example / SplitIntoDirectories.cxx
index 1e453a19976959428c18a1454d5acf48354d5ad7..07e7b58e713593e16ee7f9737ba900f551e96689 100755 (executable)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: SplitIntoDirectories.cxx,v $
   Language:  C++
-  Date:      $Date: 2007/09/26 08:14:27 $
-  Version:   $Revision: 1.1 $
+  Date:      $Date: 2007/10/30 11:37:16 $
+  Version:   $Revision: 1.4 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -60,7 +60,7 @@ int main(int argc, char *argv[])
    "                  dirout=outputDirectoryName                              ",
    "                  {  [keep= list of seriesNumber to process]              ",
    "                   | [drop= list of seriesNumber to ignore] }             ",
-   "                  [listonly]                                              ",
+   "                  [listonly]  [skel] [seriedescr]                         ",
    "                  [noshadowseq][noshadow][noseq] [verbose] [debug]        ",
    "                                                                          ",
    " dirout : will be created if doesn't exist                                ",
@@ -69,7 +69,10 @@ int main(int argc, char *argv[])
    " drop : if user wants to ignore a limited number of series                ",
    "            he gives the list of 'SeriesNumber' (tag 0020|0011)           ",
    "        SeriesNumber are short enough to be human readable                ",
-   "        e.g : 1030,1035,1043                                              ", 
+   "        e.g : 1030,1035,1043                                              ",
+   " seriedescr : SerieDescription+SerieNumber use for directory name         ",
+   "              (instead of SeriesInstanceUID)                              ",
+   " skel     : name skeleton eg : patName_1.nema -> skel=patName_            ",
    " noshadowseq: user doesn't want to load Private Sequences                 ",
    " noshadow : user doesn't want to load Private groups (odd number)         ",
    " noseq    : user doesn't want to load Sequences                           ",
@@ -78,11 +81,18 @@ int main(int argc, char *argv[])
    FINISH_USAGE
 
 
+   // VERY IMPORTANT :
+   // Respect this order while creating 'UserFileIdentifier'
+   // (mind the order of the 'AddSeriesDetail' !)
+   
    enum Index
    {
       IND_PatientName,
       IND_StudyInstanceUID,
-      IND_SerieInstanceUID
+      IND_SerieInstanceUID,
+      IND_SerieDescription,
+      IND_SerieNumber,
+      IND_FileName
    };
       
    std::cout << "... inside " << argv[0] << std::endl;
@@ -118,9 +128,10 @@ int main(int argc, char *argv[])
    if (am->ArgMgrDefined("debug"))
       GDCM_NAME_SPACE::Debug::DebugOn();
 
-   bool verbose  = ( 0 != am->ArgMgrDefined("verbose") );
-   bool listonly = ( 0 != am->ArgMgrDefined("listonly") );
-           
+   bool verbose    = ( 0 != am->ArgMgrDefined("verbose") );
+   bool listonly   = ( 0 != am->ArgMgrDefined("listonly") );
+   bool seriedescr = ( 0 != am->ArgMgrDefined("seriedescr") );
+            
    int nbSeriesToKeep;
    int *seriesToKeep = am->ArgMgrGetListOfInt("keep", &nbSeriesToKeep);
    int nbSeriesToDrop;
@@ -133,11 +144,12 @@ int main(int argc, char *argv[])
       return 0;         
    }
 
-   int hasSkel = am->ArgMgrDefined("skel");
+   bool hasSkel = ( 0 != am->ArgMgrDefined("hasSkel") );    
    const char *skel;
    if (hasSkel)
       skel = am->ArgMgrGetString("skel");   
-
+      
+      
    const char *input   = am->ArgMgrGetString("input","DCM");
    
    // if unused Param we give up
@@ -228,7 +240,23 @@ int main(int argc, char *argv[])
 
    std::string userFileIdentifier;
    SortedFiles sf;
+
+
+   // VERY IMPORTANT :
+   // While coding the various AddSeriesDetail,
+   // respect the order you choosed in 'enum Index' !
  
+/*
+   enum Index
+   {
+      IND_PatientName,
+      IND_StudyInstanceUID,
+      IND_SerieInstanceUID,
+      IND_SerieDescription,
+      IND_SerieNumber,
+      IND_FileName
+   }; 
+*/     
    s->AddSeriesDetail(0x0010, 0x0010, false); // Patient's Name (false : no convert)
    
    // You may prefer 0020 0010  Study ID
@@ -244,11 +272,15 @@ int main(int argc, char *argv[])
    // use :
    // s->AddSeriesDetail(0x0020, 0x0011, true);    
    s->AddSeriesDetail(0x0020, 0x000e, false); // Series Instance UID (false : no convert)
+
+   s->AddSeriesDetail(0x0008, 0x103e, false); // Serie Description
+   s->AddSeriesDetail(0x0020, 0x0011, false); // Serie Number (more than 1 serie may have the same Ser.Nbr don't 'convert!)
+
    
    // Feel free to add more fields, if they can help a suitable (for you)
    // image sorting
 
-// Loop on all the gdcm-readable files
+   // Loop on all the gdcm-readable files
    for (GDCM_NAME_SPACE::DirListType::iterator it = fileNames.begin();
                                     it != fileNames.end();
                                   ++it)
@@ -256,8 +288,17 @@ int main(int argc, char *argv[])
       f = GDCM_NAME_SPACE::File::New();
       f->SetLoadMode(loadMode);
       f->SetFileName( *it );
+      if (verbose)
+         std::cout << "Try[" << *it << "]\n";
       f->Load();
-
+      if (!f->IsReadable())
+      {
+         if (verbose)
+            std::cout << "File : [" << *it << "] not gdcm-readable -> skipped !" << std::endl;
+         continue;     
+      }
+      if (verbose)
+         std::cout << "Loaded!\n";
       std::string strSeriesNumber;
       int seriesNumber;
       int j;
@@ -301,23 +342,51 @@ int main(int argc, char *argv[])
            f->Delete();
            continue;
         }
-      } 
+      }
 
-      userFileIdentifier=s->CreateUserDefinedFileIdentifier(f); 
+      userFileIdentifier=s->CreateUserDefinedFileIdentifier(f);
+      if (verbose)
+         std::cout << "userFileIdentifier [" << userFileIdentifier << "]" << std::endl; 
       tokens.clear();
-      GDCM_NAME_SPACE::Util::Tokenize (userFileIdentifier, tokens, token); 
-   
-      int imageNum; // Within FileName
+      GDCM_NAME_SPACE::Util::Tokenize (userFileIdentifier, tokens, token);
+
       char newName[1024];
       
+      ///this is a trick to build up a lexicographical compliant name :
+      ///     eg : fich001.ima vs fich100.ima as opposed to fich1.ima vs fich100.ima
+      std::string name = GDCM_NAME_SPACE::Util::GetName( *it );
+      
+      std::cout << "name :[" << name << "]\n";
+      
+      if (hasSkel)
+      {
+         int imageNum; // Within FileName
+         GDCM_NAME_SPACE::Util::Tokenize (name, tokensForFileName, skel);
+         imageNum = atoi ( tokensForFileName[0].c_str() );
+         // probabely we could write something much more complicated using C++ !
+         sprintf (newName, "%s%06d.dcm", skel, imageNum);
+         tokens[IND_FileName] = newName;
+         tokensForFileName.clear();
+       }
+       else
+       {
+         tokens[IND_FileName] = name;
+       }
+    
          // Patient's Name
          // Study Instance UID 
          // Series Instance UID
-
+         // SerieDescription
+         // Serie Number
+         // file Name
+           
       userFileIdentifier = tokens[IND_PatientName]      + token +
                            tokens[IND_StudyInstanceUID] + token + 
-                           tokens[IND_SerieInstanceUID] + token;
+                           tokens[IND_SerieInstanceUID] + token +
+
+                           tokens[IND_SerieDescription] + token +
+                           tokens[IND_SerieNumber]      + token +
+                           tokens[IND_FileName];
          
       if (verbose) 
          std::cout << "[" << userFileIdentifier  << "] : " << *it << std::endl;
@@ -327,17 +396,18 @@ int main(int argc, char *argv[])
    }
    
    if (verbose)
-      std::cout << "  " << std::endl;
+      std::cout << " ==== " << std::endl;
       
    std::string fullFilename, lastFilename;
    std::string previousPatientName, currentPatientName;
    std::string previousStudyInstanceUID, currentStudyInstanceUID;   
    std::string previousSerieInstanceUID, currentSerieInstanceUID;
    
+   std::string currentSerieDescription, currentSerieNumber;   
       
    std::string writeDir, currentWriteDir;
    std::string currentPatientWriteDir;
-   std::string currentStudyWriteDir;    
+   std::string currentStudyWriteDir;
    std::string currentSerieWriteDir; 
 
    std::string fullWriteFilename;
@@ -368,7 +438,9 @@ int main(int argc, char *argv[])
       currentPatientName            = tokens[IND_PatientName];
       currentStudyInstanceUID       = tokens[IND_StudyInstanceUID];      
       currentSerieInstanceUID       = tokens[IND_SerieInstanceUID];
-     
+      currentSerieDescription       = tokens[IND_SerieDescription];
+      currentSerieNumber            = tokens[IND_SerieNumber];
+             
       if (previousPatientName != currentPatientName)
       {  
          previousPatientName = currentPatientName;
@@ -382,38 +454,50 @@ int main(int argc, char *argv[])
          currentPatientWriteDir = writeDir + currentPatientName;
 
          systemCommand   = "mkdir " + currentPatientWriteDir;
-         if (verbose)
-            std::cout << systemCommand << std::endl;
-   
-         system ( systemCommand.c_str() );
+         if (verbose || listonly)
+            std::cout << "[" << systemCommand << "]" << std::endl;
+         if (!listonly)               
+            system ( systemCommand.c_str() );
       }
       
       if (previousStudyInstanceUID != currentStudyInstanceUID)
       {        
+         previousStudyInstanceUID       = currentStudyInstanceUID;
          if (verbose)   
             std::cout << "==== === new Study [" << currentStudyInstanceUID << "]"
                       << std::endl;      
 
          currentStudyWriteDir  = currentPatientWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
                              + currentStudyInstanceUID;
-         systemCommand   = "mkdir " + currentStudyWriteDir;  
-         system (systemCommand.c_str());
+         systemCommand   = "mkdir " + currentStudyWriteDir;
+         
+         if (listonly)
+           std::cout << "[" << systemCommand << "]" << std::endl;         
+         else            
+            system (systemCommand.c_str());
 
-         previousStudyInstanceUID       = currentStudyInstanceUID;
       }  
       
       if (previousSerieInstanceUID != currentSerieInstanceUID)
       {        
+         previousSerieInstanceUID       = currentSerieInstanceUID;
          if (verbose)   
             std::cout << "=== ==== === new Serie [" << currentSerieInstanceUID << "]"
-                      << std::endl;      
-
-         currentSerieWriteDir  = currentStudyWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
-                             + currentSerieInstanceUID;
-         systemCommand   = "mkdir " + currentSerieWriteDir;  
-         system (systemCommand.c_str());
-
-         previousSerieInstanceUID       = currentSerieInstanceUID;
+                      << std::endl;
+                            
+         if (seriedescr) // more human readable!
+            currentSerieWriteDir  = currentStudyWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
+                                  + currentSerieDescription + "_" + currentSerieNumber;
+         else
+            currentSerieWriteDir  = currentStudyWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR
+                                  + currentSerieInstanceUID;         
+                                 
+         systemCommand   = "mkdir " + currentSerieWriteDir;
+         
+         if (listonly)
+            std::cout << "[" << systemCommand << "]" << std::endl;         
+         else             
+            system (systemCommand.c_str());
       }            
    
       if ( GDCM_NAME_SPACE::Debug::GetDebugFlag())
@@ -427,7 +511,11 @@ int main(int argc, char *argv[])
                                          + lastFilename; 
 
       systemCommand   = "cp " + fullFilename + " " + fullWriteFilename;
-      system ( systemCommand.c_str());          
+      
+      if (listonly)
+         std::cout << "[" << systemCommand << "]" << std::endl;         
+      else             
+         system (systemCommand.c_str());
 
    }
    return 0;