Skip to content

Commit

Permalink
tidy up file writing
Browse files Browse the repository at this point in the history
  • Loading branch information
JFriel committed Feb 17, 2025
1 parent c109d31 commit 48e5e64
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions Rdmp.Dicom/Extraction/DicomTagToCSV.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CsvHelper;
using CsvHelper.Configuration;
using FellowOakDicom;
using Rdmp.Core.Curation.Data;
using Rdmp.Core.DataExport.DataExtraction.Commands;
Expand Down Expand Up @@ -76,24 +77,26 @@ public DataTable ProcessPipelineData(DataTable toProcess, IDataLoadEventListener

var dicomFilePaths = new AmbiguousFilePath(ArchiveRootIfAny, dicomFiles).GetDataset(FileFetchRetryLimit, FileFetchRetryTimeout, listener);
var destinationDirectory = new DirectoryInfo(Path.Combine(_extractCommand.GetExtractionDirectory().FullName));
destinationDirectory.Create();
var filepath = Path.Combine(destinationDirectory.FullName, "DicomTags.csv");

foreach (var dcm in dicomFilePaths)
using (var sw = new StreamWriter(filepath))
{
try
sw.WriteLine("Id,Name,Value");
foreach (var dcm in dicomFilePaths)
{
destinationDirectory.Create();
var filepath = Path.Combine(destinationDirectory.FullName, "DicomTags.csv");

var sw = new StreamWriter(filepath);
using var w = new CsvWriter(sw, System.Globalization.CultureInfo.InvariantCulture);
w.WriteRecords(
DicomFile.Open(dcm.Item1, FileReadOption.ReadAll).Dataset.SelectMany(t => Entry.ProcessTag(dcm.Item1, t))
);
}
catch (Exception e)
{
listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Error, $"Failed to extract tags from DICOM {dcm.Item1}", e));
_errors++;
try
{
foreach (var record in DicomFile.Open(dcm.Item1, FileReadOption.ReadAll).Dataset.SelectMany(t => Entry.ProcessTag(dcm.Item1, t)))
{
sw.WriteLine($"{record.Id},{record.Name},{record.Value}");
}
}
catch (Exception e)
{
listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Error, $"Failed to extract tags from DICOM {dcm.Item1}", e));
_errors++;
}
}
}

Expand Down

0 comments on commit 48e5e64

Please sign in to comment.