%% Script used to load data from Pierre-Marie Poulain and save it in the YAO format


%% Parametrization of the script
start_time = '20130827T130000'; 
end_time='20130915T190000'; 

%directory/files containing data
indir='../altifloat/';
filename='*.mat';

%directory/file to save the obs file
outdir='../obs_float';
fout=['obs_altifloat_all_' start_time '_' end_time '.dat'];

%step time in day (6h=0?25). Discard all time that doesn not fit
dt=0.25

%meshfile to interpolate
meshfile = '../obs_float/meshgrid3.dat';
%% THE SCRIPT - NORMALLY NOT TO BE MODIFIED

stime_num=datenum(start_time,'yyyymmddTHHMMSS');
etime_num=datenum(end_time,'yyyymmddTHHMMSS');

id=1;

MM=[]; %output matrix to be saved

Fdir=dir([indir filename]);

for j=1:length(Fdir)
  
  load([indir Fdir(j).name]);
  
  %Prendre à Epsilon près
  I=find(time>=stime_num & time<=etime_num);
 
  if (length(I)>2) %%at least two positions of the drifter
    fprintf(1,'Drifter #%d starting at %s: ',id,datestr(time(1)));
    [x,y,t,M] = obs2yao(lon,lat,time,stime_num,dt,meshfile,id);
    fprintf(1,'%d position selected\n',length(t));
    if ~isempty(M)
      id=id+1;
      MM=[MM;M]; %à arrondir    
    end
  end %length(I)>2
  
end %for j

save([outdir fout],'-ascii','MM');