%**************************************************************************
%This function reads measurement data from a file.  The columns
%are z, measured voltages, drift corrected voltages, and the calculated
%fields.
%
%Input:
%file_name, name of the measurement data file
%
%Output:
%header, header information of the data file
%z, z positions of the measurements (m)
%bx, measurements of the Bx field (T)
%by, measurements of the By field (T)
%bz, measurements of the Bz field (T)
%
%Zack Wolf, 11/24/06, 11/2/10

function [header, z, bx, by, bz] = read_meas_zvxyzbxyzh(file_name)

%Open the data file
fid = fopen(file_name, 'r');
if fid == -1
    disp('idanal_read_zvxyzbxyzh: unable to open input file');
end

%Extract the header information
%Read down to the title line
ii = 0;
while 1
    in_line = fgetl(fid);
    ii = ii +1;
    header{ii} = in_line;
    if findstr('Magnetic Field Measurements vs Z', in_line) > 0
        break;
    end
end

%Continue reading until the data begins
%Look for the column headers
while 1
    in_line = fgets(fid);
    if findstr('----------', in_line) > 0
        break;
    end
end

%Read the data
data_array = fscanf(fid, '%f %f %f %f %f %f %f %f %f %f', [10, inf]);
z = data_array(1, :);
vy = data_array(2, :);
vx = data_array(3, :);
vz = data_array(4, :);
vy_corr = data_array(5, :);
vx_corr = data_array(6, :);
vz_corr = data_array(7, :);
by = data_array(8, :);
bx = data_array(9, :);
bz = data_array(10, :);

%Close the file
fclose(fid);

%Done