% mk_ascii_p17.m
%
% This version of mk_ascii.m was made specifically for creating
% ascii files of LADCP profiles from tt9305. These were requested
% by Eric Kunze.
%
% This script is run on the output of do_abs.
% For P17, you need to run read_proc.m as well to get some fields.
%
% P17N is older, narrowband LADCP data. I tried running the latest do_abs in order to
% generate individual matfiles, per norm for what we now do, but the merge files
% lack the index variables (i_day, etc.) and there are less fields. It seemed 
% time consuming to fake the variables and/or to redo the merged profiles from scratch,
% given that the do_abs output did exist, only the files contained multiple casts -
% one large matfile for each geographic section - and did not contain time.
%
% Using the 3 "b_" section files that I had previously made from the final data, I uncomment
% the appropriate load statement and execute this  script on each section: 
% ccsb.mat		LADCP stations 3-13, in the California Current
% aleutb.mat    LADCP stations 14 - 27, off the Aleutians
% sitkab.mat	LADCP stations 28 - 34, off of Sitka
%

% cast start and end time was not saved by the old do_abs. I need these times, 
% so I wrote read_proc.m to parse proc.dat and to save the time fields to proc.mat

% Run from directory proc 

%   output files names are based on LADCP cast no. with *asc extension

% TESTING, use Nans for flag and don't write the header line. Then it is a quick
% 	check to load and plot for comparison with data report.

bad_value   = 9999;   % bad data flag
%TESTING
%bad_value   = NaN;   % bad data flag, nan for me to do quick plots 
ndep        = 273;   % standard no. of 20 m bins  in files

% document output file
cruiseTitle = 'p17N';
cruiseid    = 'tt9305';
yearbase    = 1993;
ocean       = 'whp_pacific';
units       = 'cm/sec';

ladcp_dir = sprintf('./');

% station/cast list
%ladcpllt = sprintf('%slatlon.asc',ladcp_dir);
%load(ladcpllt);
%
%
%station = latlon(:,1);


% binary version of proc.dat
% contains row vectors of bottom, castno, starttime, endtime, lat, lon

load proc.mat
lon = -1*lon;   % fix the west longitudes sign here

% output from do_abs

load ccsb
%load aleutb
%load sitkab

[n_samp, ncr] = size(su_up);

% calculate the mean from the up and down casts and convert velocity to cm/s
% use logical and for the joint mask

su = 0.5 * (su_up + su_dn)*100;
sv = 0.5 * (sv_up + sv_dn)*100;
sm = sm_up & sm_dn;

d_ = d_samp';
%ddd = d_(:,ones(1,ncr));


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

su(~sm) = bad_value;
sv(~sm) = bad_value;
 
%u = reshape(su, n_samp, ncr);
%v = reshape(sv, n_samp, ncr);


for ii = 1:length(plist)
  
  jj = find (plist(ii) == castno); % jj is the index into all the proc.mat vectors

  fname = sprintf('%sascii/t%03d.asc', ...
			 ladcp_dir,castno(jj));
  fid = fopen(fname, 'w');
  u = su(:,ii);
  v = sv(:,ii);

%  cut the profile below the max depth
   d_ = d_samp';
   ibotm = find(d_ > bottom(jj));
   u(ibotm) = [];
   v(ibotm) = [];

   d_(ibotm) = [];


   con = [d_ u v];  


%TESTING
fprintf(fid,'%s %4.0f %03d %9.4f %9.4f %04d %10.6f %10.6f \n', ...
           cruiseTitle,yearbase,castno(jj), lat(jj), lon(jj), bottom(jj), starttime(jj), endtime(jj))

fprintf(fid, '%4.0f %6.1f %6.1f\n', con.');
fclose(fid);


end
