# $Header: /home/pros/xray/xplot/RCS/tvproj.cl,v 11.0 1997/11/06 16:38:45 prosb Exp $
# $Log: tvproj.cl,v $
# Revision 11.0  1997/11/06 16:38:45  prosb
# General Release 2.5
#
# Revision 9.1  1997/04/24 19:16:46  prosb
# JCC (4/24/97) - add 6 new parameters for IRAF2.11/display :
#                    bpmask,bpdisplay,bpcolors,overlay,ocolors,zmask
#               - rename nsample_line to nsample.
#
#Revision 9.0  1995/11/16  19:08:25  prosb
#General Release 2.4
#
#Revision 8.0  1994/06/27  17:01:22  prosb
#General Release 2.3.1
#
#Revision 7.0  93/12/27  18:47:44  prosb
#General Release 2.3
#
#Revision 6.1  93/12/17  08:52:29  janet
#jd - updated to match sgraph changes.
#
#Revision 6.0  93/05/24  16:40:12  prosb
#General Release 2.2
#
#Revision 5.0  92/10/29  22:34:22  prosb
#General Release 2.1
#
#Revision 4.1  92/09/15  17:10:57  mo
#MC	9/15/92		Add the 'getdevdim' call to eliminate the user
#			xdim and ydim parameters.
#
#Revision 4.0  92/04/27  15:10:06  prosb
#General Release 2.0:  April 1992
#
#Revision 1.4  92/04/13  15:04:20  mo
#MC	4/13/92		Default zrange = yes and then force sample lines
#			to 512 for the whole image
#
#Revision 1.3  92/03/23  17:28:32  mo
#MC	3/23/92		Add log scaling
#
#Revision 1.2  92/02/19  17:35:20  mo
#MC	2/11/92		Make the fill parameter dynamic depending on
#			input image size
#
#Revision 1.1  92/02/05  11:22:10  mo
#Initial revision
#
# -----------------------------------------------------------------------
# Module:	tvproj.cl
# Description:  Displays an image and calculates and plots x and y projectsions
# -----------------------------------------------------------------------
procedure tvproj(image)
#
#  Parameters for the Imcontour task
#
# -- xdisplay Params -
 string image    {prompt="IRAF image filename",mode="a"}
# string region   {"field",prompt="field or box region", mode="a"}
 string gcolor {"r",min="r|y|b|g|w", prompt="Contour Graphics Overlay Color - r|y|b|g|w",mode="h"}
 string axes	{"b",min="x|y|b",prompt="axes to project",mode="h"}
 real yscale {0.5,min=0.0,max=1.0, prompt="Fraction of display for plot",mode="h"}
 bool xlog  { no,min=no,max=yes,prompt="use log not linear xscale",mode="h"}
 bool ylog  { no,min=no,max=yes,prompt="use log not linear yscale",mode="h"}
# int    xdim	{512, prompt="x-dimension of IMAGE window",mode="h"}
# int    ydim	{512, prompt="y-dimension of IMAGE window",mode="h"}
#
# -- Display Params --
 real z1 {0.0, prompt="minimum greylevel to be displayed", mode="h"}
 real z2 {30.0, prompt="maximum greylevel to be displayed", mode="h"}
 string ztrans {"linear",prompt= "greylevel transformation (linear|log|none)", mode="h"} 
 bool zrange {yes, prompt="display full image intensity range", mode="h"}
 bool zscale {no,prompt="display range of greylevels near median", mode="h"}
 bool clobber{yes, prompt="OK to delete existing file?", mode="h"}

begin
 
    string  color       # drawing color
    string  img		# Image filename
    string  sdevice
    string  gdevice
    string  gfile
    string  outtab
    string reg
    string  tempname
    real scale
    bool clob
    int	ixdim
    int iydim
    int xdim
    int ydim
    bool tvfill

#  Check if display & imcontour task are defined
    if ( !deftask("display") )
       error(1, "Requires images/tv to be loaded for display task!")
    if ( !deftask("sgraph") )
       error(1, "Requires tables to be loaded for sgraph task!")
    if ( !deftask("improj") )
       error(1, "Requires xspatial to be loaded for improj task!")

    img = image
    color = gcolor
    clob = clobber
#    reg = region
    reg = "field"
    scale = yscale

#  Set the image graph device based on the color chosen
    if ( color == "y" ) {
       sdevice="imdy"
       gdevice="imdy"
    } else if ( color == "r" ) {
       sdevice="imdr"
       gdevice="imdr"
    } else if ( color == "b" ) {
       sdevice="imdb"
       gdevice="imdb"
    } else if ( color == "g" ) {
       sdevice="imdg"
       gdevice="imdg"
    } else if ( color == "w" ) {
       sdevice="imdw"
       gdevice="imdw"
    } else {
       sdevice="imd"
       gdevice="imd"
    } 

# jcc- initialize xdim, ydim to 1000 which is the
#      default of nsample in display/iraf2.11
        xdim = 1000
        ydim = 1000

        imgets(img,"i_naxis1")
        ixdim = int( imgets.value)
        imgets(img,"i_naxis2")
        iydim = int( imgets.value)
	_getdevdim("stdimage")
	xdim = x
	ydim = y
        if( ixdim < xdim || iydim < ydim )
                tvfill = yes
        else
                tvfill = no

# Display the image in the Image Windo
#
    display (img, 1,
       bpmask="BPM", bpdisplay="none", bpcolors="red", overlay="",
       ocolors="green",
       erase=yes, border_erase=no, select_frame=yes,
       repeat=no, fill=tvfill, zscale=zscale, contrast=0.25, 
       zrange=zrange,
       zmask="",
       nsample=ydim, xcenter=0.5, ycenter=0.5, xsize=1., ysize=1.,
       xmag=1., ymag=1., order=0, z1=z1, z2=z2, ztrans=ztrans,
       lutfile="")

    #display (img, 1, erase=yes, border_erase=no, select_frame=yes, 
       #repeat=no, fill=tvfill, zscale=zscale, contrast=0.25, 
       #zrange=zrange, nsample=ydim, xcenter=0.5, 
       #ycenter=0.5, xsize=1., ysize=1., xmag=1., ymag=1., 
       #order=0, z1=z1, z2=z2, ztrans=ztrans, lutfile="")
#
#  Calculate the x and y projections
#
    if( access("improj.tab") ){
	if( !clob )
           error(1,"Output file already exists" )
	else
	   delete("improj.tab")
    }
    _rtname(img,"improj.tab","_prj.tab")
    outtab = s1
    tempname = "foo"
    _clobname(outtab,tempname,clob)
    tempname = s1
    improj(img,reg,tempname,xdim,ydim,clobber=clob,display=0)
#    pltpar.transpose=no
#    print("temp: " // tempname )
#    print("out: " // outtab)
    _fnlname(tempname,outtab)
 
    gfile = outtab // " counts_x"
#   print ("tab1: " // gfile)
#   print ("scale: ",scale)
    if( axes == "x" || axes == "b" ){

    sgraph (gfile,errcolumn="none",device=gdevice,append=no, stack=no,
	    wl=0., wr=0., wb=0., wt=0., xflip=no, yflip=no, 
            transpose=no, pointmode=no, marker="box", szmarker=0.005, 
	    erraxis=0, errtype="bartck", pattern="solid", 
	    crvstyle="straight", logx=no, logy=xlog, rejectlog=yes, box=no, 
	    ticklabels=no, grid=yes, xlabel=" ", ylabel=" ", 
            title="imtitle", sysid=no, lintran=no, p1=0., p2=0., q1=0., 
            q2=1., left=0., right=1., bottom=0., top=scale, 
            majrx=5, minrx=5, majry=5, minry=5, round=no,margin=0.,fill=no)
    }
    gfile = outtab // " counts_y"
#   print ("tab2: " // gfile)
#   print ("scale: ",scale)
    if( axes == "y" || axes == "b" ){
    sgraph (gfile, errcolumn="none", device=gdevice, append=no, stack=no,
	    wl=0., wr=0., wb=0., wt=0., xflip=no, yflip=no,  
	    transpose=yes, pointmode=no, marker="box", szmarker=0.005, 
	    erraxis=0, errtype="bartck", pattern="solid", 
	    crvstyle="straight", logx=ylog, logy=no, rejectlog=yes, box=no, 
	    ticklabels=no, grid=yes,xlabel=" ", ylabel=" ", title="imtitle", 
	    sysid=no, lintran=no, p1=0., p2=0., q1=0., q2=1., left=0.,
	    right=scale, bottom=0., top=1., majrx=5, minrx=5, majry=5, 
            minry=5, round=no, margin=0., fill=no)  
    }
    delete ("improj.tab")
end
