; Makes view of angular data output from Refinement
;
; SOURCE: spider/docs/techs/recon/newprogs/angdiff.spi
;
; PURPOSE: Plot differences from one iteration to the next.
;          Make difference maps for each pair of refinement iterations n,n-1
;
; I/O PARAMETERS AND FILES ARE SET HERE:

 [igo]  = 1   ; Start defocus group
 [iend] = 1   ; End defocus group

 [i1]   = 2   ; Start iteration (start with 2nd!)
 [i2]   = 7   ; Last iteration

 ;  ----------- Input files ---------------------------------------

 [angles]   = 'final/align_{**[v23]}_{***[v20]}'   ; Alignment doc files, [v23]=current iteration

 [ang_prev] = 'final/align_{**[v29]}_{***[v20]}'   ; Alignment doc files, [v29]=previous iteration

 ; --------------- Output files  -------------------------------------

 [dir]     = 'display'                             ; Output directory

 [disp]    = 'diff_{**[v23]}_{***[v20]}'           ; Output image

 ; -------------- END BATCH HEADER ------------------------------

 SYS                                         ; Make output directory
   mkdir -p [dir]

 [out] = '[dir]/[disp]'

 DO [v20] = [igo], [iend]                    ; Loop over defocus groups

   DO [v23] = [i1],[i2]                      ; Loop over refinement iterations

      [v29] = [v23] - 1                      ; The previous iteration   

      BL                                     ; Create a blank image
        _1
        607,607 
        N
        0.0 
   
      PT                                     ; Draw the large circles
        _1
        CL
        303,303 
        302 
        N

      PT                                     ; Draw lines
        _1
        L
        1,303 
        605,303 
        Y
        L
        303,1 
        303,605 
        N

     ;    ----------------------------------------------------

      DO                                     ; Loop over all angles
         UD NEXT [key],[v51],[v52],[v53]     ; Doc file with  angles
           [angles]                          ; Angle file      (input) 
         IF ([key] <= 0) EXIT                ; End of angles file
 
         ; Compute coords
         IF ([v52]  > 90.0) THEN
            [v52] = 180.0-[v52]
            [v53] = [v53]+180.0

            IF ([v53] >= 360.0) THEN
               [v53] = [v53]-360.0
            ENDIF
         ENDIF

         [v61] = [v52] / 90
         [v61] = [v61] * 300
         [v81] = cos([v53])
         [v82] = sin([v53])
         [v81] = [v81] * [v61]
         [v82] = [v82] * [v61]
         [v81] = [v81] + 303
         [v82] = [v82] + 303

         [v71] = [v81]
         [v72] = [v82]

         UD IC [key],[v51],[v52],[v53]      ; Previous Doc file with  angles
         [ang_prev]

         IF  ([v52] > 90.0) THEN
            [v52] = 180.0 - [v52]
            [v53] = [v53] + 180.0
            IF ([v53] >= 360.0) THEN
               [v53] = [v53] - 360.0
            ENDIF
         ENDIF

         [v61] = [v52] / 90
         [v61] = [v61] * 300
         [v81] = cos([v53])
         [v82] = sin([v53])
         [v81] = [v81] * [v61]
         [v82] = [v82] * [v61]
         [v81] = [v81] + 303
         [v82] = [v82] + 303
   
         PT
           _1
           L                              ; Draw a line from previous point to current
           [v71],[v72]
           [v81],[v82]
         N

      ENDDO

      UD ICE
        [angles]
      UD ICE
        [ang_prev]

      FS
        _1

      NEG
        _1
        _2

      CP
        _2
        [out]                             ; The output file

     SYS
       echo ' 'Iteration: {%i2%[v23]}, Defocus group: {%i3%[v20]}

   ENDDO
ENDDO

EN
;