Skip to Content
go-dicom(3)
Packages

fileset

Reads and writes DICOMDIR files, the directory of a PS3.10 file-set on media, and keeps an in-memory list of the DICOM files under a directory.

import "github.com/amrshadid/go-dicom/fileset" · pkg.go.dev

When to use it

  • Read a DICOMDIR from a CD, DVD or USB file-set and walk its patients, studies, series and images: ReadDICOMDIR.
  • Write a DICOMDIR for files you are putting on media: Builder.
  • Scan a folder and find files by patient, study, series or modality: FileSet.

The command line's dicomdir is built on it.

Key types and functions

NameWhat it is for
ReadDICOMDIR, DICOMDIRThe directory records of a read DICOMDIR, as a tree.
ReadDICOMDIRWithOptions, ReadOptionsThe same, with a bound on the number of records.
RecordOne directory record: type, in-use flag, Referenced File ID, its data set and children.
DICOMDIR.RecordsOfType, DICOMDIR.WalkFind records of one type; visit every record.
RecordPatient and the other Record… constantsDirectory Record Type values.
NewBuilder, Builder, DICOMDIROptionsCollect each instance's File ID and header, then write the DICOMDIR. Safe for several goroutines.
ValidFileIDWhether File ID components meet PS3.10 8.5: one to eight components of one to eight characters from A-Z, 0-9 and underscore.
NewFileSet, FileSetFiles under a root directory: scan, find, sort, generate a DICOMDIR.
FileSet.Scan, SkippedFileAdd every DICOM file found, and say which were skipped and why.
ErrTooManyRecords, ErrInvalidFileIDErrors to test for.

Example

// List what a DICOMDIR says is on the media, patient by patient.
package main

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/amrshadid/go-dicom/filebase"
	"github.com/amrshadid/go-dicom/filereader"
	"github.com/amrshadid/go-dicom/fileset"
)

func main() {
	f, err := os.Open(os.Args[1]) // the DICOMDIR file
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	df, err := filereader.ReadDICOMFile(filebase.NewFileReader(f))
	if err != nil {
		log.Fatal(err)
	}
	dir, err := fileset.ReadDICOMDIR(df)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("records:", len(dir.Records), "images:", len(dir.RecordsOfType(fileset.RecordImage)))

	var show func(r *fileset.Record, depth int)
	show = func(r *fileset.Record, depth int) {
		line := strings.Repeat("  ", depth) + r.Type
		if r.Type == fileset.RecordPatient {
			line += " " + r.Dataset.GetStringByKeyword("PatientName")
		}
		if r.HasValidFileID() {
			line += " " + filepath.Join(r.ReferencedFileID...)
		}
		if depth < 3 { // patient, study, series
			fmt.Println(line)
		}
		for _, c := range r.Children {
			show(c, depth+1)
		}
	}
	for _, root := range dir.Roots {
		show(root, 0)
	}
}

On pydicom's dicomdirtests/DICOMDIR it prints records: 52 images: 31 and the patient, study and series tree with each record's file.

Notes

  • A record not in use (a deleted entry) is kept in the tree with InUse false, so you see what the file claims.
  • MaxRecords bounds the tree, not the parse: bound the size of a DICOMDIR you do not trust before reading it.
  • ConvertFileSetToRawFormat, ConvertRawFormatToFileSet and ExecuteFileSetHook belong to the hooks machinery and are not needed to read or write a DICOMDIR.
  • What is written and how it is checked is in CONFORMANCE.md §8.6.

Tip

Join Record.ReferencedFileID with filepath.Join under the directory the DICOMDIR is in. The components are kept apart because the separator belongs to the media, not the file-set.

See also

filereader · filewriter · Media and DICOMDIR

On this page