%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%                    LATEXtools                         %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  This  file  is  free software ;  it  is distributed  in  %
%%  the hope that  it  will  be  useful,  but  without  any  %
%%  warranty.  You  can  redistribute  it and/or modify  it  %
%%  under  the  terms  of  the  GNU  General Public License  %
%%  as  published  by  the   Free  Software  Foundation  at  %
%%       http://www.gnu.org/copyleft/gpl.html                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% function [lon,lat]=plot_JASON2
%%
%% Plots tracks from JASON
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 24/10/15 AD: adapted for both High resolution and low resolution input files
% 26/10/15 LR: louise.rousselet@mio.osupytheas.fr : creation 
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [lon,lat]=plot_JASON2(file_name,lonmin,lonmax,latmin,latmax,color)

track=load(['JASON2/',file_name]);
[nline,nrow]=size(file_name)
if nrow > 20
 tracename=file_name(12:14) 
else
 tracename=file_name(end-6:end-4) 
end
    
	%write track name
	%track_nb=char(file_name);
	id=find(track(:,1)<lonmax & track(:,1)>lonmin+5/60 & track(:,2)<latmax & track(:,2)>latmin+5/60); %position to write name in the window

	%id_valeurs=find(track(:,4)==23); %id_valeurs(1) is the first position where we have the max current values (23)

	m_plot(track(:,1),track(:,2),color,'Linewidth',2);
	%m_plot(track(id_valeurs(1):end,1),track(id_valeurs(1):end,2),'b-','Linewidth',1);

	if (isempty(id)==0)
        m_text(track(id(1),1),track(id(1),2),['  JASON2 ',tracename],'color',color(1));
	end
return

%%%%%%%%%%%%%OLD VERSION%%%%%%%%%%%%

	fid=fopen(file_name);
	data = textscan(fid,'%f %f %*[^\n]','delimiter',' ','HeaderLines',1);
	lon_j=data{1};
	lat_j=data{2};
    
    s_idx=strfind(file_name,'T'); % find start of trace number
    e_idx=strfind(file_name,'_'); % find start of trace number
    trace=file_name(s_idx+1:e_idx(2)-1); 
    
    id=find(lon_j<lonmax & lon_j>lonmin & lat_j<latmax & lat_j>latmin); %positions that fall within the window

	m_plot(lon_j,lat_j,'color',[0 0.5 0],'linestyle','--')	
    
    if (isempty(id)==0)
        m_text(lon_j(id(3))-1/60,lat_j(id(3))-2/60,['JASON2 ',trace],'color',[0 0.5 0],'horizontalalignment','right');
	end

return
