Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/define_origin.pro'
pro define_origin, Raw_Mosaic, xorg, yorg, CHECK_SCALE=check
;+
; NAME:
;	define_origin
; PURPOSE:
;	Define origin of arc-second coordinate system for raw mosaic structure.
;	Fields in structure giving relative arc-seconds are updated.
; CALLING:
;	define_origin, Raw_Mosaic, xorg, yorg
;
; INPUTS and OUTPUTS:
;
;	Raw_Mosaic = structured array of images (image_List type structure)
;			or image pointers (mosaic_List type structure),
;			containing fields for ARCSX and ARCSY, and
;			these fields are updated with new coordinates.
;
;	xorg, yorg = window (device) coordinates (optional) to be the origin,
;		If (xorg,yorg) are not given,
;			then users selects origin with mouse-cursor.
; KEYWORDS:
;	/CHECK_SCALE : causes the current relative coordinates to be
;			checked for consistency with current plate scale,
;		and if inconsistent the origin is set to center of mosaic.
; EXTERNAL CALLS:
;	function check_struct
;	function draw_mark
; COMMON BLOCKS:
;	common array_scale, ArcSec_Pix_x, ArcSec_Pix_y
;	Plate scale of pixel array, used if not containing within structure.
; PROCEDURE:
;	Straightforward.
; HISTORY:
;	Written, Frank Varosi NASA/GSFC 1989.
;	F.V.1993, check for and use arc-seconds/pixel scale from structure.
;-
  common array_scale, ArcSec_Pix_x, ArcSec_Pix_y   ;plate scale of pixel array 

	Nim = check_struct( Raw_Mosaic, Magf )
	if (Nim LE 0) then return

	BELL = string( 7b )
	Tags = tag_names( Raw_Mosaic )

	if (NOT max( Tags EQ "ARCSX" )) then begin
		message,"cannot define arc-sec coordinate system" + $
			" for this image structure" + BELL,/INFO
		wait,1
		return
	   endif

	if max( Tags EQ "ARCS_PIXEL" ) then begin
		Arcs_Pix_x = Raw_Mosaic(0).arcs_pixel(0)
		Arcs_Pix_y = Raw_Mosaic(0).arcs_pixel(1)
	  endif else begin
		Arcs_Pix_x = ArcSec_Pix_x
		Arcs_Pix_y = ArcSec_Pix_y
	   endelse

	Magf = float( Magf )
	x0 = Raw_Mosaic(0).Locx
	y0 = Raw_Mosaic(0).Locy
	arcsx0 = Raw_Mosaic(0).arcsx
	arcsy0 = Raw_Mosaic(0).arcsy

	if keyword_set( check ) then begin
		if (Nim LE 1) then return
		dx = Raw_Mosaic.arcsx - arcsx0 - Arcs_Pix_x*(Raw_Mosaic.Locx-x0)
		dy = Raw_Mosaic.arcsy - arcsy0 - Arcs_Pix_y*(Raw_Mosaic.Locy-y0)
		if ( max( abs( dx ) ) GT Arcs_Pix_x/100 ) OR $
		   ( max( abs( dy ) ) GT Arcs_Pix_y/100 ) then begin
			xorg = total( Raw_Mosaic.xmin )/Nim
			yorg = total( Raw_Mosaic.ymin )/Nim
			message,"current arc-sec coordinates inconsistent",/INFO
		  endif else return
	   endif

 if (N_elements( xorg ) EQ 1) AND $
    (N_elements( yorg ) EQ 1) then goto,SET_ARCS

	printw," Select Point to be NEW ORIGIN of relative coordinates",$
					WINDOW = Raw_Mosaic(0).windo, /ERASE
	tvcrs,.5,.5,/NORM
	cursor,/DEV,xorg,yorg

	if (!err EQ 4) then begin
		printw," ",/ERASE
		return
	  endif else wsav = draw_mark( xorg,yorg, 32, xsav,ysav )
AGAIN:
	arcsxorg = arcsx0 + Arcs_Pix_x * (xorg/Magf - x0)
	arcsyorg = arcsy0 + Arcs_Pix_y * (yorg/Magf - y0)

	print," "
	print," selected point currently at X=",arcsxorg," arcsec  Y=", $
						arcsyorg," arcsec",     $
						FORM="(A,F7.2,A,F7.2,A)"

	print,"  press MIDDLE button to make this point the NEW ORIGIN"
	print,"        RIGHT button to ABORT, or select point again with LEFT"

	printw,["press MIDDLE button to make this point the NEW ORIGIN",$
		"RIGHT button to ABORT, or select point again with LEFT"],/ERAS

	cursor,/DEV,xorg,yorg
	printw,[" "," "," "],/ERASE
	tv,wsav,xsav,ysav

	CASE !err OF
	     1:	BEGIN
			wsav = draw_mark( xorg,yorg, 32, xsav,ysav )
			goto,AGAIN
		  END
	     4:	return
	  else:
	ENDCASE

SET_ARCS:
	Raw_Mosaic.arcsx = Arcs_Pix_x * ( Raw_Mosaic.Locx - xorg/Magf )
	Raw_Mosaic.arcsy = Arcs_Pix_y * ( Raw_Mosaic.Locy - yorg/Magf )

	message,"new origin is set",/INFO
return
end