Skip to Content
go-dicom(3)
Packages

dcmstore

An on-disk store of DICOM instances with an index that answers C-FIND queries, and a network.Handler that serves Verification, Storage and Query/Retrieve from it. Together they are a small archive.

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

When to use it

  • You need an SCP that receives images and answers C-FIND, C-MOVE and C-GET, without writing the filing, indexing and matching yourself.
  • You want a DICOMweb server: dicomweb.NewServer takes a *Store as its backend.
  • You want to file instances by study and series and query them in code.

The command line's qrscp is this package behind network.

Key types and functions

NameWhat it is for
Open, StoreOpen or create an archive directory and load its index.
Store.Store, Store.PutFile a data set and index it.
Store.QueryAnswer a C‑FIND identifier at its Query/Retrieve Level.
Store.MatchingInstances, InstanceThe instances a query names, as index entries.
Store.Load, Store.OpenRead an instance back as a data set or as its file.
Store.LocateThe SOP Instance UIDs under a study, series or instance.
Store.Remove, Store.RebuildDelete one instance; rebuild the index from the files.
NewHandler, HandlerA network.Handler over a store; OnStored is called after each instance is written.
SupportedSOPClasses, SupportedTransferSyntaxesWhat to give the SCP to accept.
QueryNegotiationThe extended negotiation the handler supports for C‑FIND.
QueryLevel, LevelRead Query/Retrieve Level from a query.

Example

// File instances into an archive directory and query it as a C-FIND would.
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/amrshadid/go-dicom/dataelem"
	"github.com/amrshadid/go-dicom/dataset"
	"github.com/amrshadid/go-dicom/dcmstore"
	"github.com/amrshadid/go-dicom/filebase"
	"github.com/amrshadid/go-dicom/filereader"
)

func main() {
	ctx := context.Background()
	store, err := dcmstore.Open(os.Args[1]) // created if it does not exist
	if err != nil {
		log.Fatal(err)
	}

	for _, path := range os.Args[2:] {
		f, err := os.Open(path)
		if err != nil {
			log.Fatal(err)
		}
		df, err := filereader.ReadDICOMFile(filebase.NewFileReader(f))
		f.Close()
		if err != nil {
			log.Fatal(err)
		}
		if _, err := store.Store(ctx, df.GetDataset()); err != nil {
			log.Fatal(err)
		}
	}
	fmt.Println("instances:", store.Count())

	query := dataset.NewDataset()
	for _, k := range []struct {
		keyword string
		vr      dataelem.VR
		value   string
	}{
		{"QueryRetrieveLevel", dataelem.CS, "STUDY"},
		{"ModalitiesInStudy", dataelem.CS, "MR"},
		{"PatientName", dataelem.PN, ""},
		{"StudyInstanceUID", dataelem.UI, ""},
	} {
		if err := query.AddByKeyword(k.keyword, k.vr, k.value); err != nil {
			log.Fatal(err)
		}
	}
	matches, err := store.Query(ctx, query)
	if err != nil {
		log.Fatal(err)
	}
	for _, m := range matches {
		fmt.Println(m.GetStringByKeyword("PatientName"), m.GetStringByKeyword("StudyInstanceUID"))
	}
}

Given a new directory, CT_small.dcm and MR_small.dcm, it prints instances: 2 and the one study whose modalities include MR.

To serve the same store over the network:

scp := network.NewSCP(network.SCPConfig{AETitle: "ARCHIVE", Port: 11112})
scp.SetHandler(dcmstore.NewHandler(store))
scp.SetSupportedAbstractSyntaxes(dcmstore.SupportedSOPClasses())
if err := scp.ListenAndServe(ctx); err != nil {
	log.Fatal(err)
}

Notes

  • Files sit under <study>/<series>/<instance>.dcm. Every UID is checked before it becomes part of a path; see fileutil.
  • index.json holds the indexed attributes and index.log the changes since. A missing or unreadable index is rebuilt from the files at Open.
  • Only the keys PS3.4 C.6.1.1 and C.6.2.1 define, and those a DICOMweb search returns, are indexed. A query on any other attribute is not matched on.
  • A Store is safe for concurrent use within one process.
  • Matching is matching's, run against the index; what is supported is in CONFORMANCE.md §2.7.

Warning

Nothing locks the directory against a second process. Open each archive directory from one process at a time.

See also

network · matching · dicomweb · Network SCP · Query and retrieve · DICOMweb server

On this page