]> Creatis software - clitk.git/blobdiff - tools/clitkWriteDicomSeriesGenericFilter.txx
Debug RTStruct conversion with empty struc
[clitk.git] / tools / clitkWriteDicomSeriesGenericFilter.txx
index a1cdd2613fd828dfb3ab8ce02587f679f0d95e53..a2bccc4aa0daa52fbef3b8a34cb115b3eb9cc20c 100644 (file)
@@ -153,6 +153,12 @@ WriteDicomSeriesGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
 
   // Read the series
   typename ReaderType::Pointer reader = ReaderType::New();
+  if (m_ArgsInfo.preserve_flag) {
+#if GDCM_MAJOR_VERSION >= 2
+    gdcmIO->LoadPrivateTagsOn();
+    gdcmIO->KeepOriginalUIDOn();
+#endif
+  }
   reader->SetImageIO( gdcmIO );
   reader->SetFileNames( filenames_in );
   try {
@@ -241,6 +247,8 @@ WriteDicomSeriesGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
 
   // Get keys
   unsigned int numberOfKeysGiven=m_ArgsInfo.key_given;
+    if (m_ArgsInfo.verbose_flag) 
+      DD(numberOfKeysGiven);
 
   std::string seriesUID;
   std::string frameOfReferenceUID;
@@ -248,68 +256,85 @@ WriteDicomSeriesGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
   
   // one pass through the keys given on the cmd-line, to check what will be recreated
   std::string seriesUIDkey = "0020|000e";
+  std::string seriesNumberKey = "0020|0011";
+  std::string seriesDescriptionKey = "0008|103e";
   std::string frameOfReferenceUIDKey = "0020|0052";
   std::string studyUIDKey = "0020|000d";
+  std::string studyIDKey = "0020|0010";
+  std::string studyDescriptionKey = "0008|1030";
   bool seriesUIDGiven = false;
+  bool seriesNumberGiven = false;
+  bool seriesDescriptionGiven = false;
   bool studyUIDGiven = false;
+  bool studyIDGiven = false;
+  bool studyDescriptionGiven = false;
   for (unsigned int i = 0; i < numberOfKeysGiven; i++) {
     std::string entryId( m_ArgsInfo.key_arg[i] );
+    if (m_ArgsInfo.verbose_flag) 
+      DD(entryId);
     
-    seriesUIDGiven = (entryId ==  seriesUIDkey || entryId ==  frameOfReferenceUIDKey);
-    if (seriesUIDGiven)
-    {
-      if (entryId ==  seriesUIDkey) 
-        seriesUID = m_ArgsInfo.tag_arg[i];
-      else
-        frameOfReferenceUID = m_ArgsInfo.tag_arg[i];
-    }
-    else if (m_ArgsInfo.newSeriesUID_flag) {
+    seriesUIDGiven |= (entryId ==  seriesUIDkey || entryId ==  frameOfReferenceUIDKey);
+    seriesNumberGiven |= (entryId == seriesNumberKey);
+    seriesDescriptionGiven |= (entryId == seriesDescriptionKey);
+    studyUIDGiven |= (entryId == studyUIDKey);
+    studyIDGiven |= (entryId == studyIDKey);
+    studyDescriptionGiven |= (entryId == studyDescriptionKey);
+  }
+
+  // force the creation of a new series if a new study was specified
+  if (!studyUIDGiven && m_ArgsInfo.newStudyUID_flag) {
+    m_ArgsInfo.newSeriesUID_flag = true;
 #if GDCM_MAJOR_VERSION >= 2
-      gdcm::UIDGenerator suid;
-      seriesUID = suid.Generate();
-      gdcm::UIDGenerator fuid;
-      frameOfReferenceUID = fuid.Generate();
+    gdcm::UIDGenerator suid;
+    studyUID = suid.Generate();
 #else
-      seriesUID = gdcm::Util::CreateUniqueUID( gdcmIO->GetUIDPrefix());
-      frameOfReferenceUID = gdcm::Util::CreateUniqueUID( gdcmIO->GetUIDPrefix());
+    studyUID = gdcm::Util::CreateUniqueUID( gdcmIO->GetUIDPrefix());
 #endif
-      if (m_ArgsInfo.verbose_flag)
-      {
-        DD(seriesUID);
-        DD(frameOfReferenceUID);
-      }
-    }
-  
-    studyUIDGiven = (entryId == studyUIDKey);
-    if (studyUIDGiven) 
-      studyUID = m_ArgsInfo.tag_arg[i];
-    else if (m_ArgsInfo.newStudyUID_flag) {
+  }
+    
+  if (!seriesUIDGiven && m_ArgsInfo.newSeriesUID_flag) {
 #if GDCM_MAJOR_VERSION >= 2
-      gdcm::UIDGenerator suid;
-      studyUID = suid.Generate();
+    gdcm::UIDGenerator suid;
+    seriesUID = suid.Generate();
+    gdcm::UIDGenerator fuid;
+    frameOfReferenceUID = fuid.Generate();
 #else
-      studyUID = gdcm::Util::CreateUniqueUID( gdcmIO->GetUIDPrefix());
-#endif
-      if (m_ArgsInfo.verbose_flag)
-        DD(studyUID);
-    }
-#if GDCM_MAJOR_VERSION < 2
-    else
-      gdcmIO->KeepOriginalUID();
+    seriesUID = gdcm::Util::CreateUniqueUID( gdcmIO->GetUIDPrefix());
+    frameOfReferenceUID = gdcm::Util::CreateUniqueUID( gdcmIO->GetUIDPrefix());
 #endif
   }
 
+  if (m_ArgsInfo.verbose_flag) {
+    DD(seriesUID);
+    DD(frameOfReferenceUID);
+    DD(studyUID);
+  }
+
   // check if file UIDs will be be preserved
   bool useInputFileUID = true;
-  if (m_ArgsInfo.newSeriesUID_flag || m_ArgsInfo.newStudyUID_flag || seriesUIDGiven || studyUIDGiven)
-      useInputFileUID = false;
+  if (m_ArgsInfo.newSeriesUID_flag || m_ArgsInfo.newStudyUID_flag || seriesUIDGiven || studyUIDGiven) {
+    useInputFileUID = false;
+  }
   else {
+#if GDCM_MAJOR_VERSION < 2
+    gdcmIO->SetKeepOriginalUID(true);
+#endif
     namesGenerator->SetOutputDirectory( m_ArgsInfo.outputDir_arg  );
-    filenames_out = namesGenerator->GetOutputFileNames();
   }
   
   filenames_out.resize(numberOfFilenames);
   
+  time_t t;
+  t = time(&t);
+  struct tm* instanceDateTimeTm = localtime(&t);
+  char datetime[16];
+  strftime(datetime, 16, "%Y%m%d", instanceDateTimeTm);
+  std::ostringstream instanceDate;
+  instanceDate << datetime;
+  std::ostringstream instanceTime;
+  strftime(datetime, 16, "%H%M%S", instanceDateTimeTm);
+  instanceTime << datetime;
+  
   // update output dicom keys/tags
   for(unsigned int fni = 0; fni<numberOfFilenames; fni++) {
     for (unsigned int i = 0; i < numberOfKeysGiven; i++) {
@@ -319,7 +344,7 @@ WriteDicomSeriesGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
       itk::EncapsulateMetaData<std::string>( *((*dictionary)[fni]), entryId, value );
     }
 
-    // 
+    // series UID
     if (!seriesUIDGiven) {
       if (m_ArgsInfo.newSeriesUID_flag) {
         itk::EncapsulateMetaData<std::string>( *((*dictionary)[fni]), seriesUIDkey, seriesUID );
@@ -327,14 +352,40 @@ WriteDicomSeriesGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
       }
     }
     
+    // study UID
     if (!studyUIDGiven) {
       if (m_ArgsInfo.newStudyUID_flag) 
         itk::EncapsulateMetaData<std::string>( *((*dictionary)[fni]), studyUIDKey, studyUID );
     }
+    
+    // study description
+    if (studyUIDGiven || m_ArgsInfo.newStudyUID_flag) {
+      if (!studyIDGiven)
+        itk::EncapsulateMetaData<std::string>( *((*dictionary)[fni]), studyIDKey,itksys::SystemTools::GetFilenameName( m_ArgsInfo.outputDir_arg ));
+      if (!studyDescriptionGiven)
+        itk::EncapsulateMetaData<std::string>( *((*dictionary)[fni]), studyDescriptionKey,itksys::SystemTools::GetFilenameName( m_ArgsInfo.outputDir_arg ));
+      
+      itk::EncapsulateMetaData<std::string>( *((*dictionary)[fni]), "0008|0020", instanceDate.str() );
+      itk::EncapsulateMetaData<std::string>( *((*dictionary)[fni]), "0008|0030", instanceTime.str() );
+    }
+    
+    // series description/number
+    if (seriesUIDGiven || m_ArgsInfo.newSeriesUID_flag) {
+      if (!seriesDescriptionGiven)
+        itk::EncapsulateMetaData<std::string>( *((*dictionary)[fni]), seriesDescriptionKey, itksys::SystemTools::GetFilenameName(m_ArgsInfo.outputDir_arg) );
+      if (!seriesNumberGiven)
+        itk::EncapsulateMetaData<std::string>( *((*dictionary)[fni]), seriesNumberKey, itksys::SystemTools::GetFilenameName(m_ArgsInfo.outputDir_arg) );
+
+      itk::EncapsulateMetaData<std::string>( *((*dictionary)[fni]), "0008|0012", instanceDate.str() );
+      itk::EncapsulateMetaData<std::string>( *((*dictionary)[fni]), "0008|0013", instanceTime.str() );
+    }
 
     // file UIDs are recreated for new studies or series
     if (!useInputFileUID)
     {
+      if (m_ArgsInfo.verbose_flag)
+        std::cout << "Recreating file UIDs" << std::endl;
+
       std::string fileUID;
 #if GDCM_MAJOR_VERSION >= 2
       gdcm::UIDGenerator fid;
@@ -344,6 +395,7 @@ WriteDicomSeriesGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
 #endif
       itk::EncapsulateMetaData<std::string>( *((*dictionary)[fni]), "0008|0018", fileUID );
       itk::EncapsulateMetaData<std::string>( *((*dictionary)[fni]), "0002|0003", fileUID );
+      
       filenames_out[fni] = itksys::SystemTools::CollapseFullPath(fileUID.c_str(), m_ArgsInfo.outputDir_arg) + std::string(".dcm"); 
     }
   }
@@ -361,6 +413,8 @@ WriteDicomSeriesGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
 
   // Write
   try {
+    if (m_ArgsInfo.verbose_flag)
+      std::cout << seriesWriter << std::endl;
     seriesWriter->Update();
   } catch( itk::ExceptionObject & excp ) {
     std::cerr << "Error: Exception thrown while writing the series!!" << std::endl;