function [x,y,t,M] = obs2yao(lon,lat,time,t0,dt,meshfile,floatid,filename)
%[x,y,t,M] = obs2yao(lon,lat,timestep,t0,meshfile)
%Convert lon,lat,time data into timestep and gridpoint number
% for one float  
% If filename is specified, save in yao format in filename  
% Input : lon in degree longitude East
% Ouput : lat in degree
% time  : number in matlab format (see datenum and datestr)
% t0 : correspond to the first step time. Discard all time before t0
% dt : step time in day (6h=0.25). Discard all time that doesn not fit
% meshfile : file contains the meshgrid in lon,lat (e.g. enerated by make_meshgrid)
% floatid : id of the float (default is 1)
% filename (optional) : filename to save x,y,t in yao format 

  %output M : matrix in the YAO format
  
%%%%%%%%% This function takes the name of the floater from the list and
%%%%%%%%% changes it's format so it is YAO compatible.


mg=load(meshfile);
Nlat=length(unique(mg(:,2))); %Phi
Nlong=length(unique(mg(:,1))); %Lambda
GR_long=reshape(mg(:,1),Nlong,Nlat); 
GR_lat=reshape(mg(:,2),Nlong,Nlat); 

%Timestep
t=time-t0;
t=t/dt;
t=t+1;
iok=(100*round(t)==round(t*100)) & (t>0);
t=t(iok);

%x,y
x=interp1(GR_long(:,1),1:Nlong,lon(iok));
y=interp1(GR_lat(1,:),1:Nlat,lat(iok));


%%Sauvegarde
if exist('floatid')~=1
  floatid=1;
end

%On fait le format YAO :

M=nan*ones(2*length(t),4);
% Column 1 (%d) : dimension (1 : lon(Yi), 2 lat(Yj))
% Column 2 (%d) : time step (from 1 to jptfl)
% Column 3 (%d) : idfloat
% Column 4 (%f) : grid point of the floater

C1=[ones(1,length(t)) ; 2*ones(1,length(t))];
C1=C1(:);

C2=repmat(t',2,1);
C2=C2(:);

C3=floatid*ones(2*length(t),1);

C4=[x';y'];
C4=C4(:);

M=[C1 C2 C3 C4];
if exist('filename')==1
%filename a été spécifié, on sauvegarde

save(filename,'-ascii','M');

end

