([deci]) ; Converts a scanned file to a SPIDER format
     
 ; SOURCE: spider/docs/techs/recon1/Procs/convert-p.spi

 ; PURPOSE: Converts a scanned file to a SPIDER format.
 ;
 ; [deci] = decimation factor for 'DC S' command
 ;  0 = Get value from param file (key=16)
 ;  1 = Full sized image
 ;  2 = 1/2 size
 ;  4 = 1/4 size

 FR
   ?Parameter file? [params]

 FR
   ?Input micrograph? [raw]

 FR
   ?SPIDER output file? [spider]

 ;  -----  Get zip & format flags from parameter file -----------------
 UD 1, [sp_zflag]
   [params]
 UD 2, [sp_fflag]
   [params]

 ; ----------- Checks if files are gzip compressed -------------------

 IF ( [sp_zflag]*[sp_fflag] > 0) THEN
    SYS                          ; Both tif & zip flags set
      gunzip [raw].gz
    SYS               
      echo "  Unzipped: [raw].gz"

 ELSEIF ( [sp_zflag] > 0 ) THEN 
    SYS                          ; Zip, but not tif, flag set
      echo "  Unzipping file "
    SYS
      gunzip [raw].$DATEXT.gz
    SYS               
      echo "  Unzipped: [raw].$DATEXT.gz"

 ENDIF

 ; ----------- Conversion based on scanner type -------------------

 IF ( [sp_fflag] == 0 ) THEN
   CP                 ; Already a SPIDER file, put in output file
     [raw]            ; File   (input)
     [spider]         ; File   (output)

   SYS
     echo "  Copied SPIDER file: [raw]   To: [spider]"

 ELSEIF ( [sp_fflag] == 1 ) THEN
    UD 3,[sp_nx]      ; HiScan raw file, Get X,Y size parameters 
      [params]        ; Params file    (input)
    UD 4,[sp_ny]      ;         
      [params]

    CP FROM RAW
      16              ; Bits / pixel
      [raw]           ; File (input)
      [sp_nx],[sp_ny] ; Size
      342             ; Header bytes
      1
      N
      _1              ; File  (output)

    AR
      _1              ; File   (input)
      [spider]        ; File   (output)
      P1*(-1)+5

    SYS
      echo "  Converted Hiscan: [raw]   To: [spider]"

 ELSEIF ( [sp_fflag] == 3 ) THEN     
    SYS              ; ZI tif file.  Overview should always = 1
      zi2spi [raw] [spider].$DATEXT 1

    SYS
      echo "  Converted ZI file: [raw]   To: [spider].$DATEXT"

 ELSEIF ( [sp_fflag] == 4) THEN     
    CP FROM NIKON   ; Nikon Tif Scanner file
      [raw] 
      [spider]

    SYS
      echo "  Converted Nikon TIF: [raw]   To: [spider]"

 ELSE     
    SYS               ; 
      echo "  Unsupported input format: {***[sp_fflag]}"

 ENDIF

; ----------- Rezip if necessary -------------------

 IF ( [sp_zflag]*[sp_fflag] > 0) THEN   
    SYS                          ; Both tif & zip flags set
      gzip [raw]

    SYS               
      echo "  Re-zipped: [raw]"
 
 ELSEIF ([sp_zflag] == 1) THEN        ;
    SYS                          ; Recompress the file
      gzip [raw].$DATEXT

    SYS               
      echo "  Re-zipped: [raw].$DATEXT"
 ENDIF

 ; ----------- Size reduction, if any -------------------


 IF ( [deci] < 1) THEN
    UD 16,[sp_deci]        ; Get reduction factor from param file
      [params]             ; Param file   (input)
    [deci] = [sp_deci]
 ENDIF

 IF ( [deci] > 1) THEN     ; Reduction

   DC S                    ; Reduce size
     [spider]
     _1                    ; File  (input)
     [deci],[deci]         ; Reduction

   CP                      ; Copy
     _1                    ; File  (input)
     [spider]              ; File  (output)

    SYS               
      echo "  Decimated: [raw]   By: {**[deci]}"
 ENDIF

 RE
 ;