MAGDSTAC ;WOIFO/PMK - Q/R Retrieve of DICOM images from PACS to VistA ; Feb 15, 2022@10:52:31
 ;;3.0;IMAGING;**231,305**;Mar 19, 2002;Build 3
 ;; 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
 ;
 ; COMPARE is called by MAGDSTAA when the study's images are already on VistA.
 ; 
 ; It is used to retrieve possibly some missing SOP Instances from PACS.
 ; 
 ; This is a multi-step process.
 ;
 ; 
 ; First, get VistA study, series, and image UID info.
 ; The Study Instance UID (0020,000D), Series Instance UIDs (0020,000E),
 ; and SOP Instance UIDs (0008,0018) are retrieved from VistA from either
 ; the legacy Image file (#2005) or the new SOP Class Database (#2005.6x).
 ; 
 ; Count the number of Series Instance UIDs and number of SOP Instances.
 ; 
 ; Store the data in the following hierarchy:
 ; 
 ; ^TMP("MAG",$J,"UIDS","VISTA",0)=<study count>
 ; ^TMP("MAG",$J,"UIDS","VISTA",<study uid>)=""
 ; ^TMP("MAG",$J,"UIDS","VISTA",<study uid>,0)=<series count>
 ; ^TMP("MAG",$J,"UIDS","VISTA",<study uid>,<series uid>)=""
 ; ^TMP("MAG",$J,"UIDS","VISTA",<study uid>,<series uid>,0)=<sop instance count>
 ; ^TMP("MAG",$J,"UIDS","VISTA",<study uid>,<series uid>,<sop instance uid>)=""
 ;
 ; This is done prior to invoking COMPARE.
 ; 
 ; The same hierarchy is used for PACS UIDs in Step 3.
 ;   
 ;
 ; Second, get study info from PACS.
 ; Issue a PACS Study Root Study Level query (C-FIND) using the
 ; the Study Instance UID (0020,000D) as the key.
 ; Request the following return data attributes:
 ;   a. Number of Study Related Series (0020,1206)
 ;   b. Number of Study Related Instances (0020,1208)
 ;   c. Series Instance UID(s) (0020,000E)
 ;
 ; If the study cannot be found on the PACS, then STOP.
 ;
 ; 
 ; Third, get series info from PACS.
 ;   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)
 ;
 ;
 ; Fourth, compare series counts.
 ; If the Number of Study Related Series (0020,1206) is greater
 ; than the number of Series Instance UIDs for the study on VistA,
 ; then compare the PACS retrieved Series Instance UIDs against those on VistA.
 ; For each missing VistA Series Instance UID, do the following four steps:
 ;   a.  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.
 ;   b.  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).
 ;   c.  Delete the study and corresponding images counts from the 
 ;
 ;
 ; Fifth, compare image counts.
 ; If the Number of Study Related Instances (0020,1208) is
 ; greater than the number of SOP Instances for the study on VistA,
 ; then for each Series Instance UID, if the number of Series Related Instances
 ; (0020,1209) is greater than the number of SOP Instances for that series
 ; on VistA, do the following steps:
 ;   a.  Use the Study Instance UID and Series Instance UID to issue a
 ;       PACS Study Root Image Level query (C-FIND) to obtain the list
 ;       of SOP Instance UIDs (0008,0018) for the series . 
 ;   b.  Compare the SOP Instance UIDs (0008,0018) from the PACS against
 ;       those on VistA.  Make a list of missing SOP Instances.
 ;   c.  Use the Study Instance UID (0020,000D), Series Instance UID
 ;       (0020,000E), and the list of missing SOP Instances (0008,0018) to
 ;       issue a Study Root Image Level retrieve (C-MOVE) to copy the missing
 ;       SOP Instances from the PACS to VistA.
 ;
 ;
 ;
QUERY(PACSSTUDYUID,SERIESCOUNT,IMAGECOUNT) ; check PACS to see if some of the images need to be retrieved
 N ERROR
 N NSTUDYRI ; -- number of study related instances
 N NSTUDYRS ; -- number of study related series
 N SERIESUID ;-- series instance uid
 N SOPICOUNT ; - number of sop instances on VistA
 N STUDYCOUNT ;- number of study instances on VistA
 N I
 ;
 S (I,IMAGECOUNT,SERIESCOUNT)=0,ERROR=""
 ;
 ; check to see if there is a Study UID available to query PACS
 I $G(PACSSTUDYUID(1))="" Q "-1,No PACS Study UID" ; nope
 ;
 ; ; Query PACS at study level and get series image counts
 F  S I=$O(PACSSTUDYUID(I)) Q:I=""  D
 . S ERROR=$$COUNTS(PACSSTUDYUID(I),.SERIESCOUNT,.IMAGECOUNT)
 . Q
 Q ERROR
 ;
COUNTS(PACSSTUDYUID,SERIESCOUNT,IMAGECOUNT) ; get series and image counts from PACS
 N ERROR
 S ERROR=$$STUDY^MAGDSTAD(PACSSTUDYUID)
 I ERROR<0 Q "-2,No such Study Instance UID "_PACSSTUDYUID_" found on PACS"
 ;
 ; get number of study related series and study related sop instances
 S NSTUDYRS=$G(^XTMP(MAGXTMP,HOSTNAME,$J,QRSTACK,"STUDY",1,1,"NSTUDYRS"))
 S PACS(PACSSTUDYUID,"SERIES COUNT")=NSTUDYRS
 S SERIESCOUNT=SERIESCOUNT+NSTUDYRS ; count of all series instance uids
 S NSTUDYRI=$G(^XTMP(MAGXTMP,HOSTNAME,$J,QRSTACK,"STUDY",1,1,"NSTUDYRI"))
 S PACS(PACSSTUDYUID,"IMAGE COUNT")=NSTUDYRI
 S IMAGECOUNT=IMAGECOUNT+NSTUDYRI ; count of sop instance uids, for all series
 Q 0
 ;
 ;
 ;
RETRIEVE(PACSSTUDYUID) ; primary retrieve capability for studies with some missing images
 N ERROR,I
 N NSERIESRI ; ------ number of series related images
 N PACSIMAGECOUNT ; - total number of PACS images
 N PACSSERIESCOUNT ;  number of PACS series
 N PACSSERIESUID ; -- PACS series instance uids
 N SERIESUID ; ------ variable for VistA series uid
 N VISTAIMAGECOUNT ;  total number of VistA images
 N VISTASERIESCOUNT ; number of VistA series
 N VISTASERIESUID ; - VistA series instance uid
 ;
 S ERROR=0
 ;
 ; Query PACS at series level and get series UIDs and series image counts
 S I=0 F  S I=$O(PACSSTUDYUID(I)) Q:I=""  D
 . D GETSERIU(PACSSTUDYUID(I))
 . Q
 ;
 ; get series UIDs and series image counts from VistA
 S VISTASTUDYUID=0
 F  S VISTASTUDYUID=$O(^TMP("MAG",$J,"UIDS","VISTA",VISTASTUDYUID)) Q:VISTASTUDYUID=""  D
 . S SERIESUID=0
 . F  S SERIESUID=$O(^TMP("MAG",$J,"UIDS","VISTA",VISTASTUDYUID,SERIESUID)) Q:SERIESUID=""  D
 . . S NSERIESRI=$G(^TMP("MAG",$J,"UIDS","VISTA",VISTASTUDYUID,SERIESUID,0))
 . . S ^("IMAGE COUNT")=$G(^TMP("MAG",$J,"UIDS","VISTA SERIES UID",SERIESUID,"IMAGE COUNT"))+NSERIESRI
 . . Q
 . Q
 ;
 ; get actual number of series and image counts
 S PACSSERIESUID="",(PACSIMAGECOUNT,PACSSERIESCOUNT)=0
 F  S PACSSERIESUID=$O(^TMP("MAG",$J,"UIDS","PACS SERIES UID",PACSSERIESUID)) Q:PACSSERIESUID=""  D
 . S PACSSERIESCOUNT=PACSSERIESCOUNT+1
 . S PACSIMAGECOUNT=PACSIMAGECOUNT+^TMP("MAG",$J,"UIDS","PACS SERIES UID",PACSSERIESUID,"IMAGE COUNT")
 . Q
 S VISTASERIESUID=0,(VISTAIMAGECOUNT,VISTASERIESCOUNT)=0
 F  S VISTASERIESUID=$O(^TMP("MAG",$J,"UIDS","VISTA SERIES UID",VISTASERIESUID)) Q:VISTASERIESUID=""  D
 . S VISTASERIESCOUNT=VISTASERIESCOUNT+1 ; $G added in next line in P305 - PMK 11/22/2021
 . S VISTAIMAGECOUNT=VISTAIMAGECOUNT+$G(^TMP("MAG",$J,"UIDS","VISTA SERIES UID",VISTASERIESUID,"IMAGE COUNT"))
 . Q
 ;
 ;
 I 'PACSIMAGECOUNT D  Q 0 ; no images on PACS
 . D QRSTATUS^MAGDSTAA("No DICOM objects on PACS to retrieve")
 . Q
 ;
 ; check for additional images on VistA - suggested by Lisa Hulslander, Philips
 I VISTASERIESCOUNT>PACSSERIESCOUNT,VISTAIMAGECOUNT>PACSIMAGECOUNT D  Q 0
 . D QRSTATUS^MAGDSTAA("VistA has more series and images than PACS")
 . Q
 ;
 I VISTASERIESCOUNT=PACSSERIESCOUNT,VISTAIMAGECOUNT=PACSIMAGECOUNT D  Q 0
 . D QRSTATUS^MAGDSTAA("VistA has same series and images as PACS")
 . Q
 ;
 I VISTASERIESCOUNT>PACSSERIESCOUNT,VISTAIMAGECOUNT=PACSIMAGECOUNT D  Q 0
 . D QRSTATUS^MAGDSTAA("VistA has more series but same images as PACS")
 . Q
 ;
 ; this test was added in P305 - PMK 11/09/2021 
 I VISTASERIESCOUNT=PACSSERIESCOUNT,VISTAIMAGECOUNT>PACSIMAGECOUNT D  Q 0
 . D QRSTATUS^MAGDSTAA("VistA has same series but more images than PACS")
 . Q
 ;
 I VISTAIMAGECOUNT>PACSIMAGECOUNT D  Q 0 ; QUIT added in P305 - PMK 11/09/2021
 . D QRSTATUS^MAGDSTAA("VistA has more images than PACS")
 . Q
 ;
 ; this test was added in P305 - PMK 11/09/2021 
 I VISTAIMAGECOUNT=PACSIMAGECOUNT D  Q 0
 . D QRSTATUS^MAGDSTAA("VistA has the same images as PACS")
 . Q
 ;
 ; retrieve any missing images 
 ;
 I 'VISTAIMAGECOUNT D  ; no images on VistA
 . I PACSIMAGECOUNT>1 D
 . . D QRSTATUS^MAGDSTAA("Retrieving entire study of "_PACS("IMAGE COUNT")_" images from PACS")
 . . Q
 . E  D
 . . D QRSTATUS^MAGDSTAA("Retrieving a study with one image from PACS")
 . . Q
 . D STTINC^MAGDSTAA("PACS STUDY LEVEL RETRIEVES",1)
 . D STTINC^MAGDSTAA("PACS IMAGES RETRIEVED",PACSIMAGECOUNT)
 . S ERROR=$$MOVEALL^MAGDSTAB() ; move all study instance uids for this study
 . Q
 E  D  ; retrieve missing series or missing images within existing series
 . ; compare the number of images in each PACS series against those in VistA 
 . S PACSSERIESUID=""
 . F  S PACSSERIESUID=$O(^TMP("MAG",$J,"UIDS","PACS SERIES UID",PACSSERIESUID)) Q:PACSSERIESUID=""  D
 . . ; if the series does not exist on VistA, retrieve the whole series
 . . I '$D(^TMP("MAG",$J,"UIDS","VISTA SERIES UID",PACSSERIESUID)) D
 . . . D SERIES^MAGDSTAE(PACSSERIESUID) ; retrieve the series 
 . . . Q
 . . ;
 . . ; if series image count on VistA is less than on PACS, retrieve missing images
 . . E  I ^TMP("MAG",$J,"UIDS","VISTA SERIES UID",PACSSERIESUID,"IMAGE COUNT")<^TMP("MAG",$J,"UIDS","PACS SERIES UID",PACSSERIESUID,"IMAGE COUNT") D
 . . . M VISTASTUDYUID=^TMP("MAG",$J,"UIDS","VISTA SERIES UID",PACSSERIESUID,"STUDY UID")
 . . . D GETIMAGE^MAGDSTAF(PACSSERIESUID)
 . . . Q
 . . Q
 . Q
 ;
 Q ERROR
 ;
GETSERIU(PACSSTUDYUID) ; query for Series Instance UIDs for the study
 N COUNT,I,NSERIESRI,PACSSERIESUID
 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")=""
 S ^TMP("MAG",$J,"Q/R QUERY",QRSTACK,"QUERY USER APPLICATION")=$$QRSCP^MAGDSTA8
 S ^TMP("MAG",$J,"Q/R QUERY",QRSTACK,"QUERY LEVEL")="SERIES"
 S ^TMP("MAG",$J,"Q/R QUERY",QRSTACK,"ROOT")="STUDY"
 D SOPUIDQ^MAGDSTV1 ; C-FIND
 S COUNT=$G(^XTMP(MAGXTMP,HOSTNAME,$J,QRSTACK,"SERIES",1,1))
 F I=1:1:COUNT D
 . S PACSSERIESUID=$G(^XTMP(MAGXTMP,HOSTNAME,$J,QRSTACK,"SERIES",1,1,I,"SERIESUID"))
 . S NSERIESRI=$G(^XTMP(MAGXTMP,HOSTNAME,$J,QRSTACK,"SERIES",1,1,I,"NSERIESRI"))
 . ; Like VistA, PACS may have same series under different study instance uids, so add image count
 . S ^("IMAGE COUNT")=$G(^TMP("MAG",$J,"UIDS","PACS SERIES UID",PACSSERIESUID,"IMAGE COUNT"))+NSERIESRI
 . S ^TMP("MAG",$J,"UIDS","PACS SERIES UID",PACSSERIESUID,"STUDY UID",PACSSTUDYUID)=""
 . Q
 Q
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HMAGDSTAC   11872     printed  Sep 23, 2025@19:38                                                                                                                                                                                                      Page 2
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
 +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      ; COMPARE is called by MAGDSTAA when the study's images are already on VistA.
 +20      ; 
 +21      ; It is used to retrieve possibly some missing SOP Instances from PACS.
 +22      ; 
 +23      ; This is a multi-step process.
 +24      ;
 +25      ; 
 +26      ; First, get VistA study, series, and image UID info.
 +27      ; The Study Instance UID (0020,000D), Series Instance UIDs (0020,000E),
 +28      ; and SOP Instance UIDs (0008,0018) are retrieved from VistA from either
 +29      ; the legacy Image file (#2005) or the new SOP Class Database (#2005.6x).
 +30      ; 
 +31      ; Count the number of Series Instance UIDs and number of SOP Instances.
 +32      ; 
 +33      ; Store the data in the following hierarchy:
 +34      ; 
 +35      ; ^TMP("MAG",$J,"UIDS","VISTA",0)=<study count>
 +36      ; ^TMP("MAG",$J,"UIDS","VISTA",<study uid>)=""
 +37      ; ^TMP("MAG",$J,"UIDS","VISTA",<study uid>,0)=<series count>
 +38      ; ^TMP("MAG",$J,"UIDS","VISTA",<study uid>,<series uid>)=""
 +39      ; ^TMP("MAG",$J,"UIDS","VISTA",<study uid>,<series uid>,0)=<sop instance count>
 +40      ; ^TMP("MAG",$J,"UIDS","VISTA",<study uid>,<series uid>,<sop instance uid>)=""
 +41      ;
 +42      ; This is done prior to invoking COMPARE.
 +43      ; 
 +44      ; The same hierarchy is used for PACS UIDs in Step 3.
 +45      ;   
 +46      ;
 +47      ; Second, get study info from PACS.
 +48      ; Issue a PACS Study Root Study Level query (C-FIND) using the
 +49      ; the Study Instance UID (0020,000D) as the key.
 +50      ; Request the following return data attributes:
 +51      ;   a. Number of Study Related Series (0020,1206)
 +52      ;   b. Number of Study Related Instances (0020,1208)
 +53      ;   c. Series Instance UID(s) (0020,000E)
 +54      ;
 +55      ; If the study cannot be found on the PACS, then STOP.
 +56      ;
 +57      ; 
 +58      ; Third, get series info from PACS.
 +59      ;   a.  Issue a PACS Study Root Series Level query (C-FIND) using the Study
 +60      ;       Instance UID (0020,000D) as the key.  Request the following return
 +61      ;       data attributes:
 +62      ;         i.   Series Instance UID (0020,000E)
 +63      ;        ii.   Number of Series Related Instances (0020,1209)
 +64      ;
 +65      ;
 +66      ; Fourth, compare series counts.
 +67      ; If the Number of Study Related Series (0020,1206) is greater
 +68      ; than the number of Series Instance UIDs for the study on VistA,
 +69      ; then compare the PACS retrieved Series Instance UIDs against those on VistA.
 +70      ; For each missing VistA Series Instance UID, do the following four steps:
 +71      ;   a.  Use the Study Instance UID (0020,000D) and PACS retrieved Series
 +72      ;       Instance UID (0020,000E) to issue a Series Level retrieve (C-MOVE)
 +73      ;       to copy all the series' DICOM objects from the PACS to VistA.
 +74      ;   b.  Use the Study Instance UID (0020,000D) and PACS retrieved Series
 +75      ;       Instance UID (0020,000E) to issue an Image Level query (C-FIND)
 +76      ;       to obtain all the series' SOP Instance UIDs (0008,0018).
 +77      ;   c.  Delete the study and corresponding images counts from the 
 +78      ;
 +79      ;
 +80      ; Fifth, compare image counts.
 +81      ; If the Number of Study Related Instances (0020,1208) is
 +82      ; greater than the number of SOP Instances for the study on VistA,
 +83      ; then for each Series Instance UID, if the number of Series Related Instances
 +84      ; (0020,1209) is greater than the number of SOP Instances for that series
 +85      ; on VistA, do the following steps:
 +86      ;   a.  Use the Study Instance UID and Series Instance UID to issue a
 +87      ;       PACS Study Root Image Level query (C-FIND) to obtain the list
 +88      ;       of SOP Instance UIDs (0008,0018) for the series . 
 +89      ;   b.  Compare the SOP Instance UIDs (0008,0018) from the PACS against
 +90      ;       those on VistA.  Make a list of missing SOP Instances.
 +91      ;   c.  Use the Study Instance UID (0020,000D), Series Instance UID
 +92      ;       (0020,000E), and the list of missing SOP Instances (0008,0018) to
 +93      ;       issue a Study Root Image Level retrieve (C-MOVE) to copy the missing
 +94      ;       SOP Instances from the PACS to VistA.
 +95      ;
 +96      ;
 +97      ;
QUERY(PACSSTUDYUID,SERIESCOUNT,IMAGECOUNT) ; check PACS to see if some of the images need to be retrieved
 +1        NEW ERROR
 +2       ; -- number of study related instances
           NEW NSTUDYRI
 +3       ; -- number of study related series
           NEW NSTUDYRS
 +4       ;-- series instance uid
           NEW SERIESUID
 +5       ; - number of sop instances on VistA
           NEW SOPICOUNT
 +6       ;- number of study instances on VistA
           NEW STUDYCOUNT
 +7        NEW I
 +8       ;
 +9        SET (I,IMAGECOUNT,SERIESCOUNT)=0
           SET ERROR=""
 +10      ;
 +11      ; check to see if there is a Study UID available to query PACS
 +12      ; nope
           IF $GET(PACSSTUDYUID(1))=""
               QUIT "-1,No PACS Study UID"
 +13      ;
 +14      ; ; Query PACS at study level and get series image counts
 +15       FOR 
               SET I=$ORDER(PACSSTUDYUID(I))
               if I=""
                   QUIT 
               Begin DoDot:1
 +16               SET ERROR=$$COUNTS(PACSSTUDYUID(I),.SERIESCOUNT,.IMAGECOUNT)
 +17               QUIT 
               End DoDot:1
 +18       QUIT ERROR
 +19      ;
COUNTS(PACSSTUDYUID,SERIESCOUNT,IMAGECOUNT) ; get series and image counts from PACS
 +1        NEW ERROR
 +2        SET ERROR=$$STUDY^MAGDSTAD(PACSSTUDYUID)
 +3        IF ERROR<0
               QUIT "-2,No such Study Instance UID "_PACSSTUDYUID_" found on PACS"
 +4       ;
 +5       ; get number of study related series and study related sop instances
 +6        SET NSTUDYRS=$GET(^XTMP(MAGXTMP,HOSTNAME,$JOB,QRSTACK,"STUDY",1,1,"NSTUDYRS"))
 +7        SET PACS(PACSSTUDYUID,"SERIES COUNT")=NSTUDYRS
 +8       ; count of all series instance uids
           SET SERIESCOUNT=SERIESCOUNT+NSTUDYRS
 +9        SET NSTUDYRI=$GET(^XTMP(MAGXTMP,HOSTNAME,$JOB,QRSTACK,"STUDY",1,1,"NSTUDYRI"))
 +10       SET PACS(PACSSTUDYUID,"IMAGE COUNT")=NSTUDYRI
 +11      ; count of sop instance uids, for all series
           SET IMAGECOUNT=IMAGECOUNT+NSTUDYRI
 +12       QUIT 0
 +13      ;
 +14      ;
 +15      ;
RETRIEVE(PACSSTUDYUID) ; primary retrieve capability for studies with some missing images
 +1        NEW ERROR,I
 +2       ; ------ number of series related images
           NEW NSERIESRI
 +3       ; - total number of PACS images
           NEW PACSIMAGECOUNT
 +4       ;  number of PACS series
           NEW PACSSERIESCOUNT
 +5       ; -- PACS series instance uids
           NEW PACSSERIESUID
 +6       ; ------ variable for VistA series uid
           NEW SERIESUID
 +7       ;  total number of VistA images
           NEW VISTAIMAGECOUNT
 +8       ; number of VistA series
           NEW VISTASERIESCOUNT
 +9       ; - VistA series instance uid
           NEW VISTASERIESUID
 +10      ;
 +11       SET ERROR=0
 +12      ;
 +13      ; Query PACS at series level and get series UIDs and series image counts
 +14       SET I=0
           FOR 
               SET I=$ORDER(PACSSTUDYUID(I))
               if I=""
                   QUIT 
               Begin DoDot:1
 +15               DO GETSERIU(PACSSTUDYUID(I))
 +16               QUIT 
               End DoDot:1
 +17      ;
 +18      ; get series UIDs and series image counts from VistA
 +19       SET VISTASTUDYUID=0
 +20       FOR 
               SET VISTASTUDYUID=$ORDER(^TMP("MAG",$JOB,"UIDS","VISTA",VISTASTUDYUID))
               if VISTASTUDYUID=""
                   QUIT 
               Begin DoDot:1
 +21               SET SERIESUID=0
 +22               FOR 
                       SET SERIESUID=$ORDER(^TMP("MAG",$JOB,"UIDS","VISTA",VISTASTUDYUID,SERIESUID))
                       if SERIESUID=""
                           QUIT 
                       Begin DoDot:2
 +23                       SET NSERIESRI=$GET(^TMP("MAG",$JOB,"UIDS","VISTA",VISTASTUDYUID,SERIESUID,0))
 +24                       SET ^("IMAGE COUNT")=$GET(^TMP("MAG",$JOB,"UIDS","VISTA SERIES UID",SERIESUID,"IMAGE COUNT"))+NSERIESRI
 +25                       QUIT 
                       End DoDot:2
 +26               QUIT 
               End DoDot:1
 +27      ;
 +28      ; get actual number of series and image counts
 +29       SET PACSSERIESUID=""
           SET (PACSIMAGECOUNT,PACSSERIESCOUNT)=0
 +30       FOR 
               SET PACSSERIESUID=$ORDER(^TMP("MAG",$JOB,"UIDS","PACS SERIES UID",PACSSERIESUID))
               if PACSSERIESUID=""
                   QUIT 
               Begin DoDot:1
 +31               SET PACSSERIESCOUNT=PACSSERIESCOUNT+1
 +32               SET PACSIMAGECOUNT=PACSIMAGECOUNT+^TMP("MAG",$JOB,"UIDS","PACS SERIES UID",PACSSERIESUID,"IMAGE COUNT")
 +33               QUIT 
               End DoDot:1
 +34       SET VISTASERIESUID=0
           SET (VISTAIMAGECOUNT,VISTASERIESCOUNT)=0
 +35       FOR 
               SET VISTASERIESUID=$ORDER(^TMP("MAG",$JOB,"UIDS","VISTA SERIES UID",VISTASERIESUID))
               if VISTASERIESUID=""
                   QUIT 
               Begin DoDot:1
 +36      ; $G added in next line in P305 - PMK 11/22/2021
                   SET VISTASERIESCOUNT=VISTASERIESCOUNT+1
 +37               SET VISTAIMAGECOUNT=VISTAIMAGECOUNT+$GET(^TMP("MAG",$JOB,"UIDS","VISTA SERIES UID",VISTASERIESUID,"IMAGE COUNT"))
 +38               QUIT 
               End DoDot:1
 +39      ;
 +40      ;
 +41      ; no images on PACS
           IF 'PACSIMAGECOUNT
               Begin DoDot:1
 +42               DO QRSTATUS^MAGDSTAA("No DICOM objects on PACS to retrieve")
 +43               QUIT 
               End DoDot:1
               QUIT 0
 +44      ;
 +45      ; check for additional images on VistA - suggested by Lisa Hulslander, Philips
 +46       IF VISTASERIESCOUNT>PACSSERIESCOUNT
               IF VISTAIMAGECOUNT>PACSIMAGECOUNT
                   Begin DoDot:1
 +47                   DO QRSTATUS^MAGDSTAA("VistA has more series and images than PACS")
 +48                   QUIT 
                   End DoDot:1
                   QUIT 0
 +49      ;
 +50       IF VISTASERIESCOUNT=PACSSERIESCOUNT
               IF VISTAIMAGECOUNT=PACSIMAGECOUNT
                   Begin DoDot:1
 +51                   DO QRSTATUS^MAGDSTAA("VistA has same series and images as PACS")
 +52                   QUIT 
                   End DoDot:1
                   QUIT 0
 +53      ;
 +54       IF VISTASERIESCOUNT>PACSSERIESCOUNT
               IF VISTAIMAGECOUNT=PACSIMAGECOUNT
                   Begin DoDot:1
 +55                   DO QRSTATUS^MAGDSTAA("VistA has more series but same images as PACS")
 +56                   QUIT 
                   End DoDot:1
                   QUIT 0
 +57      ;
 +58      ; this test was added in P305 - PMK 11/09/2021 
 +59       IF VISTASERIESCOUNT=PACSSERIESCOUNT
               IF VISTAIMAGECOUNT>PACSIMAGECOUNT
                   Begin DoDot:1
 +60                   DO QRSTATUS^MAGDSTAA("VistA has same series but more images than PACS")
 +61                   QUIT 
                   End DoDot:1
                   QUIT 0
 +62      ;
 +63      ; QUIT added in P305 - PMK 11/09/2021
           IF VISTAIMAGECOUNT>PACSIMAGECOUNT
               Begin DoDot:1
 +64               DO QRSTATUS^MAGDSTAA("VistA has more images than PACS")
 +65               QUIT 
               End DoDot:1
               QUIT 0
 +66      ;
 +67      ; this test was added in P305 - PMK 11/09/2021 
 +68       IF VISTAIMAGECOUNT=PACSIMAGECOUNT
               Begin DoDot:1
 +69               DO QRSTATUS^MAGDSTAA("VistA has the same images as PACS")
 +70               QUIT 
               End DoDot:1
               QUIT 0
 +71      ;
 +72      ; retrieve any missing images 
 +73      ;
 +74      ; no images on VistA
           IF 'VISTAIMAGECOUNT
               Begin DoDot:1
 +75               IF PACSIMAGECOUNT>1
                       Begin DoDot:2
 +76                       DO QRSTATUS^MAGDSTAA("Retrieving entire study of "_PACS("IMAGE COUNT")_" images from PACS")
 +77                       QUIT 
                       End DoDot:2
 +78              IF '$TEST
                       Begin DoDot:2
 +79                       DO QRSTATUS^MAGDSTAA("Retrieving a study with one image from PACS")
 +80                       QUIT 
                       End DoDot:2
 +81               DO STTINC^MAGDSTAA("PACS STUDY LEVEL RETRIEVES",1)
 +82               DO STTINC^MAGDSTAA("PACS IMAGES RETRIEVED",PACSIMAGECOUNT)
 +83      ; move all study instance uids for this study
                   SET ERROR=$$MOVEALL^MAGDSTAB()
 +84               QUIT 
               End DoDot:1
 +85      ; retrieve missing series or missing images within existing series
          IF '$TEST
               Begin DoDot:1
 +86      ; compare the number of images in each PACS series against those in VistA 
 +87               SET PACSSERIESUID=""
 +88               FOR 
                       SET PACSSERIESUID=$ORDER(^TMP("MAG",$JOB,"UIDS","PACS SERIES UID",PACSSERIESUID))
                       if PACSSERIESUID=""
                           QUIT 
                       Begin DoDot:2
 +89      ; if the series does not exist on VistA, retrieve the whole series
 +90                       IF '$DATA(^TMP("MAG",$JOB,"UIDS","VISTA SERIES UID",PACSSERIESUID))
                               Begin DoDot:3
 +91      ; retrieve the series 
                                   DO SERIES^MAGDSTAE(PACSSERIESUID)
 +92                               QUIT 
                               End DoDot:3
 +93      ;
 +94      ; if series image count on VistA is less than on PACS, retrieve missing images
 +95                      IF '$TEST
                               IF ^TMP("MAG",$JOB,"UIDS","VISTA SERIES UID",PACSSERIESUID,"IMAGE COUNT")<^TMP("MAG",$JOB,"UIDS","PACS SERIES UID",PACSSERIESUID,"IMAGE COUNT")
                                   Begin DoDot:3
 +96                                   MERGE VISTASTUDYUID=^TMP("MAG",$JOB,"UIDS","VISTA SERIES UID",PACSSERIESUID,"STUDY UID")
 +97                                   DO GETIMAGE^MAGDSTAF(PACSSERIESUID)
 +98                                   QUIT 
                                   End DoDot:3
 +99                       QUIT 
                       End DoDot:2
 +100              QUIT 
               End DoDot:1
 +101     ;
 +102      QUIT ERROR
 +103     ;
GETSERIU(PACSSTUDYUID) ; query for Series Instance UIDs for the study
 +1        NEW COUNT,I,NSERIESRI,PACSSERIESUID
 +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")=""
 +5        SET ^TMP("MAG",$JOB,"Q/R QUERY",QRSTACK,"QUERY USER APPLICATION")=$$QRSCP^MAGDSTA8
 +6        SET ^TMP("MAG",$JOB,"Q/R QUERY",QRSTACK,"QUERY LEVEL")="SERIES"
 +7        SET ^TMP("MAG",$JOB,"Q/R QUERY",QRSTACK,"ROOT")="STUDY"
 +8       ; C-FIND
           DO SOPUIDQ^MAGDSTV1
 +9        SET COUNT=$GET(^XTMP(MAGXTMP,HOSTNAME,$JOB,QRSTACK,"SERIES",1,1))
 +10       FOR I=1:1:COUNT
               Begin DoDot:1
 +11               SET PACSSERIESUID=$GET(^XTMP(MAGXTMP,HOSTNAME,$JOB,QRSTACK,"SERIES",1,1,I,"SERIESUID"))
 +12               SET NSERIESRI=$GET(^XTMP(MAGXTMP,HOSTNAME,$JOB,QRSTACK,"SERIES",1,1,I,"NSERIESRI"))
 +13      ; Like VistA, PACS may have same series under different study instance uids, so add image count
 +14               SET ^("IMAGE COUNT")=$GET(^TMP("MAG",$JOB,"UIDS","PACS SERIES UID",PACSSERIESUID,"IMAGE COUNT"))+NSERIESRI
 +15               SET ^TMP("MAG",$JOB,"UIDS","PACS SERIES UID",PACSSERIESUID,"STUDY UID",PACSSTUDYUID)=""
 +16               QUIT 
               End DoDot:1
 +17       QUIT