% 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=7;

% 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-2a/oc415_2/ctd/SAM',cast_str,'/'];
mat_path=['.'];

% load in the pieces of the cast from the various files a,b,c, etc.
if cast == 1
  % cast 1 was a test sampling
  file_ltr = ['A','B'];
elseif cast == 2
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N'];
elseif cast == 3
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N'];
elseif cast == 4
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L'];
elseif cast == 5
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L'];
elseif cast == 6
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L','M'];
elseif cast == 7
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L','M'];
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),
  if cast == 1 | cast == 2   
    eval(['!tail +2 ',asc_path,fn,file_ltr(ii),'.CAL > xxx'])
    load xxx
    data = [data' xxx']';
  else
    eval(['load ',asc_path,fn,file_ltr(ii),'.CAL'])    
    eval(['temp=',fn,file_ltr(ii),';'])
    data = [data' temp']';
  end
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:2464];          % downcast
  ind_up = [3106:length(data)]; % upcast
  ind_fl = [2464:3106];
elseif cast == 2
  ind_down = [1:1646];          % downcast
  ind_up = [42765:length(data)]; % upcast
  ind_fl = [1646:42765];
elseif cast == 3
  ind_down = [1:2664];          % downcast
  ind_up = [44404:length(data)]; % upcast
  ind_fl = [3980:44404];
elseif cast == 4
  ind_down = [1:886];          % downcast
  ind_up = [42743:length(data)]; % upcast
  ind_fl = [886:42743];
elseif cast == 5
  ind_down = [1:1];          % downcast
  ind_up = [41281:length(data)]; % upcast
  ind_fl = [1:41281];
elseif cast == 6
  ind_down = [1:907];          % downcast
  ind_up = [42730:length(data)]; % upcast
  ind_fl = [907:42730];
elseif cast == 7
  ind_down = [1:1126];          % downcast
  ind_up = [42871:length(data)]; % upcast
  ind_fl = [1126:42871];
else
  disp(['set up indices for cast',cast_str])
  keyboard
end

% build three files for each cast
%% DOWN %%
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);
v4      = data(ind_down,12);
v5      = data(ind_down,13);
fl      = (v5*6)-.642;
sampl   = data(ind_down,15);
lat     = data(ind_down,16);
lon     = data(ind_down,17);

eval(['save ',fn,'_down scan yearday press temp0', ...
      ' temp1 cond0 sal0 cond1 sal1 theta sigma0 ', ...
      ' v4 v5 fl sampl lat lon'])

clear scan yearday press temp0 temp1 cond0 sal0 cond1 sal1 theta ...
    sigma0 v4 v5 fl sampl lat lon

%% 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);
v4      = data(ind_up,12);
v5      = data(ind_up,13);
fl      = (v5*6)-.642;
sampl   = data(ind_up,15);
lat     = data(ind_up,16);
lon     = data(ind_up,17);

eval(['save ',fn,'_up scan yearday press temp0', ...
      ' temp1 cond0 sal0 cond1 sal1 theta sigma0 ', ...
      ' v4 v5 fl sampl lat lon'])

clear scan yearday press temp0 temp1 cond0 sal0 cond1 sal1 theta ...
    sigma0 v4 v5 fl sampl lat lon

%% 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);
v4      = data(ind_fl,12);
v5      = data(ind_fl,13);
fl      = (v5*6)-.642;
sampl   = data(ind_fl,15);
lat     = data(ind_fl,16);
lon     = data(ind_fl,17);

eval(['save ',fn,'_fl scan yearday press temp0', ...
      ' temp1 cond0 sal0 cond1 sal1 theta sigma0 ', ...
      ' v4 v5 fl sampl lat lon'])

clear scan yearday press temp0 temp1 cond0 sal0 cond1 sal1 theta ...
    sigma0 v4 v5 fl sampl lat lon


