function [S, R] = ParseDataFileLineII(L)

% [S, Remainder] = ParseDataFileLineII(L)
%
% Determine fields of structure S from '\n' terminated line
% L from DataFile.  R is remainder of line.
% Fields of S are:
%	idnum (int)
%  weight(double)       DEFAULT = 1
%	sample_id (string)
% 	basic_class (string) - basic classification
%	classification (string)
%
% 	specfile (string) - complete path + filename
%  sample_area (interior, crust, etc)	
%
% 	imagefile (string)
%  distance (double) /[meters]
%  scale (double)[x y] /[mm/pixel]
%  origin (int)[x y] /[pixel number - cartesian axes]
%
%  metalfile (string) - path + filename of metal detector readings
%
%  position [X Y Z] /[m] - position (usually DGPS coords) of sample, if applicable.  
%                          If position n/a, then values are Inf.

% Liam Pedersen, NASA ARC, August 1998
% version 1.0
% Liam Pedersen, NASA ARC, July 1999
% version 2.0

S.idnum = 0;
[S,L] = GetId(S,L);
[S,L] = GetSpec(S,L);
[S,L] = GetImage(S,L);
[S,L] = GetMetal(S,L);
[S,L] = GetPos(S,L);

%********************************************************************
%********************************************************************
function [S,R] = GetId(S0,L)

S = S0;

%extract idnum
[st, L] = strtok(L);
if (st(1) == '*') S.idnum = -1;
else S.idnum = str2num(st);
   if (isempty(S.idnum)) S.idnum = -1;
   end
end

%extract weight
[st, L] = strtok(L);
if (st(1) == '*') S.weight = 1;
else S.weight = str2num(st);
   if (isempty(S.weight)) S.weight = 1;
   end
end

% extract sample_id
[S.sample_id,L] = strtok(L);

% extract basic classification
[S.basic_class, L] = strtok(L);

% extract classification {gabbro, granite, basalt, diorite, slate, limestone, etc...}
[S.classification, L] = strtok(L);

% extract formation (Sedimentary, Igneous, Metamorphic, Extraterrestrial)
[S.formation, L] = strtok(L);

R = L

%********************************************************************
%********************************************************************
function [S,R] = GetSpec(S0,L)

S = S0;

% extract spectrum path + file
[spath, L] = strtok(L);
[sfile, L] = strtok(L);

if (~isempty(spath) & ~isempty(sfile) & spath(1) ~= '*' & sfile(1) ~= '*')
   S.specfile = [spath sfile];
else
   S.specfile = '*';
end

% extract sampling area
[S.sample_area, L] = strtok(L);
R = L;

%********************************************************************
%********************************************************************
function [S,R] = GetImage(S0,L)

S = S0;

% extract image path + file
[ipath, L] = strtok(L);
[ifile, L] = strtok(L);

if (~isempty(ipath) & ~isempty(ifile) & ipath(1) ~= '*' & ifile(1) ~= '*')
   S.imagefile = [ipath ifile];
else
   S.imagefile = '*';
end

% extract distance from camera to sample
[st, L] = strtok(L);
if (~isempty(st) & st(1) ~= '*')
   S.distance = str2num(st);
else
   S.distance = -1;
end

% extract x scale of image (mm/pixel)
[st, L] = strtok(L);
if (~isempty(st) & st(1) ~= '*')
   S.scale(1) = str2num(st);
else
   S.scale(1) = -1;
end

% extract y scale of image (mm/pixel)
[st, L] = strtok(L);
if (~isempty(st) & st(1) ~= '*')
   S.scale(2) = str2num(st);
else
   S.scale(2) = -1;
end

% extract x origin of image (pixel)
[st, L] = strtok(L);
if (~isempty(st) & st(1) ~= '*')
   S.origin(1) = str2num(st);
else
   S.origin(1) = -1;
end

% extract y origin of image (pixel)
[st, L] = strtok(L);
if (~isempty(st) & st(1) ~= '*')
   S.origin(2) = str2num(st);
else
   S.origin(2) = -1;
end

R = L;

%********************************************************************
%********************************************************************
function [S,L] = GetMetal(S0,L)
% extract metal detector path + file
S = S0;

[mpath, L] = strtok(L);
[mfile, L] = strtok(L);

if (~isempty(mpath) & mpath(1) ~= '*' & mfile(1) ~= '*')
   S.metalfile = [mpath mfile];
else
   S.metalfile = '*';
end

R = L;

%********************************************************************
%********************************************************************
function [S,R] = GetPos(S0,L)
%extract position of sample
S = S0;

[stx, L] = strtok(L);
[sty, L] = strtok(L);
[stz, L] = strtok(L);
if (~isempty(stx) & stx(1) ~= '*') X = str2num(stx); else X = inf; end
if (~isempty(sty) & sty(1) ~= '*') Y = str2num(sty); else Y = inf; end
if (~isempty(stz) & stz(1) ~= '*') Z = str2num(stz); else Z = inf; end
S.position = [X Y Z];

R = L;


%********************************************************************
%********************************************************************


%********************************************************************
%********************************************************************


%********************************************************************
%********************************************************************
