
% wgSparky was flown near NDBC buoy to compare wind data

 atlas = '//atlas.shore.mbari.org/';
 mydir1 = [atlas 'jsevadjian/Waveglider/compareWinds_wgSparky_46042/'];

%load NDBC data
 fmt = [repmat('%d ',1,6) ' %f %d %f %d'];
 d = dir([mydir1 '46042_*.txt']);
 ndbcData = [];
 for n=1:length(d)
   fid = fopen([mydir1 '46042_Jun2016.txt']);
   fgetl(fid);
   fgetl(fid);
   data = fscanf(fid, fmt)';
   ndbcData = [ndbcData; reshape(data,length(data)/10,10)];
   fclose(fid);
   clear fid data ans;
 end
 clear fmt d;

%define vehicle, deployment, data directory
 veh = 'wgSparky';
 dep = '20160630';
 mydir = ['//atlas.shore.mbari.org/WaveGlider/deployment_data/' ...
    veh '/' dep '/realTime/'];

%read-in .csv files
 ctd1 = csvread([mydir 'CTD1.csv'],1,0);
 ctd2 = csvread([mydir 'CTD2.csv'],1,0);
 met = csvread([mydir 'MET.csv'],1,0);
 metw = csvread([mydir 'METwind.csv'],1,0);
 pos = csvread([mydir 'Position.csv'],1,0);

%replace zeros w/ NaNs
 ctd1(ctd1==0) = NaN;
 ctd2(ctd2==0) = NaN;
 met(met==0) = NaN;
 metw(metw==0) = NaN;
 pos(pos==0) = NaN;

%convert time in Unix to Matlab datenum
 dn = datenum([1970 1 1]);
 ctd1(:,1) = dn + ctd1(:,1)/86400/1000;
 ctd2(:,1) = dn + ctd2(:,1)/86400/1000;
 met(:,1) = dn + met(:,1)/86400/1000;
 metw(:,1) = dn + metw(:,1)/86400/1000;
 pos(:,1) = dn + pos(:,1)/86400/1000;
 clear dn;

%interpolate to get lon/lat for each sensor
 ctd1(:,5) = interp1(pos(:,1),pos(:,2),ctd1(:,1));
 ctd1(:,6) = interp1(pos(:,1),pos(:,3),ctd1(:,1));
 ctd2(:,5) = interp1(pos(:,1),pos(:,2),ctd2(:,1));
 ctd2(:,6) = interp1(pos(:,1),pos(:,3),ctd2(:,1));
 met(:,end+1) = interp1(pos(:,1),pos(:,2),met(:,1));
 met(:,end+1) = interp1(pos(:,1),pos(:,3),met(:,1));
 metw(:,end+1) = interp1(pos(:,1),pos(:,2),metw(:,1));
 metw(:,end+1) = interp1(pos(:,1),pos(:,3),metw(:,1));

%buoy 46042 location
 ndbcLon = -122.452;
 ndbcLat = 36.791;
 
%plot
 coast=dlmread([atlas 'jsevadjian/GIS/montereyBay.txt']);
 figure; hold on;
 plot(coast(:,1),coast(:,2),'k');
 plot(pos(:,2),pos(:,3),'.-');
 plot(ndbcLon,ndbcLat,'g.');

%index 46042 data within 4 km, 10-60 min
 tolSp = 4; %km
 %metw
  [ia,ib,indBad] = getIndexNear(metw(:,1), metw(:,4), metw(:,5),...
    ndbcTime, ndbcLon, ndbcLat, tolSp, 10/1440);
  CTD(:,7:8) = NaN(size(CTD,1),2); %T, S
  CTD(ia,7:8) = [M1ctd.temp(ib(ia)) M1ctd.sal(ib(ia))];
  CTD(indBad,7:8) = NaN;

