; $Id: merge.pro,v 1.2 2002/01/14 20:59:26 amunter Exp $
function combine,a,ptnum,b,npt,msct

if (msct eq 'MON') then begin
  scaf1=1./(a(tasid('SIGM',b),ptnum)/a(tasid('MON',b),ptnum))^2.
  scaf2=1./(a(tasid('SIGM',b),ptnum+1)/a(tasid('MON',b),ptnum+1))^2.
  endif else begin
    scaf1=a(tasid(msct,b),ptnum)
    scaf2=a(tasid(msct,b),ptnum+1)
    endelse
toscaf=scaf1+scaf2
scaf1=scaf1/toscaf
scaf2=scaf2/toscaf               
d=fltarr(b.pars)
for i=0,b.pars-1 do begin
  d(i)=scaf1*a(i,ptnum)+scaf2*a(i,ptnum+1)
  if (i eq tasid('SIGI',b)) then $
    d(i)=sqrt((scaf1*a(i,ptnum))^2.+(scaf2*a(i,ptnum+1))^2.)
  if (i eq tasid('SIGM',b)) then $
    d(i)=sqrt((scaf1*a(i,ptnum))^2.+(scaf2*a(i,ptnum+1))^2.)
  if (a(i,ptnum) eq -9999.) then d(i)=a(i,ptnum+1)
  if (a(i,ptnum+1) eq -9999.) then d(i)=a(i,ptnum)
  endfor
c=fltarr(b.pars,npt-1)
if (ptnum gt 0) then c(*,0:ptnum-1)=a(*,0:ptnum-1)
c(*,ptnum)=d
if (ptnum lt npt-2) then c(*,ptnum+1:npt-2)=a(*,ptnum+2:npt-1)
return,c
end

function merge,a,b,xind
;+
; NAME:         MERGE
; PURPOSE:
;               merge and rebin two TASCOM data structures
; CATEGORY:
; CALLING SEQUENCE:
;               c=merge(a,b,'x-index')
; INPUTS:
;               a,b- the two TASCOM data structures to be merged
;               'x-index'- the print parameter to use to index the
;                       data structures for rebining, ie if two points
;                       have the same value (within 5% of step size) of
;                       x-index they will be averaged
;                  e.g. c=merge(a,b,'h')
; OPTIONAL INPUT PARAMETERS:
; KEYWORD PARAMETERS:
; OUPUTS:
;               a new data structure consisting of:
;                 d.points - integer - # of points in scan
;                 d.heads - integer - # of header parameters
;                 d.pars - integer - # of scan parameters
;                 d.text - string array - 2 lines of descriptive text
;                 d.hdef - string array - definition of header parameters
;                 d.hnum - float array - header parameters
;                 d.prp - string array - defintion of scan parameters
;                 d.data - 2D float array - scan data (parameter,point#)
; OPTIONAL OUTPUT PARAMETERS:
; COMMON BLOCKS:
; SIDE EFFECTS:
;               if one of the TASCOM structures is missing a print parameter
;               field the other is used, only fields found in the first
;               structure (a) will be duplicated in the result (c).  Errors
;               are calculated RMS, monitor is used to weight averages,
;               all fields of the structure are averaged when present in both
;               the assumes that the counts are per constant monitor, the
;               fields 'MON' and 'SIGM' (calculated by TASRD) must exist for
;               it to work
; RESTRICTIONS:
; PROCEDURE:
; MODIFICATION HISTORY:
;               Thom Mason, Riso National Laboratory, 1993
;-
if (n_params() eq 2) then xind='H'
if (tasid(xind,a) eq -1) then xind=a.prp(0)
temp=a.text
if (strmid(a.text(0),0,2) eq strmid(b.text(0),0,2)) then $
  temp(0)=strcompress(a.text(0)+'+'+strmid(b.text(0),3,100),/remove_all) else $
  temp(0)=strcompress(a.text(0)+'+'+b.text(0),/remove_all)
c=fltarr(a.pars,a.points+b.points)
if (tasid('MON',a) eq -1) then msct='TIM' else $
  if (a.data(tasid('MON',a),0) le 0) then msct='TIM' else msct='MON'
msc=a.data(tasid(msct,a),0)
ap=a.points
bp=b.points
tol=abs(0.15*(a.data(tasid(xind,a),0)-a.data(tasid(xind,a),1)))
for i=0,a.pars-1 do begin
  if (tasid(a.prp(i),b) ge 0) then begin
    c(i,0:ap-1)=a.data(i,*)
    if (i eq tasid('I',a)) then $
       c(i,0:ap-1)=c(i,0:ap-1)*msc/a.data(tasid(msct,a),*) else $
      if (i eq tasid('MON',a)) then $
       c(i,0:ap-1)=c(i,0:ap-1)*msc/a.data(tasid(msct,a),*) else $
      if (i eq tasid('SIGI',a)) then $
       c(i,0:ap-1)=c(i,0:ap-1)*msc/a.data(tasid(msct,a),*) else $
      if (i eq tasid('SIGM',a)) then $
       c(i,0:ap-1)=c(i,0:ap-1)*msc/a.data(tasid(msct,a),*) else $
      if (i eq tasid('TIM',a)) then $
       c(i,0:ap-1)=c(i,0:ap-1)*msc/a.data(tasid(msct,b),*)
    c(i,ap:ap+bp-1)=b.data(tasid(a.prp(i),b),*)
    if (i eq tasid('I',a)) then $
       c(i,ap:ap+bp-1)=c(i,ap:ap+bp-1)*msc/b.data(tasid(msct,b),*) else $
      if (i eq tasid('MON',a)) then $
       c(i,ap:ap+bp-1)=c(i,ap:ap+bp-1)*msc/b.data(tasid(msct,b),*) else $
      if (i eq tasid('SIGI',a)) then $
       c(i,ap:ap+bp-1)=c(i,ap:ap+bp-1)*msc/b.data(tasid(msct,b),*) else $
      if (i eq tasid('SIGM',a)) then $
       c(i,ap:ap+bp-1)=c(i,ap:ap+bp-1)*msc/b.data(tasid(msct,b),*) else $
      if (i eq tasid('TIM',a)) then $
       c(i,ap:ap+bp-1)=c(i,ap:ap+bp-1)*msc/b.data(tasid(msct,b),*)
   endif else begin
    c(i,0:ap-1)=a.data(i,*)
    if (i eq tasid('I',a)) then $
       c(i,0:ap-1)=c(i,0:ap-1)*msc/a.data(tasid(msct,a),*) else $
      if (i eq tasid('MON',a)) then $
       c(i,0:ap-1)=c(i,0:ap-1)*msc/a.data(tasid(msct,a),*) else $
      if (i eq tasid('SIGI',a)) then $
       c(i,0:ap-1)=c(i,0:ap-1)*msc/a.data(tasid(msct,a),*) else $
      if (i eq tasid('SIGM',a)) then $
       c(i,0:ap-1)=c(i,0:ap-1)*msc/a.data(tasid(msct,a),*) else $
      if (i eq tasid('TIM',a)) then $
       c(i,0:ap-1)=c(i,0:ap-1)*msc/a.data(tasid(msct,b),*)
    c(i,ap:ap+bp-1)=-9999.
   endelse
  endfor
; now have append the two datasets need to sort
sortin=sort(c(tasid(xind,a),*))
c(*,*)=c(*,sortin)
npt=ap+bp
ptnum=0
while (ptnum lt npt-1) do begin
  step=abs(c(tasid(xind,a),ptnum)-c(tasid(xind,a),ptnum+1))
  if (step le tol) then begin
    c=combine(c,ptnum,a,npt,msct)
    npt=npt-1
    endif else ptnum=ptnum+1
  endwhile
scan={points: npt,heads: a.heads,pars: a.pars,text: temp,$
hdef: a.hdef,hnum: a.hnum,prp: a.prp,data: c}
return,scan
end
