% mk_ascii_kunze.m
%
% This version of mk_ascii.m was made specifically for creating
% ascii files of LADCP profiles from kn9509. 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 = 'AUCE';
cruiseid    = 'me0302';
yearbase    = 2003;
ocean       = 'indian';
units       = 'cm/sec';

ladcp_dir = sprintf('./');

% station/cast list

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

[nr nc] = size(latlon);
station = fix(latlon(:,1));
castno  = round((latlon(:,1)-station)*100);
lon     = latlon(:,3);
lat     = latlon(:,2);
bottom  = latlon(:,4);


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

% get lowered cast

   ladcp_prof = sprintf('%smatprof/h/m%03d%s%02d', ...
                             ladcp_dir,station(ii),'_',castno(ii))
   
   ladcp_id = sprintf('%03d%s%02d',station(ii),'_',castno(ii))
   
   load(ladcp_prof) 

% open output
  fname = [ladcp_dir,'ascii/m',ladcp_id,'.asc']


  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 %s %9.4f %9.4f %04d %10.6f %10.6f \n', ...
            cruiseTitle,yearbase,...,
            ladcp_id, latlon(ii,2:4),txy_start_end(:,1));
fprintf(fid, '%4.0f %6.1f %6.1f\n', con.');
fclose(fid);

end  % of for ii=1:nr













