function X = database_DiPol_V2_load(xlsname,varargin)
%DATABASE_DIPOL_V2_LOAD load data and make html per sample
%
%See also: xls2struct, database_DiPol_V2_load2html

%% Version <http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html>
% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $

%xlsname = 'D:\PROJECTS\DIPOL\RDBMS\database_DiPol_Complete_V8a_June2012.xls';

fldnames = {'LCS',...
            'Stations',...
            'Parameters',...
            'Samples',...
            'Indices',...
            'Observations',...
            'SamplesQuery',...
            'ObservationsQuery'};
            
disp(['... Please wait ', mfilename,' loading ',xlsname])

%% load

   for ifld=1:length(fldnames)
      fldname = fldnames{ifld};
      X.(fldname) = xls2struct(xlsname,fldname,'units',0);
   end

%% manual fixes

   %
   charfields = {'SamplingStart',...
                 'SampleOriginType',...
                 'SampleOriginSubType'};
              
  for j=1:length(charfields)
    charfld = charfields{j};
    for i=1:length(X.SamplesQuery.(charfld))
      if isnumeric(X.SamplesQuery.(charfld){i})
        if isnan(X.SamplesQuery.(charfld){i})
            X.SamplesQuery.(charfld){i} = '';
        else
            error('??')
        end
      end
    end
  end
  
  %
  for i=1:length(X.Parameters.ParameterId)
    if isnan(X.Parameters.ParameterLong{i})
       X.Parameters.ParameterLong{i} = X.Parameters.ParameterShort{i};
    end
  end
  
%% make empty numeric fields NaN

  OPT.parameter.numeric = {
         'ParameterId',...
           'Pathogens',...
       'WFD_EQS_Fresh',...
       'WFD_EQS_Other',...
        'LC50_Daphnia',...
     'LC50_Pimephales',...
    'LC50_Selenastrum',...
                'Comp',...
               'Scale',...
              'logKOC'}; % 'Nr_msPAF','x_values'};
                         
  for j=1:length(OPT.parameter.numeric)
    charfld = OPT.parameter.numeric{j};
    if iscell(X.Parameters.(charfld)) % if numeric, no correction needed
      for i=1:length(X.Parameters.ParameterId)
        if isempty(X.Parameters.(charfld){i})
           X.Parameters.(charfld){i} = nan;
        end
      end
      X.Parameters.(charfld) = cell2mat(X.Parameters.(charfld));
    end
  end
  
%% make nan     character fields empty
%  make numeric character fields text
  OPT.parameter.char = {
   'ParameterLong',...
  'ParameterShort',...
   'ParameterUnit',...
             'CAS'}; % 'Group','End'};
             
  for j=1:length(OPT.parameter.char)
    charfld = OPT.parameter.char{j};
    for i=1:length(X.Parameters.ParameterId)
      if isnan(X.Parameters.(charfld){i})
         X.Parameters.(charfld){i} = '';
      elseif isnumeric(X.Parameters.(charfld){i})
         X.Parameters.(charfld){i} = num2str(X.Parameters.(charfld){i});
      end
    end
  end
