; file: frimage_begin.idl = start custum frimage
; last: Jul 18 2000


; data dimensions 
datasize=size(data)
nx=datasize(1)
ny=datasize(2)

; what windows?
windowsdevice='X'
if (!d.name eq 'WIN') then windowsdevice='WIN'

; device character sizes
; IDL standard charsizes:  SUN   DEC    PS         SGI  Dell
;           !d.x_ch_size =  6     8    0.222cm     12     6
;           !d.y_ch_size = 12    10    0.352cm     12    10
xcharsun=6.                   ; SUN char width
ycharsun=12.                  ; SUN char height
xchardev=float(!d.x_ch_size)  ; this device char width
ychardev=float(!d.y_ch_size)  ; this device char height
xcharps=0.222                 ; postscript char width in cm
ycharps=0.352                 ; postscript char height in cm
xpixratio=xcharsun/xchardev   ; SUN=1 DEC=0.75 SGI=0.5 Dell=1
ypixratio=ycharsun/ychardev   ; SUN=1 DEC=1.2  SGI=1   Dell=1.2

; set thicknesses
xthick=1.0*charscale
ythick=xthick
charthick=1.5*charscale
labelcharthick=charthick

; tick mark sizes
ticklen=-10.0*charscale*xpixratio   ; negative = outward
xticklen=ticklen/ny 
yticklen=ticklen/nx

; character sizes
charsize=1.5*charscale*xpixratio
xcharsize=charsize*xcharscale  ; bumping problem?
ycharsize=charsize*ycharscale
xlabelcharsize=2.0*charscale*xpixratio  
ylabelcharsize=xlabelcharsize
titlecharsize=xlabelcharsize*titlecharscale

; offsets and margins
xlabeloffset=4*xlabelcharsize*xlabeloffsetscale
ylabeloffset=5*ylabelcharsize*ylabeloffsetscale
toplabeloffset=3*charscale  ; in ychar units
leftmargin=ylabeloffset+5*charscale
rightmargin=4*charscale
bottommargin=xlabeloffset+1*charscale
topmargin=toplabeloffset+2*charscale
xmargin=[leftmargin,rightmargin]
ymargin=[bottommargin,topmargin]

; loop over output: first screen, then optional additional postscript
for iloop=1,2 do begin

; 1st time = screen initialization (example Users Guide version 3.1 15-8)
  if iloop eq 1 then begin
  set_plot,windowsdevice
  device=1
  norm=0
  xcharnorm=xchardev
  ycharnorm=ychardev
  windowwidth=nx+xchardev*(leftmargin+rightmargin)    ; in xchardev
  windowheight=ny+ychardev*(bottommargin+topmargin)   ; in ychardev
  window,nrwindow,xpos=xwindow,ypos=ywindow,$
    xsize=windowwidth,ysize=windowheight,colors=255
  contour,data,xstyle=4,ystyle=4,/nodata,xmargin=xmargin,ymargin=ymargin
  px=!x.window*!d.x_vsize
  py=!y.window*!d.y_vsize
  xsize=nx
  ysize=ny
endif

; 2nd time = postscript initialization (example scalable pixels 15-6)
if (iloop eq 2 and ps ne 0) then begin
  set_plot,'ps'
  device=0
  norm=1  
  pswidth=nx*xcharps/xchardev+xcharps*(leftmargin+rightmargin)  ; cm
  psheight=ny*xcharps/xchardev+ycharps*(bottommargin+topmargin) ; cm
  if (psheight/pswidth ge 1.5) then factor=24.0/psheight $
    else factor=16.0/pswidth     ; fill page portrait-wise
  if (landscape ne 0 and pswidth/psheight ge 1.5) then factor=1.5*factor
  factor=scaleps*factor
  if (landscape eq 0) then $ny*xcharps/xchardev
    device,bits_per_pixel=8,file=filename,xsize=pswidth,ysize=psheight,$
      scale_factor=factor,xoff=2,yoff=2,/portrait
  if (landscape ne 0) then $  
    device,bits_per_pixel=8,file=filename,xsize=pswidth,ysize=psheight,$
      scale_factor=factor,xoff=2,yoff=26,/landscape
  contour,data,xstyle=4,ystyle=4,/nodata,xmargin=xmargin,ymargin=ymargin
  px(0)=!x.window(0)
  py(0)=!y.window(0)
  xsize=!x.window(1)-!x.window(0)
  ysize=!y.window(1)-!y.window(0)
  xcharnorm=xcharps/pswidth
  ycharnorm=ycharps/psheight*xpixratio  ; 0.75 for DEC, 0.5 for SGI; why??
endif

if (iloop eq 1 or ps ne 0) then begin
  tv,data,px(0),py(0),xsize=xsize,ysize=ysize,device=device,norm=norm
  contour,data,xaxisarr,yaxisarr,/noerase,/nodata,device=device,norm=norm,$
    xstyle=1,ystyle=1,xmargin=xmargin,ymargin=ymargin,$
    xticks=xticks,yticks=yticks,$
    xthick=xthick,ythick=ythick,$
    xticklen=xticklen,yticklen=yticklen,$
    xcharsize=xcharsize,ycharsize=ycharsize,charthick=charthick
  xyouts,device=device,norm=norm,$     ; x-axis label
    px(0)+0.5*xsize,py(0)-xlabeloffset*ycharnorm,$
    xtitle,$
    align=0.5,$
    charsize=xlabelcharsize,charthick=labelcharthick
  xyouts,device=device,norm=norm,$     ; y-axis label
    px(0)-ylabeloffset*xcharnorm,py(0)+0.5*ysize,$
    ytitle,$
    align=0.5,orientation=90,$
    charsize=ylabelcharsize,charthick=labelcharthick
  if (xfigtitle eq -1 and yfigtitle eq -1) then begin  ; title above frame
    xyouts,device=device,norm=norm,$     ; top label (file identifier)
      px(0)-ylabeloffset*xcharnorm,py(0)+ysize+toplabeloffset*ycharnorm,$
      figtitle,$
      charsize=titlecharsize,charthick=labelcharthick*titlecharscale
  endif else begin                       ; identifier within frame
    xyouts,device=device,norm=norm,$
      px(0)+xfigtitle*xsize,py(0)+yfigtitle*ysize,$
      figtitle,color=255,$  ; reverse black/white when overlay
      charsize=titlecharsize,charthick=labelcharthick*titlecharscale
  endelse
endif
