function [x, y, err, monitor, xlab, ylab]=spiceload(filespec)

% function [x, y, err, monitor, xlab, ylab]=spiceload(filespec)
%
% Load routine for SPICE triple axis data files. 
% This routine either accepts a simple filename input 
% argument, or alternatively a compound file
% specification of the form:
%
% filename,<x column name>,<y column name>,<Monitor name>,<Normalization factor>
% can be used. 
%
% Examples:
%
%  'HB1_exp0058_scan0010.dat,e,detector,monitor,400000'
%     Load data from HB1_exp0058_scan0010.dat. 'e' is the independent
%     (x) variable, 'detector' is the y variable and is
%     normalized by 'monitor' and then scaled by 400000.
%      
%  'HB1_exp0058_scan0011.dat,h'
%     Load data from HB1_exp0058_scan0011.dat. 'h' is the independent
%     variable. The y default is 'detector', and the
%     monitor default is no monitor. Normalization default is 1
%
%  'HB1_exp0058_scan0012.dat'
%      Load data from HB1_exp0058_scan0012.dat with the default x and y
%      values with no monitor and normalization of 1.
%
%  'HB1_exp0058_scan0013.dat,default,default,monitor,20000'
%      Load data from HB1_exp0058_scan0013.dat with default x and y 
%      normalized to monitor and scaled by 20000.
%
% ML 4-Aprl-03
%

%===== Parse filespec, open data file, and get column names ============================

%---- Set default column names

xname='NONE';
yname='NONE';
monname='NONE';
norm=1;

%===== Get column names ====================================

%----- Parse filespec --------------------------------------

[fspec filespec]=strtok(filespec,',');
while ~isempty(filespec)
   [s filespec]=strtok(filespec,',');
   fspec=str2mat(fspec,s);
end
nargs=size(fspec,1);

% 
%----- Update scan parameters from filespec---------------------------

if nargs>1 xname=deblank(fspec(2,:)); end
if nargs>2 yname=deblank(fspec(3,:)); end
if nargs>3 monname=deblank(fspec(4,:)); end
if nargs>4 norm=str2num(fspec(5,:)); end

filename=deblank(fspec(1,:));

%----- Load data, and attempt to auto-check for new or old tascom files ---------

[data,headertext,headers,defxname,defyname,defxvalue,defyvalue]=spicedata(filename);

if strcmp(xname,'NONE')==1 
    xname=defxname;
elseif strcmp(xname,'default')==1 
    xname=defxname;
end

if strcmp(yname,'NONE')==1 
    yname=defyname;
elseif strcmp(yname,'default')==1 
    yname=defyname;
end

xcol=strmatch(xname,headers,'exact');
ycol=strmatch(yname,headers,'exact');

%----- Select data columns to analyse

x=data(xcol,:);
y=data(ycol,:);

%----- Select monitor column

if strcmp(monname,'NONE')==1
    monitor=ones(size(x));
else
    moncol=strmatch(monname,headers,'exact');
    monitor=data(moncol,:);
end

%----- Test to see if selected monitor is zero

[monzeros]=find(monitor==0);
if ~isempty(monzeros);
   disp(' ')
   disp('  Warning: selected monitor has some zeros')
   disp(' ')
   monitor=ones(size(x));
end

%----- Calculate errors, and trap for zero counts

err=zeros(size(y));      
for itest=1:length(y)
   if y(itest) == 0
      err(itest)=1/monitor(itest);
   else
      err(itest)=sqrt(y(itest)) /monitor(itest);
   end
end

%----- Normalise counts

y=norm*y ./monitor; 
err=norm*err;

%----- Create labels

xlab=char(xname); 

if strcmp(monname,'NONE')==1
    ylab=char(yname);
else    
    ymon=[' ( per  ' num2str(mean(monitor)) ' '  char(monname) ' ) * ' num2str(norm)];
    ylab=[char(yname) ymon];
end

return



