; Creates average and variance images
 ;
 ; SOURCE: spider/docs/techs/recon1/Procs/show-ref-views.spi
 ;
 ; PURPOSE: Creates a large circular plot (a SPIDER image) containing smaller 
 ;          circles representing the 83 angular groups. The radii of the small
 ;          circles are proportional to the number of particles for each view.
 ;          Selects  '*_good'input file if it exists otherwise selects: '*_all'.
 ;          Can display plot.
 ;
 ; USAGE:   clean ; ./spider spi/dat @show-ref-views

 ; I/O PARAMETERS AND FILES ARE SET HERE:

 ;  ------------ Parameters -------------------------------------

 [wantGoodYN]   = 0    ; Use 0 for all particles, 1 for verified particles
                       ; If outputs for verified particles don't exist, then all particles will be used
 
 [scale]        = 1    ; Scale factor, if circles get too big or too small (default == 1)
 
 [autoOpen]     = 2    ; Automatically open plot? (0 == No, 1 == ImageMagick display, 2 == qview)

 [disp-type]    = 1    ;  1 = put projection numbers in display
                       ;  0 = don't put in numbers
                       ; -1 = numbers only, no circles!

 [backgd]       = 1    ; 1 = white background, 0 = black background

 [disp-dim]     = 640  ; Display dimension
 [outer-diam]   = 600  ; Diameter of outer circle

 [num-views]    = 83   ; Number of reference projections


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

 ; KEY=VIEW# /     TOTPARTS
 [how_many_prefix]  = 'views/parts_vsview'                    ; Number of particles per view

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

 [display_prefix]    = 'show_ref_views'         ; All group plot image

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

; Set common filenames & parameters
 @verify-settings
 [iter] = 0

 IQ FI [goodexist]           ; See if this should access 'all' or 'good'
   [how_many_prefix]_good

 ; Will only use the verified ('_good') data if it exists *and* if [wantGoodYN] = 1
 IF ( [wantGoodYN] == 1 ) THEN
   IF ( [goodexist] == 1 ) THEN
     [useGood] = 1
   ELSE
     SYS
       echo ' Verified particle list [how_many_prefix]_good.$DATEXT not found.  Trying all particles...' ; echo
   ENDIF
 ENDIF
 
 IF ( [useGood] == 1 ) THEN
   [allvsgood] = '_good'
 ELSE
   [allvsgood] = '_all'
 ENDIF

 ; Concatenate name
 [display]  = '[display_prefix][allvsgood]'
 [how_many] = '[how_many_prefix][allvsgood]'

 SYS
   echo " Computing image of reference view usage" ; echo ' '

 [height-digit]  = INT(13/2)
 [width-1digit]  = INT(10/2)
 [width-2digits] = INT(23/2)

 BL     ; Create a blank image
   _1
   [disp-dim],[disp-dim]
   N
   0.0 

 [half-dim]  = INT(([disp-dim]+1)/2)

 ; Draw outline
 PT
   _1
   L          ; Line
   1,[half-dim]  ; Starting point
   [disp-dim],[half-dim]    ; Ending point
   Y          ; Continue?
   L          ; Line
   [half-dim],1      ; Starting point
   [half-dim],[disp-dim]    ; Ending point
   N          ; Continue?

 [outer-radius] = ([outer-diam]+1)/2

 ; Draw the large circles
 PT
   _1
   CL         ; Circle Line
   [half-dim],[half-dim]    ; Center coordinates
   [outer-radius]        ; Radius
   N          ; Do not Continue

 ; '''''''''''''''''''''''''''''''''''''''''''''''''''''
 ; If they want labels, do them 1st
 ; '''''''''''''''''''''''''''''''''''''''''''''''''''''

 IF ( [disp-type] == 0 ) GOTO LB8

 ;; if [disp-type] > 0, numbered labels are added
 ;; They're drawn w/ lower left corner at (x,y), so they need to be shifted.
 ;; Digits from the LA command are approx 13 pixels high,
 ;;       10 pix wide (single digit),23 p wide (2 digits)

 DO [view] = 1,[num-views]
   UD IC [view], [psi],[theta],[phi]      ; doc file with reference angles
     [iter_refangs]

   ; Force theta between 0 & 90 (shouldn't be necessary with VO EA)
   IF ( [theta] > 90.0 ) THEN
     [theta] = 180.0-[theta]
     [phi]   = [phi]+180.0

     ; force phi between 0 & 360 (shouldn't be necessary with VO EA)
     IF ( [phi]> 360.0 )  [phi]=[phi]-360.0

   ENDIF

   [theta-latitude] = ([theta]/90) * [outer-radius]
   ; Looking down the Hilbert sphere like looking down at the North Pole, where
   ; theta is equivalent to latitude, except here the North Pole corresponds to latitude 0.

   [xcoord] = cos([phi])*[theta-latitude] + [half-dim]  ; formerly [v81]
   [ycoord] = sin([phi])*[theta-latitude] + [half-dim]  ; formerly [v82]
   
   ; Add a label-----------------
   [label-ycoord] = [ycoord] + [height-digit]    ; y coord

   ; Label x-coord depends on number of digits
   IF ([view].LT.10) THEN   ; single digit
      [label-xcoord] = [xcoord]-[width-1digit]
      
      LA L   
        _1
        _2
        {*[view]}
        [label-xcoord],[label-ycoord]

   ELSE
      [label-xcoord] = [xcoord]-[width-2digits]
      
      LA L   
        _1
        _2
        {**[view]}
        [label-xcoord],[label-ycoord]

   ENDIF

   CP
     _2
     _1

 ENDDO


 LB8  ; Jump here is display-type is 0

 AR
   _1
   _2
   p1/2

 RP      ; This little hack keeps the max=1 for subsequent graphix
   _2
   1,1 
   1 

 CP
   _2
   _1

 ; '''''''''''''''''''''''''''''''''''''''''''''''''''''
 ; The main loop that draws the circles
 
 DO [view]= 1,[num-views]

   UD IC [view],[psi],[theta],[phi]      ; doc file with reference angles
     [iter_refangs]

   IF ( [theta] > 90.0 ) THEN
     [theta]=180.0-[theta]
     [phi]=[phi]+180.0

     IF( [phi] .lt. 360.0 ) GOTO LB2
     [phi]=[phi]-360.0

   ENDIF

   [theta-latitude] = ([theta]/90) * [outer-radius]
   [xcoord] = cos([phi])*[theta-latitude] + [half-dim]
   [ycoord] = sin([phi])*[theta-latitude] + [half-dim]

   UD IC [view], [num-parts]
     [how_many]     ; Doc file: how many particles in each view

   ; Skip unpopulated views
   IF([num-parts] == 0) CYCLE

   [popul-radius] = sqr([num-parts])*[scale]

   ; Draw a circle
   PT
     _1
     CL  ; _C_ircle _L_ine
     [xcoord],[ycoord]
     [popul-radius]
     N                 ; Continue?

 ENDDO

 RP                   ; Undo the little hack
   _1
  1,1
  0 

 ; Invert, if necessary
 IF ( [backgd] > 0 ) THEN
   NEG
     _1
     [display]
 ELSE
   CP
     _1
     [display]    ; Output file
 ENDIF

 UD ICE
   [iter_refangs]
 UD ICE
   [how_many]

 CP TO TIF    ; was JPG
   [display]              ; SPIDER  file       (input)
   [display].tif          ; JPG     file       (output)
;(CP TO JPG was giving an error in Ubuntu.)
 
 IF ([autoOpen] == 1) THEN
   SYS                      ; Display image using display (from ImageMagick)
     display [display].tif &
 ELSEIF ([autoOpen] == 2) THEN
   SYS                      ; Display image using qview (from SPIRE)
     qview [display].$DATEXT &
 ELSE
   SYS
     echo ; echo " Use Web to show reference view usage:  [display].$DATEXT" ; echo ' '
 ENDIF

 EN D

 ; Modified 2016-10-19
 ;    TO DO: intelligent scaling factor
 ;    2016-05-04 (trs) -- added option to open plot automatically, using ImageMagick or qview
 ;    2016-04-27 (trs) -- was previously ignoring [wantGoodYN]
 ;    2016-04-27 (trs) -- writing output as TIFF (CP TO JPG gives an error in Ubuntu)
 ;    2013-11-27 (trs) -- user-defined dimensions
 ;    2013-10-23 (agl) -- New file names, modernized syntax & cosmetic
 ;    2011-11-23 (trs) -- added user-defined scale factor for radii
 ;    2011-11-02 (trs) -- re-annotated

 ;