function [data,failed] = build_fields (data)
% build_fields: build data fields
failed=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% build a list of field names
data.config.field_names={};
data.config.field_location=[];
data.config.field_ranges=[];

% accel variables configuration
if (data.accel.loaded==1)
   for (i=1:size(data.accel.labels,2))
      data.config.field_names(end+1,:)={data.accel.labels{i}};
      data.config.field_location(end+1,:)=[1 i+1 0];
      data.config.field_ranges(end+1,:)=[nan nan nan 0 1 0 1 0];
      % field_ranges: min  max  grid  prec  analysis_disp  play_window_disp  filter  used_in_analysis
   end;
end;

% GPS variables configuration
if (data.GPS.loaded==1)
   % GPS field data
   data.config.field_names=[data.config.field_names;data.GPS.field_names];
   data.config.field_location=[data.config.field_location;data.GPS.field_location];
   data.config.field_ranges=[data.config.field_ranges;data.GPS.field_ranges];
   % field_ranges: min  max  grid  prec  analysis_disp  play_window_disp  filter  used_in_analysis
   
   % build min & max time field
   if ((strcmp(data.GPS.type,'RT17')==1)|(strcmp(data.GPS.type,'PreRT17')==1)|(strcmp(data.GPS.type,'DAT')==1))
      if ((isempty(data.GPS.position)==0)&(isempty(data.GPS.observables)==0))
         GPS_time_range=[min([min(data.GPS.position(:,1)) min(data.GPS.observables.time(:,1))])  ...
               max([max(data.GPS.position(:,1)) max(data.GPS.observables.time(:,1))])];
      elseif ((isempty(data.GPS.position)==0)&(isempty(data.GPS.observables)==1))
         GPS_time_range=[min(data.GPS.position(:,1))  max(data.GPS.position(:,1))];
      elseif ((isempty(data.GPS.position)==1)&(isempty(data.GPS.observables)==0))
         GPS_time_range=[min(data.GPS.observables.time(:,1))  max(data.GPS.observables.time(:,1))];
      end;
   end;
end;

if ((data.accel.loaded==1)&(data.GPS.loaded==1))
   data.config.time_range=[min([min(data.accel.accel(:,1)) GPS_time_range(1)])  ...
         max([max(data.accel.accel(:,1)) GPS_time_range(2)])];
elseif ((data.accel.loaded==1)&(data.GPS.loaded==0))
   data.config.time_range=[min(data.accel.accel(:,1))  max(data.accel.accel(:,1))];
elseif ((data.accel.loaded==0)&(data.GPS.loaded==1))
   data.config.time_range=GPS_time_range;
end;
data.config.time_range=[floor(data.config.time_range(1))  ceil(data.config.time_range(2))];

% labels
data.config.field_labels=[];
if (isempty(data.config.field_names)==0)
   data.config.field_labels{size(data.config.field_names,1)}=[];
end;

% check sizes
if (size(data.config.field_names,1)~=size(data.config.field_location,1))
   err_text=sprintf('Error building "name" & "location" fields!\n');
   fault(err_text);
   return;
end;
if (size(data.config.field_names,1)~=size(data.config.field_ranges,1))
   err_text=sprintf('Error building "name" & "range" fields!\n');
   fault(err_text);
   return;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
failed=0;
return;
