diff --git a/template_combine_adcp_up_and_down.m b/template_combine_adcp_up_and_down.m
index 5ba13365b94dba8f20e2852af7a6ea87374fb2fc..a6e2009851db61043f5e8d6cd1feee6365a280a6 100644
--- a/template_combine_adcp_up_and_down.m
+++ b/template_combine_adcp_up_and_down.m
@@ -10,8 +10,7 @@
 % - U and V fields interpolated on a regulard grid, filtered and subsampled
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-clear all;
-close all;
+clear all;close all;
 
 % path
 addpath('.\moored_adcp_proc');
@@ -122,4 +121,30 @@ print(hf,graph_name,'-dpdf','-r300');
 
 % rmpath
 rmpath('.\moored_adcp_proc');
-clear all; close all;
+
+%%  Write netcdf file           
+[yr_start , ~, ~] = gregorian(down_time(1));
+[yr_end,  ~, ~] = gregorian(down_time(length(down_time)));
+
+ncid=netcdf.create([fpath_output,'ADCP_',mooring.name, '_',num2str(yr_start),'_',num2str(yr_end),'_1d.nc'],'NC_WRITE');
+ 
+%create dimension
+dimidt = netcdf.defDim(ncid,'time',length(down_time));
+dimidz = netcdf.defDim(ncid,'depth',length(z_final));
+%Define IDs for the dimension variables (pressure,time,latitude,...)
+time_ID=netcdf.defVar(ncid,'time','double',dimidt);
+depth_ID=netcdf.defVar(ncid,'depth','double',dimidz);
+%Define the main variable ()
+u_ID = netcdf.defVar(ncid,'u','double',[dimidt dimidz]);
+v_ID = netcdf.defVar(ncid,'v','double',[dimidt dimidz]);
+%We are done defining the NetCdf
+netcdf.endDef(ncid);
+%Then store the dimension variables in
+netcdf.putVar(ncid,time_ID,down_time);
+netcdf.putVar(ncid,depth_ID,z_final);  
+%Then store my main variable
+netcdf.putVar(ncid,u_ID,u_final);
+netcdf.putVar(ncid,v_ID,v_final);
+%We're done, close the netcdf
+netcdf.close(ncid);
+
diff --git a/template_get_adcp_data.m b/template_get_adcp_data.m
index be847873e810a2c0f1706b9a8a29f467ce0270bb..26376b6df8b5b325ae6109b8e58f62181e925a41 100644
--- a/template_get_adcp_data.m
+++ b/template_get_adcp_data.m
@@ -8,14 +8,17 @@
 % OUTPUTS:
 % - U and V fields interpolated on a regulard grid, filtered and subsampled
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+% First part --------------------------------------------------------------------------------------------------------------------
 close all
 clear all
  
-% path
-addpath('.\moored_adcp_proc');
-
 %% META information:
 
+% Path
+addpath('.\moored_adcp_proc'); % ou par exemple ('C:\Users\IRD_US_IMAGO\TRAITEMENTS\ADCP_MOUILLAGE\01_DATA_PROCESSING\moored_adcp_proc');
+addpath('.\backscatter'); % (Optionnel) / ou par exemple ('C:\Users\IRD_US_IMAGO\TRAITEMENTS\ADCP_MOUILLAGE\01_DATA_PROCESSING\backscatter');
+
 % Location rawfile
 fpath = '';
 rawfile='.\data_example\FR24_000.000'; % binary file with .000 extension
@@ -23,47 +26,57 @@ rawfile='.\data_example\FR24_000.000'; % binary file with .000 extension
 % Directory for outputs
 fpath_output = '.\data_example\';
 
-cruise.name = '';
-mooring.name='10W0N';
-mooring.lat=00+00/60; %latitude
-mooring.lon=-10+00/60; %longitude
+% Cruise/mooring info
+cruise.name = 'Cruise Name';
+mooring.name='Lat Lon'; % 0N10W par exemple
+mooring.lat=00+00/60; %latitude en degrés décimaux
+mooring.lon=-10+00/60; %longitude en degrés décimaux
 
+% ADCP info
 adcp.sn=15258;
-adcp.type='150 khz Quartermaster';
+adcp.type='150 khz Quartermaster'; % Type : ‘Quartermaster’, ‘longranger’
 adcp.direction='up';        % upward-looking 'up', downward-looking 'dn'
-adcp.instr_depth=178;       % nominal instrument depth
-
+adcp.instr_depth=280;       % nominal instrument depth
 instr=1;                    % this is just for name convention and sorting of all mooring instruments
 
 % If ADCP was not set up to correct for magnetic deviation internally
 % ("EA0" code in configuration file), use http://www.ngdc.noaa.gov/geomag-web/#declination
 % Magnetic deviation: Mean of deviations at time of deployment and time of recovery 
 
-% magnetic deviation values
-magnetic_deviation_ini = 15.11;
-magnetic_deviation_end = 15.01;
+% Magnetic deviation values
+magnetic_deviation_ini = 9.29;
+magnetic_deviation_end = 9.05;
 rot=-(magnetic_deviation_ini+magnetic_deviation_end)/2;  
 
-% determine first and last indiced when instrument was at depth (you can do this by plotting 'raw.pressure' for example           
-first = 8; 
-last = 7963; 
+% Read rawfile
+fprintf('Read %s\n', rawfile);
+raw=read_os3(rawfile,'all');
+figure;plot(raw.pressure);set(gca,'ydir','reverse');
+title('pressure sensor');ylabel('Depth(m)');xlabel('Time index');grid on; 
+saveas(gcf,[fpath_output,mooring.name,'_',num2str(adcp.sn),'_instr_',num2str(instr),'_','Pressure_sensor'],'fig')
+
+% Second part --------------------------------------------------------------------------------------------------------------------
+
+% Determine first and last indiced when instrument was at depth (you can do this by plotting 'raw.pressure' for example           
+first = 12; 
+last = 17620; 
+
+% amplitude of the bins / Correction ADCP's depth
+ea = squeeze(mean(raw.amp(:,:,first:last),2));   
+figure; imagesc(ea);title('Amplitude of the bins'); colorbar;
+ylabel('Bins');xlabel('Time index'); 
+saveas(gcf,[fpath_output,mooring.name,'_',num2str(adcp.sn),'_instr_',num2str(instr),'_','Amplitude_bins'],'fig')
+
+% Third part --------------------------------------------------------------------------------------------------------------------
 
 % If upward looking: range of surface bins used for instrument depth correction below!
-sbins= 17:28; % here a range of bins is given which cover the surface reflection
+sbins= 31:38; % here a range of bins is given which cover the surface reflection
 
 % Exclude data with percent good below prct_good
 prct_good = 20;
 
-% Read rawfile
-fprintf('Read %s\n', rawfile);
-raw=read_os3(rawfile,'all');
-figure;plot(raw.pressure);set(gca,'ydir','reverse');
- 
-freq = raw.config.sysconfig.frequency;
-
 %% Read data
-ea = squeeze(mean(raw.amp(:,:,first:last),2));  % amplitude of the bins 
-figure; imagesc(ea);title('Amplitude of the bins'); colorbar;
+freq = raw.config.sysconfig.frequency;
 
 u2 = squeeze(raw.vel(:,1,first:last));
 v2 = squeeze(raw.vel(:,2,first:last));
@@ -101,12 +114,16 @@ binmat = repmat((1:nbin)',1,length(dpt1));
 % surface reflection, which is done in adcp_surface_fit
 if strcmp(adcp.direction,'up')  
     [z,dpt1,offset,xnull]=adcp_surface_fit(dpt,ea,sbins,blen,blnk,nbin);
-elseif strcmp(direction,'dn')
+elseif strcmp(adcp.direction,'dn')
     z = dpt1+(binmat-0.5)*blen+blnk;
 else
     error('Bin depth calculation: unknown direction!');
 end
 
+saveas(figure(1),[fpath_output,mooring.name,'_',num2str(adcp.sn),'_instr_',num2str(instr),'_','Hist_diff_orig-depth_recon-depth'],'fig')
+saveas(figure(2),[fpath_output,mooring.name,'_',num2str(adcp.sn),'_instr_',num2str(instr),'_','Offset_depth'],'fig')
+saveas(figure(3),[fpath_output,mooring.name,'_',num2str(adcp.sn),'_instr_',num2str(instr),'_','Amplitude_bins_2'],'fig')
+
 %% Remove bad data if ADCP is looking upward
 u1=u; v1=v; w1=w; vel_err1=vel_err; ea1=ea;
 
@@ -115,7 +132,7 @@ if strcmp(adcp.direction,'up')
         sz_dpt(i)=adcp_shadowzone(dpt(i),raw.config.sysconfig.angle); % depending on the instrument depth and the beam angle the shadow zone, i.e. the depth below the surface which is contaminated by the surface reflection is determined
 
         iz(i)=find(z(:,i)>sz_dpt(i),1,'last');
-        sbin(i)=bin(iz(i))-1;
+        sbin(i)=bin(iz(i));
         
         % here a manual criterion should be hard-coded if
         % adcp_check_surface (below) shows bad velocities close to the
@@ -138,6 +155,7 @@ if strcmp(adcp.direction,'up')
         % here the closest bins below the surface are plotted that are supposed to have good velocities, if there are still bad velocities a manual criterion needs to be found
     end
 end
+saveas(figure(4),[fpath_output,mooring.name,'_',num2str(adcp.sn),'_instr_',num2str(instr),'_','Meridional_zonal_velocity'],'fig')
 
 %% SAVE DATA
 % More meta information
@@ -180,11 +198,14 @@ end
   
 %% Horizontal interpolation, filtering and subsampling
 [uintfilt,vintfilt,inttim] = adcp_filt_sub(data,u_interp',v_interp',1:length(Z),40);
+saveas(figure(5),[fpath_output,mooring.name,'_',num2str(adcp.sn),'_instr_',num2str(instr),'_','data_raw_filt_subsampled_1'],'fig')
+saveas(figure(6),[fpath_output,mooring.name,'_',num2str(adcp.sn),'_instr_',num2str(instr),'_','data_raw_filt_subsampled_2'],'fig')
 
 % Save interpolated data
-data.uintfilt=uintfilt(1:length(Z),:);
-data.vintfilt=vintfilt(1:length(Z),:);
-data.Z = Z(1:length(Z));
+bin_start = 1; % bin indice where good interpolated data for the whole dataset start
+data.uintfilt=uintfilt(bin_start:length(Z),:);
+data.vintfilt=vintfilt(bin_start:length(Z),:);
+data.Z = Z(bin_start:length(Z));
 data.inttim = inttim;
 save([fpath_output, mooring.name '_' num2str(adcp.sn) '_instr_' sprintf('%02d',instr) '_int_filt_sub.mat'],'adcp','mooring','data','raw');
 
@@ -195,7 +216,7 @@ niv_v = (-0.5:0.1:0.5);
 hf=figure('position', [0, 0, 1400, 1000]);
 %u
 subplot(2,1,1);
-[C,h] = contourf(inttim,Z(1:length(Z)),uintfilt(1:length(Z),:),niv_u); 
+[C,h] = contourf(inttim,Z(bin_start:length(Z)),uintfilt(bin_start:length(Z),:),niv_u); 
 set(h,'LineColor','none');
 caxis(niv_u([1 end]));
 h=colorbar;
@@ -209,7 +230,7 @@ title({[mooring.name, ' - MERIDIONAL VELOCITY - RDI ',num2str(freq),' kHz']});
 
 %v
 subplot(2,1,2);
-[C,h] = contourf(inttim,Z(1:length(Z)),vintfilt(1:length(Z),:),niv_v); 
+[C,h] = contourf(inttim,Z(bin_start:length(Z)),vintfilt(bin_start:length(Z),:),niv_v); 
 set(h,'LineColor','none');
 caxis(niv_v([1 end]));
 h=colorbar;
@@ -254,5 +275,7 @@ netcdf.putVar(ncid,v_ID,vintfilt);
 netcdf.close(ncid);
 
 % rmpath
-rmpath('..\moored_adcp_proc');
-clear all; close all;
\ No newline at end of file
+% rmpath('..\moored_adcp_proc');
+clear all; close all;
+
+% -------------------------------------------------------------------------------------------
\ No newline at end of file