% mk_mat_inj.m - make flat ascii files of EDDIES inject

%cast 3 header
%  Scan  Julian_Date Press    Temp0     Temp1     Cond0      Sal0    Cond1     Sal1    Theta   Sigma0     V0    V1    V2    V3   V4    V5  Fluoro pump1 pump_2 p1_sf6 p2_sf6

% cast 4 header
%  Scan  Julian_Date Press    Temp0     Temp1     Cond0      Sal0    Cond1     Sal1    Theta   Sigma0     V0    V1    V2    V3   V4    V5  Fluoro pump1 pump2 p1_sf6 p2_sf6 p1_spd p2_spd lat lon


cast=4;

% 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/INJ',cast_str,'/'];
mat_path=['.'];

% load in the pieces of the cast from the various files a,b,c, etc.
if cast == 3
  file_ltr = ['A','B','C','D','E','F','G','H','I','J','K','L'];
elseif cast == 4
  file_ltr = ['A','B','C','D','E'];
end

fn=['INJ_',cast_str];

data=[];
for ii = 1:length(file_ltr),
  if cast == 3
    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 == 3
  % inject cast
  ind_down = [1:1062];          % downcast
  ind_up = [40631:length(data)]; % upcast
  ind_fl = [1063:40630];
elseif cast == 4
  % inject cast
  ind_down = [1:812];          % downcast
  ind_up = [9758:length(data)]; % upcast
  ind_fl = [812:9758];
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);
v0      = data(ind_down,12);
v1      = data(ind_down,13);
v2      = data(ind_down,14);
v3      = data(ind_down,15);
v4      = data(ind_down,16);
v5      = data(ind_down,17);
%fl      = data(ind_down,18);
% used wrong formula for fl this one matches CTD data
fl      = (v5*6)-0.642;
pump_1  = data(ind_down,19);
pump_2  = data(ind_down,20);
p1_sf6  = data(ind_down,21);
p2_sf6  = data(ind_down,22);
if cast == 4
  p1_spd  = data(ind_down,23);
  p2_spd  = data(ind_down,24);
  lat     = data(ind_down,25);
  lon     = data(ind_down,26);

  eval(['save ',fn,'_down scan yearday press temp0', ...
	' temp1 cond0 sal0 cond1 sal1 theta sigma0 v0 v1 v2 v3 pump_1', ...
	' pump_2 p1_sf6 p2_sf6 v4 v5 fl p1_spd p2_spd lat lon'])

  clear scan yearday press temp0 temp1 cond0 sal0 cond1 sal1 theta ...
      sigma0 v0 v1 v2 v3 pump_1 pump_2 p1_sf6 p2_sf6 v4 v5 fl ...
      p1_spd p2_spd lat lon

else
  eval(['save ',fn,'_down scan yearday press temp0', ...
	' temp1 cond0 sal0 cond1 sal1 theta sigma0 v0 v1 v2 v3 pump_1', ...
	' pump_2 p1_sf6 p2_sf6 v4 v5 fl'])

  clear scan yearday press temp0 temp1 cond0 sal0 cond1 sal1 theta ...
      sigma0 v0 v1 v2 v3 pump_1 pump_2 p1_sf6 p2_sf6 v4 v5 fl  
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);
v2      = data(ind_up,14);
v3      = data(ind_up,15);
v4      = data(ind_up,16);
v5      = data(ind_up,17);
%fl      = data(ind_up,18);
% used wrong formula for fl this one matches CTD data
fl      = (v5*6)-0.642;
pump_1  = data(ind_up,19);
pump_2  = data(ind_up,20);
p1_sf6  = data(ind_up,21);
p2_sf6  = data(ind_up,22);

if cast == 4
  p1_spd  = data(ind_up,23);
  p2_spd  = data(ind_up,24);
  lat     = data(ind_up,25);
  lon     = data(ind_up,26);

  eval(['save ',fn,'_up scan yearday press temp0', ...
	' temp1 cond0 sal0 cond1 sal1 theta sigma0 v0 v1 v2 v3 pump_1', ...
	' pump_2 p1_sf6 p2_sf6 v4 v5 fl p1_spd p2_spd lat lon'])

  clear scan yearday press temp0 temp1 cond0 sal0 cond1 sal1 theta ...
      sigma0 v0 v1 v2 v3 pump_1 pump_2 p1_sf6 p2_sf6 v4 v5 fl ...
      p1_spd p2_spd lat lon
else
  eval(['save ',fn,'_up scan yearday press temp0', ...
	' temp1 cond0 sal0 cond1 sal1 theta sigma0 v0 v1 v2 v3 pump_1', ...
	' pump_2 p1_sf6 p2_sf6 v4 v5 fl'])
  clear scan yearday press temp0 temp1 cond0 sal0 cond1 sal1 theta ...
      sigma0 v0 v1 v2 v3 pump_1 pump_2 p1_sf6 p2_sf6 v4 v5 fl 
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);
v2      = data(ind_fl,14);
v3      = data(ind_fl,15);
v4      = data(ind_fl,16);
v5      = data(ind_fl,17);
fl      = data(ind_fl,18);
% used wrong formula for fl this one matches CTD data
fl      = (v5*6)-0.642;
pump_1  = data(ind_fl,19);
pump_2  = data(ind_fl,20);
p1_sf6  = data(ind_fl,21);
p2_sf6  = data(ind_fl,22);

if cast == 4
  p1_spd  = data(ind_fl,23);
  p2_spd  = data(ind_fl,24);
  lat     = data(ind_fl,25);
  lon     = data(ind_fl,26);

  eval(['save ',fn,'_fl scan yearday press temp0', ...
	' temp1 cond0 sal0 cond1 sal1 theta sigma0 v0 v1 v2 v3 pump_1', ...
	' pump_2 p1_sf6 p2_sf6 v4 v5 fl p1_spd p2_spd lat lon'])

  clear scan yearday press temp0 temp1 cond0 sal0 cond1 sal1 theta ...
      sigma0 v0 v1 v2 v3 pump_1 pump_2 p1_sf6 p2_sf6 v4 v5 fl ...
      p1_spd p2_spd lat lon
else
  eval(['save ',fn,'_fl scan yearday press temp0', ...
	' temp1 cond0 sal0 cond1 sal1 theta sigma0 v0 v1 v2 v3 pump_1', ...
	' pump_2 p1_sf6 p2_sf6 v4 v5 fl'])

  clear scan yearday press temp0 temp1 cond0 sal0 cond1 sal1 theta ...
      sigma0 v0 v1 v2 v3 pump_1 pump_2 p1_sf6 p2_sf6 v4 v5 fl
end

