

% Usage:  sum_id = add_orders (minus_id, plus_id)
public define add_orders (m_id, p_id)
{
   variable m, p, id, t;
   
   % Copy the data into S-Lang structures
   % (I'm assuming they're both on the same wavelength grid)
   m = get_data_counts (m_id);
   p = get_data_counts (p_id);
   
   % Add the counts, assume Poisson errors 
   m.value += p.value;
   m.err = sqrt(m.value);
   
   % Register the result as a new dataset 
   id = define_counts (m);
   set_data_info (id, get_data_info (p_id));

   % Define the exposure time in the summed dataset
   % (although the ARF exposure is the value that matters)
   t = 0.5 * (get_data_exposure (m_id) + get_data_exposure (p_id));
   set_data_exposure (id, t);

   % return the index of the summed counts data set
   return id;
}

% Usage:  sum_id = add_arfs (minus_id, plus_id)
public define add_arfs (m_id, p_id)
{
   variable ma, pa;
   
   % Copy the ARFs into S-Lang structures
   % (I'm assuming they're both on the same wavelength grid)
   ma = get_arf (m_id);
   pa = get_arf (p_id);
   
   variable mt, pt, t;
   
   % Compute the mean exposure time
   mt = get_arf_exposure (m_id);
   pt = get_arf_exposure (p_id);
   t = 0.5 * (mt + pt);
   
   variable a = @ma;
   
   % Add the ARFs, weighting by exposure and
   % adding the uncertainties in quadrature 
   a.value = (mt/t) * ma.value + (pt/t) * pa.value;
   a.err = hypot ((mt/t)*ma.err, (pt/t)*pa.err);
   
   % Register the summed ARF in the internal ARF list
   % and update its exposure and information fields
   variable id = define_arf (a);
   set_arf_info (id, get_arf_info (p_id));
   set_arf_exposure (id, t);
   
   % return the index of the summed ARF
   return id;
}

