function moor2m3d(in_file,out_file)

% note: order of mooring from north to south I,F,E,B - F is tucked into
% Penobscot Bay latitudes: 44.1066, 44.055, 43.714, 43.180

% hardwire in depths of moorings to approximate averaging interval in the wc
% mooring I depth is a bit shallow - derived from stellwagon mesh

%
% GOMOOS moorings 
%
% A 43 31'40" N,70 33'59" W,65 m (43.5278)
% B 43 10'51" N,70 25'40" W, 62 m (43.1808)
% C 43 34'10" N, 70 03'18" W, 46 m (43.5694)
% E 43 42'47" N, 69 21'20" W, 100 m (43.7131)
% F 44 03'20" N, 68 59'53" W, 110 m (44.0556)
% I 44 06'21" N, 68 06'31" W, 100 m (44.1058)
%


%code=['B','C','E','F','I'];
%depth=[62,46,100,110,100];
%rlat=[43.18,43.63,43.71,44.06,44.11];
%code=['B','E','I'];
%depth=[62,100,100];
%rlat=[43.18,43.71,44.11];

code=['A','B','E','I'];
depth=[65,62,100,100];
rlat=[43.52,43.18,43.71,44.11];

m=length(rlat);

fid= fopen(in_file,'r')
data = fscanf(fid,'%f')

% mooring data comes has 9 columns:
numtimes = size(data,1)/9;
% time, lat, lon, wind speed, wind dir, taux, tauy, uavg, vavg
temp = reshape(data,[9 numtimes]);
clear data;
data = temp';
clear temp;

% find the indexes in the data file corresponding to each mooring
% use for reference to depth
j=[];
for k=1:m
    j=[j;find(data(:,2)==rlat(k))];
end

whos
data=data(j,:);
[mmm,j]=sort(data(:,1));
data=data(j,:);

day = data(:,1);
% convert time to yearday - not *completely* sure what the initial time
% base was, but this seems to work, using routine from snackbar
for i = 1:size(data,1); 
  yday(i) =data(i,1)-datenum(2004,1,1);
end
% convert lat/lon to x,y
lat = data(:,2);
lon = data(:,3);
[x y] = ll2xy(lat, lon);

% convert u,v to m/s
u = data(:,8)./100;
v = data(:,9)./100;

% initialize an array of depth, tb, and bb
d(1:size(data,1)) = 0;
tb(1:size(data,1)) = 0;
bb(1:size(data,1)) = 0;


% fill in depths, tb, bb.  Right now, we are approximating tb and bb
blanking=15;
for k=1:m
    ind=find(data(:,2)==rlat(k));
    d(ind)=depth(k);
    bb(ind)=depth(k)-blanking;
end

tb(:) = 15;

fid_out = fopen(out_file,'w')
fprintf(fid_out,'XXXX \n');
fprintf(fid_out,'stw2 \n');
fprintf(fid_out,['data_starts:',datestr(data(1,1)),' \n']);
fprintf(fid_out,'2004 \n');
fprintf(fid_out,'8 \n');
 
% need to add top bin, bottom bin, and depth
for i = 1:size(data,1);
  if ~isnan(yday(i)+x(i)+y(i)+u(i)+v(i)+tb(i)+bb(i)+d(i)),
          fprintf(fid_out,'%f %f %f %f %f %f %f %f \n', ...
          yday(i),x(i),y(i),u(i),v(i),tb(i),bb(i),d(i));
  end
end
fclose(fid)
