% mk_mat_sam.m - make flat ascii files of EDDIES sampler casts

%SAM_001 header
%  Scan  Julian_Date Press    Temp0     Temp1     Cond0      Sal0    Cond1     Sal1    Theta   Sigma0     V4    V5  Fluoro SamplerOn Lat   Lon

cast=13

% build path and file name 
cast_str=int2str(cast);
if cast < 10
  cast_str=['00',cast_str];
elseif cast < 100
  cast_str=['0',cast_str];
end

asc_path=['/mnt/oc415-4a/oc415_4/sled/SAM_',cast_str,'/'];
mat_path=['.'];

% load in the pieces of the cast from the various files a,b,c, etc.
if cast == 1
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N'];
elseif cast == 2
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L','M'];
elseif cast == 3
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L','M'];
elseif cast == 4
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N'];
elseif cast == 5
  file_ltr = ['A','B'];
elseif cast == 6
  file_ltr = ['E','F','G','H','I','J','K','L','M','N','O','P'];
elseif cast == 7
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L'];
elseif cast == 8
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L'];
elseif cast == 9
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O'];
elseif cast == 10
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L','M'];
elseif cast == 11
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N'];
elseif cast == 12
  file_ltr = ['A','B'];
elseif cast == 13
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L'];
elseif cast == 99
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L'];
end

fn=['SAM_',cast_str];

data=[];
for ii = 1:length(file_ltr),
  eval(['load ',asc_path,fn,file_ltr(ii),'.CAL'])    
  eval(['temp=',fn,file_ltr(ii),';'])
  data = [data' temp']';
end

% here want to id by hand the indices of up/down/flight
% setup info for this cast
if cast == 999
  %% sample of indexing
  ind_down = [1:1062];          % downcast
  ind_up = [40631:length(data)]; % upcast
  ind_fl = [1063:40630];
elseif cast == 1
  ind_down = [1:1667];          % downcast
  ind_up = [45020:length(data)]; % upcast
  ind_fl = [1667:45020];
elseif cast == 2
  ind_down = [1:1091];          % downcast
  ind_up = [42919:length(data)]; % upcast
  ind_fl = [1091:42919];
elseif cast == 3
  % this has a downcast to 175m
  ind_down = [1:1234];          % downcast
  ind_up = [43530:length(data)]; % upcast
  ind_fl = [1510:43530];
elseif cast == 4
  % this has a downcast to 175m
  ind_down = [1:894];          % downcast
  ind_up = [44635:length(data)]; % upcast
  ind_fl = [1265:44635];
elseif cast == 5
  ind_down = [1:534];          % downcast
  ind_up = [3804:length(data)]; % upcast
  ind_fl = [534:3804];
elseif cast == 6
  if 0
    % this is with all segments (A-P)
    ind_down = [1:1010];          % downcast
    ind_up = [42363:length(data)]; % upcast
    ind_fl = [1010:42363];
  else
    % use only E-P
    ind_down = [1:2];          % downcast
    ind_up = [40724:length(data)]; % upcast
    ind_fl = [1:40724];
  end
elseif cast == 7
  % this has a downcast to 175m
  ind_down = [1:1677];          % downcast
  ind_up = [41244:length(data)]; % upcast
  ind_fl = [1979:41244];
elseif cast == 8
  ind_down = [1:998];          % downcast
  ind_up = [40472:length(data)]; % upcast
  ind_fl = [998:40472];
elseif cast == 9
  % this has a downcast to 175m
  ind_down = [1:2022];          % downcast
  ind_up = [42596:length(data)]; % upcast
  ind_fl = [2639:42596];
elseif cast == 10
  ind_down = [1:660];          % downcast
  ind_up = [43602:length(data)]; % upcast
  ind_fl = [660:43602];
elseif cast == 11
  % this has a downcast to 175m
  ind_down = [1:1103];          % downcast
  ind_up = [44944:length(data)]; % upcast
  ind_fl = [1685:44944];
elseif cast == 12
  ind_down = [1:1567];          % downcast
  ind_up = [4437:length(data)]; % upcast
  ind_fl = [1567:4437];
elseif cast == 13
  % cast to 175m
  ind_down = [1:1110];          % downcast
  ind_up = [43408:length(data)]; % upcast
  ind_fl = [1291:43408];
else
  disp(['set up indices for cast',cast_str])
  keyboard
end

% build three files for each cast
%% DOWN %%
% Scan  Julian_Date Press    Temp0     Temp1     Cond0      Sal0
% Cond1 Sal1    Theta   Sigma0     V0    V1    V4    V5  Chelsea
% Wetlabs SamplerOn Lat   Lon

scan    = data(ind_down,1);
yearday = data(ind_down,2);
press   = data(ind_down,3);
temp0   = data(ind_down,4);
temp1   = data(ind_down,5);
cond0   = data(ind_down,6);
sal0    = data(ind_down,7);
cond1   = data(ind_down,8);
sal1    = data(ind_down,9);
theta   = data(ind_down,10);
sigma0  = data(ind_down,11);
v0      = data(ind_down,12);
v1      = data(ind_down,13);
v4      = data(ind_down,14);
v5      = data(ind_down,15);
flv     = 10.^v0; 
%aa=find(Wetl < 1);
% [p,s]=polyfit(flv(aa),Wetl(aa),2);
p = [0.1273   -0.3648    0.3207];     
fl      = p(1)*flv.^2 + p(2)*flv + p(3);
Wetl    = data(ind_down,17);
sampl   = data(ind_down,18);
lat     = data(ind_down,19);
lon     = data(ind_down,20);

if 1
  eval(['save ',fn,'_down scan yearday press temp0', ...
	' temp1 cond0 sal0 cond1 sal1 theta sigma0 ', ...
	' v0 v1 v4 v5 flv fl Wetl sampl lat lon'])
  clear scan yearday press temp0 temp1 cond0 sal0 cond1 sal1 theta ...
      sigma0 v0 v1 v4 v5 flv fl Wetl sampl lat lon
else
  error('Fix for cast >1')
end

%% UP %%

scan    = data(ind_up,1);
yearday = data(ind_up,2);
press   = data(ind_up,3);
temp0   = data(ind_up,4);
temp1   = data(ind_up,5);
cond0   = data(ind_up,6);
sal0    = data(ind_up,7);
cond1   = data(ind_up,8);
sal1    = data(ind_up,9);
theta   = data(ind_up,10);
sigma0  = data(ind_up,11);
v0      = data(ind_up,12);
v1      = data(ind_up,13);
v4      = data(ind_up,14);
v5      = data(ind_up,15);
flv     = 10.^v0; 
%aa=find(Wetl < 1);
% [p,s]=polyfit(flv(aa),Wetl(aa),2);
p = [0.1273   -0.3648    0.3207];     
fl      = p(1)*flv.^2 + p(2)*flv + p(3);
Wetl    = data(ind_up,17);
sampl   = data(ind_up,18);
lat     = data(ind_up,19);
lon     = data(ind_up,20);

if 1
  eval(['save ',fn,'_up scan yearday press temp0', ...
	' temp1 cond0 sal0 cond1 sal1 theta sigma0 ', ...
	' v0 v1 v4 v5 flv fl Wetl sampl lat lon'])
  clear scan yearday press temp0 temp1 cond0 sal0 cond1 sal1 theta ...
      sigma0 v0 v1 v4 v5 flv fl Wetl sampl lat lon
else
  error('Fix for cast >1')
end

%% Flight %%
scan    = data(ind_fl,1);
yearday = data(ind_fl,2);
press   = data(ind_fl,3);
temp0   = data(ind_fl,4);
temp1   = data(ind_fl,5);
cond0   = data(ind_fl,6);
sal0    = data(ind_fl,7);
cond1   = data(ind_fl,8);
sal1    = data(ind_fl,9);
theta   = data(ind_fl,10);
sigma0  = data(ind_fl,11);
v0      = data(ind_fl,12);
v1      = data(ind_fl,13);
v4      = data(ind_fl,14);
v5      = data(ind_fl,15);
flv     = 10.^v0; 
%aa=find(Wetl < 1);
% [p,s]=polyfit(flv(aa),Wetl(aa),2);
p = [0.1273   -0.3648    0.3207];     
fl      = p(1)*flv.^2 + p(2)*flv + p(3);
Wetl    = data(ind_fl,17);
sampl   = data(ind_fl,18);
lat     = data(ind_fl,19);
lon     = data(ind_fl,20);
if cast == 6
  % fix bad points in lat/lon - simple straight line
  lon_new = interp1([1 length(lon)],[lon(1) lon(end)],1:length(lon))';
  lat_new = interp1([1 length(lat)],[lat(1) lat(end)],1:length(lat))';
  lon=lon_new;
  lat=lat_new;
end

if 1
  eval(['save ',fn,'_fl scan yearday press temp0', ...
	' temp1 cond0 sal0 cond1 sal1 theta sigma0 ', ...
	' v0 v1 v4 v5 flv fl Wetl sampl lat lon'])
  clear scan yearday press temp0 temp1 cond0 sal0 cond1 sal1 theta ...
      sigma0 v0 v1 v4 v5 flv fl Wetl sampl lat lon
else
  error('Fix for cast >1')
end

