Home   Package List   Routine Alphabetical List   Global Alphabetical List   FileMan Files List   FileMan Sub-Files List   Package Component Lists   Package-Namespace Mapping  
Routine: MAGDSTAC

MAGDSTAC.m

Go to the documentation of this file.
  1. MAGDSTAC ;WOIFO/PMK - Q/R Retrieve of DICOM images from PACS to VistA ; Feb 15, 2022@10:52:31
  1. ;;3.0;IMAGING;**231,305**;Mar 19, 2002;Build 3
  1. ;; Per VHA Directive 2004-038, this routine should not be modified.
  1. ;; +---------------------------------------------------------------+
  1. ;; | Property of the US Government. |
  1. ;; | No permission to copy or redistribute this software is given. |
  1. ;; | Use of unreleased versions of this software requires the user |
  1. ;; | to execute a written test agreement with the VistA Imaging |
  1. ;; | Development Office of the Department of Veterans Affairs, |
  1. ;; | telephone (301) 734-0100. |
  1. ;; | The Food and Drug Administration classifies this software as |
  1. ;; | a medical device. As such, it may not be changed in any way. |
  1. ;; | Modifications to this software may result in an adulterated |
  1. ;; | medical device under 21CFR820, the use of which is considered |
  1. ;; | to be a violation of US Federal Statutes. |
  1. ;; +---------------------------------------------------------------+
  1. ;;
  1. Q
  1. ;
  1. ; COMPARE is called by MAGDSTAA when the study's images are already on VistA.
  1. ;
  1. ; It is used to retrieve possibly some missing SOP Instances from PACS.
  1. ;
  1. ; This is a multi-step process.
  1. ;
  1. ;
  1. ; First, get VistA study, series, and image UID info.
  1. ; The Study Instance UID (0020,000D), Series Instance UIDs (0020,000E),
  1. ; and SOP Instance UIDs (0008,0018) are retrieved from VistA from either
  1. ; the legacy Image file (#2005) or the new SOP Class Database (#2005.6x).
  1. ;
  1. ; Count the number of Series Instance UIDs and number of SOP Instances.
  1. ;
  1. ; Store the data in the following hierarchy:
  1. ;
  1. ; ^TMP("MAG",$J,"UIDS","VISTA",0)=<study count>
  1. ; ^TMP("MAG",$J,"UIDS","VISTA",<study uid>)=""
  1. ; ^TMP("MAG",$J,"UIDS","VISTA",<study uid>,0)=<series count>
  1. ; ^TMP("MAG",$J,"UIDS","VISTA",<study uid>,<series uid>)=""
  1. ; ^TMP("MAG",$J,"UIDS","VISTA",<study uid>,<series uid>,0)=<sop instance count>
  1. ; ^TMP("MAG",$J,"UIDS","VISTA",<study uid>,<series uid>,<sop instance uid>)=""
  1. ;
  1. ; This is done prior to invoking COMPARE.
  1. ;
  1. ; The same hierarchy is used for PACS UIDs in Step 3.
  1. ;
  1. ;
  1. ; Second, get study info from PACS.
  1. ; Issue a PACS Study Root Study Level query (C-FIND) using the
  1. ; the Study Instance UID (0020,000D) as the key.
  1. ; Request the following return data attributes:
  1. ; a. Number of Study Related Series (0020,1206)
  1. ; b. Number of Study Related Instances (0020,1208)
  1. ; c. Series Instance UID(s) (0020,000E)
  1. ;
  1. ; If the study cannot be found on the PACS, then STOP.
  1. ;
  1. ;
  1. ; Third, get series info from PACS.
  1. ; a. Issue a PACS Study Root Series Level query (C-FIND) using the Study
  1. ; Instance UID (0020,000D) as the key. Request the following return
  1. ; data attributes:
  1. ; i. Series Instance UID (0020,000E)
  1. ; ii. Number of Series Related Instances (0020,1209)
  1. ;
  1. ;
  1. ; Fourth, compare series counts.
  1. ; If the Number of Study Related Series (0020,1206) is greater
  1. ; than the number of Series Instance UIDs for the study on VistA,
  1. ; then compare the PACS retrieved Series Instance UIDs against those on VistA.
  1. ; For each missing VistA Series Instance UID, do the following four steps:
  1. ; a. Use the Study Instance UID (0020,000D) and PACS retrieved Series
  1. ; Instance UID (0020,000E) to issue a Series Level retrieve (C-MOVE)
  1. ; to copy all the series' DICOM objects from the PACS to VistA.
  1. ; b. Use the Study Instance UID (0020,000D) and PACS retrieved Series
  1. ; Instance UID (0020,000E) to issue an Image Level query (C-FIND)
  1. ; to obtain all the series' SOP Instance UIDs (0008,0018).
  1. ; c. Delete the study and corresponding images counts from the
  1. ;
  1. ;
  1. ; Fifth, compare image counts.
  1. ; If the Number of Study Related Instances (0020,1208) is
  1. ; greater than the number of SOP Instances for the study on VistA,
  1. ; then for each Series Instance UID, if the number of Series Related Instances
  1. ; (0020,1209) is greater than the number of SOP Instances for that series
  1. ; on VistA, do the following steps:
  1. ; a. Use the Study Instance UID and Series Instance UID to issue a
  1. ; PACS Study Root Image Level query (C-FIND) to obtain the list
  1. ; of SOP Instance UIDs (0008,0018) for the series .
  1. ; b. Compare the SOP Instance UIDs (0008,0018) from the PACS against
  1. ; those on VistA. Make a list of missing SOP Instances.
  1. ; c. Use the Study Instance UID (0020,000D), Series Instance UID
  1. ; (0020,000E), and the list of missing SOP Instances (0008,0018) to
  1. ; issue a Study Root Image Level retrieve (C-MOVE) to copy the missing
  1. ; SOP Instances from the PACS to VistA.
  1. ;
  1. ;
  1. ;
  1. QUERY(PACSSTUDYUID,SERIESCOUNT,IMAGECOUNT) ; check PACS to see if some of the images need to be retrieved
  1. N ERROR
  1. N NSTUDYRI ; -- number of study related instances
  1. N NSTUDYRS ; -- number of study related series
  1. N SERIESUID ;-- series instance uid
  1. N SOPICOUNT ; - number of sop instances on VistA
  1. N STUDYCOUNT ;- number of study instances on VistA
  1. N I
  1. ;
  1. S (I,IMAGECOUNT,SERIESCOUNT)=0,ERROR=""
  1. ;
  1. ; check to see if there is a Study UID available to query PACS
  1. I $G(PACSSTUDYUID(1))="" Q "-1,No PACS Study UID" ; nope
  1. ;
  1. ; ; Query PACS at study level and get series image counts
  1. F S I=$O(PACSSTUDYUID(I)) Q:I="" D
  1. . S ERROR=$$COUNTS(PACSSTUDYUID(I),.SERIESCOUNT,.IMAGECOUNT)
  1. . Q
  1. Q ERROR
  1. ;
  1. COUNTS(PACSSTUDYUID,SERIESCOUNT,IMAGECOUNT) ; get series and image counts from PACS
  1. N ERROR
  1. S ERROR=$$STUDY^MAGDSTAD(PACSSTUDYUID)
  1. I ERROR<0 Q "-2,No such Study Instance UID "_PACSSTUDYUID_" found on PACS"
  1. ;
  1. ; get number of study related series and study related sop instances
  1. S NSTUDYRS=$G(^XTMP(MAGXTMP,HOSTNAME,$J,QRSTACK,"STUDY",1,1,"NSTUDYRS"))
  1. S PACS(PACSSTUDYUID,"SERIES COUNT")=NSTUDYRS
  1. S SERIESCOUNT=SERIESCOUNT+NSTUDYRS ; count of all series instance uids
  1. S NSTUDYRI=$G(^XTMP(MAGXTMP,HOSTNAME,$J,QRSTACK,"STUDY",1,1,"NSTUDYRI"))
  1. S PACS(PACSSTUDYUID,"IMAGE COUNT")=NSTUDYRI
  1. S IMAGECOUNT=IMAGECOUNT+NSTUDYRI ; count of sop instance uids, for all series
  1. Q 0
  1. ;
  1. ;
  1. ;
  1. RETRIEVE(PACSSTUDYUID) ; primary retrieve capability for studies with some missing images
  1. N ERROR,I
  1. N NSERIESRI ; ------ number of series related images
  1. N PACSIMAGECOUNT ; - total number of PACS images
  1. N PACSSERIESCOUNT ; number of PACS series
  1. N PACSSERIESUID ; -- PACS series instance uids
  1. N SERIESUID ; ------ variable for VistA series uid
  1. N VISTAIMAGECOUNT ; total number of VistA images
  1. N VISTASERIESCOUNT ; number of VistA series
  1. N VISTASERIESUID ; - VistA series instance uid
  1. ;
  1. S ERROR=0
  1. ;
  1. ; Query PACS at series level and get series UIDs and series image counts
  1. S I=0 F S I=$O(PACSSTUDYUID(I)) Q:I="" D
  1. . D GETSERIU(PACSSTUDYUID(I))
  1. . Q
  1. ;
  1. ; get series UIDs and series image counts from VistA
  1. S VISTASTUDYUID=0
  1. F S VISTASTUDYUID=$O(^TMP("MAG",$J,"UIDS","VISTA",VISTASTUDYUID)) Q:VISTASTUDYUID="" D
  1. . S SERIESUID=0
  1. . F S SERIESUID=$O(^TMP("MAG",$J,"UIDS","VISTA",VISTASTUDYUID,SERIESUID)) Q:SERIESUID="" D
  1. . . S NSERIESRI=$G(^TMP("MAG",$J,"UIDS","VISTA",VISTASTUDYUID,SERIESUID,0))
  1. . . S ^("IMAGE COUNT")=$G(^TMP("MAG",$J,"UIDS","VISTA SERIES UID",SERIESUID,"IMAGE COUNT"))+NSERIESRI
  1. . . Q
  1. . Q
  1. ;
  1. ; get actual number of series and image counts
  1. S PACSSERIESUID="",(PACSIMAGECOUNT,PACSSERIESCOUNT)=0
  1. F S PACSSERIESUID=$O(^TMP("MAG",$J,"UIDS","PACS SERIES UID",PACSSERIESUID)) Q:PACSSERIESUID="" D
  1. . S PACSSERIESCOUNT=PACSSERIESCOUNT+1
  1. . S PACSIMAGECOUNT=PACSIMAGECOUNT+^TMP("MAG",$J,"UIDS","PACS SERIES UID",PACSSERIESUID,"IMAGE COUNT")
  1. . Q
  1. S VISTASERIESUID=0,(VISTAIMAGECOUNT,VISTASERIESCOUNT)=0
  1. F S VISTASERIESUID=$O(^TMP("MAG",$J,"UIDS","VISTA SERIES UID",VISTASERIESUID)) Q:VISTASERIESUID="" D
  1. . S VISTASERIESCOUNT=VISTASERIESCOUNT+1 ; $G added in next line in P305 - PMK 11/22/2021
  1. . S VISTAIMAGECOUNT=VISTAIMAGECOUNT+$G(^TMP("MAG",$J,"UIDS","VISTA SERIES UID",VISTASERIESUID,"IMAGE COUNT"))
  1. . Q
  1. ;
  1. ;
  1. I 'PACSIMAGECOUNT D Q 0 ; no images on PACS
  1. . D QRSTATUS^MAGDSTAA("No DICOM objects on PACS to retrieve")
  1. . Q
  1. ;
  1. ; check for additional images on VistA - suggested by Lisa Hulslander, Philips
  1. I VISTASERIESCOUNT>PACSSERIESCOUNT,VISTAIMAGECOUNT>PACSIMAGECOUNT D Q 0
  1. . D QRSTATUS^MAGDSTAA("VistA has more series and images than PACS")
  1. . Q
  1. ;
  1. I VISTASERIESCOUNT=PACSSERIESCOUNT,VISTAIMAGECOUNT=PACSIMAGECOUNT D Q 0
  1. . D QRSTATUS^MAGDSTAA("VistA has same series and images as PACS")
  1. . Q
  1. ;
  1. I VISTASERIESCOUNT>PACSSERIESCOUNT,VISTAIMAGECOUNT=PACSIMAGECOUNT D Q 0
  1. . D QRSTATUS^MAGDSTAA("VistA has more series but same images as PACS")
  1. . Q
  1. ;
  1. ; this test was added in P305 - PMK 11/09/2021
  1. I VISTASERIESCOUNT=PACSSERIESCOUNT,VISTAIMAGECOUNT>PACSIMAGECOUNT D Q 0
  1. . D QRSTATUS^MAGDSTAA("VistA has same series but more images than PACS")
  1. . Q
  1. ;
  1. I VISTAIMAGECOUNT>PACSIMAGECOUNT D Q 0 ; QUIT added in P305 - PMK 11/09/2021
  1. . D QRSTATUS^MAGDSTAA("VistA has more images than PACS")
  1. . Q
  1. ;
  1. ; this test was added in P305 - PMK 11/09/2021
  1. I VISTAIMAGECOUNT=PACSIMAGECOUNT D Q 0
  1. . D QRSTATUS^MAGDSTAA("VistA has the same images as PACS")
  1. . Q
  1. ;
  1. ; retrieve any missing images
  1. ;
  1. I 'VISTAIMAGECOUNT D ; no images on VistA
  1. . I PACSIMAGECOUNT>1 D
  1. . . D QRSTATUS^MAGDSTAA("Retrieving entire study of "_PACS("IMAGE COUNT")_" images from PACS")
  1. . . Q
  1. . E D
  1. . . D QRSTATUS^MAGDSTAA("Retrieving a study with one image from PACS")
  1. . . Q
  1. . D STTINC^MAGDSTAA("PACS STUDY LEVEL RETRIEVES",1)
  1. . D STTINC^MAGDSTAA("PACS IMAGES RETRIEVED",PACSIMAGECOUNT)
  1. . S ERROR=$$MOVEALL^MAGDSTAB() ; move all study instance uids for this study
  1. . Q
  1. E D ; retrieve missing series or missing images within existing series
  1. . ; compare the number of images in each PACS series against those in VistA
  1. . S PACSSERIESUID=""
  1. . F S PACSSERIESUID=$O(^TMP("MAG",$J,"UIDS","PACS SERIES UID",PACSSERIESUID)) Q:PACSSERIESUID="" D
  1. . . ; if the series does not exist on VistA, retrieve the whole series
  1. . . I '$D(^TMP("MAG",$J,"UIDS","VISTA SERIES UID",PACSSERIESUID)) D
  1. . . . D SERIES^MAGDSTAE(PACSSERIESUID) ; retrieve the series
  1. . . . Q
  1. . . ;
  1. . . ; if series image count on VistA is less than on PACS, retrieve missing images
  1. . . E I ^TMP("MAG",$J,"UIDS","VISTA SERIES UID",PACSSERIESUID,"IMAGE COUNT")<^TMP("MAG",$J,"UIDS","PACS SERIES UID",PACSSERIESUID,"IMAGE COUNT") D
  1. . . . M VISTASTUDYUID=^TMP("MAG",$J,"UIDS","VISTA SERIES UID",PACSSERIESUID,"STUDY UID")
  1. . . . D GETIMAGE^MAGDSTAF(PACSSERIESUID)
  1. . . . Q
  1. . . Q
  1. . Q
  1. ;
  1. Q ERROR
  1. ;
  1. GETSERIU(PACSSTUDYUID) ; query for Series Instance UIDs for the study
  1. N COUNT,I,NSERIESRI,PACSSERIESUID
  1. K ^TMP("MAG",$J,"Q/R QUERY")
  1. S ^TMP("MAG",$J,"Q/R QUERY",QRSTACK,"STUDY INSTANCE UID(0001)")=PACSSTUDYUID
  1. S ^TMP("MAG",$J,"Q/R QUERY",QRSTACK,"SERIES INSTANCE UID")=""
  1. S ^TMP("MAG",$J,"Q/R QUERY",QRSTACK,"QUERY USER APPLICATION")=$$QRSCP^MAGDSTA8
  1. S ^TMP("MAG",$J,"Q/R QUERY",QRSTACK,"QUERY LEVEL")="SERIES"
  1. S ^TMP("MAG",$J,"Q/R QUERY",QRSTACK,"ROOT")="STUDY"
  1. D SOPUIDQ^MAGDSTV1 ; C-FIND
  1. S COUNT=$G(^XTMP(MAGXTMP,HOSTNAME,$J,QRSTACK,"SERIES",1,1))
  1. F I=1:1:COUNT D
  1. . S PACSSERIESUID=$G(^XTMP(MAGXTMP,HOSTNAME,$J,QRSTACK,"SERIES",1,1,I,"SERIESUID"))
  1. . S NSERIESRI=$G(^XTMP(MAGXTMP,HOSTNAME,$J,QRSTACK,"SERIES",1,1,I,"NSERIESRI"))
  1. . ; Like VistA, PACS may have same series under different study instance uids, so add image count
  1. . S ^("IMAGE COUNT")=$G(^TMP("MAG",$J,"UIDS","PACS SERIES UID",PACSSERIESUID,"IMAGE COUNT"))+NSERIESRI
  1. . S ^TMP("MAG",$J,"UIDS","PACS SERIES UID",PACSSERIESUID,"STUDY UID",PACSSTUDYUID)=""
  1. . Q
  1. Q