;+
; NAME:
;	SMEI_SEQUENCE::PRINT
;
;
; PURPOSE:
;	Make a PostScript file of a SMEI sequence
;
;
; CATEGORY:
;	SMEI_SEQUENCE
;
;
; CALLING SEQUENCE:
;	seqref -> print
;
;
; KEYWORD PARAMETERS:
;	/grid		If specfied, then add a grid to the images
;	date	int	Date format for the images.
;	/file		If specified, then add the filename to the
;			images.
;	/add_range	If set, then add the range to the plot.
;	range	float	The min and max values for the display
;			(implies adding the range to the plot)
;	colour_table int The colour table to use. (-1 or unset to use
;			the sequence map)
;	xsize	float	The size in cm of the x-dimension of the page
;	ysize	float	The size in cm of the y-dimension of the page
;	/no_colour	Do not generate colour PS (equivalent to
;			colour_table=0)
;	/portrait	Print in portrait mode.
;	/landscape	Print in landscape mode
;	plot_file str	Specify a filename other than the default idl.ps
;	/encapsulated	If set, then make an eps file (handled
;			explicitly as other things are done
;			differently for eps).
;	ncolumns int	The number of columns to arrange the images
;	nrows	int	The number of rows to arrange the images
;	/logarithmic	If set, then use a logarithmic mapping of the
;			colour table.
;	/in_rows	If set, then go arrange the frames in rows
;			rather than in columns.
;	/cmyk		If set, then use the CMYK colour model
;
;	Any keywords to DEVICE that do not clash with the above and
;	which are accepted by the PS device may be passed.
;
; SIDE EFFECTS:
;	A PostScript file is generated.
;
;
; RESTRICTIONS:
;	Pro-tem it doesn't print the file.
;
;
; MODIFICATION HISTORY:
;	Original (after image version): 16/5/03; SJT
;	Add row orientation: 21/7/03; SJT
;	Use print options: 22/7/03; SJT
;	Add spooling of print file: 6/6/05; SJT
;	Add cmyk option: 3/6/10; SJT
;-

pro smei_sequence::print, grid = grid, date = date, colour_table = $
                 colour_table, range = range, xsize = xsize, $
                 no_colour = no_colour, portrait = portrait, $
                 plot_file = plot_file, encapsulated = $
                 encapsulated, file = file, add_range = $
                 add_range, logarithmic = logarithmic, $
                 ncolumns = ncolumns, nrows = nrows, $
                 ysize = ysize, in_rows = in_rows, landscape $ 
                 = landscape, print = print, preview = preview, $
                 id = id, cmyk = cmyk, _extra = _extra


if n_elements(grid) ne 0 then igrid = grid $
else igrid = self -> get_annotations(/grid)

if n_elements(date) ne 0 then idate = date $
else idate = self -> get_annotations(/date)

if n_elements(file) ne 0 then ifile = keyword_set(file) $
else ifile = self -> get_annotations(/file)

if n_elements(add_range) ne 0 then irange = keyword_set(add_range) $
else irange =  self -> get_annotations(/range)

if keyword_set(landscape) then orient = 0 $
else if keyword_set(portrait) then orient = 1 $
else if keyword_set(encapsulated) then orient = 2 $
else orient = self -> get_print_opts(/type)

if not keyword_set(xsize) then xsize = self -> get_print_opts(/xsize)
if not keyword_set(ysize) then ysize = self -> get_print_opts(/ysize)

if n_elements(in_rows) eq 0 then in_rows = self ->  $
  get_print_opts(/order) 

if not keyword_set(plot_file) then plot_file = self ->  $
  get_print_opts(/file)
if strlen(plot_file) eq 0 then plot_file = 'idl.ps'

if not keyword_set(ncolumns) then ncolumns = self ->  $
  get_print_opts(/columns) 
if not keyword_set(nrows) then nrows = self -> get_print_opts(/rows)

if n_elements(print) eq 0 then print_cmd = self ->  $
  get_print_opts(/print) $
else print_cmd = print

if n_elements(preview) eq 0 then preview_cmd = self ->  $
  get_preview_opts(/preview) $
else preview_cmd = preview

if n_elements(cmyk) eq 0 then cmyk_m = self -> get_print_opts(/cmyk) $
else cmyk_m = cmyk

odev = !d.name                  ; Will allow windoze & macintrash
                                ; as well as unix to be used. (in
                                ; principle)

plot_env, /save

set_plot, 'ps'

if n_elements(colour_table) ne 0 then table = colour_table $
else if keyword_set(no_colour) then table = 0 $
else table = -1

device, color = table ne 0, bits_per_pixel = 8, _extra = _extra, file $
        = plot_file, cmyk = cmyk_m

!p.font = 0

case orient of
    2: begin                    ; EPS
        if xsize eq 0 then xsize = 24
        if ysize eq 0 then ysize = 18

        device, /portrait, xsize = xsize, ysize = ysize, /encapsulated
    end
    1: begin                    ; Portrait
        if xsize eq 0 then xsize = 18
        if ysize eq 0 then ysize = 24
        device, /portrait, xsize = xsize, ysize = ysize, xoffset = $
                2+(xsize - 18)/2., yoffset = 3., encapsulated = 0
    end
    0: begin                    ; Landscape
        if xsize eq 0 then xsize = 24
        if ysize eq 0 then ysize = 18

        device, /landscape, xsize = xsize, ysize = ysize, $
                yoffset = 2+xsize, xoffset = (18 - ysize)/2., $
                encapsulated = 0
    end
endcase

if ncolumns eq 0 then ncolumns = 1
if nrows eq 0 then nrows = 1
npanel = ncolumns*nrows

if table gt 0 then loadct, table $
else if table lt 0 then self -> apply_colour_map

img = self -> get_first()
pixels = float(img -> get_size())

xip = xsize/float(ncolumns)
yip = ysize/float(nrows)
if xip/yip ge pixels[0]/pixels[1] then begin ; Y space limits
    yis = yip
    xis = yip*pixels[0]/pixels[1]
endif else begin
    xis = xip
    yis = xis*pixels[1]/pixels[0]
endelse

iseq = 0l
while obj_valid(img) do begin
    if ((ipanel = iseq mod npanel)) eq 0 and iseq ne 0 then erase
    if keyword_set(in_rows) then begin
        x0 = (ipanel mod ncolumns) * xip + (xip-xis)/2.
        y0 = (nrows - (ipanel / ncolumns) - 1)*yip + (yip-yis)/2.
    endif else begin
        x0 = (ipanel/nrows) * xip + (xip-xis)/2.
        y0 = (nrows - (ipanel mod nrows) - 1)*yip + (yip-yis)/2.
    endelse
    if not img -> get_pflags(/skip) then begin
        img -> ps_show, xis, yis, x0, y0, $
          logarithmic = logarithmic, igrid = $
          igrid, idate = idate, range = range, $
          irange = irange, ifile = ifile
        iseq = iseq+1
    endif
    img = img -> get_next()
endwhile

device, /close

set_plot, odev

plot_env, /restore

choose_ps_actions, plot_file, print_cmd, preview_cmd, printable = $
                   orient ne 2, id = id

end
