; $Id: tasappend.pro,v 1.2 2002/01/14 20:59:27 amunter Exp $
function tasappend,a,b,xind,notext=notext
;+
; NAME:         APPEND
; PURPOSE:
;               append two TASCOM data structures
; CATEGORY:
; CALLING SEQUENCE:
;               c=append(a,b,'x-index',/NOTEXT)
; INPUTS:
;               a,b- the two TASCOM data structures to be merged
;                  e.g. c=append(a,b)
; OPTIONAL INPUT PARAMETERS:
;               'x-index'- the parameter used to index the data for sorting
;                  the default is 'h'
;                  e.g. c=append(a,b,'em')
;		/NOTEXT - if this is set, then no new text is added to d.text
; 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 the second TASCOM structure is missing a print parameter
;               field then -9999. will be used, only fields found in the first
;               structure (a) will be duplicated in the result (c).  
;               Intensities are normalized to monitor.
; RESTRICTIONS:
; PROCEDURE:
; MODIFICATION HISTORY:
;               Thom Mason, Riso National Laboratory, 1993
;		August 18, 93: Changed name from append to tasappend
;	                Added the option /NOTEXT, to disable the descriptive
;			text appending to d.text
;			Paul Schleger, Riso
;-
IF (n_params() eq 2) THEN xind='H'                  
IF (tasid(xind,a) eq -1) THEN xind=a.prp(0)
temp=a.text
IF NOT keyword_set(NOTEXT) THEN BEGIN   
   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,4),/remove_all) else $
     temp(0)=strcompress(a.text(0)+'+'+b.text(0),/remove_all)
ENDIF
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
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)
scan={points: ap+bp,heads: a.heads,pars: a.pars,text: temp,$
hdef: a.hdef,hnum: a.hnum,prp: a.prp,data: c}
return,scan
end
