Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/analysis_addmos.pro'
;+
; NAME:
;	analysis_addmos
; PURPOSE:
;	Add a mosaic image to the math-mosaic List structure,
;	and apply any required rotation and/or geometric scaling.
; CALLING:
;	analysis_addmos, imos, imath
; INPUTS:
;	imos = integer, mosaic image number.
;	imath = integer, image number in math_List.
; INPUT and OUTPUT:
;	All other input/output is thru:
;		common mosaic_array, mosaics, mosaic_specs, mosaic_infos
;		common math_mosaics, math_List, math_images, math_imscaled
; KEYWORDS:
;	/RESET causes imath image to be reset to original values of imos image.
; EXTERNAL CALLS:
; 	function N_struct
; 	function RotMag
; 	function scale_mosaic
; 	function image_extract
;	pro copy_struct_inx
; 	pro image_replace
; 	pro image_remove
; 	pro image_insert
; COMMON BLOCKS:
;	common math_mosaics, math_List, math_images, math_imscaled
;	common mosaic_array, mosaics, mosaic_specs, mosaic_infos
; HISTORY:
;	Written: Frank Varosi NASA/GSFC 1990
;	F.V. 1993, added rotation & geometric scaling of image data.
;-

pro analysis_addmos, imos, imath, RESET=reset, TEMPLATE=math_Lsave

   common mosaic_array, mosaics, mosaic_specs, mosaic_infos
   common math_mosaics, math_List, math_images, math_imscaled

	Nmath = N_struct( math_List )
	Nmos = N_struct( mosaic_specs )

	if (imos LT 0) OR (imos GE Nmos) then begin
		message,"mosaic number out of range",/INFO
		return
	  endif else  mospec = mosaic_specs(imos)

	if (imath GE Nmath) then begin

		if (Nmath GT 0) then begin

			xmin = 8 + max( [math_List.xmax] )
			ymin = total( [math_List.ymin] )/Nmath

			if (xmin GT 500) then begin
				ymin = 8 + max( [math_List.ymax] )
				xmin = min( [math_List.xmin] )
			   endif

			math_List = [ math_List, mosaic_struct( 1 ) ]
			math_List.scaling = math_List(0).scaling
			math_List.minLog = math_List(0).minLog

		  endif else begin

			math_List = mosaic_struct( 1 )
			color_st_Load, mospec.colors

			if N_struct( math_Lsave ) GT 0 then begin
				math_List.scaling = math_Lsave(0).scaling
				math_List.minLog = math_Lsave(0).minLog
			   endif
		   endelse

		reset = 0
		imath = imath < Nmath

		if (Nmath GT 0) then begin
			math_List.Magf = math_List(0).Magf
			math_List.winame = math_List(0).winame
			math_List(imath).xmin = xmin
			math_List(imath).ymin = ymin
		  endif else begin
			math_List.Magf = 1
			math_List.winame = "MosMath"
		   endelse
	   endif

	mostmp = image_extract( imos, mosaics, mosaic_specs.size_image )
	math_sizes = math_List.size_image
	math_sizebs = math_List.size_imscaled

	copy_struct_inx, mospec, math_List, INDEX_TO=imath, $
			EXCEPT=["Winame","Magf","scaling","Filter","minLog"]

	mmL = math_List(imath)
	arcs_pixel = [ mospec.RA_pix, mospec.DEC_pix ]

	if (imath GT 0) then begin
		Magfs = arcs_pixel/math_List(0).arcs_pixel
		Magf = total( Magfs )/2
		if (Magf NE 1) then print," Magfs =",Magfs
		mmL.arcs_pixel = math_List(0).arcs_pixel
	  endif else begin
		Magf = 1
		mmL.arcs_pixel = arcs_pixel
	   endelse

	if (mmL.rot_ang_deg NE 0) then begin
		print," Rot.Ang.Deg. =",mmL.rot_ang_deg
		print,"  around origin:",mmL.origin
	   endif

	mostmp = Rotate( RotMag( mostmp, /PIVOT, INT=mmL.rot_interp, $
				XC=mmL.origin(0), YC=mmL.origin(1), $
				ANG=mmL.rot_ang_deg, MAG=Magf, $
				MISS=mospec.min ) , mmL.rotation )
	mmL.Filter = "none"
	mmL.truncate = 0
	math_List(imath) = mmL
	print, mmL.name, mmL.rotation

	if keyword_set( reset ) then begin

		print," reset mosaic [",mospec.name,"] in the Math List..."

		image_replace, mostmp, imath, math_images, math_sizes
		if !DEBUG then stop

	 endif else begin

		print," adding mosaic [",mospec.name,"] to the Math List..."

		image_remove, imath, math_images, math_sizes
		image_insert, mostmp, imath, math_images, math_sizes
	  endelse

	Magf = math_List(imath).Magf
	mosize = size( mostmp ) * Magf

	if (mosize(1) GT !DEVX) OR (mosize(2) GT !DEVY) then begin
		print," reducing Magnification Factor"
		print," from",Magf
		Magf = ( Magf/2 ) > 1
		math_List.Magf = Magf
		print," to  ",Magf
		print," because this mosaic is really big..."
		reset=0
	   endif

	copy_struct, mmL, mospec
	mostmp = scale_mosaic( mostmp, mospec )
	math_List(imath).index = imos

	if keyword_set( reset ) then begin

		image_replace, mostmp, imath, math_imscaled, math_sizebs
		if !DEBUG then stop

	 endif else begin

		image_remove, imath, math_imscaled, math_sizebs
		image_insert, mostmp, imath, math_imscaled, math_sizebs

		s = size( mostmp )
		math_List(imath).xmax = math_List(imath).xmin + s(1) -1
		math_List(imath).ymax = math_List(imath).ymin + s(2) -1
		math_List(imath).Locx = math_List(imath).xmin / float( Magf )
		math_List(imath).Locy = math_List(imath).ymin / float( Magf )
		math_List(imath).Level = max( [math_List.Level] ) + 1
	  endelse

	math_List.size_image = math_sizes
	math_List.size_imscaled = math_sizebs
end