; NAME:
;       pro sb304_deinterlace.pro
;
; PURPOSE:
;       This program deinterlaces SB304 images, grouping the outputs
;       in 4 vertical band. This is useful for e.g. making Fourier
;       Power spectra.  Reference pixels in columns [0:3,*] and
;       [2052:2055,*] are not de-interlaced. As such, the resultaning
;       images are 2048x2048 pixels^2.
;
;       Input files must be FITS images having the suffix '.fits'. The
;       output image has the suffix '.dl.fits'.
;
; CALLING SEQUENCE:
;       sb304_deinterlace, infile [, /help]
;
; INPUTS:
;       infile  - A 2-dimensional SB304 FITS image
;       
;
; KEYWORD PARAMETERS:
;       help             - Print a help message
;
; EXAMPLE
;       De-interlace a single SB304 image named 'Image7.fits' creating
;       a 2048x2048 pixels^2 output file having the suffix '.dl.fits'.
;
;       IDL> sb304_deinterlace, 'Image7.fits'
;
;
; REFERENCE:
;
;
; MODIFICATION HISTORY:
;       Written by:
;       Modified:    B.J. Rauscher, IDTL, March 11, 2002
;                    Initial release
;
;-
;     

pro sb304_deinterlace, infile, help=help

       ;; Set command line defaults
       if (keyword_set(help)) then begin
           stop, 'Usage: sb304_deinterlace, FITS_file [, /help]'
       endif

       ;; Read in the data
       fits_read, infile, data_in, header_in

       ;; Trim the reference pixels
       dataTrim = data_in[4:2051,*]

       ;; Fill the output data array
       q1=dataTrim((findgen(512)*4)+0,*)
       q2=dataTrim((findgen(512)*4)+1,*)
       q3=dataTrim((findgen(512)*4)+2,*)
       q4=dataTrim((findgen(512)*4)+3,*)
       data_out=[q1,q2,q3,q4]

       ;; Create the output filename by searching the input file for
       ;; the .fits suffix and replacing this with the .dl.fits
       ;; suffix.
       outfile = strmid(infile, 0, strpos(infile, '.fits', /REVERSE_SEARCH))+'.dl.fits'

       ;; Write the result
       fits_write, outfile, data_out

end
