% mk_ascii_kunze.m
%
% This version of mk_ascii.m was made specifically for creating
% ascii files of LADCP profiles from kn9511. These were requested
% by Eric Kunze.
%
% This script is run on the output of do_abs.
% Run from directory proc 
% Edit ladcp_prof (*mat data names) as needed; output
%   file has the same name with *asc extension

bad_value   = 9999;   % bad data flag
ndep        = 323;   % standard no. of 20 m bins   

% document output file
cruiseTitle = 'I10';
cruiseid    = 'kn9511';
yearbase    = 1995;
ocean       = 'whp_indian';
units       = 'cm/sec';

ladcp_dir = sprintf('./');

% station/cast list

ladcpllt = sprintf('%slatlon.asc',ladcp_dir);
load(ladcpllt);					

[nr nc] = size(latlon);
station = latlon(:,1);
lon     = latlon(:,3);
lat     = latlon(:,2);
bottom  = latlon(:,4);


for  ii = 1:nr				% for each profile ....

% get lowered cast
 
   ladcp_prof = sprintf('%smatprof/k%04d', ...
                             ladcp_dir,station(ii));
   load(ladcp_prof) 

% open output

  fname = sprintf('%sascii/k%04d.asc', ...
			 ladcp_dir,station(ii)); 
  fid = fopen(fname, 'w');

% convert velocity to cm/s 

   su = su_mn_i * 100;
   sv = sv_mn_i * 100;

%  apply quality mask; set all bad/missing values to bad_value 
%  cut the profile below the max depth


   su(~sm_mn_i) = bad_value;
   sv(~sm_mn_i) = bad_value;

   ibotm = find(d_samp > bottom(ii));
   su(ibotm) = [];
   sv(ibotm) = [];
   d_ = d_samp;
   d_(ibotm) = [];



   con = [d_ su sv];   
   

fprintf(fid,'%s %4.0f %03d %9.4f %9.4f %04d %10.6f %10.6f \n', ...
            cruiseTitle,yearbase,latlon(ii,:),txy_start_end(:,1));
fprintf(fid, '%4.0f %6.1f %6.1f\n', con.');
fclose(fid);

end  % of for ii=1:nr





