function [argnData] = READ_viewarg_beam(userMeta, argnMeta);
% READ_viewarg_beam.m  A function to read Aquadopp velocities, convert
%                      them from BEAM to ENU coordinates (if necessary),
%                      and correct for magnetic variation.
%
% usage:    [argnData] = READ_viewarg_beam(userMeta, argnMeta);
%
%               argnData  - a structure w/ the following fields:
%                     nBursts - number of bursts
%                      nCells - number of velocity cells
%                    err_code - error code
%                    sta_code - status code
%                        batt - Battery, volts
%                   sound_spd - soundspeed, m/s
%                         hdg - Heading, deg
%                        ptch - Pitch, deg
%                        roll - Roll, deg
%                        pres - Pressure, dBar
%                          tC - temperature, degrees C
%                       xsen1 - analog input 1
%                       xsen2 - analog input 2
%                    velBeam1 - velocity along beam 1, m/s
%                    velBeam2 - velocity along beam 2, m/s
%                    velBeam3 - velocity along beam 3, m/s
%                    ampBeam1 - amplitude along beam 1, counts
%                    ampBeam2 - amplitude along beam 2, counts
%                    ampBeam3 - amplitude along beam3, counts
%                           u - eastward velocity, m/s
%                           v - northward velocity, m/s
%                           w - vertical velocity, m/s
%               userMeta - a structure with user-defined metadata
%               argnMeta - a structure with instrument metadata
%
% Copyright 2004 
% USGS Woods Hole Field Center
% Written by Charlene Sullivan
% csullivan@usgs.gov
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use of this program is described in:
%
% Sullivan, C.M., Warner, J.C., Martini, M.A., Voulgaris, G., 
% Work, P.A., Haas, K.A., and Hanes, D.H. (2006) 
% South Carolina Coastal Erosion Study Data Report for Observations
% October 2003 - April 2004., USGS Open-File Report 2005-1429.
%
% Program written in Matlab v7.1.0 SP3
% Program ran on PC with Windows XP Professional OS.
%
% "Although this program has been used by the USGS, no warranty, 
% expressed or implied, is made by the USGS or the United States 
% Government as to the accuracy and functioning of the program 
% and related program material nor shall the fact of distribution 
% constitute any such warranty, and no responsibility is assumed 
% by the USGS in connection therewith."
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% Dependencies:
%   julian.m
%   gmean.m
%   beam2enu.m

% C. Sullivan   06/22/05,   version 1.0
% Turn GV's and BG's script for Aquadopp into a function, and edit function
% for use with the Argonaut. Metadata is loaded by running get_meta_sontek.m
% before running this function.


more off

version = '1.0';

% metadata
cellsize = argnMeta.CellSize; %m
blanking = argnMeta.BlankDistance; %m 
serialnum = argnMeta.SerialNumber;
mount_h = userMeta.sensor_height; %m
MagDev = userMeta.magnetic_variation * pi/180; %rad
nBursts = argnMeta.Number_of_Samples;
nCells = argnMeta.Number_of_Cells;

% dimensions
argnData.nBursts = nBursts;
argnData.nCells = nCells;

% load multi-cell velocities, current speed, and current direction
% into arrays dimensioned [nBursts, nCells]
fid = fopen(argnMeta.velFile,'r');
head1 = fgetl(fid);
head2 = fgetl(fid);
data = fscanf(fid,'%f',inf);
fclose(fid);
data = reshape(data,(nCells*5)+1,nBursts);
data = data';
argnData.burst = data(:,1);
argnData.multcell.velBeam1 = data(:,2:5:(nCells*5)+1); %cm/s
argnData.multcell.velBeam2 = data(:,3:5:(nCells*5)+1); %cm/s
argnData.multcell.velBeam3 = data(:,4:5:(nCells*5)+1); %cm/s
argnData.multcell.spd = data(:,5:5:(nCells*5)+1);
argnData.multcell.dir = data(:,6:5:(nCells*5)+1);


% load multi-cell standard errors into arrays dimensioned [nBursts, nCells]
fid = fopen(argnMeta.stdFile,'r');
head1 = fgetl(fid);
head2 = fgetl(fid);
data = fscanf(fid,'%f',inf);
fclose(fid);
data = reshape(data,(nCells*3)+1,nBursts);
data = data';
argnData.multcell.stderrBeam1 = data(:,2:3:(nCells*3)+1);
argnData.multcell.stderrBeam2 = data(:,3:3:(nCells*3)+1);
argnData.multcell.stderrBeam3 = data(:,4:3:(nCells*3)+1);

% load multi-cell SNR and amplitude for each beam into arrays dimensioned
% [nBursts, nCells]
fid = fopen(argnMeta.snrFile,'r');
head1 = fgetl(fid);
head2 = fgetl(fid);
data = fscanf(fid,'%f',inf);
fclose(fid);
data = reshape(data,(nCells*6)+1,nBursts);
data = data';
argnData.multcell.snrBeam1 = data(:,2:6:(nCells*6)+1);
argnData.multcell.snrBeam2 = data(:,3:6:(nCells*6)+1);
argnData.multcell.snrBeam3 = data(:,4:6:(nCells*6)+1);
argnData.multcell.ampBeam1 = data(:,5:6:(nCells*6)+1);
argnData.multcell.ampBeam2 = data(:,6:6:(nCells*6)+1);
argnData.multcell.ampBeam3 = data(:,7:6:(nCells*6)+1);

% load time and single-cell velocities, SNRs, amplitudes, and sensor
% information. time is the time at the beginning of the burst?
data = load(argnMeta.datFile);
yyyy = data(:,1);
mm = data(:,2);
dd = data(:,3);
hr = data(:,4);
min = data(:,5);
sec = data(:,6);
argnData.snglcell.velBeam1 = data(:,7); %cm/s
argnData.snglcell.velBeam2 = data(:,8); %cm/s
argnData.snglcell.velBeam3 = data(:,9); %cm/s
argnData.snglcell.stdBeam1 = data(:,10); %cm/s
argnData.snglcell.stdBeam2 = data(:,11); %cm/s
argnData.snglcell.stdBeam3 = data(:,12); %cm/s
argnData.snglcell.snrBeam1 = data(:,13); %dB
argnData.snglcell.snrBeam2 = data(:,14); %dB
argnData.snglcell.snrBeam3 = data(:,15); %dB
argnData.snglcell.ampBeam1 = data(:,16); %counts
argnData.snglcell.ampBeam2 = data(:,17); %counts
argnData.snglcell.ampBeam3 = data(:,18); %counts
argnData.snglcell.noiseBeam1 = data(:,19); %counts
argnData.snglcell.noiseBeam2 = data(:,20); %counts
argnData.snglcell.noiseBeam3 = data(:,21); %counts
argnData.snglcell.pctGd = data(:,22);
argnData.snglcell.hdg = data(:,23);
argnData.snglcell.ptch = data(:,24);
argnData.snglcell.roll = data(:,25);
argnData.snglcell.stdhdg = data(:,26);
argnData.snglcell.stdptch = data(:,27);
argnData.snglcell.stdroll = data(:,28);
argnData.snglcell.temperature = data(:,29);
argnData.snglcell.pressure = data(:,30);
argnData.snglcell.stdpressure = data(:,31);
argnData.snglcell.batt = data(:,32);
argnData.snglcell.cellbegin = data(:,33);
argnData.snglcell.cellend = data(:,34);
argnData.snglcell.spd = data(:,35);
argnData.snglcell.dir = data(:,36);

% convert time to julian days
argnData.time = julian([yyyy mm dd hr min sec]); 
argnData.delta_t = gmean(diff(argnData.time)); %time step, julian days

% assuming time is time at the beginning of the burst, shift time
% to the time at the center of the burst
delta_t = argnData.delta_t; %profile interval, seconds
delta_t = delta_t / (3600*24); % profile interval, days
argnData.time = argnData.time + 0.5*delta_t;

% calculate the height of the center of each cell
% above the bed
argnData.multcell.cell_height = mount_h + blanking + cellsize/2 + ...
                      ([0:nCells-1])*cellsize;

% convert multi-cell velocities from BEAM to ENU (if necessary).  we use
% the single-cell heading, pitch, and roll, and GV's beam2enu.m for this
% conversion
vv1 = nan(argnData.nBursts, argnData.nCells);
vv2 = nan(argnData.nBursts, argnData.nCells);
vv3 = nan(argnData.nBursts, argnData.nCells);
if strcmp(argnMeta.CoordSystem,' BEAM')
    for ii = 1:argnData.nBursts,
        for j = 1:argnData.nCells,
            vbeam = [argnData.multcell.velBeam1(ii,j) argnData.multcell.velBeam2(ii,j) argnData.multcell.velBeam3(ii,j)];
            venu = beam2enu(vbeam,argnData.snglcell.hdg(ii),argnData.snglcell.ptch(ii),argnData.snglcell.roll(ii),0);
            vv1(ii,j) = venu(1);
            vv2(ii,j) = venu(2);
            vv3(ii,j) = venu(3);
        end
    end
elseif strcmp(argnMeta.CoordSystem,' ENU')
    vv1 = argnData.multcell.velBeam1;
    vv2 = argnData.multcell.velBeam2;
    vv3 = argnData.multcell.velBeam3;
else
    error('Data must be in either BEAM or ENU coordinates')
end

% correct multi-cell velocities for magnetic variation
East =  vv1*cos(MagDev) + vv2*sin(MagDev);
North = -vv1*sin(MagDev) + vv2*cos(MagDev);

argnData.multcell.u = East;  %Eastward velocity (cm/s)
argnData.multcell.v = North; %Northward velocity (cm/s)
argnData.multcell.w = vv3;   %Upward velocity (cm/s)

%%AAA=(a1(1:ncells,:)+a2(1:ncells,:)+a3(1:ncells,:))/3;

% convert single-cell velocities from BEAM to ENU (if necessary).  we use
% the single-cell heading, pitch, and roll, and GV's beam2enu.m for this
% conversion
vv1 = nan(argnData.nBursts, argnData.nCells);
vv2 = nan(argnData.nBursts, argnData.nCells);
vv3 = nan(argnData.nBursts, argnData.nCells);
if strcmp(argnMeta.CoordSystem,' BEAM')
    for ii = 1:argnData.nBursts,
            vbeam = [argnData.snglcell.velBeam1(ii) argnData.snglcell.velBeam2(ii) argnData.snglcell.velBeam3(ii)];
            venu = beam2enu(vbeam,argnData.snglcell.hdg(ii),argnData.snglcell.ptch(ii),argnData.snglcell.roll(ii),0);
            vv1(ii,1) = venu(1);
            vv2(ii,1) = venu(2);
            vv3(ii,1) = venu(3);
    end
elseif strcmp(argnMeta.CoordSystem,' ENU')
    vv1 = argnData.snglcell.velBeam1;
    vv2 = argnData.snglcell.velBeam2;
    vv3 = argnData.snglcell.velBeam3;
else
    error('Data must be in either BEAM or ENU coordinates')
end

% correct multi-cell velocities for magnetic variation
East =  vv1*cos(MagDev) + vv2*sin(MagDev);
North = -vv1*sin(MagDev) + vv2*cos(MagDev);

argnData.snglcell.u = East;  %Eastward velocity (cm/s)
argnData.snglcell.v = North; %Northward velocity (cm/s)
argnData.snglcell.w = vv3;   %Upward velocity (cm/s)

%%AAA=(a1(1:ncells,:)+a2(1:ncells,:)+a3(1:ncells,:))/3;