MAGDSTAE ;WOIFO/PMK - Q/R Retrieve of DICOM images from PACS to VistA ; Jan 04, 2021@11:56:22
 ;;3.0;IMAGING;**231**;5-May-2007;Build 9
 ;; Per VHA Directive 2004-038, this routine should not be modified.
 ;; +---------------------------------------------------------------+
 ;; | Property of the US Government.                                |
 ;; | No permission to copy or redistribute this software is given. |
 ;; | Use of unreleased versions of this software requires the user |
 ;; | to execute a written test agreement with the VistA Imaging    |
 ;; | Development Office of the Department of Veterans Affairs,     |
 ;; | telephone (301) 734-0100.                                     |
 ;; | The Food and Drug Administration classifies this software as  |
 ;; | a medical device.  As such, it may not be changed in any way. |
 ;; | Modifications to this software may result in an adulterated   |
 ;; | medical device under 21CFR820, the use of which is considered |
 ;; | to be a violation of US Federal Statutes.                     |
 ;; +---------------------------------------------------------------+
 ;;
 Q
 ;
 ; Third Step - Get missing series from PACS.
 ; 
 ; VistA appears to be missing one or more series. Get them from the PACS.
 ; 
 ; Perform the following steps to get all the missing series:
 ;   a.  Issue a PACS Study Root Series Level query (C-FIND) using the Study
 ;       Instance UID (0020,000D) as the key.  Request the following return
 ;       data attributes:
 ;         i.   Series Instance UID (0020,000E)
 ;        ii.   Number of Series Related Instances (0020,1209)
 ;   b.  Compare the PACS retrieved Series Instance UIDs against those on VistA.
 ;       For each missing Series Instance UID, do the following four steps:
 ;         i.  Use the Study Instance UID (0020,000D) and PACS retrieved Series
 ;             Instance UID (0020,000E) to issue a Series Level retrieve (C-MOVE)
 ;             to copy all the series' DICOM objects from the PACS to VistA.
 ;        ii.  Use the Study Instance UID (0020,000D) and PACS retrieved Series
 ;             Instance UID (0020,000E) to issue an Image Level query (C-FIND)
 ;             to obtain all the series' SOP Instance UIDs (0008,0018).
 ;       iii.  Add the retrieved Series Instance UID (0020,000E) and the series'
 ;             SOP Instance UIDs (0008,0018) to the hierarchy mentioned above.
 ;        iv.  Update the study's SOP Instance count for VistA.
 ; get the series uid(s) for this study
 ;
 ; Note: PACS STUDYUID is a multiple, since PACS may have more than on Study UID per study.
 ;       SERIES is called for each PACSSERIESUID for the study.
 ;
SERIES(PACSSERIESUID) ; retrieve the series
 N I,NSERIESRI,PACSSTUDYUID,RETURN
 N SOPUID ;----- sop instance uid
 S RETURN=0
 ; get all of the series sop instances and send them to VistA
 S NSERIESRI=^TMP("MAG",$J,"UIDS","PACS SERIES UID",PACSSERIESUID,"IMAGE COUNT")
 I NSERIESRI>1 D
 . D QRSTATUS^MAGDSTAA("Retrieving a series with "_NSERIESRI_" images from PACS")
 . Q
 E  D
 . D QRSTATUS^MAGDSTAA("Retrieving a series with one image from PACS")
 . Q
 S PACSSTUDYUID=$O(^TMP("MAG",$J,"UIDS","PACS SERIES UID",PACSSERIESUID,"STUDY UID",""))
 I PACSSTUDYUID="" Q  ; there should always be a single Study UID for each Series UID
 D STTINC^MAGDSTAA("PACS SERIES LEVEL RETRIEVES",1)
 D STTINC^MAGDSTAA("PACS IMAGES RETRIEVED",NSERIESRI)
 D GETIMAGE(PACSSTUDYUID,SERIESUID) ; get one series of images
 Q
 ;
GETIMAGE(PACSSTUDYUID,SERIESUID) ; retrieve one series SOP instances
 N I,SOPUID
 K ^TMP("MAG",$J,"Q/R QUERY")
 S ^TMP("MAG",$J,"Q/R QUERY",QRSTACK,"STUDY INSTANCE UID(0001)")=PACSSTUDYUID
 S ^TMP("MAG",$J,"Q/R QUERY",QRSTACK,"SERIES INSTANCE UID(0001)")=PACSSERIESUID
 S ^TMP("MAG",$J,"Q/R QUERY",QRSTACK,"QUERY USER APPLICATION")=$$QRSCP^MAGDSTA8
 S ^TMP("MAG",$J,"Q/R QUERY",QRSTACK,"RETRIEVE LEVEL")="SERIES"
 S ^TMP("MAG",$J,"Q/R QUERY",QRSTACK,"ROOT")="STUDY"
 D SOPUIDR^MAGDSTV1 ; C-MOVE
 Q
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HMAGDSTAE   4035     printed  Sep 23, 2025@19:38:02                                                                                                                                                                                                    Page 2
MAGDSTAE  ;WOIFO/PMK - Q/R Retrieve of DICOM images from PACS to VistA ; Jan 04, 2021@11:56:22
 +1       ;;3.0;IMAGING;**231**;5-May-2007;Build 9
 +2       ;; Per VHA Directive 2004-038, this routine should not be modified.
 +3       ;; +---------------------------------------------------------------+
 +4       ;; | Property of the US Government.                                |
 +5       ;; | No permission to copy or redistribute this software is given. |
 +6       ;; | Use of unreleased versions of this software requires the user |
 +7       ;; | to execute a written test agreement with the VistA Imaging    |
 +8       ;; | Development Office of the Department of Veterans Affairs,     |
 +9       ;; | telephone (301) 734-0100.                                     |
 +10      ;; | The Food and Drug Administration classifies this software as  |
 +11      ;; | a medical device.  As such, it may not be changed in any way. |
 +12      ;; | Modifications to this software may result in an adulterated   |
 +13      ;; | medical device under 21CFR820, the use of which is considered |
 +14      ;; | to be a violation of US Federal Statutes.                     |
 +15      ;; +---------------------------------------------------------------+
 +16      ;;
 +17       QUIT 
 +18      ;
 +19      ; Third Step - Get missing series from PACS.
 +20      ; 
 +21      ; VistA appears to be missing one or more series. Get them from the PACS.
 +22      ; 
 +23      ; Perform the following steps to get all the missing series:
 +24      ;   a.  Issue a PACS Study Root Series Level query (C-FIND) using the Study
 +25      ;       Instance UID (0020,000D) as the key.  Request the following return
 +26      ;       data attributes:
 +27      ;         i.   Series Instance UID (0020,000E)
 +28      ;        ii.   Number of Series Related Instances (0020,1209)
 +29      ;   b.  Compare the PACS retrieved Series Instance UIDs against those on VistA.
 +30      ;       For each missing Series Instance UID, do the following four steps:
 +31      ;         i.  Use the Study Instance UID (0020,000D) and PACS retrieved Series
 +32      ;             Instance UID (0020,000E) to issue a Series Level retrieve (C-MOVE)
 +33      ;             to copy all the series' DICOM objects from the PACS to VistA.
 +34      ;        ii.  Use the Study Instance UID (0020,000D) and PACS retrieved Series
 +35      ;             Instance UID (0020,000E) to issue an Image Level query (C-FIND)
 +36      ;             to obtain all the series' SOP Instance UIDs (0008,0018).
 +37      ;       iii.  Add the retrieved Series Instance UID (0020,000E) and the series'
 +38      ;             SOP Instance UIDs (0008,0018) to the hierarchy mentioned above.
 +39      ;        iv.  Update the study's SOP Instance count for VistA.
 +40      ; get the series uid(s) for this study
 +41      ;
 +42      ; Note: PACS STUDYUID is a multiple, since PACS may have more than on Study UID per study.
 +43      ;       SERIES is called for each PACSSERIESUID for the study.
 +44      ;
SERIES(PACSSERIESUID) ; retrieve the series
 +1        NEW I,NSERIESRI,PACSSTUDYUID,RETURN
 +2       ;----- sop instance uid
           NEW SOPUID
 +3        SET RETURN=0
 +4       ; get all of the series sop instances and send them to VistA
 +5        SET NSERIESRI=^TMP("MAG",$JOB,"UIDS","PACS SERIES UID",PACSSERIESUID,"IMAGE COUNT")
 +6        IF NSERIESRI>1
               Begin DoDot:1
 +7                DO QRSTATUS^MAGDSTAA("Retrieving a series with "_NSERIESRI_" images from PACS")
 +8                QUIT 
               End DoDot:1
 +9       IF '$TEST
               Begin DoDot:1
 +10               DO QRSTATUS^MAGDSTAA("Retrieving a series with one image from PACS")
 +11               QUIT 
               End DoDot:1
 +12       SET PACSSTUDYUID=$ORDER(^TMP("MAG",$JOB,"UIDS","PACS SERIES UID",PACSSERIESUID,"STUDY UID",""))
 +13      ; there should always be a single Study UID for each Series UID
           IF PACSSTUDYUID=""
               QUIT 
 +14       DO STTINC^MAGDSTAA("PACS SERIES LEVEL RETRIEVES",1)
 +15       DO STTINC^MAGDSTAA("PACS IMAGES RETRIEVED",NSERIESRI)
 +16      ; get one series of images
           DO GETIMAGE(PACSSTUDYUID,SERIESUID)
 +17       QUIT 
 +18      ;
GETIMAGE(PACSSTUDYUID,SERIESUID) ; retrieve one series SOP instances
 +1        NEW I,SOPUID
 +2        KILL ^TMP("MAG",$JOB,"Q/R QUERY")
 +3        SET ^TMP("MAG",$JOB,"Q/R QUERY",QRSTACK,"STUDY INSTANCE UID(0001)")=PACSSTUDYUID
 +4        SET ^TMP("MAG",$JOB,"Q/R QUERY",QRSTACK,"SERIES INSTANCE UID(0001)")=PACSSERIESUID
 +5        SET ^TMP("MAG",$JOB,"Q/R QUERY",QRSTACK,"QUERY USER APPLICATION")=$$QRSCP^MAGDSTA8
 +6        SET ^TMP("MAG",$JOB,"Q/R QUERY",QRSTACK,"RETRIEVE LEVEL")="SERIES"
 +7        SET ^TMP("MAG",$JOB,"Q/R QUERY",QRSTACK,"ROOT")="STUDY"
 +8       ; C-MOVE
           DO SOPUIDR^MAGDSTV1
 +9        QUIT