% Program to look at the Sensor data from the ADV. (Tilt and Compass)
% David S. Ciochetto
% 7 Sep 2005 (Last Modified)
%
% This program is to study the sampling sled flight characteristics.
%
% USES:
%   paper.m, ooz.m, 


%% Set up the work environment
clear all; close all; paper;


%% Global analysis info
% List of cast to include.
casts2do = [3:7];
% Deployment Idnetifier string input to Vector program.
depid = 'Bermud';


%% 1. Noise Characteristics from 1st sample of each burst.
% To answer the question, is the measuring environment too noisy?

%% Load the Data
ext = '.vhd';     % Data file extension i.e. '.vhd', '.dat' or '.sen'
for i=casts2do
  switch ext
  case '.vhd', idstr = 'N';
  case '.sen', idstr = 'S';
  case '.dat', idstr = 'D';
  end
  eval([idstr ooz(i,2) ' = load(''' depid ooz(i,2) ext ''');']);
end
clear i idstr

% Collect the Noise Data
ND = [];
for i=casts2do
  eval(['ND = [ND; N' ooz(i,2) '(:,9:14)];']);
  eval(['clear N' ooz(i,2)])
end
clear i

% Plot the data
% # Bin centers for noise correlation
Xc = [min(min(ND(:,4:6))):max(max(ND(:,4:6)))-1]+0.5;
% # Bin centers for noise amplitude
Xa = [min(min(ND(:,1:3))):max(max(ND(:,1:3)))-1]+0.5;
% Plot Histograms
subplot 321, hist(ND(:,1),Xa)
set(gca,'fontsize',14); xlabel('Beam 1')
title('Noise Amplitude [counts]')
subplot 322, hist(ND(:,4),Xc)
set(gca,'fontsize',14); xlabel('Beam 1')
title(' Noise Correlation [%]')
subplot 323, hist(ND(:,2),Xa)
set(gca,'fontsize',14); xlabel('Beam 2')
subplot 324, hist(ND(:,5),Xc)
set(gca,'fontsize',14); xlabel('Beam 2')
subplot 325, hist(ND(:,3),Xa)
set(gca,'fontsize',14); xlabel('Beam 3')
subplot 326, hist(ND(:,6),Xc)
set(gca,'fontsize',14); xlabel('Beam 3')
figure %pause

% Clean up.
clear ND Xa Xc ext


%% 2. Sensor Data - How noisy do the tilt and compass say the sled is?
% Be careful, can include pressure data but it is taken at the set sample rate
% and not 1 Hz like the tilt and compass.

% Load the sensor data (all 1 Hz)
ext = '.sen';     % Data file extension i.e. '.vhd', '.dat' or '.sen'
for i=casts2do
  switch ext
  case '.vhd', idstr = 'N';
  case '.sen', idstr = 'S';
  case '.dat', idstr = 'D';
  end
  eval([idstr ooz(i,2) ' = load(''' depid ooz(i,2) ext ''');']);
end
clear i idstr

% Collect the Sensor Data
SD = [];
for i=casts2do
  % Just lump all of the data together first, strip out columns later.
  eval(['SD = [SD; S' ooz(i,2) '(:,:)];']);
  % Add a row of NaN to delineate between casts.
  SD = [SD; NaN*ones(size(SD(end,:)))];
  eval(['clear S' ooz(i,2)])
end
clear i

% Look at the voltage to see how it is dropping.
% Q: do I have enough juice? Consult the manual
% From the vector manual, a new battery has 13.5 VDC with a capacity of 50 Wh.
% Typical consumption is 0.6 - 1 W at 4 Hz. It claims that the battery pack
% enables deployments of up to a year. It does not say how to tell when the voltage
% is too low or the battery is going bad.
% Make a time vector in yearday.
D = yearday1(2005,SD(:,3),SD(:,1),SD(:,2),SD(:,4),SD(:,5),SD(:,6));
% plot(D,SD(:,9),'.')
% figure

% % Any errors (sensor file 7, 8, 13)
% ER1 = sum(SD(:,7))
% ER2 = sum(diff(SD(:,8)))
% ER3 = sum(SD(:,16))

% Trim the data down to the sensor data for space. So 2 3 4 will be heading, pitch and roll.
SD = [D SD(:,11:13)];
clear D

% % Heading
% subplot 311
% plot(SD(:,1),SD(:,2),'.:')
% set(gca,'fontsize',14,'ylim',[0 360],'fontname','times')
% %xlabel('Elapsed YearDay')
% ylabel('Heading [deg]')
% 
% %The manual claims a max tilt of 30 deg.
% % Pitch
% subplot 312
% plot(SD(:,1),SD(:,3),'.:')
% set(gca,'fontsize',14,'fontname','times');  
% set(gca,'ylim',[-30 30]);
% %xlabel('Elapsed YearDay')
% ylabel('Pitch [deg]')
% zoom on
% 
% % Roll
% subplot 313
% plot(SD(:,1),SD(:,4),'.:')
% set(gca,'fontsize',14,'fontname','times'); 
% set(gca,'ylim',[-30 30]);
% xlabel('Elapsed YearDay')
% ylabel('Roll [deg]')
% linkedzoom onx
% figure
% 
% % Roll and pitch histograms
% subplot 121
% hist(SD(:,3),100)
% set(gca,'fontsize',14,'fontname','times');
% set(gca,'xlim',[-30 30]);
% % XL = get(gca, 'xlim');
% % if XL(1) < -30
% %   XL(1) = -30;
% % end
% % if XL(2) > 30
% %   XL(2) = 30
% % end
% % set(gca,'xlim',XL); clear XL
% xlabel('Pitch [deg]')
% orient landscape
% 
% subplot 122
% hist(SD(:,4),100)
% set(gca,'fontsize',14,'fontname','times');
% set(gca,'xlim',[-30 30]);
% xlabel('Pitch [deg]')
% figure

% % Power Spectra by cast
% % Casts 3 - 7 have 3595 samples per burst. I can use that to break them up into bursts and
% % use the bursts for ensembles in multispec.
% if casts2do == [3:7]
%   DAT = reshape(SD(:,2),3595,
% end