function line = extract_line_nr_na_NetCDF(data2,line)
% function to extract transect data from the survey path 
% Made specifically for the Vlugetnburg Dataset
% De Vries, De Schipper 2009-2011

%% extract line
[xgrid, ygrid] = meshgrid(line.xi,line.yi);
warning off
zgrid = griddata(data2.data(:,1),data2.data(:,2),data2.data(:,3),xgrid,ygrid);
warning on

line.zi = interp2(xgrid,ygrid,zgrid,line.xi,line.yi);

%line.surveydate=data2.surveydate;


% Problem.. script cannot extrapolate. If surveypath is southwards of
% survey line 1 it results in NaNs in the last transect. (Similar problems
% for transect 22)
% Solution: Copy the data of the last transect and add it to the survey data so it can interpolate
% M.A. de Schipper dec 2011

if line.lineno==1 | line.lineno==22
    % box around last trasect data
B1=[68075.9231747258,448364.984611428;69294.5724449348,447285.495943037;69352.2246942596,447341.160183765;68129.5994068557,448410.708809168;];
B22=[66901.5049743433,447029.817151648;68089.2657824974,445994.597053344;68152.6815832214,446040.873448466;66964.9207750673,447103.516595733;];

% data last transect
if line.lineno==1
[in] = inpolygon(data2.data(:,1),data2.data(:,2),B1(:,1),B1(:,2));
end
if line.lineno==22
[in] = inpolygon(data2.data(:,1),data2.data(:,2),B22(:,1),B22(:,2));
end
good=+in;
LastLineData=data2.data((good == 1),:); 

% shift data
shift=25; % Shift alongshore in m
angle=line.angle_surveyline+180; % Appr -41 deg wrt N
if line.lineno==1
LastLineData_trans(:,1)=LastLineData(:,1)+shift*cosd(-angle); 
LastLineData_trans(:,2)=LastLineData(:,2)+shift*sind(-angle);
end
if line.lineno==22
LastLineData_trans(:,1)=LastLineData(:,1)-shift*cosd(-angle); 
LastLineData_trans(:,2)=LastLineData(:,2)-shift*sind(-angle);
end
LastLineData_trans(:,3)=LastLineData(:,3);



% New interpolation and extraction
zgrid_old=zgrid; line.zi_old=line.zi; % store old values without new interp, just to show in the image

warning off
zgrid = griddata([data2.data(:,1) ; LastLineData_trans(:,1)],[data2.data(:,2) ; LastLineData_trans(:,2)],[data2.data(:,3) ; LastLineData_trans(:,3)],xgrid,ygrid);
warning on

line.zi = interp2(xgrid,ygrid,zgrid,line.xi,line.yi);

% plotting extra data 
if 0
    figure(66)
    subplot (121)
    plot(line.xi,line.yi.*(line.zi_old./line.zi_old),'xk' )  % good data points on transect
    hold all
    pcolor(xgrid,ygrid,zgrid_old)    % grid data
    plot(line.xi,line.yi,'.k','MarkerSize',0.5)  % transect line
     plot(data2.data(:,1),data2.data(:,2),'w')   % survey path
    plot(LastLineData_trans(:,1),LastLineData_trans(:,2),'r') % made up data for interpolation
    shading flat
    set(gcf,'renderer','zbuffer')
    xlabel('Distance from waterline [m]')
    ylabel('Elevation relative to NAP [m]')
    title('Cross section over transect xx, old interp')
    % set(gcf,'position',[233   444   879   224]);
    hold off
    axis equal
    colorbar
    
        subplot (122)
    plot(line.xi,line.yi.*(line.zi./line.zi),'xk' )  % good data points on transect
    hold all
    pcolor(xgrid,ygrid,zgrid)    % grid data
    plot(line.xi,line.yi,'.k','MarkerSize',0.5)  % transect line
     plot(data2.data(:,1),data2.data(:,2),'w')   % survey path
    plot(LastLineData_trans(:,1),LastLineData_trans(:,2),'r') % made up data for interpolation
    shading flat
    set(gcf,'renderer','zbuffer')
    xlabel('Distance from waterline [m]')
    ylabel('Elevation relative to NAP [m]')
    title('Cross section over transect xx')
    % set(gcf,'position',[233   444   879   224]);
    hold off
    axis equal
    colorbar
    
end


end





%% plotting  
if 0  % kan worden uitgeschakeld... is irritant als je veel lines extract
    figure(line.lineno)
    % subplot(2,1,1)
    hold all
    plot(line.cross_shore, line.zi,'linewidth',2);
    xlabel('Distance from waterline [m]')
    ylabel('Elevation relative to NAP [m]')
    title('Cross section over transect xx')
    % set(gcf,'position',[233   444   879   224]);
    
end
if 0
    figure
        plot(line.xi,line.yi.*(line.zi./line.zi),'xk' )  % good data points on transect
    hold all
    pcolor(xgrid,ygrid,zgrid)    % grid data
    plot(line.xi,line.yi,'.k','MarkerSize',0.5)  % transect line
     plot(data2.data(:,1),data2.data(:,2),'w')   % survey path
     shading flat
    set(gcf,'renderer','zbuffer')
    xlabel('Distance from waterline [m]')
    ylabel('Elevation relative to NAP [m]')
    title('Cross section over transect xx')
    % set(gcf,'position',[233   444   879   224]);
    hold off
    axis equal
    colorbar
end


