
mydir = '\\atlas\WaveGlider\deployment_data\wgTiny\';
d = dir([mydir '201*']);

dist = NaN(size(d));
distOld = NaN(size(d));
time = NaN(size(d));
for n=1:length(d)
 %new method
  myfile = [mydir d(n).name '\QC\Position.csv'];
  if exist(myfile)
    pos = csvread(myfile,1,0);
    pos(pos==0) = NaN;
    f = find(~isnan(pos(:,2)));
    [x,y] = lonlat2km(pos(f(1),2),pos(f(1),3),pos(:,2),pos(:,3));
    dist(n) = nansum( sqrt( (diff(x).^2) + (diff(y).^2) ) );
    time(n) = diff(pos([1 end],1)) / 86400 / 1000;
%     figure; plot(pos(:,1),'.-'); title(d(n).name);
    clear pos f x y;
  else
    d2 = dir([mydir d(n).name '\*QC*.mat']);
    if length(d2)==1
      load([mydir d(n).name '\' d2.name]);
      f = find(~isnan(glider.lon));
      [x,y] = lonlat2km(glider.lon(f(1)),glider.lat(f(1)),glider.lon,glider.lat);
      dist(n) = nansum( sqrt( (diff(x).^2) + (diff(y).^2) ) );
      time(n) = diff(glider.time([1 end]));
%       figure; plot(glider.time,'.-'); title(d(n).name);
    end
    clear d2 f x y glider*;
  end
  clear myfile;
 %old method
  myfile = [mydir d(n).name '\Distance.csv'];
  if exist(myfile)
    dis = csvread(myfile,1,0);
    distOld(n) = sum(dis(:,2))/1000; %m to km
  end
 %print stats
  [d(n).name ' ' num2str(dist(n)) ' ' num2str(time(n))]
end

% %manual correction for first two shake-out missions
% % (see readFetchOld_posOnly.m)
%  dist(1) = 215.395;
%  time(1) = 9.7343;
% May as well continue to not include missions w no sensor data

%all missions
 distAll = nansum(dist);
 timeAll = nansum(time);

%by year
 f14 = [];
 f15 = [];
 f16 = [];
 f17 = [];
 for n=1:length(d)
   if ~isempty(strfind(d(n).name,'2014'))
     f14 = [f14 n];
   elseif ~isempty(strfind(d(n).name,'2015'))
     f15 = [f15 n];
   elseif ~isempty(strfind(d(n).name,'2016'))
     f16 = [f16 n];
   elseif ~isempty(strfind(d(n).name,'2017'))
     f17 = [f17 n];
   end
 end
 clear n;
 
 yr2014 = [nansum(dist(f14)); nansum(time(f14))];
 yr2015 = [nansum(dist(f15)); nansum(time(f15))];
 yr2016 = [nansum(dist(f16)); nansum(time(f16))];
 yr2017 = [nansum(dist(f17)); nansum(time(f17))];
 
 